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
Gilles Peskine449bd832023-01-11 14:50:10 +010035int mbedtls_test_platform_setup(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020036{
37 int ret = 0;
38#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010039 ret = mbedtls_platform_setup(&platform_ctx);
Ronald Cronf40529d2020-06-09 16:27:37 +020040#endif /* MBEDTLS_PLATFORM_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010041 return ret;
Ronald Cronf40529d2020-06-09 16:27:37 +020042}
43
Gilles Peskine449bd832023-01-11 14:50:10 +010044void mbedtls_test_platform_teardown(void)
Ronald Cronf40529d2020-06-09 16:27:37 +020045{
46#if defined(MBEDTLS_PLATFORM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010047 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{
Gilles Peskine449bd832023-01-11 14:50:10 +010053 if ((c >= '0') && (c <= '9')) {
Ronald Crona0c25392020-06-18 10:10:46 +020054 *uc = c - '0';
Gilles Peskine449bd832023-01-11 14:50:10 +010055 } else if ((c >= 'a') && (c <= 'f')) {
Ronald Crona0c25392020-06-18 10:10:46 +020056 *uc = c - 'a' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010057 } else if ((c >= 'A') && (c <= 'F')) {
Ronald Crona0c25392020-06-18 10:10:46 +020058 *uc = c - 'A' + 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010059 } else {
60 return -1;
61 }
Ronald Crona0c25392020-06-18 10:10:46 +020062
Gilles Peskine449bd832023-01-11 14:50:10 +010063 return 0;
Ronald Crona0c25392020-06-18 10:10:46 +020064}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_test_fail(const char *test, int line_no, const char *filename)
Chris Jones9634bb12021-01-20 15:56:42 +000067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 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
Gilles Peskine449bd832023-01-11 14:50:10 +010079void 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
Gilles Peskine449bd832023-01-11 14:50:10 +010087void 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
Gilles Peskine449bd832023-01-11 14:50:10 +010096void mbedtls_test_info_reset(void)
Chris Jonesa5ab7652021-02-02 16:20:45 +000097{
98 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 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;
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100110int 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{
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_CF_PUBLIC(&value1, sizeof(value1));
114 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (value1 == value2) {
117 return 1;
118 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 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. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200124 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100137int 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{
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 TEST_CF_PUBLIC(&value1, sizeof(value1));
141 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if (value1 <= value2) {
144 return 1;
145 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 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. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200151 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int 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{
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 TEST_CF_PUBLIC(&value1, sizeof(value1));
168 TEST_CF_PUBLIC(&value2, sizeof(value2));
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 if (value1 <= value2) {
171 return 1;
172 }
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 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. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 return 0;
Gilles Peskined1465422022-04-13 23:59:52 +0200178 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100191int 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 *len = strlen(ibuf);
Ronald Crona0c25392020-06-18 10:10:46 +0200199
200 /* Must be even number of bytes. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if ((*len) & 1) {
202 return -1;
203 }
Ronald Crona0c25392020-06-18 10:10:46 +0200204 *len /= 2;
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if ((*len) > obufmax) {
207 return -1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200208 }
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100225void 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 while (len != 0) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200232 h = *ibuf / 16;
233 l = *ibuf % 16;
234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (h < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200236 *obuf++ = '0' + h;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200238 *obuf++ = 'a' + h - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (l < 10) {
Ronald Cronf40529d2020-06-09 16:27:37 +0200242 *obuf++ = '0' + l;
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 } else {
Ronald Cronf40529d2020-06-09 16:27:37 +0200244 *obuf++ = 'a' + l - 10;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200246
247 ++ibuf;
248 len--;
249 }
250}
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252unsigned char *mbedtls_test_zero_alloc(size_t len)
Ronald Cronf40529d2020-06-09 16:27:37 +0200253{
254 void *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 size_t actual_len = (len != 0) ? len : 1;
Ronald Cronf40529d2020-06-09 16:27:37 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 p = mbedtls_calloc(1, actual_len);
258 TEST_HELPER_ASSERT(p != NULL);
Ronald Cronf40529d2020-06-09 16:27:37 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 memset(p, 0x00, actual_len);
Ronald Cronf40529d2020-06-09 16:27:37 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return p;
Ronald Cronf40529d2020-06-09 16:27:37 +0200263}
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265unsigned 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 *olen = strlen(ibuf) / 2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (*olen == 0) {
273 return mbedtls_test_zero_alloc(*olen);
274 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return obuf;
Ronald Cronf40529d2020-06-09 16:27:37 +0200281}
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283int 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (a_len != b_len) {
290 return -1;
291 }
Ronald Cronf40529d2020-06-09 16:27:37 +0200292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 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)
Gilles Peskine449bd832023-01-11 14:50:10 +0100303void 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 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 */