1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright(c) 2018 Intel Corporation
*/
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef USE_JANSSON
#include <jansson.h>
#endif /* USE_JANSSON */
#include <rte_cryptodev.h>
#include <rte_malloc.h>
#include "fips_validation.h"
#define NEW_LINE_STR "#"
#define OP_STR "GCM "
#define PARAM_PREFIX "["
#define KEYLEN_STR "Keylen = "
#define IVLEN_STR "IVlen = "
#define PTLEN_STR "PTlen = "
#define AADLEN_STR "AADlen = "
#define TAGLEN_STR "Taglen = "
#define COUNT_STR "Count = "
#define KEY_STR "Key = "
#define IV_STR "IV = "
#define PT_STR "PT = "
#define CT_STR "CT = "
#define TAG_STR "Tag = "
#define AAD_STR "AAD = "
#define OP_ENC_STR "Encrypt"
#define OP_DEC_STR "Decrypt"
/* External/Internal IV generation, specified in file name, following NIST
* GCMVS Section 6.1
*/
#define OP_ENC_EXT_STR "ExtIV"
#define OP_ENC_INT_STR "IntIV"
#define KEYLEN_JSON_STR "keyLen"
#define IVLEN_JSON_STR "ivLen"
#define PAYLOADLEN_JSON_STR "payloadLen"
#define AADLEN_JSON_STR "aadLen"
#define TAGLEN_JSON_STR "tagLen"
#define KEY_JSON_STR "key"
#define IV_JSON_STR "iv"
#define PT_JSON_STR "pt"
#define CT_JSON_STR "ct"
#define AAD_JSON_STR "aad"
#define TAG_JSON_STR "tag"
#define DIR_JSON_STR "direction"
#define OP_ENC_JSON_STR "encrypt"
#define OP_DEC_JSON_STR "decrypt"
#define IVGEN_JSON_STR "ivGen"
#define OP_ENC_EXT_JSON_STR "external"
#define OP_ENC_INT_JSON_STR "internal"
#define NEG_TEST_STR "FAIL"
/**
* GMAC is essentially zero length plaintext and uses AAD as input data.
* NIST does not have GMAC specific test vector but using zero length "PTlen"
* and uses AAD as input.
**/
static int
parser_read_gcm_pt_len(const char *key, char *src,
__rte_unused struct fips_val *val)
{
int ret = parser_read_uint32_bit_val(key, src, &vec.pt);
if (ret < 0)
return ret;
if (info.algo == FIPS_TEST_ALGO_AES_GMAC && vec.pt.len == 0) {
info.interim_info.gcm_data.is_gmac = 1;
test_ops.prepare_sym_op = prepare_auth_op;
test_ops.prepare_sym_xform = prepare_gmac_xform;
} else {
info.interim_info.gcm_data.is_gmac = 0;
test_ops.prepare_sym_op = prepare_aead_op;
test_ops.prepare_sym_xform = prepare_gcm_xform;
}
return ret;
}
static int
parse_gcm_aad_str(const char *key, char *src,
__rte_unused struct fips_val *val)
{
/* For GMAC test vector, AAD is treated as input */
if (info.interim_info.gcm_data.is_gmac) {
vec.pt.len = vec.aead.aad.len;
return parse_uint8_known_len_hex_str(key, src, &vec.pt);
} else /* gcm */
return parse_uint8_known_len_hex_str(key, src, &vec.aead.aad);
}
static int
parse_gcm_pt_ct_str(const char *key, char *src, struct fips_val *val)
{
/* According to NIST GCMVS section 6.1, IUT should generate IV data */
if (info.interim_info.gcm_data.gen_iv && vec.iv.len) {
uint32_t i;
if (!vec.iv.val) {
vec.iv.val = rte_malloc(0, vec.iv.len, 0);
if (!vec.iv.val)
return -ENOMEM;
}
for (i = 0; i < vec.iv.len; i++) {
int random = rand();
vec.iv.val[i] = (uint8_t)random;
}
}
/* if PTlen == 0, pt or ct will be handled by AAD later */
if (info.interim_info.gcm_data.is_gmac)
return 0;
return parse_uint8_known_len_hex_str(key, src, val);
}
struct fips_test_callback gcm_dec_vectors[] = {
{KEY_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
{CT_STR, parse_gcm_pt_ct_str, &vec.ct},
{AAD_STR, parse_gcm_aad_str, &vec.aead.aad},
{TAG_STR, parse_uint8_known_len_hex_str,
&vec.aead.digest},
{NULL, NULL, NULL} /**< end pointer */
};
struct fips_test_callback gcm_interim_vectors[] = {
{KEYLEN_STR, parser_read_uint32_bit_val, &vec.aead.key},
{IVLEN_STR, parser_read_uint32_bit_val, &vec.iv},
{PTLEN_STR, parser_read_gcm_pt_len, &vec.pt},
{PTLEN_STR, parser_read_uint32_bit_val, &vec.ct},
/**< The NIST test vectors use 'PTlen' to denote input text
* length in case of decrypt & encrypt operations.
*/
{AADLEN_STR, parser_read_uint32_bit_val, &vec.aead.aad},
{TAGLEN_STR, parser_read_uint32_bit_val,
&vec.aead.digest},
{NULL, NULL, NULL} /**< end pointer */
};
struct fips_test_callback gcm_enc_vectors[] = {
{KEY_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
{PT_STR, parse_gcm_pt_ct_str, &vec.pt},
{AAD_STR, parse_gcm_aad_str, &vec.aead.aad},
{NULL, NULL, NULL} /**< end pointer */
};
#ifdef USE_JANSSON
struct fips_test_callback gcm_dec_json_vectors[] = {
{KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
{IV_JSON_STR, parse_uint8_known_len_hex_str, &vec.iv},
{CT_JSON_STR, parse_gcm_pt_ct_str, &vec.ct},
{AAD_JSON_STR, parse_gcm_aad_str, &vec.aead.aad},
{TAG_JSON_STR, parse_uint8_known_len_hex_str,
&vec.aead.digest},
{NULL, NULL, NULL} /**< end pointer */
};
struct fips_test_callback gcm_interim_json_vectors[] = {
{KEYLEN_JSON_STR, parser_read_uint32_bit_val, &vec.aead.key},
{IVLEN_JSON_STR, parser_read_uint32_bit_val, &vec.iv},
{PAYLOADLEN_JSON_STR, parser_read_gcm_pt_len, &vec.pt},
{PAYLOADLEN_JSON_STR, parser_read_uint32_bit_val, &vec.ct},
/**< The NIST json test vectors use 'payloadLen' to denote input text
* length in case of decrypt & encrypt operations.
*/
{AADLEN_JSON_STR, parser_read_uint32_bit_val, &vec.aead.aad},
{TAGLEN_JSON_STR, parser_read_uint32_bit_val,
&vec.aead.digest},
{NULL, NULL, NULL} /**< end pointer */
};
struct fips_test_callback gcm_enc_json_vectors[] = {
{KEY_JSON_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
{IV_JSON_STR, parse_uint8_known_len_hex_str, &vec.iv},
{PT_JSON_STR, parse_gcm_pt_ct_str, &vec.pt},
{AAD_JSON_STR, parse_gcm_aad_str, &vec.aead.aad},
{NULL, NULL, NULL} /**< end pointer */
};
#endif /* USE_JANSSON */
static int
parse_test_gcm_writeback(struct fips_val *val)
{
struct fips_val tmp_val;
if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
/* According to NIST GCMVS section 6.1, IUT should provide
* generate IV data
*/
if (info.interim_info.gcm_data.gen_iv) {
fprintf(info.fp_wr, "%s", IV_STR);
tmp_val.val = vec.iv.val;
tmp_val.len = vec.iv.len;
parse_write_hex_str(&tmp_val);
rte_free(vec.iv.val);
vec.iv.val = NULL;
}
fprintf(info.fp_wr, "%s", CT_STR);
if (!info.interim_info.gcm_data.is_gmac) {
tmp_val.val = val->val;
tmp_val.len = vec.pt.len;
parse_write_hex_str(&tmp_val);
} else
fprintf(info.fp_wr, "\n");
fprintf(info.fp_wr, "%s", TAG_STR);
tmp_val.val = val->val + vec.pt.len;
tmp_val.len = val->len - vec.pt.len;
parse_write_hex_str(&tmp_val);
} else {
if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
fprintf(info.fp_wr, "%s", PT_STR);
if (!info.interim_info.gcm_data.is_gmac) {
tmp_val.val = val->val;
tmp_val.len = vec.pt.len;
parse_write_hex_str(&tmp_val);
} else
fprintf(info.fp_wr, "\n");
} else
fprintf(info.fp_wr, "%s\n", NEG_TEST_STR);
}
return 0;
}
int
parse_test_gcm_init(void)
{
char *tmp;
uint32_t i;
for (i = 0; i < info.nb_vec_lines; i++) {
char *line = info.vec[i];
tmp = strstr(line, OP_STR);
if (tmp) {
if (strstr(line, OP_ENC_STR)) {
info.op = FIPS_TEST_ENC_AUTH_GEN;
info.callbacks = gcm_enc_vectors;
if (strstr(info.file_name, OP_ENC_INT_STR))
info.interim_info.gcm_data.gen_iv = 1;
} else if (strstr(line, OP_DEC_STR)) {
info.op = FIPS_TEST_DEC_AUTH_VERIF;
info.callbacks = gcm_dec_vectors;
} else
return -EINVAL;
}
}
info.interim_callbacks = gcm_interim_vectors;
info.parse_writeback = parse_test_gcm_writeback;
return 0;
}
#ifdef USE_JANSSON
static int
parse_test_gcm_json_writeback(struct fips_val *val)
{
struct fips_val tmp_val;
json_t *tcId, *tag;
tcId = json_object_get(json_info.json_test_case, "tcId");
json_info.json_write_case = json_object();
json_object_set(json_info.json_write_case, "tcId", tcId);
if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
json_t *ct;
if (!info.interim_info.gcm_data.is_gmac) {
tmp_val.val = val->val;
tmp_val.len = vec.pt.len;
info.one_line_text[0] = '\0';
writeback_hex_str("", info.one_line_text, &tmp_val);
ct = json_string(info.one_line_text);
json_object_set_new(json_info.json_write_case, CT_JSON_STR, ct);
}
if (info.interim_info.gcm_data.gen_iv) {
json_t *iv;
tmp_val.val = vec.iv.val;
tmp_val.len = vec.iv.len;
writeback_hex_str("", info.one_line_text, &tmp_val);
iv = json_string(info.one_line_text);
json_object_set_new(json_info.json_write_case, IV_JSON_STR, iv);
rte_free(vec.iv.val);
vec.iv.val = NULL;
}
tmp_val.val = val->val + vec.pt.len;
tmp_val.len = val->len - vec.pt.len;
writeback_hex_str("", info.one_line_text, &tmp_val);
tag = json_string(info.one_line_text);
json_object_set_new(json_info.json_write_case, TAG_JSON_STR, tag);
} else {
if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
if (!info.interim_info.gcm_data.is_gmac) {
tmp_val.val = val->val;
tmp_val.len = vec.pt.len;
info.one_line_text[0] = '\0';
writeback_hex_str("", info.one_line_text, &tmp_val);
json_object_set_new(json_info.json_write_case, PT_JSON_STR,
json_string(info.one_line_text));
} else {
json_object_set_new(json_info.json_write_case, "testPassed",
json_true());
}
} else {
json_object_set_new(json_info.json_write_case, "testPassed",
json_false());
}
}
return 0;
}
int
parse_test_gcm_json_init(void)
{
json_t *direction_obj;
const char *direction_str;
direction_obj = json_object_get(json_info.json_test_group, DIR_JSON_STR);
direction_str = json_string_value(direction_obj);
info.interim_info.gcm_data.gen_iv = 0;
if (strcmp(direction_str, OP_ENC_JSON_STR) == 0) {
json_t *ivGen_obj = json_object_get(json_info.json_test_group, IVGEN_JSON_STR);
const char *ivGen_str = json_string_value(ivGen_obj);
info.op = FIPS_TEST_ENC_AUTH_GEN;
info.callbacks = gcm_enc_json_vectors;
if (strcmp(ivGen_str, OP_ENC_INT_JSON_STR) == 0)
info.interim_info.gcm_data.gen_iv = 1;
} else if (strcmp(direction_str, OP_DEC_JSON_STR) == 0) {
info.op = FIPS_TEST_DEC_AUTH_VERIF;
info.callbacks = gcm_dec_json_vectors;
} else {
return -EINVAL;
}
info.interim_callbacks = gcm_interim_json_vectors;
info.parse_writeback = parse_test_gcm_json_writeback;
return 0;
}
#endif /* USE_JANSSON */
|