blob: b810253e6c3bfff71434638181d50cfcae2241a5 [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 Elliotte66c8412024-02-13 15:33:26 +000053static void mbedtls_test_set_result(mbedtls_test_result_t result, const char *test,
Paul Elliott5c498f32023-10-31 16:38:56 +000054 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 Elliotte66c8412024-02-13 15:33:26 +0000147static void mbedtls_test_reset_step(void)
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
Paul Elliott9efc6022024-01-31 15:33:23 +0000150 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000151
Paul Elliottac61cee2024-02-02 17:53:38 +0000152 mbedtls_test_info.step = (unsigned long) (-1);
153}
154
155void mbedtls_test_set_step(unsigned long step)
156{
157#ifdef MBEDTLS_THREADING_C
158 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
159#endif /* MBEDTLS_THREADING_C */
160
Paul Elliott65064262023-11-27 17:29:05 +0000161 mbedtls_test_info.step = step;
Paul Elliottac61cee2024-02-02 17:53:38 +0000162
163#ifdef MBEDTLS_THREADING_C
164 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
165#endif /* MBEDTLS_THREADING_C */
Paul Elliott65064262023-11-27 17:29:05 +0000166}
167
168void mbedtls_test_get_line1(char *line)
169{
170#ifdef MBEDTLS_THREADING_C
171 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
172#endif /* MBEDTLS_THREADING_C */
173
174 memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH);
175
176#ifdef MBEDTLS_THREADING_C
177 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
178#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100179}
Paul Elliott5c498f32023-10-31 16:38:56 +0000180
Paul Elliotte66c8412024-02-13 15:33:26 +0000181static void mbedtls_test_set_line1(const char *line)
Paul Elliott5c498f32023-10-31 16:38:56 +0000182{
Paul Elliottfad978b2024-01-30 18:00:26 +0000183 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000184 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000185
Paul Elliott5c498f32023-10-31 16:38:56 +0000186 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000187 memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000188 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000189 memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000190 }
191}
192
Paul Elliott65064262023-11-27 17:29:05 +0000193void mbedtls_test_get_line2(char *line)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100194{
Paul Elliott65064262023-11-27 17:29:05 +0000195#ifdef MBEDTLS_THREADING_C
196 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
197#endif /* MBEDTLS_THREADING_C */
198
199 memcpy(line, mbedtls_test_info.line2, MBEDTLS_TEST_LINE_LENGTH);
200
201#ifdef MBEDTLS_THREADING_C
202 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
203#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100204}
205
Paul Elliotte66c8412024-02-13 15:33:26 +0000206static void mbedtls_test_set_line2(const char *line)
Paul Elliott65064262023-11-27 17:29:05 +0000207{
Paul Elliottfad978b2024-01-30 18:00:26 +0000208 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000209 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000210
Paul Elliott5c498f32023-10-31 16:38:56 +0000211 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000212 memset(mbedtls_test_info.line2, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000213 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000214 memcpy(mbedtls_test_info.line2, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000215 }
216}
217
218
Paul Elliott4580d4d2023-10-27 18:41:02 +0100219#if defined(MBEDTLS_TEST_MUTEX_USAGE)
220const char *mbedtls_test_get_mutex_usage_error(void)
221{
222 return mbedtls_test_info.mutex_usage_error;
223}
224
225void mbedtls_test_set_mutex_usage_error(const char *msg)
226{
Paul Elliott65064262023-11-27 17:29:05 +0000227#ifdef MBEDTLS_THREADING_C
228 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
229#endif /* MBEDTLS_THREADING_C */
230
Paul Elliott4580d4d2023-10-27 18:41:02 +0100231 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
232 mbedtls_test_info.mutex_usage_error = msg;
233 }
Paul Elliott65064262023-11-27 17:29:05 +0000234
235#ifdef MBEDTLS_THREADING_C
236 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
237#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100238}
239#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
240
Paul Elliottc7a1e992023-11-03 18:44:57 +0000241#if defined(MBEDTLS_BIGNUM_C)
242
243unsigned mbedtls_test_get_case_uses_negative_0(void)
244{
Paul Elliott65064262023-11-27 17:29:05 +0000245 unsigned test_case_uses_negative_0 = 0;
246#ifdef MBEDTLS_THREADING_C
247 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
248#endif /* MBEDTLS_THREADING_C */
249 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
250
251#ifdef MBEDTLS_THREADING_C
252 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
253#endif /* MBEDTLS_THREADING_C */
254
255 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000256}
257
Paul Elliotte66c8412024-02-13 15:33:26 +0000258static void mbedtls_test_set_case_uses_negative_0(unsigned uses)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000259{
Paul Elliottfad978b2024-01-30 18:00:26 +0000260 /* Internal function only - mbedtls_test_info_mutex should be held prior
Paul Elliott9efc6022024-01-31 15:33:23 +0000261 * to calling this function. */
Paul Elliott65064262023-11-27 17:29:05 +0000262
Paul Elliottc7a1e992023-11-03 18:44:57 +0000263 mbedtls_test_info.case_uses_negative_0 = uses;
264}
265
266void mbedtls_test_increment_case_uses_negative_0(void)
267{
Paul Elliott65064262023-11-27 17:29:05 +0000268#ifdef MBEDTLS_THREADING_C
269 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
270#endif /* MBEDTLS_THREADING_C */
271
Paul Elliottc7a1e992023-11-03 18:44:57 +0000272 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000273
274#ifdef MBEDTLS_THREADING_C
275 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
276#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000277}
278
Paul Elliott3d2db892024-01-19 20:42:56 +0000279#endif /* MBEDTLS_BIGNUM_C */
280
281#ifdef MBEDTLS_TEST_MUTEX_USAGE
282mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
283{
284 return &mbedtls_test_info_mutex;
285}
286
287#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000288
Paul Elliott4580d4d2023-10-27 18:41:02 +0100289/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200290/* Helper Functions */
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200293{
294 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200295
296#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
297 /* Make sure that injected entropy is present. Otherwise
298 * psa_crypto_init() will fail. This is not necessary for test suites
299 * that don't use PSA, but it's harmless (except for leaving a file
300 * behind). */
301 ret = mbedtls_test_inject_entropy_restore();
302 if (ret != 0) {
303 return ret;
304 }
305#endif
306
Ronald Cronf40529d2020-06-09 16:27:37 +0200307#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200309#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200310
Paul Elliott65064262023-11-27 17:29:05 +0000311#ifdef MBEDTLS_THREADING_C
312 mbedtls_mutex_init(&mbedtls_test_info_mutex);
313#endif /* MBEDTLS_THREADING_C */
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200316}
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200319{
Paul Elliott65064262023-11-27 17:29:05 +0000320#ifdef MBEDTLS_THREADING_C
321 mbedtls_mutex_free(&mbedtls_test_info_mutex);
322#endif /* MBEDTLS_THREADING_C */
323
Ronald Cronf40529d2020-06-09 16:27:37 +0200324#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200326#endif /* MBEDTLS_PLATFORM_C */
327}
328
Gilles Peskine881447d2022-12-08 15:24:52 +0100329int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200330{
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200332 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200334 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200336 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 } else {
338 return -1;
339 }
Ronald Crona0c25392020-06-18 10:10:46 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200342}
343
Paul Elliott97182032024-02-13 13:27:06 +0000344static void mbedtls_test_fail_internal(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000345{
Paul Elliott97182032024-02-13 13:27:06 +0000346 /* Internal function only - mbedtls_test_info_mutex should be held prior
347 * to calling this function. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000348
349 /* Don't use accessor, we already hold mutex. */
350 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
351 /* If we have already recorded the test as having failed then don't
Chris Jones9634bb12021-01-20 15:56:42 +0000352 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000353 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000354 }
Paul Elliott97182032024-02-13 13:27:06 +0000355}
356
357void mbedtls_test_fail(const char *test, int line_no, const char *filename)
358{
359#ifdef MBEDTLS_THREADING_C
360 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
361#endif /* MBEDTLS_THREADING_C */
362
363 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000364
365#ifdef MBEDTLS_THREADING_C
366 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
367#endif /* MBEDTLS_THREADING_C */
Chris Jones9634bb12021-01-20 15:56:42 +0000368}
369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000371{
Paul Elliottfad978b2024-01-30 18:00:26 +0000372#ifdef MBEDTLS_THREADING_C
373 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
374#endif /* MBEDTLS_THREADING_C */
375
Paul Elliott5c498f32023-10-31 16:38:56 +0000376 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000377
378#ifdef MBEDTLS_THREADING_C
379 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
380#endif /* MBEDTLS_THREADING_C */
Chris Jonesa5ab7652021-02-02 16:20:45 +0000381}
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000384{
Paul Elliottfad978b2024-01-30 18:00:26 +0000385#ifdef MBEDTLS_THREADING_C
386 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
387#endif /* MBEDTLS_THREADING_C */
388
Paul Elliott5c498f32023-10-31 16:38:56 +0000389 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
Paul Elliottac61cee2024-02-02 17:53:38 +0000390 mbedtls_test_reset_step();
Paul Elliott5c498f32023-10-31 16:38:56 +0000391 mbedtls_test_set_line1(NULL);
392 mbedtls_test_set_line2(NULL);
393
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100394#if defined(MBEDTLS_BIGNUM_C)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000395 mbedtls_test_set_case_uses_negative_0(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100396#endif
Paul Elliottfad978b2024-01-30 18:00:26 +0000397
398#ifdef MBEDTLS_THREADING_C
Paul Elliott0b2835d2024-02-01 13:27:04 +0000399 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
Paul Elliottfad978b2024-01-30 18:00:26 +0000400#endif /* MBEDTLS_THREADING_C */
Gilles Peskine89615ee2021-04-29 20:28:54 +0200401}
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403int mbedtls_test_equal(const char *test, int line_no, const char *filename,
404 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200405{
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 TEST_CF_PUBLIC(&value1, sizeof(value1));
407 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (value1 == value2) {
410 return 1;
411 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200412
Paul Elliottfad978b2024-01-30 18:00:26 +0000413#ifdef MBEDTLS_THREADING_C
414 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
415#endif /* MBEDTLS_THREADING_C */
416
417 /* Don't use accessor, as we already hold mutex. */
418 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
419 /* If we've already recorded the test as having failed then don't
Gilles Peskine89615ee2021-04-29 20:28:54 +0200420 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000421
422 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000423 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000424 (void) mbedtls_snprintf(buf, sizeof(buf),
425 "lhs = 0x%016llx = %lld",
426 value1, (long long) value1);
427 mbedtls_test_set_line1(buf);
428 (void) mbedtls_snprintf(buf, sizeof(buf),
429 "rhs = 0x%016llx = %lld",
430 value2, (long long) value2);
431 mbedtls_test_set_line2(buf);
Gilles Peskine89615ee2021-04-29 20:28:54 +0200432 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000433
434#ifdef MBEDTLS_THREADING_C
435 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
436#endif /* MBEDTLS_THREADING_C */
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000439}
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
442 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200443{
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 TEST_CF_PUBLIC(&value1, sizeof(value1));
445 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (value1 <= value2) {
448 return 1;
449 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200450
Paul Elliottfad978b2024-01-30 18:00:26 +0000451#ifdef MBEDTLS_THREADING_C
452 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
453#endif /* MBEDTLS_THREADING_C */
454
455 /* Don't use accessor, we already hold mutex. */
456 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
457 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200458 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000459
460 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000461 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000462 (void) mbedtls_snprintf(buf, sizeof(buf),
463 "lhs = 0x%016llx = %llu",
464 value1, value1);
465 mbedtls_test_set_line1(buf);
466 (void) mbedtls_snprintf(buf, sizeof(buf),
467 "rhs = 0x%016llx = %llu",
468 value2, value2);
469 mbedtls_test_set_line2(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200470 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000471
472#ifdef MBEDTLS_THREADING_C
473 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
474#endif /* MBEDTLS_THREADING_C */
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200477}
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
480 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200481{
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 TEST_CF_PUBLIC(&value1, sizeof(value1));
483 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (value1 <= value2) {
486 return 1;
487 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200488
Paul Elliottfad978b2024-01-30 18:00:26 +0000489#ifdef MBEDTLS_THREADING_C
490 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
491#endif /* MBEDTLS_THREADING_C */
492
493 /* Don't use accessor, we already hold mutex. */
Paul Elliottf20728e2024-02-06 12:49:45 +0000494 if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
Paul Elliottfad978b2024-01-30 18:00:26 +0000495 /* If we've already recorded the test as having failed then don't
Gilles Peskined1465422022-04-13 23:59:52 +0200496 * overwrite any previous information about the failure. */
Paul Elliottfad978b2024-01-30 18:00:26 +0000497
498 char buf[MBEDTLS_TEST_LINE_LENGTH];
Paul Elliott97182032024-02-13 13:27:06 +0000499 mbedtls_test_fail_internal(test, line_no, filename);
Paul Elliottfad978b2024-01-30 18:00:26 +0000500 (void) mbedtls_snprintf(buf, sizeof(buf),
501 "lhs = 0x%016llx = %lld",
502 (unsigned long long) value1, value1);
503 mbedtls_test_set_line1(buf);
504 (void) mbedtls_snprintf(buf, sizeof(buf),
505 "rhs = 0x%016llx = %lld",
506 (unsigned long long) value2, value2);
507 mbedtls_test_set_line2(buf);
Gilles Peskined1465422022-04-13 23:59:52 +0200508 }
Paul Elliottfad978b2024-01-30 18:00:26 +0000509
510#ifdef MBEDTLS_THREADING_C
511 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
512#endif /* MBEDTLS_THREADING_C */
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200515}
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517int mbedtls_test_unhexify(unsigned char *obuf,
518 size_t obufmax,
519 const char *ibuf,
520 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200521{
522 unsigned char uc, uc2;
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200525
526 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if ((*len) & 1) {
528 return -1;
529 }
Ronald Crona0c25392020-06-18 10:10:46 +0200530 *len /= 2;
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if ((*len) > obufmax) {
533 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200534 }
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 while (*ibuf != 0) {
537 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
538 return -1;
539 }
540
541 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
542 return -1;
543 }
544
545 *(obuf++) = (uc << 4) | uc2;
546 }
547
548 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200549}
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551void mbedtls_test_hexify(unsigned char *obuf,
552 const unsigned char *ibuf,
553 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200554{
555 unsigned char l, h;
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200558 h = *ibuf / 16;
559 l = *ibuf % 16;
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200562 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200564 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200568 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200570 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200572
573 ++ibuf;
574 len--;
575 }
576}
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200579{
580 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 p = mbedtls_calloc(1, actual_len);
584 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200589}
590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200592{
593 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200594 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (*olen == 0) {
599 return mbedtls_test_zero_alloc(*olen);
600 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 obuf = mbedtls_calloc(1, *olen);
603 TEST_HELPER_ASSERT(obuf != NULL);
604 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200607}
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
610 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200611{
612 int ret = 0;
613 uint32_t i = 0;
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (a_len != b_len) {
616 return -1;
617 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 for (i = 0; i < a_len; i++) {
620 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200621 ret = -1;
622 break;
623 }
624 }
625 return ret;
626}
Ronald Crona1236142020-07-01 16:01:21 +0200627
Chris Jones96ae73b2021-01-08 17:04:59 +0000628#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100629void mbedtls_test_err_add_check(int high, int low,
630 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000631{
Chris Jones3f613c12021-03-31 09:34:22 +0100632 /* Error codes are always negative (a value of zero is a success) however
633 * their positive opposites can be easier to understand. The following
634 * examples given in comments have been made positive for ease of
635 * understanding. The structure of an error code is such:
636 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100637 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100638 *
639 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100640 * h = high level error code (includes high level module ID (bits 12..14)
641 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100642 * l = low level error code.
643 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (high > -0x1000 && high != 0) {
645 /* high < 0001000000000000
646 * No high level module ID bits are set.
647 */
648 mbedtls_test_fail("'high' is not a high-level error code",
649 line, file);
650 } else if (high < -0x7F80) {
651 /* high > 0111111110000000
652 * Error code is greater than the largest allowed high level module ID.
653 */
654 mbedtls_test_fail("'high' error code is greater than 15 bits",
655 line, file);
656 } else if ((high & 0x7F) != 0) {
657 /* high & 0000000001111111
658 * Error code contains low level error code bits.
659 */
660 mbedtls_test_fail("'high' contains a low-level error code",
661 line, file);
662 } else if (low < -0x007F) {
663 /* low > 0000000001111111
664 * Error code contains high or module level error code bits.
665 */
666 mbedtls_test_fail("'low' error code is greater than 7 bits",
667 line, file);
668 } else if (low > 0) {
669 mbedtls_test_fail("'low' error code is greater than zero",
670 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000671 }
672}
673#endif /* MBEDTLS_TEST_HOOKS */