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