blob: 77b4d942dbc27785b8da46b4288acc2210dc3588 [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 Peskine7db8e892022-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#if defined(MBEDTLS_CHECK_PARAMS)
24#include <setjmp.h>
25#endif
26
27/*----------------------------------------------------------------------------*/
28/* Static global variables */
29
30#if defined(MBEDTLS_CHECK_PARAMS)
31typedef 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}
40param_failed_ctx_t;
41static param_failed_ctx_t param_failed_ctx;
42#endif
43
Ronald Cronf40529d2020-06-09 16:27:37 +020044#if defined(MBEDTLS_PLATFORM_C)
45static mbedtls_platform_context platform_ctx;
46#endif
47
Chris Jonese60e2ae2021-01-20 17:51:47 +000048mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000049
Ronald Crona1236142020-07-01 16:01:21 +020050/*----------------------------------------------------------------------------*/
51/* Helper Functions */
52
Ronald Crone9c09f12020-06-08 16:44:58 +020053int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020054{
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 Crone9c09f12020-06-08 16:44:58 +020062void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020063{
64#if defined(MBEDTLS_PLATFORM_C)
65 mbedtls_platform_teardown( &platform_ctx );
66#endif /* MBEDTLS_PLATFORM_C */
67}
68
Ronald Crona0c25392020-06-18 10:10:46 +020069static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020070{
Ronald Crona0c25392020-06-18 10:10:46 +020071 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 Jones9634bb12021-01-20 15:56:42 +000083void mbedtls_test_fail( const char *test, int line_no, const char* filename )
84{
Chris Jonese60e2ae2021-01-20 17:51:47 +000085 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
Chris Jones9634bb12021-01-20 15:56:42 +000086 {
87 /* We've already recorded the test as having failed. Don't
88 * overwrite any previous information about the failure. */
89 return;
90 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000091 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 Jones9634bb12021-01-20 15:56:42 +000095}
96
Chris Jones9634bb12021-01-20 15:56:42 +000097void mbedtls_test_skip( const char *test, int line_no, const char* filename )
98{
Chris Jonese60e2ae2021-01-20 17:51:47 +000099 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 Jones9634bb12021-01-20 15:56:42 +0000103}
104
Chris Jonesa5ab7652021-02-02 16:20:45 +0000105void mbedtls_test_set_step( unsigned long step )
106{
107 mbedtls_test_info.step = step;
108}
109
Gilles Peskine53a72062022-11-09 21:08:44 +0100110#if defined(MBEDTLS_BIGNUM_C)
111unsigned mbedtls_test_case_uses_negative_0 = 0;
112#endif
113
Chris Jonesa5ab7652021-02-02 16:20:45 +0000114void mbedtls_test_info_reset( void )
115{
116 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
117 mbedtls_test_info.step = (unsigned long)( -1 );
118 mbedtls_test_info.test = 0;
119 mbedtls_test_info.line_no = 0;
120 mbedtls_test_info.filename = 0;
Gilles Peskineb4366492021-04-29 20:28:54 +0200121 memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) );
122 memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) );
Gilles Peskine53a72062022-11-09 21:08:44 +0100123#if defined(MBEDTLS_BIGNUM_C)
124 mbedtls_test_case_uses_negative_0 = 0;
125#endif
Gilles Peskineb4366492021-04-29 20:28:54 +0200126}
127
128int mbedtls_test_equal( const char *test, int line_no, const char* filename,
129 unsigned long long value1, unsigned long long value2 )
130{
Gilles Peskine7db8e892022-09-20 18:31:30 +0200131 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
132 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
133
Gilles Peskineb4366492021-04-29 20:28:54 +0200134 if( value1 == value2 )
135 return( 1 );
Gilles Peskine7db8e892022-09-20 18:31:30 +0200136
Gilles Peskineb4366492021-04-29 20:28:54 +0200137 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
138 {
139 /* We've already recorded the test as having failed. Don't
140 * overwrite any previous information about the failure. */
141 return( 0 );
142 }
143 mbedtls_test_fail( test, line_no, filename );
144 (void) mbedtls_snprintf( mbedtls_test_info.line1,
145 sizeof( mbedtls_test_info.line1 ),
146 "lhs = 0x%016llx = %lld",
147 value1, (long long) value1 );
148 (void) mbedtls_snprintf( mbedtls_test_info.line2,
149 sizeof( mbedtls_test_info.line2 ),
150 "rhs = 0x%016llx = %lld",
151 value2, (long long) value2 );
152 return( 0 );
Chris Jonesa5ab7652021-02-02 16:20:45 +0000153}
154
Gilles Peskine063700d2022-04-13 23:59:52 +0200155int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
156 unsigned long long value1, unsigned long long value2 )
157{
Gilles Peskine7db8e892022-09-20 18:31:30 +0200158 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
159 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
160
Gilles Peskine063700d2022-04-13 23:59:52 +0200161 if( value1 <= value2 )
162 return( 1 );
Gilles Peskine7db8e892022-09-20 18:31:30 +0200163
Gilles Peskine063700d2022-04-13 23:59:52 +0200164 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
165 {
166 /* We've already recorded the test as having failed. Don't
167 * overwrite any previous information about the failure. */
168 return( 0 );
169 }
170 mbedtls_test_fail( test, line_no, filename );
171 (void) mbedtls_snprintf( mbedtls_test_info.line1,
172 sizeof( mbedtls_test_info.line1 ),
173 "lhs = 0x%016llx = %llu",
174 value1, value1 );
175 (void) mbedtls_snprintf( mbedtls_test_info.line2,
176 sizeof( mbedtls_test_info.line2 ),
177 "rhs = 0x%016llx = %llu",
178 value2, value2 );
179 return( 0 );
180}
181
182int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
183 long long value1, long long value2 )
184{
Gilles Peskine7db8e892022-09-20 18:31:30 +0200185 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
186 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
187
Gilles Peskine063700d2022-04-13 23:59:52 +0200188 if( value1 <= value2 )
189 return( 1 );
Gilles Peskine7db8e892022-09-20 18:31:30 +0200190
Gilles Peskine063700d2022-04-13 23:59:52 +0200191 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
192 {
193 /* We've already recorded the test as having failed. Don't
194 * overwrite any previous information about the failure. */
195 return( 0 );
196 }
197 mbedtls_test_fail( test, line_no, filename );
198 (void) mbedtls_snprintf( mbedtls_test_info.line1,
199 sizeof( mbedtls_test_info.line1 ),
200 "lhs = 0x%016llx = %lld",
201 (unsigned long long) value1, value1 );
202 (void) mbedtls_snprintf( mbedtls_test_info.line2,
203 sizeof( mbedtls_test_info.line2 ),
204 "rhs = 0x%016llx = %lld",
205 (unsigned long long) value2, value2 );
206 return( 0 );
207}
208
Ronald Crona0c25392020-06-18 10:10:46 +0200209int mbedtls_test_unhexify( unsigned char *obuf,
210 size_t obufmax,
211 const char *ibuf,
212 size_t *len )
213{
214 unsigned char uc, uc2;
215
216 *len = strlen( ibuf );
217
218 /* Must be even number of bytes. */
219 if ( ( *len ) & 1 )
220 return( -1 );
221 *len /= 2;
222
223 if ( (*len) > obufmax )
224 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200225
226 while( *ibuf != 0 )
227 {
Ronald Crona0c25392020-06-18 10:10:46 +0200228 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
229 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200230
Ronald Crona0c25392020-06-18 10:10:46 +0200231 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
232 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200233
Ronald Crona0c25392020-06-18 10:10:46 +0200234 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200235 }
236
Ronald Crona0c25392020-06-18 10:10:46 +0200237 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200238}
239
Ronald Cron72d628f2020-06-08 17:05:57 +0200240void mbedtls_test_hexify( unsigned char *obuf,
241 const unsigned char *ibuf,
242 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200243{
244 unsigned char l, h;
245
246 while( len != 0 )
247 {
248 h = *ibuf / 16;
249 l = *ibuf % 16;
250
251 if( h < 10 )
252 *obuf++ = '0' + h;
253 else
254 *obuf++ = 'a' + h - 10;
255
256 if( l < 10 )
257 *obuf++ = '0' + l;
258 else
259 *obuf++ = 'a' + l - 10;
260
261 ++ibuf;
262 len--;
263 }
264}
265
Ronald Cron690f3eb2020-06-10 10:42:18 +0200266unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200267{
268 void *p;
269 size_t actual_len = ( len != 0 ) ? len : 1;
270
271 p = mbedtls_calloc( 1, actual_len );
272 TEST_HELPER_ASSERT( p != NULL );
273
274 memset( p, 0x00, actual_len );
275
276 return( p );
277}
278
Ronald Crona256c702020-06-10 10:53:11 +0200279unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200280{
281 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200282 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200283
284 *olen = strlen( ibuf ) / 2;
285
286 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200287 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200288
289 obuf = mbedtls_calloc( 1, *olen );
290 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200291 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200292
293 return( obuf );
294}
295
Ronald Cronde70b162020-06-10 11:03:08 +0200296int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
297 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200298{
299 int ret = 0;
300 uint32_t i = 0;
301
302 if( a_len != b_len )
303 return( -1 );
304
305 for( i = 0; i < a_len; i++ )
306 {
307 if( a[i] != b[i] )
308 {
309 ret = -1;
310 break;
311 }
312 }
313 return ret;
314}
Ronald Crona1236142020-07-01 16:01:21 +0200315
316#if defined(MBEDTLS_CHECK_PARAMS)
317void mbedtls_test_param_failed_get_location_record(
318 mbedtls_test_param_failed_location_record_t *location_record )
319{
320 *location_record = param_failed_ctx.location_record;
321}
322
323void mbedtls_test_param_failed_expect_call( void )
324{
325 param_failed_ctx.expected_call_happened = 0;
326 param_failed_ctx.expected_call = 1;
327}
328
329int mbedtls_test_param_failed_check_expected_call( void )
330{
331 param_failed_ctx.expected_call = 0;
332
333 if( param_failed_ctx.expected_call_happened != 0 )
334 return( 0 );
335
336 return( -1 );
337}
338
339void* mbedtls_test_param_failed_get_state_buf( void )
340{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200341 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200342}
343
344void mbedtls_test_param_failed_reset_state( void )
345{
346 memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
347}
348
349void mbedtls_param_failed( const char *failure_condition,
350 const char *file,
351 int line )
352{
353 /* Record the location of the failure */
354 param_failed_ctx.location_record.failure_condition = failure_condition;
355 param_failed_ctx.location_record.file = file;
356 param_failed_ctx.location_record.line = line;
357
358 /* If we are testing the callback function... */
359 if( param_failed_ctx.expected_call != 0 )
360 {
361 param_failed_ctx.expected_call = 0;
362 param_failed_ctx.expected_call_happened = 1;
363 }
364 else
365 {
366 /* ...else try a long jump. If the execution state has not been set-up
367 * or reset then the long jump buffer is all zero's and the call will
368 * with high probability fault, emphasizing there is something to look
369 * at.
370 */
371
372 longjmp( param_failed_ctx.state, 1 );
373 }
374}
375#endif /* MBEDTLS_CHECK_PARAMS */
Chris Jones96ae73b2021-01-08 17:04:59 +0000376
377#if defined(MBEDTLS_TEST_HOOKS)
378void mbedtls_test_err_add_check( int high, int low,
379 const char *file, int line )
380{
Chris Jones3f613c12021-03-31 09:34:22 +0100381 /* Error codes are always negative (a value of zero is a success) however
382 * their positive opposites can be easier to understand. The following
383 * examples given in comments have been made positive for ease of
384 * understanding. The structure of an error code is such:
385 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100386 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100387 *
388 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100389 * h = high level error code (includes high level module ID (bits 12..14)
390 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100391 * l = low level error code.
392 */
Chris Jonese11e8142021-04-22 15:28:56 +0100393 if ( high > -0x1000 && high != 0 )
394 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100395 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100396 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000397 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000398 mbedtls_test_fail( "'high' is not a high-level error code",
399 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000400 }
Chris Jonese11e8142021-04-22 15:28:56 +0100401 else if ( high < -0x7F80 )
402 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100403 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100404 */
Chris Jonesa203c382021-01-29 14:56:20 +0000405 {
Chris Jones3f613c12021-03-31 09:34:22 +0100406 mbedtls_test_fail( "'high' error code is greater than 15 bits",
407 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000408 }
Chris Jonese11e8142021-04-22 15:28:56 +0100409 else if ( ( high & 0x7F ) != 0 )
410 /* high & 0000000001111111
411 * Error code contains low level error code bits.
412 */
Chris Jonesa203c382021-01-29 14:56:20 +0000413 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000414 mbedtls_test_fail( "'high' contains a low-level error code",
415 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000416 }
Chris Jonese11e8142021-04-22 15:28:56 +0100417 else if ( low < -0x007F )
418 /* low > 0000000001111111
419 * Error code contains high or module level error code bits.
420 */
Chris Jonesa203c382021-01-29 14:56:20 +0000421 {
Chris Jones3f613c12021-03-31 09:34:22 +0100422 mbedtls_test_fail( "'low' error code is greater than 7 bits",
423 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000424 }
425 else if ( low > 0 )
426 {
Chris Jones3f613c12021-03-31 09:34:22 +0100427 mbedtls_test_fail( "'low' error code is greater than zero",
428 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000429 }
430}
431#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskinedb479712021-06-11 14:13:53 +0200432
433#if defined(MBEDTLS_BIGNUM_C)
Werner Lewis24b60782022-07-07 15:08:17 +0100434int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
Gilles Peskinedb479712021-06-11 14:13:53 +0200435{
Gilles Peskine53a72062022-11-09 21:08:44 +0100436 int negative = 0;
437 /* Always set the sign bit to -1 if the input has a minus sign, even for 0.
438 * This creates an invalid representation, which mbedtls_mpi_read_string()
439 * avoids but we want to be able to create that in test data. */
440 if( s[0] == '-' )
441 {
442 ++s;
443 negative = 1;
444 }
Gilles Peskinedb479712021-06-11 14:13:53 +0200445 /* mbedtls_mpi_read_string() currently retains leading zeros.
446 * It always allocates at least one limb for the value 0. */
447 if( s[0] == 0 )
448 {
449 mbedtls_mpi_free( X );
450 return( 0 );
451 }
Gilles Peskine53a72062022-11-09 21:08:44 +0100452 int ret = mbedtls_mpi_read_string( X, 16, s );
453 if( ret != 0 )
454 return( ret );
455 if( negative )
456 {
457 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
458 ++mbedtls_test_case_uses_negative_0;
459 X->s = -1;
460 }
461 return( 0 );
Gilles Peskinedb479712021-06-11 14:13:53 +0200462}
463#endif