blob: 7c83714f19e45ab9487d24fe234a940a2506ed06 [file] [log] [blame]
Bence Szépkúti86974652020-06-15 11:59:37 +02001/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02002 * Copyright The Mbed TLS Contributors
Ronald Cronb6d6d4c2020-06-03 10:11:18 +02003 * 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 Cronb6d6d4c2020-06-03 10:11:18 +020016 */
17
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +020018#include <test/constant_flow.h>
Ronald Cronb6d6d4c2020-06-03 10:11:18 +020019#include <test/helpers.h>
Ronald Cronf40529d2020-06-09 16:27:37 +020020#include <test/macros.h>
21#include <string.h>
22
Ronald Crona1236142020-07-01 16:01:21 +020023/*----------------------------------------------------------------------------*/
24/* Static global variables */
25
Ronald Cronf40529d2020-06-09 16:27:37 +020026#if defined(MBEDTLS_PLATFORM_C)
27static mbedtls_platform_context platform_ctx;
28#endif
29
Chris Jonese60e2ae2021-01-20 17:51:47 +000030mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000031
Ronald Crona1236142020-07-01 16:01:21 +020032/*----------------------------------------------------------------------------*/
33/* Helper Functions */
34
Ronald Crone9c09f12020-06-08 16:44:58 +020035int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020036{
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 Crone9c09f12020-06-08 16:44:58 +020044void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020045{
46#if defined(MBEDTLS_PLATFORM_C)
47 mbedtls_platform_teardown( &platform_ctx );
48#endif /* MBEDTLS_PLATFORM_C */
49}
50
Ronald Crona0c25392020-06-18 10:10:46 +020051static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020052{
Ronald Crona0c25392020-06-18 10:10:46 +020053 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 Jones9634bb12021-01-20 15:56:42 +000065void mbedtls_test_fail( const char *test, int line_no, const char* filename )
66{
Chris Jonese60e2ae2021-01-20 17:51:47 +000067 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
Chris Jones9634bb12021-01-20 15:56:42 +000068 {
69 /* We've already recorded the test as having failed. Don't
70 * overwrite any previous information about the failure. */
71 return;
72 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000073 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 Jones9634bb12021-01-20 15:56:42 +000077}
78
Chris Jones9634bb12021-01-20 15:56:42 +000079void mbedtls_test_skip( const char *test, int line_no, const char* filename )
80{
Chris Jonese60e2ae2021-01-20 17:51:47 +000081 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 Jones9634bb12021-01-20 15:56:42 +000085}
86
Chris Jonesa5ab7652021-02-02 16:20:45 +000087void mbedtls_test_set_step( unsigned long step )
88{
89 mbedtls_test_info.step = step;
90}
91
Gilles Peskineca6e8aa2022-11-09 21:08:44 +010092#if defined(MBEDTLS_BIGNUM_C)
93unsigned mbedtls_test_case_uses_negative_0 = 0;
94#endif
95
Chris Jonesa5ab7652021-02-02 16:20:45 +000096void mbedtls_test_info_reset( void )
97{
98 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
99 mbedtls_test_info.step = (unsigned long)( -1 );
100 mbedtls_test_info.test = 0;
101 mbedtls_test_info.line_no = 0;
102 mbedtls_test_info.filename = 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200103 memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) );
104 memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) );
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100105#if defined(MBEDTLS_BIGNUM_C)
106 mbedtls_test_case_uses_negative_0 = 0;
107#endif
Gilles Peskine89615ee2021-04-29 20:28:54 +0200108}
109
110int mbedtls_test_equal( const char *test, int line_no, const char* filename,
111 unsigned long long value1, unsigned long long value2 )
112{
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200113 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
114 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
115
Gilles Peskine89615ee2021-04-29 20:28:54 +0200116 if( value1 == value2 )
117 return( 1 );
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200118
Gilles Peskine89615ee2021-04-29 20:28:54 +0200119 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
120 {
121 /* We've already recorded the test as having failed. Don't
122 * overwrite any previous information about the failure. */
123 return( 0 );
124 }
125 mbedtls_test_fail( test, line_no, filename );
126 (void) mbedtls_snprintf( mbedtls_test_info.line1,
127 sizeof( mbedtls_test_info.line1 ),
128 "lhs = 0x%016llx = %lld",
129 value1, (long long) value1 );
130 (void) mbedtls_snprintf( mbedtls_test_info.line2,
131 sizeof( mbedtls_test_info.line2 ),
132 "rhs = 0x%016llx = %lld",
133 value2, (long long) value2 );
134 return( 0 );
Chris Jonesa5ab7652021-02-02 16:20:45 +0000135}
136
Gilles Peskined1465422022-04-13 23:59:52 +0200137int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
138 unsigned long long value1, unsigned long long value2 )
139{
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200140 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
141 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
142
Gilles Peskined1465422022-04-13 23:59:52 +0200143 if( value1 <= value2 )
144 return( 1 );
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200145
Gilles Peskined1465422022-04-13 23:59:52 +0200146 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
147 {
148 /* We've already recorded the test as having failed. Don't
149 * overwrite any previous information about the failure. */
150 return( 0 );
151 }
152 mbedtls_test_fail( test, line_no, filename );
153 (void) mbedtls_snprintf( mbedtls_test_info.line1,
154 sizeof( mbedtls_test_info.line1 ),
155 "lhs = 0x%016llx = %llu",
156 value1, value1 );
157 (void) mbedtls_snprintf( mbedtls_test_info.line2,
158 sizeof( mbedtls_test_info.line2 ),
159 "rhs = 0x%016llx = %llu",
160 value2, value2 );
161 return( 0 );
162}
163
164int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
165 long long value1, long long value2 )
166{
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200167 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
168 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
169
Gilles Peskined1465422022-04-13 23:59:52 +0200170 if( value1 <= value2 )
171 return( 1 );
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200172
Gilles Peskined1465422022-04-13 23:59:52 +0200173 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
174 {
175 /* We've already recorded the test as having failed. Don't
176 * overwrite any previous information about the failure. */
177 return( 0 );
178 }
179 mbedtls_test_fail( test, line_no, filename );
180 (void) mbedtls_snprintf( mbedtls_test_info.line1,
181 sizeof( mbedtls_test_info.line1 ),
182 "lhs = 0x%016llx = %lld",
183 (unsigned long long) value1, value1 );
184 (void) mbedtls_snprintf( mbedtls_test_info.line2,
185 sizeof( mbedtls_test_info.line2 ),
186 "rhs = 0x%016llx = %lld",
187 (unsigned long long) value2, value2 );
188 return( 0 );
189}
190
Ronald Crona0c25392020-06-18 10:10:46 +0200191int mbedtls_test_unhexify( unsigned char *obuf,
192 size_t obufmax,
193 const char *ibuf,
194 size_t *len )
195{
196 unsigned char uc, uc2;
197
198 *len = strlen( ibuf );
199
200 /* Must be even number of bytes. */
201 if ( ( *len ) & 1 )
202 return( -1 );
203 *len /= 2;
204
205 if ( (*len) > obufmax )
206 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200207
208 while( *ibuf != 0 )
209 {
Ronald Crona0c25392020-06-18 10:10:46 +0200210 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
211 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200212
Ronald Crona0c25392020-06-18 10:10:46 +0200213 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
214 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200215
Ronald Crona0c25392020-06-18 10:10:46 +0200216 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200217 }
218
Ronald Crona0c25392020-06-18 10:10:46 +0200219 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200220}
221
Ronald Cron72d628f2020-06-08 17:05:57 +0200222void mbedtls_test_hexify( unsigned char *obuf,
223 const unsigned char *ibuf,
224 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200225{
226 unsigned char l, h;
227
228 while( len != 0 )
229 {
230 h = *ibuf / 16;
231 l = *ibuf % 16;
232
233 if( h < 10 )
234 *obuf++ = '0' + h;
235 else
236 *obuf++ = 'a' + h - 10;
237
238 if( l < 10 )
239 *obuf++ = '0' + l;
240 else
241 *obuf++ = 'a' + l - 10;
242
243 ++ibuf;
244 len--;
245 }
246}
247
Ronald Cron690f3eb2020-06-10 10:42:18 +0200248unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200249{
250 void *p;
251 size_t actual_len = ( len != 0 ) ? len : 1;
252
253 p = mbedtls_calloc( 1, actual_len );
254 TEST_HELPER_ASSERT( p != NULL );
255
256 memset( p, 0x00, actual_len );
257
258 return( p );
259}
260
Ronald Crona256c702020-06-10 10:53:11 +0200261unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200262{
263 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200264 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200265
266 *olen = strlen( ibuf ) / 2;
267
268 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200269 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200270
271 obuf = mbedtls_calloc( 1, *olen );
272 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200273 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200274
275 return( obuf );
276}
277
Ronald Cronde70b162020-06-10 11:03:08 +0200278int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
279 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200280{
281 int ret = 0;
282 uint32_t i = 0;
283
284 if( a_len != b_len )
285 return( -1 );
286
287 for( i = 0; i < a_len; i++ )
288 {
289 if( a[i] != b[i] )
290 {
291 ret = -1;
292 break;
293 }
294 }
295 return ret;
296}
Ronald Crona1236142020-07-01 16:01:21 +0200297
Chris Jones96ae73b2021-01-08 17:04:59 +0000298#if defined(MBEDTLS_TEST_HOOKS)
299void mbedtls_test_err_add_check( int high, int low,
300 const char *file, int line )
301{
Chris Jones3f613c12021-03-31 09:34:22 +0100302 /* Error codes are always negative (a value of zero is a success) however
303 * their positive opposites can be easier to understand. The following
304 * examples given in comments have been made positive for ease of
305 * understanding. The structure of an error code is such:
306 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100307 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100308 *
309 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100310 * h = high level error code (includes high level module ID (bits 12..14)
311 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100312 * l = low level error code.
313 */
Chris Jonese11e8142021-04-22 15:28:56 +0100314 if ( high > -0x1000 && high != 0 )
315 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100316 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100317 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000318 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000319 mbedtls_test_fail( "'high' is not a high-level error code",
320 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000321 }
Chris Jonese11e8142021-04-22 15:28:56 +0100322 else if ( high < -0x7F80 )
323 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100324 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100325 */
Chris Jonesa203c382021-01-29 14:56:20 +0000326 {
Chris Jones3f613c12021-03-31 09:34:22 +0100327 mbedtls_test_fail( "'high' error code is greater than 15 bits",
328 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000329 }
Chris Jonese11e8142021-04-22 15:28:56 +0100330 else if ( ( high & 0x7F ) != 0 )
331 /* high & 0000000001111111
332 * Error code contains low level error code bits.
333 */
Chris Jonesa203c382021-01-29 14:56:20 +0000334 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000335 mbedtls_test_fail( "'high' contains a low-level error code",
336 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000337 }
Chris Jonese11e8142021-04-22 15:28:56 +0100338 else if ( low < -0x007F )
339 /* low > 0000000001111111
340 * Error code contains high or module level error code bits.
341 */
Chris Jonesa203c382021-01-29 14:56:20 +0000342 {
Chris Jones3f613c12021-03-31 09:34:22 +0100343 mbedtls_test_fail( "'low' error code is greater than 7 bits",
344 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000345 }
346 else if ( low > 0 )
347 {
Chris Jones3f613c12021-03-31 09:34:22 +0100348 mbedtls_test_fail( "'low' error code is greater than zero",
349 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000350 }
351}
352#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskineebc49e52021-06-11 14:13:53 +0200353
354#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200355#include "bignum_core.h"
356
357int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs,
Gilles Peskinec5772a12022-10-09 21:14:09 +0200358 const char *input )
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200359{
360 /* Sanity check */
361 if( *pX != NULL )
362 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
363
Gilles Peskinec5772a12022-10-09 21:14:09 +0200364 size_t hex_len = strlen( input );
365 size_t byte_len = ( hex_len + 1 ) / 2;
366 *plimbs = CHARS_TO_LIMBS( byte_len );
Gilles Peskine0b7e0792022-11-09 10:45:15 +0100367
368 /* A core bignum is not allowed to be empty. Forbid it as test data,
369 * this way static analyzers have a chance of knowing we don't expect
370 * the bignum functions to support empty inputs. */
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200371 if( *plimbs == 0 )
Gilles Peskine0b7e0792022-11-09 10:45:15 +0100372 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskinec5772a12022-10-09 21:14:09 +0200373
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200374 *pX = mbedtls_calloc( *plimbs, sizeof( **pX ) );
375 if( *pX == NULL )
376 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Gilles Peskinec5772a12022-10-09 21:14:09 +0200377
378 unsigned char *byte_start = ( unsigned char * ) *pX;
379 if( byte_len % sizeof( mbedtls_mpi_uint ) != 0 )
380 {
381 byte_start += sizeof( mbedtls_mpi_uint ) - byte_len % sizeof( mbedtls_mpi_uint );
382 }
383 if( ( hex_len & 1 ) != 0 )
384 {
385 /* mbedtls_test_unhexify wants an even number of hex digits */
386 TEST_ASSERT( ascii2uc( *input, byte_start ) == 0 );
387 ++byte_start;
388 ++input;
389 --byte_len;
390 }
391 TEST_ASSERT( mbedtls_test_unhexify( byte_start,
392 byte_len,
393 input,
394 &byte_len ) == 0 );
395
396 mbedtls_mpi_core_bigendian_to_host( *pX, *plimbs );
397 return( 0 );
398
399exit:
400 mbedtls_free( *pX );
401 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200402}
403
Werner Lewis19b4cd82022-07-07 11:02:27 +0100404int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
Gilles Peskineebc49e52021-06-11 14:13:53 +0200405{
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100406 int negative = 0;
407 /* Always set the sign bit to -1 if the input has a minus sign, even for 0.
408 * This creates an invalid representation, which mbedtls_mpi_read_string()
409 * avoids but we want to be able to create that in test data. */
410 if( s[0] == '-' )
411 {
412 ++s;
413 negative = 1;
414 }
Gilles Peskineebc49e52021-06-11 14:13:53 +0200415 /* mbedtls_mpi_read_string() currently retains leading zeros.
416 * It always allocates at least one limb for the value 0. */
417 if( s[0] == 0 )
418 {
419 mbedtls_mpi_free( X );
420 return( 0 );
421 }
Gilles Peskineca6e8aa2022-11-09 21:08:44 +0100422 int ret = mbedtls_mpi_read_string( X, 16, s );
423 if( ret != 0 )
424 return( ret );
425 if( negative )
426 {
427 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
428 ++mbedtls_test_case_uses_negative_0;
429 X->s = -1;
430 }
431 return( 0 );
Gilles Peskineebc49e52021-06-11 14:13:53 +0200432}
433#endif