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 |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Ronald Cron | b6d6d4c | 2020-06-03 10:11:18 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <test/helpers.h> |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 19 | #include <test/macros.h> |
| 20 | #include <string.h> |
| 21 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 23 | #include <setjmp.h> |
| 24 | #endif |
| 25 | |
| 26 | /*----------------------------------------------------------------------------*/ |
| 27 | /* Static global variables */ |
| 28 | |
| 29 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 30 | typedef struct |
| 31 | { |
| 32 | uint8_t expected_call; |
| 33 | uint8_t expected_call_happened; |
| 34 | |
| 35 | jmp_buf state; |
| 36 | |
| 37 | mbedtls_test_param_failed_location_record_t location_record; |
| 38 | } |
| 39 | param_failed_ctx_t; |
| 40 | static param_failed_ctx_t param_failed_ctx; |
| 41 | #endif |
| 42 | |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_PLATFORM_C) |
| 44 | static mbedtls_platform_context platform_ctx; |
| 45 | #endif |
| 46 | |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 47 | mbedtls_test_info_t mbedtls_test_info; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 48 | |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 49 | /*----------------------------------------------------------------------------*/ |
| 50 | /* Helper Functions */ |
| 51 | |
Ronald Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 52 | int mbedtls_test_platform_setup( void ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 53 | { |
| 54 | int ret = 0; |
| 55 | #if defined(MBEDTLS_PLATFORM_C) |
| 56 | ret = mbedtls_platform_setup( &platform_ctx ); |
| 57 | #endif /* MBEDTLS_PLATFORM_C */ |
| 58 | return( ret ); |
| 59 | } |
| 60 | |
Ronald Cron | e9c09f1 | 2020-06-08 16:44:58 +0200 | [diff] [blame] | 61 | void mbedtls_test_platform_teardown( void ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 62 | { |
| 63 | #if defined(MBEDTLS_PLATFORM_C) |
| 64 | mbedtls_platform_teardown( &platform_ctx ); |
| 65 | #endif /* MBEDTLS_PLATFORM_C */ |
| 66 | } |
| 67 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 68 | static int ascii2uc(const char c, unsigned char *uc) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 69 | { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 70 | if( ( c >= '0' ) && ( c <= '9' ) ) |
| 71 | *uc = c - '0'; |
| 72 | else if( ( c >= 'a' ) && ( c <= 'f' ) ) |
| 73 | *uc = c - 'a' + 10; |
| 74 | else if( ( c >= 'A' ) && ( c <= 'F' ) ) |
| 75 | *uc = c - 'A' + 10; |
| 76 | else |
| 77 | return( -1 ); |
| 78 | |
| 79 | return( 0 ); |
| 80 | } |
| 81 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 82 | void mbedtls_test_fail( const char *test, int line_no, const char* filename ) |
| 83 | { |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 84 | if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ) |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 85 | { |
| 86 | /* We've already recorded the test as having failed. Don't |
| 87 | * overwrite any previous information about the failure. */ |
| 88 | return; |
| 89 | } |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 90 | mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED; |
| 91 | mbedtls_test_info.test = test; |
| 92 | mbedtls_test_info.line_no = line_no; |
| 93 | mbedtls_test_info.filename = filename; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 96 | void mbedtls_test_skip( const char *test, int line_no, const char* filename ) |
| 97 | { |
Chris Jones | e60e2ae | 2021-01-20 17:51:47 +0000 | [diff] [blame] | 98 | mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED; |
| 99 | mbedtls_test_info.test = test; |
| 100 | mbedtls_test_info.line_no = line_no; |
| 101 | mbedtls_test_info.filename = filename; |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 104 | void mbedtls_test_set_step( unsigned long step ) |
| 105 | { |
| 106 | mbedtls_test_info.step = step; |
| 107 | } |
| 108 | |
| 109 | void mbedtls_test_info_reset( void ) |
| 110 | { |
| 111 | mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS; |
| 112 | mbedtls_test_info.step = (unsigned long)( -1 ); |
| 113 | mbedtls_test_info.test = 0; |
| 114 | mbedtls_test_info.line_no = 0; |
| 115 | mbedtls_test_info.filename = 0; |
Gilles Peskine | b436649 | 2021-04-29 20:28:54 +0200 | [diff] [blame] | 116 | memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) ); |
| 117 | memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) ); |
| 118 | } |
| 119 | |
| 120 | int mbedtls_test_equal( const char *test, int line_no, const char* filename, |
| 121 | unsigned long long value1, unsigned long long value2 ) |
| 122 | { |
| 123 | if( value1 == value2 ) |
| 124 | return( 1 ); |
| 125 | if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ) |
| 126 | { |
| 127 | /* We've already recorded the test as having failed. Don't |
| 128 | * overwrite any previous information about the failure. */ |
| 129 | return( 0 ); |
| 130 | } |
| 131 | mbedtls_test_fail( test, line_no, filename ); |
| 132 | (void) mbedtls_snprintf( mbedtls_test_info.line1, |
| 133 | sizeof( mbedtls_test_info.line1 ), |
| 134 | "lhs = 0x%016llx = %lld", |
| 135 | value1, (long long) value1 ); |
| 136 | (void) mbedtls_snprintf( mbedtls_test_info.line2, |
| 137 | sizeof( mbedtls_test_info.line2 ), |
| 138 | "rhs = 0x%016llx = %lld", |
| 139 | value2, (long long) value2 ); |
| 140 | return( 0 ); |
Chris Jones | a5ab765 | 2021-02-02 16:20:45 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Gilles Peskine | 063700d | 2022-04-13 23:59:52 +0200 | [diff] [blame] | 143 | int mbedtls_test_le_u( const char *test, int line_no, const char* filename, |
| 144 | unsigned long long value1, unsigned long long value2 ) |
| 145 | { |
| 146 | if( value1 <= value2 ) |
| 147 | return( 1 ); |
| 148 | if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ) |
| 149 | { |
| 150 | /* We've already recorded the test as having failed. Don't |
| 151 | * overwrite any previous information about the failure. */ |
| 152 | return( 0 ); |
| 153 | } |
| 154 | mbedtls_test_fail( test, line_no, filename ); |
| 155 | (void) mbedtls_snprintf( mbedtls_test_info.line1, |
| 156 | sizeof( mbedtls_test_info.line1 ), |
| 157 | "lhs = 0x%016llx = %llu", |
| 158 | value1, value1 ); |
| 159 | (void) mbedtls_snprintf( mbedtls_test_info.line2, |
| 160 | sizeof( mbedtls_test_info.line2 ), |
| 161 | "rhs = 0x%016llx = %llu", |
| 162 | value2, value2 ); |
| 163 | return( 0 ); |
| 164 | } |
| 165 | |
| 166 | int mbedtls_test_le_s( const char *test, int line_no, const char* filename, |
| 167 | long long value1, long long value2 ) |
| 168 | { |
| 169 | if( value1 <= value2 ) |
| 170 | return( 1 ); |
| 171 | if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ) |
| 172 | { |
| 173 | /* We've already recorded the test as having failed. Don't |
| 174 | * overwrite any previous information about the failure. */ |
| 175 | return( 0 ); |
| 176 | } |
| 177 | mbedtls_test_fail( test, line_no, filename ); |
| 178 | (void) mbedtls_snprintf( mbedtls_test_info.line1, |
| 179 | sizeof( mbedtls_test_info.line1 ), |
| 180 | "lhs = 0x%016llx = %lld", |
| 181 | (unsigned long long) value1, value1 ); |
| 182 | (void) mbedtls_snprintf( mbedtls_test_info.line2, |
| 183 | sizeof( mbedtls_test_info.line2 ), |
| 184 | "rhs = 0x%016llx = %lld", |
| 185 | (unsigned long long) value2, value2 ); |
| 186 | return( 0 ); |
| 187 | } |
| 188 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 189 | int mbedtls_test_unhexify( unsigned char *obuf, |
| 190 | size_t obufmax, |
| 191 | const char *ibuf, |
| 192 | size_t *len ) |
| 193 | { |
| 194 | unsigned char uc, uc2; |
| 195 | |
| 196 | *len = strlen( ibuf ); |
| 197 | |
| 198 | /* Must be even number of bytes. */ |
| 199 | if ( ( *len ) & 1 ) |
| 200 | return( -1 ); |
| 201 | *len /= 2; |
| 202 | |
| 203 | if ( (*len) > obufmax ) |
| 204 | return( -1 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 205 | |
| 206 | while( *ibuf != 0 ) |
| 207 | { |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 208 | if ( ascii2uc( *(ibuf++), &uc ) != 0 ) |
| 209 | return( -1 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 210 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 211 | if ( ascii2uc( *(ibuf++), &uc2 ) != 0 ) |
| 212 | return( -1 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 213 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 214 | *(obuf++) = ( uc << 4 ) | uc2; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 217 | return( 0 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Ronald Cron | 72d628f | 2020-06-08 17:05:57 +0200 | [diff] [blame] | 220 | void mbedtls_test_hexify( unsigned char *obuf, |
| 221 | const unsigned char *ibuf, |
| 222 | int len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 223 | { |
| 224 | unsigned char l, h; |
| 225 | |
| 226 | while( len != 0 ) |
| 227 | { |
| 228 | h = *ibuf / 16; |
| 229 | l = *ibuf % 16; |
| 230 | |
| 231 | if( h < 10 ) |
| 232 | *obuf++ = '0' + h; |
| 233 | else |
| 234 | *obuf++ = 'a' + h - 10; |
| 235 | |
| 236 | if( l < 10 ) |
| 237 | *obuf++ = '0' + l; |
| 238 | else |
| 239 | *obuf++ = 'a' + l - 10; |
| 240 | |
| 241 | ++ibuf; |
| 242 | len--; |
| 243 | } |
| 244 | } |
| 245 | |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 246 | unsigned char *mbedtls_test_zero_alloc( size_t len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 247 | { |
| 248 | void *p; |
| 249 | size_t actual_len = ( len != 0 ) ? len : 1; |
| 250 | |
| 251 | p = mbedtls_calloc( 1, actual_len ); |
| 252 | TEST_HELPER_ASSERT( p != NULL ); |
| 253 | |
| 254 | memset( p, 0x00, actual_len ); |
| 255 | |
| 256 | return( p ); |
| 257 | } |
| 258 | |
Ronald Cron | a256c70 | 2020-06-10 10:53:11 +0200 | [diff] [blame] | 259 | unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 260 | { |
| 261 | unsigned char *obuf; |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 262 | size_t len; |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 263 | |
| 264 | *olen = strlen( ibuf ) / 2; |
| 265 | |
| 266 | if( *olen == 0 ) |
Ronald Cron | 690f3eb | 2020-06-10 10:42:18 +0200 | [diff] [blame] | 267 | return( mbedtls_test_zero_alloc( *olen ) ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 268 | |
| 269 | obuf = mbedtls_calloc( 1, *olen ); |
| 270 | TEST_HELPER_ASSERT( obuf != NULL ); |
Ronald Cron | a0c2539 | 2020-06-18 10:10:46 +0200 | [diff] [blame] | 271 | TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 ); |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 272 | |
| 273 | return( obuf ); |
| 274 | } |
| 275 | |
Ronald Cron | de70b16 | 2020-06-10 11:03:08 +0200 | [diff] [blame] | 276 | int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b, |
| 277 | uint32_t a_len, uint32_t b_len ) |
Ronald Cron | f40529d | 2020-06-09 16:27:37 +0200 | [diff] [blame] | 278 | { |
| 279 | int ret = 0; |
| 280 | uint32_t i = 0; |
| 281 | |
| 282 | if( a_len != b_len ) |
| 283 | return( -1 ); |
| 284 | |
| 285 | for( i = 0; i < a_len; i++ ) |
| 286 | { |
| 287 | if( a[i] != b[i] ) |
| 288 | { |
| 289 | ret = -1; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | return ret; |
| 294 | } |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 295 | |
| 296 | #if defined(MBEDTLS_CHECK_PARAMS) |
| 297 | void mbedtls_test_param_failed_get_location_record( |
| 298 | mbedtls_test_param_failed_location_record_t *location_record ) |
| 299 | { |
| 300 | *location_record = param_failed_ctx.location_record; |
| 301 | } |
| 302 | |
| 303 | void mbedtls_test_param_failed_expect_call( void ) |
| 304 | { |
| 305 | param_failed_ctx.expected_call_happened = 0; |
| 306 | param_failed_ctx.expected_call = 1; |
| 307 | } |
| 308 | |
| 309 | int mbedtls_test_param_failed_check_expected_call( void ) |
| 310 | { |
| 311 | param_failed_ctx.expected_call = 0; |
| 312 | |
| 313 | if( param_failed_ctx.expected_call_happened != 0 ) |
| 314 | return( 0 ); |
| 315 | |
| 316 | return( -1 ); |
| 317 | } |
| 318 | |
| 319 | void* mbedtls_test_param_failed_get_state_buf( void ) |
| 320 | { |
Ronald Cron | bf4f408 | 2020-09-25 10:45:06 +0200 | [diff] [blame] | 321 | return ¶m_failed_ctx.state; |
Ronald Cron | a123614 | 2020-07-01 16:01:21 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void mbedtls_test_param_failed_reset_state( void ) |
| 325 | { |
| 326 | memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) ); |
| 327 | } |
| 328 | |
| 329 | void mbedtls_param_failed( const char *failure_condition, |
| 330 | const char *file, |
| 331 | int line ) |
| 332 | { |
| 333 | /* Record the location of the failure */ |
| 334 | param_failed_ctx.location_record.failure_condition = failure_condition; |
| 335 | param_failed_ctx.location_record.file = file; |
| 336 | param_failed_ctx.location_record.line = line; |
| 337 | |
| 338 | /* If we are testing the callback function... */ |
| 339 | if( param_failed_ctx.expected_call != 0 ) |
| 340 | { |
| 341 | param_failed_ctx.expected_call = 0; |
| 342 | param_failed_ctx.expected_call_happened = 1; |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | /* ...else try a long jump. If the execution state has not been set-up |
| 347 | * or reset then the long jump buffer is all zero's and the call will |
| 348 | * with high probability fault, emphasizing there is something to look |
| 349 | * at. |
| 350 | */ |
| 351 | |
| 352 | longjmp( param_failed_ctx.state, 1 ); |
| 353 | } |
| 354 | } |
| 355 | #endif /* MBEDTLS_CHECK_PARAMS */ |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 356 | |
| 357 | #if defined(MBEDTLS_TEST_HOOKS) |
| 358 | void mbedtls_test_err_add_check( int high, int low, |
| 359 | const char *file, int line ) |
| 360 | { |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 361 | /* Error codes are always negative (a value of zero is a success) however |
| 362 | * their positive opposites can be easier to understand. The following |
| 363 | * examples given in comments have been made positive for ease of |
| 364 | * understanding. The structure of an error code is such: |
| 365 | * |
Chris Jones | abded0e | 2021-04-12 15:44:47 +0100 | [diff] [blame] | 366 | * shhhhhhhhlllllll |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 367 | * |
| 368 | * s = sign bit. |
Chris Jones | 4f91d8d | 2021-04-23 12:07:25 +0100 | [diff] [blame] | 369 | * h = high level error code (includes high level module ID (bits 12..14) |
| 370 | * and module-dependent error code (bits 7..11)). |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 371 | * l = low level error code. |
| 372 | */ |
Chris Jones | e11e814 | 2021-04-22 15:28:56 +0100 | [diff] [blame] | 373 | if ( high > -0x1000 && high != 0 ) |
| 374 | /* high < 0001000000000000 |
Chris Jones | 4f91d8d | 2021-04-23 12:07:25 +0100 | [diff] [blame] | 375 | * No high level module ID bits are set. |
Chris Jones | e11e814 | 2021-04-22 15:28:56 +0100 | [diff] [blame] | 376 | */ |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 377 | { |
Chris Jones | fe285f5 | 2021-02-08 12:32:41 +0000 | [diff] [blame] | 378 | mbedtls_test_fail( "'high' is not a high-level error code", |
| 379 | line, file ); |
Chris Jones | a203c38 | 2021-01-29 14:56:20 +0000 | [diff] [blame] | 380 | } |
Chris Jones | e11e814 | 2021-04-22 15:28:56 +0100 | [diff] [blame] | 381 | else if ( high < -0x7F80 ) |
| 382 | /* high > 0111111110000000 |
Chris Jones | 860f509 | 2021-04-26 16:31:16 +0100 | [diff] [blame] | 383 | * Error code is greater than the largest allowed high level module ID. |
Chris Jones | e11e814 | 2021-04-22 15:28:56 +0100 | [diff] [blame] | 384 | */ |
Chris Jones | a203c38 | 2021-01-29 14:56:20 +0000 | [diff] [blame] | 385 | { |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 386 | mbedtls_test_fail( "'high' error code is greater than 15 bits", |
| 387 | line, file ); |
Chris Jones | a203c38 | 2021-01-29 14:56:20 +0000 | [diff] [blame] | 388 | } |
Chris Jones | e11e814 | 2021-04-22 15:28:56 +0100 | [diff] [blame] | 389 | else if ( ( high & 0x7F ) != 0 ) |
| 390 | /* high & 0000000001111111 |
| 391 | * Error code contains low level error code bits. |
| 392 | */ |
Chris Jones | a203c38 | 2021-01-29 14:56:20 +0000 | [diff] [blame] | 393 | { |
Chris Jones | fe285f5 | 2021-02-08 12:32:41 +0000 | [diff] [blame] | 394 | mbedtls_test_fail( "'high' contains a low-level error code", |
| 395 | line, file ); |
Chris Jones | a203c38 | 2021-01-29 14:56:20 +0000 | [diff] [blame] | 396 | } |
Chris Jones | e11e814 | 2021-04-22 15:28:56 +0100 | [diff] [blame] | 397 | else if ( low < -0x007F ) |
| 398 | /* low > 0000000001111111 |
| 399 | * Error code contains high or module level error code bits. |
| 400 | */ |
Chris Jones | a203c38 | 2021-01-29 14:56:20 +0000 | [diff] [blame] | 401 | { |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 402 | mbedtls_test_fail( "'low' error code is greater than 7 bits", |
| 403 | line, file ); |
Chris Jones | a203c38 | 2021-01-29 14:56:20 +0000 | [diff] [blame] | 404 | } |
| 405 | else if ( low > 0 ) |
| 406 | { |
Chris Jones | 3f613c1 | 2021-03-31 09:34:22 +0100 | [diff] [blame] | 407 | mbedtls_test_fail( "'low' error code is greater than zero", |
| 408 | line, file ); |
Chris Jones | 96ae73b | 2021-01-08 17:04:59 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | #endif /* MBEDTLS_TEST_HOOKS */ |
Gilles Peskine | db47971 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 412 | |
| 413 | #if defined(MBEDTLS_BIGNUM_C) |
Werner Lewis | 24b6078 | 2022-07-07 15:08:17 +0100 | [diff] [blame^] | 414 | int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s ) |
Gilles Peskine | db47971 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 415 | { |
| 416 | /* mbedtls_mpi_read_string() currently retains leading zeros. |
| 417 | * It always allocates at least one limb for the value 0. */ |
| 418 | if( s[0] == 0 ) |
| 419 | { |
| 420 | mbedtls_mpi_free( X ); |
| 421 | return( 0 ); |
| 422 | } |
| 423 | else |
Werner Lewis | 24b6078 | 2022-07-07 15:08:17 +0100 | [diff] [blame^] | 424 | return( mbedtls_mpi_read_string( X, 16, s ) ); |
Gilles Peskine | db47971 | 2021-06-11 14:13:53 +0200 | [diff] [blame] | 425 | } |
| 426 | #endif |