blob: 8e1d564145e73f2be2a66253c847c182a344c535 [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{
Paul Elliott114ed5e2024-02-13 15:35:14 +0000232 const char *usage_error;
233
234#ifdef MBEDTLS_THREADING_C
235 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
236#endif /* MBEDTLS_THREADING_C */
237
238 usage_error = mbedtls_test_info.mutex_usage_error;
239
240#ifdef MBEDTLS_THREADING_C
241 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
242#endif /* MBEDTLS_THREADING_C */
243
244 return usage_error;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100245}
246
247void mbedtls_test_set_mutex_usage_error(const char *msg)
248{
Paul Elliott65064262023-11-27 17:29:05 +0000249#ifdef MBEDTLS_THREADING_C
250 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
251#endif /* MBEDTLS_THREADING_C */
252
Paul Elliott4580d4d2023-10-27 18:41:02 +0100253 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
254 mbedtls_test_info.mutex_usage_error = msg;
255 }
Paul Elliott65064262023-11-27 17:29:05 +0000256
257#ifdef MBEDTLS_THREADING_C
258 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
259#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100260}
261#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
262
Paul Elliottc7a1e992023-11-03 18:44:57 +0000263#if defined(MBEDTLS_BIGNUM_C)
264
265unsigned mbedtls_test_get_case_uses_negative_0(void)
266{
Paul Elliott65064262023-11-27 17:29:05 +0000267 unsigned test_case_uses_negative_0 = 0;
268#ifdef MBEDTLS_THREADING_C
269 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
270#endif /* MBEDTLS_THREADING_C */
271 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
272
273#ifdef MBEDTLS_THREADING_C
274 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
275#endif /* MBEDTLS_THREADING_C */
276
277 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000278}
279
Paul Elliott264e2102024-02-15 12:28:56 +0000280static void mbedtls_test_set_case_uses_negative_0_internal(unsigned uses)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000281{
Paul Elliottfad978b2024-01-30 18:00:26 +0000282 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000283 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000284
Paul Elliottc7a1e992023-11-03 18:44:57 +0000285 mbedtls_test_info.case_uses_negative_0 = uses;
286}
287
288void mbedtls_test_increment_case_uses_negative_0(void)
289{
Paul Elliott65064262023-11-27 17:29:05 +0000290#ifdef MBEDTLS_THREADING_C
291 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
292#endif /* MBEDTLS_THREADING_C */
293
Paul Elliottc7a1e992023-11-03 18:44:57 +0000294 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000295
296#ifdef MBEDTLS_THREADING_C
297 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
298#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000299}
300
Paul Elliott3d2db892024-01-19 20:42:56 +0000301#endif /* MBEDTLS_BIGNUM_C */
302
303#ifdef MBEDTLS_TEST_MUTEX_USAGE
304mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
305{
306 return &mbedtls_test_info_mutex;
307}
308
309#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000310
Paul Elliott4580d4d2023-10-27 18:41:02 +0100311/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200312/* Helper Functions */
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200315{
316 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200317
318#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
319 /* Make sure that injected entropy is present. Otherwise
320 * psa_crypto_init() will fail. This is not necessary for test suites
321 * that don't use PSA, but it's harmless (except for leaving a file
322 * behind). */
323 ret = mbedtls_test_inject_entropy_restore();
324 if (ret != 0) {
325 return ret;
326 }
327#endif
328
Ronald Cronf40529d2020-06-09 16:27:37 +0200329#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200331#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200332
Paul Elliott65064262023-11-27 17:29:05 +0000333#ifdef MBEDTLS_THREADING_C
334 mbedtls_mutex_init(&mbedtls_test_info_mutex);
335#endif /* MBEDTLS_THREADING_C */
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200338}
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200341{
Paul Elliott65064262023-11-27 17:29:05 +0000342#ifdef MBEDTLS_THREADING_C
343 mbedtls_mutex_free(&mbedtls_test_info_mutex);
344#endif /* MBEDTLS_THREADING_C */
345
Ronald Cronf40529d2020-06-09 16:27:37 +0200346#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200348#endif /* MBEDTLS_PLATFORM_C */
349}
350
Gilles Peskine881447d2022-12-08 15:24:52 +0100351int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200352{
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200354 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200356 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200358 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 } else {
360 return -1;
361 }
Ronald Crona0c25392020-06-18 10:10:46 +0200362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200364}
365
Paul Elliott97182032024-02-13 13:27:06 +0000366static void mbedtls_test_fail_internal(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000367{
Paul Elliott97182032024-02-13 13:27:06 +0000368 /* Internal function only - mbedtls_test_info_mutex should be held prior
369 * to calling this function. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000370
371 /* Don't use accessor, we already hold mutex. */
372 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
373 /* If we have already recorded the test as having failed then don't
Chris Jones9634bb12021-01-20 15:56:42 +0000374 * overwrite any previous information about the failure. */
Paul Elliott264e2102024-02-15 12:28:56 +0000375 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000376 }
Paul Elliott97182032024-02-13 13:27:06 +0000377}
378
379void mbedtls_test_fail(const char *test, int line_no, const char *filename)
380{
381#ifdef MBEDTLS_THREADING_C
382 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
383#endif /* MBEDTLS_THREADING_C */
384
385 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000386
387#ifdef MBEDTLS_THREADING_C
388 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
389#endif /* MBEDTLS_THREADING_C */
Chris Jones9634bb12021-01-20 15:56:42 +0000390}
391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000393{
Paul Elliottfad978b2024-01-30 18:00:26 +0000394#ifdef MBEDTLS_THREADING_C
395 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
396#endif /* MBEDTLS_THREADING_C */
397
Paul Elliott264e2102024-02-15 12:28:56 +0000398 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000399
400#ifdef MBEDTLS_THREADING_C
401 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
402#endif /* MBEDTLS_THREADING_C */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000403}
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000406{
Paul Elliottfad978b2024-01-30 18:00:26 +0000407#ifdef MBEDTLS_THREADING_C
408 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
409#endif /* MBEDTLS_THREADING_C */
410
Paul Elliott264e2102024-02-15 12:28:56 +0000411 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
412 mbedtls_test_reset_step_internal();
413 mbedtls_test_set_line1_internal(NULL);
414 mbedtls_test_set_line2_internal(NULL);
Paul Elliott5c498f32023-10-31 16:38:56 +0000415
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100416#if defined(MBEDTLS_BIGNUM_C)
Paul Elliott264e2102024-02-15 12:28:56 +0000417 mbedtls_test_set_case_uses_negative_0_internal(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100418#endif
Paul Elliottfad978b2024-01-30 18:00:26 +0000419
420#ifdef MBEDTLS_THREADING_C
Paul Elliott0b2835d2024-02-01 13:27:04 +0000421 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
Paul Elliottfad978b2024-01-30 18:00:26 +0000422#endif /* MBEDTLS_THREADING_C */
Gilles Peskine89615ee2021-04-29 20:28:54 +0200423}
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425int mbedtls_test_equal(const char *test, int line_no, const char *filename,
426 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200427{
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 TEST_CF_PUBLIC(&value1, sizeof(value1));
429 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (value1 == value2) {
432 return 1;
433 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200434
Paul Elliottfad978b2024-01-30 18:00:26 +0000435#ifdef MBEDTLS_THREADING_C
436 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
437#endif /* MBEDTLS_THREADING_C */
438
439 /* Don't use accessor, as we already hold mutex. */
440 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
441 /* If we've already recorded the test as having failed then don't
Gilles Peskine89615ee2021-04-29 20:28:54 +0200442 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000443
444 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000445 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000446 (void) mbedtls_snprintf(buf, sizeof(buf),
447 "lhs = 0x%016llx = %lld",
448 value1, (long long) value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000449 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000450 (void) mbedtls_snprintf(buf, sizeof(buf),
451 "rhs = 0x%016llx = %lld",
452 value2, (long long) value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000453 mbedtls_test_set_line2_internal(buf);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200454 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000455
456#ifdef MBEDTLS_THREADING_C
457 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
458#endif /* MBEDTLS_THREADING_C */
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000461}
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
464 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200465{
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 TEST_CF_PUBLIC(&value1, sizeof(value1));
467 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (value1 <= value2) {
470 return 1;
471 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200472
Paul Elliottfad978b2024-01-30 18:00:26 +0000473#ifdef MBEDTLS_THREADING_C
474 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
475#endif /* MBEDTLS_THREADING_C */
476
477 /* Don't use accessor, we already hold mutex. */
478 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
479 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200480 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000481
482 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000483 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000484 (void) mbedtls_snprintf(buf, sizeof(buf),
485 "lhs = 0x%016llx = %llu",
486 value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000487 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000488 (void) mbedtls_snprintf(buf, sizeof(buf),
489 "rhs = 0x%016llx = %llu",
490 value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000491 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200492 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000493
494#ifdef MBEDTLS_THREADING_C
495 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
496#endif /* MBEDTLS_THREADING_C */
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200499}
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
502 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200503{
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 TEST_CF_PUBLIC(&value1, sizeof(value1));
505 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (value1 <= value2) {
508 return 1;
509 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200510
Paul Elliottfad978b2024-01-30 18:00:26 +0000511#ifdef MBEDTLS_THREADING_C
512 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
513#endif /* MBEDTLS_THREADING_C */
514
515 /* Don't use accessor, we already hold mutex. */
Paul Elliottf20728e2024-02-06 12:49:45 +0000516 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
Paul Elliottfad978b2024-01-30 18:00:26 +0000517 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200518 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000519
520 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000521 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000522 (void) mbedtls_snprintf(buf, sizeof(buf),
523 "lhs = 0x%016llx = %lld",
524 (unsigned long long) value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000525 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000526 (void) mbedtls_snprintf(buf, sizeof(buf),
527 "rhs = 0x%016llx = %lld",
528 (unsigned long long) value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000529 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200530 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000531
532#ifdef MBEDTLS_THREADING_C
533 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
534#endif /* MBEDTLS_THREADING_C */
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200537}
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539int mbedtls_test_unhexify(unsigned char *obuf,
540 size_t obufmax,
541 const char *ibuf,
542 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200543{
544 unsigned char uc, uc2;
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200547
548 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 if ((*len) & 1) {
550 return -1;
551 }
Ronald Crona0c25392020-06-18 10:10:46 +0200552 *len /= 2;
553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if ((*len) > obufmax) {
555 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200556 }
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 while (*ibuf != 0) {
559 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
560 return -1;
561 }
562
563 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
564 return -1;
565 }
566
567 *(obuf++) = (uc << 4) | uc2;
568 }
569
570 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200571}
572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573void mbedtls_test_hexify(unsigned char *obuf,
574 const unsigned char *ibuf,
575 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200576{
577 unsigned char l, h;
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200580 h = *ibuf / 16;
581 l = *ibuf % 16;
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200584 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200586 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200590 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200592 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200594
595 ++ibuf;
596 len--;
597 }
598}
599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200601{
602 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 p = mbedtls_calloc(1, actual_len);
606 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200611}
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200614{
615 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200616 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (*olen == 0) {
621 return mbedtls_test_zero_alloc(*olen);
622 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 obuf = mbedtls_calloc(1, *olen);
625 TEST_HELPER_ASSERT(obuf != NULL);
626 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200629}
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
632 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200633{
634 int ret = 0;
635 uint32_t i = 0;
636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 if (a_len != b_len) {
638 return -1;
639 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 for (i = 0; i < a_len; i++) {
642 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200643 ret = -1;
644 break;
645 }
646 }
647 return ret;
648}
Ronald Crona1236142020-07-01 16:01:21 +0200649
Chris Jones96ae73b2021-01-08 17:04:59 +0000650#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100651void mbedtls_test_err_add_check(int high, int low,
652 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000653{
Chris Jones3f613c12021-03-31 09:34:22 +0100654 /* Error codes are always negative (a value of zero is a success) however
655 * their positive opposites can be easier to understand. The following
656 * examples given in comments have been made positive for ease of
657 * understanding. The structure of an error code is such:
658 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100659 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100660 *
661 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100662 * h = high level error code (includes high level module ID (bits 12..14)
663 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100664 * l = low level error code.
665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (high > -0x1000 && high != 0) {
667 /* high < 0001000000000000
668 * No high level module ID bits are set.
669 */
670 mbedtls_test_fail("'high' is not a high-level error code",
671 line, file);
672 } else if (high < -0x7F80) {
673 /* high > 0111111110000000
674 * Error code is greater than the largest allowed high level module ID.
675 */
676 mbedtls_test_fail("'high' error code is greater than 15 bits",
677 line, file);
678 } else if ((high & 0x7F) != 0) {
679 /* high & 0000000001111111
680 * Error code contains low level error code bits.
681 */
682 mbedtls_test_fail("'high' contains a low-level error code",
683 line, file);
684 } else if (low < -0x007F) {
685 /* low > 0000000001111111
686 * Error code contains high or module level error code bits.
687 */
688 mbedtls_test_fail("'low' error code is greater than 7 bits",
689 line, file);
690 } else if (low > 0) {
691 mbedtls_test_fail("'low' error code is greater than zero",
692 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000693 }
694}
695#endif /* MBEDTLS_TEST_HOOKS */