blob: c310d48e7a4cd39248ee1e6eec7c4931cf63bf56 [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
18#include <test/helpers.h>
Ronald Cronf40529d2020-06-09 16:27:37 +020019#include <test/macros.h>
20#include <string.h>
21
Ronald Crona1236142020-07-01 16:01:21 +020022#if defined(MBEDTLS_CHECK_PARAMS)
23#include <setjmp.h>
24#endif
25
26/*----------------------------------------------------------------------------*/
27/* Static global variables */
28
29#if defined(MBEDTLS_CHECK_PARAMS)
30typedef struct
31{
32 uint8_t expected_call;
33 uint8_t expected_call_happened;
34
35 jmp_buf state;
36
37 mbedtls_test_param_failed_location_record_t location_record;
38}
39param_failed_ctx_t;
40static param_failed_ctx_t param_failed_ctx;
41#endif
42
Ronald Cronf40529d2020-06-09 16:27:37 +020043#if defined(MBEDTLS_PLATFORM_C)
44static mbedtls_platform_context platform_ctx;
45#endif
46
Chris Jonese60e2ae2021-01-20 17:51:47 +000047mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000048
Ronald Crona1236142020-07-01 16:01:21 +020049/*----------------------------------------------------------------------------*/
50/* Helper Functions */
51
Ronald Crone9c09f12020-06-08 16:44:58 +020052int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020053{
54 int ret = 0;
55#if defined(MBEDTLS_PLATFORM_C)
56 ret = mbedtls_platform_setup( &platform_ctx );
57#endif /* MBEDTLS_PLATFORM_C */
58 return( ret );
59}
60
Ronald Crone9c09f12020-06-08 16:44:58 +020061void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020062{
63#if defined(MBEDTLS_PLATFORM_C)
64 mbedtls_platform_teardown( &platform_ctx );
65#endif /* MBEDTLS_PLATFORM_C */
66}
67
Ronald Crona0c25392020-06-18 10:10:46 +020068static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020069{
Ronald Crona0c25392020-06-18 10:10:46 +020070 if( ( c >= '0' ) && ( c <= '9' ) )
71 *uc = c - '0';
72 else if( ( c >= 'a' ) && ( c <= 'f' ) )
73 *uc = c - 'a' + 10;
74 else if( ( c >= 'A' ) && ( c <= 'F' ) )
75 *uc = c - 'A' + 10;
76 else
77 return( -1 );
78
79 return( 0 );
80}
81
Chris Jones9634bb12021-01-20 15:56:42 +000082void mbedtls_test_fail( const char *test, int line_no, const char* filename )
83{
Chris Jonese60e2ae2021-01-20 17:51:47 +000084 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
Chris Jones9634bb12021-01-20 15:56:42 +000085 {
86 /* We've already recorded the test as having failed. Don't
87 * overwrite any previous information about the failure. */
88 return;
89 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000090 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
91 mbedtls_test_info.test = test;
92 mbedtls_test_info.line_no = line_no;
93 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000094}
95
Chris Jones9634bb12021-01-20 15:56:42 +000096void mbedtls_test_skip( const char *test, int line_no, const char* filename )
97{
Chris Jonese60e2ae2021-01-20 17:51:47 +000098 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
99 mbedtls_test_info.test = test;
100 mbedtls_test_info.line_no = line_no;
101 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +0000102}
103
Chris Jonesa5ab7652021-02-02 16:20:45 +0000104void mbedtls_test_set_step( unsigned long step )
105{
106 mbedtls_test_info.step = step;
107}
108
109void mbedtls_test_info_reset( void )
110{
111 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
112 mbedtls_test_info.step = (unsigned long)( -1 );
113 mbedtls_test_info.test = 0;
114 mbedtls_test_info.line_no = 0;
115 mbedtls_test_info.filename = 0;
Gilles Peskineb4366492021-04-29 20:28:54 +0200116 memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) );
117 memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) );
118}
119
120int mbedtls_test_equal( const char *test, int line_no, const char* filename,
121 unsigned long long value1, unsigned long long value2 )
122{
123 if( value1 == value2 )
124 return( 1 );
125 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
126 {
127 /* We've already recorded the test as having failed. Don't
128 * overwrite any previous information about the failure. */
129 return( 0 );
130 }
131 mbedtls_test_fail( test, line_no, filename );
132 (void) mbedtls_snprintf( mbedtls_test_info.line1,
133 sizeof( mbedtls_test_info.line1 ),
134 "lhs = 0x%016llx = %lld",
135 value1, (long long) value1 );
136 (void) mbedtls_snprintf( mbedtls_test_info.line2,
137 sizeof( mbedtls_test_info.line2 ),
138 "rhs = 0x%016llx = %lld",
139 value2, (long long) value2 );
140 return( 0 );
Chris Jonesa5ab7652021-02-02 16:20:45 +0000141}
142
Gilles Peskine063700d2022-04-13 23:59:52 +0200143int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
144 unsigned long long value1, unsigned long long value2 )
145{
146 if( value1 <= value2 )
147 return( 1 );
148 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
149 {
150 /* We've already recorded the test as having failed. Don't
151 * overwrite any previous information about the failure. */
152 return( 0 );
153 }
154 mbedtls_test_fail( test, line_no, filename );
155 (void) mbedtls_snprintf( mbedtls_test_info.line1,
156 sizeof( mbedtls_test_info.line1 ),
157 "lhs = 0x%016llx = %llu",
158 value1, value1 );
159 (void) mbedtls_snprintf( mbedtls_test_info.line2,
160 sizeof( mbedtls_test_info.line2 ),
161 "rhs = 0x%016llx = %llu",
162 value2, value2 );
163 return( 0 );
164}
165
166int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
167 long long value1, long long value2 )
168{
169 if( value1 <= value2 )
170 return( 1 );
171 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
172 {
173 /* We've already recorded the test as having failed. Don't
174 * overwrite any previous information about the failure. */
175 return( 0 );
176 }
177 mbedtls_test_fail( test, line_no, filename );
178 (void) mbedtls_snprintf( mbedtls_test_info.line1,
179 sizeof( mbedtls_test_info.line1 ),
180 "lhs = 0x%016llx = %lld",
181 (unsigned long long) value1, value1 );
182 (void) mbedtls_snprintf( mbedtls_test_info.line2,
183 sizeof( mbedtls_test_info.line2 ),
184 "rhs = 0x%016llx = %lld",
185 (unsigned long long) value2, value2 );
186 return( 0 );
187}
188
Ronald Crona0c25392020-06-18 10:10:46 +0200189int mbedtls_test_unhexify( unsigned char *obuf,
190 size_t obufmax,
191 const char *ibuf,
192 size_t *len )
193{
194 unsigned char uc, uc2;
195
196 *len = strlen( ibuf );
197
198 /* Must be even number of bytes. */
199 if ( ( *len ) & 1 )
200 return( -1 );
201 *len /= 2;
202
203 if ( (*len) > obufmax )
204 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200205
206 while( *ibuf != 0 )
207 {
Ronald Crona0c25392020-06-18 10:10:46 +0200208 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
209 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200210
Ronald Crona0c25392020-06-18 10:10:46 +0200211 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
212 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200213
Ronald Crona0c25392020-06-18 10:10:46 +0200214 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200215 }
216
Ronald Crona0c25392020-06-18 10:10:46 +0200217 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200218}
219
Ronald Cron72d628f2020-06-08 17:05:57 +0200220void mbedtls_test_hexify( unsigned char *obuf,
221 const unsigned char *ibuf,
222 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200223{
224 unsigned char l, h;
225
226 while( len != 0 )
227 {
228 h = *ibuf / 16;
229 l = *ibuf % 16;
230
231 if( h < 10 )
232 *obuf++ = '0' + h;
233 else
234 *obuf++ = 'a' + h - 10;
235
236 if( l < 10 )
237 *obuf++ = '0' + l;
238 else
239 *obuf++ = 'a' + l - 10;
240
241 ++ibuf;
242 len--;
243 }
244}
245
Ronald Cron690f3eb2020-06-10 10:42:18 +0200246unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200247{
248 void *p;
249 size_t actual_len = ( len != 0 ) ? len : 1;
250
251 p = mbedtls_calloc( 1, actual_len );
252 TEST_HELPER_ASSERT( p != NULL );
253
254 memset( p, 0x00, actual_len );
255
256 return( p );
257}
258
Ronald Crona256c702020-06-10 10:53:11 +0200259unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200260{
261 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200262 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200263
264 *olen = strlen( ibuf ) / 2;
265
266 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200267 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200268
269 obuf = mbedtls_calloc( 1, *olen );
270 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200271 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200272
273 return( obuf );
274}
275
Ronald Cronde70b162020-06-10 11:03:08 +0200276int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
277 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200278{
279 int ret = 0;
280 uint32_t i = 0;
281
282 if( a_len != b_len )
283 return( -1 );
284
285 for( i = 0; i < a_len; i++ )
286 {
287 if( a[i] != b[i] )
288 {
289 ret = -1;
290 break;
291 }
292 }
293 return ret;
294}
Ronald Crona1236142020-07-01 16:01:21 +0200295
296#if defined(MBEDTLS_CHECK_PARAMS)
297void mbedtls_test_param_failed_get_location_record(
298 mbedtls_test_param_failed_location_record_t *location_record )
299{
300 *location_record = param_failed_ctx.location_record;
301}
302
303void mbedtls_test_param_failed_expect_call( void )
304{
305 param_failed_ctx.expected_call_happened = 0;
306 param_failed_ctx.expected_call = 1;
307}
308
309int mbedtls_test_param_failed_check_expected_call( void )
310{
311 param_failed_ctx.expected_call = 0;
312
313 if( param_failed_ctx.expected_call_happened != 0 )
314 return( 0 );
315
316 return( -1 );
317}
318
319void* mbedtls_test_param_failed_get_state_buf( void )
320{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200321 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200322}
323
324void mbedtls_test_param_failed_reset_state( void )
325{
326 memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
327}
328
329void mbedtls_param_failed( const char *failure_condition,
330 const char *file,
331 int line )
332{
333 /* Record the location of the failure */
334 param_failed_ctx.location_record.failure_condition = failure_condition;
335 param_failed_ctx.location_record.file = file;
336 param_failed_ctx.location_record.line = line;
337
338 /* If we are testing the callback function... */
339 if( param_failed_ctx.expected_call != 0 )
340 {
341 param_failed_ctx.expected_call = 0;
342 param_failed_ctx.expected_call_happened = 1;
343 }
344 else
345 {
346 /* ...else try a long jump. If the execution state has not been set-up
347 * or reset then the long jump buffer is all zero's and the call will
348 * with high probability fault, emphasizing there is something to look
349 * at.
350 */
351
352 longjmp( param_failed_ctx.state, 1 );
353 }
354}
355#endif /* MBEDTLS_CHECK_PARAMS */
Chris Jones96ae73b2021-01-08 17:04:59 +0000356
357#if defined(MBEDTLS_TEST_HOOKS)
358void mbedtls_test_err_add_check( int high, int low,
359 const char *file, int line )
360{
Chris Jones3f613c12021-03-31 09:34:22 +0100361 /* Error codes are always negative (a value of zero is a success) however
362 * their positive opposites can be easier to understand. The following
363 * examples given in comments have been made positive for ease of
364 * understanding. The structure of an error code is such:
365 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100366 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100367 *
368 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100369 * h = high level error code (includes high level module ID (bits 12..14)
370 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100371 * l = low level error code.
372 */
Chris Jonese11e8142021-04-22 15:28:56 +0100373 if ( high > -0x1000 && high != 0 )
374 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100375 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100376 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000377 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000378 mbedtls_test_fail( "'high' is not a high-level error code",
379 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000380 }
Chris Jonese11e8142021-04-22 15:28:56 +0100381 else if ( high < -0x7F80 )
382 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100383 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100384 */
Chris Jonesa203c382021-01-29 14:56:20 +0000385 {
Chris Jones3f613c12021-03-31 09:34:22 +0100386 mbedtls_test_fail( "'high' error code is greater than 15 bits",
387 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000388 }
Chris Jonese11e8142021-04-22 15:28:56 +0100389 else if ( ( high & 0x7F ) != 0 )
390 /* high & 0000000001111111
391 * Error code contains low level error code bits.
392 */
Chris Jonesa203c382021-01-29 14:56:20 +0000393 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000394 mbedtls_test_fail( "'high' contains a low-level error code",
395 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000396 }
Chris Jonese11e8142021-04-22 15:28:56 +0100397 else if ( low < -0x007F )
398 /* low > 0000000001111111
399 * Error code contains high or module level error code bits.
400 */
Chris Jonesa203c382021-01-29 14:56:20 +0000401 {
Chris Jones3f613c12021-03-31 09:34:22 +0100402 mbedtls_test_fail( "'low' error code is greater than 7 bits",
403 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000404 }
405 else if ( low > 0 )
406 {
Chris Jones3f613c12021-03-31 09:34:22 +0100407 mbedtls_test_fail( "'low' error code is greater than zero",
408 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000409 }
410}
411#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskinedb479712021-06-11 14:13:53 +0200412
413#if defined(MBEDTLS_BIGNUM_C)
Werner Lewis24b60782022-07-07 15:08:17 +0100414int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
Gilles Peskinedb479712021-06-11 14:13:53 +0200415{
416 /* mbedtls_mpi_read_string() currently retains leading zeros.
417 * It always allocates at least one limb for the value 0. */
418 if( s[0] == 0 )
419 {
420 mbedtls_mpi_free( X );
421 return( 0 );
422 }
423 else
Werner Lewis24b60782022-07-07 15:08:17 +0100424 return( mbedtls_mpi_read_string( X, 16, s ) );
Gilles Peskinedb479712021-06-11 14:13:53 +0200425}
426#endif