blob: 6c215d1c0a7d73568570880785be696767fe579c [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
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020016 */
17
Gilles Peskine7db8e892022-09-20 18:31:30 +020018#include <test/constant_flow.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020019#include <test/helpers.h>
Ronald Cronf40529d2020-06-09 16:27:37 +020020#include <test/macros.h>
21#include <string.h>
22
Ronald Crona1236142020-07-01 16:01:21 +020023#if defined(MBEDTLS_CHECK_PARAMS)
24#include <setjmp.h>
25#endif
26
27/*----------------------------------------------------------------------------*/
28/* Static global variables */
29
30#if defined(MBEDTLS_CHECK_PARAMS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010031typedef struct {
Ronald Crona1236142020-07-01 16:01:21 +020032 uint8_t expected_call;
33 uint8_t expected_call_happened;
34
35 jmp_buf state;
36
37 mbedtls_test_param_failed_location_record_t location_record;
38}
39param_failed_ctx_t;
40static param_failed_ctx_t param_failed_ctx;
41#endif
42
Ronald Cronf40529d2020-06-09 16:27:37 +020043#if defined(MBEDTLS_PLATFORM_C)
44static mbedtls_platform_context platform_ctx;
45#endif
46
Chris Jonese60e2ae2021-01-20 17:51:47 +000047mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000048
Ronald Crona1236142020-07-01 16:01:21 +020049/*----------------------------------------------------------------------------*/
50/* Helper Functions */
51
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020053{
54 int ret = 0;
55#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020057#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020059}
60
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020062{
63#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020065#endif /* MBEDTLS_PLATFORM_C */
66}
67
Ronald Crona0c25392020-06-18 10:10:46 +020068static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020069{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020071 *uc = c - '0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +020073 *uc = c - 'a' + 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020075 *uc = c - 'A' + 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 } else {
77 return -1;
78 }
Ronald Crona0c25392020-06-18 10:10:46 +020079
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +020081}
82
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000084{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +000086 /* We've already recorded the test as having failed. Don't
87 * overwrite any previous information about the failure. */
88 return;
89 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000090 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
91 mbedtls_test_info.test = test;
92 mbedtls_test_info.line_no = line_no;
93 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000094}
95
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000097{
Chris Jonese60e2ae2021-01-20 17:51:47 +000098 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
99 mbedtls_test_info.test = test;
100 mbedtls_test_info.line_no = line_no;
101 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000102}
103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000105{
106 mbedtls_test_info.step = step;
107}
108
Gilles Peskine53a72062022-11-09 21:08:44 +0100109#if defined(MBEDTLS_BIGNUM_C)
110unsigned mbedtls_test_case_uses_negative_0 = 0;
111#endif
112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000114{
115 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000117 mbedtls_test_info.test = 0;
118 mbedtls_test_info.line_no = 0;
119 mbedtls_test_info.filename = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
121 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskine53a72062022-11-09 21:08:44 +0100122#if defined(MBEDTLS_BIGNUM_C)
123 mbedtls_test_case_uses_negative_0 = 0;
124#endif
Gilles Peskineb4366492021-04-29 20:28:54 +0200125}
126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127int mbedtls_test_equal(const char *test, int line_no, const char *filename,
128 unsigned long long value1, unsigned long long value2)
Gilles Peskineb4366492021-04-29 20:28:54 +0200129{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 TEST_CF_PUBLIC(&value1, sizeof(value1));
131 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 if (value1 == value2) {
134 return 1;
135 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskineb4366492021-04-29 20:28:54 +0200138 /* We've already recorded the test as having failed. Don't
139 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140 return 0;
Gilles Peskineb4366492021-04-29 20:28:54 +0200141 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 mbedtls_test_fail(test, line_no, filename);
143 (void) mbedtls_snprintf(mbedtls_test_info.line1,
144 sizeof(mbedtls_test_info.line1),
145 "lhs = 0x%016llx = %lld",
146 value1, (long long) value1);
147 (void) mbedtls_snprintf(mbedtls_test_info.line2,
148 sizeof(mbedtls_test_info.line2),
149 "rhs = 0x%016llx = %lld",
150 value2, (long long) value2);
151 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000152}
153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
155 unsigned long long value1, unsigned long long value2)
Gilles Peskine063700d2022-04-13 23:59:52 +0200156{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100157 TEST_CF_PUBLIC(&value1, sizeof(value1));
158 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (value1 <= value2) {
161 return 1;
162 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200163
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine063700d2022-04-13 23:59:52 +0200165 /* We've already recorded the test as having failed. Don't
166 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200168 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 mbedtls_test_fail(test, line_no, filename);
170 (void) mbedtls_snprintf(mbedtls_test_info.line1,
171 sizeof(mbedtls_test_info.line1),
172 "lhs = 0x%016llx = %llu",
173 value1, value1);
174 (void) mbedtls_snprintf(mbedtls_test_info.line2,
175 sizeof(mbedtls_test_info.line2),
176 "rhs = 0x%016llx = %llu",
177 value2, value2);
178 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200179}
180
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
182 long long value1, long long value2)
Gilles Peskine063700d2022-04-13 23:59:52 +0200183{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 TEST_CF_PUBLIC(&value1, sizeof(value1));
185 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskine7db8e892022-09-20 18:31:30 +0200186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 if (value1 <= value2) {
188 return 1;
189 }
Gilles Peskine7db8e892022-09-20 18:31:30 +0200190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine063700d2022-04-13 23:59:52 +0200192 /* We've already recorded the test as having failed. Don't
193 * overwrite any previous information about the failure. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200195 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 mbedtls_test_fail(test, line_no, filename);
197 (void) mbedtls_snprintf(mbedtls_test_info.line1,
198 sizeof(mbedtls_test_info.line1),
199 "lhs = 0x%016llx = %lld",
200 (unsigned long long) value1, value1);
201 (void) mbedtls_snprintf(mbedtls_test_info.line2,
202 sizeof(mbedtls_test_info.line2),
203 "rhs = 0x%016llx = %lld",
204 (unsigned long long) value2, value2);
205 return 0;
Gilles Peskine063700d2022-04-13 23:59:52 +0200206}
207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208int mbedtls_test_unhexify(unsigned char *obuf,
209 size_t obufmax,
210 const char *ibuf,
211 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200212{
213 unsigned char uc, uc2;
214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200216
217 /* Must be even number of bytes. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 if ((*len) & 1) {
219 return -1;
220 }
Ronald Crona0c25392020-06-18 10:10:46 +0200221 *len /= 2;
222
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 if ((*len) > obufmax) {
224 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200225 }
226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 while (*ibuf != 0) {
228 if (ascii2uc(*(ibuf++), &uc) != 0) {
229 return -1;
230 }
231
232 if (ascii2uc(*(ibuf++), &uc2) != 0) {
233 return -1;
234 }
235
236 *(obuf++) = (uc << 4) | uc2;
237 }
238
239 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200240}
241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242void mbedtls_test_hexify(unsigned char *obuf,
243 const unsigned char *ibuf,
244 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200245{
246 unsigned char l, h;
247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200249 h = *ibuf / 16;
250 l = *ibuf % 16;
251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200253 *obuf++ = '0' + h;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200255 *obuf++ = 'a' + h - 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200259 *obuf++ = '0' + l;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200261 *obuf++ = 'a' + l - 10;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200263
264 ++ibuf;
265 len--;
266 }
267}
268
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200270{
271 void *p;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200273
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 p = mbedtls_calloc(1, actual_len);
275 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200278
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200280}
281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200283{
284 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200285 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200286
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 if (*olen == 0) {
290 return mbedtls_test_zero_alloc(*olen);
291 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200292
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 obuf = mbedtls_calloc(1, *olen);
294 TEST_HELPER_ASSERT(obuf != NULL);
295 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200298}
299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
301 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200302{
303 int ret = 0;
304 uint32_t i = 0;
305
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 if (a_len != b_len) {
307 return -1;
308 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200309
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 for (i = 0; i < a_len; i++) {
311 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200312 ret = -1;
313 break;
314 }
315 }
316 return ret;
317}
Ronald Crona1236142020-07-01 16:01:21 +0200318
319#if defined(MBEDTLS_CHECK_PARAMS)
320void mbedtls_test_param_failed_get_location_record(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 mbedtls_test_param_failed_location_record_t *location_record)
Ronald Crona1236142020-07-01 16:01:21 +0200322{
323 *location_record = param_failed_ctx.location_record;
324}
325
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326void mbedtls_test_param_failed_expect_call(void)
Ronald Crona1236142020-07-01 16:01:21 +0200327{
328 param_failed_ctx.expected_call_happened = 0;
329 param_failed_ctx.expected_call = 1;
330}
331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332int mbedtls_test_param_failed_check_expected_call(void)
Ronald Crona1236142020-07-01 16:01:21 +0200333{
334 param_failed_ctx.expected_call = 0;
335
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 if (param_failed_ctx.expected_call_happened != 0) {
337 return 0;
338 }
Ronald Crona1236142020-07-01 16:01:21 +0200339
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 return -1;
Ronald Crona1236142020-07-01 16:01:21 +0200341}
342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343void *mbedtls_test_param_failed_get_state_buf(void)
Ronald Crona1236142020-07-01 16:01:21 +0200344{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200345 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200346}
347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348void mbedtls_test_param_failed_reset_state(void)
Ronald Crona1236142020-07-01 16:01:21 +0200349{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 memset(param_failed_ctx.state, 0, sizeof(param_failed_ctx.state));
Ronald Crona1236142020-07-01 16:01:21 +0200351}
352
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100353void mbedtls_param_failed(const char *failure_condition,
354 const char *file,
355 int line)
Ronald Crona1236142020-07-01 16:01:21 +0200356{
357 /* Record the location of the failure */
358 param_failed_ctx.location_record.failure_condition = failure_condition;
359 param_failed_ctx.location_record.file = file;
360 param_failed_ctx.location_record.line = line;
361
362 /* If we are testing the callback function... */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 if (param_failed_ctx.expected_call != 0) {
Ronald Crona1236142020-07-01 16:01:21 +0200364 param_failed_ctx.expected_call = 0;
365 param_failed_ctx.expected_call_happened = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 } else {
Ronald Crona1236142020-07-01 16:01:21 +0200367 /* ...else try a long jump. If the execution state has not been set-up
368 * or reset then the long jump buffer is all zero's and the call will
369 * with high probability fault, emphasizing there is something to look
370 * at.
371 */
372
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100373 longjmp(param_failed_ctx.state, 1);
Ronald Crona1236142020-07-01 16:01:21 +0200374 }
375}
376#endif /* MBEDTLS_CHECK_PARAMS */
Chris Jones96ae73b2021-01-08 17:04:59 +0000377
378#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100379void mbedtls_test_err_add_check(int high, int low,
380 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000381{
Chris Jones3f613c12021-03-31 09:34:22 +0100382 /* Error codes are always negative (a value of zero is a success) however
383 * their positive opposites can be easier to understand. The following
384 * examples given in comments have been made positive for ease of
385 * understanding. The structure of an error code is such:
386 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100387 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100388 *
389 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100390 * h = high level error code (includes high level module ID (bits 12..14)
391 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100392 * l = low level error code.
393 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394 if (high > -0x1000 && high != 0) {
395 /* high < 0001000000000000
396 * No high level module ID bits are set.
397 */
398 mbedtls_test_fail("'high' is not a high-level error code",
399 line, file);
400 } else if (high < -0x7F80) {
401 /* high > 0111111110000000
402 * Error code is greater than the largest allowed high level module ID.
403 */
404 mbedtls_test_fail("'high' error code is greater than 15 bits",
405 line, file);
406 } else if ((high & 0x7F) != 0) {
407 /* high & 0000000001111111
408 * Error code contains low level error code bits.
409 */
410 mbedtls_test_fail("'high' contains a low-level error code",
411 line, file);
412 } else if (low < -0x007F) {
413 /* low > 0000000001111111
414 * Error code contains high or module level error code bits.
415 */
416 mbedtls_test_fail("'low' error code is greater than 7 bits",
417 line, file);
418 } else if (low > 0) {
419 mbedtls_test_fail("'low' error code is greater than zero",
420 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000421 }
422}
423#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskinedb479712021-06-11 14:13:53 +0200424
425#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s)
Gilles Peskinedb479712021-06-11 14:13:53 +0200427{
Gilles Peskine53a72062022-11-09 21:08:44 +0100428 int negative = 0;
429 /* Always set the sign bit to -1 if the input has a minus sign, even for 0.
430 * This creates an invalid representation, which mbedtls_mpi_read_string()
431 * avoids but we want to be able to create that in test data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 if (s[0] == '-') {
Gilles Peskine53a72062022-11-09 21:08:44 +0100433 ++s;
434 negative = 1;
435 }
Gilles Peskinedb479712021-06-11 14:13:53 +0200436 /* mbedtls_mpi_read_string() currently retains leading zeros.
437 * It always allocates at least one limb for the value 0. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 if (s[0] == 0) {
439 mbedtls_mpi_free(X);
440 return 0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200441 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 int ret = mbedtls_mpi_read_string(X, 16, s);
443 if (ret != 0) {
444 return ret;
445 }
446 if (negative) {
447 if (mbedtls_mpi_cmp_int(X, 0) == 0) {
Gilles Peskine53a72062022-11-09 21:08:44 +0100448 ++mbedtls_test_case_uses_negative_0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 }
Gilles Peskine53a72062022-11-09 21:08:44 +0100450 X->s = -1;
451 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 return 0;
Gilles Peskinedb479712021-06-11 14:13:53 +0200453}
454#endif