blob: 1c58faefabdb5c2d011d7dd0766053cab55d1f57 [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 Horstmann66684532023-12-15 18:29:54 +000036#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
37 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannb4892572023-12-14 14:17:04 +000038 mbedtls_poison_test_hooks_setup();
39#endif
40
Gilles Peskinec2d16b22023-04-28 23:39:45 +020041#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
42 /* Make sure that injected entropy is present. Otherwise
43 * psa_crypto_init() will fail. This is not necessary for test suites
44 * that don't use PSA, but it's harmless (except for leaving a file
45 * behind). */
46 ret = mbedtls_test_inject_entropy_restore();
47 if (ret != 0) {
48 return ret;
49 }
50#endif
51
Ronald Cronf40529d2020-06-09 16:27:37 +020052#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010053 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020054#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +020055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020057}
58
Gilles Peskine449bd832023-01-11 14:50:10 +010059void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020060{
David Horstmann66684532023-12-15 18:29:54 +000061#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
62 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannb4892572023-12-14 14:17:04 +000063 mbedtls_poison_test_hooks_teardown();
64#endif
65
Ronald Cronf40529d2020-06-09 16:27:37 +020066#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010067 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020068#endif /* MBEDTLS_PLATFORM_C */
69}
70
Gilles Peskine881447d2022-12-08 15:24:52 +010071int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020074 *uc = c - '0';
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 if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020078 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010079 } else {
80 return -1;
81 }
Ronald Crona0c25392020-06-18 10:10:46 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +020084}
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +000089 /* We've already recorded the test as having failed. Don't
90 * overwrite any previous information about the failure. */
91 return;
92 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000093 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
94 mbedtls_test_info.test = test;
95 mbedtls_test_info.line_no = line_no;
96 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000097}
98
Gilles Peskine449bd832023-01-11 14:50:10 +010099void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000100{
Chris Jonese60e2ae2021-01-20 17:51:47 +0000101 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
102 mbedtls_test_info.test = test;
103 mbedtls_test_info.line_no = line_no;
104 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000105}
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000108{
109 mbedtls_test_info.step = step;
110}
111
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100112#if defined(MBEDTLS_BIGNUM_C)
113unsigned mbedtls_test_case_uses_negative_0 = 0;
114#endif
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000117{
118 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000120 mbedtls_test_info.test = 0;
121 mbedtls_test_info.line_no = 0;
122 mbedtls_test_info.filename = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
124 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100125#if defined(MBEDTLS_BIGNUM_C)
126 mbedtls_test_case_uses_negative_0 = 0;
127#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200128}
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_test_equal(const char *test, int line_no, const char *filename,
131 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200132{
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 TEST_CF_PUBLIC(&value1, sizeof(value1));
134 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (value1 == value2) {
137 return 1;
138 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200141 /* We've already recorded the test as having failed. Don't
142 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200144 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 mbedtls_test_fail(test, line_no, filename);
146 (void) mbedtls_snprintf(mbedtls_test_info.line1,
147 sizeof(mbedtls_test_info.line1),
148 "lhs = 0x%016llx = %lld",
149 value1, (long long) value1);
150 (void) mbedtls_snprintf(mbedtls_test_info.line2,
151 sizeof(mbedtls_test_info.line2),
152 "rhs = 0x%016llx = %lld",
153 value2, (long long) value2);
154 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000155}
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
158 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200159{
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 TEST_CF_PUBLIC(&value1, sizeof(value1));
161 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (value1 <= value2) {
164 return 1;
165 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200168 /* We've already recorded the test as having failed. Don't
169 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200171 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_test_fail(test, line_no, filename);
173 (void) mbedtls_snprintf(mbedtls_test_info.line1,
174 sizeof(mbedtls_test_info.line1),
175 "lhs = 0x%016llx = %llu",
176 value1, value1);
177 (void) mbedtls_snprintf(mbedtls_test_info.line2,
178 sizeof(mbedtls_test_info.line2),
179 "rhs = 0x%016llx = %llu",
180 value2, value2);
181 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200182}
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
185 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200186{
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 TEST_CF_PUBLIC(&value1, sizeof(value1));
188 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (value1 <= value2) {
191 return 1;
192 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200195 /* We've already recorded the test as having failed. Don't
196 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200198 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 mbedtls_test_fail(test, line_no, filename);
200 (void) mbedtls_snprintf(mbedtls_test_info.line1,
201 sizeof(mbedtls_test_info.line1),
202 "lhs = 0x%016llx = %lld",
203 (unsigned long long) value1, value1);
204 (void) mbedtls_snprintf(mbedtls_test_info.line2,
205 sizeof(mbedtls_test_info.line2),
206 "rhs = 0x%016llx = %lld",
207 (unsigned long long) value2, value2);
208 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200209}
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211int mbedtls_test_unhexify(unsigned char *obuf,
212 size_t obufmax,
213 const char *ibuf,
214 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200215{
216 unsigned char uc, uc2;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200219
220 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if ((*len) & 1) {
222 return -1;
223 }
Ronald Crona0c25392020-06-18 10:10:46 +0200224 *len /= 2;
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if ((*len) > obufmax) {
227 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200228 }
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 while (*ibuf != 0) {
231 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
232 return -1;
233 }
234
235 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
236 return -1;
237 }
238
239 *(obuf++) = (uc << 4) | uc2;
240 }
241
242 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200243}
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245void mbedtls_test_hexify(unsigned char *obuf,
246 const unsigned char *ibuf,
247 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200248{
249 unsigned char l, h;
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200252 h = *ibuf / 16;
253 l = *ibuf % 16;
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200256 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200258 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200262 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200264 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200266
267 ++ibuf;
268 len--;
269 }
270}
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200273{
274 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 p = mbedtls_calloc(1, actual_len);
278 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200283}
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200286{
287 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200288 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (*olen == 0) {
293 return mbedtls_test_zero_alloc(*olen);
294 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 obuf = mbedtls_calloc(1, *olen);
297 TEST_HELPER_ASSERT(obuf != NULL);
298 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200301}
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
304 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200305{
306 int ret = 0;
307 uint32_t i = 0;
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (a_len != b_len) {
310 return -1;
311 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 for (i = 0; i < a_len; i++) {
314 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200315 ret = -1;
316 break;
317 }
318 }
319 return ret;
320}
Ronald Crona1236142020-07-01 16:01:21 +0200321
Chris Jones96ae73b2021-01-08 17:04:59 +0000322#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323void mbedtls_test_err_add_check(int high, int low,
324 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000325{
Chris Jones3f613c12021-03-31 09:34:22 +0100326 /* Error codes are always negative (a value of zero is a success) however
327 * their positive opposites can be easier to understand. The following
328 * examples given in comments have been made positive for ease of
329 * understanding. The structure of an error code is such:
330 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100331 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100332 *
333 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100334 * h = high level error code (includes high level module ID (bits 12..14)
335 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100336 * l = low level error code.
337 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (high > -0x1000 && high != 0) {
339 /* high < 0001000000000000
340 * No high level module ID bits are set.
341 */
342 mbedtls_test_fail("'high' is not a high-level error code",
343 line, file);
344 } else if (high < -0x7F80) {
345 /* high > 0111111110000000
346 * Error code is greater than the largest allowed high level module ID.
347 */
348 mbedtls_test_fail("'high' error code is greater than 15 bits",
349 line, file);
350 } else if ((high & 0x7F) != 0) {
351 /* high & 0000000001111111
352 * Error code contains low level error code bits.
353 */
354 mbedtls_test_fail("'high' contains a low-level error code",
355 line, file);
356 } else if (low < -0x007F) {
357 /* low > 0000000001111111
358 * Error code contains high or module level error code bits.
359 */
360 mbedtls_test_fail("'low' error code is greater than 7 bits",
361 line, file);
362 } else if (low > 0) {
363 mbedtls_test_fail("'low' error code is greater than zero",
364 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000365 }
366}
367#endif /* MBEDTLS_TEST_HOOKS */