blob: db50296e01dceccd2d66fa1770932c91031bd53c [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
Janos Follath96cfd7a2024-08-22 18:30:06 +010019#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
20#include <test/bignum_codepath_check.h>
21#endif
Paul Elliott65064262023-11-27 17:29:05 +000022#if defined(MBEDTLS_THREADING_C)
23#include "mbedtls/threading.h"
24#endif
David Horstmannb4892572023-12-14 14:17:04 +000025
Ronald Crona1236142020-07-01 16:01:21 +020026/*----------------------------------------------------------------------------*/
27/* Static global variables */
28
Ronald Cronf40529d2020-06-09 16:27:37 +020029#if defined(MBEDTLS_PLATFORM_C)
30static mbedtls_platform_context platform_ctx;
31#endif
32
Paul Elliotte2f66622024-01-19 20:22:24 +000033static mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000034
Paul Elliott65064262023-11-27 17:29:05 +000035#ifdef MBEDTLS_THREADING_C
36mbedtls_threading_mutex_t mbedtls_test_info_mutex;
37#endif /* MBEDTLS_THREADING_C */
38
Ronald Crona1236142020-07-01 16:01:21 +020039/*----------------------------------------------------------------------------*/
Paul Elliott264e2102024-02-15 12:28:56 +000040/* Mbedtls Test Info accessors
41 *
Paul Elliott9011dae2024-02-24 10:57:22 +000042 * NOTE - there are two types of accessors here: public accessors and internal
43 * accessors. The public accessors have prototypes in helpers.h and lock
44 * mbedtls_test_info_mutex (if mutexes are enabled). The _internal accessors,
45 * which are expected to be used from this module *only*, do not lock the mutex.
46 * These are designed to be called from within public functions which already
47 * hold the mutex. The main reason for this difference is the need to set
48 * multiple test data values atomically (without releasing the mutex) to prevent
49 * race conditions. */
Paul Elliott4580d4d2023-10-27 18:41:02 +010050
51mbedtls_test_result_t mbedtls_test_get_result(void)
52{
Paul Elliott65064262023-11-27 17:29:05 +000053 mbedtls_test_result_t result;
54
55#ifdef MBEDTLS_THREADING_C
56 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
57#endif /* MBEDTLS_THREADING_C */
58
59 result = mbedtls_test_info.result;
60
61#ifdef MBEDTLS_THREADING_C
62 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
63#endif /* MBEDTLS_THREADING_C */
64
65 return result;
Paul Elliott4580d4d2023-10-27 18:41:02 +010066}
67
Paul Elliott264e2102024-02-15 12:28:56 +000068static void mbedtls_test_set_result_internal(mbedtls_test_result_t result, const char *test,
69 int line_no, const char *filename)
Paul Elliott5c498f32023-10-31 16:38:56 +000070{
Paul Elliottfad978b2024-01-30 18:00:26 +000071 /* Internal function only - mbedtls_test_info_mutex should be held prior
72 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +000073
Paul Elliott5c498f32023-10-31 16:38:56 +000074 mbedtls_test_info.result = result;
75 mbedtls_test_info.test = test;
76 mbedtls_test_info.line_no = line_no;
77 mbedtls_test_info.filename = filename;
78}
79
Paul Elliott4580d4d2023-10-27 18:41:02 +010080const char *mbedtls_test_get_test(void)
81{
Paul Elliott65064262023-11-27 17:29:05 +000082 const char *test;
83
84#ifdef MBEDTLS_THREADING_C
85 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
86#endif /* MBEDTLS_THREADING_C */
87
88 test = mbedtls_test_info.test;
89
90#ifdef MBEDTLS_THREADING_C
91 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
92#endif /* MBEDTLS_THREADING_C */
93
94 return test;
Paul Elliott4580d4d2023-10-27 18:41:02 +010095}
96const char *mbedtls_get_test_filename(void)
97{
Paul Elliott65064262023-11-27 17:29:05 +000098 const char *filename;
99
100#ifdef MBEDTLS_THREADING_C
101 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
102#endif /* MBEDTLS_THREADING_C */
103
104 /* It should be ok just to pass back the pointer here, as it is going to
105 * be a pointer into non changing data. */
106 filename = mbedtls_test_info.filename;
107
108#ifdef MBEDTLS_THREADING_C
109 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
110#endif /* MBEDTLS_THREADING_C */
111
112 return filename;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100113}
114
115int mbedtls_test_get_line_no(void)
116{
Paul Elliott65064262023-11-27 17:29:05 +0000117 int line_no;
118
119#ifdef MBEDTLS_THREADING_C
120 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
121#endif /* MBEDTLS_THREADING_C */
122
123 line_no = mbedtls_test_info.line_no;
124
125#ifdef MBEDTLS_THREADING_C
126 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
127#endif /* MBEDTLS_THREADING_C */
128
129 return line_no;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100130}
131
132void mbedtls_test_increment_step(void)
133{
Paul Elliott65064262023-11-27 17:29:05 +0000134#ifdef MBEDTLS_THREADING_C
135 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
136#endif /* MBEDTLS_THREADING_C */
137
Paul Elliott4580d4d2023-10-27 18:41:02 +0100138 ++mbedtls_test_info.step;
Paul Elliott65064262023-11-27 17:29:05 +0000139
140#ifdef MBEDTLS_THREADING_C
141 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
142#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100143}
144
145unsigned long mbedtls_test_get_step(void)
146{
Paul Elliott65064262023-11-27 17:29:05 +0000147 unsigned long step;
148
149#ifdef MBEDTLS_THREADING_C
150 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
151#endif /* MBEDTLS_THREADING_C */
152
153 step = mbedtls_test_info.step;
154
155#ifdef MBEDTLS_THREADING_C
156 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
157#endif /* MBEDTLS_THREADING_C */
158
159 return step;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100160}
161
Paul Elliott264e2102024-02-15 12:28:56 +0000162static void mbedtls_test_reset_step_internal(void)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100163{
Paul Elliottfad978b2024-01-30 18:00:26 +0000164 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000165 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000166
Paul Elliottac61cee2024-02-02 17:53:38 +0000167 mbedtls_test_info.step = (unsigned long) (-1);
168}
169
170void mbedtls_test_set_step(unsigned long step)
171{
172#ifdef MBEDTLS_THREADING_C
173 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
174#endif /* MBEDTLS_THREADING_C */
175
Paul Elliott65064262023-11-27 17:29:05 +0000176 mbedtls_test_info.step = step;
Paul Elliottac61cee2024-02-02 17:53:38 +0000177
178#ifdef MBEDTLS_THREADING_C
179 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
180#endif /* MBEDTLS_THREADING_C */
Paul Elliott65064262023-11-27 17:29:05 +0000181}
182
183void mbedtls_test_get_line1(char *line)
184{
185#ifdef MBEDTLS_THREADING_C
186 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
187#endif /* MBEDTLS_THREADING_C */
188
189 memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH);
190
191#ifdef MBEDTLS_THREADING_C
192 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
193#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100194}
Paul Elliott5c498f32023-10-31 16:38:56 +0000195
Paul Elliott264e2102024-02-15 12:28:56 +0000196static void mbedtls_test_set_line1_internal(const char *line)
Paul Elliott5c498f32023-10-31 16:38:56 +0000197{
Paul Elliottfad978b2024-01-30 18:00:26 +0000198 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000199 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000200
Paul Elliott5c498f32023-10-31 16:38:56 +0000201 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000202 memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000203 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000204 memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000205 }
206}
207
Paul Elliott65064262023-11-27 17:29:05 +0000208void mbedtls_test_get_line2(char *line)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100209{
Paul Elliott65064262023-11-27 17:29:05 +0000210#ifdef MBEDTLS_THREADING_C
211 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
212#endif /* MBEDTLS_THREADING_C */
213
214 memcpy(line, mbedtls_test_info.line2, MBEDTLS_TEST_LINE_LENGTH);
215
216#ifdef MBEDTLS_THREADING_C
217 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
218#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100219}
220
Paul Elliott264e2102024-02-15 12:28:56 +0000221static void mbedtls_test_set_line2_internal(const char *line)
Paul Elliott65064262023-11-27 17:29:05 +0000222{
Paul Elliottfad978b2024-01-30 18:00:26 +0000223 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000224 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000225
Paul Elliott5c498f32023-10-31 16:38:56 +0000226 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000227 memset(mbedtls_test_info.line2, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000228 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000229 memcpy(mbedtls_test_info.line2, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000230 }
231}
232
233
Paul Elliott4580d4d2023-10-27 18:41:02 +0100234#if defined(MBEDTLS_TEST_MUTEX_USAGE)
235const char *mbedtls_test_get_mutex_usage_error(void)
236{
Paul Elliott114ed5e2024-02-13 15:35:14 +0000237 const char *usage_error;
238
239#ifdef MBEDTLS_THREADING_C
240 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
241#endif /* MBEDTLS_THREADING_C */
242
243 usage_error = mbedtls_test_info.mutex_usage_error;
244
245#ifdef MBEDTLS_THREADING_C
246 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
247#endif /* MBEDTLS_THREADING_C */
248
249 return usage_error;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100250}
251
252void mbedtls_test_set_mutex_usage_error(const char *msg)
253{
Paul Elliott65064262023-11-27 17:29:05 +0000254#ifdef MBEDTLS_THREADING_C
255 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
256#endif /* MBEDTLS_THREADING_C */
257
Paul Elliott4580d4d2023-10-27 18:41:02 +0100258 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
259 mbedtls_test_info.mutex_usage_error = msg;
260 }
Paul Elliott65064262023-11-27 17:29:05 +0000261
262#ifdef MBEDTLS_THREADING_C
263 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
264#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100265}
266#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
267
Paul Elliottc7a1e992023-11-03 18:44:57 +0000268#if defined(MBEDTLS_BIGNUM_C)
269
270unsigned mbedtls_test_get_case_uses_negative_0(void)
271{
Paul Elliott65064262023-11-27 17:29:05 +0000272 unsigned test_case_uses_negative_0 = 0;
273#ifdef MBEDTLS_THREADING_C
274 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
275#endif /* MBEDTLS_THREADING_C */
276 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
277
278#ifdef MBEDTLS_THREADING_C
279 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
280#endif /* MBEDTLS_THREADING_C */
281
282 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000283}
284
Paul Elliott264e2102024-02-15 12:28:56 +0000285static void mbedtls_test_set_case_uses_negative_0_internal(unsigned uses)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000286{
Paul Elliottfad978b2024-01-30 18:00:26 +0000287 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000288 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000289
Paul Elliottc7a1e992023-11-03 18:44:57 +0000290 mbedtls_test_info.case_uses_negative_0 = uses;
291}
292
293void mbedtls_test_increment_case_uses_negative_0(void)
294{
Paul Elliott65064262023-11-27 17:29:05 +0000295#ifdef MBEDTLS_THREADING_C
296 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
297#endif /* MBEDTLS_THREADING_C */
298
Paul Elliottc7a1e992023-11-03 18:44:57 +0000299 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000300
301#ifdef MBEDTLS_THREADING_C
302 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
303#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000304}
305
Paul Elliott3d2db892024-01-19 20:42:56 +0000306#endif /* MBEDTLS_BIGNUM_C */
307
308#ifdef MBEDTLS_TEST_MUTEX_USAGE
309mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
310{
311 return &mbedtls_test_info_mutex;
312}
313
314#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Ronald Cronf40529d2020-06-09 16:27:37 +0200315
Ronald Crona1236142020-07-01 16:01:21 +0200316/*----------------------------------------------------------------------------*/
317/* Helper Functions */
318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200320{
321 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200322
David Horstmann66684532023-12-15 18:29:54 +0000323#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
David Horstmann4a48bec2024-03-13 15:42:31 +0000324 && !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) \
David Horstmann66684532023-12-15 18:29:54 +0000325 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannb4892572023-12-14 14:17:04 +0000326 mbedtls_poison_test_hooks_setup();
327#endif
328
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200329#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
330 /* Make sure that injected entropy is present. Otherwise
331 * psa_crypto_init() will fail. This is not necessary for test suites
332 * that don't use PSA, but it's harmless (except for leaving a file
333 * behind). */
334 ret = mbedtls_test_inject_entropy_restore();
335 if (ret != 0) {
336 return ret;
337 }
338#endif
339
Ronald Cronf40529d2020-06-09 16:27:37 +0200340#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200342#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200343
Paul Elliott65064262023-11-27 17:29:05 +0000344#ifdef MBEDTLS_THREADING_C
345 mbedtls_mutex_init(&mbedtls_test_info_mutex);
346#endif /* MBEDTLS_THREADING_C */
347
Janos Follath96cfd7a2024-08-22 18:30:06 +0100348
349#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
350 mbedtls_codepath_test_hooks_setup();
351#endif /* MBEDTLS_TEST_HOOKS && !MBEDTLS_THREADING_C */
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200357{
David Horstmann66684532023-12-15 18:29:54 +0000358#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
David Horstmann4a48bec2024-03-13 15:42:31 +0000359 && !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) \
David Horstmann66684532023-12-15 18:29:54 +0000360 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannb4892572023-12-14 14:17:04 +0000361 mbedtls_poison_test_hooks_teardown();
362#endif
Paul Elliott65064262023-11-27 17:29:05 +0000363#ifdef MBEDTLS_THREADING_C
364 mbedtls_mutex_free(&mbedtls_test_info_mutex);
365#endif /* MBEDTLS_THREADING_C */
David Horstmannb4892572023-12-14 14:17:04 +0000366
Ronald Cronf40529d2020-06-09 16:27:37 +0200367#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200369#endif /* MBEDTLS_PLATFORM_C */
Janos Follath96cfd7a2024-08-22 18:30:06 +0100370
371#if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
372 mbedtls_codepath_test_hooks_teardown();
373#endif /* MBEDTLS_TEST_HOOKS && !MBEDTLS_THREADING_C */
Ronald Cronf40529d2020-06-09 16:27:37 +0200374}
375
Gilles Peskine881447d2022-12-08 15:24:52 +0100376int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200377{
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200379 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200381 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200383 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 } else {
385 return -1;
386 }
Ronald Crona0c25392020-06-18 10:10:46 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200389}
390
Paul Elliott97182032024-02-13 13:27:06 +0000391static void mbedtls_test_fail_internal(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000392{
Paul Elliott97182032024-02-13 13:27:06 +0000393 /* Internal function only - mbedtls_test_info_mutex should be held prior
394 * to calling this function. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000395
396 /* Don't use accessor, we already hold mutex. */
397 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
398 /* If we have already recorded the test as having failed then don't
Chris Jones9634bb12021-01-20 15:56:42 +0000399 * overwrite any previous information about the failure. */
Paul Elliott264e2102024-02-15 12:28:56 +0000400 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000401 }
Paul Elliott97182032024-02-13 13:27:06 +0000402}
403
Chris Jones9634bb12021-01-20 15:56:42 +0000404void mbedtls_test_fail(const char *test, int line_no, const char *filename)
405{
Paul Elliott97182032024-02-13 13:27:06 +0000406#ifdef MBEDTLS_THREADING_C
407 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
408#endif /* MBEDTLS_THREADING_C */
409
410 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000411
412#ifdef MBEDTLS_THREADING_C
413 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
414#endif /* MBEDTLS_THREADING_C */
Chris Jones9634bb12021-01-20 15:56:42 +0000415}
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000418{
Paul Elliottfad978b2024-01-30 18:00:26 +0000419#ifdef MBEDTLS_THREADING_C
420 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
421#endif /* MBEDTLS_THREADING_C */
Chris Jones9634bb12021-01-20 15:56:42 +0000422
Paul Elliott264e2102024-02-15 12:28:56 +0000423 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000424
Paul Elliottfad978b2024-01-30 18:00:26 +0000425#ifdef MBEDTLS_THREADING_C
426 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
427#endif /* MBEDTLS_THREADING_C */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000428}
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000431{
Paul Elliottfad978b2024-01-30 18:00:26 +0000432#ifdef MBEDTLS_THREADING_C
433 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
434#endif /* MBEDTLS_THREADING_C */
435
Paul Elliott264e2102024-02-15 12:28:56 +0000436 mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
437 mbedtls_test_reset_step_internal();
438 mbedtls_test_set_line1_internal(NULL);
439 mbedtls_test_set_line2_internal(NULL);
Paul Elliott5c498f32023-10-31 16:38:56 +0000440
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100441#if defined(MBEDTLS_BIGNUM_C)
Paul Elliott264e2102024-02-15 12:28:56 +0000442 mbedtls_test_set_case_uses_negative_0_internal(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100443#endif
Paul Elliottfad978b2024-01-30 18:00:26 +0000444
445#ifdef MBEDTLS_THREADING_C
Paul Elliott0b2835d2024-02-01 13:27:04 +0000446 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
Paul Elliottfad978b2024-01-30 18:00:26 +0000447#endif /* MBEDTLS_THREADING_C */
Gilles Peskine89615ee2021-04-29 20:28:54 +0200448}
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450int mbedtls_test_equal(const char *test, int line_no, const char *filename,
451 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200452{
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 TEST_CF_PUBLIC(&value1, sizeof(value1));
454 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (value1 == value2) {
457 return 1;
458 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200459
Paul Elliottfad978b2024-01-30 18:00:26 +0000460#ifdef MBEDTLS_THREADING_C
461 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
462#endif /* MBEDTLS_THREADING_C */
463
464 /* Don't use accessor, as we already hold mutex. */
465 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
466 /* If we've already recorded the test as having failed then don't
Gilles Peskine89615ee2021-04-29 20:28:54 +0200467 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000468
469 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000470 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000471 (void) mbedtls_snprintf(buf, sizeof(buf),
472 "lhs = 0x%016llx = %lld",
473 value1, (long long) value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000474 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000475 (void) mbedtls_snprintf(buf, sizeof(buf),
476 "rhs = 0x%016llx = %lld",
477 value2, (long long) value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000478 mbedtls_test_set_line2_internal(buf);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200479 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000480
481#ifdef MBEDTLS_THREADING_C
482 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
483#endif /* MBEDTLS_THREADING_C */
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000486}
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
489 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200490{
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 TEST_CF_PUBLIC(&value1, sizeof(value1));
492 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (value1 <= value2) {
495 return 1;
496 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200497
Paul Elliottfad978b2024-01-30 18:00:26 +0000498#ifdef MBEDTLS_THREADING_C
499 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
500#endif /* MBEDTLS_THREADING_C */
501
502 /* Don't use accessor, we already hold mutex. */
503 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
504 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200505 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000506
507 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000508 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000509 (void) mbedtls_snprintf(buf, sizeof(buf),
510 "lhs = 0x%016llx = %llu",
511 value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000512 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000513 (void) mbedtls_snprintf(buf, sizeof(buf),
514 "rhs = 0x%016llx = %llu",
515 value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000516 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200517 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000518
519#ifdef MBEDTLS_THREADING_C
520 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
521#endif /* MBEDTLS_THREADING_C */
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200524}
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
527 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200528{
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 TEST_CF_PUBLIC(&value1, sizeof(value1));
530 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (value1 <= value2) {
533 return 1;
534 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200535
Paul Elliottfad978b2024-01-30 18:00:26 +0000536#ifdef MBEDTLS_THREADING_C
537 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
538#endif /* MBEDTLS_THREADING_C */
539
540 /* Don't use accessor, we already hold mutex. */
Paul Elliottf20728e2024-02-06 12:49:45 +0000541 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
Paul Elliottfad978b2024-01-30 18:00:26 +0000542 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200543 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000544
545 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000546 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000547 (void) mbedtls_snprintf(buf, sizeof(buf),
548 "lhs = 0x%016llx = %lld",
549 (unsigned long long) value1, value1);
Paul Elliott264e2102024-02-15 12:28:56 +0000550 mbedtls_test_set_line1_internal(buf);
Paul Elliottfad978b2024-01-30 18:00:26 +0000551 (void) mbedtls_snprintf(buf, sizeof(buf),
552 "rhs = 0x%016llx = %lld",
553 (unsigned long long) value2, value2);
Paul Elliott264e2102024-02-15 12:28:56 +0000554 mbedtls_test_set_line2_internal(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200555 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000556
557#ifdef MBEDTLS_THREADING_C
558 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
559#endif /* MBEDTLS_THREADING_C */
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200562}
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564int mbedtls_test_unhexify(unsigned char *obuf,
565 size_t obufmax,
566 const char *ibuf,
567 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200568{
569 unsigned char uc, uc2;
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200572
573 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if ((*len) & 1) {
575 return -1;
576 }
Ronald Crona0c25392020-06-18 10:10:46 +0200577 *len /= 2;
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if ((*len) > obufmax) {
580 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200581 }
582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 while (*ibuf != 0) {
584 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
585 return -1;
586 }
587
588 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
589 return -1;
590 }
591
592 *(obuf++) = (uc << 4) | uc2;
593 }
594
595 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200596}
597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598void mbedtls_test_hexify(unsigned char *obuf,
599 const unsigned char *ibuf,
600 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200601{
602 unsigned char l, h;
603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200605 h = *ibuf / 16;
606 l = *ibuf % 16;
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200609 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200611 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200615 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200617 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200619
620 ++ibuf;
621 len--;
622 }
623}
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200626{
627 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 p = mbedtls_calloc(1, actual_len);
631 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200636}
637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200639{
640 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200641 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (*olen == 0) {
646 return mbedtls_test_zero_alloc(*olen);
647 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 obuf = mbedtls_calloc(1, *olen);
650 TEST_HELPER_ASSERT(obuf != NULL);
651 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200654}
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
657 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200658{
659 int ret = 0;
660 uint32_t i = 0;
661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (a_len != b_len) {
663 return -1;
664 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 for (i = 0; i < a_len; i++) {
667 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200668 ret = -1;
669 break;
670 }
671 }
672 return ret;
673}
Ronald Crona1236142020-07-01 16:01:21 +0200674
Chris Jones96ae73b2021-01-08 17:04:59 +0000675#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100676void mbedtls_test_err_add_check(int high, int low,
677 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000678{
Chris Jones3f613c12021-03-31 09:34:22 +0100679 /* Error codes are always negative (a value of zero is a success) however
680 * their positive opposites can be easier to understand. The following
681 * examples given in comments have been made positive for ease of
682 * understanding. The structure of an error code is such:
683 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100684 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100685 *
686 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100687 * h = high level error code (includes high level module ID (bits 12..14)
688 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100689 * l = low level error code.
690 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (high > -0x1000 && high != 0) {
692 /* high < 0001000000000000
693 * No high level module ID bits are set.
694 */
695 mbedtls_test_fail("'high' is not a high-level error code",
696 line, file);
697 } else if (high < -0x7F80) {
698 /* high > 0111111110000000
699 * Error code is greater than the largest allowed high level module ID.
700 */
701 mbedtls_test_fail("'high' error code is greater than 15 bits",
702 line, file);
703 } else if ((high & 0x7F) != 0) {
704 /* high & 0000000001111111
705 * Error code contains low level error code bits.
706 */
707 mbedtls_test_fail("'high' contains a low-level error code",
708 line, file);
709 } else if (low < -0x007F) {
710 /* low > 0000000001111111
711 * Error code contains high or module level error code bits.
712 */
713 mbedtls_test_fail("'low' error code is greater than 7 bits",
714 line, file);
715 } else if (low > 0) {
716 mbedtls_test_fail("'low' error code is greater than zero",
717 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000718 }
719}
720#endif /* MBEDTLS_TEST_HOOKS */