blob: da0b54a00abc13ecdef2b20f68fa8c35e6bc59f5 [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
Paul Elliott65064262023-11-27 17:29:05 +000016#if defined(MBEDTLS_THREADING_C)
17#include "mbedtls/threading.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
Paul Elliotte2f66622024-01-19 20:22:24 +000027static mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000028
Paul Elliott65064262023-11-27 17:29:05 +000029#ifdef MBEDTLS_THREADING_C
30mbedtls_threading_mutex_t mbedtls_test_info_mutex;
31#endif /* MBEDTLS_THREADING_C */
32
Ronald Crona1236142020-07-01 16:01:21 +020033/*----------------------------------------------------------------------------*/
Paul Elliott4580d4d2023-10-27 18:41:02 +010034/* Mbedtls Test Info accessors */
35
36mbedtls_test_result_t mbedtls_test_get_result(void)
37{
Paul Elliott65064262023-11-27 17:29:05 +000038 mbedtls_test_result_t result;
39
40#ifdef MBEDTLS_THREADING_C
41 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
42#endif /* MBEDTLS_THREADING_C */
43
44 result = mbedtls_test_info.result;
45
46#ifdef MBEDTLS_THREADING_C
47 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
48#endif /* MBEDTLS_THREADING_C */
49
50 return result;
Paul Elliott4580d4d2023-10-27 18:41:02 +010051}
52
Paul Elliott5c498f32023-10-31 16:38:56 +000053void mbedtls_test_set_result(mbedtls_test_result_t result, const char *test,
54 int line_no, const char *filename)
55{
Paul Elliottfad978b2024-01-30 18:00:26 +000056 /* Internal function only - mbedtls_test_info_mutex should be held prior
57 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +000058
Paul Elliott5c498f32023-10-31 16:38:56 +000059 mbedtls_test_info.result = result;
60 mbedtls_test_info.test = test;
61 mbedtls_test_info.line_no = line_no;
62 mbedtls_test_info.filename = filename;
63}
64
Paul Elliott4580d4d2023-10-27 18:41:02 +010065const char *mbedtls_test_get_test(void)
66{
Paul Elliott65064262023-11-27 17:29:05 +000067 const char *test;
68
69#ifdef MBEDTLS_THREADING_C
70 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
71#endif /* MBEDTLS_THREADING_C */
72
73 test = mbedtls_test_info.test;
74
75#ifdef MBEDTLS_THREADING_C
76 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
77#endif /* MBEDTLS_THREADING_C */
78
79 return test;
Paul Elliott4580d4d2023-10-27 18:41:02 +010080}
81const char *mbedtls_get_test_filename(void)
82{
Paul Elliott65064262023-11-27 17:29:05 +000083 const char *filename;
84
85#ifdef MBEDTLS_THREADING_C
86 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
87#endif /* MBEDTLS_THREADING_C */
88
89 /* It should be ok just to pass back the pointer here, as it is going to
90 * be a pointer into non changing data. */
91 filename = mbedtls_test_info.filename;
92
93#ifdef MBEDTLS_THREADING_C
94 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
95#endif /* MBEDTLS_THREADING_C */
96
97 return filename;
Paul Elliott4580d4d2023-10-27 18:41:02 +010098}
99
100int mbedtls_test_get_line_no(void)
101{
Paul Elliott65064262023-11-27 17:29:05 +0000102 int line_no;
103
104#ifdef MBEDTLS_THREADING_C
105 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
106#endif /* MBEDTLS_THREADING_C */
107
108 line_no = mbedtls_test_info.line_no;
109
110#ifdef MBEDTLS_THREADING_C
111 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
112#endif /* MBEDTLS_THREADING_C */
113
114 return line_no;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100115}
116
117void mbedtls_test_increment_step(void)
118{
Paul Elliott65064262023-11-27 17:29:05 +0000119#ifdef MBEDTLS_THREADING_C
120 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
121#endif /* MBEDTLS_THREADING_C */
122
Paul Elliott4580d4d2023-10-27 18:41:02 +0100123 ++mbedtls_test_info.step;
Paul Elliott65064262023-11-27 17:29:05 +0000124
125#ifdef MBEDTLS_THREADING_C
126 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
127#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100128}
129
130unsigned long mbedtls_test_get_step(void)
131{
Paul Elliott65064262023-11-27 17:29:05 +0000132 unsigned long step;
133
134#ifdef MBEDTLS_THREADING_C
135 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
136#endif /* MBEDTLS_THREADING_C */
137
138 step = mbedtls_test_info.step;
139
140#ifdef MBEDTLS_THREADING_C
141 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
142#endif /* MBEDTLS_THREADING_C */
143
144 return step;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100145}
146
Paul Elliottac61cee2024-02-02 17:53:38 +0000147void mbedtls_test_reset_step(void)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100148{
Paul Elliottfad978b2024-01-30 18:00:26 +0000149 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000150 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000151
Paul Elliottac61cee2024-02-02 17:53:38 +0000152 mbedtls_test_info.step = (unsigned long) (-1);
153}
154
155void mbedtls_test_set_step(unsigned long step)
156{
157#ifdef MBEDTLS_THREADING_C
158 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
159#endif /* MBEDTLS_THREADING_C */
160
Paul Elliott65064262023-11-27 17:29:05 +0000161 mbedtls_test_info.step = step;
Paul Elliottac61cee2024-02-02 17:53:38 +0000162
163#ifdef MBEDTLS_THREADING_C
164 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
165#endif /* MBEDTLS_THREADING_C */
Paul Elliott65064262023-11-27 17:29:05 +0000166}
167
168void mbedtls_test_get_line1(char *line)
169{
170#ifdef MBEDTLS_THREADING_C
171 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
172#endif /* MBEDTLS_THREADING_C */
173
174 memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH);
175
176#ifdef MBEDTLS_THREADING_C
177 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
178#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100179}
Paul Elliott5c498f32023-10-31 16:38:56 +0000180
181void mbedtls_test_set_line1(const char *line)
182{
Paul Elliottfad978b2024-01-30 18:00:26 +0000183 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000184 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000185
Paul Elliott5c498f32023-10-31 16:38:56 +0000186 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000187 memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000188 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000189 memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000190 }
191}
192
Paul Elliott65064262023-11-27 17:29:05 +0000193void mbedtls_test_get_line2(char *line)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100194{
Paul Elliott65064262023-11-27 17:29:05 +0000195#ifdef MBEDTLS_THREADING_C
196 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
197#endif /* MBEDTLS_THREADING_C */
198
199 memcpy(line, mbedtls_test_info.line2, MBEDTLS_TEST_LINE_LENGTH);
200
201#ifdef MBEDTLS_THREADING_C
202 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
203#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100204}
205
Paul Elliott65064262023-11-27 17:29:05 +0000206void mbedtls_test_set_line2(const char *line)
207{
Paul Elliottfad978b2024-01-30 18:00:26 +0000208 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000209 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000210
Paul Elliott5c498f32023-10-31 16:38:56 +0000211 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000212 memset(mbedtls_test_info.line2, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000213 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000214 memcpy(mbedtls_test_info.line2, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000215 }
216}
217
218
Paul Elliott4580d4d2023-10-27 18:41:02 +0100219#if defined(MBEDTLS_TEST_MUTEX_USAGE)
220const char *mbedtls_test_get_mutex_usage_error(void)
221{
222 return mbedtls_test_info.mutex_usage_error;
223}
224
225void mbedtls_test_set_mutex_usage_error(const char *msg)
226{
Paul Elliott65064262023-11-27 17:29:05 +0000227#ifdef MBEDTLS_THREADING_C
228 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
229#endif /* MBEDTLS_THREADING_C */
230
Paul Elliott4580d4d2023-10-27 18:41:02 +0100231 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
232 mbedtls_test_info.mutex_usage_error = msg;
233 }
Paul Elliott65064262023-11-27 17:29:05 +0000234
235#ifdef MBEDTLS_THREADING_C
236 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
237#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100238}
239#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
240
Paul Elliottc7a1e992023-11-03 18:44:57 +0000241#if defined(MBEDTLS_BIGNUM_C)
242
243unsigned mbedtls_test_get_case_uses_negative_0(void)
244{
Paul Elliott65064262023-11-27 17:29:05 +0000245 unsigned test_case_uses_negative_0 = 0;
246#ifdef MBEDTLS_THREADING_C
247 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
248#endif /* MBEDTLS_THREADING_C */
249 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
250
251#ifdef MBEDTLS_THREADING_C
252 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
253#endif /* MBEDTLS_THREADING_C */
254
255 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000256}
257
258void mbedtls_test_set_case_uses_negative_0(unsigned uses)
259{
Paul Elliottfad978b2024-01-30 18:00:26 +0000260 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000261 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000262
Paul Elliottc7a1e992023-11-03 18:44:57 +0000263 mbedtls_test_info.case_uses_negative_0 = uses;
264}
265
266void mbedtls_test_increment_case_uses_negative_0(void)
267{
Paul Elliott65064262023-11-27 17:29:05 +0000268#ifdef MBEDTLS_THREADING_C
269 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
270#endif /* MBEDTLS_THREADING_C */
271
Paul Elliottc7a1e992023-11-03 18:44:57 +0000272 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000273
274#ifdef MBEDTLS_THREADING_C
275 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
276#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000277}
278
Paul Elliott3d2db892024-01-19 20:42:56 +0000279#endif /* MBEDTLS_BIGNUM_C */
280
281#ifdef MBEDTLS_TEST_MUTEX_USAGE
282mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
283{
284 return &mbedtls_test_info_mutex;
285}
286
287#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000288
Paul Elliott4580d4d2023-10-27 18:41:02 +0100289/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200290/* Helper Functions */
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200293{
294 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200295
296#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
297 /* Make sure that injected entropy is present. Otherwise
298 * psa_crypto_init() will fail. This is not necessary for test suites
299 * that don't use PSA, but it's harmless (except for leaving a file
300 * behind). */
301 ret = mbedtls_test_inject_entropy_restore();
302 if (ret != 0) {
303 return ret;
304 }
305#endif
306
Ronald Cronf40529d2020-06-09 16:27:37 +0200307#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200309#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200310
Paul Elliott65064262023-11-27 17:29:05 +0000311#ifdef MBEDTLS_THREADING_C
312 mbedtls_mutex_init(&mbedtls_test_info_mutex);
313#endif /* MBEDTLS_THREADING_C */
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200316}
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200319{
Paul Elliott65064262023-11-27 17:29:05 +0000320#ifdef MBEDTLS_THREADING_C
321 mbedtls_mutex_free(&mbedtls_test_info_mutex);
322#endif /* MBEDTLS_THREADING_C */
323
Ronald Cronf40529d2020-06-09 16:27:37 +0200324#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200326#endif /* MBEDTLS_PLATFORM_C */
327}
328
Gilles Peskine881447d2022-12-08 15:24:52 +0100329int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200330{
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200332 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200334 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200336 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 } else {
338 return -1;
339 }
Ronald Crona0c25392020-06-18 10:10:46 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200342}
343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000345{
Paul Elliottfad978b2024-01-30 18:00:26 +0000346#ifdef MBEDTLS_THREADING_C
347 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
348#endif /* MBEDTLS_THREADING_C */
349
350 /* Don't use accessor, we already hold mutex. */
351 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
352 /* If we have already recorded the test as having failed then don't
Chris Jones9634bb12021-01-20 15:56:42 +0000353 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000354 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000355 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000356
357#ifdef MBEDTLS_THREADING_C
358 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
359#endif /* MBEDTLS_THREADING_C */
Chris Jones9634bb12021-01-20 15:56:42 +0000360}
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000363{
Paul Elliottfad978b2024-01-30 18:00:26 +0000364#ifdef MBEDTLS_THREADING_C
365 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
366#endif /* MBEDTLS_THREADING_C */
367
Paul Elliott5c498f32023-10-31 16:38:56 +0000368 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000369
370#ifdef MBEDTLS_THREADING_C
371 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
372#endif /* MBEDTLS_THREADING_C */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000373}
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000376{
Paul Elliottfad978b2024-01-30 18:00:26 +0000377#ifdef MBEDTLS_THREADING_C
378 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
379#endif /* MBEDTLS_THREADING_C */
380
Paul Elliott5c498f32023-10-31 16:38:56 +0000381 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
Paul Elliottac61cee2024-02-02 17:53:38 +0000382 mbedtls_test_reset_step();
Paul Elliott5c498f32023-10-31 16:38:56 +0000383 mbedtls_test_set_line1(NULL);
384 mbedtls_test_set_line2(NULL);
385
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100386#if defined(MBEDTLS_BIGNUM_C)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000387 mbedtls_test_set_case_uses_negative_0(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100388#endif
Paul Elliottfad978b2024-01-30 18:00:26 +0000389
390#ifdef MBEDTLS_THREADING_C
Paul Elliott0b2835d2024-02-01 13:27:04 +0000391 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
Paul Elliottfad978b2024-01-30 18:00:26 +0000392#endif /* MBEDTLS_THREADING_C */
Gilles Peskine89615ee2021-04-29 20:28:54 +0200393}
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395int mbedtls_test_equal(const char *test, int line_no, const char *filename,
396 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200397{
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 TEST_CF_PUBLIC(&value1, sizeof(value1));
399 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (value1 == value2) {
402 return 1;
403 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200404
Paul Elliottfad978b2024-01-30 18:00:26 +0000405#ifdef MBEDTLS_THREADING_C
406 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
407#endif /* MBEDTLS_THREADING_C */
408
409 /* Don't use accessor, as we already hold mutex. */
410 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
411 /* If we've already recorded the test as having failed then don't
Gilles Peskine89615ee2021-04-29 20:28:54 +0200412 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000413
414 char buf[MBEDTLS_TEST_LINE_LENGTH];
415 mbedtls_test_fail(test, line_no, filename);
416 (void) mbedtls_snprintf(buf, sizeof(buf),
417 "lhs = 0x%016llx = %lld",
418 value1, (long long) value1);
419 mbedtls_test_set_line1(buf);
420 (void) mbedtls_snprintf(buf, sizeof(buf),
421 "rhs = 0x%016llx = %lld",
422 value2, (long long) value2);
423 mbedtls_test_set_line2(buf);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200424 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000425
426#ifdef MBEDTLS_THREADING_C
427 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
428#endif /* MBEDTLS_THREADING_C */
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000431}
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
434 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200435{
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 TEST_CF_PUBLIC(&value1, sizeof(value1));
437 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (value1 <= value2) {
440 return 1;
441 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200442
Paul Elliottfad978b2024-01-30 18:00:26 +0000443#ifdef MBEDTLS_THREADING_C
444 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
445#endif /* MBEDTLS_THREADING_C */
446
447 /* Don't use accessor, we already hold mutex. */
448 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
449 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200450 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000451
452 char buf[MBEDTLS_TEST_LINE_LENGTH];
453 mbedtls_test_fail(test, line_no, filename);
454 (void) mbedtls_snprintf(buf, sizeof(buf),
455 "lhs = 0x%016llx = %llu",
456 value1, value1);
457 mbedtls_test_set_line1(buf);
458 (void) mbedtls_snprintf(buf, sizeof(buf),
459 "rhs = 0x%016llx = %llu",
460 value2, value2);
461 mbedtls_test_set_line2(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200462 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000463
464#ifdef MBEDTLS_THREADING_C
465 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
466#endif /* MBEDTLS_THREADING_C */
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200469}
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
472 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200473{
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 TEST_CF_PUBLIC(&value1, sizeof(value1));
475 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (value1 <= value2) {
478 return 1;
479 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200480
Paul Elliottfad978b2024-01-30 18:00:26 +0000481#ifdef MBEDTLS_THREADING_C
482 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
483#endif /* MBEDTLS_THREADING_C */
484
485 /* Don't use accessor, we already hold mutex. */
Paul Elliottf20728e2024-02-06 12:49:45 +0000486 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
Paul Elliottfad978b2024-01-30 18:00:26 +0000487 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200488 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000489
490 char buf[MBEDTLS_TEST_LINE_LENGTH];
491 mbedtls_test_fail(test, line_no, filename);
492 (void) mbedtls_snprintf(buf, sizeof(buf),
493 "lhs = 0x%016llx = %lld",
494 (unsigned long long) value1, value1);
495 mbedtls_test_set_line1(buf);
496 (void) mbedtls_snprintf(buf, sizeof(buf),
497 "rhs = 0x%016llx = %lld",
498 (unsigned long long) value2, value2);
499 mbedtls_test_set_line2(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200500 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000501
502#ifdef MBEDTLS_THREADING_C
503 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
504#endif /* MBEDTLS_THREADING_C */
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200507}
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509int mbedtls_test_unhexify(unsigned char *obuf,
510 size_t obufmax,
511 const char *ibuf,
512 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200513{
514 unsigned char uc, uc2;
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200517
518 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if ((*len) & 1) {
520 return -1;
521 }
Ronald Crona0c25392020-06-18 10:10:46 +0200522 *len /= 2;
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if ((*len) > obufmax) {
525 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 while (*ibuf != 0) {
529 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
530 return -1;
531 }
532
533 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
534 return -1;
535 }
536
537 *(obuf++) = (uc << 4) | uc2;
538 }
539
540 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200541}
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543void mbedtls_test_hexify(unsigned char *obuf,
544 const unsigned char *ibuf,
545 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200546{
547 unsigned char l, h;
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200550 h = *ibuf / 16;
551 l = *ibuf % 16;
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200554 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200556 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200560 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200562 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200564
565 ++ibuf;
566 len--;
567 }
568}
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200571{
572 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 p = mbedtls_calloc(1, actual_len);
576 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200581}
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200584{
585 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200586 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (*olen == 0) {
591 return mbedtls_test_zero_alloc(*olen);
592 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 obuf = mbedtls_calloc(1, *olen);
595 TEST_HELPER_ASSERT(obuf != NULL);
596 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200599}
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
602 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200603{
604 int ret = 0;
605 uint32_t i = 0;
606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (a_len != b_len) {
608 return -1;
609 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 for (i = 0; i < a_len; i++) {
612 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200613 ret = -1;
614 break;
615 }
616 }
617 return ret;
618}
Ronald Crona1236142020-07-01 16:01:21 +0200619
Chris Jones96ae73b2021-01-08 17:04:59 +0000620#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100621void mbedtls_test_err_add_check(int high, int low,
622 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000623{
Chris Jones3f613c12021-03-31 09:34:22 +0100624 /* Error codes are always negative (a value of zero is a success) however
625 * their positive opposites can be easier to understand. The following
626 * examples given in comments have been made positive for ease of
627 * understanding. The structure of an error code is such:
628 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100629 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100630 *
631 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100632 * h = high level error code (includes high level module ID (bits 12..14)
633 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100634 * l = low level error code.
635 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (high > -0x1000 && high != 0) {
637 /* high < 0001000000000000
638 * No high level module ID bits are set.
639 */
640 mbedtls_test_fail("'high' is not a high-level error code",
641 line, file);
642 } else if (high < -0x7F80) {
643 /* high > 0111111110000000
644 * Error code is greater than the largest allowed high level module ID.
645 */
646 mbedtls_test_fail("'high' error code is greater than 15 bits",
647 line, file);
648 } else if ((high & 0x7F) != 0) {
649 /* high & 0000000001111111
650 * Error code contains low level error code bits.
651 */
652 mbedtls_test_fail("'high' contains a low-level error code",
653 line, file);
654 } else if (low < -0x007F) {
655 /* low > 0000000001111111
656 * Error code contains high or module level error code bits.
657 */
658 mbedtls_test_fail("'low' error code is greater than 7 bits",
659 line, file);
660 } else if (low > 0) {
661 mbedtls_test_fail("'low' error code is greater than zero",
662 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000663 }
664}
665#endif /* MBEDTLS_TEST_HOOKS */