blob: 52785fc01a79ae78e5474e767f46000a3516cde1 [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
Ronald Crona1236142020-07-01 16:01:21 +020016/*----------------------------------------------------------------------------*/
17/* Static global variables */
18
Ronald Cronf40529d2020-06-09 16:27:37 +020019#if defined(MBEDTLS_PLATFORM_C)
20static mbedtls_platform_context platform_ctx;
21#endif
22
Chris Jonese60e2ae2021-01-20 17:51:47 +000023mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000024
Ronald Crona1236142020-07-01 16:01:21 +020025/*----------------------------------------------------------------------------*/
Paul Elliott4580d4d2023-10-27 18:41:02 +010026/* Mbedtls Test Info accessors */
27
28mbedtls_test_result_t mbedtls_test_get_result(void)
29{
30 return mbedtls_test_info.result;
31}
32
Paul Elliott5c498f32023-10-31 16:38:56 +000033void mbedtls_test_set_result(mbedtls_test_result_t result, const char *test,
34 int line_no, const char *filename)
35{
36 mbedtls_test_info.result = result;
37 mbedtls_test_info.test = test;
38 mbedtls_test_info.line_no = line_no;
39 mbedtls_test_info.filename = filename;
40}
41
Paul Elliott4580d4d2023-10-27 18:41:02 +010042const char *mbedtls_test_get_test(void)
43{
44 return mbedtls_test_info.test;
45}
46const char *mbedtls_get_test_filename(void)
47{
48 return mbedtls_test_info.filename;
49}
50
51int mbedtls_test_get_line_no(void)
52{
53 return mbedtls_test_info.line_no;
54}
55
56void mbedtls_test_increment_step(void)
57{
58 ++mbedtls_test_info.step;
59}
60
61unsigned long mbedtls_test_get_step(void)
62{
63 return mbedtls_test_info.step;
64}
65
Paul Elliott5c498f32023-10-31 16:38:56 +000066void mbedtls_test_set_step(unsigned long step) {
67 mbedtls_test_info.step = step;
68}
69
Paul Elliott4580d4d2023-10-27 18:41:02 +010070const char *mbedtls_test_get_line1(void)
71{
72 return mbedtls_test_info.line1;
73}
Paul Elliott5c498f32023-10-31 16:38:56 +000074
75void mbedtls_test_set_line1(const char *line)
76{
77 if (line == NULL) {
78 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
79 } else {
80 strncpy(mbedtls_test_info.line1, line, sizeof(mbedtls_test_info.line1));
81 }
82}
83
Paul Elliott4580d4d2023-10-27 18:41:02 +010084const char *mbedtls_test_get_line2(void)
85{
86 return mbedtls_test_info.line2;
87}
88
Paul Elliott5c498f32023-10-31 16:38:56 +000089void mbedtls_test_set_line2(const char *line) {
90 if (line == NULL) {
91 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
92 } else {
93 strncpy(mbedtls_test_info.line2, line, sizeof(mbedtls_test_info.line2));
94 }
95}
96
97
Paul Elliott4580d4d2023-10-27 18:41:02 +010098#if defined(MBEDTLS_TEST_MUTEX_USAGE)
99const char *mbedtls_test_get_mutex_usage_error(void)
100{
101 return mbedtls_test_info.mutex_usage_error;
102}
103
104void mbedtls_test_set_mutex_usage_error(const char *msg)
105{
106 if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
107 mbedtls_test_info.mutex_usage_error = msg;
108 }
109}
110#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
111
112/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200113/* Helper Functions */
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200116{
117 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200118
119#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
120 /* Make sure that injected entropy is present. Otherwise
121 * psa_crypto_init() will fail. This is not necessary for test suites
122 * that don't use PSA, but it's harmless (except for leaving a file
123 * behind). */
124 ret = mbedtls_test_inject_entropy_restore();
125 if (ret != 0) {
126 return ret;
127 }
128#endif
129
Ronald Cronf40529d2020-06-09 16:27:37 +0200130#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200132#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200135}
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200138{
139#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200141#endif /* MBEDTLS_PLATFORM_C */
142}
143
Gilles Peskine881447d2022-12-08 15:24:52 +0100144int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200145{
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200147 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200149 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200151 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 } else {
153 return -1;
154 }
Ronald Crona0c25392020-06-18 10:10:46 +0200155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200157}
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000160{
Paul Elliott5c498f32023-10-31 16:38:56 +0000161 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +0000162 /* We've already recorded the test as having failed. Don't
163 * overwrite any previous information about the failure. */
164 return;
165 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000166 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000167}
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000170{
Paul Elliott5c498f32023-10-31 16:38:56 +0000171 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000172}
173
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100174#if defined(MBEDTLS_BIGNUM_C)
175unsigned mbedtls_test_case_uses_negative_0 = 0;
176#endif
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000179{
Paul Elliott5c498f32023-10-31 16:38:56 +0000180 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
181 mbedtls_test_set_step((unsigned long) (-1));
182 mbedtls_test_set_line1(NULL);
183 mbedtls_test_set_line2(NULL);
184
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100185#if defined(MBEDTLS_BIGNUM_C)
186 mbedtls_test_case_uses_negative_0 = 0;
187#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200188}
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190int mbedtls_test_equal(const char *test, int line_no, const char *filename,
191 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200192{
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 TEST_CF_PUBLIC(&value1, sizeof(value1));
194 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (value1 == value2) {
197 return 1;
198 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200199
Paul Elliott5c498f32023-10-31 16:38:56 +0000200 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200201 /* We've already recorded the test as having failed. Don't
202 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200204 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000205 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000207 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 "lhs = 0x%016llx = %lld",
209 value1, (long long) value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000210 mbedtls_test_set_line1(buf);
211 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 "rhs = 0x%016llx = %lld",
213 value2, (long long) value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000214 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000216}
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
219 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200220{
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 TEST_CF_PUBLIC(&value1, sizeof(value1));
222 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (value1 <= value2) {
225 return 1;
226 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200227
Paul Elliott5c498f32023-10-31 16:38:56 +0000228 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200229 /* We've already recorded the test as having failed. Don't
230 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200232 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000233 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000235 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 "lhs = 0x%016llx = %llu",
237 value1, value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000238 mbedtls_test_set_line1(buf);
239 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 "rhs = 0x%016llx = %llu",
241 value2, value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000242 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200244}
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
247 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200248{
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 TEST_CF_PUBLIC(&value1, sizeof(value1));
250 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (value1 <= value2) {
253 return 1;
254 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200255
Paul Elliott5c498f32023-10-31 16:38:56 +0000256 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200257 /* We've already recorded the test as having failed. Don't
258 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200260 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000261 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000263 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 "lhs = 0x%016llx = %lld",
265 (unsigned long long) value1, value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000266 mbedtls_test_set_line1(buf);
267 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 "rhs = 0x%016llx = %lld",
269 (unsigned long long) value2, value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000270 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200272}
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274int mbedtls_test_unhexify(unsigned char *obuf,
275 size_t obufmax,
276 const char *ibuf,
277 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200278{
279 unsigned char uc, uc2;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200282
283 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if ((*len) & 1) {
285 return -1;
286 }
Ronald Crona0c25392020-06-18 10:10:46 +0200287 *len /= 2;
288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if ((*len) > obufmax) {
290 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200291 }
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 while (*ibuf != 0) {
294 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
295 return -1;
296 }
297
298 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
299 return -1;
300 }
301
302 *(obuf++) = (uc << 4) | uc2;
303 }
304
305 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200306}
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308void mbedtls_test_hexify(unsigned char *obuf,
309 const unsigned char *ibuf,
310 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200311{
312 unsigned char l, h;
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200315 h = *ibuf / 16;
316 l = *ibuf % 16;
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200319 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200321 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200325 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200327 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200329
330 ++ibuf;
331 len--;
332 }
333}
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200336{
337 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 p = mbedtls_calloc(1, actual_len);
341 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200346}
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200349{
350 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200351 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (*olen == 0) {
356 return mbedtls_test_zero_alloc(*olen);
357 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 obuf = mbedtls_calloc(1, *olen);
360 TEST_HELPER_ASSERT(obuf != NULL);
361 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200364}
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
367 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200368{
369 int ret = 0;
370 uint32_t i = 0;
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (a_len != b_len) {
373 return -1;
374 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 for (i = 0; i < a_len; i++) {
377 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200378 ret = -1;
379 break;
380 }
381 }
382 return ret;
383}
Ronald Crona1236142020-07-01 16:01:21 +0200384
Chris Jones96ae73b2021-01-08 17:04:59 +0000385#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100386void mbedtls_test_err_add_check(int high, int low,
387 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000388{
Chris Jones3f613c12021-03-31 09:34:22 +0100389 /* Error codes are always negative (a value of zero is a success) however
390 * their positive opposites can be easier to understand. The following
391 * examples given in comments have been made positive for ease of
392 * understanding. The structure of an error code is such:
393 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100394 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100395 *
396 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100397 * h = high level error code (includes high level module ID (bits 12..14)
398 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100399 * l = low level error code.
400 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (high > -0x1000 && high != 0) {
402 /* high < 0001000000000000
403 * No high level module ID bits are set.
404 */
405 mbedtls_test_fail("'high' is not a high-level error code",
406 line, file);
407 } else if (high < -0x7F80) {
408 /* high > 0111111110000000
409 * Error code is greater than the largest allowed high level module ID.
410 */
411 mbedtls_test_fail("'high' error code is greater than 15 bits",
412 line, file);
413 } else if ((high & 0x7F) != 0) {
414 /* high & 0000000001111111
415 * Error code contains low level error code bits.
416 */
417 mbedtls_test_fail("'high' contains a low-level error code",
418 line, file);
419 } else if (low < -0x007F) {
420 /* low > 0000000001111111
421 * Error code contains high or module level error code bits.
422 */
423 mbedtls_test_fail("'low' error code is greater than 7 bits",
424 line, file);
425 } else if (low > 0) {
426 mbedtls_test_fail("'low' error code is greater than zero",
427 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000428 }
429}
430#endif /* MBEDTLS_TEST_HOOKS */