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