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 | { |
| 232 | return mbedtls_test_info.mutex_usage_error; |
| 233 | } |
| 234 | |
| 235 | void mbedtls_test_set_mutex_usage_error(const char *msg) |
| 236 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 237 | #ifdef MBEDTLS_THREADING_C |
| 238 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 239 | #endif /* MBEDTLS_THREADING_C */ |
| 240 | |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 241 | if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) { |
| 242 | mbedtls_test_info.mutex_usage_error = msg; |
| 243 | } |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 244 | |
| 245 | #ifdef MBEDTLS_THREADING_C |
| 246 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 247 | #endif /* MBEDTLS_THREADING_C */ |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 248 | } |
| 249 | #endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE) |
| 250 | |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 251 | #if defined(MBEDTLS_BIGNUM_C) |
| 252 | |
| 253 | unsigned mbedtls_test_get_case_uses_negative_0(void) |
| 254 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 255 | unsigned test_case_uses_negative_0 = 0; |
| 256 | #ifdef MBEDTLS_THREADING_C |
| 257 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 258 | #endif /* MBEDTLS_THREADING_C */ |
| 259 | test_case_uses_negative_0 = mbedtls_test_info.case_uses_negative_0; |
| 260 | |
| 261 | #ifdef MBEDTLS_THREADING_C |
| 262 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 263 | #endif /* MBEDTLS_THREADING_C */ |
| 264 | |
| 265 | return test_case_uses_negative_0; |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 268 | static void mbedtls_test_set_case_uses_negative_0_internal(unsigned uses) |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 269 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 270 | /* Internal function only - mbedtls_test_info_mutex should be held prior |
Paul Elliott | 9efc602 | 2024-01-31 15:33:23 +0000 | [diff] [blame] | 271 | * to calling this function. */ |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 272 | |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 273 | mbedtls_test_info.case_uses_negative_0 = uses; |
| 274 | } |
| 275 | |
| 276 | void mbedtls_test_increment_case_uses_negative_0(void) |
| 277 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 278 | #ifdef MBEDTLS_THREADING_C |
| 279 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 280 | #endif /* MBEDTLS_THREADING_C */ |
| 281 | |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 282 | ++mbedtls_test_info.case_uses_negative_0; |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 283 | |
| 284 | #ifdef MBEDTLS_THREADING_C |
| 285 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 286 | #endif /* MBEDTLS_THREADING_C */ |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Paul Elliott | 3d2db89 | 2024-01-19 20:42:56 +0000 | [diff] [blame] | 289 | #endif /* MBEDTLS_BIGNUM_C */ |
| 290 | |
| 291 | #ifdef MBEDTLS_TEST_MUTEX_USAGE |
| 292 | mbedtls_threading_mutex_t *mbedtls_test_get_info_mutex(void) |
| 293 | { |
| 294 | return &mbedtls_test_info_mutex; |
| 295 | } |
| 296 | |
| 297 | #endif /* MBEDTLS_TEST_MUTEX_USAGE */ |
Paul Elliott | c7a1e99 | 2023-11-03 18:44:57 +0000 | [diff] [blame] | 298 | |
Paul Elliott | 4580d4d | 2023-10-27 18:41:02 +0100 | [diff] [blame] | 299 | /*----------------------------------------------------------------------------*/ |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 300 | /* Helper Functions */ |
| 301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | int mbedtls_test_platform_setup(void) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 303 | { |
| 304 | int ret = 0; |
Gilles Peskine | c2d16b2 | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 305 | |
| 306 | #if defined(MBEDTLS_PSA_INJECT_ENTROPY) |
| 307 | /* Make sure that injected entropy is present. Otherwise |
| 308 | * psa_crypto_init() will fail. This is not necessary for test suites |
| 309 | * that don't use PSA, but it's harmless (except for leaving a file |
| 310 | * behind). */ |
| 311 | ret = mbedtls_test_inject_entropy_restore(); |
| 312 | if (ret != 0) { |
| 313 | return ret; |
| 314 | } |
| 315 | #endif |
| 316 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 317 | #if defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | ret = mbedtls_platform_setup(&platform_ctx); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 319 | #endif /* MBEDTLS_PLATFORM_C */ |
Gilles Peskine | c2d16b2 | 2023-04-28 23:39:45 +0200 | [diff] [blame] | 320 | |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 321 | #ifdef MBEDTLS_THREADING_C |
| 322 | mbedtls_mutex_init(&mbedtls_test_info_mutex); |
| 323 | #endif /* MBEDTLS_THREADING_C */ |
| 324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | return ret; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | void mbedtls_test_platform_teardown(void) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 329 | { |
Paul Elliott | 6506426 | 2023-11-27 17:29:05 +0000 | [diff] [blame] | 330 | #ifdef MBEDTLS_THREADING_C |
| 331 | mbedtls_mutex_free(&mbedtls_test_info_mutex); |
| 332 | #endif /* MBEDTLS_THREADING_C */ |
| 333 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 334 | #if defined(MBEDTLS_PLATFORM_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | mbedtls_platform_teardown(&platform_ctx); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 336 | #endif /* MBEDTLS_PLATFORM_C */ |
| 337 | } |
| 338 | |
Gilles Peskine | 881447d | 2022-12-08 15:24:52 +0100 | [diff] [blame] | 339 | int mbedtls_test_ascii2uc(const char c, unsigned char *uc) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 340 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | if ((c >= '0') && (c <= '9')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 342 | *uc = c - '0'; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | } else if ((c >= 'a') && (c <= 'f')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 344 | *uc = c - 'a' + 10; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | } else if ((c >= 'A') && (c <= 'F')) { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 346 | *uc = c - 'A' + 10; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | } else { |
| 348 | return -1; |
| 349 | } |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | return 0; |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 354 | 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] | 355 | { |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 356 | /* Internal function only - mbedtls_test_info_mutex should be held prior |
| 357 | * to calling this function. */ |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 358 | |
| 359 | /* Don't use accessor, we already hold mutex. */ |
| 360 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
| 361 | /* 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] | 362 | * overwrite any previous information about the failure. */ |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 363 | 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] | 364 | } |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void mbedtls_test_fail(const char *test, int line_no, const char *filename) |
| 368 | { |
| 369 | #ifdef MBEDTLS_THREADING_C |
| 370 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 371 | #endif /* MBEDTLS_THREADING_C */ |
| 372 | |
| 373 | mbedtls_test_fail_internal(test, line_no, filename); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 374 | |
| 375 | #ifdef MBEDTLS_THREADING_C |
| 376 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 377 | #endif /* MBEDTLS_THREADING_C */ |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | 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] | 381 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 382 | #ifdef MBEDTLS_THREADING_C |
| 383 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 384 | #endif /* MBEDTLS_THREADING_C */ |
| 385 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 386 | 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] | 387 | |
| 388 | #ifdef MBEDTLS_THREADING_C |
| 389 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 390 | #endif /* MBEDTLS_THREADING_C */ |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | void mbedtls_test_info_reset(void) |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 394 | { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 395 | #ifdef MBEDTLS_THREADING_C |
| 396 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 397 | #endif /* MBEDTLS_THREADING_C */ |
| 398 | |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 399 | mbedtls_test_set_result_internal(MBEDTLS_TEST_RESULT_SUCCESS, 0, 0, 0); |
| 400 | mbedtls_test_reset_step_internal(); |
| 401 | mbedtls_test_set_line1_internal(NULL); |
| 402 | mbedtls_test_set_line2_internal(NULL); |
Paul Elliott | 5c498f3 | 2023-10-31 16:38:56 +0000 | [diff] [blame] | 403 | |
Gilles Peskine | ca6e8aa | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 404 | #if defined(MBEDTLS_BIGNUM_C) |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 405 | mbedtls_test_set_case_uses_negative_0_internal(0); |
Gilles Peskine | ca6e8aa | 2022-11-09 21:08:44 +0100 | [diff] [blame] | 406 | #endif |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 407 | |
| 408 | #ifdef MBEDTLS_THREADING_C |
Paul Elliott | 0b2835d | 2024-02-01 13:27:04 +0000 | [diff] [blame] | 409 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 410 | #endif /* MBEDTLS_THREADING_C */ |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | int mbedtls_test_equal(const char *test, int line_no, const char *filename, |
| 414 | unsigned long long value1, unsigned long long value2) |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 415 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 417 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 418 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | if (value1 == value2) { |
| 420 | return 1; |
| 421 | } |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 422 | |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 423 | #ifdef MBEDTLS_THREADING_C |
| 424 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 425 | #endif /* MBEDTLS_THREADING_C */ |
| 426 | |
| 427 | /* Don't use accessor, as we already hold mutex. */ |
| 428 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
| 429 | /* 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] | 430 | * overwrite any previous information about the failure. */ |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 431 | |
| 432 | char buf[MBEDTLS_TEST_LINE_LENGTH]; |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 433 | mbedtls_test_fail_internal(test, line_no, filename); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 434 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 435 | "lhs = 0x%016llx = %lld", |
| 436 | value1, (long long) value1); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 437 | mbedtls_test_set_line1_internal(buf); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 438 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 439 | "rhs = 0x%016llx = %lld", |
| 440 | value2, (long long) value2); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 441 | mbedtls_test_set_line2_internal(buf); |
Gilles Peskine | 89615ee | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 442 | } |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 443 | |
| 444 | #ifdef MBEDTLS_THREADING_C |
| 445 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 446 | #endif /* MBEDTLS_THREADING_C */ |
| 447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | return 0; |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | int mbedtls_test_le_u(const char *test, int line_no, const char *filename, |
| 452 | unsigned long long value1, unsigned long long value2) |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 453 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 455 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 457 | if (value1 <= value2) { |
| 458 | return 1; |
| 459 | } |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 460 | |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 461 | #ifdef MBEDTLS_THREADING_C |
| 462 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 463 | #endif /* MBEDTLS_THREADING_C */ |
| 464 | |
| 465 | /* Don't use accessor, we already hold mutex. */ |
| 466 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
| 467 | /* 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] | 468 | * overwrite any previous information about the failure. */ |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 469 | |
| 470 | char buf[MBEDTLS_TEST_LINE_LENGTH]; |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 471 | mbedtls_test_fail_internal(test, line_no, filename); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 472 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 473 | "lhs = 0x%016llx = %llu", |
| 474 | value1, value1); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 475 | mbedtls_test_set_line1_internal(buf); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 476 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 477 | "rhs = 0x%016llx = %llu", |
| 478 | value2, value2); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 479 | mbedtls_test_set_line2_internal(buf); |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 480 | } |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 481 | |
| 482 | #ifdef MBEDTLS_THREADING_C |
| 483 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 484 | #endif /* MBEDTLS_THREADING_C */ |
| 485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | return 0; |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 487 | } |
| 488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | int mbedtls_test_le_s(const char *test, int line_no, const char *filename, |
| 490 | long long value1, long long value2) |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 491 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | TEST_CF_PUBLIC(&value1, sizeof(value1)); |
| 493 | TEST_CF_PUBLIC(&value2, sizeof(value2)); |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | if (value1 <= value2) { |
| 496 | return 1; |
| 497 | } |
Gilles Peskine | bdc7b8b | 2022-09-20 18:31:30 +0200 | [diff] [blame] | 498 | |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 499 | #ifdef MBEDTLS_THREADING_C |
| 500 | mbedtls_mutex_lock(&mbedtls_test_info_mutex); |
| 501 | #endif /* MBEDTLS_THREADING_C */ |
| 502 | |
| 503 | /* Don't use accessor, we already hold mutex. */ |
Paul Elliott | f20728e | 2024-02-06 12:49:45 +0000 | [diff] [blame] | 504 | if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) { |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 505 | /* 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] | 506 | * overwrite any previous information about the failure. */ |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 507 | |
| 508 | char buf[MBEDTLS_TEST_LINE_LENGTH]; |
Paul Elliott | 9718203 | 2024-02-13 13:27:06 +0000 | [diff] [blame] | 509 | mbedtls_test_fail_internal(test, line_no, filename); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 510 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 511 | "lhs = 0x%016llx = %lld", |
| 512 | (unsigned long long) value1, value1); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 513 | mbedtls_test_set_line1_internal(buf); |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 514 | (void) mbedtls_snprintf(buf, sizeof(buf), |
| 515 | "rhs = 0x%016llx = %lld", |
| 516 | (unsigned long long) value2, value2); |
Paul Elliott | 264e210 | 2024-02-15 12:28:56 +0000 | [diff] [blame^] | 517 | mbedtls_test_set_line2_internal(buf); |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 518 | } |
Paul Elliott | fad978b | 2024-01-30 18:00:26 +0000 | [diff] [blame] | 519 | |
| 520 | #ifdef MBEDTLS_THREADING_C |
| 521 | mbedtls_mutex_unlock(&mbedtls_test_info_mutex); |
| 522 | #endif /* MBEDTLS_THREADING_C */ |
| 523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | return 0; |
Gilles Peskine | d146542 | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | int mbedtls_test_unhexify(unsigned char *obuf, |
| 528 | size_t obufmax, |
| 529 | const char *ibuf, |
| 530 | size_t *len) |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 531 | { |
| 532 | unsigned char uc, uc2; |
| 533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | *len = strlen(ibuf); |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 535 | |
| 536 | /* Must be even number of bytes. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | if ((*len) & 1) { |
| 538 | return -1; |
| 539 | } |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 540 | *len /= 2; |
| 541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | if ((*len) > obufmax) { |
| 543 | return -1; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | while (*ibuf != 0) { |
| 547 | if (mbedtls_test_ascii2uc(*(ibuf++), &uc) != 0) { |
| 548 | return -1; |
| 549 | } |
| 550 | |
| 551 | if (mbedtls_test_ascii2uc(*(ibuf++), &uc2) != 0) { |
| 552 | return -1; |
| 553 | } |
| 554 | |
| 555 | *(obuf++) = (uc << 4) | uc2; |
| 556 | } |
| 557 | |
| 558 | return 0; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 561 | void mbedtls_test_hexify(unsigned char *obuf, |
| 562 | const unsigned char *ibuf, |
| 563 | int len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 564 | { |
| 565 | unsigned char l, h; |
| 566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | while (len != 0) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 568 | h = *ibuf / 16; |
| 569 | l = *ibuf % 16; |
| 570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | if (h < 10) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 572 | *obuf++ = '0' + h; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | } else { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 574 | *obuf++ = 'a' + h - 10; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | if (l < 10) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 578 | *obuf++ = '0' + l; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 579 | } else { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 580 | *obuf++ = 'a' + l - 10; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 581 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 582 | |
| 583 | ++ibuf; |
| 584 | len--; |
| 585 | } |
| 586 | } |
| 587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | unsigned char *mbedtls_test_zero_alloc(size_t len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 589 | { |
| 590 | void *p; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | size_t actual_len = (len != 0) ? len : 1; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | p = mbedtls_calloc(1, actual_len); |
| 594 | TEST_HELPER_ASSERT(p != NULL); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 596 | memset(p, 0x00, actual_len); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | return p; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 599 | } |
| 600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 601 | unsigned char *mbedtls_test_unhexify_alloc(const char *ibuf, size_t *olen) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 602 | { |
| 603 | unsigned char *obuf; |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 604 | size_t len; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | *olen = strlen(ibuf) / 2; |
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 | if (*olen == 0) { |
| 609 | return mbedtls_test_zero_alloc(*olen); |
| 610 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | obuf = mbedtls_calloc(1, *olen); |
| 613 | TEST_HELPER_ASSERT(obuf != NULL); |
| 614 | TEST_HELPER_ASSERT(mbedtls_test_unhexify(obuf, *olen, ibuf, &len) == 0); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 616 | return obuf; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 619 | int mbedtls_test_hexcmp(uint8_t *a, uint8_t *b, |
| 620 | uint32_t a_len, uint32_t b_len) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 621 | { |
| 622 | int ret = 0; |
| 623 | uint32_t i = 0; |
| 624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | if (a_len != b_len) { |
| 626 | return -1; |
| 627 | } |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | for (i = 0; i < a_len; i++) { |
| 630 | if (a[i] != b[i]) { |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 631 | ret = -1; |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | return ret; |
| 636 | } |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 637 | |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 638 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | void mbedtls_test_err_add_check(int high, int low, |
| 640 | const char *file, int line) |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 641 | { |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 642 | /* Error codes are always negative (a value of zero is a success) however |
| 643 | * their positive opposites can be easier to understand. The following |
| 644 | * examples given in comments have been made positive for ease of |
| 645 | * understanding. The structure of an error code is such: |
| 646 | * |
Chris Jones | abded0e | 2021-04-12 15:44:47 +0100 | [diff] [blame] | 647 | * shhhhhhhhlllllll |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 648 | * |
| 649 | * s = sign bit. |
Chris Jones | 4f91d8d | 2021-04-23 12:07:25 +0100 | [diff] [blame] | 650 | * h = high level error code (includes high level module ID (bits 12..14) |
| 651 | * and module-dependent error code (bits 7..11)). |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 652 | * l = low level error code. |
| 653 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | if (high > -0x1000 && high != 0) { |
| 655 | /* high < 0001000000000000 |
| 656 | * No high level module ID bits are set. |
| 657 | */ |
| 658 | mbedtls_test_fail("'high' is not a high-level error code", |
| 659 | line, file); |
| 660 | } else if (high < -0x7F80) { |
| 661 | /* high > 0111111110000000 |
| 662 | * Error code is greater than the largest allowed high level module ID. |
| 663 | */ |
| 664 | mbedtls_test_fail("'high' error code is greater than 15 bits", |
| 665 | line, file); |
| 666 | } else if ((high & 0x7F) != 0) { |
| 667 | /* high & 0000000001111111 |
| 668 | * Error code contains low level error code bits. |
| 669 | */ |
| 670 | mbedtls_test_fail("'high' contains a low-level error code", |
| 671 | line, file); |
| 672 | } else if (low < -0x007F) { |
| 673 | /* low > 0000000001111111 |
| 674 | * Error code contains high or module level error code bits. |
| 675 | */ |
| 676 | mbedtls_test_fail("'low' error code is greater than 7 bits", |
| 677 | line, file); |
| 678 | } else if (low > 0) { |
| 679 | mbedtls_test_fail("'low' error code is greater than zero", |
| 680 | line, file); |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | #endif /* MBEDTLS_TEST_HOOKS */ |