blob: c53e4fc81695bc26979bcb74d59c31eac50104ce [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
Ronald Crona0c25392020-06-18 10:10:46 +0200143int mbedtls_test_unhexify( unsigned char *obuf,
144 size_t obufmax,
145 const char *ibuf,
146 size_t *len )
147{
148 unsigned char uc, uc2;
149
150 *len = strlen( ibuf );
151
152 /* Must be even number of bytes. */
153 if ( ( *len ) & 1 )
154 return( -1 );
155 *len /= 2;
156
157 if ( (*len) > obufmax )
158 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200159
160 while( *ibuf != 0 )
161 {
Ronald Crona0c25392020-06-18 10:10:46 +0200162 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
163 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200164
Ronald Crona0c25392020-06-18 10:10:46 +0200165 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
166 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200167
Ronald Crona0c25392020-06-18 10:10:46 +0200168 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200169 }
170
Ronald Crona0c25392020-06-18 10:10:46 +0200171 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200172}
173
Ronald Cron72d628f2020-06-08 17:05:57 +0200174void mbedtls_test_hexify( unsigned char *obuf,
175 const unsigned char *ibuf,
176 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200177{
178 unsigned char l, h;
179
180 while( len != 0 )
181 {
182 h = *ibuf / 16;
183 l = *ibuf % 16;
184
185 if( h < 10 )
186 *obuf++ = '0' + h;
187 else
188 *obuf++ = 'a' + h - 10;
189
190 if( l < 10 )
191 *obuf++ = '0' + l;
192 else
193 *obuf++ = 'a' + l - 10;
194
195 ++ibuf;
196 len--;
197 }
198}
199
Ronald Cron690f3eb2020-06-10 10:42:18 +0200200unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200201{
202 void *p;
203 size_t actual_len = ( len != 0 ) ? len : 1;
204
205 p = mbedtls_calloc( 1, actual_len );
206 TEST_HELPER_ASSERT( p != NULL );
207
208 memset( p, 0x00, actual_len );
209
210 return( p );
211}
212
Ronald Crona256c702020-06-10 10:53:11 +0200213unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200214{
215 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200216 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200217
218 *olen = strlen( ibuf ) / 2;
219
220 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200221 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200222
223 obuf = mbedtls_calloc( 1, *olen );
224 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200225 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200226
227 return( obuf );
228}
229
Ronald Cronde70b162020-06-10 11:03:08 +0200230int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
231 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200232{
233 int ret = 0;
234 uint32_t i = 0;
235
236 if( a_len != b_len )
237 return( -1 );
238
239 for( i = 0; i < a_len; i++ )
240 {
241 if( a[i] != b[i] )
242 {
243 ret = -1;
244 break;
245 }
246 }
247 return ret;
248}
Ronald Crona1236142020-07-01 16:01:21 +0200249
250#if defined(MBEDTLS_CHECK_PARAMS)
251void mbedtls_test_param_failed_get_location_record(
252 mbedtls_test_param_failed_location_record_t *location_record )
253{
254 *location_record = param_failed_ctx.location_record;
255}
256
257void mbedtls_test_param_failed_expect_call( void )
258{
259 param_failed_ctx.expected_call_happened = 0;
260 param_failed_ctx.expected_call = 1;
261}
262
263int mbedtls_test_param_failed_check_expected_call( void )
264{
265 param_failed_ctx.expected_call = 0;
266
267 if( param_failed_ctx.expected_call_happened != 0 )
268 return( 0 );
269
270 return( -1 );
271}
272
273void* mbedtls_test_param_failed_get_state_buf( void )
274{
Ronald Cronbf4f4082020-09-25 10:45:06 +0200275 return &param_failed_ctx.state;
Ronald Crona1236142020-07-01 16:01:21 +0200276}
277
278void mbedtls_test_param_failed_reset_state( void )
279{
280 memset( param_failed_ctx.state, 0, sizeof( param_failed_ctx.state ) );
281}
282
283void mbedtls_param_failed( const char *failure_condition,
284 const char *file,
285 int line )
286{
287 /* Record the location of the failure */
288 param_failed_ctx.location_record.failure_condition = failure_condition;
289 param_failed_ctx.location_record.file = file;
290 param_failed_ctx.location_record.line = line;
291
292 /* If we are testing the callback function... */
293 if( param_failed_ctx.expected_call != 0 )
294 {
295 param_failed_ctx.expected_call = 0;
296 param_failed_ctx.expected_call_happened = 1;
297 }
298 else
299 {
300 /* ...else try a long jump. If the execution state has not been set-up
301 * or reset then the long jump buffer is all zero's and the call will
302 * with high probability fault, emphasizing there is something to look
303 * at.
304 */
305
306 longjmp( param_failed_ctx.state, 1 );
307 }
308}
309#endif /* MBEDTLS_CHECK_PARAMS */
Chris Jones96ae73b2021-01-08 17:04:59 +0000310
311#if defined(MBEDTLS_TEST_HOOKS)
312void mbedtls_test_err_add_check( int high, int low,
313 const char *file, int line )
314{
Chris Jones3f613c12021-03-31 09:34:22 +0100315 /* Error codes are always negative (a value of zero is a success) however
316 * their positive opposites can be easier to understand. The following
317 * examples given in comments have been made positive for ease of
318 * understanding. The structure of an error code is such:
319 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100320 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100321 *
322 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100323 * h = high level error code (includes high level module ID (bits 12..14)
324 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100325 * l = low level error code.
326 */
Chris Jonese11e8142021-04-22 15:28:56 +0100327 if ( high > -0x1000 && high != 0 )
328 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100329 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100330 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000331 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000332 mbedtls_test_fail( "'high' is not a high-level error code",
333 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000334 }
Chris Jonese11e8142021-04-22 15:28:56 +0100335 else if ( high < -0x7F80 )
336 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100337 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100338 */
Chris Jonesa203c382021-01-29 14:56:20 +0000339 {
Chris Jones3f613c12021-03-31 09:34:22 +0100340 mbedtls_test_fail( "'high' error code is greater than 15 bits",
341 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000342 }
Chris Jonese11e8142021-04-22 15:28:56 +0100343 else if ( ( high & 0x7F ) != 0 )
344 /* high & 0000000001111111
345 * Error code contains low level error code bits.
346 */
Chris Jonesa203c382021-01-29 14:56:20 +0000347 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000348 mbedtls_test_fail( "'high' contains a low-level error code",
349 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000350 }
Chris Jonese11e8142021-04-22 15:28:56 +0100351 else if ( low < -0x007F )
352 /* low > 0000000001111111
353 * Error code contains high or module level error code bits.
354 */
Chris Jonesa203c382021-01-29 14:56:20 +0000355 {
Chris Jones3f613c12021-03-31 09:34:22 +0100356 mbedtls_test_fail( "'low' error code is greater than 7 bits",
357 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000358 }
359 else if ( low > 0 )
360 {
Chris Jones3f613c12021-03-31 09:34:22 +0100361 mbedtls_test_fail( "'low' error code is greater than zero",
362 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000363 }
364}
365#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskinedb479712021-06-11 14:13:53 +0200366
367#if defined(MBEDTLS_BIGNUM_C)
368int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s )
369{
370 /* mbedtls_mpi_read_string() currently retains leading zeros.
371 * It always allocates at least one limb for the value 0. */
372 if( s[0] == 0 )
373 {
374 mbedtls_mpi_free( X );
375 return( 0 );
376 }
377 else
378 return( mbedtls_mpi_read_string( X, radix, s ) );
379}
380#endif