blob: 4f976a27b0435c717c21b2e5a85a3f17f3af3630 [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/*----------------------------------------------------------------------------*/
23/* Static global variables */
24
Ronald Cronf40529d2020-06-09 16:27:37 +020025#if defined(MBEDTLS_PLATFORM_C)
26static mbedtls_platform_context platform_ctx;
27#endif
28
Chris Jonese60e2ae2021-01-20 17:51:47 +000029mbedtls_test_info_t mbedtls_test_info;
Chris Jones9634bb12021-01-20 15:56:42 +000030
Ronald Crona1236142020-07-01 16:01:21 +020031/*----------------------------------------------------------------------------*/
32/* Helper Functions */
33
Ronald Crone9c09f12020-06-08 16:44:58 +020034int mbedtls_test_platform_setup( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020035{
36 int ret = 0;
37#if defined(MBEDTLS_PLATFORM_C)
38 ret = mbedtls_platform_setup( &platform_ctx );
39#endif /* MBEDTLS_PLATFORM_C */
40 return( ret );
41}
42
Ronald Crone9c09f12020-06-08 16:44:58 +020043void mbedtls_test_platform_teardown( void )
Ronald Cronf40529d2020-06-09 16:27:37 +020044{
45#if defined(MBEDTLS_PLATFORM_C)
46 mbedtls_platform_teardown( &platform_ctx );
47#endif /* MBEDTLS_PLATFORM_C */
48}
49
Ronald Crona0c25392020-06-18 10:10:46 +020050static int ascii2uc(const char c, unsigned char *uc)
Ronald Cronf40529d2020-06-09 16:27:37 +020051{
Ronald Crona0c25392020-06-18 10:10:46 +020052 if( ( c >= '0' ) && ( c <= '9' ) )
53 *uc = c - '0';
54 else if( ( c >= 'a' ) && ( c <= 'f' ) )
55 *uc = c - 'a' + 10;
56 else if( ( c >= 'A' ) && ( c <= 'F' ) )
57 *uc = c - 'A' + 10;
58 else
59 return( -1 );
60
61 return( 0 );
62}
63
Chris Jones9634bb12021-01-20 15:56:42 +000064void mbedtls_test_fail( const char *test, int line_no, const char* filename )
65{
Chris Jonese60e2ae2021-01-20 17:51:47 +000066 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
Chris Jones9634bb12021-01-20 15:56:42 +000067 {
68 /* We've already recorded the test as having failed. Don't
69 * overwrite any previous information about the failure. */
70 return;
71 }
Chris Jonese60e2ae2021-01-20 17:51:47 +000072 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_FAILED;
73 mbedtls_test_info.test = test;
74 mbedtls_test_info.line_no = line_no;
75 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000076}
77
Chris Jones9634bb12021-01-20 15:56:42 +000078void mbedtls_test_skip( const char *test, int line_no, const char* filename )
79{
Chris Jonese60e2ae2021-01-20 17:51:47 +000080 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SKIPPED;
81 mbedtls_test_info.test = test;
82 mbedtls_test_info.line_no = line_no;
83 mbedtls_test_info.filename = filename;
Chris Jones9634bb12021-01-20 15:56:42 +000084}
85
Chris Jonesa5ab7652021-02-02 16:20:45 +000086void mbedtls_test_set_step( unsigned long step )
87{
88 mbedtls_test_info.step = step;
89}
90
91void mbedtls_test_info_reset( void )
92{
93 mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
94 mbedtls_test_info.step = (unsigned long)( -1 );
95 mbedtls_test_info.test = 0;
96 mbedtls_test_info.line_no = 0;
97 mbedtls_test_info.filename = 0;
Gilles Peskine89615ee2021-04-29 20:28:54 +020098 memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) );
99 memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) );
100}
101
102int mbedtls_test_equal( const char *test, int line_no, const char* filename,
103 unsigned long long value1, unsigned long long value2 )
104{
105 if( value1 == value2 )
106 return( 1 );
107 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
108 {
109 /* We've already recorded the test as having failed. Don't
110 * overwrite any previous information about the failure. */
111 return( 0 );
112 }
113 mbedtls_test_fail( test, line_no, filename );
114 (void) mbedtls_snprintf( mbedtls_test_info.line1,
115 sizeof( mbedtls_test_info.line1 ),
116 "lhs = 0x%016llx = %lld",
117 value1, (long long) value1 );
118 (void) mbedtls_snprintf( mbedtls_test_info.line2,
119 sizeof( mbedtls_test_info.line2 ),
120 "rhs = 0x%016llx = %lld",
121 value2, (long long) value2 );
122 return( 0 );
Chris Jonesa5ab7652021-02-02 16:20:45 +0000123}
124
Gilles Peskined1465422022-04-13 23:59:52 +0200125int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
126 unsigned long long value1, unsigned long long value2 )
127{
128 if( value1 <= value2 )
129 return( 1 );
130 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 = %llu",
140 value1, value1 );
141 (void) mbedtls_snprintf( mbedtls_test_info.line2,
142 sizeof( mbedtls_test_info.line2 ),
143 "rhs = 0x%016llx = %llu",
144 value2, value2 );
145 return( 0 );
146}
147
148int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
149 long long value1, long long value2 )
150{
151 if( value1 <= value2 )
152 return( 1 );
153 if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
154 {
155 /* We've already recorded the test as having failed. Don't
156 * overwrite any previous information about the failure. */
157 return( 0 );
158 }
159 mbedtls_test_fail( test, line_no, filename );
160 (void) mbedtls_snprintf( mbedtls_test_info.line1,
161 sizeof( mbedtls_test_info.line1 ),
162 "lhs = 0x%016llx = %lld",
163 (unsigned long long) value1, value1 );
164 (void) mbedtls_snprintf( mbedtls_test_info.line2,
165 sizeof( mbedtls_test_info.line2 ),
166 "rhs = 0x%016llx = %lld",
167 (unsigned long long) value2, value2 );
168 return( 0 );
169}
170
Ronald Crona0c25392020-06-18 10:10:46 +0200171int mbedtls_test_unhexify( unsigned char *obuf,
172 size_t obufmax,
173 const char *ibuf,
174 size_t *len )
175{
176 unsigned char uc, uc2;
177
178 *len = strlen( ibuf );
179
180 /* Must be even number of bytes. */
181 if ( ( *len ) & 1 )
182 return( -1 );
183 *len /= 2;
184
185 if ( (*len) > obufmax )
186 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200187
188 while( *ibuf != 0 )
189 {
Ronald Crona0c25392020-06-18 10:10:46 +0200190 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
191 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200192
Ronald Crona0c25392020-06-18 10:10:46 +0200193 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
194 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200195
Ronald Crona0c25392020-06-18 10:10:46 +0200196 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200197 }
198
Ronald Crona0c25392020-06-18 10:10:46 +0200199 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200200}
201
Ronald Cron72d628f2020-06-08 17:05:57 +0200202void mbedtls_test_hexify( unsigned char *obuf,
203 const unsigned char *ibuf,
204 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200205{
206 unsigned char l, h;
207
208 while( len != 0 )
209 {
210 h = *ibuf / 16;
211 l = *ibuf % 16;
212
213 if( h < 10 )
214 *obuf++ = '0' + h;
215 else
216 *obuf++ = 'a' + h - 10;
217
218 if( l < 10 )
219 *obuf++ = '0' + l;
220 else
221 *obuf++ = 'a' + l - 10;
222
223 ++ibuf;
224 len--;
225 }
226}
227
Ronald Cron690f3eb2020-06-10 10:42:18 +0200228unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200229{
230 void *p;
231 size_t actual_len = ( len != 0 ) ? len : 1;
232
233 p = mbedtls_calloc( 1, actual_len );
234 TEST_HELPER_ASSERT( p != NULL );
235
236 memset( p, 0x00, actual_len );
237
238 return( p );
239}
240
Ronald Crona256c702020-06-10 10:53:11 +0200241unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200242{
243 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200244 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200245
246 *olen = strlen( ibuf ) / 2;
247
248 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200249 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200250
251 obuf = mbedtls_calloc( 1, *olen );
252 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200253 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200254
255 return( obuf );
256}
257
Ronald Cronde70b162020-06-10 11:03:08 +0200258int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
259 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200260{
261 int ret = 0;
262 uint32_t i = 0;
263
264 if( a_len != b_len )
265 return( -1 );
266
267 for( i = 0; i < a_len; i++ )
268 {
269 if( a[i] != b[i] )
270 {
271 ret = -1;
272 break;
273 }
274 }
275 return ret;
276}
Ronald Crona1236142020-07-01 16:01:21 +0200277
Chris Jones96ae73b2021-01-08 17:04:59 +0000278#if defined(MBEDTLS_TEST_HOOKS)
279void mbedtls_test_err_add_check( int high, int low,
280 const char *file, int line )
281{
Chris Jones3f613c12021-03-31 09:34:22 +0100282 /* Error codes are always negative (a value of zero is a success) however
283 * their positive opposites can be easier to understand. The following
284 * examples given in comments have been made positive for ease of
285 * understanding. The structure of an error code is such:
286 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100287 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100288 *
289 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100290 * h = high level error code (includes high level module ID (bits 12..14)
291 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100292 * l = low level error code.
293 */
Chris Jonese11e8142021-04-22 15:28:56 +0100294 if ( high > -0x1000 && high != 0 )
295 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100296 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100297 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000298 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000299 mbedtls_test_fail( "'high' is not a high-level error code",
300 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000301 }
Chris Jonese11e8142021-04-22 15:28:56 +0100302 else if ( high < -0x7F80 )
303 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100304 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100305 */
Chris Jonesa203c382021-01-29 14:56:20 +0000306 {
Chris Jones3f613c12021-03-31 09:34:22 +0100307 mbedtls_test_fail( "'high' error code is greater than 15 bits",
308 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000309 }
Chris Jonese11e8142021-04-22 15:28:56 +0100310 else if ( ( high & 0x7F ) != 0 )
311 /* high & 0000000001111111
312 * Error code contains low level error code bits.
313 */
Chris Jonesa203c382021-01-29 14:56:20 +0000314 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000315 mbedtls_test_fail( "'high' contains a low-level error code",
316 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000317 }
Chris Jonese11e8142021-04-22 15:28:56 +0100318 else if ( low < -0x007F )
319 /* low > 0000000001111111
320 * Error code contains high or module level error code bits.
321 */
Chris Jonesa203c382021-01-29 14:56:20 +0000322 {
Chris Jones3f613c12021-03-31 09:34:22 +0100323 mbedtls_test_fail( "'low' error code is greater than 7 bits",
324 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000325 }
326 else if ( low > 0 )
327 {
Chris Jones3f613c12021-03-31 09:34:22 +0100328 mbedtls_test_fail( "'low' error code is greater than zero",
329 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000330 }
331}
332#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskineebc49e52021-06-11 14:13:53 +0200333
334#if defined(MBEDTLS_BIGNUM_C)
Werner Lewis19b4cd82022-07-07 11:02:27 +0100335int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
Gilles Peskineebc49e52021-06-11 14:13:53 +0200336{
337 /* mbedtls_mpi_read_string() currently retains leading zeros.
338 * It always allocates at least one limb for the value 0. */
339 if( s[0] == 0 )
340 {
341 mbedtls_mpi_free( X );
342 return( 0 );
343 }
344 else
Werner Lewis19b4cd82022-07-07 11:02:27 +0100345 return( mbedtls_mpi_read_string( X, 16, s ) );
Gilles Peskineebc49e52021-06-11 14:13:53 +0200346}
347#endif