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