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