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(renesas_smartbond_crypto)
15 #define CRYPTO_DEV_COMPAT renesas_smartbond_crypto
16 #else
17 #error "You need to enable one crypto device"
18 #endif
19
20 /* Following test are part of mbedTLS */
21
22 uint8_t test1[] = {};
23 uint8_t test2[] = {0xbd};
24 uint8_t test3[] = {0x5f, 0xd4};
25 uint8_t test4[] = {0xb0, 0xbd, 0x69};
26 uint8_t test5[] = {0xc9, 0x8c, 0x8e, 0x55};
27 uint8_t test6[] = {0x81, 0xa7, 0x23, 0xd9, 0x66};
28 uint8_t test7[] = {
29 0x83, 0x90, 0xcf, 0x0b, 0xe0, 0x76, 0x61, 0xcc, 0x76, 0x69, 0xaa, 0xc5,
30 0x4c, 0xe0, 0x9a, 0x37, 0x73, 0x3a, 0x62, 0x9d, 0x45, 0xf5, 0xd9, 0x83,
31 0xef, 0x20, 0x1f, 0x9b, 0x2d, 0x13, 0x80, 0x0e, 0x55, 0x5d, 0x9b, 0x10,
32 0x97, 0xfe, 0xc3, 0xb7, 0x83, 0xd7, 0xa5, 0x0d, 0xcb, 0x5e, 0x2b, 0x64,
33 0x4b, 0x96, 0xa1, 0xe9, 0x46, 0x3f, 0x17, 0x7c, 0xf3, 0x49, 0x06, 0xbf,
34 0x38, 0x8f, 0x36, 0x6d, 0xb5, 0xc2, 0xde, 0xee, 0x04, 0xa3, 0x0e, 0x28,
35 0x3f, 0x76, 0x4a, 0x97, 0xc3, 0xb3, 0x77, 0xa0, 0x34, 0xfe, 0xfc, 0x22,
36 0xc2, 0x59, 0x21, 0x4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x26, 0xfd, 0xb7,
96 0x3f, 0x33, 0xa2, 0xa7, 0x0f, 0x1e, 0x0c, 0x25, 0x9f, 0x8f, 0x95, 0x33,
97 0xb9, 0xb8, 0xf9, 0xaf, 0x92, 0x88, 0xb7, 0x27, 0x4f, 0x21, 0xba, 0xee,
98 0xc7, 0x8d, 0x39, 0x6f, 0x8b, 0xac, 0xdc, 0xc2, 0x24, 0x71, 0x20, 0x7d,
99 0x9b, 0x4e, 0xfc, 0xcd, 0x3f, 0xed, 0xc5, 0xc5, 0xa2, 0x21, 0x4f, 0xf5,
100 0xe5, 0x1c, 0x55, 0x3f, 0x35, 0xe2, 0x1a, 0xe6, 0x96, 0xfe, 0x51, 0xe8,
101 0xdf, 0x73, 0x3a, 0x8e, 0x06, 0xf5, 0x0f, 0x41, 0x9e, 0x59, 0x9e, 0x9f,
102 0x9e, 0x4b, 0x37, 0xce, 0x64, 0x3f, 0xc8, 0x10, 0xfa, 0xaa, 0x47, 0x98,
103 0x97, 0x71, 0x50, 0x9d, 0x69, 0xa1, 0x10, 0xac, 0x91, 0x62, 0x61, 0x42,
104 0x70, 0x26, 0x36, 0x9a, 0x21, 0x26, 0x3a, 0xc4, 0x46, 0x0f, 0xb4, 0xf7,
105 0x08, 0xf8, 0xae, 0x28, 0x59, 0x98, 0x56, 0xdb, 0x7c, 0xb6, 0xa4, 0x3a,
106 0xc8, 0xe0, 0x3d, 0x64, 0xa9, 0x60, 0x98, 0x07, 0xe7, 0x6c, 0x5f, 0x31,
107 0x2b, 0x9d, 0x18, 0x63, 0xbf, 0xa3, 0x04, 0xe8, 0x95, 0x36, 0x47, 0x64,
108 0x8b, 0x4f, 0x4a, 0xb0, 0xed, 0x99, 0x5e
109 };
110
111 uint8_t sha256_results[7][32] = {
112 {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
113 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
114 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
115 {0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82, 0xf3, 0x0f, 0x55, 0x4b,
116 0x31, 0x3d, 0x05, 0x70, 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4, 0xb5, 0xaa,
117 0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b},
118 {0x7c, 0x4f, 0xbf, 0x48, 0x44, 0x98, 0xd2, 0x1b, 0x48, 0x7b, 0x9d, 0x61,
119 0xde, 0x89, 0x14, 0xb2, 0xea, 0xda, 0xf2, 0x69, 0x87, 0x12, 0x93, 0x6d,
120 0x47, 0xc3, 0xad, 0xa2, 0x55, 0x8f, 0x67, 0x88},
121 {0x40, 0x96, 0x80, 0x42, 0x21, 0x09, 0x3d, 0xdc, 0xcf, 0xbf, 0x46, 0x83,
122 0x14, 0x90, 0xea, 0x63, 0xe9, 0xe9, 0x94, 0x14, 0x85, 0x8f, 0x8d, 0x75,
123 0xff, 0x7f, 0x64, 0x2c, 0x7c, 0xa6, 0x18, 0x03},
124 {0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c, 0xe9, 0x3d, 0xbb, 0x94,
125 0x43, 0x3a, 0x0e, 0x0b, 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e, 0x7f, 0x65,
126 0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04},
127 {0x75, 0x16, 0xfb, 0x8b, 0xb1, 0x13, 0x50, 0xdf, 0x2b, 0xf3, 0x86, 0xbc,
128 0x3c, 0x33, 0xbd, 0x0f, 0x52, 0xcb, 0x4c, 0x67, 0xc6, 0xe4, 0x74, 0x5e,
129 0x04, 0x88, 0xe6, 0x2c, 0x2a, 0xea, 0x26, 0x05},
130 {0x41, 0x09, 0xcd, 0xbe, 0xc3, 0x24, 0x0a, 0xd7, 0x4c, 0xc6, 0xc3, 0x7f,
131 0x39, 0x30, 0x0f, 0x70, 0xfe, 0xde, 0x16, 0xe2, 0x1e, 0xfc, 0x77, 0xf7,
132 0x86, 0x59, 0x98, 0x71, 0x4a, 0xad, 0x0b, 0x5e}
133 };
134
ZTEST_USER(crypto_hash,test_hash)135 ZTEST_USER(crypto_hash, test_hash)
136 {
137 int ret;
138 struct hash_ctx ctx;
139
140 #ifdef CRYPTO_DRV_NAME
141 const struct device *dev = device_get_binding(CRYPTO_DRV_NAME);
142
143 if (!dev) {
144 zassert(0, "Crypto device is not ready");
145 }
146 #else
147 const struct device *dev = DEVICE_DT_GET_ONE(CRYPTO_DEV_COMPAT);
148
149 if (!device_is_ready(dev)) {
150 zassert(0, "Crypto device is not ready");
151 }
152 #endif
153
154 ctx.flags = CAP_SYNC_OPS | CAP_SEPARATE_IO_BUFS;
155
156 ret = hash_begin_session(dev, &ctx, CRYPTO_HASH_ALGO_SHA256);
157 zassert_true(ret == 0, "Failed to init sha256 session");
158
159 #define TEST_HASH(_i) \
160 do { \
161 uint8_t out_buf[32] = {0}; \
162 struct hash_pkt pkt = { \
163 .in_buf = test ## _i, \
164 .in_len = sizeof(test ## _i), \
165 .out_buf = out_buf, \
166 }; \
167 ret = hash_compute(&ctx, &pkt); \
168 zassert_true(ret == 0, "Failed to compute hash for test " #_i); \
169 ret = memcmp(pkt.out_buf, sha256_results[_i - 1], 32); \
170 zassert_true(ret == 0, "Failed to compute hash for test " #_i); \
171 } while (0)
172
173
174 TEST_HASH(1);
175 TEST_HASH(2);
176 TEST_HASH(3);
177 TEST_HASH(4);
178 TEST_HASH(5);
179 TEST_HASH(6);
180 TEST_HASH(7);
181
182 hash_free_session(dev, &ctx);
183 }
184
185 ZTEST_SUITE(crypto_hash, NULL, NULL, NULL, NULL, NULL);
186