blob: 065d17d3e05be41cc9fee4d13761f64740d14c20 [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
David Horstmann83ece2f2023-12-18 15:30:46 +000016#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C)
David Horstmannb4892572023-12-14 14:17:04 +000017#include <test/psa_memory_poisoning_wrappers.h>
18#endif
Paul Elliott65064262023-11-27 17:29:05 +000019#if defined(MBEDTLS_THREADING_C)
20#include "mbedtls/threading.h"
21#endif
David Horstmannb4892572023-12-14 14:17:04 +000022
Ronald Crona1236142020-07-01 16:01:21 +020023/*----------------------------------------------------------------------------*/
24/* Static global variables */
25
Ronald Cronf40529d2020-06-09 16:27:37 +020026#if defined(MBEDTLS_PLATFORM_C)
27static mbedtls_platform_context platform_ctx;
28#endif
29
Paul Elliotte2f66622024-01-19 20:22:24 +000030static mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000031
Paul Elliott65064262023-11-27 17:29:05 +000032#ifdef MBEDTLS_THREADING_C
33mbedtls_threading_mutex_t mbedtls_test_info_mutex;
34#endif /* MBEDTLS_THREADING_C */
35
Ronald Crona1236142020-07-01 16:01:21 +020036/*----------------------------------------------------------------------------*/
Paul Elliott264e2102024-02-15 12:28:56 +000037/* Mbedtls Test Info accessors
38 *
Paul Elliott9011dae2024-02-24 10:57:22 +000039 * NOTE - there are two types of accessors here: public accessors and internal
40 * accessors. The public accessors have prototypes in helpers.h and lock
41 * mbedtls_test_info_mutex (if mutexes are enabled). The _internal accessors,
42 * which are expected to be used from this module *only*, do not lock the mutex.
43 * These are designed to be called from within public functions which already
44 * hold the mutex. The main reason for this difference is the need to set
45 * multiple test data values atomically (without releasing the mutex) to prevent
46 * race conditions. */
Paul Elliott4580d4d2023-10-27 18:41:02 +010047
48mbedtls_test_result_t mbedtls_test_get_result(void)
49{
Paul Elliott65064262023-11-27 17:29:05 +000050 mbedtls_test_result_t result;
51
52#ifdef MBEDTLS_THREADING_C
53 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
54#endif /* MBEDTLS_THREADING_C */
55
56 result = mbedtls_test_info.result;
57
58#ifdef MBEDTLS_THREADING_C
59 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
60#endif /* MBEDTLS_THREADING_C */
61
62 return result;
Paul Elliott4580d4d2023-10-27 18:41:02 +010063}
64
Paul Elliott264e2102024-02-15 12:28:56 +000065static void mbedtls_test_set_result_internal(mbedtls_test_result_t result, const char *test,
66 int line_no, const char *filename)
Paul Elliott5c498f32023-10-31 16:38:56 +000067{
Paul Elliottfad978b2024-01-30 18:00:26 +000068 /* Internal function only - mbedtls_test_info_mutex should be held prior
69 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +000070
Paul Elliott5c498f32023-10-31 16:38:56 +000071 mbedtls_test_info.result = result;
72 mbedtls_test_info.test = test;
73 mbedtls_test_info.line_no = line_no;
74 mbedtls_test_info.filename = filename;
75}
76
Paul Elliott4580d4d2023-10-27 18:41:02 +010077const char *mbedtls_test_get_test(void)
78{
Paul Elliott65064262023-11-27 17:29:05 +000079 const char *test;
80
81#ifdef MBEDTLS_THREADING_C
82 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
83#endif /* MBEDTLS_THREADING_C */
84
85 test = mbedtls_test_info.test;
86
87#ifdef MBEDTLS_THREADING_C
88 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
89#endif /* MBEDTLS_THREADING_C */
90
91 return test;
Paul Elliott4580d4d2023-10-27 18:41:02 +010092}
93const char *mbedtls_get_test_filename(void)
94{
Paul Elliott65064262023-11-27 17:29:05 +000095 const char *filename;
96
97#ifdef MBEDTLS_THREADING_C
98 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
99#endif /* MBEDTLS_THREADING_C */
100
101 /* It should be ok just to pass back the pointer here, as it is going to
102 * be a pointer into non changing data. */
103 filename = mbedtls_test_info.filename;
104
105#ifdef MBEDTLS_THREADING_C
106 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
107#endif /* MBEDTLS_THREADING_C */
108
109 return filename;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100110}
111
112int mbedtls_test_get_line_no(void)
113{
Paul Elliott65064262023-11-27 17:29:05 +0000114 int line_no;
115
116#ifdef MBEDTLS_THREADING_C
117 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
118#endif /* MBEDTLS_THREADING_C */
119
120 line_no = mbedtls_test_info.line_no;
121
122#ifdef MBEDTLS_THREADING_C
123 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
124#endif /* MBEDTLS_THREADING_C */
125
126 return line_no;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100127}
128
129void mbedtls_test_increment_step(void)
130{
Paul Elliott65064262023-11-27 17:29:05 +0000131#ifdef MBEDTLS_THREADING_C
132 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
133#endif /* MBEDTLS_THREADING_C */
134
Paul Elliott4580d4d2023-10-27 18:41:02 +0100135 ++mbedtls_test_info.step;
Paul Elliott65064262023-11-27 17:29:05 +0000136
137#ifdef MBEDTLS_THREADING_C
138 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
139#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100140}
141
142unsigned long mbedtls_test_get_step(void)
143{
Paul Elliott65064262023-11-27 17:29:05 +0000144 unsigned long step;
145
146#ifdef MBEDTLS_THREADING_C
147 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
148#endif /* MBEDTLS_THREADING_C */
149
150 step = mbedtls_test_info.step;
151
152#ifdef MBEDTLS_THREADING_C
153 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
154#endif /* MBEDTLS_THREADING_C */
155
156 return step;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100157}
158
Paul Elliott264e2102024-02-15 12:28:56 +0000159static void mbedtls_test_reset_step_internal(void)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100160{
Paul Elliottfad978b2024-01-30 18:00:26 +0000161 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000162 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000163
Paul Elliottac61cee2024-02-02 17:53:38 +0000164 mbedtls_test_info.step = (unsigned long) (-1);
165}
166
167void mbedtls_test_set_step(unsigned long step)
168{
169#ifdef MBEDTLS_THREADING_C
170 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
171#endif /* MBEDTLS_THREADING_C */
172
Paul Elliott65064262023-11-27 17:29:05 +0000173 mbedtls_test_info.step = step;
Paul Elliottac61cee2024-02-02 17:53:38 +0000174
175#ifdef MBEDTLS_THREADING_C
176 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
177#endif /* MBEDTLS_THREADING_C */
Paul Elliott65064262023-11-27 17:29:05 +0000178}
179
180void mbedtls_test_get_line1(char *line)
181{
182#ifdef MBEDTLS_THREADING_C
183 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
184#endif /* MBEDTLS_THREADING_C */
185
186 memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH);
187
188#ifdef MBEDTLS_THREADING_C
189 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
190#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100191}
Paul Elliott5c498f32023-10-31 16:38:56 +0000192
Paul Elliott264e2102024-02-15 12:28:56 +0000193static void mbedtls_test_set_line1_internal(const char *line)
Paul Elliott5c498f32023-10-31 16:38:56 +0000194{
Paul Elliottfad978b2024-01-30 18:00:26 +0000195 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000196 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000197
Paul Elliott5c498f32023-10-31 16:38:56 +0000198 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000199 memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000200 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000201 memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000202 }
203}
204
Paul Elliott65064262023-11-27 17:29:05 +0000205void mbedtls_test_get_line2(char *line)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100206{
Paul Elliott65064262023-11-27 17:29:05 +0000207#ifdef MBEDTLS_THREADING_C
208 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
209#endif /* MBEDTLS_THREADING_C */
210
211 memcpy(line, mbedtls_test_info.line2, MBEDTLS_TEST_LINE_LENGTH);
212
213#ifdef MBEDTLS_THREADING_C
214 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
215#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100216}
217
Paul Elliott264e2102024-02-15 12:28:56 +0000218static void mbedtls_test_set_line2_internal(const char *line)
Paul Elliott65064262023-11-27 17:29:05 +0000219{
Paul Elliottfad978b2024-01-30 18:00:26 +0000220 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000221 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000222
Paul Elliott5c498f32023-10-31 16:38:56 +0000223 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000224 memset(mbedtls_test_info.line2, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000225 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000226 memcpy(mbedtls_test_info.line2, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000227 }
228}
229
230
Paul Elliott4580d4d2023-10-27 18:41:02 +0100231#if defined(MBEDTLS_TEST_MUTEX_USAGE)
232const char *mbedtls_test_get_mutex_usage_error(void)
233{
Paul Elliott114ed5e2024-02-13 15:35:14 +0000234 const char *usage_error;
235
236#ifdef MBEDTLS_THREADING_C
237 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
238#endif /* MBEDTLS_THREADING_C */
239
240 usage_error = mbedtls_test_info.mutex_usage_error;
241
242#ifdef MBEDTLS_THREADING_C
243 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
244#endif /* MBEDTLS_THREADING_C */
245
246 return usage_error;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100247}
248
249void mbedtls_test_set_mutex_usage_error(const char *msg)
250{
Paul Elliott65064262023-11-27 17:29:05 +0000251#ifdef MBEDTLS_THREADING_C
252 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
253#endif /* MBEDTLS_THREADING_C */
254
Paul Elliott4580d4d2023-10-27 18:41:02 +0100255 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
256 mbedtls_test_info.mutex_usage_error = msg;
257 }
Paul Elliott65064262023-11-27 17:29:05 +0000258
259#ifdef MBEDTLS_THREADING_C
260 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
261#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100262}
263#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
264
Paul Elliottc7a1e992023-11-03 18:44:57 +0000265#if defined(MBEDTLS_BIGNUM_C)
266
267unsigned mbedtls_test_get_case_uses_negative_0(void)
268{
Paul Elliott65064262023-11-27 17:29:05 +0000269 unsigned test_case_uses_negative_0 = 0;
270#ifdef MBEDTLS_THREADING_C
271 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
272#endif /* MBEDTLS_THREADING_C */
273 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
274
275#ifdef MBEDTLS_THREADING_C
276 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
277#endif /* MBEDTLS_THREADING_C */
278
279 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000280}
281
Paul Elliott264e2102024-02-15 12:28:56 +0000282static void mbedtls_test_set_case_uses_negative_0_internal(unsigned uses)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000283{
Paul Elliottfad978b2024-01-30 18:00:26 +0000284 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000285 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000286
Paul Elliottc7a1e992023-11-03 18:44:57 +0000287 mbedtls_test_info.case_uses_negative_0 = uses;
288}
289
290void mbedtls_test_increment_case_uses_negative_0(void)
291{
Paul Elliott65064262023-11-27 17:29:05 +0000292#ifdef MBEDTLS_THREADING_C
293 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
294#endif /* MBEDTLS_THREADING_C */
295
Paul Elliottc7a1e992023-11-03 18:44:57 +0000296 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000297
298#ifdef MBEDTLS_THREADING_C
299 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
300#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000301}
302
Paul Elliott3d2db892024-01-19 20:42:56 +0000303#endif /* MBEDTLS_BIGNUM_C */
304
305#ifdef MBEDTLS_TEST_MUTEX_USAGE
306mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
307{
308 return &mbedtls_test_info_mutex;
309}
310
311#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Ronald Cronf40529d2020-06-09 16:27:37 +0200312
Ronald Crona1236142020-07-01 16:01:21 +0200313/*----------------------------------------------------------------------------*/
314/* Helper Functions */
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200317{
318 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200319
David Horstmann66684532023-12-15 18:29:54 +0000320#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
David Horstmann4a48bec2024-03-13 15:42:31 +0000321 && !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) \
David Horstmann66684532023-12-15 18:29:54 +0000322 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannb4892572023-12-14 14:17:04 +0000323 mbedtls_poison_test_hooks_setup();
324#endif
325
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200326#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
327 /* Make sure that injected entropy is present. Otherwise
328 * psa_crypto_init() will fail. This is not necessary for test suites
329 * that don't use PSA, but it's harmless (except for leaving a file
330 * behind). */
331 ret = mbedtls_test_inject_entropy_restore();
332 if (ret != 0) {
333 return ret;
334 }
335#endif
336
Ronald Cronf40529d2020-06-09 16:27:37 +0200337#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200339#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200340
Paul Elliott65064262023-11-27 17:29:05 +0000341#ifdef MBEDTLS_THREADING_C
342 mbedtls_mutex_init(&mbedtls_test_info_mutex);
343#endif /* MBEDTLS_THREADING_C */
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200346}
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200349{
David Horstmann66684532023-12-15 18:29:54 +0000350#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
David Horstmann4a48bec2024-03-13 15:42:31 +0000351 && !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) \
David Horstmann66684532023-12-15 18:29:54 +0000352 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannb4892572023-12-14 14:17:04 +0000353 mbedtls_poison_test_hooks_teardown();
354#endif
Paul Elliott65064262023-11-27 17:29:05 +0000355#ifdef MBEDTLS_THREADING_C
356 mbedtls_mutex_free(&mbedtls_test_info_mutex);
357#endif /* MBEDTLS_THREADING_C */
David Horstmannb4892572023-12-14 14:17:04 +0000358
Ronald Cronf40529d2020-06-09 16:27:37 +0200359#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200361#endif /* MBEDTLS_PLATFORM_C */
362}
363
Gilles Peskine881447d2022-12-08 15:24:52 +0100364int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200365{
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200367 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200369 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200371 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 } else {
373 return -1;
374 }
Ronald Crona0c25392020-06-18 10:10:46 +0200375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200377}
378
Paul Elliott97182032024-02-13 13:27:06 +0000379static void mbedtls_test_fail_internal(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000380{
Paul Elliott97182032024-02-13 13:27:06 +0000381 /* Internal function only - mbedtls_test_info_mutex should be held prior
382 * to calling this function. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000383
384 /* Don't use accessor, we already hold mutex. */
385 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
386 /* If we have already recorded the test as having failed then don't
Chris Jones9634bb12021-01-20 15:56:42 +0000387 * overwrite any previous information about the failure. */
Paul Elliott264e2102024-02-15 12:28:56 +0000388 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000389 }
Paul Elliott97182032024-02-13 13:27:06 +0000390}
391
Chris Jones9634bb12021-01-20 15:56:42 +0000392void mbedtls_test_fail(const char *test, int line_no, const char *filename)
393{
Paul Elliott97182032024-02-13 13:27:06 +0000394#ifdef MBEDTLS_THREADING_C
395 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
396#endif /* MBEDTLS_THREADING_C */
397
398 mbedtls_test_fail_internal(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 Jones9634bb12021-01-20 15:56:42 +0000403}
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +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 */
Chris Jones9634bb12021-01-20 15:56:42 +0000410
Paul Elliott264e2102024-02-15 12:28:56 +0000411 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000412
Paul Elliottfad978b2024-01-30 18:00:26 +0000413#ifdef MBEDTLS_THREADING_C
414 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
415#endif /* MBEDTLS_THREADING_C */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000416}
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000419{
Paul Elliottfad978b2024-01-30 18:00:26 +0000420#ifdef MBEDTLS_THREADING_C
421 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
422#endif /* MBEDTLS_THREADING_C */
423
Paul Elliott264e2102024-02-15 12:28:56 +0000424 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
425 mbedtls_test_reset_step_internal();
426 mbedtls_test_set_line1_internal(NULL);
427 mbedtls_test_set_line2_internal(NULL);
Paul Elliott5c498f32023-10-31 16:38:56 +0000428
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100429#if defined(MBEDTLS_BIGNUM_C)
Paul Elliott264e2102024-02-15 12:28:56 +0000430 mbedtls_test_set_case_uses_negative_0_internal(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100431#endif
Paul Elliottfad978b2024-01-30 18:00:26 +0000432
433#ifdef MBEDTLS_THREADING_C
Paul Elliott0b2835d2024-02-01 13:27:04 +0000434 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
Paul Elliottfad978b2024-01-30 18:00:26 +0000435#endif /* MBEDTLS_THREADING_C */
Gilles Peskine89615ee2021-04-29 20:28:54 +0200436}
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438int mbedtls_test_equal(const char *test, int line_no, const char *filename,
439 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200440{
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 TEST_CF_PUBLIC(&value1, sizeof(value1));
442 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (value1 == value2) {
445 return 1;
446 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200447
Paul Elliottfad978b2024-01-30 18:00:26 +0000448#ifdef MBEDTLS_THREADING_C
449 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
450#endif /* MBEDTLS_THREADING_C */
451
452 /* Don't use accessor, as we already hold mutex. */
453 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
454 /* If we've already recorded the test as having failed then don't
Gilles Peskine89615ee2021-04-29 20:28:54 +0200455 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000456
457 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000458 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000459 (void) mbedtls_snprintf(buf, sizeof(buf),
460 "lhs = 0x%016llx = %lld",
461 value1, (long long) value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000462 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000463 (void) mbedtls_snprintf(buf, sizeof(buf),
464 "rhs = 0x%016llx = %lld",
465 value2, (long long) value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000466 mbedtls_test_set_line2_internal(buf);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200467 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000468
469#ifdef MBEDTLS_THREADING_C
470 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
471#endif /* MBEDTLS_THREADING_C */
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000474}
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
477 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200478{
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 TEST_CF_PUBLIC(&value1, sizeof(value1));
480 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if (value1 <= value2) {
483 return 1;
484 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200485
Paul Elliottfad978b2024-01-30 18:00:26 +0000486#ifdef MBEDTLS_THREADING_C
487 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
488#endif /* MBEDTLS_THREADING_C */
489
490 /* Don't use accessor, we already hold mutex. */
491 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
492 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200493 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000494
495 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000496 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000497 (void) mbedtls_snprintf(buf, sizeof(buf),
498 "lhs = 0x%016llx = %llu",
499 value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000500 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000501 (void) mbedtls_snprintf(buf, sizeof(buf),
502 "rhs = 0x%016llx = %llu",
503 value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000504 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200505 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000506
507#ifdef MBEDTLS_THREADING_C
508 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
509#endif /* MBEDTLS_THREADING_C */
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200512}
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
515 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200516{
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 TEST_CF_PUBLIC(&value1, sizeof(value1));
518 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (value1 <= value2) {
521 return 1;
522 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200523
Paul Elliottfad978b2024-01-30 18:00:26 +0000524#ifdef MBEDTLS_THREADING_C
525 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
526#endif /* MBEDTLS_THREADING_C */
527
528 /* Don't use accessor, we already hold mutex. */
Paul Elliottf20728e2024-02-06 12:49:45 +0000529 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
Paul Elliottfad978b2024-01-30 18:00:26 +0000530 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200531 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000532
533 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000534 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000535 (void) mbedtls_snprintf(buf, sizeof(buf),
536 "lhs = 0x%016llx = %lld",
537 (unsigned long long) value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000538 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000539 (void) mbedtls_snprintf(buf, sizeof(buf),
540 "rhs = 0x%016llx = %lld",
541 (unsigned long long) value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000542 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200543 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000544
545#ifdef MBEDTLS_THREADING_C
546 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
547#endif /* MBEDTLS_THREADING_C */
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200550}
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552int mbedtls_test_unhexify(unsigned char *obuf,
553 size_t obufmax,
554 const char *ibuf,
555 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200556{
557 unsigned char uc, uc2;
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200560
561 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if ((*len) & 1) {
563 return -1;
564 }
Ronald Crona0c25392020-06-18 10:10:46 +0200565 *len /= 2;
566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if ((*len) > obufmax) {
568 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200569 }
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 while (*ibuf != 0) {
572 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
573 return -1;
574 }
575
576 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
577 return -1;
578 }
579
580 *(obuf++) = (uc << 4) | uc2;
581 }
582
583 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200584}
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586void mbedtls_test_hexify(unsigned char *obuf,
587 const unsigned char *ibuf,
588 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200589{
590 unsigned char l, h;
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200593 h = *ibuf / 16;
594 l = *ibuf % 16;
595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200597 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200599 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200603 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200605 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200607
608 ++ibuf;
609 len--;
610 }
611}
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200614{
615 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 p = mbedtls_calloc(1, actual_len);
619 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200624}
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200627{
628 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200629 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (*olen == 0) {
634 return mbedtls_test_zero_alloc(*olen);
635 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 obuf = mbedtls_calloc(1, *olen);
638 TEST_HELPER_ASSERT(obuf != NULL);
639 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200642}
643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
645 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200646{
647 int ret = 0;
648 uint32_t i = 0;
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (a_len != b_len) {
651 return -1;
652 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 for (i = 0; i < a_len; i++) {
655 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200656 ret = -1;
657 break;
658 }
659 }
660 return ret;
661}
Ronald Crona1236142020-07-01 16:01:21 +0200662
Chris Jones96ae73b2021-01-08 17:04:59 +0000663#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100664void mbedtls_test_err_add_check(int high, int low,
665 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000666{
Chris Jones3f613c12021-03-31 09:34:22 +0100667 /* Error codes are always negative (a value of zero is a success) however
668 * their positive opposites can be easier to understand. The following
669 * examples given in comments have been made positive for ease of
670 * understanding. The structure of an error code is such:
671 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100672 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100673 *
674 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100675 * h = high level error code (includes high level module ID (bits 12..14)
676 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100677 * l = low level error code.
678 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (high > -0x1000 && high != 0) {
680 /* high < 0001000000000000
681 * No high level module ID bits are set.
682 */
683 mbedtls_test_fail("'high' is not a high-level error code",
684 line, file);
685 } else if (high < -0x7F80) {
686 /* high > 0111111110000000
687 * Error code is greater than the largest allowed high level module ID.
688 */
689 mbedtls_test_fail("'high' error code is greater than 15 bits",
690 line, file);
691 } else if ((high & 0x7F) != 0) {
692 /* high & 0000000001111111
693 * Error code contains low level error code bits.
694 */
695 mbedtls_test_fail("'high' contains a low-level error code",
696 line, file);
697 } else if (low < -0x007F) {
698 /* low > 0000000001111111
699 * Error code contains high or module level error code bits.
700 */
701 mbedtls_test_fail("'low' error code is greater than 7 bits",
702 line, file);
703 } else if (low > 0) {
704 mbedtls_test_fail("'low' error code is greater than zero",
705 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000706 }
707}
708#endif /* MBEDTLS_TEST_HOOKS */