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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 6 | #include <test/constant_flow.h> |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 7 | #include <test/helpers.h> |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 8 | #include <test/macros.h> |
| 9 | #include <string.h> |
| 10 | |
Gilles Peskine | c2d16b2 | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 11 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 12 | #include <psa/crypto.h> |
| 13 | #include <test/psa_crypto_helpers.h> |
| 14 | #endif |
| 15 | |
David Horstmann | 83ece2f | 2023-12-18 15:30:46 +0000 | [diff] [blame] | 16 | #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) |
David Horstmann | b489257 | 2023-12-14 14:17:04 +0000 | [diff] [blame] | 17 | #include <test/psa_memory_poisoning_wrappers.h> |
| 18 | #endif |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 19 | #if defined(MBEDTLS_THREADING_C) |
| 20 | #include "mbedtls/threading.h" |
| 21 | #endif |
David Horstmann | b489257 | 2023-12-14 14:17:04 +0000 | [diff] [blame] | 22 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 23 | /*----------------------------------------------------------------------------*/ |
| 24 | /* Static global variables */ |
| 25 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_PLATFORM_C) |
| 27 | static mbedtls_platform_context platform_ctx; |
| 28 | #endif |
| 29 | |
Paul Elliott | e2f6662 | 2024-01-19 20:22:24 +0000 | [diff] [blame] | 30 | static mbedtls_test_info_t mbedtls_test_info; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 31 | |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 32 | #ifdef MBEDTLS_THREADING_C |
| 33 | mbedtls_threading_mutex_t mbedtls_test_info_mutex; |
| 34 | #endif /* MBEDTLS_THREADING_C */ |
| 35 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 36 | /*----------------------------------------------------------------------------*/ |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 37 | /* Mbedtls Test Info accessors |
| 38 | * |
Paul Elliott | 9011dae | 2024-02-24 10:57:22 +0000 | [diff] [blame] | 39 | * NOTE - there are two types of accessors here: public accessors and internal |
| 40 | * accessors. The public accessors have prototypes in helpers.h and lock |
| 41 | * mbedtls_test_info_mutex (if mutexes are enabled). The _internal accessors, |
| 42 | * which are expected to be used from this module *only*, do not lock the mutex. |
| 43 | * These are designed to be called from within public functions which already |
| 44 | * hold the mutex. The main reason for this difference is the need to set |
| 45 | * multiple test data values atomically (without releasing the mutex) to prevent |
| 46 | * race conditions. */ |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 47 | |
| 48 | mbedtls_test_result_t mbedtls_test_get_result(void) |
| 49 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 50 | mbedtls_test_result_t result; |
| 51 | |
| 52 | #ifdef MBEDTLS_THREADING_C |
| 53 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 54 | #endif /* MBEDTLS_THREADING_C */ |
| 55 | |
| 56 | result = mbedtls_test_info.result; |
| 57 | |
| 58 | #ifdef MBEDTLS_THREADING_C |
| 59 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 60 | #endif /* MBEDTLS_THREADING_C */ |
| 61 | |
| 62 | return result; |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 65 | static void mbedtls_test_set_result_internal(mbedtls_test_result_t result, const char *test, |
| 66 | int line_no, const char *filename) |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 67 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 68 | /* Internal function only - mbedtls_test_info_mutex should be held prior |
| 69 | * to calling this function. */ |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 70 | |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 71 | mbedtls_test_info.result = result; |
| 72 | mbedtls_test_info.test = test; |
| 73 | mbedtls_test_info.line_no = line_no; |
| 74 | mbedtls_test_info.filename = filename; |
| 75 | } |
| 76 | |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 77 | const char *mbedtls_test_get_test(void) |
| 78 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 79 | const char *test; |
| 80 | |
| 81 | #ifdef MBEDTLS_THREADING_C |
| 82 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 83 | #endif /* MBEDTLS_THREADING_C */ |
| 84 | |
| 85 | test = mbedtls_test_info.test; |
| 86 | |
| 87 | #ifdef MBEDTLS_THREADING_C |
| 88 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 89 | #endif /* MBEDTLS_THREADING_C */ |
| 90 | |
| 91 | return test; |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 92 | } |
| 93 | const char *mbedtls_get_test_filename(void) |
| 94 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 95 | const char *filename; |
| 96 | |
| 97 | #ifdef MBEDTLS_THREADING_C |
| 98 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 99 | #endif /* MBEDTLS_THREADING_C */ |
| 100 | |
| 101 | /* It should be ok just to pass back the pointer here, as it is going to |
| 102 | * be a pointer into non changing data. */ |
| 103 | filename = mbedtls_test_info.filename; |
| 104 | |
| 105 | #ifdef MBEDTLS_THREADING_C |
| 106 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 107 | #endif /* MBEDTLS_THREADING_C */ |
| 108 | |
| 109 | return filename; |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int mbedtls_test_get_line_no(void) |
| 113 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 114 | int line_no; |
| 115 | |
| 116 | #ifdef MBEDTLS_THREADING_C |
| 117 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 118 | #endif /* MBEDTLS_THREADING_C */ |
| 119 | |
| 120 | line_no = mbedtls_test_info.line_no; |
| 121 | |
| 122 | #ifdef MBEDTLS_THREADING_C |
| 123 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 124 | #endif /* MBEDTLS_THREADING_C */ |
| 125 | |
| 126 | return line_no; |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void mbedtls_test_increment_step(void) |
| 130 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 131 | #ifdef MBEDTLS_THREADING_C |
| 132 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 133 | #endif /* MBEDTLS_THREADING_C */ |
| 134 | |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 135 | ++mbedtls_test_info.step; |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 136 | |
| 137 | #ifdef MBEDTLS_THREADING_C |
| 138 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 139 | #endif /* MBEDTLS_THREADING_C */ |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | unsigned long mbedtls_test_get_step(void) |
| 143 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 144 | unsigned long step; |
| 145 | |
| 146 | #ifdef MBEDTLS_THREADING_C |
| 147 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 148 | #endif /* MBEDTLS_THREADING_C */ |
| 149 | |
| 150 | step = mbedtls_test_info.step; |
| 151 | |
| 152 | #ifdef MBEDTLS_THREADING_C |
| 153 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 154 | #endif /* MBEDTLS_THREADING_C */ |
| 155 | |
| 156 | return step; |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 159 | static void mbedtls_test_reset_step_internal(void) |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 160 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 161 | /* Internal function only - mbedtls_test_info_mutex should be held prior |
Paul Elliott | 9efc602 | 2024-01-31 15:33:23 +0000 | [diff] [blame] | 162 | * to calling this function. */ |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 163 | |
Paul Elliott | ac61cee | 2024-02-02 17:53:38 +0000 | [diff] [blame] | 164 | mbedtls_test_info.step = (unsigned long) (-1); |
| 165 | } |
| 166 | |
| 167 | void mbedtls_test_set_step(unsigned long step) |
| 168 | { |
| 169 | #ifdef MBEDTLS_THREADING_C |
| 170 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 171 | #endif /* MBEDTLS_THREADING_C */ |
| 172 | |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 173 | mbedtls_test_info.step = step; |
Paul Elliott | ac61cee | 2024-02-02 17:53:38 +0000 | [diff] [blame] | 174 | |
| 175 | #ifdef MBEDTLS_THREADING_C |
| 176 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 177 | #endif /* MBEDTLS_THREADING_C */ |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void mbedtls_test_get_line1(char *line) |
| 181 | { |
| 182 | #ifdef MBEDTLS_THREADING_C |
| 183 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 184 | #endif /* MBEDTLS_THREADING_C */ |
| 185 | |
| 186 | memcpy(line, mbedtls_test_info.line1, MBEDTLS_TEST_LINE_LENGTH); |
| 187 | |
| 188 | #ifdef MBEDTLS_THREADING_C |
| 189 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 190 | #endif /* MBEDTLS_THREADING_C */ |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 191 | } |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 192 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 193 | static void mbedtls_test_set_line1_internal(const char *line) |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 194 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 195 | /* Internal function only - mbedtls_test_info_mutex should be held prior |
Paul Elliott | 9efc602 | 2024-01-31 15:33:23 +0000 | [diff] [blame] | 196 | * to calling this function. */ |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 197 | |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 198 | if (line == NULL) { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 199 | memset(mbedtls_test_info.line1, 0, MBEDTLS_TEST_LINE_LENGTH); |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 200 | } else { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 201 | memcpy(mbedtls_test_info.line1, line, MBEDTLS_TEST_LINE_LENGTH); |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 205 | void mbedtls_test_get_line2(char *line) |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 206 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 207 | #ifdef MBEDTLS_THREADING_C |
| 208 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 209 | #endif /* MBEDTLS_THREADING_C */ |
| 210 | |
| 211 | memcpy(line, mbedtls_test_info.line2, MBEDTLS_TEST_LINE_LENGTH); |
| 212 | |
| 213 | #ifdef MBEDTLS_THREADING_C |
| 214 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 215 | #endif /* MBEDTLS_THREADING_C */ |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 218 | static void mbedtls_test_set_line2_internal(const char *line) |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 219 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 220 | /* Internal function only - mbedtls_test_info_mutex should be held prior |
Paul Elliott | 9efc602 | 2024-01-31 15:33:23 +0000 | [diff] [blame] | 221 | * to calling this function. */ |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 222 | |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 223 | if (line == NULL) { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 224 | memset(mbedtls_test_info.line2, 0, MBEDTLS_TEST_LINE_LENGTH); |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 225 | } else { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 226 | memcpy(mbedtls_test_info.line2, line, MBEDTLS_TEST_LINE_LENGTH); |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 231 | #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 232 | const char *mbedtls_test_get_mutex_usage_error(void) |
| 233 | { |
Paul Elliott | 114ed5e | 2024-02-13 15:35:14 +0000 | [diff] [blame] | 234 | const char *usage_error; |
| 235 | |
| 236 | #ifdef MBEDTLS_THREADING_C |
| 237 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 238 | #endif /* MBEDTLS_THREADING_C */ |
| 239 | |
| 240 | usage_error = mbedtls_test_info.mutex_usage_error; |
| 241 | |
| 242 | #ifdef MBEDTLS_THREADING_C |
| 243 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 244 | #endif /* MBEDTLS_THREADING_C */ |
| 245 | |
| 246 | return usage_error; |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void mbedtls_test_set_mutex_usage_error(const char *msg) |
| 250 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 251 | #ifdef MBEDTLS_THREADING_C |
| 252 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 253 | #endif /* MBEDTLS_THREADING_C */ |
| 254 | |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 255 | if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) { |
| 256 | mbedtls_test_info.mutex_usage_error = msg; |
| 257 | } |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 258 | |
| 259 | #ifdef MBEDTLS_THREADING_C |
| 260 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 261 | #endif /* MBEDTLS_THREADING_C */ |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 262 | } |
| 263 | #endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 264 | |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 265 | #if defined(MBEDTLS_BIGNUM_C) |
| 266 | |
| 267 | unsigned mbedtls_test_get_case_uses_negative_0(void) |
| 268 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 269 | unsigned test_case_uses_negative_0 = 0; |
| 270 | #ifdef MBEDTLS_THREADING_C |
| 271 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 272 | #endif /* MBEDTLS_THREADING_C */ |
| 273 | test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0; |
| 274 | |
| 275 | #ifdef MBEDTLS_THREADING_C |
| 276 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 277 | #endif /* MBEDTLS_THREADING_C */ |
| 278 | |
| 279 | return test_case_uses_negative_0; |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 282 | static void mbedtls_test_set_case_uses_negative_0_internal(unsigned uses) |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 283 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 284 | /* Internal function only - mbedtls_test_info_mutex should be held prior |
Paul Elliott | 9efc602 | 2024-01-31 15:33:23 +0000 | [diff] [blame] | 285 | * to calling this function. */ |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 286 | |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 287 | mbedtls_test_info.case_uses_negative_0 = uses; |
| 288 | } |
| 289 | |
| 290 | void mbedtls_test_increment_case_uses_negative_0(void) |
| 291 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 292 | #ifdef MBEDTLS_THREADING_C |
| 293 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 294 | #endif /* MBEDTLS_THREADING_C */ |
| 295 | |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 296 | ++mbedtls_test_info.case_uses_negative_0; |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 297 | |
| 298 | #ifdef MBEDTLS_THREADING_C |
| 299 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 300 | #endif /* MBEDTLS_THREADING_C */ |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Paul Elliott | 3d2db89 | 2024-01-19 20:42:56 +0000 | [diff] [blame] | 303 | #endif /* MBEDTLS_BIGNUM_C */ |
| 304 | |
| 305 | #ifdef MBEDTLS_TEST_MUTEX_USAGE |
| 306 | mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void) |
| 307 | { |
| 308 | return &mbedtls_test_info_mutex; |
| 309 | } |
| 310 | |
| 311 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 312 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 313 | /*----------------------------------------------------------------------------*/ |
| 314 | /* Helper Functions */ |
| 315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | int mbedtls_test_platform_setup(void) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 317 | { |
| 318 | int ret = 0; |
Gilles Peskine | c2d16b2 | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 319 | |
David Horstmann | 6668453 | 2023-12-15 18:29:54 +0000 | [diff] [blame] | 320 | #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \ |
David Horstmann | 4a48bec | 2024-03-13 15:42:31 +0000 | [diff] [blame] | 321 | && !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) \ |
David Horstmann | 6668453 | 2023-12-15 18:29:54 +0000 | [diff] [blame] | 322 | && defined(MBEDTLS_TEST_MEMORY_CAN_POISON) |
David Horstmann | b489257 | 2023-12-14 14:17:04 +0000 | [diff] [blame] | 323 | mbedtls_poison_test_hooks_setup(); |
| 324 | #endif |
| 325 | |
Gilles Peskine | c2d16b2 | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 326 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 327 | /* Make sure that injected entropy is present. Otherwise |
| 328 | * psa_crypto_init() will fail. This is not necessary for test suites |
| 329 | * that don't use PSA, but it's harmless (except for leaving a file |
| 330 | * behind). */ |
| 331 | ret = mbedtls_test_inject_entropy_restore(); |
| 332 | if (ret != 0) { |
| 333 | return ret; |
| 334 | } |
| 335 | #endif |
| 336 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 337 | #if defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | ret = mbedtls_platform_setup(&platform_ctx); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 339 | #endif /* MBEDTLS_PLATFORM_C */ |
Gilles Peskine | c2d16b2 | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 340 | |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 341 | #ifdef MBEDTLS_THREADING_C |
| 342 | mbedtls_mutex_init(&mbedtls_test_info_mutex); |
| 343 | #endif /* MBEDTLS_THREADING_C */ |
| 344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | return ret; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 346 | } |
| 347 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | void mbedtls_test_platform_teardown(void) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 349 | { |
David Horstmann | 6668453 | 2023-12-15 18:29:54 +0000 | [diff] [blame] | 350 | #if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \ |
David Horstmann | 4a48bec | 2024-03-13 15:42:31 +0000 | [diff] [blame] | 351 | && !defined(MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS) \ |
David Horstmann | 6668453 | 2023-12-15 18:29:54 +0000 | [diff] [blame] | 352 | && defined(MBEDTLS_TEST_MEMORY_CAN_POISON) |
David Horstmann | b489257 | 2023-12-14 14:17:04 +0000 | [diff] [blame] | 353 | mbedtls_poison_test_hooks_teardown(); |
| 354 | #endif |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 355 | #ifdef MBEDTLS_THREADING_C |
| 356 | mbedtls_mutex_free(&mbedtls_test_info_mutex); |
| 357 | #endif /* MBEDTLS_THREADING_C */ |
David Horstmann | b489257 | 2023-12-14 14:17:04 +0000 | [diff] [blame] | 358 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 359 | #if defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | mbedtls_platform_teardown(&platform_ctx); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 361 | #endif /* MBEDTLS_PLATFORM_C */ |
| 362 | } |
| 363 | |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 364 | int mbedtls_test_ascii2uc(const char c, unsigned char *uc) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 365 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | if ((c >= '0') && (c <= '9')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 367 | *uc = c - '0'; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | } else if ((c >= 'a') && (c <= 'f')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 369 | *uc = c - 'a' + 10; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | } else if ((c >= 'A') && (c <= 'F')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 371 | *uc = c - 'A' + 10; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | } else { |
| 373 | return -1; |
| 374 | } |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | return 0; |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 379 | static void mbedtls_test_fail_internal(const char *test, int line_no, const char *filename) |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 380 | { |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 381 | /* Internal function only - mbedtls_test_info_mutex should be held prior |
| 382 | * to calling this function. */ |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 383 | |
| 384 | /* Don't use accessor, we already hold mutex. */ |
| 385 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
| 386 | /* If we have already recorded the test as having failed then don't |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 387 | * overwrite any previous information about the failure. */ |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 388 | mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_FAILED, test, line_no, filename); |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 389 | } |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 392 | void mbedtls_test_fail(const char *test, int line_no, const char *filename) |
| 393 | { |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 394 | #ifdef MBEDTLS_THREADING_C |
| 395 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 396 | #endif /* MBEDTLS_THREADING_C */ |
| 397 | |
| 398 | mbedtls_test_fail_internal(test, line_no, filename); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 399 | |
| 400 | #ifdef MBEDTLS_THREADING_C |
| 401 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 402 | #endif /* MBEDTLS_THREADING_C */ |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | 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] | 406 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 407 | #ifdef MBEDTLS_THREADING_C |
| 408 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 409 | #endif /* MBEDTLS_THREADING_C */ |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 410 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 411 | mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SKIPPED, test, line_no, filename); |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 412 | |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 413 | #ifdef MBEDTLS_THREADING_C |
| 414 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 415 | #endif /* MBEDTLS_THREADING_C */ |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 416 | } |
Gilles Peskine | ca6e8aa | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | void mbedtls_test_info_reset(void) |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 419 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 420 | #ifdef MBEDTLS_THREADING_C |
| 421 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 422 | #endif /* MBEDTLS_THREADING_C */ |
| 423 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 424 | mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0); |
| 425 | mbedtls_test_reset_step_internal(); |
| 426 | mbedtls_test_set_line1_internal(NULL); |
| 427 | mbedtls_test_set_line2_internal(NULL); |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 428 | |
Gilles Peskine | ca6e8aa | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 429 | #if defined(MBEDTLS_BIGNUM_C) |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 430 | mbedtls_test_set_case_uses_negative_0_internal(0); |
Gilles Peskine | ca6e8aa | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 431 | #endif |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 432 | |
| 433 | #ifdef MBEDTLS_THREADING_C |
Paul Elliott | 0b2835d | 2024-02-01 13:27:04 +0000 | [diff] [blame] | 434 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 435 | #endif /* MBEDTLS_THREADING_C */ |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | int mbedtls_test_equal(const char *test, int line_no, const char *filename, |
| 439 | unsigned long long value1, unsigned long long value2) |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 440 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 441 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 442 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 443 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | if (value1 == value2) { |
| 445 | return 1; |
| 446 | } |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 447 | |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 448 | #ifdef MBEDTLS_THREADING_C |
| 449 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 450 | #endif /* MBEDTLS_THREADING_C */ |
| 451 | |
| 452 | /* Don't use accessor, as we already hold mutex. */ |
| 453 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
| 454 | /* If we've already recorded the test as having failed then don't |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 455 | * overwrite any previous information about the failure. */ |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 456 | |
| 457 | char buf[MBEDTLS_TEST_LINE_LENGTH]; |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 458 | mbedtls_test_fail_internal(test, line_no, filename); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 459 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 460 | "lhs = 0x%016llx = %lld", |
| 461 | value1, (long long) value1); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 462 | mbedtls_test_set_line1_internal(buf); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 463 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 464 | "rhs = 0x%016llx = %lld", |
| 465 | value2, (long long) value2); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 466 | mbedtls_test_set_line2_internal(buf); |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 467 | } |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 468 | |
| 469 | #ifdef MBEDTLS_THREADING_C |
| 470 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 471 | #endif /* MBEDTLS_THREADING_C */ |
| 472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | return 0; |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | int mbedtls_test_le_u(const char *test, int line_no, const char *filename, |
| 477 | unsigned long long value1, unsigned long long value2) |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 478 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 480 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | if (value1 <= value2) { |
| 483 | return 1; |
| 484 | } |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 485 | |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 486 | #ifdef MBEDTLS_THREADING_C |
| 487 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 488 | #endif /* MBEDTLS_THREADING_C */ |
| 489 | |
| 490 | /* Don't use accessor, we already hold mutex. */ |
| 491 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
| 492 | /* If we've already recorded the test as having failed then don't |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 493 | * overwrite any previous information about the failure. */ |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 494 | |
| 495 | char buf[MBEDTLS_TEST_LINE_LENGTH]; |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 496 | mbedtls_test_fail_internal(test, line_no, filename); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 497 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 498 | "lhs = 0x%016llx = %llu", |
| 499 | value1, value1); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 500 | mbedtls_test_set_line1_internal(buf); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 501 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 502 | "rhs = 0x%016llx = %llu", |
| 503 | value2, value2); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 504 | mbedtls_test_set_line2_internal(buf); |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 505 | } |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 506 | |
| 507 | #ifdef MBEDTLS_THREADING_C |
| 508 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 509 | #endif /* MBEDTLS_THREADING_C */ |
| 510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | return 0; |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | int mbedtls_test_le_s(const char *test, int line_no, const char *filename, |
| 515 | long long value1, long long value2) |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 516 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 518 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 520 | if (value1 <= value2) { |
| 521 | return 1; |
| 522 | } |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 523 | |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 524 | #ifdef MBEDTLS_THREADING_C |
| 525 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 526 | #endif /* MBEDTLS_THREADING_C */ |
| 527 | |
| 528 | /* Don't use accessor, we already hold mutex. */ |
Paul Elliott | f20728e | 2024-02-06 12:49:45 +0000 | [diff] [blame] | 529 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 530 | /* If we've already recorded the test as having failed then don't |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 531 | * overwrite any previous information about the failure. */ |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 532 | |
| 533 | char buf[MBEDTLS_TEST_LINE_LENGTH]; |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 534 | mbedtls_test_fail_internal(test, line_no, filename); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 535 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 536 | "lhs = 0x%016llx = %lld", |
| 537 | (unsigned long long) value1, value1); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 538 | mbedtls_test_set_line1_internal(buf); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 539 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 540 | "rhs = 0x%016llx = %lld", |
| 541 | (unsigned long long) value2, value2); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame] | 542 | mbedtls_test_set_line2_internal(buf); |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 543 | } |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 544 | |
| 545 | #ifdef MBEDTLS_THREADING_C |
| 546 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 547 | #endif /* MBEDTLS_THREADING_C */ |
| 548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | return 0; |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 550 | } |
| 551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | int mbedtls_test_unhexify(unsigned char *obuf, |
| 553 | size_t obufmax, |
| 554 | const char *ibuf, |
| 555 | size_t *len) |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 556 | { |
| 557 | unsigned char uc, uc2; |
| 558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | *len = strlen(ibuf); |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 560 | |
| 561 | /* Must be even number of bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | if ((*len) & 1) { |
| 563 | return -1; |
| 564 | } |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 565 | *len /= 2; |
| 566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | if ((*len) > obufmax) { |
| 568 | return -1; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 569 | } |
| 570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | while (*ibuf != 0) { |
| 572 | if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) { |
| 573 | return -1; |
| 574 | } |
| 575 | |
| 576 | if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) { |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | *(obuf++) = (uc << 4) | uc2; |
| 581 | } |
| 582 | |
| 583 | return 0; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 584 | } |
| 585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | void mbedtls_test_hexify(unsigned char *obuf, |
| 587 | const unsigned char *ibuf, |
| 588 | int len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 589 | { |
| 590 | unsigned char l, h; |
| 591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | while (len != 0) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 593 | h = *ibuf / 16; |
| 594 | l = *ibuf % 16; |
| 595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 596 | if (h < 10) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 597 | *obuf++ = '0' + h; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | } else { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 599 | *obuf++ = 'a' + h - 10; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 600 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | if (l < 10) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 603 | *obuf++ = '0' + l; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | } else { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 605 | *obuf++ = 'a' + l - 10; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 607 | |
| 608 | ++ibuf; |
| 609 | len--; |
| 610 | } |
| 611 | } |
| 612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | unsigned char *mbedtls_test_zero_alloc(size_t len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 614 | { |
| 615 | void *p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 616 | size_t actual_len = (len != 0) ? len : 1; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 617 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 618 | p = mbedtls_calloc(1, actual_len); |
| 619 | TEST_HELPER_ASSERT(p != NULL); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 621 | memset(p, 0x00, actual_len); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | return p; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 627 | { |
| 628 | unsigned char *obuf; |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 629 | size_t len; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | *olen = strlen(ibuf) / 2; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | if (*olen == 0) { |
| 634 | return mbedtls_test_zero_alloc(*olen); |
| 635 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 637 | obuf = mbedtls_calloc(1, *olen); |
| 638 | TEST_HELPER_ASSERT(obuf != NULL); |
| 639 | TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | return obuf; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b, |
| 645 | uint32_t a_len, uint32_t b_len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 646 | { |
| 647 | int ret = 0; |
| 648 | uint32_t i = 0; |
| 649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | if (a_len != b_len) { |
| 651 | return -1; |
| 652 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | for (i = 0; i < a_len; i++) { |
| 655 | if (a[i] != b[i]) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 656 | ret = -1; |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | return ret; |
| 661 | } |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 662 | |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 663 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 664 | void mbedtls_test_err_add_check(int high, int low, |
| 665 | const char *file, int line) |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 666 | { |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 667 | /* Error codes are always negative (a value of zero is a success) however |
| 668 | * their positive opposites can be easier to understand. The following |
| 669 | * examples given in comments have been made positive for ease of |
| 670 | * understanding. The structure of an error code is such: |
| 671 | * |
Chris Jones | abded0e | 2021-04-12 15:44:47 +0100 | [diff] [blame] | 672 | * shhhhhhhhlllllll |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 673 | * |
| 674 | * s = sign bit. |
Chris Jones | 4f91d8d | 2021-04-23 12:07:25 +0100 | [diff] [blame] | 675 | * h = high level error code (includes high level module ID (bits 12..14) |
| 676 | * and module-dependent error code (bits 7..11)). |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 677 | * l = low level error code. |
| 678 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | if (high > -0x1000 && high != 0) { |
| 680 | /* high < 0001000000000000 |
| 681 | * No high level module ID bits are set. |
| 682 | */ |
| 683 | mbedtls_test_fail("'high' is not a high-level error code", |
| 684 | line, file); |
| 685 | } else if (high < -0x7F80) { |
| 686 | /* high > 0111111110000000 |
| 687 | * Error code is greater than the largest allowed high level module ID. |
| 688 | */ |
| 689 | mbedtls_test_fail("'high' error code is greater than 15 bits", |
| 690 | line, file); |
| 691 | } else if ((high & 0x7F) != 0) { |
| 692 | /* high & 0000000001111111 |
| 693 | * Error code contains low level error code bits. |
| 694 | */ |
| 695 | mbedtls_test_fail("'high' contains a low-level error code", |
| 696 | line, file); |
| 697 | } else if (low < -0x007F) { |
| 698 | /* low > 0000000001111111 |
| 699 | * Error code contains high or module level error code bits. |
| 700 | */ |
| 701 | mbedtls_test_fail("'low' error code is greater than 7 bits", |
| 702 | line, file); |
| 703 | } else if (low > 0) { |
| 704 | mbedtls_test_fail("'low' error code is greater than zero", |
| 705 | line, file); |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | #endif /* MBEDTLS_TEST_HOOKS */ |