blob: d0c75b08d192a57ac93f764716938f6ae15618eb [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 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
Paul Elliott3d2db892024-01-19 20:42:56 +0000291#endif /* MBEDTLS_BIGNUM_C */
292
293#ifdef MBEDTLS_TEST_MUTEX_USAGE
294mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void)
295{
296 return &mbedtls_test_info_mutex;
297}
298
299#endif /* MBEDTLS_TEST_MUTEX_USAGE */
Paul Elliottc7a1e992023-11-03 18:44:57 +0000300
Paul Elliott4580d4d2023-10-27 18:41:02 +0100301/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200302/* Helper Functions */
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200305{
306 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200307
308#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
309 /* Make sure that injected entropy is present. Otherwise
310 * psa_crypto_init() will fail. This is not necessary for test suites
311 * that don't use PSA, but it's harmless (except for leaving a file
312 * behind). */
313 ret = mbedtls_test_inject_entropy_restore();
314 if (ret != 0) {
315 return ret;
316 }
317#endif
318
Ronald Cronf40529d2020-06-09 16:27:37 +0200319#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200321#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200322
Paul Elliott65064262023-11-27 17:29:05 +0000323#ifdef MBEDTLS_THREADING_C
324 mbedtls_mutex_init(&mbedtls_test_info_mutex);
325#endif /* MBEDTLS_THREADING_C */
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200328}
329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200331{
Paul Elliott65064262023-11-27 17:29:05 +0000332#ifdef MBEDTLS_THREADING_C
333 mbedtls_mutex_free(&mbedtls_test_info_mutex);
334#endif /* MBEDTLS_THREADING_C */
335
Ronald Cronf40529d2020-06-09 16:27:37 +0200336#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200338#endif /* MBEDTLS_PLATFORM_C */
339}
340
Gilles Peskine881447d2022-12-08 15:24:52 +0100341int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200342{
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200344 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200346 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200348 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 } else {
350 return -1;
351 }
Ronald Crona0c25392020-06-18 10:10:46 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000357{
Paul Elliott5c498f32023-10-31 16:38:56 +0000358 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +0000359 /* We've already recorded the test as having failed. Don't
360 * overwrite any previous information about the failure. */
361 return;
362 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000363 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000364}
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000367{
Paul Elliott5c498f32023-10-31 16:38:56 +0000368 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000369}
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000372{
Paul Elliott5c498f32023-10-31 16:38:56 +0000373 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
374 mbedtls_test_set_step((unsigned long) (-1));
375 mbedtls_test_set_line1(NULL);
376 mbedtls_test_set_line2(NULL);
377
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100378#if defined(MBEDTLS_BIGNUM_C)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000379 mbedtls_test_set_case_uses_negative_0(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100380#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200381}
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383int mbedtls_test_equal(const char *test, int line_no, const char *filename,
384 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200385{
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 TEST_CF_PUBLIC(&value1, sizeof(value1));
387 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (value1 == value2) {
390 return 1;
391 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200392
Paul Elliott5c498f32023-10-31 16:38:56 +0000393 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200394 /* We've already recorded the test as having failed. Don't
395 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200397 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000398 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000400 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 "lhs = 0x%016llx = %lld",
402 value1, (long long) value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000403 mbedtls_test_set_line1(buf);
404 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 "rhs = 0x%016llx = %lld",
406 value2, (long long) value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000407 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000409}
410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
412 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200413{
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 TEST_CF_PUBLIC(&value1, sizeof(value1));
415 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (value1 <= value2) {
418 return 1;
419 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200420
Paul Elliott5c498f32023-10-31 16:38:56 +0000421 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200422 /* We've already recorded the test as having failed. Don't
423 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200425 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000426 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000428 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 "lhs = 0x%016llx = %llu",
430 value1, value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000431 mbedtls_test_set_line1(buf);
432 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 "rhs = 0x%016llx = %llu",
434 value2, value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000435 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200437}
438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
440 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200441{
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 TEST_CF_PUBLIC(&value1, sizeof(value1));
443 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (value1 <= value2) {
446 return 1;
447 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200448
Paul Elliott5c498f32023-10-31 16:38:56 +0000449 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200450 /* We've already recorded the test as having failed. Don't
451 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200453 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000454 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000456 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 "lhs = 0x%016llx = %lld",
458 (unsigned long long) value1, value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000459 mbedtls_test_set_line1(buf);
460 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 "rhs = 0x%016llx = %lld",
462 (unsigned long long) value2, value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000463 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200465}
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467int mbedtls_test_unhexify(unsigned char *obuf,
468 size_t obufmax,
469 const char *ibuf,
470 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200471{
472 unsigned char uc, uc2;
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200475
476 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if ((*len) & 1) {
478 return -1;
479 }
Ronald Crona0c25392020-06-18 10:10:46 +0200480 *len /= 2;
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if ((*len) > obufmax) {
483 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200484 }
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 while (*ibuf != 0) {
487 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
488 return -1;
489 }
490
491 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
492 return -1;
493 }
494
495 *(obuf++) = (uc << 4) | uc2;
496 }
497
498 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200499}
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501void mbedtls_test_hexify(unsigned char *obuf,
502 const unsigned char *ibuf,
503 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200504{
505 unsigned char l, h;
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200508 h = *ibuf / 16;
509 l = *ibuf % 16;
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200512 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200514 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200518 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200520 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200522
523 ++ibuf;
524 len--;
525 }
526}
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200529{
530 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 p = mbedtls_calloc(1, actual_len);
534 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200539}
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200542{
543 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200544 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (*olen == 0) {
549 return mbedtls_test_zero_alloc(*olen);
550 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 obuf = mbedtls_calloc(1, *olen);
553 TEST_HELPER_ASSERT(obuf != NULL);
554 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200557}
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
560 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200561{
562 int ret = 0;
563 uint32_t i = 0;
564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (a_len != b_len) {
566 return -1;
567 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 for (i = 0; i < a_len; i++) {
570 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200571 ret = -1;
572 break;
573 }
574 }
575 return ret;
576}
Ronald Crona1236142020-07-01 16:01:21 +0200577
Chris Jones96ae73b2021-01-08 17:04:59 +0000578#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100579void mbedtls_test_err_add_check(int high, int low,
580 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000581{
Chris Jones3f613c12021-03-31 09:34:22 +0100582 /* Error codes are always negative (a value of zero is a success) however
583 * their positive opposites can be easier to understand. The following
584 * examples given in comments have been made positive for ease of
585 * understanding. The structure of an error code is such:
586 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100587 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100588 *
589 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100590 * h = high level error code (includes high level module ID (bits 12..14)
591 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100592 * l = low level error code.
593 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (high > -0x1000 && high != 0) {
595 /* high < 0001000000000000
596 * No high level module ID bits are set.
597 */
598 mbedtls_test_fail("'high' is not a high-level error code",
599 line, file);
600 } else if (high < -0x7F80) {
601 /* high > 0111111110000000
602 * Error code is greater than the largest allowed high level module ID.
603 */
604 mbedtls_test_fail("'high' error code is greater than 15 bits",
605 line, file);
606 } else if ((high & 0x7F) != 0) {
607 /* high & 0000000001111111
608 * Error code contains low level error code bits.
609 */
610 mbedtls_test_fail("'high' contains a low-level error code",
611 line, file);
612 } else if (low < -0x007F) {
613 /* low > 0000000001111111
614 * Error code contains high or module level error code bits.
615 */
616 mbedtls_test_fail("'low' error code is greater than 7 bits",
617 line, file);
618 } else if (low > 0) {
619 mbedtls_test_fail("'low' error code is greater than zero",
620 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000621 }
622}
623#endif /* MBEDTLS_TEST_HOOKS */