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