1 /*
2 * Copyright (c) 2022 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/crypto/crypto.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/device.h>
11
12 #ifdef CONFIG_CRYPTO_MBEDTLS_SHIM
13 #define CRYPTO_DRV_NAME CONFIG_CRYPTO_MBEDTLS_SHIM_DRV_NAME
14 #elif DT_HAS_COMPAT_STATUS_OKAY(microchip_sha_g1_crypto)
15 #define CRYPTO_DEV_COMPAT microchip_sha_g1_crypto
16 #elif DT_HAS_COMPAT_STATUS_OKAY(renesas_smartbond_crypto)
17 #define CRYPTO_DEV_COMPAT renesas_smartbond_crypto
18 #elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_hash)
19 #define CRYPTO_DEV_COMPAT st_stm32_hash
20 #elif DT_HAS_COMPAT_STATUS_OKAY(espressif_esp32_sha)
21 #define CRYPTO_DEV_COMPAT espressif_esp32_sha
22 #elif DT_HAS_COMPAT_STATUS_OKAY(nxp_s32_crypto_hse_mu)
23 #define CRYPTO_DEV_COMPAT nxp_s32_crypto_hse_mu
24 #else
25 #error "You need to enable one crypto device"
26 #endif
27
28 /* Following test are part of mbedTLS */
29
30 uint8_t test1[] = {};
31 uint8_t test2[] = {0xbd};
32 uint8_t test3[] = {0x5f, 0xd4};
33 uint8_t test4[] = {0xb0, 0xbd, 0x69};
34 uint8_t test5[] = {0xc9, 0x8c, 0x8e, 0x55};
35 uint8_t test6[] = {0x81, 0xa7, 0x23, 0xd9, 0x66};
36 uint8_t test7[] = {
37 0x83, 0x90, 0xcf, 0x0b, 0xe0, 0x76, 0x61, 0xcc, 0x76, 0x69, 0xaa, 0xc5,
38 0x4c, 0xe0, 0x9a, 0x37, 0x73, 0x3a, 0x62, 0x9d, 0x45, 0xf5, 0xd9, 0x83,
39 0xef, 0x20, 0x1f, 0x9b, 0x2d, 0x13, 0x80, 0x0e, 0x55, 0x5d, 0x9b, 0x10,
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, 0xde, 0xd6, 0x53, 0x61, 0x98, 0xff,
100 0x13, 0xaa, 0x5a, 0x1c, 0xd4, 0x6a, 0x5b, 0x9a, 0x17, 0xa4, 0xdd, 0xc1,
101 0xd9, 0xf8, 0x55, 0x44, 0xd1, 0xd1, 0xcc, 0x16, 0xf3, 0xdf, 0x85, 0x80,
102 0x38, 0xc8, 0xe0, 0x71, 0xa1, 0x1a, 0x7e, 0x15, 0x7a, 0x85, 0xa6, 0xa8,
103 0xdc, 0x47, 0xe8, 0x8d, 0x75, 0xe7, 0x00, 0x9a, 0x8b, 0x26, 0xfd, 0xb7,
104 0x3f, 0x33, 0xa2, 0xa7, 0x0f, 0x1e, 0x0c, 0x25, 0x9f, 0x8f, 0x95, 0x33,
105 0xb9, 0xb8, 0xf9, 0xaf, 0x92, 0x88, 0xb7, 0x27, 0x4f, 0x21, 0xba, 0xee,
106 0xc7, 0x8d, 0x39, 0x6f, 0x8b, 0xac, 0xdc, 0xc2, 0x24, 0x71, 0x20, 0x7d,
107 0x9b, 0x4e, 0xfc, 0xcd, 0x3f, 0xed, 0xc5, 0xc5, 0xa2, 0x21, 0x4f, 0xf5,
108 0xe5, 0x1c, 0x55, 0x3f, 0x35, 0xe2, 0x1a, 0xe6, 0x96, 0xfe, 0x51, 0xe8,
109 0xdf, 0x73, 0x3a, 0x8e, 0x06, 0xf5, 0x0f, 0x41, 0x9e, 0x59, 0x9e, 0x9f,
110 0x9e, 0x4b, 0x37, 0xce, 0x64, 0x3f, 0xc8, 0x10, 0xfa, 0xaa, 0x47, 0x98,
111 0x97, 0x71, 0x50, 0x9d, 0x69, 0xa1, 0x10, 0xac, 0x91, 0x62, 0x61, 0x42,
112 0x70, 0x26, 0x36, 0x9a, 0x21, 0x26, 0x3a, 0xc4, 0x46, 0x0f, 0xb4, 0xf7,
113 0x08, 0xf8, 0xae, 0x28, 0x59, 0x98, 0x56, 0xdb, 0x7c, 0xb6, 0xa4, 0x3a,
114 0xc8, 0xe0, 0x3d, 0x64, 0xa9, 0x60, 0x98, 0x07, 0xe7, 0x6c, 0x5f, 0x31,
115 0x2b, 0x9d, 0x18, 0x63, 0xbf, 0xa3, 0x04, 0xe8, 0x95, 0x36, 0x47, 0x64,
116 0x8b, 0x4f, 0x4a, 0xb0, 0xed, 0x99, 0x5e
117 };
118
119 static const uint8_t sha224_results[7][28] = {
120 {0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, 0x61, 0x02, 0xbb, 0x28, 0x82,
121 0x34, 0xc4, 0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, 0x2f},
122 {0xb1, 0xe4, 0x6b, 0xb9, 0xef, 0xe4, 0x5a, 0xf5, 0x54, 0x36, 0x34, 0x49, 0xc6, 0x94,
123 0x5a, 0x0d, 0x61, 0x69, 0xfc, 0x3a, 0x5a, 0x39, 0x6a, 0x56, 0xcb, 0x97, 0xcb, 0x57},
124 {0xd2, 0xb0, 0x86, 0xdb, 0xee, 0x60, 0x90, 0x7e, 0xd1, 0x09, 0x86, 0x77, 0xfe, 0x04,
125 0x37, 0xb8, 0x14, 0x55, 0x66, 0x87, 0x17, 0x2d, 0x1f, 0x59, 0xcf, 0x38, 0x6c, 0x97},
126 {0xbb, 0xac, 0x30, 0x15, 0x10, 0x7f, 0xe0, 0x2b, 0x0c, 0x0b, 0x2b, 0xdc, 0xb2, 0x6f,
127 0x44, 0x19, 0x1b, 0xa8, 0xdb, 0xab, 0x43, 0xf5, 0xc6, 0xbb, 0xaa, 0x0a, 0x13, 0x41},
128 {0xc2, 0x63, 0xf2, 0x74, 0x12, 0xc1, 0xfd, 0xfb, 0x29, 0x33, 0xb6, 0x2a, 0xec, 0x9f,
129 0xa2, 0x60, 0x9c, 0x05, 0x7b, 0x91, 0x1e, 0x99, 0x2d, 0x8f, 0xb2, 0xe5, 0xe1, 0x5a},
130 {0x95, 0x99, 0xbd, 0x2d, 0xe7, 0x5b, 0xdf, 0xef, 0xe4, 0xff, 0xf3, 0xc3, 0x78, 0x98,
131 0x87, 0xf5, 0xa5, 0x26, 0xd0, 0xea, 0x63, 0xa8, 0xa5, 0xd2, 0x8f, 0xc5, 0x62, 0xd6},
132 {0x1f, 0xf8, 0x37, 0xcf, 0xf3, 0x74, 0x77, 0x4d, 0x0c, 0x23, 0x2e, 0x77, 0xb9, 0x64,
133 0xed, 0x29, 0xf5, 0xd1, 0xe7, 0x75, 0xad, 0x1a, 0x4e, 0x76, 0x6f, 0x18, 0x7a, 0x77},
134 };
135
136 uint8_t sha256_results[7][32] = {
137 {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
138 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
139 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
140 {0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82, 0xf3, 0x0f, 0x55, 0x4b,
141 0x31, 0x3d, 0x05, 0x70, 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4, 0xb5, 0xaa,
142 0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b},
143 {0x7c, 0x4f, 0xbf, 0x48, 0x44, 0x98, 0xd2, 0x1b, 0x48, 0x7b, 0x9d, 0x61,
144 0xde, 0x89, 0x14, 0xb2, 0xea, 0xda, 0xf2, 0x69, 0x87, 0x12, 0x93, 0x6d,
145 0x47, 0xc3, 0xad, 0xa2, 0x55, 0x8f, 0x67, 0x88},
146 {0x40, 0x96, 0x80, 0x42, 0x21, 0x09, 0x3d, 0xdc, 0xcf, 0xbf, 0x46, 0x83,
147 0x14, 0x90, 0xea, 0x63, 0xe9, 0xe9, 0x94, 0x14, 0x85, 0x8f, 0x8d, 0x75,
148 0xff, 0x7f, 0x64, 0x2c, 0x7c, 0xa6, 0x18, 0x03},
149 {0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c, 0xe9, 0x3d, 0xbb, 0x94,
150 0x43, 0x3a, 0x0e, 0x0b, 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e, 0x7f, 0x65,
151 0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04},
152 {0x75, 0x16, 0xfb, 0x8b, 0xb1, 0x13, 0x50, 0xdf, 0x2b, 0xf3, 0x86, 0xbc,
153 0x3c, 0x33, 0xbd, 0x0f, 0x52, 0xcb, 0x4c, 0x67, 0xc6, 0xe4, 0x74, 0x5e,
154 0x04, 0x88, 0xe6, 0x2c, 0x2a, 0xea, 0x26, 0x05},
155 {0x41, 0x09, 0xcd, 0xbe, 0xc3, 0x24, 0x0a, 0xd7, 0x4c, 0xc6, 0xc3, 0x7f,
156 0x39, 0x30, 0x0f, 0x70, 0xfe, 0xde, 0x16, 0xe2, 0x1e, 0xfc, 0x77, 0xf7,
157 0x86, 0x59, 0x98, 0x71, 0x4a, 0xad, 0x0b, 0x5e}
158 };
159
160 static const uint8_t sha384_results[7][48] = {
161 {0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e,
162 0xb1, 0xb1, 0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43,
163 0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf,
164 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b},
165 {0x43, 0x72, 0xe3, 0x8a, 0x92, 0xa2, 0x8b, 0x5d, 0x2c, 0x39, 0x1e, 0x62,
166 0x45, 0x2a, 0x86, 0xd5, 0x0e, 0x02, 0x67, 0x22, 0x8b, 0xe1, 0x76, 0xc7,
167 0x7d, 0x24, 0x02, 0xef, 0xfe, 0x9f, 0xa5, 0x0d, 0xe4, 0x07, 0xbb, 0xb8,
168 0x51, 0xb3, 0x7d, 0x59, 0x04, 0xab, 0xa2, 0xde, 0xde, 0x74, 0xda, 0x2a},
169 {0xe9, 0x32, 0x48, 0x92, 0xec, 0x87, 0xf2, 0x29, 0x75, 0xc1, 0x4e, 0x42,
170 0x0f, 0x3d, 0x77, 0x7d, 0xe5, 0xe9, 0x75, 0xbf, 0x01, 0x86, 0xeb, 0x99,
171 0xdc, 0x44, 0x7c, 0xbe, 0x96, 0xcf, 0x8a, 0x7a, 0xcb, 0xbb, 0x78, 0x31,
172 0xcf, 0xaf, 0xbb, 0xbf, 0x40, 0x4c, 0xdd, 0x5a, 0x45, 0x91, 0x64, 0x75},
173 {0x81, 0x3d, 0x8e, 0x6f, 0x29, 0x18, 0x99, 0xb8, 0x49, 0xf1, 0xab, 0x87,
174 0xcb, 0x04, 0x51, 0x48, 0x88, 0xb7, 0x0f, 0x5c, 0x66, 0xbe, 0xc7, 0x96,
175 0x44, 0x59, 0x55, 0xae, 0x83, 0xc3, 0x9e, 0xee, 0xec, 0x62, 0xc2, 0x5c,
176 0x25, 0xe9, 0x42, 0x94, 0x49, 0xd1, 0x33, 0x40, 0xc4, 0x7a, 0x96, 0x38},
177 {0x2a, 0x6c, 0xca, 0xf7, 0xc7, 0xa7, 0xde, 0x23, 0x2e, 0xec, 0x95, 0x52,
178 0x1a, 0x64, 0x4e, 0x6d, 0x27, 0x95, 0x6e, 0x32, 0x1c, 0x9f, 0x49, 0x21,
179 0x87, 0x04, 0x14, 0x77, 0x76, 0xbc, 0x52, 0x39, 0xc5, 0x72, 0xaa, 0xe4,
180 0xbf, 0xbb, 0x19, 0x72, 0x49, 0xd7, 0xd7, 0xbd, 0x09, 0xef, 0x12, 0x88},
181 {0x7c, 0xab, 0x44, 0x4f, 0xc2, 0x63, 0xf1, 0x68, 0x83, 0x29, 0x6a, 0x84,
182 0xc5, 0xb3, 0x00, 0xc0, 0x56, 0x0f, 0x4c, 0x4f, 0x08, 0x96, 0x95, 0x99,
183 0xef, 0xe0, 0x81, 0x10, 0x81, 0x16, 0xd2, 0xea, 0xed, 0xb3, 0x30, 0xb8,
184 0x06, 0x11, 0x61, 0x9c, 0x12, 0x97, 0x46, 0x05, 0xf5, 0xdf, 0x91, 0x18},
185 {0xa1, 0x4c, 0x97, 0x00, 0x2f, 0x08, 0x3a, 0xf7, 0x17, 0x2d, 0x30, 0x99,
186 0x28, 0x3f, 0x36, 0xcf, 0xd1, 0xf8, 0x56, 0xe3, 0x4b, 0x89, 0x63, 0x26,
187 0x39, 0xe5, 0x36, 0xbe, 0xbe, 0xa5, 0x69, 0xa6, 0xac, 0x89, 0x19, 0x91,
188 0x5a, 0xc7, 0x78, 0xfb, 0xaa, 0xa2, 0xe2, 0x0d, 0x6f, 0xfc, 0x28, 0x41},
189 };
190
191 static const uint8_t sha512_results[7][64] = {
192 {0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6,
193 0x6d, 0x80, 0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4,
194 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2,
195 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 0x63, 0xb9, 0x31, 0xbd,
196 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e},
197 {0x29, 0x6e, 0x22, 0x67, 0xd7, 0x4c, 0x27, 0x8d, 0xaa, 0xaa, 0x94, 0x0d, 0x17,
198 0xb0, 0xcf, 0xb7, 0x4a, 0x50, 0x83, 0xf8, 0xe0, 0x69, 0x72, 0x6d, 0x8c, 0x84,
199 0x1c, 0xbe, 0x59, 0x6e, 0x04, 0x31, 0xcb, 0x77, 0x41, 0xa5, 0xb5, 0x0f, 0x71,
200 0x66, 0x6c, 0xfd, 0x54, 0xba, 0xcb, 0x7b, 0x00, 0xae, 0xa8, 0x91, 0x49, 0x9c,
201 0xf4, 0xef, 0x6a, 0x03, 0xc8, 0xa8, 0x3f, 0xe3, 0x7c, 0x3f, 0x7b, 0xaf},
202 {0x1c, 0xac, 0x66, 0x62, 0x60, 0x2f, 0xcc, 0x5f, 0x6c, 0x69, 0xf4, 0x8a, 0xa1,
203 0x7a, 0x92, 0x47, 0x50, 0x31, 0x36, 0x3d, 0xa5, 0xc7, 0xb7, 0x8e, 0x2c, 0xa3,
204 0xb9, 0xd2, 0x1f, 0x27, 0xd6, 0x72, 0x6c, 0x2c, 0xc7, 0x6c, 0xa5, 0x51, 0x8a,
205 0xda, 0x2c, 0x2f, 0xa7, 0x2e, 0xca, 0xf8, 0x34, 0x3f, 0x0c, 0xe1, 0xc1, 0xe8,
206 0x2f, 0x69, 0x96, 0xad, 0x6e, 0x8b, 0xbc, 0x5c, 0x84, 0xc2, 0x7f, 0xb3},
207 {0x74, 0x29, 0x16, 0x51, 0x71, 0x75, 0xab, 0x4c, 0xa7, 0x01, 0x89, 0xc9, 0xae,
208 0x15, 0xca, 0xad, 0x39, 0x59, 0x9b, 0x67, 0x2d, 0xca, 0x75, 0x7b, 0x25, 0x62,
209 0x75, 0x47, 0xfb, 0x84, 0x5b, 0xe0, 0x55, 0x25, 0x23, 0x21, 0x32, 0x67, 0x3f,
210 0x09, 0x10, 0xcc, 0x24, 0x29, 0xf5, 0x0b, 0xe9, 0xc3, 0x86, 0xec, 0x14, 0xe8,
211 0x52, 0xc5, 0xa9, 0x0a, 0x03, 0xc5, 0x63, 0xe1, 0xf6, 0xd7, 0x7b, 0x5c},
212 {0x09, 0xa9, 0x85, 0xc1, 0x5f, 0xa5, 0xcc, 0x68, 0xa8, 0x96, 0x8f, 0xfa, 0xea,
213 0xf3, 0xb5, 0xec, 0x23, 0x45, 0x69, 0xad, 0x56, 0x74, 0x50, 0x39, 0x4c, 0x07,
214 0x5b, 0x90, 0x61, 0xc8, 0xf7, 0xdf, 0x58, 0xff, 0x11, 0x29, 0x95, 0x70, 0x18,
215 0x82, 0x16, 0xc6, 0x31, 0xde, 0x60, 0x4d, 0xf0, 0xab, 0x08, 0xcd, 0xd1, 0x93,
216 0x29, 0x1c, 0xe5, 0x76, 0x3a, 0xcd, 0xc4, 0x27, 0xdf, 0x06, 0x04, 0x89},
217 {0xf8, 0xbc, 0x6c, 0x96, 0x37, 0x92, 0xe6, 0x3d, 0x84, 0x15, 0x32, 0xee, 0xa5,
218 0x2f, 0x56, 0x70, 0xa4, 0x3e, 0xd0, 0xac, 0x72, 0xde, 0xa9, 0xb8, 0xd9, 0x2b,
219 0x0d, 0xd9, 0x19, 0xc8, 0x1a, 0x28, 0xa0, 0xff, 0xc5, 0x6d, 0xb6, 0x9a, 0xc9,
220 0x12, 0x30, 0x78, 0x2a, 0xe1, 0x2d, 0x35, 0x6e, 0x62, 0xd3, 0x94, 0x4b, 0xa0,
221 0xdb, 0x29, 0x7f, 0xe9, 0xaf, 0x34, 0x85, 0xbc, 0xd6, 0x9e, 0xe4, 0x1c},
222 {0xd2, 0x7d, 0xf8, 0xfb, 0x7e, 0xd5, 0xb9, 0x1c, 0xa8, 0x08, 0x33, 0x77, 0xf1,
223 0x3c, 0x6f, 0x59, 0x63, 0xd7, 0x4e, 0x4a, 0xcc, 0x54, 0x24, 0x6b, 0x2e, 0x14,
224 0x96, 0x25, 0xd2, 0x52, 0xeb, 0xf2, 0x66, 0x25, 0x3c, 0xae, 0x62, 0x83, 0x38,
225 0xc1, 0x11, 0x94, 0x16, 0x8b, 0x73, 0x72, 0xf3, 0xf8, 0x61, 0x04, 0x1a, 0x46,
226 0xaf, 0x54, 0x4b, 0x2f, 0xec, 0xdf, 0x76, 0x08, 0x57, 0x71, 0xef, 0x8c},
227 };
228
get_crypto_dev(void)229 static inline const struct device *get_crypto_dev(void)
230 {
231 #ifdef CRYPTO_DRV_NAME
232 const struct device *dev = device_get_binding(CRYPTO_DRV_NAME);
233 #else
234 const struct device *dev = DEVICE_DT_GET_ONE(CRYPTO_DEV_COMPAT);
235 #endif
236 return dev;
237 }
238
run_vector_set(enum hash_algo algo,size_t out_len,const uint8_t * const * inputs,const size_t * in_lens,const uint8_t * const expected[],size_t nvec)239 static void run_vector_set(enum hash_algo algo, size_t out_len, const uint8_t *const *inputs,
240 const size_t *in_lens, const uint8_t *const expected[], size_t nvec)
241 {
242 const struct device *dev = get_crypto_dev();
243
244 zassert_true(dev && device_is_ready(dev), "Crypto device is not ready");
245
246 struct hash_ctx ctx = {.flags = CAP_SYNC_OPS | CAP_SEPARATE_IO_BUFS};
247 int rc = hash_begin_session(dev, &ctx, algo);
248
249 /* Skip test if algorithm is not supported */
250 if (rc == -ENOTSUP) {
251 ztest_test_skip();
252 return;
253 }
254
255 zassert_equal(rc, 0, "begin_session failed");
256
257 for (size_t i = 0; i < nvec; i++) {
258 uint8_t out[64] = {0}; /* big enough for SHA-512 */
259
260 struct hash_pkt pkt = {
261 /* Safe: hash operations only read from in_buf, never modify it */
262 .in_buf = inputs[i],
263 .in_len = in_lens[i],
264 .out_buf = out,
265 };
266 rc = hash_compute(&ctx, &pkt);
267 zassert_equal(rc, 0, "hash_compute failed @vec %d", (int)i + 1);
268 rc = memcmp(out, expected[i], out_len);
269 zassert_equal(rc, 0, "digest mismatch @vec %d", (int)i + 1);
270 }
271
272 hash_free_session(dev, &ctx);
273 }
274
275 /* Convert our 2D byte arrays to array-of-pointers for helper */
276 #define PTRS_FROM_2D(name) \
277 static const uint8_t *name##_ptrs[] = {name[0], name[1], name[2], name[3], \
278 name[4], name[5], name[6]}
279
280 PTRS_FROM_2D(sha224_results);
281 PTRS_FROM_2D(sha256_results);
282 PTRS_FROM_2D(sha384_results);
283 PTRS_FROM_2D(sha512_results);
284
285 /* Inputs & sizes */
286 static const uint8_t *inputs[] = {test1, test2, test3, test4, test5, test6, test7};
287 static const size_t in_lens[] = {sizeof(test1), sizeof(test2), sizeof(test3), sizeof(test4),
288 sizeof(test5), sizeof(test6), sizeof(test7)};
289
ZTEST(crypto_hash,test_sha224)290 ZTEST(crypto_hash, test_sha224)
291 {
292 run_vector_set(CRYPTO_HASH_ALGO_SHA224, 28, inputs, in_lens,
293 (const uint8_t *const *)sha224_results_ptrs, 7);
294 }
295
ZTEST(crypto_hash,test_sha256)296 ZTEST(crypto_hash, test_sha256)
297 {
298 run_vector_set(CRYPTO_HASH_ALGO_SHA256, 32, inputs, in_lens,
299 (const uint8_t *const *)sha256_results_ptrs, 7);
300 }
301
ZTEST(crypto_hash,test_sha384)302 ZTEST(crypto_hash, test_sha384)
303 {
304 run_vector_set(CRYPTO_HASH_ALGO_SHA384, 48, inputs, in_lens,
305 (const uint8_t *const *)sha384_results_ptrs, 7);
306 }
307
ZTEST(crypto_hash,test_sha512)308 ZTEST(crypto_hash, test_sha512)
309 {
310 run_vector_set(CRYPTO_HASH_ALGO_SHA512, 64, inputs, in_lens,
311 (const uint8_t *const *)sha512_results_ptrs, 7);
312 }
313
314 ZTEST_SUITE(crypto_hash, NULL, NULL, NULL, NULL, NULL);
315