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