blob: bfd21893d1a2c1d728e99fb5d2b40011aef17b79 [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
110void 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 Peskineb4366492021-04-29 20:28:54 +0200117 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
121int mbedtls_test_equal( const char *test, int line_no, const char* filename,
122 unsigned long long value1, unsigned long long value2 )
123{
Gilles Peskine7db8e892022-09-20 18:31:30 +0200124 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
125 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
126
Gilles Peskineb4366492021-04-29 20:28:54 +0200127 if( value1 == value2 )
128 return( 1 );
Gilles Peskine7db8e892022-09-20 18:31:30 +0200129
Gilles Peskineb4366492021-04-29 20:28:54 +0200130 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 Jonesa5ab7652021-02-02 16:20:45 +0000146}
147
Gilles Peskine063700d2022-04-13 23:59:52 +0200148int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
149 unsigned long long value1, unsigned long long value2 )
150{
Gilles Peskine7db8e892022-09-20 18:31:30 +0200151 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
152 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
153
Gilles Peskine063700d2022-04-13 23:59:52 +0200154 if( value1 <= value2 )
155 return( 1 );
Gilles Peskine7db8e892022-09-20 18:31:30 +0200156
Gilles Peskine063700d2022-04-13 23:59:52 +0200157 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
175int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
176 long long value1, long long value2 )
177{
Gilles Peskine7db8e892022-09-20 18:31:30 +0200178 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
179 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
180
Gilles Peskine063700d2022-04-13 23:59:52 +0200181 if( value1 <= value2 )
182 return( 1 );
Gilles Peskine7db8e892022-09-20 18:31:30 +0200183
Gilles Peskine063700d2022-04-13 23:59:52 +0200184 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 Crona0c25392020-06-18 10:10:46 +0200202int 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 Cronf40529d2020-06-09 16:27:37 +0200218
219 while( *ibuf != 0 )
220 {
Ronald Crona0c25392020-06-18 10:10:46 +0200221 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
222 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200223
Ronald Crona0c25392020-06-18 10:10:46 +0200224 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
225 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200226
Ronald Crona0c25392020-06-18 10:10:46 +0200227 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200228 }
229
Ronald Crona0c25392020-06-18 10:10:46 +0200230 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200231}
232
Ronald Cron72d628f2020-06-08 17:05:57 +0200233void mbedtls_test_hexify( unsigned char *obuf,
234 const unsigned char *ibuf,
235 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200236{
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 Cron690f3eb2020-06-10 10:42:18 +0200259unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200260{
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 Crona256c702020-06-10 10:53:11 +0200272unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200273{
274 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200275 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200276
277 *olen = strlen( ibuf ) / 2;
278
279 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200280 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200281
282 obuf = mbedtls_calloc( 1, *olen );
283 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200284 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200285
286 return( obuf );
287}
288
Ronald Cronde70b162020-06-10 11:03:08 +0200289int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
290 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200291{
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 Crona1236142020-07-01 16:01:21 +0200308
309#if defined(MBEDTLS_CHECK_PARAMS)
310void 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
316void 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
322int 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
332void* mbedtls_test_param_failed_get_state_buf( void )
333{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200334 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200335}
336
337void mbedtls_test_param_failed_reset_state( void )
338{
339 memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
340}
341
342void 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 Jones96ae73b2021-01-08 17:04:59 +0000369
370#if defined(MBEDTLS_TEST_HOOKS)
371void mbedtls_test_err_add_check( int high, int low,
372 const char *file, int line )
373{
Chris Jones3f613c12021-03-31 09:34:22 +0100374 /* 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 Jonesabded0e2021-04-12 15:44:47 +0100379 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100380 *
381 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100382 * h = high level error code (includes high level module ID (bits 12..14)
383 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100384 * l = low level error code.
385 */
Chris Jonese11e8142021-04-22 15:28:56 +0100386 if ( high > -0x1000 && high != 0 )
387 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100388 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100389 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000390 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000391 mbedtls_test_fail( "'high' is not a high-level error code",
392 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000393 }
Chris Jonese11e8142021-04-22 15:28:56 +0100394 else if ( high < -0x7F80 )
395 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100396 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100397 */
Chris Jonesa203c382021-01-29 14:56:20 +0000398 {
Chris Jones3f613c12021-03-31 09:34:22 +0100399 mbedtls_test_fail( "'high' error code is greater than 15 bits",
400 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000401 }
Chris Jonese11e8142021-04-22 15:28:56 +0100402 else if ( ( high & 0x7F ) != 0 )
403 /* high & 0000000001111111
404 * Error code contains low level error code bits.
405 */
Chris Jonesa203c382021-01-29 14:56:20 +0000406 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000407 mbedtls_test_fail( "'high' contains a low-level error code",
408 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000409 }
Chris Jonese11e8142021-04-22 15:28:56 +0100410 else if ( low < -0x007F )
411 /* low > 0000000001111111
412 * Error code contains high or module level error code bits.
413 */
Chris Jonesa203c382021-01-29 14:56:20 +0000414 {
Chris Jones3f613c12021-03-31 09:34:22 +0100415 mbedtls_test_fail( "'low' error code is greater than 7 bits",
416 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000417 }
418 else if ( low > 0 )
419 {
Chris Jones3f613c12021-03-31 09:34:22 +0100420 mbedtls_test_fail( "'low' error code is greater than zero",
421 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000422 }
423}
424#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskinedb479712021-06-11 14:13:53 +0200425
426#if defined(MBEDTLS_BIGNUM_C)
Werner Lewis24b60782022-07-07 15:08:17 +0100427int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
Gilles Peskinedb479712021-06-11 14:13:53 +0200428{
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 Lewis24b60782022-07-07 15:08:17 +0100437 return( mbedtls_mpi_read_string( X, 16, s ) );
Gilles Peskinedb479712021-06-11 14:13:53 +0200438}
439#endif