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