Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 1 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 2 | * Copyright The Mbed TLS Contributors |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 3 | * 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 Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
Gilles Peskine | 7db8e89 | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 18 | #include <test/constant_flow.h> |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 19 | #include <test/helpers.h> |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 20 | #include <test/macros.h> |
| 21 | #include <string.h> |
| 22 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 23 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 24 | #include <setjmp.h> |
| 25 | #endif |
| 26 | |
| 27 | /*----------------------------------------------------------------------------*/ |
| 28 | /* Static global variables */ |
| 29 | |
| 30 | #if defined(MBEDTLS_CHECK_PARAMS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 31 | typedef struct { |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 32 | uint8_t expected_call; |
| 33 | uint8_t expected_call_happened; |
| 34 | |
| 35 | jmp_buf state; |
| 36 | |
| 37 | mbedtls_test_param_failed_location_record_t location_record; |
| 38 | } |
| 39 | param_failed_ctx_t; |
| 40 | static param_failed_ctx_t param_failed_ctx; |
| 41 | #endif |
| 42 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_PLATFORM_C) |
| 44 | static mbedtls_platform_context platform_ctx; |
| 45 | #endif |
| 46 | |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 47 | mbedtls_test_info_t mbedtls_test_info; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 48 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 49 | /*----------------------------------------------------------------------------*/ |
| 50 | /* Helper Functions */ |
| 51 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | int mbedtls_test_platform_setup(void) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 53 | { |
| 54 | int ret = 0; |
| 55 | #if defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 56 | ret = mbedtls_platform_setup(&platform_ctx); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 57 | #endif /* MBEDTLS_PLATFORM_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 58 | return ret; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | void mbedtls_test_platform_teardown(void) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 62 | { |
| 63 | #if defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | mbedtls_platform_teardown(&platform_ctx); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 65 | #endif /* MBEDTLS_PLATFORM_C */ |
| 66 | } |
| 67 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 68 | static int ascii2uc(const char c, unsigned char *uc) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 69 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | if ((c >= '0') && (c <= '9')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 71 | *uc = c - '0'; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 72 | } else if ((c >= 'a') && (c <= 'f')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 73 | *uc = c - 'a' + 10; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 74 | } else if ((c >= 'A') && (c <= 'F')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 75 | *uc = c - 'A' + 10; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | } else { |
| 77 | return -1; |
| 78 | } |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 79 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 80 | return 0; |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 81 | } |
| 82 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | void mbedtls_test_fail(const char *test, int line_no, const char *filename) |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 84 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 85 | if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 86 | /* We've already recorded the test as having failed. Don't |
| 87 | * overwrite any previous information about the failure. */ |
| 88 | return; |
| 89 | } |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 90 | mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED; |
| 91 | mbedtls_test_info.test = test; |
| 92 | mbedtls_test_info.line_no = line_no; |
| 93 | mbedtls_test_info.filename = filename; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | void mbedtls_test_skip(const char *test, int line_no, const char *filename) |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 97 | { |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 98 | mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED; |
| 99 | mbedtls_test_info.test = test; |
| 100 | mbedtls_test_info.line_no = line_no; |
| 101 | mbedtls_test_info.filename = filename; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | void mbedtls_test_set_step(unsigned long step) |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 105 | { |
| 106 | mbedtls_test_info.step = step; |
| 107 | } |
| 108 | |
Gilles Peskine | 53a7206 | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 109 | #if defined(MBEDTLS_BIGNUM_C) |
| 110 | unsigned mbedtls_test_case_uses_negative_0 = 0; |
| 111 | #endif |
| 112 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | void mbedtls_test_info_reset(void) |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 114 | { |
| 115 | mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 116 | mbedtls_test_info.step = (unsigned long) (-1); |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 117 | mbedtls_test_info.test = 0; |
| 118 | mbedtls_test_info.line_no = 0; |
| 119 | mbedtls_test_info.filename = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | memset(mbedtls_test_info.line1, 0, sizeof(mbedtls_test_info.line1)); |
| 121 | memset(mbedtls_test_info.line2, 0, sizeof(mbedtls_test_info.line2)); |
Gilles Peskine | 53a7206 | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 122 | #if defined(MBEDTLS_BIGNUM_C) |
| 123 | mbedtls_test_case_uses_negative_0 = 0; |
| 124 | #endif |
Gilles Peskine | b436649 | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | int mbedtls_test_equal(const char *test, int line_no, const char *filename, |
| 128 | unsigned long long value1, unsigned long long value2) |
Gilles Peskine | b436649 | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 129 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 130 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 131 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | 7db8e89 | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 132 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | if (value1 == value2) { |
| 134 | return 1; |
| 135 | } |
Gilles Peskine | 7db8e89 | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 136 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 137 | if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) { |
Gilles Peskine | b436649 | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 138 | /* We've already recorded the test as having failed. Don't |
| 139 | * overwrite any previous information about the failure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | return 0; |
Gilles Peskine | b436649 | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 141 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | mbedtls_test_fail(test, line_no, filename); |
| 143 | (void) mbedtls_snprintf(mbedtls_test_info.line1, |
| 144 | sizeof(mbedtls_test_info.line1), |
| 145 | "lhs = 0x%016llx = %lld", |
| 146 | value1, (long long) value1); |
| 147 | (void) mbedtls_snprintf(mbedtls_test_info.line2, |
| 148 | sizeof(mbedtls_test_info.line2), |
| 149 | "rhs = 0x%016llx = %lld", |
| 150 | value2, (long long) value2); |
| 151 | return 0; |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 154 | int mbedtls_test_le_u(const char *test, int line_no, const char *filename, |
| 155 | unsigned long long value1, unsigned long long value2) |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 156 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 158 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | 7db8e89 | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 159 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | if (value1 <= value2) { |
| 161 | return 1; |
| 162 | } |
Gilles Peskine | 7db8e89 | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 163 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) { |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 165 | /* We've already recorded the test as having failed. Don't |
| 166 | * overwrite any previous information about the failure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | return 0; |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 168 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 169 | mbedtls_test_fail(test, line_no, filename); |
| 170 | (void) mbedtls_snprintf(mbedtls_test_info.line1, |
| 171 | sizeof(mbedtls_test_info.line1), |
| 172 | "lhs = 0x%016llx = %llu", |
| 173 | value1, value1); |
| 174 | (void) mbedtls_snprintf(mbedtls_test_info.line2, |
| 175 | sizeof(mbedtls_test_info.line2), |
| 176 | "rhs = 0x%016llx = %llu", |
| 177 | value2, value2); |
| 178 | return 0; |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 181 | int mbedtls_test_le_s(const char *test, int line_no, const char *filename, |
| 182 | long long value1, long long value2) |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 183 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 184 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 185 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | 7db8e89 | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | if (value1 <= value2) { |
| 188 | return 1; |
| 189 | } |
Gilles Peskine | 7db8e89 | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 191 | if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED) { |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 192 | /* We've already recorded the test as having failed. Don't |
| 193 | * overwrite any previous information about the failure. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 194 | return 0; |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 195 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | mbedtls_test_fail(test, line_no, filename); |
| 197 | (void) mbedtls_snprintf(mbedtls_test_info.line1, |
| 198 | sizeof(mbedtls_test_info.line1), |
| 199 | "lhs = 0x%016llx = %lld", |
| 200 | (unsigned long long) value1, value1); |
| 201 | (void) mbedtls_snprintf(mbedtls_test_info.line2, |
| 202 | sizeof(mbedtls_test_info.line2), |
| 203 | "rhs = 0x%016llx = %lld", |
| 204 | (unsigned long long) value2, value2); |
| 205 | return 0; |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 208 | int mbedtls_test_unhexify(unsigned char *obuf, |
| 209 | size_t obufmax, |
| 210 | const char *ibuf, |
| 211 | size_t *len) |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 212 | { |
| 213 | unsigned char uc, uc2; |
| 214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 215 | *len = strlen(ibuf); |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 216 | |
| 217 | /* Must be even number of bytes. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | if ((*len) & 1) { |
| 219 | return -1; |
| 220 | } |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 221 | *len /= 2; |
| 222 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 223 | if ((*len) > obufmax) { |
| 224 | return -1; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 227 | while (*ibuf != 0) { |
| 228 | if (ascii2uc(*(ibuf++), &uc) != 0) { |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | if (ascii2uc(*(ibuf++), &uc2) != 0) { |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | *(obuf++) = (uc << 4) | uc2; |
| 237 | } |
| 238 | |
| 239 | return 0; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 242 | void mbedtls_test_hexify(unsigned char *obuf, |
| 243 | const unsigned char *ibuf, |
| 244 | int len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 245 | { |
| 246 | unsigned char l, h; |
| 247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 248 | while (len != 0) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 249 | h = *ibuf / 16; |
| 250 | l = *ibuf % 16; |
| 251 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 252 | if (h < 10) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 253 | *obuf++ = '0' + h; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | } else { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 255 | *obuf++ = 'a' + h - 10; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 256 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | if (l < 10) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 259 | *obuf++ = '0' + l; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 260 | } else { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 261 | *obuf++ = 'a' + l - 10; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 263 | |
| 264 | ++ibuf; |
| 265 | len--; |
| 266 | } |
| 267 | } |
| 268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 269 | unsigned char *mbedtls_test_zero_alloc(size_t len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 270 | { |
| 271 | void *p; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | size_t actual_len = (len != 0) ? len : 1; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 273 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 274 | p = mbedtls_calloc(1, actual_len); |
| 275 | TEST_HELPER_ASSERT(p != NULL); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | memset(p, 0x00, actual_len); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 278 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 279 | return p; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 282 | unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 283 | { |
| 284 | unsigned char *obuf; |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 285 | size_t len; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 286 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | *olen = strlen(ibuf) / 2; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 288 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 289 | if (*olen == 0) { |
| 290 | return mbedtls_test_zero_alloc(*olen); |
| 291 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 292 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | obuf = mbedtls_calloc(1, *olen); |
| 294 | TEST_HELPER_ASSERT(obuf != NULL); |
| 295 | TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 296 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | return obuf; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 300 | int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b, |
| 301 | uint32_t a_len, uint32_t b_len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 302 | { |
| 303 | int ret = 0; |
| 304 | uint32_t i = 0; |
| 305 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | if (a_len != b_len) { |
| 307 | return -1; |
| 308 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | for (i = 0; i < a_len; i++) { |
| 311 | if (a[i] != b[i]) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 312 | ret = -1; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | return ret; |
| 317 | } |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 318 | |
| 319 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 320 | void mbedtls_test_param_failed_get_location_record( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 321 | mbedtls_test_param_failed_location_record_t *location_record) |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 322 | { |
| 323 | *location_record = param_failed_ctx.location_record; |
| 324 | } |
| 325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 326 | void mbedtls_test_param_failed_expect_call(void) |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 327 | { |
| 328 | param_failed_ctx.expected_call_happened = 0; |
| 329 | param_failed_ctx.expected_call = 1; |
| 330 | } |
| 331 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 332 | int mbedtls_test_param_failed_check_expected_call(void) |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 333 | { |
| 334 | param_failed_ctx.expected_call = 0; |
| 335 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 336 | if (param_failed_ctx.expected_call_happened != 0) { |
| 337 | return 0; |
| 338 | } |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 339 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 340 | return -1; |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 343 | void *mbedtls_test_param_failed_get_state_buf(void) |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 344 | { |
Ronald Cron | bf4f408 | 2020-09-25 10:45:06 +0200 | [diff] [blame] | 345 | return ¶m_failed_ctx.state; |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 348 | void mbedtls_test_param_failed_reset_state(void) |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 349 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 350 | memset(param_failed_ctx.state, 0, sizeof(param_failed_ctx.state)); |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 353 | void mbedtls_param_failed(const char *failure_condition, |
| 354 | const char *file, |
| 355 | int line) |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 356 | { |
| 357 | /* Record the location of the failure */ |
| 358 | param_failed_ctx.location_record.failure_condition = failure_condition; |
| 359 | param_failed_ctx.location_record.file = file; |
| 360 | param_failed_ctx.location_record.line = line; |
| 361 | |
| 362 | /* If we are testing the callback function... */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | if (param_failed_ctx.expected_call != 0) { |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 364 | param_failed_ctx.expected_call = 0; |
| 365 | param_failed_ctx.expected_call_happened = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 366 | } else { |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 367 | /* ...else try a long jump. If the execution state has not been set-up |
| 368 | * or reset then the long jump buffer is all zero's and the call will |
| 369 | * with high probability fault, emphasizing there is something to look |
| 370 | * at. |
| 371 | */ |
| 372 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 373 | longjmp(param_failed_ctx.state, 1); |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | #endif /* MBEDTLS_CHECK_PARAMS */ |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 377 | |
| 378 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 379 | void mbedtls_test_err_add_check(int high, int low, |
| 380 | const char *file, int line) |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 381 | { |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 382 | /* Error codes are always negative (a value of zero is a success) however |
| 383 | * their positive opposites can be easier to understand. The following |
| 384 | * examples given in comments have been made positive for ease of |
| 385 | * understanding. The structure of an error code is such: |
| 386 | * |
Chris Jones | abded0e | 2021-04-12 15:44:47 +0100 | [diff] [blame] | 387 | * shhhhhhhhlllllll |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 388 | * |
| 389 | * s = sign bit. |
Chris Jones | 4f91d8d | 2021-04-23 12:07:25 +0100 | [diff] [blame] | 390 | * h = high level error code (includes high level module ID (bits 12..14) |
| 391 | * and module-dependent error code (bits 7..11)). |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 392 | * l = low level error code. |
| 393 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | if (high > -0x1000 && high != 0) { |
| 395 | /* high < 0001000000000000 |
| 396 | * No high level module ID bits are set. |
| 397 | */ |
| 398 | mbedtls_test_fail("'high' is not a high-level error code", |
| 399 | line, file); |
| 400 | } else if (high < -0x7F80) { |
| 401 | /* high > 0111111110000000 |
| 402 | * Error code is greater than the largest allowed high level module ID. |
| 403 | */ |
| 404 | mbedtls_test_fail("'high' error code is greater than 15 bits", |
| 405 | line, file); |
| 406 | } else if ((high & 0x7F) != 0) { |
| 407 | /* high & 0000000001111111 |
| 408 | * Error code contains low level error code bits. |
| 409 | */ |
| 410 | mbedtls_test_fail("'high' contains a low-level error code", |
| 411 | line, file); |
| 412 | } else if (low < -0x007F) { |
| 413 | /* low > 0000000001111111 |
| 414 | * Error code contains high or module level error code bits. |
| 415 | */ |
| 416 | mbedtls_test_fail("'low' error code is greater than 7 bits", |
| 417 | line, file); |
| 418 | } else if (low > 0) { |
| 419 | mbedtls_test_fail("'low' error code is greater than zero", |
| 420 | line, file); |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | #endif /* MBEDTLS_TEST_HOOKS */ |
Gilles Peskine | db47971 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 424 | |
| 425 | #if defined(MBEDTLS_BIGNUM_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 426 | int mbedtls_test_read_mpi(mbedtls_mpi *X, const char *s) |
Gilles Peskine | db47971 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 427 | { |
Gilles Peskine | 53a7206 | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 428 | int negative = 0; |
| 429 | /* Always set the sign bit to -1 if the input has a minus sign, even for 0. |
| 430 | * This creates an invalid representation, which mbedtls_mpi_read_string() |
| 431 | * avoids but we want to be able to create that in test data. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 432 | if (s[0] == '-') { |
Gilles Peskine | 53a7206 | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 433 | ++s; |
| 434 | negative = 1; |
| 435 | } |
Gilles Peskine | db47971 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 436 | /* mbedtls_mpi_read_string() currently retains leading zeros. |
| 437 | * It always allocates at least one limb for the value 0. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | if (s[0] == 0) { |
| 439 | mbedtls_mpi_free(X); |
| 440 | return 0; |
Gilles Peskine | db47971 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 441 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 442 | int ret = mbedtls_mpi_read_string(X, 16, s); |
| 443 | if (ret != 0) { |
| 444 | return ret; |
| 445 | } |
| 446 | if (negative) { |
| 447 | if (mbedtls_mpi_cmp_int(X, 0) == 0) { |
Gilles Peskine | 53a7206 | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 448 | ++mbedtls_test_case_uses_negative_0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 449 | } |
Gilles Peskine | 53a7206 | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 450 | X->s = -1; |
| 451 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 452 | return 0; |
Gilles Peskine | db47971 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 453 | } |
| 454 | #endif |