1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <rte_cryptodev.h>
10 #include <rte_malloc.h>
12 #include "fips_validation.h"
14 #define NEW_LINE_STR "#"
17 #define PARAM_PREFIX "["
18 #define KEYLEN_STR "Keylen = "
19 #define IVLEN_STR "IVlen = "
20 #define PTLEN_STR "PTlen = "
21 #define AADLEN_STR "AADlen = "
22 #define TAGLEN_STR "Taglen = "
24 #define COUNT_STR "Count = "
25 #define KEY_STR "Key = "
26 #define IV_STR "IV = "
27 #define PT_STR "PT = "
28 #define CT_STR "CT = "
29 #define TAG_STR "Tag = "
30 #define AAD_STR "AAD = "
32 #define OP_ENC_STR "Encrypt"
33 #define OP_DEC_STR "Decrypt"
34 /* External/Internal IV generation, specified in file name, following NIST
37 #define OP_ENC_EXT_STR "ExtIV"
38 #define OP_ENC_INT_STR "IntIV"
40 #define NEG_TEST_STR "FAIL"
43 * GMAC is essentially zero length plaintext and uses AAD as input data.
44 * NIST does not have GMAC specific test vector but using zero length "PTlen"
45 * and uses AAD as input.
48 parser_read_gcm_pt_len(const char *key, char *src,
49 __rte_unused struct fips_val *val)
51 int ret = parser_read_uint32_bit_val(key, src, &vec.pt);
56 if (vec.pt.len == 0) {
57 info.interim_info.gcm_data.is_gmac = 1;
58 test_ops.prepare_op = prepare_auth_op;
59 test_ops.prepare_xform = prepare_gmac_xform;
61 info.interim_info.gcm_data.is_gmac = 0;
62 test_ops.prepare_op = prepare_aead_op;
63 test_ops.prepare_xform = prepare_gcm_xform;
70 parse_gcm_aad_str(const char *key, char *src,
71 __rte_unused struct fips_val *val)
73 /* For GMAC test vector, AAD is treated as input */
74 if (info.interim_info.gcm_data.is_gmac) {
75 vec.pt.len = vec.aead.aad.len;
76 return parse_uint8_known_len_hex_str(key, src, &vec.pt);
78 return parse_uint8_known_len_hex_str(key, src, &vec.aead.aad);
82 parse_gcm_pt_ct_str(const char *key, char *src, struct fips_val *val)
84 /* According to NIST GCMVS section 6.1, IUT should generate IV data */
85 if (info.interim_info.gcm_data.gen_iv && vec.iv.len) {
89 vec.iv.val = rte_malloc(0, vec.iv.len, 0);
94 for (i = 0; i < vec.iv.len; i++) {
96 vec.iv.val[i] = (uint8_t)random;
100 /* if PTlen == 0, pt or ct will be handled by AAD later */
101 if (info.interim_info.gcm_data.is_gmac)
104 return parse_uint8_known_len_hex_str(key, src, val);
107 struct fips_test_callback gcm_dec_vectors[] = {
108 {KEY_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
109 {IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
110 {CT_STR, parse_gcm_pt_ct_str, &vec.ct},
111 {AAD_STR, parse_gcm_aad_str, &vec.aead.aad},
112 {TAG_STR, parse_uint8_known_len_hex_str,
114 {NULL, NULL, NULL} /**< end pointer */
117 struct fips_test_callback gcm_interim_vectors[] = {
118 {KEYLEN_STR, parser_read_uint32_bit_val, &vec.aead.key},
119 {IVLEN_STR, parser_read_uint32_bit_val, &vec.iv},
120 {PTLEN_STR, parser_read_gcm_pt_len, &vec.pt},
121 {PTLEN_STR, parser_read_uint32_bit_val, &vec.ct},
122 /**< The NIST test vectors use 'PTlen' to denote input text
123 * length in case of decrypt & encrypt operations.
125 {AADLEN_STR, parser_read_uint32_bit_val, &vec.aead.aad},
126 {TAGLEN_STR, parser_read_uint32_bit_val,
128 {NULL, NULL, NULL} /**< end pointer */
131 struct fips_test_callback gcm_enc_vectors[] = {
132 {KEY_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
133 {IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
134 {PT_STR, parse_gcm_pt_ct_str, &vec.pt},
135 {AAD_STR, parse_gcm_aad_str, &vec.aead.aad},
136 {NULL, NULL, NULL} /**< end pointer */
140 parse_test_gcm_writeback(struct fips_val *val)
142 struct fips_val tmp_val;
144 if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
145 /* According to NIST GCMVS section 6.1, IUT should provide
148 if (info.interim_info.gcm_data.gen_iv) {
149 fprintf(info.fp_wr, "%s", IV_STR);
150 tmp_val.val = vec.iv.val;
151 tmp_val.len = vec.iv.len;
153 parse_write_hex_str(&tmp_val);
154 rte_free(vec.iv.val);
158 fprintf(info.fp_wr, "%s", CT_STR);
160 if (!info.interim_info.gcm_data.is_gmac) {
161 tmp_val.val = val->val;
162 tmp_val.len = vec.pt.len;
164 parse_write_hex_str(&tmp_val);
166 fprintf(info.fp_wr, "\n");
168 fprintf(info.fp_wr, "%s", TAG_STR);
170 tmp_val.val = val->val + vec.pt.len;
171 tmp_val.len = val->len - vec.pt.len;
173 parse_write_hex_str(&tmp_val);
175 if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
176 fprintf(info.fp_wr, "%s", PT_STR);
177 if (!info.interim_info.gcm_data.is_gmac) {
178 tmp_val.val = val->val;
179 tmp_val.len = vec.pt.len;
181 parse_write_hex_str(&tmp_val);
183 fprintf(info.fp_wr, "\n");
185 fprintf(info.fp_wr, "%s\n", NEG_TEST_STR);
192 parse_test_gcm_init(void)
198 for (i = 0; i < info.nb_vec_lines; i++) {
199 char *line = info.vec[i];
201 tmp = strstr(line, OP_STR);
203 if (strstr(line, OP_ENC_STR)) {
204 info.op = FIPS_TEST_ENC_AUTH_GEN;
205 info.callbacks = gcm_enc_vectors;
206 if (strstr(info.file_name, OP_ENC_INT_STR))
207 info.interim_info.gcm_data.gen_iv = 1;
208 } else if (strstr(line, OP_DEC_STR)) {
209 info.op = FIPS_TEST_DEC_AUTH_VERIF;
210 info.callbacks = gcm_dec_vectors;
216 info.interim_callbacks = gcm_interim_vectors;
217 info.parse_writeback = parse_test_gcm_writeback;