1 /*
2  * Copyright (c) 2023 Microchip Technolofy, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #include <zephyr/kernel.h>
9 #include <zephyr/device.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/crypto/crypto.h>
12 #include <zephyr/crypto/hash.h>
13 #include <zephyr/sys/util.h>
14 
15 #include <stdio.h>
16 #include <string.h>
17 #include <soc.h>
18 
19 struct hash_tp {
20 	enum hash_algo hash_id;
21 	size_t msgsz;
22 	const uint8_t *msg;
23 	const uint8_t *digest;
24 };
25 
26 const uint8_t msg37[] = {
27 	0x45, 0x73, 0x66, 0x72, 0x44, 0x36, 0x70, 0x73,
28 	0x47, 0x7a, 0x66, 0x70, 0x78, 0x56, 0x78, 0x6b,
29 	0x70, 0x65, 0x4a, 0x56, 0x55, 0x4a, 0x58, 0x62,
30 	0x41, 0x61, 0x53, 0x33, 0x6e, 0x55, 0x78, 0x41,
31 	0x46, 0x6d, 0x34, 0x77, 0x42,
32 };
33 const uint8_t msg37_sha224_digest[] = {
34 	0xe4, 0x70, 0xa9, 0x89, 0xc5, 0x37, 0xda, 0x0d,
35 	0x9f, 0x55, 0x9a, 0x4e, 0x9d, 0xed, 0xaa, 0x75,
36 	0xf8, 0xe0, 0x58, 0x0f, 0xc4, 0x2e, 0x0d, 0x23,
37 	0x03, 0x7c, 0x0f, 0x18,
38 };
39 const uint8_t msg37_sha256_digest[] = {
40 	0xe2, 0x82, 0x23, 0xfb, 0x3f, 0x6a, 0x49, 0x17,
41 	0xb0, 0x97, 0xba, 0x60, 0x51, 0xd0, 0xb6, 0x38,
42 	0x1d, 0xd0, 0x85, 0xfe, 0xd0, 0x6b, 0x99, 0x17,
43 	0x81, 0x32, 0x89, 0x1b, 0xbb, 0x18, 0x33, 0xfe,
44 };
45 const uint8_t msg37_sha384_digest[] = {
46 	0xce, 0x92, 0x9e, 0x57, 0xd5, 0xc1, 0x6a, 0x73,
47 	0xf2, 0xfd, 0xcc, 0xed, 0x8b, 0xec, 0xf4, 0xd7,
48 	0xf5, 0x61, 0x1b, 0x57, 0xaa, 0x74, 0x80, 0x3f,
49 	0x76, 0x64, 0x83, 0x06, 0x8a, 0x47, 0x04, 0x41,
50 	0x83, 0x21, 0x83, 0xd7, 0xb5, 0x44, 0xb7, 0xe4,
51 	0xeb, 0xe4, 0xb0, 0xba, 0xbb, 0x7b, 0x92, 0x75,
52 };
53 const uint8_t msg37_sha512_digest[] = {
54 	0xe1, 0x7c, 0x02, 0xe7, 0x74, 0x44, 0x2d, 0x37,
55 	0xd9, 0x6c, 0xf9, 0x12, 0xfd, 0xb3, 0x64, 0xcc,
56 	0x00, 0x68, 0xbd, 0x0b, 0x79, 0xb6, 0x52, 0x00,
57 	0x37, 0x9f, 0xa2, 0xbe, 0x4d, 0x1f, 0x4a, 0x67,
58 	0x46, 0xc6, 0x74, 0x01, 0x60, 0x46, 0x32, 0x5e,
59 	0x35, 0x4f, 0x36, 0x26, 0x84, 0xd7, 0x2e, 0x47,
60 	0x58, 0x75, 0xb1, 0x94, 0x77, 0x3d, 0x11, 0xd1,
61 	0x3f, 0x69, 0x5b, 0x80, 0x36, 0x9c, 0x69, 0x48,
62 };
63 
64 const uint8_t msg185[] = {
65 	0x35, 0x61, 0x42, 0x65, 0x38, 0x65, 0x70, 0x50,
66 	0x62, 0x4b, 0x74, 0x47, 0x4d, 0x4a, 0x79, 0x65,
67 	0x64, 0x53, 0x6a, 0x4e, 0x7a, 0x39, 0x70, 0x43,
68 	0x62, 0x53, 0x67, 0x64, 0x6b, 0x32, 0x65, 0x6f,
69 	0x42, 0x6a, 0x38, 0x4d, 0x54, 0x79, 0x62, 0x6b,
70 	0x4c, 0x4e, 0x73, 0x34, 0x34, 0x70, 0x59, 0x78,
71 	0x4a, 0x45, 0x37, 0x33, 0x69, 0x77, 0x71, 0x39,
72 	0x72, 0x41, 0x70, 0x6f, 0x65, 0x70, 0x66, 0x42,
73 	0x39, 0x33, 0x6d, 0x54, 0x6d, 0x48, 0x6f, 0x51,
74 	0x57, 0x57, 0x53, 0x4d, 0x37, 0x6c, 0x30, 0x6a,
75 	0x78, 0x46, 0x35, 0x6f, 0x65, 0x61, 0x46, 0x44,
76 	0x39, 0x55, 0x66, 0x49, 0x56, 0x4a, 0x42, 0x38,
77 	0x4e, 0x64, 0x6f, 0x64, 0x6a, 0x63, 0x61, 0x32,
78 	0x74, 0x34, 0x38, 0x69, 0x36, 0x7a, 0x56, 0x31,
79 	0x31, 0x37, 0x4b, 0x55, 0x57, 0x64, 0x72, 0x61,
80 	0x4b, 0x55, 0x33, 0x72, 0x58, 0x61, 0x31, 0x34,
81 	0x4e, 0x44, 0x59, 0x6a, 0x7a, 0x69, 0x73, 0x34,
82 	0x70, 0x50, 0x51, 0x62, 0x57, 0x41, 0x39, 0x4d,
83 	0x68, 0x45, 0x4e, 0x35, 0x49, 0x4f, 0x41, 0x41,
84 	0x67, 0x52, 0x31, 0x57, 0x78, 0x76, 0x36, 0x4b,
85 	0x34, 0x6b, 0x57, 0x31, 0x75, 0x75, 0x38, 0x4c,
86 	0x75, 0x41, 0x30, 0x77, 0x64, 0x4c, 0x4d, 0x41,
87 	0x5a, 0x46, 0x46, 0x51, 0x59, 0x79, 0x51, 0x34,
88 	0x63,
89 };
90 const uint8_t msg185_sha224_digest[] = {
91 	0x24, 0xf4, 0xa2, 0x16, 0x52, 0x80, 0x9f, 0xc0,
92 	0x84, 0x5e, 0x17, 0xd4, 0x2c, 0xe0, 0x50, 0x74,
93 	0x69, 0xdc, 0xec, 0x21, 0x29, 0x58, 0xc2, 0x2f,
94 	0xf4, 0x34, 0x89, 0x48,
95 };
96 const uint8_t msg185_sha256_digest[] = {
97 	0x79, 0x2a, 0x14, 0xc2, 0x97, 0x90, 0x1d, 0xb4,
98 	0x97, 0x73, 0xaa, 0xc8, 0x18, 0x50, 0x84, 0xf6,
99 	0xab, 0xf4, 0x10, 0x88, 0x9c, 0x30, 0x6a, 0x44,
100 	0x40, 0x29, 0x01, 0x16, 0xc7, 0x14, 0x79, 0xd1,
101 };
102 const uint8_t msg185_sha384_digest[] = {
103 	0xff, 0x99, 0x7a, 0x44, 0x3b, 0xe1, 0x96, 0x84,
104 	0x74, 0x7d, 0x32, 0x96, 0x76, 0xd1, 0x10, 0x85,
105 	0x91, 0x30, 0xc3, 0x4b, 0xde, 0x4d, 0xb7, 0x6f,
106 	0xf2, 0xbe, 0x69, 0xaa, 0xe1, 0x2c, 0x80, 0xd4,
107 	0x1d, 0x8f, 0xf3, 0x3a, 0x51, 0x86, 0xb9, 0x8c,
108 	0xc4, 0xb6, 0x1b, 0x45, 0xfc, 0x86, 0xac, 0x31,
109 };
110 const uint8_t msg185_sha512_digest[] = {
111 	0xaa, 0xba, 0x04, 0xc9, 0x58, 0x37, 0xaa, 0xa8,
112 	0xb9, 0x1c, 0x42, 0x66, 0x4f, 0x64, 0xa3, 0x52,
113 	0x6f, 0xf9, 0x94, 0x75, 0x40, 0x91, 0x5c, 0x1d,
114 	0x20, 0x6d, 0xa2, 0xd5, 0x59, 0x0c, 0x8b, 0xdc,
115 	0xb7, 0x83, 0x80, 0x76, 0xa6, 0xe6, 0x4c, 0xcf,
116 	0xe3, 0x8c, 0x4c, 0x4e, 0xfc, 0xdf, 0x16, 0xb8,
117 	0xdf, 0x95, 0x42, 0xb0, 0x8d, 0x25, 0x9a, 0x19,
118 	0x91, 0x5e, 0x79, 0x32, 0xf1, 0x74, 0xc8, 0x62,
119 };
120 
121 const uint8_t msg238[] = {
122 	0x75, 0x79, 0x30, 0x65, 0x45, 0x4a, 0x59, 0x4d,
123 	0x36, 0x76, 0x4d, 0x41, 0x4f, 0x6b, 0x47, 0x38,
124 	0x74, 0x63, 0x61, 0x39, 0x46, 0x41, 0x73, 0x4d,
125 	0x4e, 0x76, 0x6c, 0x35, 0x61, 0x39, 0x65, 0x39,
126 	0x4c, 0x7a, 0x53, 0x70, 0x72, 0x72, 0x51, 0x6b,
127 	0x73, 0x68, 0x4e, 0x6a, 0x6a, 0x79, 0x65, 0x63,
128 	0x69, 0x70, 0x65, 0x6a, 0x76, 0x53, 0x67, 0x47,
129 	0x33, 0x59, 0x57, 0x42, 0x38, 0x43, 0x72, 0x39,
130 	0x71, 0x36, 0x7a, 0x47, 0x76, 0x74, 0x67, 0x4f,
131 	0x30, 0x54, 0x71, 0x78, 0x38, 0x41, 0x70, 0x49,
132 	0x51, 0x75, 0x30, 0x7a, 0x4e, 0x52, 0x53, 0x75,
133 	0x50, 0x68, 0x31, 0x67, 0x77, 0x53, 0x59, 0x46,
134 	0x43, 0x69, 0x52, 0x4d, 0x4e, 0x58, 0x53, 0x54,
135 	0x49, 0x30, 0x78, 0x6c, 0x63, 0x4d, 0x42, 0x50,
136 	0x58, 0x64, 0x30, 0x71, 0x57, 0x44, 0x56, 0x34,
137 	0x6e, 0x79, 0x4d, 0x4d, 0x30, 0x69, 0x55, 0x56,
138 	0x62, 0x53, 0x50, 0x49, 0x79, 0x70, 0x55, 0x55,
139 	0x54, 0x42, 0x58, 0x4a, 0x63, 0x77, 0x6d, 0x5a,
140 	0x31, 0x30, 0x4a, 0x66, 0x4e, 0x79, 0x55, 0x66,
141 	0x44, 0x7a, 0x56, 0x71, 0x56, 0x56, 0x72, 0x4c,
142 	0x4b, 0x38, 0x4e, 0x35, 0x45, 0x79, 0x4c, 0x65,
143 	0x4c, 0x67, 0x59, 0x44, 0x46, 0x42, 0x78, 0x47,
144 	0x76, 0x4d, 0x76, 0x74, 0x4a, 0x57, 0x57, 0x4f,
145 	0x4f, 0x79, 0x52, 0x4b, 0x55, 0x52, 0x6a, 0x69,
146 	0x4a, 0x4b, 0x74, 0x6b, 0x79, 0x50, 0x50, 0x78,
147 	0x6c, 0x77, 0x63, 0x4d, 0x50, 0x70, 0x55, 0x6e,
148 	0x4e, 0x41, 0x52, 0x56, 0x66, 0x79, 0x35, 0x6e,
149 	0x31, 0x66, 0x32, 0x69, 0x65, 0x6d, 0x77, 0x46,
150 	0x4f, 0x31, 0x31, 0x6f, 0x56, 0x49, 0x67, 0x76,
151 	0x72, 0x6f, 0x55, 0x74, 0x51, 0x52,
152 };
153 const uint8_t msg238_sha224_digest[] = {
154 	0xfb, 0xe5, 0xec, 0x23, 0xab, 0x7f, 0x71, 0x99,
155 	0xf1, 0xb7, 0x26, 0x7a, 0x6c, 0xcf, 0x68, 0xae,
156 	0x2f, 0xe5, 0x00, 0x0c, 0x58, 0x70, 0x5e, 0xe0,
157 	0x88, 0x3b, 0x51, 0x12,
158 };
159 const uint8_t msg238_sha256_digest[] = {
160 	0x1f, 0xd2, 0x20, 0xe1, 0xd4, 0x6b, 0xeb, 0x6c,
161 	0xaf, 0x3b, 0x18, 0xc5, 0x6a, 0xfa, 0x83, 0x6e,
162 	0xb9, 0xfc, 0x05, 0xff, 0x7c, 0x77, 0xc0, 0xb6,
163 	0x5f, 0x30, 0xd5, 0xd8, 0x98, 0x24, 0xdf, 0xcf,
164 };
165 const uint8_t msg238_sha384_digest[] = {
166 	0x87, 0x2c, 0xd9, 0xee, 0xd1, 0x18, 0xdf, 0x0c,
167 	0xdb, 0xbb, 0x76, 0xcf, 0xdd, 0xc5, 0x8d, 0xcb,
168 	0x14, 0xf1, 0x68, 0x7d, 0x18, 0x51, 0x37, 0xbb,
169 	0x55, 0x79, 0xd9, 0x59, 0xca, 0x49, 0xdb, 0x61,
170 	0x16, 0xf2, 0x4b, 0xeb, 0xa8, 0xce, 0xc8, 0xcd,
171 	0x26, 0x48, 0xf6, 0xe4, 0x20, 0xd1, 0x75, 0x4d,
172 };
173 const uint8_t msg238_sha512_digest[] = {
174 	0x24, 0x08, 0x8a, 0x1c, 0xc3, 0x44, 0x04, 0x79,
175 	0x56, 0xab, 0x65, 0xad, 0xf1, 0xdb, 0x99, 0x9c,
176 	0x54, 0x36, 0x84, 0xf0, 0xa8, 0x9c, 0x9b, 0x6d,
177 	0xc5, 0x13, 0x14, 0xb2, 0xff, 0x15, 0x17, 0x9d,
178 	0x4f, 0xf8, 0x61, 0x0f, 0x39, 0x28, 0x91, 0xe2,
179 	0x94, 0x3b, 0x32, 0x0f, 0xc5, 0xba, 0x31, 0xeb,
180 	0x41, 0xf4, 0xbe, 0x28, 0x0b, 0x39, 0x41, 0x26,
181 	0x17, 0x8c, 0x76, 0x1d, 0x4c, 0x9d, 0x4f, 0x47,
182 };
183 
184 const struct hash_tp hash_test_tbl2[] = {
185 	{
186 		.hash_id = CRYPTO_HASH_ALGO_SHA224,
187 		.msgsz = sizeof(msg37),
188 		.msg = msg37,
189 		.digest = msg37_sha224_digest,
190 	},
191 	{
192 		.hash_id = CRYPTO_HASH_ALGO_SHA256,
193 		.msgsz = sizeof(msg37),
194 		.msg = msg37,
195 		.digest = msg37_sha256_digest,
196 	},
197 	{
198 		.hash_id = CRYPTO_HASH_ALGO_SHA384,
199 		.msgsz = sizeof(msg37),
200 		.msg = msg37,
201 		.digest = msg37_sha384_digest,
202 	},
203 	{
204 		.hash_id = CRYPTO_HASH_ALGO_SHA512,
205 		.msgsz = sizeof(msg37),
206 		.msg = msg37,
207 		.digest = msg37_sha512_digest,
208 	},
209 	{
210 		.hash_id = CRYPTO_HASH_ALGO_SHA224,
211 		.msgsz = sizeof(msg185),
212 		.msg = msg185,
213 		.digest = msg185_sha224_digest,
214 	},
215 	{
216 		.hash_id = CRYPTO_HASH_ALGO_SHA256,
217 		.msgsz = sizeof(msg185),
218 		.msg = msg185,
219 		.digest = msg185_sha256_digest,
220 	},
221 	{
222 		.hash_id = CRYPTO_HASH_ALGO_SHA384,
223 		.msgsz = sizeof(msg185),
224 		.msg = msg185,
225 		.digest = msg185_sha384_digest,
226 	},
227 	{
228 		.hash_id = CRYPTO_HASH_ALGO_SHA512,
229 		.msgsz = sizeof(msg185),
230 		.msg = msg185,
231 		.digest = msg185_sha512_digest,
232 	},
233 	{
234 		.hash_id = CRYPTO_HASH_ALGO_SHA224,
235 		.msgsz = sizeof(msg238),
236 		.msg = msg238,
237 		.digest = msg238_sha224_digest,
238 	},
239 	{
240 		.hash_id = CRYPTO_HASH_ALGO_SHA256,
241 		.msgsz = sizeof(msg238),
242 		.msg = msg238,
243 		.digest = msg238_sha256_digest,
244 	},
245 	{
246 		.hash_id = CRYPTO_HASH_ALGO_SHA384,
247 		.msgsz = sizeof(msg238),
248 		.msg = msg238,
249 		.digest = msg238_sha384_digest,
250 	},
251 	{
252 		.hash_id = CRYPTO_HASH_ALGO_SHA512,
253 		.msgsz = sizeof(msg238),
254 		.msg = msg238,
255 		.digest = msg238_sha512_digest,
256 	},
257 };
258 
259 /* zephyr crypto driver capability flags, hash context, and hash packet */
260 static uint32_t cap_flags;
261 static struct hash_ctx zhash_ctx;
262 static struct hash_pkt zhash_pkt;
263 
264 /* Hash digest buffer sized for largest digest (SHA-512) */
265 static uint8_t digest[64];
266 
267 #define MCHP_XEC_ROM_API_NODE DT_NODELABEL(rom_api)
268 #define MCHP_XEC_SYMCR_NODE DT_NODELABEL(symcr)
269 
270 static const struct device *const symcr_dev = DEVICE_DT_GET(MCHP_XEC_SYMCR_NODE);
271 
272 static int test_zephyr_hash_chunk_block_size(const struct hash_tp *htbl, size_t nentries);
273 static int test_zephyr_hash_chunk(const struct hash_tp *htbl, size_t nentries, size_t chunksz);
274 static int validate_hw_compatibility(const struct device *dev);
275 
276 static int pr_hash_algo_name(enum hash_algo algo);
277 static size_t hash_digest_size(enum hash_algo algo);
278 static size_t hash_block_size(enum hash_algo algo);
279 
280 #define MCHP_XEC_STRUCT_HASH_STATE_STRUCT_SIZE	8
281 #define MCHP_XEC_STRUCT_HASH_STATE_MEM_SIZE	256
282 #define MCHP_XEC_STRUCT_HASH_STRUCT_SIZE	240
283 #define MCHP_XEC_STRUCT_HMAC2_STRUCT_SIZE	256
284 
main(void)285 int main(void)
286 {
287 	int ret;
288 	size_t chunk_size = 0;
289 
290 	printf("It lives! %s\n", CONFIG_BOARD);
291 	printf("ROM API say GIVE MEMORY, MORE MEMORY!\n");
292 	printf("Size of MEC172x ROM API hash state save memory is %u bytes!!!!\n",
293 		MCHP_XEC_STRUCT_HASH_STATE_MEM_SIZE);
294 	printf("Size of MEC172x ROM API hash context structure is %u bytes!!!!\n",
295 		MCHP_XEC_STRUCT_HASH_STRUCT_SIZE);
296 	printf("Size of MEC172x ROM API HMAC context structure is %u bytes!!!!\n",
297 		MCHP_XEC_STRUCT_HMAC2_STRUCT_SIZE);
298 
299 	if (!device_is_ready(symcr_dev)) {
300 		printf("ERROR: symcr device is not ready!\n");
301 		return 0;
302 	}
303 
304 	/* Check zephyr crypto drivers capabilities */
305 	ret = validate_hw_compatibility(symcr_dev);
306 	if (ret) {
307 		printf("ERROR: symcr driver capabilties failure\n");
308 		return 0;
309 	}
310 
311 	printf("\nTest Zephyr crypto hash API for multiblock plus remainder\n");
312 	ret = test_zephyr_hash_chunk_block_size(hash_test_tbl2, ARRAY_SIZE(hash_test_tbl2));
313 	printf("Test Zephyr crypto hash API for multiblock plus remainder returned %d\n", ret);
314 
315 	printf("\nTest Zephyr crypto arbitrary chunk size = %u\n", chunk_size);
316 	ret = test_zephyr_hash_chunk(hash_test_tbl2, ARRAY_SIZE(hash_test_tbl2), chunk_size);
317 	printf("Test Zephyr crypto arbitrary chunk size returned %d\n", ret);
318 
319 	chunk_size = 8u;
320 	printf("\nTest Zephyr crypto arbitrary chunk size = %u\n", chunk_size);
321 	ret = test_zephyr_hash_chunk(hash_test_tbl2, ARRAY_SIZE(hash_test_tbl2), chunk_size);
322 	printf("Test Zephyr crypto arbitrary chunk size returned %d\n", ret);
323 
324 	chunk_size = 23u;
325 	printf("\nTest Zephyr crypto arbitrary chunk size = %u\n", chunk_size);
326 	ret = test_zephyr_hash_chunk(hash_test_tbl2, ARRAY_SIZE(hash_test_tbl2), chunk_size);
327 	printf("Test Zephyr crypto arbitrary chunk size returned %d\n", ret);
328 
329 	printf("Application done\n");
330 	return 0;
331 }
332 
hash_digest_size(enum hash_algo algo)333 static size_t hash_digest_size(enum hash_algo algo)
334 {
335 	size_t digestsz;
336 
337 	switch (algo) {
338 	case CRYPTO_HASH_ALGO_SHA224:
339 		digestsz = 28u;
340 		break;
341 	case CRYPTO_HASH_ALGO_SHA256:
342 		digestsz = 32u;
343 		break;
344 	case CRYPTO_HASH_ALGO_SHA384:
345 		digestsz = 48u;
346 		break;
347 	case CRYPTO_HASH_ALGO_SHA512:
348 		digestsz = 64u;
349 		break;
350 	default:
351 		digestsz = 0u;
352 	}
353 
354 	return digestsz;
355 }
356 
hash_block_size(enum hash_algo algo)357 static size_t hash_block_size(enum hash_algo algo)
358 {
359 	size_t blocksz;
360 
361 	switch (algo) {
362 	case CRYPTO_HASH_ALGO_SHA224:
363 	case CRYPTO_HASH_ALGO_SHA256:
364 		blocksz = 64u;
365 		break;
366 	case CRYPTO_HASH_ALGO_SHA384:
367 	case CRYPTO_HASH_ALGO_SHA512:
368 		blocksz = 128u;
369 		break;
370 	default:
371 		blocksz = 0u;
372 	}
373 
374 	return blocksz;
375 }
376 
pr_hash_algo_name(enum hash_algo algo)377 static int pr_hash_algo_name(enum hash_algo algo)
378 {
379 	int ret = 0;
380 
381 	switch (algo) {
382 	case CRYPTO_HASH_ALGO_SHA224:
383 		printf("SHA-224\n");
384 		break;
385 	case CRYPTO_HASH_ALGO_SHA256:
386 		printf("SHA-256\n");
387 		break;
388 	case CRYPTO_HASH_ALGO_SHA384:
389 		printf("SHA-384\n");
390 		break;
391 	case CRYPTO_HASH_ALGO_SHA512:
392 		printf("SHA-512\n");
393 		break;
394 	default:
395 		printf("Uknown Hash algorithm\n");
396 		ret = -ENOTSUP;
397 	}
398 
399 	return ret;
400 }
401 
test_zephyr_hash_chunk_block_size(const struct hash_tp * htbl,size_t nentries)402 static int test_zephyr_hash_chunk_block_size(const struct hash_tp *htbl, size_t nentries)
403 {
404 	int fail_cnt = 0, ret = 0;
405 	size_t blocksz = 0, digestsz = 0, msgsz = 0, updatesz = 0;
406 
407 	for (size_t n = 0; n < nentries; n++) {
408 		const struct hash_tp *tp = &htbl[n];
409 		const uint8_t *msgptr = tp->msg;
410 		const uint8_t *exp_digest = tp->digest;
411 
412 		memset(digest, 0, sizeof(digest));
413 
414 		ret = pr_hash_algo_name(tp->hash_id);
415 		if (ret) {
416 			continue;
417 		}
418 
419 		msgsz = tp->msgsz;
420 		blocksz = hash_block_size(tp->hash_id);
421 		digestsz = hash_digest_size(tp->hash_id);
422 		printf("Message size is %u, block size is %u, digest size is %u byte\n",
423 			msgsz, blocksz, digestsz);
424 
425 		/* Open a hash session from our crypto driver */
426 		zhash_ctx.device = NULL;
427 		zhash_ctx.drv_sessn_state = NULL;
428 		zhash_ctx.hash_hndlr = NULL;
429 		zhash_ctx.started = false;
430 		/* application must supply correct flags */
431 		zhash_ctx.flags = CAP_SYNC_OPS | CAP_SEPARATE_IO_BUFS;
432 
433 		ret = hash_begin_session(symcr_dev, &zhash_ctx, tp->hash_id);
434 		if (ret) {
435 			printf("ERROR: zephyr crypto begin hash session: %d\n", ret);
436 			fail_cnt++;
437 			continue;
438 		}
439 
440 		if (msgsz > blocksz) {
441 			/* SHA algorithms block sizes are powers of 2 */
442 			updatesz = msgsz & ~(blocksz - 1u);
443 			printf("  Update size is %d\n", updatesz);
444 			zhash_pkt.in_buf = (uint8_t *)msgptr;
445 			zhash_pkt.in_len = updatesz;
446 			zhash_pkt.out_buf = digest;
447 			zhash_pkt.ctx = &zhash_ctx;
448 
449 			ret = hash_update(&zhash_ctx, &zhash_pkt);
450 			if (ret) {
451 				printf("ERROR: zephyr crypto hash update: %d\n", ret);
452 				fail_cnt++;
453 				continue;
454 			}
455 
456 			msgptr += updatesz;
457 			msgsz -= updatesz;
458 		}
459 
460 		printf("  Final size is %u\n", msgsz);
461 
462 		zhash_pkt.in_buf = (uint8_t *)msgptr;
463 		zhash_pkt.in_len = msgsz;
464 		zhash_pkt.out_buf = digest;
465 		zhash_pkt.ctx = &zhash_ctx;
466 
467 		/* final */
468 		ret = hash_compute(&zhash_ctx, &zhash_pkt);
469 		if (ret) {
470 			printf("ERROR: zephyr crypto hash compute: %d\n", ret);
471 			fail_cnt++;
472 			continue;
473 		}
474 
475 		ret = hash_free_session(symcr_dev, &zhash_ctx);
476 		if (ret) {
477 			printf("ERROR: zephyr crypto free hash session: %d\n", ret);
478 			fail_cnt++;
479 			continue;
480 		}
481 
482 		ret = strncmp(exp_digest, digest, digestsz);
483 		if (ret == 0) {
484 			printf("Hash computation PASS\n");
485 		} else {
486 			printf("Hash computation FAIL\n");
487 			fail_cnt++;
488 		}
489 	}
490 
491 	return fail_cnt;
492 }
493 
test_zephyr_hash_chunk(const struct hash_tp * htbl,size_t nentries,size_t chunksz)494 static int test_zephyr_hash_chunk(const struct hash_tp *htbl, size_t nentries, size_t chunksz)
495 {
496 	int fail_cnt = 0, ret = 0, chunk_cnt = 0;
497 	size_t blocksz = 0, digestsz = 0, msgsz = 0;
498 	size_t remsz = 0, updatesz = 0, total_updatesz = 0;
499 
500 	for (size_t n = 0; n < nentries; n++) {
501 		const struct hash_tp *tp = &htbl[n];
502 		const uint8_t *msgptr = tp->msg;
503 		const uint8_t *exp_digest = tp->digest;
504 
505 		memset(digest, 0, sizeof(digest));
506 
507 		ret = pr_hash_algo_name(tp->hash_id);
508 		if (ret) {
509 			continue;
510 		}
511 
512 		msgsz = tp->msgsz;
513 		blocksz = hash_block_size(tp->hash_id);
514 		digestsz = hash_digest_size(tp->hash_id);
515 		printf("Chunk size is %u Message size is %u, block size is %u, digest size is %u\n",
516 			chunksz, msgsz, blocksz, digestsz);
517 
518 		/* Open a hash session from our crypto driver */
519 		zhash_ctx.device = NULL;
520 		zhash_ctx.drv_sessn_state = NULL;
521 		zhash_ctx.hash_hndlr = NULL;
522 		zhash_ctx.started = false;
523 		/* application must supply correct flags */
524 		zhash_ctx.flags = CAP_SYNC_OPS | CAP_SEPARATE_IO_BUFS;
525 
526 		ret = hash_begin_session(symcr_dev, &zhash_ctx, tp->hash_id);
527 		if (ret) {
528 			printf("ERROR: zephyr crypto begin hash session: %d\n", ret);
529 			fail_cnt++;
530 			continue;
531 		}
532 
533 		chunk_cnt = 0;
534 		total_updatesz = 0;
535 		updatesz = chunksz;
536 		while (chunksz && ((msgsz - total_updatesz) > chunksz)) {
537 			chunk_cnt++;
538 
539 			zhash_pkt.in_buf = (uint8_t *)msgptr;
540 			zhash_pkt.in_len = updatesz;
541 			zhash_pkt.out_buf = digest;
542 			zhash_pkt.ctx = &zhash_ctx;
543 
544 			printf("  Chunk %d update size is %u\n", chunk_cnt, updatesz);
545 			ret = hash_update(&zhash_ctx, &zhash_pkt);
546 			if (ret) {
547 				printf("ERROR: zephyr crypto hash update: %d\n", ret);
548 				return ret;
549 			}
550 
551 			msgptr += updatesz;
552 			total_updatesz += updatesz;
553 		}
554 
555 		remsz = msgsz - total_updatesz;
556 		zhash_pkt.in_buf = (uint8_t *)msgptr;
557 		zhash_pkt.in_len = remsz;
558 		zhash_pkt.out_buf = digest;
559 		zhash_pkt.ctx = &zhash_ctx;
560 
561 		printf("  Compute hash final digest. Remaining size is %u\n", remsz);
562 		ret = hash_compute(&zhash_ctx, &zhash_pkt);
563 		if (ret) {
564 			printf("ERROR: zephyr crypto hash compute final: %d\n", ret);
565 			return ret;
566 		}
567 
568 		ret = hash_free_session(symcr_dev, &zhash_ctx);
569 		if (ret) {
570 			printf("ERROR: zephyr crypto free hash session: %d\n", ret);
571 			return ret;
572 		}
573 
574 		ret = strncmp(exp_digest, digest, digestsz);
575 		if (ret == 0) {
576 			printf("Hash computation PASS\n");
577 		} else {
578 			printf("Hash computation FAIL\n");
579 			fail_cnt++;
580 		}
581 	}
582 
583 	return fail_cnt;
584 }
585 
validate_hw_compatibility(const struct device * dev)586 static int validate_hw_compatibility(const struct device *dev)
587 {
588 	uint32_t flags = 0U;
589 
590 	flags = crypto_query_hwcaps(dev);
591 	if ((flags & CAP_RAW_KEY) == 0U) {
592 		printf("Please provision the key separately "
593 			"as the module doesn't support a raw key\n");
594 		return -1;
595 	}
596 
597 	if ((flags & CAP_SYNC_OPS) == 0U) {
598 		printf("The app assumes sync semantics. "
599 			"Please rewrite the app accordingly before proceeding\n");
600 		return -1;
601 	}
602 
603 	if ((flags & CAP_SEPARATE_IO_BUFS) == 0U) {
604 		printf("The app assumes distinct IO buffers. "
605 			"Please rewrite the app accordingly before proceeding\n");
606 		return -1;
607 	}
608 
609 	cap_flags = CAP_RAW_KEY | CAP_SYNC_OPS | CAP_SEPARATE_IO_BUFS;
610 
611 	return 0;
612 
613 }
614