blob: 53a17abda2bcad82665af78224726bc921f5761f [file] [log] [blame]
Bence Szépkúti86974652020-06-15 11:59:37 +02001/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02002 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00003 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02004 */
5
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +02006#include <test/constant_flow.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02007#include <test/helpers.h>
Ronald Cronf40529d2020-06-09 16:27:37 +02008#include <test/macros.h>
9#include <string.h>
10
Gilles Peskinec2d16b22023-04-28 23:39:45 +020011#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
12#include <psa/crypto.h>
13#include <test/psa_crypto_helpers.h>
14#endif
15
David Horstmannb4892572023-12-14 14:17:04 +000016#if defined(MBEDTLS_TEST_HOOKS)
17#include <test/psa_memory_poisoning_wrappers.h>
18#endif
19
Ronald Crona1236142020-07-01 16:01:21 +020020/*----------------------------------------------------------------------------*/
21/* Static global variables */
22
Ronald Cronf40529d2020-06-09 16:27:37 +020023#if defined(MBEDTLS_PLATFORM_C)
24static mbedtls_platform_context platform_ctx;
25#endif
26
Chris Jonese60e2ae2021-01-20 17:51:47 +000027mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000028
Ronald Crona1236142020-07-01 16:01:21 +020029/*----------------------------------------------------------------------------*/
30/* Helper Functions */
31
Gilles Peskine449bd832023-01-11 14:50:10 +010032int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020033{
34 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +020035
David Horstmannb4892572023-12-14 14:17:04 +000036#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
37 mbedtls_poison_test_hooks_setup();
38#endif
39
Gilles Peskinec2d16b22023-04-28 23:39:45 +020040#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
41 /* Make sure that injected entropy is present. Otherwise
42 * psa_crypto_init() will fail. This is not necessary for test suites
43 * that don't use PSA, but it's harmless (except for leaving a file
44 * behind). */
45 ret = mbedtls_test_inject_entropy_restore();
46 if (ret != 0) {
47 return ret;
48 }
49#endif
50
Ronald Cronf40529d2020-06-09 16:27:37 +020051#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010052 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020053#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +020054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020056}
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020059{
David Horstmannb4892572023-12-14 14:17:04 +000060#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
61 mbedtls_poison_test_hooks_teardown();
62#endif
63
Ronald Cronf40529d2020-06-09 16:27:37 +020064#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010065 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020066#endif /* MBEDTLS_PLATFORM_C */
67}
68
Gilles Peskine881447d2022-12-08 15:24:52 +010069int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020070{
Gilles Peskine449bd832023-01-11 14:50:10 +010071 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020072 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +010073 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +020074 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020076 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010077 } else {
78 return -1;
79 }
Ronald Crona0c25392020-06-18 10:10:46 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +020082}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000085{
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +000087 /* We've already recorded the test as having failed. Don't
88 * overwrite any previous information about the failure. */
89 return;
90 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000091 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
92 mbedtls_test_info.test = test;
93 mbedtls_test_info.line_no = line_no;
94 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000095}
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000098{
Chris Jonese60e2ae2021-01-20 17:51:47 +000099 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
100 mbedtls_test_info.test = test;
101 mbedtls_test_info.line_no = line_no;
102 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000103}
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000106{
107 mbedtls_test_info.step = step;
108}
109
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100110#if defined(MBEDTLS_BIGNUM_C)
111unsigned mbedtls_test_case_uses_negative_0 = 0;
112#endif
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000115{
116 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000118 mbedtls_test_info.test = 0;
119 mbedtls_test_info.line_no = 0;
120 mbedtls_test_info.filename = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
122 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100123#if defined(MBEDTLS_BIGNUM_C)
124 mbedtls_test_case_uses_negative_0 = 0;
125#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200126}
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128int mbedtls_test_equal(const char *test, int line_no, const char *filename,
129 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200130{
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 TEST_CF_PUBLIC(&value1, sizeof(value1));
132 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (value1 == value2) {
135 return 1;
136 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200139 /* We've already recorded the test as having failed. Don't
140 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200142 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_test_fail(test, line_no, filename);
144 (void) mbedtls_snprintf(mbedtls_test_info.line1,
145 sizeof(mbedtls_test_info.line1),
146 "lhs = 0x%016llx = %lld",
147 value1, (long long) value1);
148 (void) mbedtls_snprintf(mbedtls_test_info.line2,
149 sizeof(mbedtls_test_info.line2),
150 "rhs = 0x%016llx = %lld",
151 value2, (long long) value2);
152 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000153}
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
156 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200157{
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 TEST_CF_PUBLIC(&value1, sizeof(value1));
159 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (value1 <= value2) {
162 return 1;
163 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200166 /* We've already recorded the test as having failed. Don't
167 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200169 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 mbedtls_test_fail(test, line_no, filename);
171 (void) mbedtls_snprintf(mbedtls_test_info.line1,
172 sizeof(mbedtls_test_info.line1),
173 "lhs = 0x%016llx = %llu",
174 value1, value1);
175 (void) mbedtls_snprintf(mbedtls_test_info.line2,
176 sizeof(mbedtls_test_info.line2),
177 "rhs = 0x%016llx = %llu",
178 value2, value2);
179 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200180}
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
183 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200184{
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 TEST_CF_PUBLIC(&value1, sizeof(value1));
186 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (value1 <= value2) {
189 return 1;
190 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200193 /* We've already recorded the test as having failed. Don't
194 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200196 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 mbedtls_test_fail(test, line_no, filename);
198 (void) mbedtls_snprintf(mbedtls_test_info.line1,
199 sizeof(mbedtls_test_info.line1),
200 "lhs = 0x%016llx = %lld",
201 (unsigned long long) value1, value1);
202 (void) mbedtls_snprintf(mbedtls_test_info.line2,
203 sizeof(mbedtls_test_info.line2),
204 "rhs = 0x%016llx = %lld",
205 (unsigned long long) value2, value2);
206 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200207}
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209int mbedtls_test_unhexify(unsigned char *obuf,
210 size_t obufmax,
211 const char *ibuf,
212 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200213{
214 unsigned char uc, uc2;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200217
218 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if ((*len) & 1) {
220 return -1;
221 }
Ronald Crona0c25392020-06-18 10:10:46 +0200222 *len /= 2;
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if ((*len) > obufmax) {
225 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200226 }
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 while (*ibuf != 0) {
229 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
230 return -1;
231 }
232
233 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
234 return -1;
235 }
236
237 *(obuf++) = (uc << 4) | uc2;
238 }
239
240 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200241}
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243void mbedtls_test_hexify(unsigned char *obuf,
244 const unsigned char *ibuf,
245 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200246{
247 unsigned char l, h;
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200250 h = *ibuf / 16;
251 l = *ibuf % 16;
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200254 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200256 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200260 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200262 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200264
265 ++ibuf;
266 len--;
267 }
268}
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200271{
272 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 p = mbedtls_calloc(1, actual_len);
276 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200281}
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200284{
285 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200286 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (*olen == 0) {
291 return mbedtls_test_zero_alloc(*olen);
292 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 obuf = mbedtls_calloc(1, *olen);
295 TEST_HELPER_ASSERT(obuf != NULL);
296 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200299}
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
302 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200303{
304 int ret = 0;
305 uint32_t i = 0;
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (a_len != b_len) {
308 return -1;
309 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 for (i = 0; i < a_len; i++) {
312 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200313 ret = -1;
314 break;
315 }
316 }
317 return ret;
318}
Ronald Crona1236142020-07-01 16:01:21 +0200319
Chris Jones96ae73b2021-01-08 17:04:59 +0000320#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100321void mbedtls_test_err_add_check(int high, int low,
322 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000323{
Chris Jones3f613c12021-03-31 09:34:22 +0100324 /* Error codes are always negative (a value of zero is a success) however
325 * their positive opposites can be easier to understand. The following
326 * examples given in comments have been made positive for ease of
327 * understanding. The structure of an error code is such:
328 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100329 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100330 *
331 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100332 * h = high level error code (includes high level module ID (bits 12..14)
333 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100334 * l = low level error code.
335 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (high > -0x1000 && high != 0) {
337 /* high < 0001000000000000
338 * No high level module ID bits are set.
339 */
340 mbedtls_test_fail("'high' is not a high-level error code",
341 line, file);
342 } else if (high < -0x7F80) {
343 /* high > 0111111110000000
344 * Error code is greater than the largest allowed high level module ID.
345 */
346 mbedtls_test_fail("'high' error code is greater than 15 bits",
347 line, file);
348 } else if ((high & 0x7F) != 0) {
349 /* high & 0000000001111111
350 * Error code contains low level error code bits.
351 */
352 mbedtls_test_fail("'high' contains a low-level error code",
353 line, file);
354 } else if (low < -0x007F) {
355 /* low > 0000000001111111
356 * Error code contains high or module level error code bits.
357 */
358 mbedtls_test_fail("'low' error code is greater than 7 bits",
359 line, file);
360 } else if (low > 0) {
361 mbedtls_test_fail("'low' error code is greater than zero",
362 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000363 }
364}
365#endif /* MBEDTLS_TEST_HOOKS */