blob: 9053aac4b7de55fdeec5b61cb8c16b71dbb0ad16 [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 *
36 * NOTE - there are two types of accessors here; the _internal functions, which
37 * are expected to be used from this module *only*. These do not attempt to lock
38 * the mbedtls_test_info_mutex, as they are designed to be called from within
39 * public functions which do lock the mutex first (if mutexes are enabled).
40 * The main reason for this is the need to set some sets of test data
41 * atomically, without releasing the mutex inbetween to prevent race conditions
42 * resulting in mixed test info. The other public accessors have prototypes in
43 * the header and have to lock the mutex for safety as we obviously cannot
44 * control where they are called from. */
Paul Elliott4580d4d2023-10-27 18:41:02 +010045
46mbedtls_test_result_t mbedtls_test_get_result(void)
47{
Paul Elliott65064262023-11-27 17:29:05 +000048 mbedtls_test_result_t result;
49
50#ifdef MBEDTLS_THREADING_C
51 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
52#endif /* MBEDTLS_THREADING_C */
53
54 result = mbedtls_test_info.result;
55
56#ifdef MBEDTLS_THREADING_C
57 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
58#endif /* MBEDTLS_THREADING_C */
59
60 return result;
Paul Elliott4580d4d2023-10-27 18:41:02 +010061}
62
Paul Elliott264e2102024-02-15 12:28:56 +000063static void mbedtls_test_set_result_internal(mbedtls_test_result_t result, const char *test,
64 int line_no, const char *filename)
Paul Elliott5c498f32023-10-31 16:38:56 +000065{
Paul Elliottfad978b2024-01-30 18:00:26 +000066 /* Internal function only - mbedtls_test_info_mutex should be held prior
67 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +000068
Paul Elliott5c498f32023-10-31 16:38:56 +000069 mbedtls_test_info.result = result;
70 mbedtls_test_info.test = test;
71 mbedtls_test_info.line_no = line_no;
72 mbedtls_test_info.filename = filename;
73}
74
Paul Elliott4580d4d2023-10-27 18:41:02 +010075const char *mbedtls_test_get_test(void)
76{
Paul Elliott65064262023-11-27 17:29:05 +000077 const char *test;
78
79#ifdef MBEDTLS_THREADING_C
80 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
81#endif /* MBEDTLS_THREADING_C */
82
83 test = mbedtls_test_info.test;
84
85#ifdef MBEDTLS_THREADING_C
86 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
87#endif /* MBEDTLS_THREADING_C */
88
89 return test;
Paul Elliott4580d4d2023-10-27 18:41:02 +010090}
91const char *mbedtls_get_test_filename(void)
92{
Paul Elliott65064262023-11-27 17:29:05 +000093 const char *filename;
94
95#ifdef MBEDTLS_THREADING_C
96 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
97#endif /* MBEDTLS_THREADING_C */
98
99 /* It should be ok just to pass back the pointer here, as it is going to
100 * be a pointer into non changing data. */
101 filename = mbedtls_test_info.filename;
102
103#ifdef MBEDTLS_THREADING_C
104 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
105#endif /* MBEDTLS_THREADING_C */
106
107 return filename;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100108}
109
110int mbedtls_test_get_line_no(void)
111{
Paul Elliott65064262023-11-27 17:29:05 +0000112 int line_no;
113
114#ifdef MBEDTLS_THREADING_C
115 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
116#endif /* MBEDTLS_THREADING_C */
117
118 line_no = mbedtls_test_info.line_no;
119
120#ifdef MBEDTLS_THREADING_C
121 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
122#endif /* MBEDTLS_THREADING_C */
123
124 return line_no;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100125}
126
127void mbedtls_test_increment_step(void)
128{
Paul Elliott65064262023-11-27 17:29:05 +0000129#ifdef MBEDTLS_THREADING_C
130 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
131#endif /* MBEDTLS_THREADING_C */
132
Paul Elliott4580d4d2023-10-27 18:41:02 +0100133 ++mbedtls_test_info.step;
Paul Elliott65064262023-11-27 17:29:05 +0000134
135#ifdef MBEDTLS_THREADING_C
136 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
137#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100138}
139
140unsigned long mbedtls_test_get_step(void)
141{
Paul Elliott65064262023-11-27 17:29:05 +0000142 unsigned long step;
143
144#ifdef MBEDTLS_THREADING_C
145 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
146#endif /* MBEDTLS_THREADING_C */
147
148 step = mbedtls_test_info.step;
149
150#ifdef MBEDTLS_THREADING_C
151 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
152#endif /* MBEDTLS_THREADING_C */
153
154 return step;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100155}
156
Paul Elliott264e2102024-02-15 12:28:56 +0000157static void mbedtls_test_reset_step_internal(void)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100158{
Paul Elliottfad978b2024-01-30 18:00:26 +0000159 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000160 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000161
Paul Elliottac61cee2024-02-02 17:53:38 +0000162 mbedtls_test_info.step = (unsigned long) (-1);
163}
164
165void mbedtls_test_set_step(unsigned long step)
166{
167#ifdef MBEDTLS_THREADING_C
168 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
169#endif /* MBEDTLS_THREADING_C */
170
Paul Elliott65064262023-11-27 17:29:05 +0000171 mbedtls_test_info.step = step;
Paul Elliottac61cee2024-02-02 17:53:38 +0000172
173#ifdef MBEDTLS_THREADING_C
174 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
175#endif /* MBEDTLS_THREADING_C */
Paul Elliott65064262023-11-27 17:29:05 +0000176}
177
178void mbedtls_test_get_line1(char *line)
179{
180#ifdef MBEDTLS_THREADING_C
181 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
182#endif /* MBEDTLS_THREADING_C */
183
184 memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH);
185
186#ifdef MBEDTLS_THREADING_C
187 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
188#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100189}
Paul Elliott5c498f32023-10-31 16:38:56 +0000190
Paul Elliott264e2102024-02-15 12:28:56 +0000191static void mbedtls_test_set_line1_internal(const char *line)
Paul Elliott5c498f32023-10-31 16:38:56 +0000192{
Paul Elliottfad978b2024-01-30 18:00:26 +0000193 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000194 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000195
Paul Elliott5c498f32023-10-31 16:38:56 +0000196 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000197 memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000198 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000199 memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000200 }
201}
202
Paul Elliott65064262023-11-27 17:29:05 +0000203void mbedtls_test_get_line2(char *line)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100204{
Paul Elliott65064262023-11-27 17:29:05 +0000205#ifdef MBEDTLS_THREADING_C
206 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
207#endif /* MBEDTLS_THREADING_C */
208
209 memcpy(line, mbedtls_test_info.line2, MBEDTLS_TEST_LINE_LENGTH);
210
211#ifdef MBEDTLS_THREADING_C
212 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
213#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100214}
215
Paul Elliott264e2102024-02-15 12:28:56 +0000216static void mbedtls_test_set_line2_internal(const char *line)
Paul Elliott65064262023-11-27 17:29:05 +0000217{
Paul Elliottfad978b2024-01-30 18:00:26 +0000218 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000219 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000220
Paul Elliott5c498f32023-10-31 16:38:56 +0000221 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000222 memset(mbedtls_test_info.line2, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000223 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000224 memcpy(mbedtls_test_info.line2, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000225 }
226}
227
228
Paul Elliott4580d4d2023-10-27 18:41:02 +0100229#if defined(MBEDTLS_TEST_MUTEX_USAGE)
230const char *mbedtls_test_get_mutex_usage_error(void)
231{
232 return mbedtls_test_info.mutex_usage_error;
233}
234
235void mbedtls_test_set_mutex_usage_error(const char *msg)
236{
Paul Elliott65064262023-11-27 17:29:05 +0000237#ifdef MBEDTLS_THREADING_C
238 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
239#endif /* MBEDTLS_THREADING_C */
240
Paul Elliott4580d4d2023-10-27 18:41:02 +0100241 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
242 mbedtls_test_info.mutex_usage_error = msg;
243 }
Paul Elliott65064262023-11-27 17:29:05 +0000244
245#ifdef MBEDTLS_THREADING_C
246 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
247#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100248}
249#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
250
Paul Elliottc7a1e992023-11-03 18:44:57 +0000251#if defined(MBEDTLS_BIGNUM_C)
252
253unsigned mbedtls_test_get_case_uses_negative_0(void)
254{
Paul Elliott65064262023-11-27 17:29:05 +0000255 unsigned test_case_uses_negative_0 = 0;
256#ifdef MBEDTLS_THREADING_C
257 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
258#endif /* MBEDTLS_THREADING_C */
259 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
260
261#ifdef MBEDTLS_THREADING_C
262 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
263#endif /* MBEDTLS_THREADING_C */
264
265 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000266}
267
Paul Elliott264e2102024-02-15 12:28:56 +0000268static void mbedtls_test_set_case_uses_negative_0_internal(unsigned uses)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000269{
Paul Elliottfad978b2024-01-30 18:00:26 +0000270 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000271 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000272
Paul Elliottc7a1e992023-11-03 18:44:57 +0000273 mbedtls_test_info.case_uses_negative_0 = uses;
274}
275
276void mbedtls_test_increment_case_uses_negative_0(void)
277{
Paul Elliott65064262023-11-27 17:29:05 +0000278#ifdef MBEDTLS_THREADING_C
279 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
280#endif /* MBEDTLS_THREADING_C */
281
Paul Elliottc7a1e992023-11-03 18:44:57 +0000282 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000283
284#ifdef MBEDTLS_THREADING_C
285 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
286#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000287}
288
Paul Elliott3d2db892024-01-19 20:42:56 +0000289#endif /* MBEDTLS_BIGNUM_C */
290
291#ifdef MBEDTLS_TEST_MUTEX_USAGE
292mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
293{
294 return &mbedtls_test_info_mutex;
295}
296
297#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000298
Paul Elliott4580d4d2023-10-27 18:41:02 +0100299/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200300/* Helper Functions */
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200303{
304 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200305
306#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
307 /* Make sure that injected entropy is present. Otherwise
308 * psa_crypto_init() will fail. This is not necessary for test suites
309 * that don't use PSA, but it's harmless (except for leaving a file
310 * behind). */
311 ret = mbedtls_test_inject_entropy_restore();
312 if (ret != 0) {
313 return ret;
314 }
315#endif
316
Ronald Cronf40529d2020-06-09 16:27:37 +0200317#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200319#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200320
Paul Elliott65064262023-11-27 17:29:05 +0000321#ifdef MBEDTLS_THREADING_C
322 mbedtls_mutex_init(&mbedtls_test_info_mutex);
323#endif /* MBEDTLS_THREADING_C */
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200326}
327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200329{
Paul Elliott65064262023-11-27 17:29:05 +0000330#ifdef MBEDTLS_THREADING_C
331 mbedtls_mutex_free(&mbedtls_test_info_mutex);
332#endif /* MBEDTLS_THREADING_C */
333
Ronald Cronf40529d2020-06-09 16:27:37 +0200334#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200336#endif /* MBEDTLS_PLATFORM_C */
337}
338
Gilles Peskine881447d2022-12-08 15:24:52 +0100339int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200340{
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200342 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200344 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200346 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 } else {
348 return -1;
349 }
Ronald Crona0c25392020-06-18 10:10:46 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200352}
353
Paul Elliott97182032024-02-13 13:27:06 +0000354static void mbedtls_test_fail_internal(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000355{
Paul Elliott97182032024-02-13 13:27:06 +0000356 /* Internal function only - mbedtls_test_info_mutex should be held prior
357 * to calling this function. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000358
359 /* Don't use accessor, we already hold mutex. */
360 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
361 /* If we have already recorded the test as having failed then don't
Chris Jones9634bb12021-01-20 15:56:42 +0000362 * overwrite any previous information about the failure. */
Paul Elliott264e2102024-02-15 12:28:56 +0000363 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000364 }
Paul Elliott97182032024-02-13 13:27:06 +0000365}
366
367void mbedtls_test_fail(const char *test, int line_no, const char *filename)
368{
369#ifdef MBEDTLS_THREADING_C
370 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
371#endif /* MBEDTLS_THREADING_C */
372
373 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000374
375#ifdef MBEDTLS_THREADING_C
376 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
377#endif /* MBEDTLS_THREADING_C */
Chris Jones9634bb12021-01-20 15:56:42 +0000378}
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000381{
Paul Elliottfad978b2024-01-30 18:00:26 +0000382#ifdef MBEDTLS_THREADING_C
383 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
384#endif /* MBEDTLS_THREADING_C */
385
Paul Elliott264e2102024-02-15 12:28:56 +0000386 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000387
388#ifdef MBEDTLS_THREADING_C
389 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
390#endif /* MBEDTLS_THREADING_C */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000391}
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000394{
Paul Elliottfad978b2024-01-30 18:00:26 +0000395#ifdef MBEDTLS_THREADING_C
396 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
397#endif /* MBEDTLS_THREADING_C */
398
Paul Elliott264e2102024-02-15 12:28:56 +0000399 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
400 mbedtls_test_reset_step_internal();
401 mbedtls_test_set_line1_internal(NULL);
402 mbedtls_test_set_line2_internal(NULL);
Paul Elliott5c498f32023-10-31 16:38:56 +0000403
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100404#if defined(MBEDTLS_BIGNUM_C)
Paul Elliott264e2102024-02-15 12:28:56 +0000405 mbedtls_test_set_case_uses_negative_0_internal(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100406#endif
Paul Elliottfad978b2024-01-30 18:00:26 +0000407
408#ifdef MBEDTLS_THREADING_C
Paul Elliott0b2835d2024-02-01 13:27:04 +0000409 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
Paul Elliottfad978b2024-01-30 18:00:26 +0000410#endif /* MBEDTLS_THREADING_C */
Gilles Peskine89615ee2021-04-29 20:28:54 +0200411}
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413int mbedtls_test_equal(const char *test, int line_no, const char *filename,
414 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200415{
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 TEST_CF_PUBLIC(&value1, sizeof(value1));
417 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (value1 == value2) {
420 return 1;
421 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200422
Paul Elliottfad978b2024-01-30 18:00:26 +0000423#ifdef MBEDTLS_THREADING_C
424 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
425#endif /* MBEDTLS_THREADING_C */
426
427 /* Don't use accessor, as we already hold mutex. */
428 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
429 /* If we've already recorded the test as having failed then don't
Gilles Peskine89615ee2021-04-29 20:28:54 +0200430 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000431
432 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000433 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000434 (void) mbedtls_snprintf(buf, sizeof(buf),
435 "lhs = 0x%016llx = %lld",
436 value1, (long long) value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000437 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000438 (void) mbedtls_snprintf(buf, sizeof(buf),
439 "rhs = 0x%016llx = %lld",
440 value2, (long long) value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000441 mbedtls_test_set_line2_internal(buf);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200442 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000443
444#ifdef MBEDTLS_THREADING_C
445 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
446#endif /* MBEDTLS_THREADING_C */
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000449}
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
452 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200453{
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 TEST_CF_PUBLIC(&value1, sizeof(value1));
455 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (value1 <= value2) {
458 return 1;
459 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200460
Paul Elliottfad978b2024-01-30 18:00:26 +0000461#ifdef MBEDTLS_THREADING_C
462 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
463#endif /* MBEDTLS_THREADING_C */
464
465 /* Don't use accessor, we already hold mutex. */
466 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
467 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200468 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000469
470 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000471 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000472 (void) mbedtls_snprintf(buf, sizeof(buf),
473 "lhs = 0x%016llx = %llu",
474 value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000475 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000476 (void) mbedtls_snprintf(buf, sizeof(buf),
477 "rhs = 0x%016llx = %llu",
478 value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000479 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200480 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000481
482#ifdef MBEDTLS_THREADING_C
483 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
484#endif /* MBEDTLS_THREADING_C */
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200487}
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
490 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200491{
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 TEST_CF_PUBLIC(&value1, sizeof(value1));
493 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (value1 <= value2) {
496 return 1;
497 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200498
Paul Elliottfad978b2024-01-30 18:00:26 +0000499#ifdef MBEDTLS_THREADING_C
500 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
501#endif /* MBEDTLS_THREADING_C */
502
503 /* Don't use accessor, we already hold mutex. */
Paul Elliottf20728e2024-02-06 12:49:45 +0000504 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
Paul Elliottfad978b2024-01-30 18:00:26 +0000505 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200506 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000507
508 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000509 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000510 (void) mbedtls_snprintf(buf, sizeof(buf),
511 "lhs = 0x%016llx = %lld",
512 (unsigned long long) value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000513 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000514 (void) mbedtls_snprintf(buf, sizeof(buf),
515 "rhs = 0x%016llx = %lld",
516 (unsigned long long) value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000517 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200518 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000519
520#ifdef MBEDTLS_THREADING_C
521 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
522#endif /* MBEDTLS_THREADING_C */
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200525}
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527int mbedtls_test_unhexify(unsigned char *obuf,
528 size_t obufmax,
529 const char *ibuf,
530 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200531{
532 unsigned char uc, uc2;
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200535
536 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if ((*len) & 1) {
538 return -1;
539 }
Ronald Crona0c25392020-06-18 10:10:46 +0200540 *len /= 2;
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if ((*len) > obufmax) {
543 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200544 }
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 while (*ibuf != 0) {
547 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
548 return -1;
549 }
550
551 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
552 return -1;
553 }
554
555 *(obuf++) = (uc << 4) | uc2;
556 }
557
558 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200559}
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561void mbedtls_test_hexify(unsigned char *obuf,
562 const unsigned char *ibuf,
563 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200564{
565 unsigned char l, h;
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200568 h = *ibuf / 16;
569 l = *ibuf % 16;
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200572 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200574 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200578 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200580 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200582
583 ++ibuf;
584 len--;
585 }
586}
587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200589{
590 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 p = mbedtls_calloc(1, actual_len);
594 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200599}
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200602{
603 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200604 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (*olen == 0) {
609 return mbedtls_test_zero_alloc(*olen);
610 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 obuf = mbedtls_calloc(1, *olen);
613 TEST_HELPER_ASSERT(obuf != NULL);
614 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200617}
618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
620 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200621{
622 int ret = 0;
623 uint32_t i = 0;
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if (a_len != b_len) {
626 return -1;
627 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 for (i = 0; i < a_len; i++) {
630 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200631 ret = -1;
632 break;
633 }
634 }
635 return ret;
636}
Ronald Crona1236142020-07-01 16:01:21 +0200637
Chris Jones96ae73b2021-01-08 17:04:59 +0000638#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100639void mbedtls_test_err_add_check(int high, int low,
640 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000641{
Chris Jones3f613c12021-03-31 09:34:22 +0100642 /* Error codes are always negative (a value of zero is a success) however
643 * their positive opposites can be easier to understand. The following
644 * examples given in comments have been made positive for ease of
645 * understanding. The structure of an error code is such:
646 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100647 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100648 *
649 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100650 * h = high level error code (includes high level module ID (bits 12..14)
651 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100652 * l = low level error code.
653 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (high > -0x1000 && high != 0) {
655 /* high < 0001000000000000
656 * No high level module ID bits are set.
657 */
658 mbedtls_test_fail("'high' is not a high-level error code",
659 line, file);
660 } else if (high < -0x7F80) {
661 /* high > 0111111110000000
662 * Error code is greater than the largest allowed high level module ID.
663 */
664 mbedtls_test_fail("'high' error code is greater than 15 bits",
665 line, file);
666 } else if ((high & 0x7F) != 0) {
667 /* high & 0000000001111111
668 * Error code contains low level error code bits.
669 */
670 mbedtls_test_fail("'high' contains a low-level error code",
671 line, file);
672 } else if (low < -0x007F) {
673 /* low > 0000000001111111
674 * Error code contains high or module level error code bits.
675 */
676 mbedtls_test_fail("'low' error code is greater than 7 bits",
677 line, file);
678 } else if (low > 0) {
679 mbedtls_test_fail("'low' error code is greater than zero",
680 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000681 }
682}
683#endif /* MBEDTLS_TEST_HOOKS */