blob: 36564fe29a2348a34c26065181865c05a860be39 [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
David Horstmann83ece2f2023-12-18 15:30:46 +000016#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C)
David Horstmannb4892572023-12-14 14:17:04 +000017#include <test/psa_memory_poisoning_wrappers.h>
18#endif
19
Ronald Crona1236142020-07-01 16:01:21 +020020/*----------------------------------------------------------------------------*/
21/* Static global variables */
22
Ronald Cronf40529d2020-06-09 16:27:37 +020023#if defined(MBEDTLS_PLATFORM_C)
24static mbedtls_platform_context platform_ctx;
25#endif
26
Chris Jonese60e2ae2021-01-20 17:51:47 +000027mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000028
Ronald Crona1236142020-07-01 16:01:21 +020029/*----------------------------------------------------------------------------*/
30/* Helper Functions */
31
Gilles Peskine449bd832023-01-11 14:50:10 +010032int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020033{
34 int ret = 0;
Gilles Peskinec2d16b22023-04-28 23:39:45 +020035
David Horstmann66684532023-12-15 18:29:54 +000036#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
David Horstmann2de5abf2023-12-20 11:26:40 +000037 && defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) \
David Horstmann66684532023-12-15 18:29:54 +000038 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannb4892572023-12-14 14:17:04 +000039 mbedtls_poison_test_hooks_setup();
40#endif
41
Gilles Peskinec2d16b22023-04-28 23:39:45 +020042#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
43 /* Make sure that injected entropy is present. Otherwise
44 * psa_crypto_init() will fail. This is not necessary for test suites
45 * that don't use PSA, but it's harmless (except for leaving a file
46 * behind). */
47 ret = mbedtls_test_inject_entropy_restore();
48 if (ret != 0) {
49 return ret;
50 }
51#endif
52
Ronald Cronf40529d2020-06-09 16:27:37 +020053#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010054 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020055#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskinec2d16b22023-04-28 23:39:45 +020056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020058}
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020061{
David Horstmann66684532023-12-15 18:29:54 +000062#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
David Horstmann2de5abf2023-12-20 11:26:40 +000063 && defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) \
David Horstmann66684532023-12-15 18:29:54 +000064 && defined(MBEDTLS_TEST_MEMORY_CAN_POISON)
David Horstmannb4892572023-12-14 14:17:04 +000065 mbedtls_poison_test_hooks_teardown();
66#endif
67
Ronald Cronf40529d2020-06-09 16:27:37 +020068#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020070#endif /* MBEDTLS_PLATFORM_C */
71}
72
Gilles Peskine881447d2022-12-08 15:24:52 +010073int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020074{
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020076 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +010077 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +020078 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010079 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020080 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010081 } else {
82 return -1;
83 }
Ronald Crona0c25392020-06-18 10:10:46 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +020086}
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000089{
Gilles Peskine449bd832023-01-11 14:50:10 +010090 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +000091 /* We've already recorded the test as having failed. Don't
92 * overwrite any previous information about the failure. */
93 return;
94 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000095 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
96 mbedtls_test_info.test = test;
97 mbedtls_test_info.line_no = line_no;
98 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000099}
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +0000102{
Chris Jonese60e2ae2021-01-20 17:51:47 +0000103 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
104 mbedtls_test_info.test = test;
105 mbedtls_test_info.line_no = line_no;
106 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000107}
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000110{
111 mbedtls_test_info.step = step;
112}
113
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100114#if defined(MBEDTLS_BIGNUM_C)
115unsigned mbedtls_test_case_uses_negative_0 = 0;
116#endif
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +0000119{
120 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000122 mbedtls_test_info.test = 0;
123 mbedtls_test_info.line_no = 0;
124 mbedtls_test_info.filename = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
126 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100127#if defined(MBEDTLS_BIGNUM_C)
128 mbedtls_test_case_uses_negative_0 = 0;
129#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200130}
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132int mbedtls_test_equal(const char *test, int line_no, const char *filename,
133 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200134{
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 TEST_CF_PUBLIC(&value1, sizeof(value1));
136 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (value1 == value2) {
139 return 1;
140 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200143 /* We've already recorded the test as having failed. Don't
144 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200146 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_test_fail(test, line_no, filename);
148 (void) mbedtls_snprintf(mbedtls_test_info.line1,
149 sizeof(mbedtls_test_info.line1),
150 "lhs = 0x%016llx = %lld",
151 value1, (long long) value1);
152 (void) mbedtls_snprintf(mbedtls_test_info.line2,
153 sizeof(mbedtls_test_info.line2),
154 "rhs = 0x%016llx = %lld",
155 value2, (long long) value2);
156 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000157}
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
160 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200161{
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 TEST_CF_PUBLIC(&value1, sizeof(value1));
163 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (value1 <= value2) {
166 return 1;
167 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200170 /* We've already recorded the test as having failed. Don't
171 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200173 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_test_fail(test, line_no, filename);
175 (void) mbedtls_snprintf(mbedtls_test_info.line1,
176 sizeof(mbedtls_test_info.line1),
177 "lhs = 0x%016llx = %llu",
178 value1, value1);
179 (void) mbedtls_snprintf(mbedtls_test_info.line2,
180 sizeof(mbedtls_test_info.line2),
181 "rhs = 0x%016llx = %llu",
182 value2, value2);
183 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200184}
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
187 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200188{
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_CF_PUBLIC(&value1, sizeof(value1));
190 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (value1 <= value2) {
193 return 1;
194 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200197 /* We've already recorded the test as having failed. Don't
198 * overwrite any previous information about the failure. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200200 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 mbedtls_test_fail(test, line_no, filename);
202 (void) mbedtls_snprintf(mbedtls_test_info.line1,
203 sizeof(mbedtls_test_info.line1),
204 "lhs = 0x%016llx = %lld",
205 (unsigned long long) value1, value1);
206 (void) mbedtls_snprintf(mbedtls_test_info.line2,
207 sizeof(mbedtls_test_info.line2),
208 "rhs = 0x%016llx = %lld",
209 (unsigned long long) value2, value2);
210 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200211}
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213int mbedtls_test_unhexify(unsigned char *obuf,
214 size_t obufmax,
215 const char *ibuf,
216 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200217{
218 unsigned char uc, uc2;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200221
222 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 if ((*len) & 1) {
224 return -1;
225 }
Ronald Crona0c25392020-06-18 10:10:46 +0200226 *len /= 2;
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if ((*len) > obufmax) {
229 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200230 }
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 while (*ibuf != 0) {
233 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
234 return -1;
235 }
236
237 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
238 return -1;
239 }
240
241 *(obuf++) = (uc << 4) | uc2;
242 }
243
244 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200245}
246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247void mbedtls_test_hexify(unsigned char *obuf,
248 const unsigned char *ibuf,
249 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200250{
251 unsigned char l, h;
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200254 h = *ibuf / 16;
255 l = *ibuf % 16;
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200258 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200260 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200264 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200266 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200268
269 ++ibuf;
270 len--;
271 }
272}
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200275{
276 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 p = mbedtls_calloc(1, actual_len);
280 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200285}
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200288{
289 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200290 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (*olen == 0) {
295 return mbedtls_test_zero_alloc(*olen);
296 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 obuf = mbedtls_calloc(1, *olen);
299 TEST_HELPER_ASSERT(obuf != NULL);
300 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200303}
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
306 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200307{
308 int ret = 0;
309 uint32_t i = 0;
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if (a_len != b_len) {
312 return -1;
313 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 for (i = 0; i < a_len; i++) {
316 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200317 ret = -1;
318 break;
319 }
320 }
321 return ret;
322}
Ronald Crona1236142020-07-01 16:01:21 +0200323
Chris Jones96ae73b2021-01-08 17:04:59 +0000324#if defined(MBEDTLS_TEST_HOOKS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100325void mbedtls_test_err_add_check(int high, int low,
326 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000327{
Chris Jones3f613c12021-03-31 09:34:22 +0100328 /* Error codes are always negative (a value of zero is a success) however
329 * their positive opposites can be easier to understand. The following
330 * examples given in comments have been made positive for ease of
331 * understanding. The structure of an error code is such:
332 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100333 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100334 *
335 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100336 * h = high level error code (includes high level module ID (bits 12..14)
337 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100338 * l = low level error code.
339 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (high > -0x1000 && high != 0) {
341 /* high < 0001000000000000
342 * No high level module ID bits are set.
343 */
344 mbedtls_test_fail("'high' is not a high-level error code",
345 line, file);
346 } else if (high < -0x7F80) {
347 /* high > 0111111110000000
348 * Error code is greater than the largest allowed high level module ID.
349 */
350 mbedtls_test_fail("'high' error code is greater than 15 bits",
351 line, file);
352 } else if ((high & 0x7F) != 0) {
353 /* high & 0000000001111111
354 * Error code contains low level error code bits.
355 */
356 mbedtls_test_fail("'high' contains a low-level error code",
357 line, file);
358 } else if (low < -0x007F) {
359 /* low > 0000000001111111
360 * Error code contains high or module level error code bits.
361 */
362 mbedtls_test_fail("'low' error code is greater than 7 bits",
363 line, file);
364 } else if (low > 0) {
365 mbedtls_test_fail("'low' error code is greater than zero",
366 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000367 }
368}
369#endif /* MBEDTLS_TEST_HOOKS */