xref: /dpdk/examples/fips_validation/fips_validation_sha.c (revision 3d26a70ae33853ac4116b135e55f3d475f148939)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 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 ALGO_PREFIX	"[L = "
14 #define MSGLEN_STR	"Len = "
15 #define MSG_STR		"Msg = "
16 #define MD_STR		"MD = "
17 #define SEED_STR	"Seed = "
18 #define MCT_STR		"Monte"
19 
20 struct plain_hash_size_conversion {
21 	const char *str;
22 	enum rte_crypto_auth_algorithm algo;
23 } phsc[] = {
24 		{"20", RTE_CRYPTO_AUTH_SHA1},
25 		{"28", RTE_CRYPTO_AUTH_SHA224},
26 		{"32", RTE_CRYPTO_AUTH_SHA256},
27 		{"48", RTE_CRYPTO_AUTH_SHA384},
28 		{"64", RTE_CRYPTO_AUTH_SHA512},
29 };
30 
31 static int
32 parse_interim_algo(__rte_unused const char *key,
33 		char *text,
34 		__rte_unused struct fips_val *val)
35 {
36 	uint32_t i;
37 
38 	for (i = 0; i < RTE_DIM(phsc); i++) {
39 		if (strstr(text, phsc[i].str)) {
40 			info.interim_info.sha_data.algo = phsc[i].algo;
41 			parser_read_uint32_val(ALGO_PREFIX,
42 				text, &vec.cipher_auth.digest);
43 			break;
44 		}
45 	}
46 
47 	if (i == RTE_DIM(phsc))
48 		return -1;
49 
50 	return 0;
51 }
52 
53 struct fips_test_callback sha_tests_vectors[] = {
54 		{MSGLEN_STR, parser_read_uint32_bit_val, &vec.pt},
55 		{MSG_STR, parse_uint8_known_len_hex_str, &vec.pt},
56 		{SEED_STR, parse_uint8_hex_str, &vec.cipher_auth.digest},
57 		{NULL, NULL, NULL} /**< end pointer */
58 };
59 
60 struct fips_test_callback sha_tests_interim_vectors[] = {
61 		{ALGO_PREFIX, parse_interim_algo, NULL},
62 		{NULL, NULL, NULL} /**< end pointer */
63 };
64 
65 static int
66 parse_test_sha_writeback(struct fips_val *val) // !
67 {
68 	struct fips_val val_local;
69 
70 	fprintf(info.fp_wr, "%s", MD_STR);
71 
72 	val_local.val = val->val + vec.pt.len;
73 	val_local.len = vec.cipher_auth.digest.len;
74 
75 	parse_write_hex_str(&val_local);
76 	return 0;
77 }
78 
79 static int
80 rsp_test_sha_check(struct fips_val *val)
81 {
82 	if (memcmp(val->val + vec.pt.len, vec.cipher_auth.digest.val,
83 			vec.cipher_auth.digest.len) == 0)
84 		fprintf(info.fp_wr, "Success\n");
85 	else
86 		fprintf(info.fp_wr, "Failed\n");
87 
88 	return 0;
89 }
90 
91 int
92 parse_test_sha_init(void)
93 {
94 	uint32_t i;
95 
96 	info.interim_info.sha_data.test_type = SHA_KAT;
97 	for (i = 0; i < info.nb_vec_lines; i++) {
98 		char *line = info.vec[i];
99 		if (strstr(line, MCT_STR))
100 			info.interim_info.sha_data.test_type = SHA_MCT;
101 	}
102 
103 	info.op = FIPS_TEST_ENC_AUTH_GEN;
104 	info.parse_writeback = parse_test_sha_writeback;
105 	info.callbacks = sha_tests_vectors;
106 	info.interim_callbacks = sha_tests_interim_vectors;
107 	info.writeback_callbacks = NULL;
108 	info.kat_check = rsp_test_sha_check;
109 	return 0;
110 }
111