blob: 9d9ab08e55ffa037a9f464db336cede1a9b0014f [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 Horstmannf3c57142023-12-14 14:17:04 +000020#if defined(MBEDTLS_TEST_HOOKS)
21#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 Horstmannf3c57142023-12-14 14:17:04 +000053#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
54 mbedtls_poison_test_hooks_setup();
55#endif
56
Gilles Peskine4f8bf3c2023-04-28 23:39:45 +020057#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
58 /* Make sure that injected entropy is present. Otherwise
59 * psa_crypto_init() will fail. This is not necessary for test suites
60 * that don't use PSA, but it's harmless (except for leaving a file
61 * behind). */
62 ret = mbedtls_test_inject_entropy_restore();
63 if (ret != 0) {
64 return ret;
65 }
66#endif
67
Ronald Cronf40529d2020-06-09 16:27:37 +020068#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010069 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020070#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskine4f8bf3c2023-04-28 23:39:45 +020071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020073}
74
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010075void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020076{
David Horstmannf3c57142023-12-14 14:17:04 +000077#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
78 mbedtls_poison_test_hooks_teardown();
79#endif
80
Ronald Cronf40529d2020-06-09 16:27:37 +020081#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020083#endif /* MBEDTLS_PLATFORM_C */
84}
85
Ronald Crona0c25392020-06-18 10:10:46 +020086static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020087{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020089 *uc = c - '0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +020091 *uc = c - 'a' + 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020093 *uc = c - 'A' + 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 } else {
95 return -1;
96 }
Ronald Crona0c25392020-06-18 10:10:46 +020097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +020099}
100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000102{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +0000104 /* We've already recorded the test as having failed. Don't
105 * overwrite any previous information about the failure. */
106 return;
107 }
Chris Jonese60e2ae2021-01-20 17:51:47 +0000108 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
109 mbedtls_test_info.test = test;
110 mbedtls_test_info.line_no = line_no;
111 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000112}
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000115{
Chris Jonese60e2ae2021-01-20 17:51:47 +0000116 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
117 mbedtls_test_info.test = test;
118 mbedtls_test_info.line_no = line_no;
119 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000120}
121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000123{
124 mbedtls_test_info.step = step;
125}
126
Gilles Peskine53a72062022-11-09 21:08:44 +0100127#if defined(MBEDTLS_BIGNUM_C)
128unsigned mbedtls_test_case_uses_negative_0 = 0;
129#endif
130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000132{
133 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000135 mbedtls_test_info.test = 0;
136 mbedtls_test_info.line_no = 0;
137 mbedtls_test_info.filename = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
139 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskine53a72062022-11-09 21:08:44 +0100140#if defined(MBEDTLS_BIGNUM_C)
141 mbedtls_test_case_uses_negative_0 = 0;
142#endif
Gilles Peskineb4366492021-04-29 20:28:54 +0200143}
144
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145int mbedtls_test_equal(const char *test, int line_no, const char *filename,
146 unsigned long long value1, unsigned long long value2)
Gilles Peskineb4366492021-04-29 20:28:54 +0200147{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 TEST_CF_PUBLIC(&value1, sizeof(value1));
149 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 if (value1 == value2) {
152 return 1;
153 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskineb4366492021-04-29 20:28:54 +0200156 /* We've already recorded the test as having failed. Don't
157 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 return 0;
Gilles Peskineb4366492021-04-29 20:28:54 +0200159 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_test_fail(test, line_no, filename);
161 (void) mbedtls_snprintf(mbedtls_test_info.line1,
162 sizeof(mbedtls_test_info.line1),
163 "lhs = 0x%016llx = %lld",
164 value1, (long long) value1);
165 (void) mbedtls_snprintf(mbedtls_test_info.line2,
166 sizeof(mbedtls_test_info.line2),
167 "rhs = 0x%016llx = %lld",
168 value2, (long long) value2);
169 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000170}
171
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
173 unsigned long long value1, unsigned long long value2)
Gilles Peskine063700d2022-04-13 23:59:52 +0200174{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 TEST_CF_PUBLIC(&value1, sizeof(value1));
176 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200177
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100178 if (value1 <= value2) {
179 return 1;
180 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine063700d2022-04-13 23:59:52 +0200183 /* We've already recorded the test as having failed. Don't
184 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200186 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 mbedtls_test_fail(test, line_no, filename);
188 (void) mbedtls_snprintf(mbedtls_test_info.line1,
189 sizeof(mbedtls_test_info.line1),
190 "lhs = 0x%016llx = %llu",
191 value1, value1);
192 (void) mbedtls_snprintf(mbedtls_test_info.line2,
193 sizeof(mbedtls_test_info.line2),
194 "rhs = 0x%016llx = %llu",
195 value2, value2);
196 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200197}
198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
200 long long value1, long long value2)
Gilles Peskine063700d2022-04-13 23:59:52 +0200201{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202 TEST_CF_PUBLIC(&value1, sizeof(value1));
203 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200204
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100205 if (value1 <= value2) {
206 return 1;
207 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine063700d2022-04-13 23:59:52 +0200210 /* We've already recorded the test as having failed. Don't
211 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200213 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 mbedtls_test_fail(test, line_no, filename);
215 (void) mbedtls_snprintf(mbedtls_test_info.line1,
216 sizeof(mbedtls_test_info.line1),
217 "lhs = 0x%016llx = %lld",
218 (unsigned long long) value1, value1);
219 (void) mbedtls_snprintf(mbedtls_test_info.line2,
220 sizeof(mbedtls_test_info.line2),
221 "rhs = 0x%016llx = %lld",
222 (unsigned long long) value2, value2);
223 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200224}
225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226int mbedtls_test_unhexify(unsigned char *obuf,
227 size_t obufmax,
228 const char *ibuf,
229 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200230{
231 unsigned char uc, uc2;
232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200234
235 /* Must be even number of bytes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 if ((*len) & 1) {
237 return -1;
238 }
Ronald Crona0c25392020-06-18 10:10:46 +0200239 *len /= 2;
240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 if ((*len) > obufmax) {
242 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200243 }
244
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 while (*ibuf != 0) {
246 if (ascii2uc(*(ibuf++), &uc) != 0) {
247 return -1;
248 }
249
250 if (ascii2uc(*(ibuf++), &uc2) != 0) {
251 return -1;
252 }
253
254 *(obuf++) = (uc << 4) | uc2;
255 }
256
257 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200258}
259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260void mbedtls_test_hexify(unsigned char *obuf,
261 const unsigned char *ibuf,
262 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200263{
264 unsigned char l, h;
265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200267 h = *ibuf / 16;
268 l = *ibuf % 16;
269
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200271 *obuf++ = '0' + h;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200273 *obuf++ = 'a' + h - 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200277 *obuf++ = '0' + l;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200279 *obuf++ = 'a' + l - 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200281
282 ++ibuf;
283 len--;
284 }
285}
286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200288{
289 void *p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200291
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 p = mbedtls_calloc(1, actual_len);
293 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200298}
299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200301{
302 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200303 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200304
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 if (*olen == 0) {
308 return mbedtls_test_zero_alloc(*olen);
309 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 obuf = mbedtls_calloc(1, *olen);
312 TEST_HELPER_ASSERT(obuf != NULL);
313 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200314
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200316}
317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
319 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200320{
321 int ret = 0;
322 uint32_t i = 0;
323
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 if (a_len != b_len) {
325 return -1;
326 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200327
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 for (i = 0; i < a_len; i++) {
329 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200330 ret = -1;
331 break;
332 }
333 }
334 return ret;
335}
Ronald Crona1236142020-07-01 16:01:21 +0200336
337#if defined(MBEDTLS_CHECK_PARAMS)
338void mbedtls_test_param_failed_get_location_record(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 mbedtls_test_param_failed_location_record_t *location_record)
Ronald Crona1236142020-07-01 16:01:21 +0200340{
341 *location_record = param_failed_ctx.location_record;
342}
343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344void mbedtls_test_param_failed_expect_call(void)
Ronald Crona1236142020-07-01 16:01:21 +0200345{
346 param_failed_ctx.expected_call_happened = 0;
347 param_failed_ctx.expected_call = 1;
348}
349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350int mbedtls_test_param_failed_check_expected_call(void)
Ronald Crona1236142020-07-01 16:01:21 +0200351{
352 param_failed_ctx.expected_call = 0;
353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (param_failed_ctx.expected_call_happened != 0) {
355 return 0;
356 }
Ronald Crona1236142020-07-01 16:01:21 +0200357
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 return -1;
Ronald Crona1236142020-07-01 16:01:21 +0200359}
360
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100361void *mbedtls_test_param_failed_get_state_buf(void)
Ronald Crona1236142020-07-01 16:01:21 +0200362{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200363 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200364}
365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366void mbedtls_test_param_failed_reset_state(void)
Ronald Crona1236142020-07-01 16:01:21 +0200367{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100368 memset(param_failed_ctx.state, 0, sizeof(param_failed_ctx.state));
Ronald Crona1236142020-07-01 16:01:21 +0200369}
370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371void mbedtls_param_failed(const char *failure_condition,
372 const char *file,
373 int line)
Ronald Crona1236142020-07-01 16:01:21 +0200374{
375 /* Record the location of the failure */
376 param_failed_ctx.location_record.failure_condition = failure_condition;
377 param_failed_ctx.location_record.file = file;
378 param_failed_ctx.location_record.line = line;
379
380 /* If we are testing the callback function... */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 if (param_failed_ctx.expected_call != 0) {
Ronald Crona1236142020-07-01 16:01:21 +0200382 param_failed_ctx.expected_call = 0;
383 param_failed_ctx.expected_call_happened = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 } else {
Ronald Crona1236142020-07-01 16:01:21 +0200385 /* ...else try a long jump. If the execution state has not been set-up
386 * or reset then the long jump buffer is all zero's and the call will
387 * with high probability fault, emphasizing there is something to look
388 * at.
389 */
390
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 longjmp(param_failed_ctx.state, 1);
Ronald Crona1236142020-07-01 16:01:21 +0200392 }
393}
394#endif /* MBEDTLS_CHECK_PARAMS */
Chris Jones96ae73b2021-01-08 17:04:59 +0000395
396#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100397void mbedtls_test_err_add_check(int high, int low,
398 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000399{
Chris Jones3f613c12021-03-31 09:34:22 +0100400 /* Error codes are always negative (a value of zero is a success) however
401 * their positive opposites can be easier to understand. The following
402 * examples given in comments have been made positive for ease of
403 * understanding. The structure of an error code is such:
404 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100405 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100406 *
407 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100408 * h = high level error code (includes high level module ID (bits 12..14)
409 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100410 * l = low level error code.
411 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100412 if (high > -0x1000 && high != 0) {
413 /* high < 0001000000000000
414 * No high level module ID bits are set.
415 */
416 mbedtls_test_fail("'high' is not a high-level error code",
417 line, file);
418 } else if (high < -0x7F80) {
419 /* high > 0111111110000000
420 * Error code is greater than the largest allowed high level module ID.
421 */
422 mbedtls_test_fail("'high' error code is greater than 15 bits",
423 line, file);
424 } else if ((high & 0x7F) != 0) {
425 /* high & 0000000001111111
426 * Error code contains low level error code bits.
427 */
428 mbedtls_test_fail("'high' contains a low-level error code",
429 line, file);
430 } else if (low < -0x007F) {
431 /* low > 0000000001111111
432 * Error code contains high or module level error code bits.
433 */
434 mbedtls_test_fail("'low' error code is greater than 7 bits",
435 line, file);
436 } else if (low > 0) {
437 mbedtls_test_fail("'low' error code is greater than zero",
438 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000439 }
440}
441#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskinedb479712021-06-11 14:13:53 +0200442
443#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s)
Gilles Peskinedb479712021-06-11 14:13:53 +0200445{
Gilles Peskine53a72062022-11-09 21:08:44 +0100446 int negative = 0;
447 /* Always set the sign bit to -1 if the input has a minus sign, even for 0.
448 * This creates an invalid representation, which mbedtls_mpi_read_string()
449 * avoids but we want to be able to create that in test data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 if (s[0] == '-') {
Gilles Peskine53a72062022-11-09 21:08:44 +0100451 ++s;
452 negative = 1;
453 }
Gilles Peskinedb479712021-06-11 14:13:53 +0200454 /* mbedtls_mpi_read_string() currently retains leading zeros.
455 * It always allocates at least one limb for the value 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456 if (s[0] == 0) {
457 mbedtls_mpi_free(X);
458 return 0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200459 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 int ret = mbedtls_mpi_read_string(X, 16, s);
461 if (ret != 0) {
462 return ret;
463 }
464 if (negative) {
465 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
Gilles Peskine53a72062022-11-09 21:08:44 +0100466 ++mbedtls_test_case_uses_negative_0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 }
Gilles Peskine53a72062022-11-09 21:08:44 +0100468 X->s = -1;
469 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 return 0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200471}
472#endif