blob: 30fd362c01f59a6ab7fd83c032d5f1e88acaa0b5 [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 Peskinebdc7b8b2022-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/*----------------------------------------------------------------------------*/
24/* Static global variables */
25
Ronald Cronf40529d2020-06-09 16:27:37 +020026#if defined(MBEDTLS_PLATFORM_C)
27static mbedtls_platform_context platform_ctx;
28#endif
29
Chris Jonese60e2ae2021-01-20 17:51:47 +000030mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000031
Ronald Crona1236142020-07-01 16:01:21 +020032/*----------------------------------------------------------------------------*/
33/* Helper Functions */
34
David Horstmann71159f42023-01-03 12:51:59 +000035int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020036{
37 int ret = 0;
38#if defined(MBEDTLS_PLATFORM_C)
David Horstmann71159f42023-01-03 12:51:59 +000039 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020040#endif /* MBEDTLS_PLATFORM_C */
David Horstmann71159f42023-01-03 12:51:59 +000041 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020042}
43
David Horstmann71159f42023-01-03 12:51:59 +000044void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020045{
46#if defined(MBEDTLS_PLATFORM_C)
David Horstmann71159f42023-01-03 12:51:59 +000047 mbedtls_platform_teardown(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020048#endif /* MBEDTLS_PLATFORM_C */
49}
50
Gilles Peskine881447d2022-12-08 15:24:52 +010051int mbedtls_test_ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020052{
David Horstmann71159f42023-01-03 12:51:59 +000053 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020054 *uc = c - '0';
David Horstmann71159f42023-01-03 12:51:59 +000055 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +020056 *uc = c - 'a' + 10;
David Horstmann71159f42023-01-03 12:51:59 +000057 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020058 *uc = c - 'A' + 10;
David Horstmann71159f42023-01-03 12:51:59 +000059 } else {
60 return -1;
61 }
Ronald Crona0c25392020-06-18 10:10:46 +020062
David Horstmann71159f42023-01-03 12:51:59 +000063 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +020064}
65
David Horstmann71159f42023-01-03 12:51:59 +000066void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000067{
David Horstmann71159f42023-01-03 12:51:59 +000068 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Chris Jones9634bb12021-01-20 15:56:42 +000069 /* We've already recorded the test as having failed. Don't
70 * overwrite any previous information about the failure. */
71 return;
72 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000073 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
74 mbedtls_test_info.test = test;
75 mbedtls_test_info.line_no = line_no;
76 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000077}
78
David Horstmann71159f42023-01-03 12:51:59 +000079void mbedtls_test_skip(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000080{
Chris Jonese60e2ae2021-01-20 17:51:47 +000081 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
82 mbedtls_test_info.test = test;
83 mbedtls_test_info.line_no = line_no;
84 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000085}
86
David Horstmann71159f42023-01-03 12:51:59 +000087void mbedtls_test_set_step(unsigned long step)
Chris Jonesa5ab7652021-02-02 16:20:45 +000088{
89 mbedtls_test_info.step = step;
90}
91
Gilles Peskineca6e8aa2022-11-09 21:08:44 +010092#if defined(MBEDTLS_BIGNUM_C)
93unsigned mbedtls_test_case_uses_negative_0 = 0;
94#endif
95
David Horstmann71159f42023-01-03 12:51:59 +000096void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +000097{
98 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
David Horstmann71159f42023-01-03 12:51:59 +000099 mbedtls_test_info.step = (unsigned long) (-1);
Chris Jonesa5ab7652021-02-02 16:20:45 +0000100 mbedtls_test_info.test = 0;
101 mbedtls_test_info.line_no = 0;
102 mbedtls_test_info.filename = 0;
David Horstmann71159f42023-01-03 12:51:59 +0000103 memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1));
104 memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2));
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100105#if defined(MBEDTLS_BIGNUM_C)
106 mbedtls_test_case_uses_negative_0 = 0;
107#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200108}
109
David Horstmann71159f42023-01-03 12:51:59 +0000110int mbedtls_test_equal(const char *test, int line_no, const char *filename,
111 unsigned long long value1, unsigned long long value2)
Gilles Peskine89615ee2021-04-29 20:28:54 +0200112{
David Horstmann71159f42023-01-03 12:51:59 +0000113 TEST_CF_PUBLIC(&value1, sizeof(value1));
114 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200115
David Horstmann71159f42023-01-03 12:51:59 +0000116 if (value1 == value2) {
117 return 1;
118 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200119
David Horstmann71159f42023-01-03 12:51:59 +0000120 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskine89615ee2021-04-29 20:28:54 +0200121 /* We've already recorded the test as having failed. Don't
122 * overwrite any previous information about the failure. */
David Horstmann71159f42023-01-03 12:51:59 +0000123 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200124 }
David Horstmann71159f42023-01-03 12:51:59 +0000125 mbedtls_test_fail(test, line_no, filename);
126 (void) mbedtls_snprintf(mbedtls_test_info.line1,
127 sizeof(mbedtls_test_info.line1),
128 "lhs = 0x%016llx = %lld",
129 value1, (long long) value1);
130 (void) mbedtls_snprintf(mbedtls_test_info.line2,
131 sizeof(mbedtls_test_info.line2),
132 "rhs = 0x%016llx = %lld",
133 value2, (long long) value2);
134 return 0;
Chris Jonesa5ab7652021-02-02 16:20:45 +0000135}
136
David Horstmann71159f42023-01-03 12:51:59 +0000137int mbedtls_test_le_u(const char *test, int line_no, const char *filename,
138 unsigned long long value1, unsigned long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200139{
David Horstmann71159f42023-01-03 12:51:59 +0000140 TEST_CF_PUBLIC(&value1, sizeof(value1));
141 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200142
David Horstmann71159f42023-01-03 12:51:59 +0000143 if (value1 <= value2) {
144 return 1;
145 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200146
David Horstmann71159f42023-01-03 12:51:59 +0000147 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200148 /* We've already recorded the test as having failed. Don't
149 * overwrite any previous information about the failure. */
David Horstmann71159f42023-01-03 12:51:59 +0000150 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200151 }
David Horstmann71159f42023-01-03 12:51:59 +0000152 mbedtls_test_fail(test, line_no, filename);
153 (void) mbedtls_snprintf(mbedtls_test_info.line1,
154 sizeof(mbedtls_test_info.line1),
155 "lhs = 0x%016llx = %llu",
156 value1, value1);
157 (void) mbedtls_snprintf(mbedtls_test_info.line2,
158 sizeof(mbedtls_test_info.line2),
159 "rhs = 0x%016llx = %llu",
160 value2, value2);
161 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200162}
163
David Horstmann71159f42023-01-03 12:51:59 +0000164int mbedtls_test_le_s(const char *test, int line_no, const char *filename,
165 long long value1, long long value2)
Gilles Peskined1465422022-04-13 23:59:52 +0200166{
David Horstmann71159f42023-01-03 12:51:59 +0000167 TEST_CF_PUBLIC(&value1, sizeof(value1));
168 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200169
David Horstmann71159f42023-01-03 12:51:59 +0000170 if (value1 <= value2) {
171 return 1;
172 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200173
David Horstmann71159f42023-01-03 12:51:59 +0000174 if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) {
Gilles Peskined1465422022-04-13 23:59:52 +0200175 /* We've already recorded the test as having failed. Don't
176 * overwrite any previous information about the failure. */
David Horstmann71159f42023-01-03 12:51:59 +0000177 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200178 }
David Horstmann71159f42023-01-03 12:51:59 +0000179 mbedtls_test_fail(test, line_no, filename);
180 (void) mbedtls_snprintf(mbedtls_test_info.line1,
181 sizeof(mbedtls_test_info.line1),
182 "lhs = 0x%016llx = %lld",
183 (unsigned long long) value1, value1);
184 (void) mbedtls_snprintf(mbedtls_test_info.line2,
185 sizeof(mbedtls_test_info.line2),
186 "rhs = 0x%016llx = %lld",
187 (unsigned long long) value2, value2);
188 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200189}
190
David Horstmann71159f42023-01-03 12:51:59 +0000191int mbedtls_test_unhexify(unsigned char *obuf,
192 size_t obufmax,
193 const char *ibuf,
194 size_t *len)
Ronald Crona0c25392020-06-18 10:10:46 +0200195{
196 unsigned char uc, uc2;
197
David Horstmann71159f42023-01-03 12:51:59 +0000198 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200199
200 /* Must be even number of bytes. */
David Horstmann71159f42023-01-03 12:51:59 +0000201 if ((*len) & 1) {
202 return -1;
203 }
Ronald Crona0c25392020-06-18 10:10:46 +0200204 *len /= 2;
205
David Horstmann71159f42023-01-03 12:51:59 +0000206 if ((*len) > obufmax) {
207 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200208 }
209
David Horstmann71159f42023-01-03 12:51:59 +0000210 while (*ibuf != 0) {
211 if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) {
212 return -1;
213 }
214
215 if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) {
216 return -1;
217 }
218
219 *(obuf++) = (uc << 4) | uc2;
220 }
221
222 return 0;
Ronald Cronf40529d2020-06-09 16:27:37 +0200223}
224
David Horstmann71159f42023-01-03 12:51:59 +0000225void mbedtls_test_hexify(unsigned char *obuf,
226 const unsigned char *ibuf,
227 int len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200228{
229 unsigned char l, h;
230
David Horstmann71159f42023-01-03 12:51:59 +0000231 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200232 h = *ibuf / 16;
233 l = *ibuf % 16;
234
David Horstmann71159f42023-01-03 12:51:59 +0000235 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200236 *obuf++ = '0' + h;
David Horstmann71159f42023-01-03 12:51:59 +0000237 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200238 *obuf++ = 'a' + h - 10;
David Horstmann71159f42023-01-03 12:51:59 +0000239 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200240
David Horstmann71159f42023-01-03 12:51:59 +0000241 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200242 *obuf++ = '0' + l;
David Horstmann71159f42023-01-03 12:51:59 +0000243 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200244 *obuf++ = 'a' + l - 10;
David Horstmann71159f42023-01-03 12:51:59 +0000245 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200246
247 ++ibuf;
248 len--;
249 }
250}
251
David Horstmann71159f42023-01-03 12:51:59 +0000252unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200253{
254 void *p;
David Horstmann71159f42023-01-03 12:51:59 +0000255 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200256
David Horstmann71159f42023-01-03 12:51:59 +0000257 p = mbedtls_calloc(1, actual_len);
258 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200259
David Horstmann71159f42023-01-03 12:51:59 +0000260 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200261
David Horstmann71159f42023-01-03 12:51:59 +0000262 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200263}
264
David Horstmann71159f42023-01-03 12:51:59 +0000265unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen)
Ronald Cronf40529d2020-06-09 16:27:37 +0200266{
267 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200268 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200269
David Horstmann71159f42023-01-03 12:51:59 +0000270 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200271
David Horstmann71159f42023-01-03 12:51:59 +0000272 if (*olen == 0) {
273 return mbedtls_test_zero_alloc(*olen);
274 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200275
David Horstmann71159f42023-01-03 12:51:59 +0000276 obuf = mbedtls_calloc(1, *olen);
277 TEST_HELPER_ASSERT(obuf != NULL);
278 TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0);
Ronald Cronf40529d2020-06-09 16:27:37 +0200279
David Horstmann71159f42023-01-03 12:51:59 +0000280 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200281}
282
David Horstmann71159f42023-01-03 12:51:59 +0000283int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b,
284 uint32_t a_len, uint32_t b_len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200285{
286 int ret = 0;
287 uint32_t i = 0;
288
David Horstmann71159f42023-01-03 12:51:59 +0000289 if (a_len != b_len) {
290 return -1;
291 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200292
David Horstmann71159f42023-01-03 12:51:59 +0000293 for (i = 0; i < a_len; i++) {
294 if (a[i] != b[i]) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200295 ret = -1;
296 break;
297 }
298 }
299 return ret;
300}
Ronald Crona1236142020-07-01 16:01:21 +0200301
Chris Jones96ae73b2021-01-08 17:04:59 +0000302#if defined(MBEDTLS_TEST_HOOKS)
David Horstmann71159f42023-01-03 12:51:59 +0000303void mbedtls_test_err_add_check(int high, int low,
304 const char *file, int line)
Chris Jones96ae73b2021-01-08 17:04:59 +0000305{
Chris Jones3f613c12021-03-31 09:34:22 +0100306 /* Error codes are always negative (a value of zero is a success) however
307 * their positive opposites can be easier to understand. The following
308 * examples given in comments have been made positive for ease of
309 * understanding. The structure of an error code is such:
310 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100311 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100312 *
313 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100314 * h = high level error code (includes high level module ID (bits 12..14)
315 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100316 * l = low level error code.
317 */
David Horstmann71159f42023-01-03 12:51:59 +0000318 if (high > -0x1000 && high != 0) {
319 /* high < 0001000000000000
320 * No high level module ID bits are set.
321 */
322 mbedtls_test_fail("'high' is not a high-level error code",
323 line, file);
324 } else if (high < -0x7F80) {
325 /* high > 0111111110000000
326 * Error code is greater than the largest allowed high level module ID.
327 */
328 mbedtls_test_fail("'high' error code is greater than 15 bits",
329 line, file);
330 } else if ((high & 0x7F) != 0) {
331 /* high & 0000000001111111
332 * Error code contains low level error code bits.
333 */
334 mbedtls_test_fail("'high' contains a low-level error code",
335 line, file);
336 } else if (low < -0x007F) {
337 /* low > 0000000001111111
338 * Error code contains high or module level error code bits.
339 */
340 mbedtls_test_fail("'low' error code is greater than 7 bits",
341 line, file);
342 } else if (low > 0) {
343 mbedtls_test_fail("'low' error code is greater than zero",
344 line, file);
Chris Jones96ae73b2021-01-08 17:04:59 +0000345 }
346}
347#endif /* MBEDTLS_TEST_HOOKS */