blob: 6bfe15dd7001c82a6169f7b842717872e3747f7c [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/*----------------------------------------------------------------------------*/
Paul Elliott4580d4d2023-10-27 18:41:02 +010026/* Mbedtls Test Info accessors */
27
28mbedtls_test_result_t mbedtls_test_get_result(void)
29{
30 return mbedtls_test_info.result;
31}
32
33const char *mbedtls_test_get_test(void)
34{
35 return mbedtls_test_info.test;
36}
37const char *mbedtls_get_test_filename(void)
38{
39 return mbedtls_test_info.filename;
40}
41
42int mbedtls_test_get_line_no(void)
43{
44 return mbedtls_test_info.line_no;
45}
46
47void mbedtls_test_increment_step(void)
48{
49 ++mbedtls_test_info.step;
50}
51
52unsigned long mbedtls_test_get_step(void)
53{
54 return mbedtls_test_info.step;
55}
56
57const char *mbedtls_test_get_line1(void)
58{
59 return mbedtls_test_info.line1;
60}
61const char *mbedtls_test_get_line2(void)
62{
63 return mbedtls_test_info.line2;
64}
65
66#if defined(MBEDTLS_TEST_MUTEX_USAGE)
67const char *mbedtls_test_get_mutex_usage_error(void)
68{
69 return mbedtls_test_info.mutex_usage_error;
70}
71
72void mbedtls_test_set_mutex_usage_error(const char *msg)
73{
74 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
75 mbedtls_test_info.mutex_usage_error = msg;
76 }
77}
78#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
79
80/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +020081/* Helper Functions */
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020084{
85 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +020086
87#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
88 /* Make sure that injected entropy is present. Otherwise
89 * psa_crypto_init() will fail. This is not necessary for test suites
90 * that don't use PSA, but it's harmless (except for leaving a file
91 * behind). */
92 ret = mbedtls_test_inject_entropy_restore();
93 if (ret != 0) {
94 return ret;
95 }
96#endif
97
Ronald Cronf40529d2020-06-09 16:27:37 +020098#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010099 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200100#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200103}
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200106{
107#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200109#endif /* MBEDTLS_PLATFORM_C */
110}
111
Gilles Peskine881447d2022-12-08 15:24:52 +0100112int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200113{
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200115 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200117 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200119 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 } else {
121 return -1;
122 }
Ronald Crona0c25392020-06-18 10:10:46 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200125}
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000128{
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +0000130 /* We've already recorded the test as having failed. Don't
131 * overwrite any previous information about the failure. */
132 return;
133 }
Chris Jonese60e2ae2021-01-20 17:51:47 +0000134 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
135 mbedtls_test_info.test = test;
136 mbedtls_test_info.line_no = line_no;
137 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000138}
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000141{
Chris Jonese60e2ae2021-01-20 17:51:47 +0000142 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
143 mbedtls_test_info.test = test;
144 mbedtls_test_info.line_no = line_no;
145 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000146}
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000149{
150 mbedtls_test_info.step = step;
151}
152
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100153#if defined(MBEDTLS_BIGNUM_C)
154unsigned mbedtls_test_case_uses_negative_0 = 0;
155#endif
156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000158{
159 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000161 mbedtls_test_info.test = 0;
162 mbedtls_test_info.line_no = 0;
163 mbedtls_test_info.filename = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
165 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100166#if defined(MBEDTLS_BIGNUM_C)
167 mbedtls_test_case_uses_negative_0 = 0;
168#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200169}
170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int mbedtls_test_equal(const char *test, int line_no, const char *filename,
172 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200173{
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 TEST_CF_PUBLIC(&value1, sizeof(value1));
175 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (value1 == value2) {
178 return 1;
179 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200182 /* We've already recorded the test as having failed. Don't
183 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200185 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_test_fail(test, line_no, filename);
187 (void) mbedtls_snprintf(mbedtls_test_info.line1,
188 sizeof(mbedtls_test_info.line1),
189 "lhs = 0x%016llx = %lld",
190 value1, (long long) value1);
191 (void) mbedtls_snprintf(mbedtls_test_info.line2,
192 sizeof(mbedtls_test_info.line2),
193 "rhs = 0x%016llx = %lld",
194 value2, (long long) value2);
195 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000196}
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
199 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200200{
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 TEST_CF_PUBLIC(&value1, sizeof(value1));
202 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (value1 <= value2) {
205 return 1;
206 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200209 /* We've already recorded the test as having failed. Don't
210 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200212 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 mbedtls_test_fail(test, line_no, filename);
214 (void) mbedtls_snprintf(mbedtls_test_info.line1,
215 sizeof(mbedtls_test_info.line1),
216 "lhs = 0x%016llx = %llu",
217 value1, value1);
218 (void) mbedtls_snprintf(mbedtls_test_info.line2,
219 sizeof(mbedtls_test_info.line2),
220 "rhs = 0x%016llx = %llu",
221 value2, value2);
222 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200223}
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
226 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200227{
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 TEST_CF_PUBLIC(&value1, sizeof(value1));
229 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (value1 <= value2) {
232 return 1;
233 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200236 /* We've already recorded the test as having failed. Don't
237 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200239 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_test_fail(test, line_no, filename);
241 (void) mbedtls_snprintf(mbedtls_test_info.line1,
242 sizeof(mbedtls_test_info.line1),
243 "lhs = 0x%016llx = %lld",
244 (unsigned long long) value1, value1);
245 (void) mbedtls_snprintf(mbedtls_test_info.line2,
246 sizeof(mbedtls_test_info.line2),
247 "rhs = 0x%016llx = %lld",
248 (unsigned long long) value2, value2);
249 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200250}
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252int mbedtls_test_unhexify(unsigned char *obuf,
253 size_t obufmax,
254 const char *ibuf,
255 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200256{
257 unsigned char uc, uc2;
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200260
261 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if ((*len) & 1) {
263 return -1;
264 }
Ronald Crona0c25392020-06-18 10:10:46 +0200265 *len /= 2;
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if ((*len) > obufmax) {
268 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200269 }
270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 while (*ibuf != 0) {
272 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
273 return -1;
274 }
275
276 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
277 return -1;
278 }
279
280 *(obuf++) = (uc << 4) | uc2;
281 }
282
283 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200284}
285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286void mbedtls_test_hexify(unsigned char *obuf,
287 const unsigned char *ibuf,
288 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200289{
290 unsigned char l, h;
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200293 h = *ibuf / 16;
294 l = *ibuf % 16;
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200297 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200299 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200303 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200305 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200307
308 ++ibuf;
309 len--;
310 }
311}
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200314{
315 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 p = mbedtls_calloc(1, actual_len);
319 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200324}
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200327{
328 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200329 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (*olen == 0) {
334 return mbedtls_test_zero_alloc(*olen);
335 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 obuf = mbedtls_calloc(1, *olen);
338 TEST_HELPER_ASSERT(obuf != NULL);
339 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200342}
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
345 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200346{
347 int ret = 0;
348 uint32_t i = 0;
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (a_len != b_len) {
351 return -1;
352 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 for (i = 0; i < a_len; i++) {
355 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200356 ret = -1;
357 break;
358 }
359 }
360 return ret;
361}
Ronald Crona1236142020-07-01 16:01:21 +0200362
Chris Jones96ae73b2021-01-08 17:04:59 +0000363#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100364void mbedtls_test_err_add_check(int high, int low,
365 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000366{
Chris Jones3f613c12021-03-31 09:34:22 +0100367 /* Error codes are always negative (a value of zero is a success) however
368 * their positive opposites can be easier to understand. The following
369 * examples given in comments have been made positive for ease of
370 * understanding. The structure of an error code is such:
371 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100372 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100373 *
374 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100375 * h = high level error code (includes high level module ID (bits 12..14)
376 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100377 * l = low level error code.
378 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (high > -0x1000 && high != 0) {
380 /* high < 0001000000000000
381 * No high level module ID bits are set.
382 */
383 mbedtls_test_fail("'high' is not a high-level error code",
384 line, file);
385 } else if (high < -0x7F80) {
386 /* high > 0111111110000000
387 * Error code is greater than the largest allowed high level module ID.
388 */
389 mbedtls_test_fail("'high' error code is greater than 15 bits",
390 line, file);
391 } else if ((high & 0x7F) != 0) {
392 /* high & 0000000001111111
393 * Error code contains low level error code bits.
394 */
395 mbedtls_test_fail("'high' contains a low-level error code",
396 line, file);
397 } else if (low < -0x007F) {
398 /* low > 0000000001111111
399 * Error code contains high or module level error code bits.
400 */
401 mbedtls_test_fail("'low' error code is greater than 7 bits",
402 line, file);
403 } else if (low > 0) {
404 mbedtls_test_fail("'low' error code is greater than zero",
405 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000406 }
407}
408#endif /* MBEDTLS_TEST_HOOKS */