blob: 03a8fa72855a2ef463aa61b344c5c72dc5f26e7b [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
Paul Elliottc7a1e992023-11-03 18:44:57 +0000112#if defined(MBEDTLS_BIGNUM_C)
113
114unsigned mbedtls_test_get_case_uses_negative_0(void)
115{
116 return mbedtls_test_info.case_uses_negative_0;
117}
118
119void mbedtls_test_set_case_uses_negative_0(unsigned uses)
120{
121 mbedtls_test_info.case_uses_negative_0 = uses;
122}
123
124void mbedtls_test_increment_case_uses_negative_0(void)
125{
126 ++mbedtls_test_info.case_uses_negative_0;
127}
128
129#endif
130
Paul Elliott4580d4d2023-10-27 18:41:02 +0100131/*----------------------------------------------------------------------------*/
Ronald Crona1236142020-07-01 16:01:21 +0200132/* Helper Functions */
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200135{
136 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200137
138#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
139 /* Make sure that injected entropy is present. Otherwise
140 * psa_crypto_init() will fail. This is not necessary for test suites
141 * that don't use PSA, but it's harmless (except for leaving a file
142 * behind). */
143 ret = mbedtls_test_inject_entropy_restore();
144 if (ret != 0) {
145 return ret;
146 }
147#endif
148
Ronald Cronf40529d2020-06-09 16:27:37 +0200149#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200151#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +0200154}
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +0200157{
158#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +0200160#endif /* MBEDTLS_PLATFORM_C */
161}
162
Gilles Peskine881447d2022-12-08 15:24:52 +0100163int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +0200164{
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200166 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200168 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +0200170 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 } else {
172 return -1;
173 }
Ronald Crona0c25392020-06-18 10:10:46 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +0200176}
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000179{
Paul Elliott5c498f32023-10-31 16:38:56 +0000180 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +0000181 /* We've already recorded the test as having failed. Don't
182 * overwrite any previous information about the failure. */
183 return;
184 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000185 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename);
Chris Jones9634bb12021-01-20 15:56:42 +0000186}
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000189{
Paul Elliott5c498f32023-10-31 16:38:56 +0000190 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000191}
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000194{
Paul Elliott5c498f32023-10-31 16:38:56 +0000195 mbedtls_test_set_result(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0);
196 mbedtls_test_set_step((unsigned long) (-1));
197 mbedtls_test_set_line1(NULL);
198 mbedtls_test_set_line2(NULL);
199
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100200#if defined(MBEDTLS_BIGNUM_C)
Paul Elliottc7a1e992023-11-03 18:44:57 +0000201 mbedtls_test_set_case_uses_negative_0(0);
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100202#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200203}
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205int mbedtls_test_equal(const char *test, int line_no, const char *filename,
206 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200207{
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 TEST_CF_PUBLIC(&value1, sizeof(value1));
209 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (value1 == value2) {
212 return 1;
213 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200214
Paul Elliott5c498f32023-10-31 16:38:56 +0000215 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200216 /* We've already recorded the test as having failed. Don't
217 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200219 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000220 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000222 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 "lhs = 0x%016llx = %lld",
224 value1, (long long) value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000225 mbedtls_test_set_line1(buf);
226 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 "rhs = 0x%016llx = %lld",
228 value2, (long long) value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000229 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000231}
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
234 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200235{
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 TEST_CF_PUBLIC(&value1, sizeof(value1));
237 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (value1 <= value2) {
240 return 1;
241 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200242
Paul Elliott5c498f32023-10-31 16:38:56 +0000243 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200244 /* We've already recorded the test as having failed. Don't
245 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200247 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000248 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000250 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 "lhs = 0x%016llx = %llu",
252 value1, value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000253 mbedtls_test_set_line1(buf);
254 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 "rhs = 0x%016llx = %llu",
256 value2, value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000257 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200259}
260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
262 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200263{
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 TEST_CF_PUBLIC(&value1, sizeof(value1));
265 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (value1 <= value2) {
268 return 1;
269 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200270
Paul Elliott5c498f32023-10-31 16:38:56 +0000271 if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200272 /* We've already recorded the test as having failed. Don't
273 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200275 }
Paul Elliott5c498f32023-10-31 16:38:56 +0000276 char buf[MBEDTLS_TEST_LINE_LENGTH];
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_test_fail(test, line_no, filename);
Paul Elliott5c498f32023-10-31 16:38:56 +0000278 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 "lhs = 0x%016llx = %lld",
280 (unsigned long long) value1, value1);
Paul Elliott5c498f32023-10-31 16:38:56 +0000281 mbedtls_test_set_line1(buf);
282 (void) mbedtls_snprintf(buf, sizeof(buf),
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 "rhs = 0x%016llx = %lld",
284 (unsigned long long) value2, value2);
Paul Elliott5c498f32023-10-31 16:38:56 +0000285 mbedtls_test_set_line2(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200287}
288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_test_unhexify(unsigned char *obuf,
290 size_t obufmax,
291 const char *ibuf,
292 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200293{
294 unsigned char uc, uc2;
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200297
298 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if ((*len) & 1) {
300 return -1;
301 }
Ronald Crona0c25392020-06-18 10:10:46 +0200302 *len /= 2;
303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if ((*len) > obufmax) {
305 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200306 }
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 while (*ibuf != 0) {
309 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
310 return -1;
311 }
312
313 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
314 return -1;
315 }
316
317 *(obuf++) = (uc << 4) | uc2;
318 }
319
320 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200321}
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323void mbedtls_test_hexify(unsigned char *obuf,
324 const unsigned char *ibuf,
325 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200326{
327 unsigned char l, h;
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200330 h = *ibuf / 16;
331 l = *ibuf % 16;
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200334 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200336 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200340 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200342 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200344
345 ++ibuf;
346 len--;
347 }
348}
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200351{
352 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 p = mbedtls_calloc(1, actual_len);
356 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200361}
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200364{
365 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200366 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (*olen == 0) {
371 return mbedtls_test_zero_alloc(*olen);
372 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 obuf = mbedtls_calloc(1, *olen);
375 TEST_HELPER_ASSERT(obuf != NULL);
376 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200379}
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
382 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200383{
384 int ret = 0;
385 uint32_t i = 0;
386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (a_len != b_len) {
388 return -1;
389 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 for (i = 0; i < a_len; i++) {
392 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200393 ret = -1;
394 break;
395 }
396 }
397 return ret;
398}
Ronald Crona1236142020-07-01 16:01:21 +0200399
Chris Jones96ae73b2021-01-08 17:04:59 +0000400#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 */