blob: a1e1d459ce883b7c73a5a945e93963626a1be3a6 [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 Rodgman7ff79652023-11-03 12:04:52 +00003 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02004 */
5
Gilles Peskine7db8e892022-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
Ronald Crona1236142020-07-01 16:01:21 +020011#if defined(MBEDTLS_CHECK_PARAMS)
12#include <setjmp.h>
13#endif
14
Gilles Peskine4f8bf3c2023-04-28 23:39:45 +020015#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
16#include <psa/crypto.h>
17#include <test/psa_crypto_helpers.h>
18#endif
19
David Horstmanncb229db2023-12-18 15:30:46 +000020#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C)
David Horstmannf3c57142023-12-14 14:17:04 +000021#include <test/psa_memory_poisoning_wrappers.h>
22#endif
23
Ronald Crona1236142020-07-01 16:01:21 +020024/*----------------------------------------------------------------------------*/
25/* Static global variables */
26
27#if defined(MBEDTLS_CHECK_PARAMS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010028typedef struct {
Ronald Crona1236142020-07-01 16:01:21 +020029 uint8_t expected_call;
30 uint8_t expected_call_happened;
31
32 jmp_buf state;
33
34 mbedtls_test_param_failed_location_record_t location_record;
35}
36param_failed_ctx_t;
37static param_failed_ctx_t param_failed_ctx;
38#endif
39
Ronald Cronf40529d2020-06-09 16:27:37 +020040#if defined(MBEDTLS_PLATFORM_C)
41static mbedtls_platform_context platform_ctx;
42#endif
43
Chris Jonese60e2ae2021-01-20 17:51:47 +000044mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000045
Ronald Crona1236142020-07-01 16:01:21 +020046/*----------------------------------------------------------------------------*/
47/* Helper Functions */
48
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020050{
51 int ret = 0;
Gilles Peskine4f8bf3c2023-04-28 23:39:45 +020052
David Horstmannffcc7692023-12-15 18:29:54 +000053#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
David Horstmann42015332024-03-13 15:42:31 +000054 && !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) \
David Horstmannffcc7692023-12-15 18:29:54 +000055 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannf3c57142023-12-14 14:17:04 +000056 mbedtls_poison_test_hooks_setup();
57#endif
58
Gilles Peskine4f8bf3c2023-04-28 23:39:45 +020059#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
60 /* Make sure that injected entropy is present. Otherwise
61 * psa_crypto_init() will fail. This is not necessary for test suites
62 * that don't use PSA, but it's harmless (except for leaving a file
63 * behind). */
64 ret = mbedtls_test_inject_entropy_restore();
65 if (ret != 0) {
66 return ret;
67 }
68#endif
69
Ronald Cronf40529d2020-06-09 16:27:37 +020070#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020072#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskine4f8bf3c2023-04-28 23:39:45 +020073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020075}
76
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020078{
David Horstmannffcc7692023-12-15 18:29:54 +000079#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
David Horstmann42015332024-03-13 15:42:31 +000080 && !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) \
David Horstmannffcc7692023-12-15 18:29:54 +000081 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannf3c57142023-12-14 14:17:04 +000082 mbedtls_poison_test_hooks_teardown();
83#endif
84
Ronald Cronf40529d2020-06-09 16:27:37 +020085#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020087#endif /* MBEDTLS_PLATFORM_C */
88}
89
Ronald Crona0c25392020-06-18 10:10:46 +020090static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020091{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020093 *uc = c - '0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +020095 *uc = c - 'a' + 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020097 *uc = c - 'A' + 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 } else {
99 return -1;
100 }
Ronald Crona0c25392020-06-18 10:10:46 +0200101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200103}
104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000106{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +0000108 /* We've already recorded the test as having failed. Don't
109 * overwrite any previous information about the failure. */
110 return;
111 }
Chris Jonese60e2ae2021-01-20 17:51:47 +0000112 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
113 mbedtls_test_info.test = test;
114 mbedtls_test_info.line_no = line_no;
115 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000116}
117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000119{
Chris Jonese60e2ae2021-01-20 17:51:47 +0000120 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
121 mbedtls_test_info.test = test;
122 mbedtls_test_info.line_no = line_no;
123 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000124}
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000127{
128 mbedtls_test_info.step = step;
129}
130
Gilles Peskine53a72062022-11-09 21:08:44 +0100131#if defined(MBEDTLS_BIGNUM_C)
132unsigned mbedtls_test_case_uses_negative_0 = 0;
133#endif
134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000136{
137 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000139 mbedtls_test_info.test = 0;
140 mbedtls_test_info.line_no = 0;
141 mbedtls_test_info.filename = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
143 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskine53a72062022-11-09 21:08:44 +0100144#if defined(MBEDTLS_BIGNUM_C)
145 mbedtls_test_case_uses_negative_0 = 0;
146#endif
Gilles Peskineb4366492021-04-29 20:28:54 +0200147}
148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149int mbedtls_test_equal(const char *test, int line_no, const char *filename,
150 unsigned long long value1, unsigned long long value2)
Gilles Peskineb4366492021-04-29 20:28:54 +0200151{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 TEST_CF_PUBLIC(&value1, sizeof(value1));
153 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 if (value1 == value2) {
156 return 1;
157 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskineb4366492021-04-29 20:28:54 +0200160 /* We've already recorded the test as having failed. Don't
161 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 return 0;
Gilles Peskineb4366492021-04-29 20:28:54 +0200163 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 mbedtls_test_fail(test, line_no, filename);
165 (void) mbedtls_snprintf(mbedtls_test_info.line1,
166 sizeof(mbedtls_test_info.line1),
167 "lhs = 0x%016llx = %lld",
168 value1, (long long) value1);
169 (void) mbedtls_snprintf(mbedtls_test_info.line2,
170 sizeof(mbedtls_test_info.line2),
171 "rhs = 0x%016llx = %lld",
172 value2, (long long) value2);
173 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000174}
175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
177 unsigned long long value1, unsigned long long value2)
Gilles Peskine063700d2022-04-13 23:59:52 +0200178{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 TEST_CF_PUBLIC(&value1, sizeof(value1));
180 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if (value1 <= value2) {
183 return 1;
184 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200185
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine063700d2022-04-13 23:59:52 +0200187 /* We've already recorded the test as having failed. Don't
188 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200190 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 mbedtls_test_fail(test, line_no, filename);
192 (void) mbedtls_snprintf(mbedtls_test_info.line1,
193 sizeof(mbedtls_test_info.line1),
194 "lhs = 0x%016llx = %llu",
195 value1, value1);
196 (void) mbedtls_snprintf(mbedtls_test_info.line2,
197 sizeof(mbedtls_test_info.line2),
198 "rhs = 0x%016llx = %llu",
199 value2, value2);
200 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200201}
202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
204 long long value1, long long value2)
Gilles Peskine063700d2022-04-13 23:59:52 +0200205{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 TEST_CF_PUBLIC(&value1, sizeof(value1));
207 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 if (value1 <= value2) {
210 return 1;
211 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200212
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine063700d2022-04-13 23:59:52 +0200214 /* We've already recorded the test as having failed. Don't
215 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200217 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 mbedtls_test_fail(test, line_no, filename);
219 (void) mbedtls_snprintf(mbedtls_test_info.line1,
220 sizeof(mbedtls_test_info.line1),
221 "lhs = 0x%016llx = %lld",
222 (unsigned long long) value1, value1);
223 (void) mbedtls_snprintf(mbedtls_test_info.line2,
224 sizeof(mbedtls_test_info.line2),
225 "rhs = 0x%016llx = %lld",
226 (unsigned long long) value2, value2);
227 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200228}
229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230int mbedtls_test_unhexify(unsigned char *obuf,
231 size_t obufmax,
232 const char *ibuf,
233 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200234{
235 unsigned char uc, uc2;
236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200238
239 /* Must be even number of bytes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if ((*len) & 1) {
241 return -1;
242 }
Ronald Crona0c25392020-06-18 10:10:46 +0200243 *len /= 2;
244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 if ((*len) > obufmax) {
246 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200247 }
248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 while (*ibuf != 0) {
250 if (ascii2uc(*(ibuf++), &uc) != 0) {
251 return -1;
252 }
253
254 if (ascii2uc(*(ibuf++), &uc2) != 0) {
255 return -1;
256 }
257
258 *(obuf++) = (uc << 4) | uc2;
259 }
260
261 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200262}
263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264void mbedtls_test_hexify(unsigned char *obuf,
265 const unsigned char *ibuf,
266 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200267{
268 unsigned char l, h;
269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200271 h = *ibuf / 16;
272 l = *ibuf % 16;
273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200275 *obuf++ = '0' + h;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200277 *obuf++ = 'a' + h - 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200281 *obuf++ = '0' + l;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200283 *obuf++ = 'a' + l - 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200285
286 ++ibuf;
287 len--;
288 }
289}
290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200292{
293 void *p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 p = mbedtls_calloc(1, actual_len);
297 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200298
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200300
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200302}
303
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200305{
306 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200307 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200308
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (*olen == 0) {
312 return mbedtls_test_zero_alloc(*olen);
313 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 obuf = mbedtls_calloc(1, *olen);
316 TEST_HELPER_ASSERT(obuf != NULL);
317 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200320}
321
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
323 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200324{
325 int ret = 0;
326 uint32_t i = 0;
327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 if (a_len != b_len) {
329 return -1;
330 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 for (i = 0; i < a_len; i++) {
333 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200334 ret = -1;
335 break;
336 }
337 }
338 return ret;
339}
Ronald Crona1236142020-07-01 16:01:21 +0200340
341#if defined(MBEDTLS_CHECK_PARAMS)
342void mbedtls_test_param_failed_get_location_record(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 mbedtls_test_param_failed_location_record_t *location_record)
Ronald Crona1236142020-07-01 16:01:21 +0200344{
345 *location_record = param_failed_ctx.location_record;
346}
347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348void mbedtls_test_param_failed_expect_call(void)
Ronald Crona1236142020-07-01 16:01:21 +0200349{
350 param_failed_ctx.expected_call_happened = 0;
351 param_failed_ctx.expected_call = 1;
352}
353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354int mbedtls_test_param_failed_check_expected_call(void)
Ronald Crona1236142020-07-01 16:01:21 +0200355{
356 param_failed_ctx.expected_call = 0;
357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 if (param_failed_ctx.expected_call_happened != 0) {
359 return 0;
360 }
Ronald Crona1236142020-07-01 16:01:21 +0200361
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 return -1;
Ronald Crona1236142020-07-01 16:01:21 +0200363}
364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365void *mbedtls_test_param_failed_get_state_buf(void)
Ronald Crona1236142020-07-01 16:01:21 +0200366{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200367 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200368}
369
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370void mbedtls_test_param_failed_reset_state(void)
Ronald Crona1236142020-07-01 16:01:21 +0200371{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 memset(param_failed_ctx.state, 0, sizeof(param_failed_ctx.state));
Ronald Crona1236142020-07-01 16:01:21 +0200373}
374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375void mbedtls_param_failed(const char *failure_condition,
376 const char *file,
377 int line)
Ronald Crona1236142020-07-01 16:01:21 +0200378{
379 /* Record the location of the failure */
380 param_failed_ctx.location_record.failure_condition = failure_condition;
381 param_failed_ctx.location_record.file = file;
382 param_failed_ctx.location_record.line = line;
383
384 /* If we are testing the callback function... */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 if (param_failed_ctx.expected_call != 0) {
Ronald Crona1236142020-07-01 16:01:21 +0200386 param_failed_ctx.expected_call = 0;
387 param_failed_ctx.expected_call_happened = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 } else {
Ronald Crona1236142020-07-01 16:01:21 +0200389 /* ...else try a long jump. If the execution state has not been set-up
390 * or reset then the long jump buffer is all zero's and the call will
391 * with high probability fault, emphasizing there is something to look
392 * at.
393 */
394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 longjmp(param_failed_ctx.state, 1);
Ronald Crona1236142020-07-01 16:01:21 +0200396 }
397}
398#endif /* MBEDTLS_CHECK_PARAMS */
Chris Jones96ae73b2021-01-08 17:04:59 +0000399
400#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401void mbedtls_test_err_add_check(int high, int low,
402 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000403{
Chris Jones3f613c12021-03-31 09:34:22 +0100404 /* Error codes are always negative (a value of zero is a success) however
405 * their positive opposites can be easier to understand. The following
406 * examples given in comments have been made positive for ease of
407 * understanding. The structure of an error code is such:
408 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100409 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100410 *
411 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100412 * h = high level error code (includes high level module ID (bits 12..14)
413 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100414 * l = low level error code.
415 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 if (high > -0x1000 && high != 0) {
417 /* high < 0001000000000000
418 * No high level module ID bits are set.
419 */
420 mbedtls_test_fail("'high' is not a high-level error code",
421 line, file);
422 } else if (high < -0x7F80) {
423 /* high > 0111111110000000
424 * Error code is greater than the largest allowed high level module ID.
425 */
426 mbedtls_test_fail("'high' error code is greater than 15 bits",
427 line, file);
428 } else if ((high & 0x7F) != 0) {
429 /* high & 0000000001111111
430 * Error code contains low level error code bits.
431 */
432 mbedtls_test_fail("'high' contains a low-level error code",
433 line, file);
434 } else if (low < -0x007F) {
435 /* low > 0000000001111111
436 * Error code contains high or module level error code bits.
437 */
438 mbedtls_test_fail("'low' error code is greater than 7 bits",
439 line, file);
440 } else if (low > 0) {
441 mbedtls_test_fail("'low' error code is greater than zero",
442 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000443 }
444}
445#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskinedb479712021-06-11 14:13:53 +0200446
447#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s)
Gilles Peskinedb479712021-06-11 14:13:53 +0200449{
Gilles Peskine53a72062022-11-09 21:08:44 +0100450 int negative = 0;
451 /* Always set the sign bit to -1 if the input has a minus sign, even for 0.
452 * This creates an invalid representation, which mbedtls_mpi_read_string()
453 * avoids but we want to be able to create that in test data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 if (s[0] == '-') {
Gilles Peskine53a72062022-11-09 21:08:44 +0100455 ++s;
456 negative = 1;
457 }
Gilles Peskinedb479712021-06-11 14:13:53 +0200458 /* mbedtls_mpi_read_string() currently retains leading zeros.
459 * It always allocates at least one limb for the value 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 if (s[0] == 0) {
461 mbedtls_mpi_free(X);
462 return 0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200463 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100464 int ret = mbedtls_mpi_read_string(X, 16, s);
465 if (ret != 0) {
466 return ret;
467 }
468 if (negative) {
469 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
Gilles Peskine53a72062022-11-09 21:08:44 +0100470 ++mbedtls_test_case_uses_negative_0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471 }
Gilles Peskine53a72062022-11-09 21:08:44 +0100472 X->s = -1;
473 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 return 0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200475}
476#endif