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