blob: 1bad819acfc2d2878d9d855b287c61560a772fdb [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
Chris Jonese60e2ae2021-01-20 17:51:47 +000027mbedtls_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 Elliott65064262023-11-27 17:29:05 +000056#ifdef MBEDTLS_THREADING_C
57 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
58#endif /* MBEDTLS_THREADING_C */
59
Paul Elliott5c498f32023-10-31 16:38:56 +000060 mbedtls_test_info.result = result;
61 mbedtls_test_info.test = test;
62 mbedtls_test_info.line_no = line_no;
63 mbedtls_test_info.filename = filename;
Paul Elliott65064262023-11-27 17:29:05 +000064
65#ifdef MBEDTLS_THREADING_C
66 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
67#endif /* MBEDTLS_THREADING_C */
Paul Elliott5c498f32023-10-31 16:38:56 +000068}
69
Paul Elliott4580d4d2023-10-27 18:41:02 +010070const char *mbedtls_test_get_test(void)
71{
Paul Elliott65064262023-11-27 17:29:05 +000072 const char *test;
73
74#ifdef MBEDTLS_THREADING_C
75 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
76#endif /* MBEDTLS_THREADING_C */
77
78 test = mbedtls_test_info.test;
79
80#ifdef MBEDTLS_THREADING_C
81 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
82#endif /* MBEDTLS_THREADING_C */
83
84 return test;
Paul Elliott4580d4d2023-10-27 18:41:02 +010085}
86const char *mbedtls_get_test_filename(void)
87{
Paul Elliott65064262023-11-27 17:29:05 +000088 const char *filename;
89
90#ifdef MBEDTLS_THREADING_C
91 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
92#endif /* MBEDTLS_THREADING_C */
93
94 /* It should be ok just to pass back the pointer here, as it is going to
95 * be a pointer into non changing data. */
96 filename = mbedtls_test_info.filename;
97
98#ifdef MBEDTLS_THREADING_C
99 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
100#endif /* MBEDTLS_THREADING_C */
101
102 return filename;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100103}
104
105int mbedtls_test_get_line_no(void)
106{
Paul Elliott65064262023-11-27 17:29:05 +0000107 int line_no;
108
109#ifdef MBEDTLS_THREADING_C
110 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
111#endif /* MBEDTLS_THREADING_C */
112
113 line_no = mbedtls_test_info.line_no;
114
115#ifdef MBEDTLS_THREADING_C
116 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
117#endif /* MBEDTLS_THREADING_C */
118
119 return line_no;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100120}
121
122void mbedtls_test_increment_step(void)
123{
Paul Elliott65064262023-11-27 17:29:05 +0000124#ifdef MBEDTLS_THREADING_C
125 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
126#endif /* MBEDTLS_THREADING_C */
127
Paul Elliott4580d4d2023-10-27 18:41:02 +0100128 ++mbedtls_test_info.step;
Paul Elliott65064262023-11-27 17:29:05 +0000129
130#ifdef MBEDTLS_THREADING_C
131 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
132#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100133}
134
135unsigned long mbedtls_test_get_step(void)
136{
Paul Elliott65064262023-11-27 17:29:05 +0000137 unsigned long step;
138
139#ifdef MBEDTLS_THREADING_C
140 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
141#endif /* MBEDTLS_THREADING_C */
142
143 step = mbedtls_test_info.step;
144
145#ifdef MBEDTLS_THREADING_C
146 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
147#endif /* MBEDTLS_THREADING_C */
148
149 return step;
Paul Elliott4580d4d2023-10-27 18:41:02 +0100150}
151
Paul Elliott65064262023-11-27 17:29:05 +0000152void mbedtls_test_set_step(unsigned long step)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100153{
Paul Elliott65064262023-11-27 17:29:05 +0000154#ifdef MBEDTLS_THREADING_C
155 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
156#endif /* MBEDTLS_THREADING_C */
157
158 mbedtls_test_info.step = step;
159
160#ifdef MBEDTLS_THREADING_C
161 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
162#endif /* MBEDTLS_THREADING_C */
163}
164
165void mbedtls_test_get_line1(char *line)
166{
167#ifdef MBEDTLS_THREADING_C
168 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
169#endif /* MBEDTLS_THREADING_C */
170
171 memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH);
172
173#ifdef MBEDTLS_THREADING_C
174 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
175#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100176}
Paul Elliott5c498f32023-10-31 16:38:56 +0000177
178void mbedtls_test_set_line1(const char *line)
179{
Paul Elliott65064262023-11-27 17:29:05 +0000180#ifdef MBEDTLS_THREADING_C
181 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
182#endif /* MBEDTLS_THREADING_C */
183
Paul Elliott5c498f32023-10-31 16:38:56 +0000184 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000185 memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000186 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000187 memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000188 }
Paul Elliott65064262023-11-27 17:29:05 +0000189
190#ifdef MBEDTLS_THREADING_C
191 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
192#endif /* MBEDTLS_THREADING_C */
Paul Elliott5c498f32023-10-31 16:38:56 +0000193}
194
Paul Elliott65064262023-11-27 17:29:05 +0000195void mbedtls_test_get_line2(char *line)
Paul Elliott4580d4d2023-10-27 18:41:02 +0100196{
Paul Elliott65064262023-11-27 17:29:05 +0000197#ifdef MBEDTLS_THREADING_C
198 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
199#endif /* MBEDTLS_THREADING_C */
200
201 memcpy(line, mbedtls_test_info.line2, MBEDTLS_TEST_LINE_LENGTH);
202
203#ifdef MBEDTLS_THREADING_C
204 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
205#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100206}
207
Paul Elliott65064262023-11-27 17:29:05 +0000208void mbedtls_test_set_line2(const char *line)
209{
210#ifdef MBEDTLS_THREADING_C
211 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
212#endif /* MBEDTLS_THREADING_C */
213
Paul Elliott5c498f32023-10-31 16:38:56 +0000214 if (line == NULL) {
Paul Elliott65064262023-11-27 17:29:05 +0000215 memset(mbedtls_test_info.line2, 0, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000216 } else {
Paul Elliott65064262023-11-27 17:29:05 +0000217 memcpy(mbedtls_test_info.line2, line, MBEDTLS_TEST_LINE_LENGTH);
Paul Elliott5c498f32023-10-31 16:38:56 +0000218 }
Paul Elliott65064262023-11-27 17:29:05 +0000219
220#ifdef MBEDTLS_THREADING_C
221 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
222#endif /* MBEDTLS_THREADING_C */
Paul Elliott5c498f32023-10-31 16:38:56 +0000223}
224
225
Paul Elliott4580d4d2023-10-27 18:41:02 +0100226#if defined(MBEDTLS_TEST_MUTEX_USAGE)
227const char *mbedtls_test_get_mutex_usage_error(void)
228{
229 return mbedtls_test_info.mutex_usage_error;
230}
231
232void mbedtls_test_set_mutex_usage_error(const char *msg)
233{
Paul Elliott65064262023-11-27 17:29:05 +0000234#ifdef MBEDTLS_THREADING_C
235 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
236#endif /* MBEDTLS_THREADING_C */
237
Paul Elliott4580d4d2023-10-27 18:41:02 +0100238 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
239 mbedtls_test_info.mutex_usage_error = msg;
240 }
Paul Elliott65064262023-11-27 17:29:05 +0000241
242#ifdef MBEDTLS_THREADING_C
243 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
244#endif /* MBEDTLS_THREADING_C */
Paul Elliott4580d4d2023-10-27 18:41:02 +0100245}
246#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
247
Paul Elliottc7a1e992023-11-03 18:44:57 +0000248#if defined(MBEDTLS_BIGNUM_C)
249
250unsigned mbedtls_test_get_case_uses_negative_0(void)
251{
Paul Elliott65064262023-11-27 17:29:05 +0000252 unsigned test_case_uses_negative_0 = 0;
253#ifdef MBEDTLS_THREADING_C
254 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
255#endif /* MBEDTLS_THREADING_C */
256 test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0;
257
258#ifdef MBEDTLS_THREADING_C
259 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
260#endif /* MBEDTLS_THREADING_C */
261
262 return test_case_uses_negative_0;
Paul Elliottc7a1e992023-11-03 18:44:57 +0000263}
264
265void mbedtls_test_set_case_uses_negative_0(unsigned uses)
266{
Paul Elliott65064262023-11-27 17:29:05 +0000267#ifdef MBEDTLS_THREADING_C
268 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
269#endif /* MBEDTLS_THREADING_C */
270
Paul Elliottc7a1e992023-11-03 18:44:57 +0000271 mbedtls_test_info.case_uses_negative_0 = uses;
Paul Elliott65064262023-11-27 17:29:05 +0000272
273#ifdef MBEDTLS_THREADING_C
274 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
275#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000276}
277
278void mbedtls_test_increment_case_uses_negative_0(void)
279{
Paul Elliott65064262023-11-27 17:29:05 +0000280#ifdef MBEDTLS_THREADING_C
281 mbedtls_mutex_lock(&mbedtls_test_info_mutex);
282#endif /* MBEDTLS_THREADING_C */
283
Paul Elliottc7a1e992023-11-03 18:44:57 +0000284 ++mbedtls_test_info.case_uses_negative_0;
Paul Elliott65064262023-11-27 17:29:05 +0000285
286#ifdef MBEDTLS_THREADING_C
287 mbedtls_mutex_unlock(&mbedtls_test_info_mutex);
288#endif /* MBEDTLS_THREADING_C */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000289}
290
291#endif
292
Paul Elliott4580d4d2023-10-27 18:41:02 +0100293/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200294/* Helper Functions */
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200297{
298 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200299
300#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
301 /* Make sure that injected entropy is present. Otherwise
302 * psa_crypto_init() will fail. This is not necessary for test suites
303 * that don't use PSA, but it's harmless (except for leaving a file
304 * behind). */
305 ret = mbedtls_test_inject_entropy_restore();
306 if (ret != 0) {
307 return ret;
308 }
309#endif
310
Ronald Cronf40529d2020-06-09 16:27:37 +0200311#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200313#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200314
Paul Elliott65064262023-11-27 17:29:05 +0000315#ifdef MBEDTLS_THREADING_C
316 mbedtls_mutex_init(&mbedtls_test_info_mutex);
317#endif /* MBEDTLS_THREADING_C */
318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200320}
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200323{
Paul Elliott65064262023-11-27 17:29:05 +0000324#ifdef MBEDTLS_THREADING_C
325 mbedtls_mutex_free(&mbedtls_test_info_mutex);
326#endif /* MBEDTLS_THREADING_C */
327
Ronald Cronf40529d2020-06-09 16:27:37 +0200328#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200330#endif /* MBEDTLS_PLATFORM_C */
331}
332
Gilles Peskine881447d2022-12-08 15:24:52 +0100333int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200334{
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200336 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200338 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200340 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 } else {
342 return -1;
343 }
Ronald Crona0c25392020-06-18 10:10:46 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200346}
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000349{
Paul Elliott5c498f32023-10-31 16:38:56 +0000350 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +0000351 /* We've already recorded the test as having failed. Don't
352 * overwrite any previous information about the failure. */
353 return;
354 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000355 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000356}
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000359{
Paul Elliott5c498f32023-10-31 16:38:56 +0000360 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000361}
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000364{
Paul Elliott5c498f32023-10-31 16:38:56 +0000365 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
366 mbedtls_test_set_step((unsigned long) (-1));
367 mbedtls_test_set_line1(NULL);
368 mbedtls_test_set_line2(NULL);
369
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100370#if defined(MBEDTLS_BIGNUM_C)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000371 mbedtls_test_set_case_uses_negative_0(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100372#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200373}
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375int mbedtls_test_equal(const char *test, int line_no, const char *filename,
376 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200377{
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 TEST_CF_PUBLIC(&value1, sizeof(value1));
379 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (value1 == value2) {
382 return 1;
383 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200384
Paul Elliott5c498f32023-10-31 16:38:56 +0000385 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200386 /* We've already recorded the test as having failed. Don't
387 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200389 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000390 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000392 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 "lhs = 0x%016llx = %lld",
394 value1, (long long) value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000395 mbedtls_test_set_line1(buf);
396 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 "rhs = 0x%016llx = %lld",
398 value2, (long long) value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000399 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000401}
402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
404 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +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 Elliott5c498f32023-10-31 16:38:56 +0000413 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200414 /* We've already recorded the test as having failed. Don't
415 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200417 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000418 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000420 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 "lhs = 0x%016llx = %llu",
422 value1, value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000423 mbedtls_test_set_line1(buf);
424 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 "rhs = 0x%016llx = %llu",
426 value2, value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000427 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200429}
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
432 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200433{
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 TEST_CF_PUBLIC(&value1, sizeof(value1));
435 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (value1 <= value2) {
438 return 1;
439 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200440
Paul Elliott5c498f32023-10-31 16:38:56 +0000441 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200442 /* We've already recorded the test as having failed. Don't
443 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200445 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000446 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000448 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 "lhs = 0x%016llx = %lld",
450 (unsigned long long) value1, value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000451 mbedtls_test_set_line1(buf);
452 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 "rhs = 0x%016llx = %lld",
454 (unsigned long long) value2, value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000455 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200457}
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459int mbedtls_test_unhexify(unsigned char *obuf,
460 size_t obufmax,
461 const char *ibuf,
462 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200463{
464 unsigned char uc, uc2;
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200467
468 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if ((*len) & 1) {
470 return -1;
471 }
Ronald Crona0c25392020-06-18 10:10:46 +0200472 *len /= 2;
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if ((*len) > obufmax) {
475 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200476 }
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 while (*ibuf != 0) {
479 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
480 return -1;
481 }
482
483 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
484 return -1;
485 }
486
487 *(obuf++) = (uc << 4) | uc2;
488 }
489
490 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200491}
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493void mbedtls_test_hexify(unsigned char *obuf,
494 const unsigned char *ibuf,
495 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200496{
497 unsigned char l, h;
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200500 h = *ibuf / 16;
501 l = *ibuf % 16;
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200504 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200506 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200510 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200512 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200514
515 ++ibuf;
516 len--;
517 }
518}
519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200521{
522 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 p = mbedtls_calloc(1, actual_len);
526 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200531}
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200534{
535 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200536 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (*olen == 0) {
541 return mbedtls_test_zero_alloc(*olen);
542 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 obuf = mbedtls_calloc(1, *olen);
545 TEST_HELPER_ASSERT(obuf != NULL);
546 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200549}
550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
552 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200553{
554 int ret = 0;
555 uint32_t i = 0;
556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (a_len != b_len) {
558 return -1;
559 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 for (i = 0; i < a_len; i++) {
562 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200563 ret = -1;
564 break;
565 }
566 }
567 return ret;
568}
Ronald Crona1236142020-07-01 16:01:21 +0200569
Chris Jones96ae73b2021-01-08 17:04:59 +0000570#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100571void mbedtls_test_err_add_check(int high, int low,
572 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000573{
Chris Jones3f613c12021-03-31 09:34:22 +0100574 /* Error codes are always negative (a value of zero is a success) however
575 * their positive opposites can be easier to understand. The following
576 * examples given in comments have been made positive for ease of
577 * understanding. The structure of an error code is such:
578 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100579 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100580 *
581 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100582 * h = high level error code (includes high level module ID (bits 12..14)
583 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100584 * l = low level error code.
585 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (high > -0x1000 && high != 0) {
587 /* high < 0001000000000000
588 * No high level module ID bits are set.
589 */
590 mbedtls_test_fail("'high' is not a high-level error code",
591 line, file);
592 } else if (high < -0x7F80) {
593 /* high > 0111111110000000
594 * Error code is greater than the largest allowed high level module ID.
595 */
596 mbedtls_test_fail("'high' error code is greater than 15 bits",
597 line, file);
598 } else if ((high & 0x7F) != 0) {
599 /* high & 0000000001111111
600 * Error code contains low level error code bits.
601 */
602 mbedtls_test_fail("'high' contains a low-level error code",
603 line, file);
604 } else if (low < -0x007F) {
605 /* low > 0000000001111111
606 * Error code contains high or module level error code bits.
607 */
608 mbedtls_test_fail("'low' error code is greater than 7 bits",
609 line, file);
610 } else if (low > 0) {
611 mbedtls_test_fail("'low' error code is greater than zero",
612 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000613 }
614}
615#endif /* MBEDTLS_TEST_HOOKS */