xref: /dpdk/examples/fips_validation/fips_validation_gcm.c (revision 2adb3b4e7e54dcce626560da39b6fb6adfe914bc)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation
3  */
4 
5 #include <string.h>
6 #include <time.h>
7 #include <stdio.h>
8 
9 #include <rte_cryptodev.h>
10 
11 #include "fips_validation.h"
12 
13 #define NEW_LINE_STR	"#"
14 #define OP_STR		"GCM "
15 
16 #define PARAM_PREFIX	"["
17 #define KEYLEN_STR	"Keylen = "
18 #define IVLEN_STR	"IVlen = "
19 #define PTLEN_STR	"PTlen = "
20 #define AADLEN_STR	"AADlen = "
21 #define TAGLEN_STR	"Taglen = "
22 #define CTLEN_STR	"CTlen = "
23 
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 = "
31 
32 #define OP_ENC_STR	"Encrypt"
33 #define OP_DEC_STR	"Decrypt"
34 
35 #define NEG_TEST_STR	"FAIL"
36 
37 struct fips_test_callback gcm_dec_vectors[] = {
38 		{KEY_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
39 		{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
40 		{CT_STR, parse_uint8_known_len_hex_str, &vec.ct},
41 		{AAD_STR, parse_uint8_known_len_hex_str, &vec.aead.aad},
42 		{TAG_STR, parse_uint8_known_len_hex_str,
43 				&vec.aead.digest},
44 		{NULL, NULL, NULL} /**< end pointer */
45 };
46 struct fips_test_callback gcm_interim_vectors[] = {
47 		{KEYLEN_STR, parser_read_uint32_bit_val, &vec.aead.key},
48 		{IVLEN_STR, parser_read_uint32_bit_val, &vec.iv},
49 		{PTLEN_STR, parser_read_uint32_bit_val, &vec.pt},
50 		{CTLEN_STR, parser_read_uint32_bit_val, &vec.ct},
51 		{AADLEN_STR, parser_read_uint32_bit_val, &vec.aead.aad},
52 		{TAGLEN_STR, parser_read_uint32_bit_val,
53 				&vec.aead.digest},
54 		{NULL, NULL, NULL} /**< end pointer */
55 };
56 
57 struct fips_test_callback gcm_enc_vectors[] = {
58 		{KEY_STR, parse_uint8_known_len_hex_str, &vec.aead.key},
59 		{IV_STR, parse_uint8_known_len_hex_str, &vec.iv},
60 		{PT_STR, parse_uint8_known_len_hex_str, &vec.pt},
61 		{AAD_STR, parse_uint8_known_len_hex_str, &vec.aead.aad},
62 		{NULL, NULL, NULL} /**< end pointer */
63 };
64 
65 static int
66 parse_test_gcm_writeback(struct fips_val *val)
67 {
68 	struct fips_val tmp_val;
69 
70 	if (info.op == FIPS_TEST_ENC_AUTH_GEN) {
71 		fprintf(info.fp_wr, "%s", CT_STR);
72 
73 		tmp_val.val = val->val;
74 		tmp_val.len = vec.pt.len;
75 
76 		parse_write_hex_str(&tmp_val);
77 
78 		fprintf(info.fp_wr, "%s", TAG_STR);
79 
80 		tmp_val.val = val->val + vec.pt.len;
81 		tmp_val.len = val->len - vec.pt.len;
82 
83 		parse_write_hex_str(&tmp_val);
84 	} else {
85 		if (vec.status == RTE_CRYPTO_OP_STATUS_SUCCESS) {
86 			fprintf(info.fp_wr, "%s", PT_STR);
87 
88 			tmp_val.val = val->val;
89 			tmp_val.len = vec.pt.len;
90 
91 			parse_write_hex_str(&tmp_val);
92 		} else
93 			fprintf(info.fp_wr, "%s\n", NEG_TEST_STR);
94 	}
95 
96 	return 0;
97 }
98 
99 int
100 parse_test_gcm_init(void)
101 {
102 	char *tmp;
103 	uint32_t i;
104 
105 
106 	for (i = 0; i < info.nb_vec_lines; i++) {
107 		char *line = info.vec[i];
108 
109 
110 		tmp = strstr(line, OP_STR);
111 		if (tmp) {
112 			if (strstr(line, OP_ENC_STR)) {
113 				info.op = FIPS_TEST_ENC_AUTH_GEN;
114 				info.callbacks = gcm_enc_vectors;
115 			} else if (strstr(line, OP_DEC_STR)) {
116 				info.op = FIPS_TEST_DEC_AUTH_VERIF;
117 				info.callbacks = gcm_dec_vectors;
118 			} else
119 				return -EINVAL;
120 		}
121 	}
122 
123 	info.interim_callbacks = gcm_interim_vectors;
124 	info.parse_writeback = parse_test_gcm_writeback;
125 
126 	return 0;
127 }
128