blob: 85345d8cfd1b3c164d043fc0420b86b82095511d [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 Elliott4580d4d2023-10-27 18:41:02 +010034/* Mbedtls Test Info accessors */
35
36mbedtls_test_result_t mbedtls_test_get_result(void)
37{
Paul Elliott65064262023-11-27 17:29:05 +000038 mbedtls_test_result_t result;
39
40#ifdef MBEDTLS_THREADING_C
41 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
42#endif /* MBEDTLS_THREADING_C */
43
44 result = mbedtls_test_info.result;
45
46#ifdef MBEDTLS_THREADING_C
47 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
48#endif /* MBEDTLS_THREADING_C */
49
50 return result;
Paul Elliott4580d4d2023-10-27 18:41:02 +010051}
52
Paul Elliott5c498f32023-10-31 16:38:56 +000053void mbedtls_test_set_result(mbedtls_test_result_t result, const char *test,
54 int line_no, const char *filename)
55{
Paul Elliottfad978b2024-01-30 18:00:26 +000056 /* Internal function only - mbedtls_test_info_mutex should be held prior
57 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +000058
Paul Elliott5c498f32023-10-31 16:38:56 +000059 mbedtls_test_info.result = result;
60 mbedtls_test_info.test = test;
61 mbedtls_test_info.line_no = line_no;
62 mbedtls_test_info.filename = filename;
63}
64
Paul Elliott4580d4d2023-10-27 18:41:02 +010065const char *mbedtls_test_get_test(void)
66{
Paul Elliott65064262023-11-27 17:29:05 +000067 const char *test;
68
69#ifdef MBEDTLS_THREADING_C
70 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
71#endif /* MBEDTLS_THREADING_C */
72
73 test = mbedtls_test_info.test;
74
75#ifdef MBEDTLS_THREADING_C
76 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
77#endif /* MBEDTLS_THREADING_C */
78
79 return test;
Paul Elliott4580d4d2023-10-27 18:41:02 +010080}
81const char *mbedtls_get_test_filename(void)
82{
Paul Elliott65064262023-11-27 17:29:05 +000083 const char *filename;
84
85#ifdef MBEDTLS_THREADING_C
86 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
87#endif /* MBEDTLS_THREADING_C */
88
89 /* It should be ok just to pass back the pointer here, as it is going to
90 * be a pointer into non changing data. */
91 filename = mbedtls_test_info.filename;
92
93#ifdef MBEDTLS_THREADING_C
94 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
95#endif /* MBEDTLS_THREADING_C */
96
97 return filename;
Paul Elliott4580d4d2023-10-27 18:41:02 +010098}
99
100int mbedtls_test_get_line_no(void)
101{
Paul Elliott65064262023-11-27 17:29:05 +0000102 int line_no;
103
104#ifdef MBEDTLS_THREADING_C
105 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
106#endif /* MBEDTLS_THREADING_C */
107
108 line_no = mbedtls_test_info.line_no;
109
110#ifdef MBEDTLS_THREADING_C
111 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
112#endif /* MBEDTLS_THREADING_C */
113
114 return line_no;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100115}
116
117void mbedtls_test_increment_step(void)
118{
Paul Elliott65064262023-11-27 17:29:05 +0000119#ifdef MBEDTLS_THREADING_C
120 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
121#endif /* MBEDTLS_THREADING_C */
122
Paul Elliott4580d4d2023-10-27 18:41:02 +0100123 ++mbedtls_test_info.step;
Paul Elliott65064262023-11-27 17:29:05 +0000124
125#ifdef MBEDTLS_THREADING_C
126 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
127#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100128}
129
130unsigned long mbedtls_test_get_step(void)
131{
Paul Elliott65064262023-11-27 17:29:05 +0000132 unsigned long step;
133
134#ifdef MBEDTLS_THREADING_C
135 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
136#endif /* MBEDTLS_THREADING_C */
137
138 step = mbedtls_test_info.step;
139
140#ifdef MBEDTLS_THREADING_C
141 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
142#endif /* MBEDTLS_THREADING_C */
143
144 return step;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100145}
146
Paul Elliott65064262023-11-27 17:29:05 +0000147void mbedtls_test_set_step(unsigned long step)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100148{
Paul Elliottfad978b2024-01-30 18:00:26 +0000149 /* Internal function only - mbedtls_test_info_mutex should be held prior
150 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000151
152 mbedtls_test_info.step = step;
Paul Elliott65064262023-11-27 17:29:05 +0000153}
154
155void mbedtls_test_get_line1(char *line)
156{
157#ifdef MBEDTLS_THREADING_C
158 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
159#endif /* MBEDTLS_THREADING_C */
160
161 memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH);
162
163#ifdef MBEDTLS_THREADING_C
164 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
165#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100166}
Paul Elliott5c498f32023-10-31 16:38:56 +0000167
168void mbedtls_test_set_line1(const char *line)
169{
Paul Elliottfad978b2024-01-30 18:00:26 +0000170 /* Internal function only - mbedtls_test_info_mutex should be held prior
171 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000172
Paul Elliott5c498f32023-10-31 16:38:56 +0000173 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000174 memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000175 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000176 memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000177 }
178}
179
Paul Elliott65064262023-11-27 17:29:05 +0000180void mbedtls_test_get_line2(char *line)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100181{
Paul Elliott65064262023-11-27 17:29:05 +0000182#ifdef MBEDTLS_THREADING_C
183 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
184#endif /* MBEDTLS_THREADING_C */
185
186 memcpy(line, mbedtls_test_info.line2, 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}
192
Paul Elliott65064262023-11-27 17:29:05 +0000193void mbedtls_test_set_line2(const char *line)
194{
Paul Elliottfad978b2024-01-30 18:00:26 +0000195 /* Internal function only - mbedtls_test_info_mutex should be held prior
196 * 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.line2, 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.line2, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000202 }
203}
204
205
Paul Elliott4580d4d2023-10-27 18:41:02 +0100206#if defined(MBEDTLS_TEST_MUTEX_USAGE)
207const char *mbedtls_test_get_mutex_usage_error(void)
208{
209 return mbedtls_test_info.mutex_usage_error;
210}
211
212void mbedtls_test_set_mutex_usage_error(const char *msg)
213{
Paul Elliott65064262023-11-27 17:29:05 +0000214#ifdef MBEDTLS_THREADING_C
215 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
216#endif /* MBEDTLS_THREADING_C */
217
Paul Elliott4580d4d2023-10-27 18:41:02 +0100218 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
219 mbedtls_test_info.mutex_usage_error = msg;
220 }
Paul Elliott65064262023-11-27 17:29:05 +0000221
222#ifdef MBEDTLS_THREADING_C
223 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
224#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100225}
226#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
227
Paul Elliottc7a1e992023-11-03 18:44:57 +0000228#if defined(MBEDTLS_BIGNUM_C)
229
230unsigned mbedtls_test_get_case_uses_negative_0(void)
231{
Paul Elliott65064262023-11-27 17:29:05 +0000232 unsigned test_case_uses_negative_0 = 0;
233#ifdef MBEDTLS_THREADING_C
234 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
235#endif /* MBEDTLS_THREADING_C */
236 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
237
238#ifdef MBEDTLS_THREADING_C
239 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
240#endif /* MBEDTLS_THREADING_C */
241
242 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000243}
244
245void mbedtls_test_set_case_uses_negative_0(unsigned uses)
246{
Paul Elliottfad978b2024-01-30 18:00:26 +0000247 /* Internal function only - mbedtls_test_info_mutex should be held prior
248 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000249
Paul Elliottc7a1e992023-11-03 18:44:57 +0000250 mbedtls_test_info.case_uses_negative_0 = uses;
251}
252
253void mbedtls_test_increment_case_uses_negative_0(void)
254{
Paul Elliott65064262023-11-27 17:29:05 +0000255#ifdef MBEDTLS_THREADING_C
256 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
257#endif /* MBEDTLS_THREADING_C */
258
Paul Elliottc7a1e992023-11-03 18:44:57 +0000259 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000260
261#ifdef MBEDTLS_THREADING_C
262 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
263#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000264}
265
Paul Elliott3d2db892024-01-19 20:42:56 +0000266#endif /* MBEDTLS_BIGNUM_C */
267
268#ifdef MBEDTLS_TEST_MUTEX_USAGE
269mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
270{
271 return &mbedtls_test_info_mutex;
272}
273
274#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000275
Paul Elliott4580d4d2023-10-27 18:41:02 +0100276/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200277/* Helper Functions */
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200280{
281 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200282
283#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
284 /* Make sure that injected entropy is present. Otherwise
285 * psa_crypto_init() will fail. This is not necessary for test suites
286 * that don't use PSA, but it's harmless (except for leaving a file
287 * behind). */
288 ret = mbedtls_test_inject_entropy_restore();
289 if (ret != 0) {
290 return ret;
291 }
292#endif
293
Ronald Cronf40529d2020-06-09 16:27:37 +0200294#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200296#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200297
Paul Elliott65064262023-11-27 17:29:05 +0000298#ifdef MBEDTLS_THREADING_C
299 mbedtls_mutex_init(&mbedtls_test_info_mutex);
300#endif /* MBEDTLS_THREADING_C */
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200303}
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200306{
Paul Elliott65064262023-11-27 17:29:05 +0000307#ifdef MBEDTLS_THREADING_C
308 mbedtls_mutex_free(&mbedtls_test_info_mutex);
309#endif /* MBEDTLS_THREADING_C */
310
Ronald Cronf40529d2020-06-09 16:27:37 +0200311#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200313#endif /* MBEDTLS_PLATFORM_C */
314}
315
Gilles Peskine881447d2022-12-08 15:24:52 +0100316int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200317{
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200319 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200321 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200323 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 } else {
325 return -1;
326 }
Ronald Crona0c25392020-06-18 10:10:46 +0200327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200329}
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000332{
Paul Elliottfad978b2024-01-30 18:00:26 +0000333#ifdef MBEDTLS_THREADING_C
334 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
335#endif /* MBEDTLS_THREADING_C */
336
337 /* Don't use accessor, we already hold mutex. */
338 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
339 /* If we have already recorded the test as having failed then don't
Chris Jones9634bb12021-01-20 15:56:42 +0000340 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000341 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000342 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000343
344#ifdef MBEDTLS_THREADING_C
345 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
346#endif /* MBEDTLS_THREADING_C */
Chris Jones9634bb12021-01-20 15:56:42 +0000347}
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000350{
Paul Elliottfad978b2024-01-30 18:00:26 +0000351#ifdef MBEDTLS_THREADING_C
352 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
353#endif /* MBEDTLS_THREADING_C */
354
Paul Elliott5c498f32023-10-31 16:38:56 +0000355 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000356
357#ifdef MBEDTLS_THREADING_C
358 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
359#endif /* MBEDTLS_THREADING_C */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000360}
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000363{
Paul Elliottfad978b2024-01-30 18:00:26 +0000364#ifdef MBEDTLS_THREADING_C
365 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
366#endif /* MBEDTLS_THREADING_C */
367
Paul Elliott5c498f32023-10-31 16:38:56 +0000368 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
369 mbedtls_test_set_step((unsigned long) (-1));
370 mbedtls_test_set_line1(NULL);
371 mbedtls_test_set_line2(NULL);
372
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100373#if defined(MBEDTLS_BIGNUM_C)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000374 mbedtls_test_set_case_uses_negative_0(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100375#endif
Paul Elliottfad978b2024-01-30 18:00:26 +0000376
377#ifdef MBEDTLS_THREADING_C
378 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
379#endif /* MBEDTLS_THREADING_C */
Gilles Peskine89615ee2021-04-29 20:28:54 +0200380}
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382int mbedtls_test_equal(const char *test, int line_no, const char *filename,
383 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200384{
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 TEST_CF_PUBLIC(&value1, sizeof(value1));
386 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (value1 == value2) {
389 return 1;
390 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200391
Paul Elliottfad978b2024-01-30 18:00:26 +0000392#ifdef MBEDTLS_THREADING_C
393 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
394#endif /* MBEDTLS_THREADING_C */
395
396 /* Don't use accessor, as we already hold mutex. */
397 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
398 /* If we've already recorded the test as having failed then don't
Gilles Peskine89615ee2021-04-29 20:28:54 +0200399 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000400
401 char buf[MBEDTLS_TEST_LINE_LENGTH];
402 mbedtls_test_fail(test, line_no, filename);
403 (void) mbedtls_snprintf(buf, sizeof(buf),
404 "lhs = 0x%016llx = %lld",
405 value1, (long long) value1);
406 mbedtls_test_set_line1(buf);
407 (void) mbedtls_snprintf(buf, sizeof(buf),
408 "rhs = 0x%016llx = %lld",
409 value2, (long long) value2);
410 mbedtls_test_set_line2(buf);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200411 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000412
413#ifdef MBEDTLS_THREADING_C
414 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
415#endif /* MBEDTLS_THREADING_C */
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000418}
419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
421 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200422{
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 TEST_CF_PUBLIC(&value1, sizeof(value1));
424 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (value1 <= value2) {
427 return 1;
428 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200429
Paul Elliottfad978b2024-01-30 18:00:26 +0000430#ifdef MBEDTLS_THREADING_C
431 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
432#endif /* MBEDTLS_THREADING_C */
433
434 /* Don't use accessor, we already hold mutex. */
435 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
436 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200437 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000438
439 char buf[MBEDTLS_TEST_LINE_LENGTH];
440 mbedtls_test_fail(test, line_no, filename);
441 (void) mbedtls_snprintf(buf, sizeof(buf),
442 "lhs = 0x%016llx = %llu",
443 value1, value1);
444 mbedtls_test_set_line1(buf);
445 (void) mbedtls_snprintf(buf, sizeof(buf),
446 "rhs = 0x%016llx = %llu",
447 value2, value2);
448 mbedtls_test_set_line2(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200449 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000450
451#ifdef MBEDTLS_THREADING_C
452 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
453#endif /* MBEDTLS_THREADING_C */
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200456}
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
459 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200460{
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 TEST_CF_PUBLIC(&value1, sizeof(value1));
462 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (value1 <= value2) {
465 return 1;
466 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200467
Paul Elliottfad978b2024-01-30 18:00:26 +0000468#ifdef MBEDTLS_THREADING_C
469 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
470#endif /* MBEDTLS_THREADING_C */
471
472 /* Don't use accessor, we already hold mutex. */
473 if (mbedtls_test_get_result() != MBEDTLS_TEST_RESULT_FAILED) {
474 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200475 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000476
477 char buf[MBEDTLS_TEST_LINE_LENGTH];
478 mbedtls_test_fail(test, line_no, filename);
479 (void) mbedtls_snprintf(buf, sizeof(buf),
480 "lhs = 0x%016llx = %lld",
481 (unsigned long long) value1, value1);
482 mbedtls_test_set_line1(buf);
483 (void) mbedtls_snprintf(buf, sizeof(buf),
484 "rhs = 0x%016llx = %lld",
485 (unsigned long long) value2, value2);
486 mbedtls_test_set_line2(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200487 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000488
489#ifdef MBEDTLS_THREADING_C
490 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
491#endif /* MBEDTLS_THREADING_C */
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200494}
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496int mbedtls_test_unhexify(unsigned char *obuf,
497 size_t obufmax,
498 const char *ibuf,
499 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200500{
501 unsigned char uc, uc2;
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200504
505 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if ((*len) & 1) {
507 return -1;
508 }
Ronald Crona0c25392020-06-18 10:10:46 +0200509 *len /= 2;
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if ((*len) > obufmax) {
512 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200513 }
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 while (*ibuf != 0) {
516 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
517 return -1;
518 }
519
520 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
521 return -1;
522 }
523
524 *(obuf++) = (uc << 4) | uc2;
525 }
526
527 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200528}
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530void mbedtls_test_hexify(unsigned char *obuf,
531 const unsigned char *ibuf,
532 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200533{
534 unsigned char l, h;
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200537 h = *ibuf / 16;
538 l = *ibuf % 16;
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200541 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200543 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200547 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200549 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200551
552 ++ibuf;
553 len--;
554 }
555}
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200558{
559 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 p = mbedtls_calloc(1, actual_len);
563 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200568}
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200571{
572 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200573 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (*olen == 0) {
578 return mbedtls_test_zero_alloc(*olen);
579 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 obuf = mbedtls_calloc(1, *olen);
582 TEST_HELPER_ASSERT(obuf != NULL);
583 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200586}
587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
589 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200590{
591 int ret = 0;
592 uint32_t i = 0;
593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (a_len != b_len) {
595 return -1;
596 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 for (i = 0; i < a_len; i++) {
599 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200600 ret = -1;
601 break;
602 }
603 }
604 return ret;
605}
Ronald Crona1236142020-07-01 16:01:21 +0200606
Chris Jones96ae73b2021-01-08 17:04:59 +0000607#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100608void mbedtls_test_err_add_check(int high, int low,
609 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000610{
Chris Jones3f613c12021-03-31 09:34:22 +0100611 /* Error codes are always negative (a value of zero is a success) however
612 * their positive opposites can be easier to understand. The following
613 * examples given in comments have been made positive for ease of
614 * understanding. The structure of an error code is such:
615 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100616 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100617 *
618 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100619 * h = high level error code (includes high level module ID (bits 12..14)
620 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100621 * l = low level error code.
622 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 if (high > -0x1000 && high != 0) {
624 /* high < 0001000000000000
625 * No high level module ID bits are set.
626 */
627 mbedtls_test_fail("'high' is not a high-level error code",
628 line, file);
629 } else if (high < -0x7F80) {
630 /* high > 0111111110000000
631 * Error code is greater than the largest allowed high level module ID.
632 */
633 mbedtls_test_fail("'high' error code is greater than 15 bits",
634 line, file);
635 } else if ((high & 0x7F) != 0) {
636 /* high & 0000000001111111
637 * Error code contains low level error code bits.
638 */
639 mbedtls_test_fail("'high' contains a low-level error code",
640 line, file);
641 } else if (low < -0x007F) {
642 /* low > 0000000001111111
643 * Error code contains high or module level error code bits.
644 */
645 mbedtls_test_fail("'low' error code is greater than 7 bits",
646 line, file);
647 } else if (low > 0) {
648 mbedtls_test_fail("'low' error code is greater than zero",
649 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000650 }
651}
652#endif /* MBEDTLS_TEST_HOOKS */