blob: eb28919b8df19d50397f31608998045363d69d23 [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
Ronald Crona1236142020-07-01 16:01:21 +020016/*----------------------------------------------------------------------------*/
17/* Static global variables */
18
Ronald Cronf40529d2020-06-09 16:27:37 +020019#if defined(MBEDTLS_PLATFORM_C)
20static mbedtls_platform_context platform_ctx;
21#endif
22
Chris Jonese60e2ae2021-01-20 17:51:47 +000023mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000024
Ronald Crona1236142020-07-01 16:01:21 +020025/*----------------------------------------------------------------------------*/
26/* Helper Functions */
27
Gilles Peskine449bd832023-01-11 14:50:10 +010028int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020029{
30 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +020031
32#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
33 /* Make sure that injected entropy is present. Otherwise
34 * psa_crypto_init() will fail. This is not necessary for test suites
35 * that don't use PSA, but it's harmless (except for leaving a file
36 * behind). */
37 ret = mbedtls_test_inject_entropy_restore();
38 if (ret != 0) {
39 return ret;
40 }
41#endif
42
Ronald Cronf40529d2020-06-09 16:27:37 +020043#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010044 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020045#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +020046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020048}
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020051{
52#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010053 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020054#endif /* MBEDTLS_PLATFORM_C */
55}
56
Gilles Peskine881447d2022-12-08 15:24:52 +010057int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020058{
Gilles Peskine449bd832023-01-11 14:50:10 +010059 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020060 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +010061 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +020062 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010063 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020064 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010065 } else {
66 return -1;
67 }
Ronald Crona0c25392020-06-18 10:10:46 +020068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +020070}
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000073{
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +000075 /* We've already recorded the test as having failed. Don't
76 * overwrite any previous information about the failure. */
77 return;
78 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000079 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
80 mbedtls_test_info.test = test;
81 mbedtls_test_info.line_no = line_no;
82 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000083}
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000086{
Chris Jonese60e2ae2021-01-20 17:51:47 +000087 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
88 mbedtls_test_info.test = test;
89 mbedtls_test_info.line_no = line_no;
90 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000091}
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +000094{
95 mbedtls_test_info.step = step;
96}
97
Gilles Peskineca6e8aa2022-11-09 21:08:44 +010098#if defined(MBEDTLS_BIGNUM_C)
99unsigned mbedtls_test_case_uses_negative_0 = 0;
100#endif
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000103{
104 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000106 mbedtls_test_info.test = 0;
107 mbedtls_test_info.line_no = 0;
108 mbedtls_test_info.filename = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
110 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100111#if defined(MBEDTLS_BIGNUM_C)
112 mbedtls_test_case_uses_negative_0 = 0;
113#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200114}
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116int mbedtls_test_equal(const char *test, int line_no, const char *filename,
117 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200118{
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_CF_PUBLIC(&value1, sizeof(value1));
120 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (value1 == value2) {
123 return 1;
124 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200127 /* We've already recorded the test as having failed. Don't
128 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200130 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_test_fail(test, line_no, filename);
132 (void) mbedtls_snprintf(mbedtls_test_info.line1,
133 sizeof(mbedtls_test_info.line1),
134 "lhs = 0x%016llx = %lld",
135 value1, (long long) value1);
136 (void) mbedtls_snprintf(mbedtls_test_info.line2,
137 sizeof(mbedtls_test_info.line2),
138 "rhs = 0x%016llx = %lld",
139 value2, (long long) value2);
140 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000141}
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
144 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200145{
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 TEST_CF_PUBLIC(&value1, sizeof(value1));
147 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (value1 <= value2) {
150 return 1;
151 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200154 /* We've already recorded the test as having failed. Don't
155 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200157 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_test_fail(test, line_no, filename);
159 (void) mbedtls_snprintf(mbedtls_test_info.line1,
160 sizeof(mbedtls_test_info.line1),
161 "lhs = 0x%016llx = %llu",
162 value1, value1);
163 (void) mbedtls_snprintf(mbedtls_test_info.line2,
164 sizeof(mbedtls_test_info.line2),
165 "rhs = 0x%016llx = %llu",
166 value2, value2);
167 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200168}
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
171 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200172{
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 TEST_CF_PUBLIC(&value1, sizeof(value1));
174 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (value1 <= value2) {
177 return 1;
178 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200181 /* We've already recorded the test as having failed. Don't
182 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200184 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_test_fail(test, line_no, filename);
186 (void) mbedtls_snprintf(mbedtls_test_info.line1,
187 sizeof(mbedtls_test_info.line1),
188 "lhs = 0x%016llx = %lld",
189 (unsigned long long) value1, value1);
190 (void) mbedtls_snprintf(mbedtls_test_info.line2,
191 sizeof(mbedtls_test_info.line2),
192 "rhs = 0x%016llx = %lld",
193 (unsigned long long) value2, value2);
194 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200195}
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int mbedtls_test_unhexify(unsigned char *obuf,
198 size_t obufmax,
199 const char *ibuf,
200 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200201{
202 unsigned char uc, uc2;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200205
206 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((*len) & 1) {
208 return -1;
209 }
Ronald Crona0c25392020-06-18 10:10:46 +0200210 *len /= 2;
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 if ((*len) > obufmax) {
213 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200214 }
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 while (*ibuf != 0) {
217 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
218 return -1;
219 }
220
221 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
222 return -1;
223 }
224
225 *(obuf++) = (uc << 4) | uc2;
226 }
227
228 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200229}
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231void mbedtls_test_hexify(unsigned char *obuf,
232 const unsigned char *ibuf,
233 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200234{
235 unsigned char l, h;
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200238 h = *ibuf / 16;
239 l = *ibuf % 16;
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200242 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200244 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200248 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200250 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200252
253 ++ibuf;
254 len--;
255 }
256}
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200259{
260 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 p = mbedtls_calloc(1, actual_len);
264 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200269}
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200272{
273 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200274 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (*olen == 0) {
279 return mbedtls_test_zero_alloc(*olen);
280 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 obuf = mbedtls_calloc(1, *olen);
283 TEST_HELPER_ASSERT(obuf != NULL);
284 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200287}
288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
290 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200291{
292 int ret = 0;
293 uint32_t i = 0;
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (a_len != b_len) {
296 return -1;
297 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 for (i = 0; i < a_len; i++) {
300 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200301 ret = -1;
302 break;
303 }
304 }
305 return ret;
306}
Ronald Crona1236142020-07-01 16:01:21 +0200307
Chris Jones96ae73b2021-01-08 17:04:59 +0000308#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100309void mbedtls_test_err_add_check(int high, int low,
310 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000311{
Chris Jones3f613c12021-03-31 09:34:22 +0100312 /* Error codes are always negative (a value of zero is a success) however
313 * their positive opposites can be easier to understand. The following
314 * examples given in comments have been made positive for ease of
315 * understanding. The structure of an error code is such:
316 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100317 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100318 *
319 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100320 * h = high level error code (includes high level module ID (bits 12..14)
321 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100322 * l = low level error code.
323 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (high > -0x1000 && high != 0) {
325 /* high < 0001000000000000
326 * No high level module ID bits are set.
327 */
328 mbedtls_test_fail("'high' is not a high-level error code",
329 line, file);
330 } else if (high < -0x7F80) {
331 /* high > 0111111110000000
332 * Error code is greater than the largest allowed high level module ID.
333 */
334 mbedtls_test_fail("'high' error code is greater than 15 bits",
335 line, file);
336 } else if ((high & 0x7F) != 0) {
337 /* high & 0000000001111111
338 * Error code contains low level error code bits.
339 */
340 mbedtls_test_fail("'high' contains a low-level error code",
341 line, file);
342 } else if (low < -0x007F) {
343 /* low > 0000000001111111
344 * Error code contains high or module level error code bits.
345 */
346 mbedtls_test_fail("'low' error code is greater than 7 bits",
347 line, file);
348 } else if (low > 0) {
349 mbedtls_test_fail("'low' error code is greater than zero",
350 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000351 }
352}
353#endif /* MBEDTLS_TEST_HOOKS */