blob: 24334228ff6226fe76f823e163737fbbcea6d731 [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 Elliott264e2102024-02-15 12:28:56 +000034/* Mbedtls Test Info accessors
35 *
Paul Elliott9011dae2024-02-24 10:57:22 +000036 * NOTE - there are two types of accessors here: public accessors and internal
37 * accessors. The public accessors have prototypes in helpers.h and lock
38 * mbedtls_test_info_mutex (if mutexes are enabled). The _internal accessors,
39 * which are expected to be used from this module *only*, do not lock the mutex.
40 * These are designed to be called from within public functions which already
41 * hold the mutex. The main reason for this difference is the need to set
42 * multiple test data values atomically (without releasing the mutex) to prevent
43 * race conditions. */
Paul Elliott4580d4d2023-10-27 18:41:02 +010044
45mbedtls_test_result_t mbedtls_test_get_result(void)
46{
Paul Elliott65064262023-11-27 17:29:05 +000047 mbedtls_test_result_t result;
48
49#ifdef MBEDTLS_THREADING_C
50 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
51#endif /* MBEDTLS_THREADING_C */
52
53 result = mbedtls_test_info.result;
54
55#ifdef MBEDTLS_THREADING_C
56 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
57#endif /* MBEDTLS_THREADING_C */
58
59 return result;
Paul Elliott4580d4d2023-10-27 18:41:02 +010060}
61
Paul Elliott264e2102024-02-15 12:28:56 +000062static void mbedtls_test_set_result_internal(mbedtls_test_result_t result, const char *test,
63 int line_no, const char *filename)
Paul Elliott5c498f32023-10-31 16:38:56 +000064{
Paul Elliottfad978b2024-01-30 18:00:26 +000065 /* Internal function only - mbedtls_test_info_mutex should be held prior
66 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +000067
Paul Elliott5c498f32023-10-31 16:38:56 +000068 mbedtls_test_info.result = result;
69 mbedtls_test_info.test = test;
70 mbedtls_test_info.line_no = line_no;
71 mbedtls_test_info.filename = filename;
72}
73
Paul Elliott4580d4d2023-10-27 18:41:02 +010074const char *mbedtls_test_get_test(void)
75{
Paul Elliott65064262023-11-27 17:29:05 +000076 const char *test;
77
78#ifdef MBEDTLS_THREADING_C
79 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
80#endif /* MBEDTLS_THREADING_C */
81
82 test = mbedtls_test_info.test;
83
84#ifdef MBEDTLS_THREADING_C
85 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
86#endif /* MBEDTLS_THREADING_C */
87
88 return test;
Paul Elliott4580d4d2023-10-27 18:41:02 +010089}
90const char *mbedtls_get_test_filename(void)
91{
Paul Elliott65064262023-11-27 17:29:05 +000092 const char *filename;
93
94#ifdef MBEDTLS_THREADING_C
95 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
96#endif /* MBEDTLS_THREADING_C */
97
98 /* It should be ok just to pass back the pointer here, as it is going to
99 * be a pointer into non changing data. */
100 filename = mbedtls_test_info.filename;
101
102#ifdef MBEDTLS_THREADING_C
103 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
104#endif /* MBEDTLS_THREADING_C */
105
106 return filename;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100107}
108
109int mbedtls_test_get_line_no(void)
110{
Paul Elliott65064262023-11-27 17:29:05 +0000111 int line_no;
112
113#ifdef MBEDTLS_THREADING_C
114 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
115#endif /* MBEDTLS_THREADING_C */
116
117 line_no = mbedtls_test_info.line_no;
118
119#ifdef MBEDTLS_THREADING_C
120 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
121#endif /* MBEDTLS_THREADING_C */
122
123 return line_no;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100124}
125
126void mbedtls_test_increment_step(void)
127{
Paul Elliott65064262023-11-27 17:29:05 +0000128#ifdef MBEDTLS_THREADING_C
129 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
130#endif /* MBEDTLS_THREADING_C */
131
Paul Elliott4580d4d2023-10-27 18:41:02 +0100132 ++mbedtls_test_info.step;
Paul Elliott65064262023-11-27 17:29:05 +0000133
134#ifdef MBEDTLS_THREADING_C
135 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
136#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100137}
138
139unsigned long mbedtls_test_get_step(void)
140{
Paul Elliott65064262023-11-27 17:29:05 +0000141 unsigned long step;
142
143#ifdef MBEDTLS_THREADING_C
144 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
145#endif /* MBEDTLS_THREADING_C */
146
147 step = mbedtls_test_info.step;
148
149#ifdef MBEDTLS_THREADING_C
150 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
151#endif /* MBEDTLS_THREADING_C */
152
153 return step;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100154}
155
Paul Elliott264e2102024-02-15 12:28:56 +0000156static void mbedtls_test_reset_step_internal(void)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100157{
Paul Elliottfad978b2024-01-30 18:00:26 +0000158 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000159 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000160
Paul Elliottac61cee2024-02-02 17:53:38 +0000161 mbedtls_test_info.step = (unsigned long) (-1);
162}
163
164void mbedtls_test_set_step(unsigned long step)
165{
166#ifdef MBEDTLS_THREADING_C
167 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
168#endif /* MBEDTLS_THREADING_C */
169
Paul Elliott65064262023-11-27 17:29:05 +0000170 mbedtls_test_info.step = step;
Paul Elliottac61cee2024-02-02 17:53:38 +0000171
172#ifdef MBEDTLS_THREADING_C
173 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
174#endif /* MBEDTLS_THREADING_C */
Paul Elliott65064262023-11-27 17:29:05 +0000175}
176
177void mbedtls_test_get_line1(char *line)
178{
179#ifdef MBEDTLS_THREADING_C
180 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
181#endif /* MBEDTLS_THREADING_C */
182
183 memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH);
184
185#ifdef MBEDTLS_THREADING_C
186 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
187#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100188}
Paul Elliott5c498f32023-10-31 16:38:56 +0000189
Paul Elliott264e2102024-02-15 12:28:56 +0000190static void mbedtls_test_set_line1_internal(const char *line)
Paul Elliott5c498f32023-10-31 16:38:56 +0000191{
Paul Elliottfad978b2024-01-30 18:00:26 +0000192 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000193 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000194
Paul Elliott5c498f32023-10-31 16:38:56 +0000195 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000196 memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000197 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000198 memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000199 }
200}
201
Paul Elliott65064262023-11-27 17:29:05 +0000202void mbedtls_test_get_line2(char *line)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100203{
Paul Elliott65064262023-11-27 17:29:05 +0000204#ifdef MBEDTLS_THREADING_C
205 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
206#endif /* MBEDTLS_THREADING_C */
207
208 memcpy(line, mbedtls_test_info.line2, MBEDTLS_TEST_LINE_LENGTH);
209
210#ifdef MBEDTLS_THREADING_C
211 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
212#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100213}
214
Paul Elliott264e2102024-02-15 12:28:56 +0000215static void mbedtls_test_set_line2_internal(const char *line)
Paul Elliott65064262023-11-27 17:29:05 +0000216{
Paul Elliottfad978b2024-01-30 18:00:26 +0000217 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000218 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000219
Paul Elliott5c498f32023-10-31 16:38:56 +0000220 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000221 memset(mbedtls_test_info.line2, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000222 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000223 memcpy(mbedtls_test_info.line2, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000224 }
225}
226
227
Paul Elliott4580d4d2023-10-27 18:41:02 +0100228#if defined(MBEDTLS_TEST_MUTEX_USAGE)
229const char *mbedtls_test_get_mutex_usage_error(void)
230{
Paul Elliott114ed5e2024-02-13 15:35:14 +0000231 const char *usage_error;
232
233#ifdef MBEDTLS_THREADING_C
234 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
235#endif /* MBEDTLS_THREADING_C */
236
237 usage_error = mbedtls_test_info.mutex_usage_error;
238
239#ifdef MBEDTLS_THREADING_C
240 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
241#endif /* MBEDTLS_THREADING_C */
242
243 return usage_error;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100244}
245
246void mbedtls_test_set_mutex_usage_error(const char *msg)
247{
Paul Elliott65064262023-11-27 17:29:05 +0000248#ifdef MBEDTLS_THREADING_C
249 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
250#endif /* MBEDTLS_THREADING_C */
251
Paul Elliott4580d4d2023-10-27 18:41:02 +0100252 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
253 mbedtls_test_info.mutex_usage_error = msg;
254 }
Paul Elliott65064262023-11-27 17:29:05 +0000255
256#ifdef MBEDTLS_THREADING_C
257 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
258#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100259}
260#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
261
Paul Elliottc7a1e992023-11-03 18:44:57 +0000262#if defined(MBEDTLS_BIGNUM_C)
263
264unsigned mbedtls_test_get_case_uses_negative_0(void)
265{
Paul Elliott65064262023-11-27 17:29:05 +0000266 unsigned test_case_uses_negative_0 = 0;
267#ifdef MBEDTLS_THREADING_C
268 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
269#endif /* MBEDTLS_THREADING_C */
270 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
271
272#ifdef MBEDTLS_THREADING_C
273 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
274#endif /* MBEDTLS_THREADING_C */
275
276 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000277}
278
Paul Elliott264e2102024-02-15 12:28:56 +0000279static void mbedtls_test_set_case_uses_negative_0_internal(unsigned uses)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000280{
Paul Elliottfad978b2024-01-30 18:00:26 +0000281 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000282 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000283
Paul Elliottc7a1e992023-11-03 18:44:57 +0000284 mbedtls_test_info.case_uses_negative_0 = uses;
285}
286
287void mbedtls_test_increment_case_uses_negative_0(void)
288{
Paul Elliott65064262023-11-27 17:29:05 +0000289#ifdef MBEDTLS_THREADING_C
290 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
291#endif /* MBEDTLS_THREADING_C */
292
Paul Elliottc7a1e992023-11-03 18:44:57 +0000293 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000294
295#ifdef MBEDTLS_THREADING_C
296 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
297#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000298}
299
Paul Elliott3d2db892024-01-19 20:42:56 +0000300#endif /* MBEDTLS_BIGNUM_C */
301
302#ifdef MBEDTLS_TEST_MUTEX_USAGE
303mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
304{
305 return &mbedtls_test_info_mutex;
306}
307
308#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000309
Paul Elliott4580d4d2023-10-27 18:41:02 +0100310/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200311/* Helper Functions */
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200314{
315 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200316
317#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
318 /* Make sure that injected entropy is present. Otherwise
319 * psa_crypto_init() will fail. This is not necessary for test suites
320 * that don't use PSA, but it's harmless (except for leaving a file
321 * behind). */
322 ret = mbedtls_test_inject_entropy_restore();
323 if (ret != 0) {
324 return ret;
325 }
326#endif
327
Ronald Cronf40529d2020-06-09 16:27:37 +0200328#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200330#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200331
Paul Elliott65064262023-11-27 17:29:05 +0000332#ifdef MBEDTLS_THREADING_C
333 mbedtls_mutex_init(&mbedtls_test_info_mutex);
334#endif /* MBEDTLS_THREADING_C */
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200337}
338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200340{
Paul Elliott65064262023-11-27 17:29:05 +0000341#ifdef MBEDTLS_THREADING_C
342 mbedtls_mutex_free(&mbedtls_test_info_mutex);
343#endif /* MBEDTLS_THREADING_C */
344
Ronald Cronf40529d2020-06-09 16:27:37 +0200345#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200347#endif /* MBEDTLS_PLATFORM_C */
348}
349
Gilles Peskine881447d2022-12-08 15:24:52 +0100350int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200351{
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200353 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200355 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200357 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 } else {
359 return -1;
360 }
Ronald Crona0c25392020-06-18 10:10:46 +0200361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200363}
364
Paul Elliott97182032024-02-13 13:27:06 +0000365static void mbedtls_test_fail_internal(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000366{
Paul Elliott97182032024-02-13 13:27:06 +0000367 /* Internal function only - mbedtls_test_info_mutex should be held prior
368 * to calling this function. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000369
370 /* Don't use accessor, we already hold mutex. */
371 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
372 /* If we have already recorded the test as having failed then don't
Chris Jones9634bb12021-01-20 15:56:42 +0000373 * overwrite any previous information about the failure. */
Paul Elliott264e2102024-02-15 12:28:56 +0000374 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000375 }
Paul Elliott97182032024-02-13 13:27:06 +0000376}
377
378void mbedtls_test_fail(const char *test, int line_no, const char *filename)
379{
380#ifdef MBEDTLS_THREADING_C
381 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
382#endif /* MBEDTLS_THREADING_C */
383
384 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000385
386#ifdef MBEDTLS_THREADING_C
387 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
388#endif /* MBEDTLS_THREADING_C */
Chris Jones9634bb12021-01-20 15:56:42 +0000389}
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000392{
Paul Elliottfad978b2024-01-30 18:00:26 +0000393#ifdef MBEDTLS_THREADING_C
394 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
395#endif /* MBEDTLS_THREADING_C */
396
Paul Elliott264e2102024-02-15 12:28:56 +0000397 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000398
399#ifdef MBEDTLS_THREADING_C
400 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
401#endif /* MBEDTLS_THREADING_C */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000402}
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000405{
Paul Elliottfad978b2024-01-30 18:00:26 +0000406#ifdef MBEDTLS_THREADING_C
407 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
408#endif /* MBEDTLS_THREADING_C */
409
Paul Elliott264e2102024-02-15 12:28:56 +0000410 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
411 mbedtls_test_reset_step_internal();
412 mbedtls_test_set_line1_internal(NULL);
413 mbedtls_test_set_line2_internal(NULL);
Paul Elliott5c498f32023-10-31 16:38:56 +0000414
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100415#if defined(MBEDTLS_BIGNUM_C)
Paul Elliott264e2102024-02-15 12:28:56 +0000416 mbedtls_test_set_case_uses_negative_0_internal(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100417#endif
Paul Elliottfad978b2024-01-30 18:00:26 +0000418
419#ifdef MBEDTLS_THREADING_C
Paul Elliott0b2835d2024-02-01 13:27:04 +0000420 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
Paul Elliottfad978b2024-01-30 18:00:26 +0000421#endif /* MBEDTLS_THREADING_C */
Gilles Peskine89615ee2021-04-29 20:28:54 +0200422}
423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424int mbedtls_test_equal(const char *test, int line_no, const char *filename,
425 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200426{
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 TEST_CF_PUBLIC(&value1, sizeof(value1));
428 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (value1 == value2) {
431 return 1;
432 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200433
Paul Elliottfad978b2024-01-30 18:00:26 +0000434#ifdef MBEDTLS_THREADING_C
435 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
436#endif /* MBEDTLS_THREADING_C */
437
438 /* Don't use accessor, as we already hold mutex. */
439 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
440 /* If we've already recorded the test as having failed then don't
Gilles Peskine89615ee2021-04-29 20:28:54 +0200441 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000442
443 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000444 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000445 (void) mbedtls_snprintf(buf, sizeof(buf),
446 "lhs = 0x%016llx = %lld",
447 value1, (long long) value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000448 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000449 (void) mbedtls_snprintf(buf, sizeof(buf),
450 "rhs = 0x%016llx = %lld",
451 value2, (long long) value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000452 mbedtls_test_set_line2_internal(buf);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200453 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000454
455#ifdef MBEDTLS_THREADING_C
456 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
457#endif /* MBEDTLS_THREADING_C */
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000460}
461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
463 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200464{
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 TEST_CF_PUBLIC(&value1, sizeof(value1));
466 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (value1 <= value2) {
469 return 1;
470 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200471
Paul Elliottfad978b2024-01-30 18:00:26 +0000472#ifdef MBEDTLS_THREADING_C
473 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
474#endif /* MBEDTLS_THREADING_C */
475
476 /* Don't use accessor, we already hold mutex. */
477 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
478 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200479 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000480
481 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000482 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000483 (void) mbedtls_snprintf(buf, sizeof(buf),
484 "lhs = 0x%016llx = %llu",
485 value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000486 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000487 (void) mbedtls_snprintf(buf, sizeof(buf),
488 "rhs = 0x%016llx = %llu",
489 value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000490 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200491 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000492
493#ifdef MBEDTLS_THREADING_C
494 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
495#endif /* MBEDTLS_THREADING_C */
496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200498}
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
501 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200502{
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 TEST_CF_PUBLIC(&value1, sizeof(value1));
504 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (value1 <= value2) {
507 return 1;
508 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200509
Paul Elliottfad978b2024-01-30 18:00:26 +0000510#ifdef MBEDTLS_THREADING_C
511 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
512#endif /* MBEDTLS_THREADING_C */
513
514 /* Don't use accessor, we already hold mutex. */
Paul Elliottf20728e2024-02-06 12:49:45 +0000515 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
Paul Elliottfad978b2024-01-30 18:00:26 +0000516 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200517 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000518
519 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000520 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000521 (void) mbedtls_snprintf(buf, sizeof(buf),
522 "lhs = 0x%016llx = %lld",
523 (unsigned long long) value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000524 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000525 (void) mbedtls_snprintf(buf, sizeof(buf),
526 "rhs = 0x%016llx = %lld",
527 (unsigned long long) value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000528 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200529 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000530
531#ifdef MBEDTLS_THREADING_C
532 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
533#endif /* MBEDTLS_THREADING_C */
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200536}
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538int mbedtls_test_unhexify(unsigned char *obuf,
539 size_t obufmax,
540 const char *ibuf,
541 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200542{
543 unsigned char uc, uc2;
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200546
547 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if ((*len) & 1) {
549 return -1;
550 }
Ronald Crona0c25392020-06-18 10:10:46 +0200551 *len /= 2;
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 if ((*len) > obufmax) {
554 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200555 }
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 while (*ibuf != 0) {
558 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
559 return -1;
560 }
561
562 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
563 return -1;
564 }
565
566 *(obuf++) = (uc << 4) | uc2;
567 }
568
569 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200570}
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572void mbedtls_test_hexify(unsigned char *obuf,
573 const unsigned char *ibuf,
574 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200575{
576 unsigned char l, h;
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200579 h = *ibuf / 16;
580 l = *ibuf % 16;
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200583 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200585 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200589 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200591 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200593
594 ++ibuf;
595 len--;
596 }
597}
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200600{
601 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 p = mbedtls_calloc(1, actual_len);
605 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200610}
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200613{
614 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200615 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (*olen == 0) {
620 return mbedtls_test_zero_alloc(*olen);
621 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 obuf = mbedtls_calloc(1, *olen);
624 TEST_HELPER_ASSERT(obuf != NULL);
625 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200628}
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
631 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200632{
633 int ret = 0;
634 uint32_t i = 0;
635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (a_len != b_len) {
637 return -1;
638 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 for (i = 0; i < a_len; i++) {
641 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200642 ret = -1;
643 break;
644 }
645 }
646 return ret;
647}
Ronald Crona1236142020-07-01 16:01:21 +0200648
Chris Jones96ae73b2021-01-08 17:04:59 +0000649#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100650void mbedtls_test_err_add_check(int high, int low,
651 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000652{
Chris Jones3f613c12021-03-31 09:34:22 +0100653 /* Error codes are always negative (a value of zero is a success) however
654 * their positive opposites can be easier to understand. The following
655 * examples given in comments have been made positive for ease of
656 * understanding. The structure of an error code is such:
657 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100658 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100659 *
660 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100661 * h = high level error code (includes high level module ID (bits 12..14)
662 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100663 * l = low level error code.
664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (high > -0x1000 && high != 0) {
666 /* high < 0001000000000000
667 * No high level module ID bits are set.
668 */
669 mbedtls_test_fail("'high' is not a high-level error code",
670 line, file);
671 } else if (high < -0x7F80) {
672 /* high > 0111111110000000
673 * Error code is greater than the largest allowed high level module ID.
674 */
675 mbedtls_test_fail("'high' error code is greater than 15 bits",
676 line, file);
677 } else if ((high & 0x7F) != 0) {
678 /* high & 0000000001111111
679 * Error code contains low level error code bits.
680 */
681 mbedtls_test_fail("'high' contains a low-level error code",
682 line, file);
683 } else if (low < -0x007F) {
684 /* low > 0000000001111111
685 * Error code contains high or module level error code bits.
686 */
687 mbedtls_test_fail("'low' error code is greater than 7 bits",
688 line, file);
689 } else if (low > 0) {
690 mbedtls_test_fail("'low' error code is greater than zero",
691 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000692 }
693}
694#endif /* MBEDTLS_TEST_HOOKS */