blob: cc23fd7c4d6245d9b2b39d65353a838aa4cafd34 [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
92void mbedtls_test_info_reset( void )
93{
94 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
95 mbedtls_test_info.step = (unsigned long)( -1 );
96 mbedtls_test_info.test = 0;
97 mbedtls_test_info.line_no = 0;
98 mbedtls_test_info.filename = 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +020099 memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) );
100 memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) );
101}
102
103int mbedtls_test_equal( const char *test, int line_no, const char* filename,
104 unsigned long long value1, unsigned long long value2 )
105{
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200106 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
107 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
108
Gilles Peskine89615ee2021-04-29 20:28:54 +0200109 if( value1 == value2 )
110 return( 1 );
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200111
Gilles Peskine89615ee2021-04-29 20:28:54 +0200112 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
113 {
114 /* We've already recorded the test as having failed. Don't
115 * overwrite any previous information about the failure. */
116 return( 0 );
117 }
118 mbedtls_test_fail( test, line_no, filename );
119 (void) mbedtls_snprintf( mbedtls_test_info.line1,
120 sizeof( mbedtls_test_info.line1 ),
121 "lhs = 0x%016llx = %lld",
122 value1, (long long) value1 );
123 (void) mbedtls_snprintf( mbedtls_test_info.line2,
124 sizeof( mbedtls_test_info.line2 ),
125 "rhs = 0x%016llx = %lld",
126 value2, (long long) value2 );
127 return( 0 );
Chris Jonesa5ab7652021-02-02 16:20:45 +0000128}
129
Gilles Peskined1465422022-04-13 23:59:52 +0200130int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
131 unsigned long long value1, unsigned long long value2 )
132{
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200133 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
134 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
135
Gilles Peskined1465422022-04-13 23:59:52 +0200136 if( value1 <= value2 )
137 return( 1 );
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200138
Gilles Peskined1465422022-04-13 23:59:52 +0200139 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
140 {
141 /* We've already recorded the test as having failed. Don't
142 * overwrite any previous information about the failure. */
143 return( 0 );
144 }
145 mbedtls_test_fail( test, line_no, filename );
146 (void) mbedtls_snprintf( mbedtls_test_info.line1,
147 sizeof( mbedtls_test_info.line1 ),
148 "lhs = 0x%016llx = %llu",
149 value1, value1 );
150 (void) mbedtls_snprintf( mbedtls_test_info.line2,
151 sizeof( mbedtls_test_info.line2 ),
152 "rhs = 0x%016llx = %llu",
153 value2, value2 );
154 return( 0 );
155}
156
157int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
158 long long value1, long long value2 )
159{
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200160 TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
161 TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
162
Gilles Peskined1465422022-04-13 23:59:52 +0200163 if( value1 <= value2 )
164 return( 1 );
Gilles Peskinebdc7b8b2022-09-20 18:31:30 +0200165
Gilles Peskined1465422022-04-13 23:59:52 +0200166 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
167 {
168 /* We've already recorded the test as having failed. Don't
169 * overwrite any previous information about the failure. */
170 return( 0 );
171 }
172 mbedtls_test_fail( test, line_no, filename );
173 (void) mbedtls_snprintf( mbedtls_test_info.line1,
174 sizeof( mbedtls_test_info.line1 ),
175 "lhs = 0x%016llx = %lld",
176 (unsigned long long) value1, value1 );
177 (void) mbedtls_snprintf( mbedtls_test_info.line2,
178 sizeof( mbedtls_test_info.line2 ),
179 "rhs = 0x%016llx = %lld",
180 (unsigned long long) value2, value2 );
181 return( 0 );
182}
183
Ronald Crona0c25392020-06-18 10:10:46 +0200184int mbedtls_test_unhexify( unsigned char *obuf,
185 size_t obufmax,
186 const char *ibuf,
187 size_t *len )
188{
189 unsigned char uc, uc2;
190
191 *len = strlen( ibuf );
192
193 /* Must be even number of bytes. */
194 if ( ( *len ) & 1 )
195 return( -1 );
196 *len /= 2;
197
198 if ( (*len) > obufmax )
199 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200200
201 while( *ibuf != 0 )
202 {
Ronald Crona0c25392020-06-18 10:10:46 +0200203 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
204 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200205
Ronald Crona0c25392020-06-18 10:10:46 +0200206 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
207 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200208
Ronald Crona0c25392020-06-18 10:10:46 +0200209 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200210 }
211
Ronald Crona0c25392020-06-18 10:10:46 +0200212 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200213}
214
Ronald Cron72d628f2020-06-08 17:05:57 +0200215void mbedtls_test_hexify( unsigned char *obuf,
216 const unsigned char *ibuf,
217 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200218{
219 unsigned char l, h;
220
221 while( len != 0 )
222 {
223 h = *ibuf / 16;
224 l = *ibuf % 16;
225
226 if( h < 10 )
227 *obuf++ = '0' + h;
228 else
229 *obuf++ = 'a' + h - 10;
230
231 if( l < 10 )
232 *obuf++ = '0' + l;
233 else
234 *obuf++ = 'a' + l - 10;
235
236 ++ibuf;
237 len--;
238 }
239}
240
Ronald Cron690f3eb2020-06-10 10:42:18 +0200241unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200242{
243 void *p;
244 size_t actual_len = ( len != 0 ) ? len : 1;
245
246 p = mbedtls_calloc( 1, actual_len );
247 TEST_HELPER_ASSERT( p != NULL );
248
249 memset( p, 0x00, actual_len );
250
251 return( p );
252}
253
Ronald Crona256c702020-06-10 10:53:11 +0200254unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200255{
256 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200257 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200258
259 *olen = strlen( ibuf ) / 2;
260
261 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200262 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200263
264 obuf = mbedtls_calloc( 1, *olen );
265 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200266 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200267
268 return( obuf );
269}
270
Ronald Cronde70b162020-06-10 11:03:08 +0200271int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
272 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200273{
274 int ret = 0;
275 uint32_t i = 0;
276
277 if( a_len != b_len )
278 return( -1 );
279
280 for( i = 0; i < a_len; i++ )
281 {
282 if( a[i] != b[i] )
283 {
284 ret = -1;
285 break;
286 }
287 }
288 return ret;
289}
Ronald Crona1236142020-07-01 16:01:21 +0200290
Chris Jones96ae73b2021-01-08 17:04:59 +0000291#if defined(MBEDTLS_TEST_HOOKS)
292void mbedtls_test_err_add_check( int high, int low,
293 const char *file, int line )
294{
Chris Jones3f613c12021-03-31 09:34:22 +0100295 /* Error codes are always negative (a value of zero is a success) however
296 * their positive opposites can be easier to understand. The following
297 * examples given in comments have been made positive for ease of
298 * understanding. The structure of an error code is such:
299 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100300 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100301 *
302 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100303 * h = high level error code (includes high level module ID (bits 12..14)
304 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100305 * l = low level error code.
306 */
Chris Jonese11e8142021-04-22 15:28:56 +0100307 if ( high > -0x1000 && high != 0 )
308 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100309 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100310 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000311 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000312 mbedtls_test_fail( "'high' is not a high-level error code",
313 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000314 }
Chris Jonese11e8142021-04-22 15:28:56 +0100315 else if ( high < -0x7F80 )
316 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100317 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100318 */
Chris Jonesa203c382021-01-29 14:56:20 +0000319 {
Chris Jones3f613c12021-03-31 09:34:22 +0100320 mbedtls_test_fail( "'high' error code is greater than 15 bits",
321 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000322 }
Chris Jonese11e8142021-04-22 15:28:56 +0100323 else if ( ( high & 0x7F ) != 0 )
324 /* high & 0000000001111111
325 * Error code contains low level error code bits.
326 */
Chris Jonesa203c382021-01-29 14:56:20 +0000327 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000328 mbedtls_test_fail( "'high' contains a low-level error code",
329 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000330 }
Chris Jonese11e8142021-04-22 15:28:56 +0100331 else if ( low < -0x007F )
332 /* low > 0000000001111111
333 * Error code contains high or module level error code bits.
334 */
Chris Jonesa203c382021-01-29 14:56:20 +0000335 {
Chris Jones3f613c12021-03-31 09:34:22 +0100336 mbedtls_test_fail( "'low' error code is greater than 7 bits",
337 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000338 }
339 else if ( low > 0 )
340 {
Chris Jones3f613c12021-03-31 09:34:22 +0100341 mbedtls_test_fail( "'low' error code is greater than zero",
342 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000343 }
344}
345#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskineebc49e52021-06-11 14:13:53 +0200346
347#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200348#include "bignum_core.h"
349
350int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs,
Gilles Peskinec5772a12022-10-09 21:14:09 +0200351 const char *input )
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200352{
353 /* Sanity check */
354 if( *pX != NULL )
355 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
356
Gilles Peskinec5772a12022-10-09 21:14:09 +0200357 size_t hex_len = strlen( input );
358 size_t byte_len = ( hex_len + 1 ) / 2;
359 *plimbs = CHARS_TO_LIMBS( byte_len );
Gilles Peskine0b7e0792022-11-09 10:45:15 +0100360
361 /* A core bignum is not allowed to be empty. Forbid it as test data,
362 * this way static analyzers have a chance of knowing we don't expect
363 * the bignum functions to support empty inputs. */
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200364 if( *plimbs == 0 )
Gilles Peskine0b7e0792022-11-09 10:45:15 +0100365 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskinec5772a12022-10-09 21:14:09 +0200366
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200367 *pX = mbedtls_calloc( *plimbs, sizeof( **pX ) );
368 if( *pX == NULL )
369 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Gilles Peskinec5772a12022-10-09 21:14:09 +0200370
371 unsigned char *byte_start = ( unsigned char * ) *pX;
372 if( byte_len % sizeof( mbedtls_mpi_uint ) != 0 )
373 {
374 byte_start += sizeof( mbedtls_mpi_uint ) - byte_len % sizeof( mbedtls_mpi_uint );
375 }
376 if( ( hex_len & 1 ) != 0 )
377 {
378 /* mbedtls_test_unhexify wants an even number of hex digits */
379 TEST_ASSERT( ascii2uc( *input, byte_start ) == 0 );
380 ++byte_start;
381 ++input;
382 --byte_len;
383 }
384 TEST_ASSERT( mbedtls_test_unhexify( byte_start,
385 byte_len,
386 input,
387 &byte_len ) == 0 );
388
389 mbedtls_mpi_core_bigendian_to_host( *pX, *plimbs );
390 return( 0 );
391
392exit:
393 mbedtls_free( *pX );
394 return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
Gilles Peskine3aae4e82022-09-20 21:38:33 +0200395}
396
Werner Lewis19b4cd82022-07-07 11:02:27 +0100397int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
Gilles Peskineebc49e52021-06-11 14:13:53 +0200398{
399 /* mbedtls_mpi_read_string() currently retains leading zeros.
400 * It always allocates at least one limb for the value 0. */
401 if( s[0] == 0 )
402 {
403 mbedtls_mpi_free( X );
404 return( 0 );
405 }
406 else
Werner Lewis19b4cd82022-07-07 11:02:27 +0100407 return( mbedtls_mpi_read_string( X, 16, s ) );
Gilles Peskineebc49e52021-06-11 14:13:53 +0200408}
409#endif