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