Skip to content

Commit 654e4f5

Browse files
sylvioalvesjhedberg
authored andcommitted
tests: crypto: hash: expand test coverage for multiple SHA algorithms
Refactor hash tests to support SHA-224, SHA-256, SHA-384, and SHA-512 algorithms with a unified test vector framework. This improves test coverage and makes it easier to verify hardware crypto drivers that support multiple SHA variants. Signed-off-by: Sylvio Alves <[email protected]>
1 parent 998d9ab commit 654e4f5

File tree

1 file changed

+159
-38
lines changed
  • tests/crypto/crypto_hash/src

1 file changed

+159
-38
lines changed

tests/crypto/crypto_hash/src/main.c

Lines changed: 159 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ uint8_t test7[] = {
112112
0x8b, 0x4f, 0x4a, 0xb0, 0xed, 0x99, 0x5e
113113
};
114114

115+
static const uint8_t sha224_results[7][28] = {
116+
{0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, 0x61, 0x02, 0xbb, 0x28, 0x82,
117+
0x34, 0xc4, 0x15, 0xa2, 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, 0x2f},
118+
{0xb1, 0xe4, 0x6b, 0xb9, 0xef, 0xe4, 0x5a, 0xf5, 0x54, 0x36, 0x34, 0x49, 0xc6, 0x94,
119+
0x5a, 0x0d, 0x61, 0x69, 0xfc, 0x3a, 0x5a, 0x39, 0x6a, 0x56, 0xcb, 0x97, 0xcb, 0x57},
120+
{0xd2, 0xb0, 0x86, 0xdb, 0xee, 0x60, 0x90, 0x7e, 0xd1, 0x09, 0x86, 0x77, 0xfe, 0x04,
121+
0x37, 0xb8, 0x14, 0x55, 0x66, 0x87, 0x17, 0x2d, 0x1f, 0x59, 0xcf, 0x38, 0x6c, 0x97},
122+
{0xbb, 0xac, 0x30, 0x15, 0x10, 0x7f, 0xe0, 0x2b, 0x0c, 0x0b, 0x2b, 0xdc, 0xb2, 0x6f,
123+
0x44, 0x19, 0x1b, 0xa8, 0xdb, 0xab, 0x43, 0xf5, 0xc6, 0xbb, 0xaa, 0x0a, 0x13, 0x41},
124+
{0xc2, 0x63, 0xf2, 0x74, 0x12, 0xc1, 0xfd, 0xfb, 0x29, 0x33, 0xb6, 0x2a, 0xec, 0x9f,
125+
0xa2, 0x60, 0x9c, 0x05, 0x7b, 0x91, 0x1e, 0x99, 0x2d, 0x8f, 0xb2, 0xe5, 0xe1, 0x5a},
126+
{0x95, 0x99, 0xbd, 0x2d, 0xe7, 0x5b, 0xdf, 0xef, 0xe4, 0xff, 0xf3, 0xc3, 0x78, 0x98,
127+
0x87, 0xf5, 0xa5, 0x26, 0xd0, 0xea, 0x63, 0xa8, 0xa5, 0xd2, 0x8f, 0xc5, 0x62, 0xd6},
128+
{0x1f, 0xf8, 0x37, 0xcf, 0xf3, 0x74, 0x77, 0x4d, 0x0c, 0x23, 0x2e, 0x77, 0xb9, 0x64,
129+
0xed, 0x29, 0xf5, 0xd1, 0xe7, 0x75, 0xad, 0x1a, 0x4e, 0x76, 0x6f, 0x18, 0x7a, 0x77},
130+
};
131+
115132
uint8_t sha256_results[7][32] = {
116133
{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
117134
0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
@@ -136,54 +153,158 @@ uint8_t sha256_results[7][32] = {
136153
0x86, 0x59, 0x98, 0x71, 0x4a, 0xad, 0x0b, 0x5e}
137154
};
138155

139-
ZTEST_USER(crypto_hash, test_hash)
140-
{
141-
int ret;
142-
struct hash_ctx ctx;
156+
static const uint8_t sha384_results[7][48] = {
157+
{0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e,
158+
0xb1, 0xb1, 0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43,
159+
0x4c, 0x0c, 0xc7, 0xbf, 0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf,
160+
0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a, 0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b},
161+
{0x43, 0x72, 0xe3, 0x8a, 0x92, 0xa2, 0x8b, 0x5d, 0x2c, 0x39, 0x1e, 0x62,
162+
0x45, 0x2a, 0x86, 0xd5, 0x0e, 0x02, 0x67, 0x22, 0x8b, 0xe1, 0x76, 0xc7,
163+
0x7d, 0x24, 0x02, 0xef, 0xfe, 0x9f, 0xa5, 0x0d, 0xe4, 0x07, 0xbb, 0xb8,
164+
0x51, 0xb3, 0x7d, 0x59, 0x04, 0xab, 0xa2, 0xde, 0xde, 0x74, 0xda, 0x2a},
165+
{0xe9, 0x32, 0x48, 0x92, 0xec, 0x87, 0xf2, 0x29, 0x75, 0xc1, 0x4e, 0x42,
166+
0x0f, 0x3d, 0x77, 0x7d, 0xe5, 0xe9, 0x75, 0xbf, 0x01, 0x86, 0xeb, 0x99,
167+
0xdc, 0x44, 0x7c, 0xbe, 0x96, 0xcf, 0x8a, 0x7a, 0xcb, 0xbb, 0x78, 0x31,
168+
0xcf, 0xaf, 0xbb, 0xbf, 0x40, 0x4c, 0xdd, 0x5a, 0x45, 0x91, 0x64, 0x75},
169+
{0x81, 0x3d, 0x8e, 0x6f, 0x29, 0x18, 0x99, 0xb8, 0x49, 0xf1, 0xab, 0x87,
170+
0xcb, 0x04, 0x51, 0x48, 0x88, 0xb7, 0x0f, 0x5c, 0x66, 0xbe, 0xc7, 0x96,
171+
0x44, 0x59, 0x55, 0xae, 0x83, 0xc3, 0x9e, 0xee, 0xec, 0x62, 0xc2, 0x5c,
172+
0x25, 0xe9, 0x42, 0x94, 0x49, 0xd1, 0x33, 0x40, 0xc4, 0x7a, 0x96, 0x38},
173+
{0x2a, 0x6c, 0xca, 0xf7, 0xc7, 0xa7, 0xde, 0x23, 0x2e, 0xec, 0x95, 0x52,
174+
0x1a, 0x64, 0x4e, 0x6d, 0x27, 0x95, 0x6e, 0x32, 0x1c, 0x9f, 0x49, 0x21,
175+
0x87, 0x04, 0x14, 0x77, 0x76, 0xbc, 0x52, 0x39, 0xc5, 0x72, 0xaa, 0xe4,
176+
0xbf, 0xbb, 0x19, 0x72, 0x49, 0xd7, 0xd7, 0xbd, 0x09, 0xef, 0x12, 0x88},
177+
{0x7c, 0xab, 0x44, 0x4f, 0xc2, 0x63, 0xf1, 0x68, 0x83, 0x29, 0x6a, 0x84,
178+
0xc5, 0xb3, 0x00, 0xc0, 0x56, 0x0f, 0x4c, 0x4f, 0x08, 0x96, 0x95, 0x99,
179+
0xef, 0xe0, 0x81, 0x10, 0x81, 0x16, 0xd2, 0xea, 0xed, 0xb3, 0x30, 0xb8,
180+
0x06, 0x11, 0x61, 0x9c, 0x12, 0x97, 0x46, 0x05, 0xf5, 0xdf, 0x91, 0x18},
181+
{0xa1, 0x4c, 0x97, 0x00, 0x2f, 0x08, 0x3a, 0xf7, 0x17, 0x2d, 0x30, 0x99,
182+
0x28, 0x3f, 0x36, 0xcf, 0xd1, 0xf8, 0x56, 0xe3, 0x4b, 0x89, 0x63, 0x26,
183+
0x39, 0xe5, 0x36, 0xbe, 0xbe, 0xa5, 0x69, 0xa6, 0xac, 0x89, 0x19, 0x91,
184+
0x5a, 0xc7, 0x78, 0xfb, 0xaa, 0xa2, 0xe2, 0x0d, 0x6f, 0xfc, 0x28, 0x41},
185+
};
143186

187+
static const uint8_t sha512_results[7][64] = {
188+
{0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6,
189+
0x6d, 0x80, 0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4,
190+
0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce, 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2,
191+
0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f, 0x63, 0xb9, 0x31, 0xbd,
192+
0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e},
193+
{0x29, 0x6e, 0x22, 0x67, 0xd7, 0x4c, 0x27, 0x8d, 0xaa, 0xaa, 0x94, 0x0d, 0x17,
194+
0xb0, 0xcf, 0xb7, 0x4a, 0x50, 0x83, 0xf8, 0xe0, 0x69, 0x72, 0x6d, 0x8c, 0x84,
195+
0x1c, 0xbe, 0x59, 0x6e, 0x04, 0x31, 0xcb, 0x77, 0x41, 0xa5, 0xb5, 0x0f, 0x71,
196+
0x66, 0x6c, 0xfd, 0x54, 0xba, 0xcb, 0x7b, 0x00, 0xae, 0xa8, 0x91, 0x49, 0x9c,
197+
0xf4, 0xef, 0x6a, 0x03, 0xc8, 0xa8, 0x3f, 0xe3, 0x7c, 0x3f, 0x7b, 0xaf},
198+
{0x1c, 0xac, 0x66, 0x62, 0x60, 0x2f, 0xcc, 0x5f, 0x6c, 0x69, 0xf4, 0x8a, 0xa1,
199+
0x7a, 0x92, 0x47, 0x50, 0x31, 0x36, 0x3d, 0xa5, 0xc7, 0xb7, 0x8e, 0x2c, 0xa3,
200+
0xb9, 0xd2, 0x1f, 0x27, 0xd6, 0x72, 0x6c, 0x2c, 0xc7, 0x6c, 0xa5, 0x51, 0x8a,
201+
0xda, 0x2c, 0x2f, 0xa7, 0x2e, 0xca, 0xf8, 0x34, 0x3f, 0x0c, 0xe1, 0xc1, 0xe8,
202+
0x2f, 0x69, 0x96, 0xad, 0x6e, 0x8b, 0xbc, 0x5c, 0x84, 0xc2, 0x7f, 0xb3},
203+
{0x74, 0x29, 0x16, 0x51, 0x71, 0x75, 0xab, 0x4c, 0xa7, 0x01, 0x89, 0xc9, 0xae,
204+
0x15, 0xca, 0xad, 0x39, 0x59, 0x9b, 0x67, 0x2d, 0xca, 0x75, 0x7b, 0x25, 0x62,
205+
0x75, 0x47, 0xfb, 0x84, 0x5b, 0xe0, 0x55, 0x25, 0x23, 0x21, 0x32, 0x67, 0x3f,
206+
0x09, 0x10, 0xcc, 0x24, 0x29, 0xf5, 0x0b, 0xe9, 0xc3, 0x86, 0xec, 0x14, 0xe8,
207+
0x52, 0xc5, 0xa9, 0x0a, 0x03, 0xc5, 0x63, 0xe1, 0xf6, 0xd7, 0x7b, 0x5c},
208+
{0x09, 0xa9, 0x85, 0xc1, 0x5f, 0xa5, 0xcc, 0x68, 0xa8, 0x96, 0x8f, 0xfa, 0xea,
209+
0xf3, 0xb5, 0xec, 0x23, 0x45, 0x69, 0xad, 0x56, 0x74, 0x50, 0x39, 0x4c, 0x07,
210+
0x5b, 0x90, 0x61, 0xc8, 0xf7, 0xdf, 0x58, 0xff, 0x11, 0x29, 0x95, 0x70, 0x18,
211+
0x82, 0x16, 0xc6, 0x31, 0xde, 0x60, 0x4d, 0xf0, 0xab, 0x08, 0xcd, 0xd1, 0x93,
212+
0x29, 0x1c, 0xe5, 0x76, 0x3a, 0xcd, 0xc4, 0x27, 0xdf, 0x06, 0x04, 0x89},
213+
{0xf8, 0xbc, 0x6c, 0x96, 0x37, 0x92, 0xe6, 0x3d, 0x84, 0x15, 0x32, 0xee, 0xa5,
214+
0x2f, 0x56, 0x70, 0xa4, 0x3e, 0xd0, 0xac, 0x72, 0xde, 0xa9, 0xb8, 0xd9, 0x2b,
215+
0x0d, 0xd9, 0x19, 0xc8, 0x1a, 0x28, 0xa0, 0xff, 0xc5, 0x6d, 0xb6, 0x9a, 0xc9,
216+
0x12, 0x30, 0x78, 0x2a, 0xe1, 0x2d, 0x35, 0x6e, 0x62, 0xd3, 0x94, 0x4b, 0xa0,
217+
0xdb, 0x29, 0x7f, 0xe9, 0xaf, 0x34, 0x85, 0xbc, 0xd6, 0x9e, 0xe4, 0x1c},
218+
{0xd2, 0x7d, 0xf8, 0xfb, 0x7e, 0xd5, 0xb9, 0x1c, 0xa8, 0x08, 0x33, 0x77, 0xf1,
219+
0x3c, 0x6f, 0x59, 0x63, 0xd7, 0x4e, 0x4a, 0xcc, 0x54, 0x24, 0x6b, 0x2e, 0x14,
220+
0x96, 0x25, 0xd2, 0x52, 0xeb, 0xf2, 0x66, 0x25, 0x3c, 0xae, 0x62, 0x83, 0x38,
221+
0xc1, 0x11, 0x94, 0x16, 0x8b, 0x73, 0x72, 0xf3, 0xf8, 0x61, 0x04, 0x1a, 0x46,
222+
0xaf, 0x54, 0x4b, 0x2f, 0xec, 0xdf, 0x76, 0x08, 0x57, 0x71, 0xef, 0x8c},
223+
};
224+
225+
static inline const struct device *get_crypto_dev(void)
226+
{
144227
#ifdef CRYPTO_DRV_NAME
145228
const struct device *dev = device_get_binding(CRYPTO_DRV_NAME);
146-
147-
if (!dev) {
148-
zassert(0, "Crypto device is not ready");
149-
}
150229
#else
151230
const struct device *dev = DEVICE_DT_GET_ONE(CRYPTO_DEV_COMPAT);
231+
#endif
232+
return dev;
233+
}
234+
235+
static void run_vector_set(enum hash_algo algo, size_t out_len, const uint8_t *const *inputs,
236+
const size_t *in_lens, const uint8_t *const expected[], size_t nvec)
237+
{
238+
const struct device *dev = get_crypto_dev();
152239

153-
if (!device_is_ready(dev)) {
154-
zassert(0, "Crypto device is not ready");
240+
zassert_true(dev && device_is_ready(dev), "Crypto device is not ready");
241+
242+
struct hash_ctx ctx = {.flags = CAP_SYNC_OPS | CAP_SEPARATE_IO_BUFS};
243+
int rc = hash_begin_session(dev, &ctx, algo);
244+
245+
/* Skip test if algorithm is not supported */
246+
if (rc == -ENOTSUP) {
247+
ztest_test_skip();
248+
return;
155249
}
156-
#endif
157250

158-
ctx.flags = CAP_SYNC_OPS | CAP_SEPARATE_IO_BUFS;
159-
160-
ret = hash_begin_session(dev, &ctx, CRYPTO_HASH_ALGO_SHA256);
161-
zassert_true(ret == 0, "Failed to init sha256 session");
162-
163-
#define TEST_HASH(_i) \
164-
do { \
165-
uint8_t out_buf[32] = {0}; \
166-
struct hash_pkt pkt = { \
167-
.in_buf = test ## _i, \
168-
.in_len = sizeof(test ## _i), \
169-
.out_buf = out_buf, \
170-
}; \
171-
ret = hash_compute(&ctx, &pkt); \
172-
zassert_true(ret == 0, "Failed to compute hash for test " #_i); \
173-
ret = memcmp(pkt.out_buf, sha256_results[_i - 1], 32); \
174-
zassert_true(ret == 0, "Failed to compute hash for test " #_i); \
175-
} while (0)
176-
177-
178-
TEST_HASH(1);
179-
TEST_HASH(2);
180-
TEST_HASH(3);
181-
TEST_HASH(4);
182-
TEST_HASH(5);
183-
TEST_HASH(6);
184-
TEST_HASH(7);
251+
zassert_equal(rc, 0, "begin_session failed");
252+
253+
for (size_t i = 0; i < nvec; i++) {
254+
uint8_t out[64] = {0}; /* big enough for SHA-512 */
255+
256+
struct hash_pkt pkt = {
257+
/* Safe: hash operations only read from in_buf, never modify it */
258+
.in_buf = inputs[i],
259+
.in_len = in_lens[i],
260+
.out_buf = out,
261+
};
262+
rc = hash_compute(&ctx, &pkt);
263+
zassert_equal(rc, 0, "hash_compute failed @vec %d", (int)i + 1);
264+
rc = memcmp(out, expected[i], out_len);
265+
zassert_equal(rc, 0, "digest mismatch @vec %d", (int)i + 1);
266+
}
185267

186268
hash_free_session(dev, &ctx);
187269
}
188270

271+
/* Convert our 2D byte arrays to array-of-pointers for helper */
272+
#define PTRS_FROM_2D(name) \
273+
static const uint8_t *name##_ptrs[] = {name[0], name[1], name[2], name[3], \
274+
name[4], name[5], name[6]}
275+
276+
PTRS_FROM_2D(sha224_results);
277+
PTRS_FROM_2D(sha256_results);
278+
PTRS_FROM_2D(sha384_results);
279+
PTRS_FROM_2D(sha512_results);
280+
281+
/* Inputs & sizes */
282+
static const uint8_t *inputs[] = {test1, test2, test3, test4, test5, test6, test7};
283+
static const size_t in_lens[] = {sizeof(test1), sizeof(test2), sizeof(test3), sizeof(test4),
284+
sizeof(test5), sizeof(test6), sizeof(test7)};
285+
286+
ZTEST(crypto_hash, test_sha224)
287+
{
288+
run_vector_set(CRYPTO_HASH_ALGO_SHA224, 28, inputs, in_lens,
289+
(const uint8_t *const *)sha224_results_ptrs, 7);
290+
}
291+
292+
ZTEST(crypto_hash, test_sha256)
293+
{
294+
run_vector_set(CRYPTO_HASH_ALGO_SHA256, 32, inputs, in_lens,
295+
(const uint8_t *const *)sha256_results_ptrs, 7);
296+
}
297+
298+
ZTEST(crypto_hash, test_sha384)
299+
{
300+
run_vector_set(CRYPTO_HASH_ALGO_SHA384, 48, inputs, in_lens,
301+
(const uint8_t *const *)sha384_results_ptrs, 7);
302+
}
303+
304+
ZTEST(crypto_hash, test_sha512)
305+
{
306+
run_vector_set(CRYPTO_HASH_ALGO_SHA512, 64, inputs, in_lens,
307+
(const uint8_t *const *)sha512_results_ptrs, 7);
308+
}
309+
189310
ZTEST_SUITE(crypto_hash, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)