blob: ec4d84eac18c5cf0cfd64c3af36f06cf5128e07e [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
Ronald Crona0c25392020-06-18 10:10:46 +0200125int mbedtls_test_unhexify( unsigned char *obuf,
126 size_t obufmax,
127 const char *ibuf,
128 size_t *len )
129{
130 unsigned char uc, uc2;
131
132 *len = strlen( ibuf );
133
134 /* Must be even number of bytes. */
135 if ( ( *len ) & 1 )
136 return( -1 );
137 *len /= 2;
138
139 if ( (*len) > obufmax )
140 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200141
142 while( *ibuf != 0 )
143 {
Ronald Crona0c25392020-06-18 10:10:46 +0200144 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
145 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200146
Ronald Crona0c25392020-06-18 10:10:46 +0200147 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
148 return( -1 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200149
Ronald Crona0c25392020-06-18 10:10:46 +0200150 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200151 }
152
Ronald Crona0c25392020-06-18 10:10:46 +0200153 return( 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200154}
155
Ronald Cron72d628f2020-06-08 17:05:57 +0200156void mbedtls_test_hexify( unsigned char *obuf,
157 const unsigned char *ibuf,
158 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200159{
160 unsigned char l, h;
161
162 while( len != 0 )
163 {
164 h = *ibuf / 16;
165 l = *ibuf % 16;
166
167 if( h < 10 )
168 *obuf++ = '0' + h;
169 else
170 *obuf++ = 'a' + h - 10;
171
172 if( l < 10 )
173 *obuf++ = '0' + l;
174 else
175 *obuf++ = 'a' + l - 10;
176
177 ++ibuf;
178 len--;
179 }
180}
181
Ronald Cron690f3eb2020-06-10 10:42:18 +0200182unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200183{
184 void *p;
185 size_t actual_len = ( len != 0 ) ? len : 1;
186
187 p = mbedtls_calloc( 1, actual_len );
188 TEST_HELPER_ASSERT( p != NULL );
189
190 memset( p, 0x00, actual_len );
191
192 return( p );
193}
194
Ronald Crona256c702020-06-10 10:53:11 +0200195unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200196{
197 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200198 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200199
200 *olen = strlen( ibuf ) / 2;
201
202 if( *olen == 0 )
Ronald Cron690f3eb2020-06-10 10:42:18 +0200203 return( mbedtls_test_zero_alloc( *olen ) );
Ronald Cronf40529d2020-06-09 16:27:37 +0200204
205 obuf = mbedtls_calloc( 1, *olen );
206 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200207 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200208
209 return( obuf );
210}
211
Ronald Cronde70b162020-06-10 11:03:08 +0200212int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
213 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200214{
215 int ret = 0;
216 uint32_t i = 0;
217
218 if( a_len != b_len )
219 return( -1 );
220
221 for( i = 0; i < a_len; i++ )
222 {
223 if( a[i] != b[i] )
224 {
225 ret = -1;
226 break;
227 }
228 }
229 return ret;
230}
Ronald Crona1236142020-07-01 16:01:21 +0200231
Chris Jones96ae73b2021-01-08 17:04:59 +0000232#if defined(MBEDTLS_TEST_HOOKS)
233void mbedtls_test_err_add_check( int high, int low,
234 const char *file, int line )
235{
Chris Jones3f613c12021-03-31 09:34:22 +0100236 /* Error codes are always negative (a value of zero is a success) however
237 * their positive opposites can be easier to understand. The following
238 * examples given in comments have been made positive for ease of
239 * understanding. The structure of an error code is such:
240 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100241 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100242 *
243 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100244 * h = high level error code (includes high level module ID (bits 12..14)
245 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100246 * l = low level error code.
247 */
Chris Jonese11e8142021-04-22 15:28:56 +0100248 if ( high > -0x1000 && high != 0 )
249 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100250 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100251 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000252 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000253 mbedtls_test_fail( "'high' is not a high-level error code",
254 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000255 }
Chris Jonese11e8142021-04-22 15:28:56 +0100256 else if ( high < -0x7F80 )
257 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100258 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100259 */
Chris Jonesa203c382021-01-29 14:56:20 +0000260 {
Chris Jones3f613c12021-03-31 09:34:22 +0100261 mbedtls_test_fail( "'high' error code is greater than 15 bits",
262 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000263 }
Chris Jonese11e8142021-04-22 15:28:56 +0100264 else if ( ( high & 0x7F ) != 0 )
265 /* high & 0000000001111111
266 * Error code contains low level error code bits.
267 */
Chris Jonesa203c382021-01-29 14:56:20 +0000268 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000269 mbedtls_test_fail( "'high' contains a low-level error code",
270 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000271 }
Chris Jonese11e8142021-04-22 15:28:56 +0100272 else if ( low < -0x007F )
273 /* low > 0000000001111111
274 * Error code contains high or module level error code bits.
275 */
Chris Jonesa203c382021-01-29 14:56:20 +0000276 {
Chris Jones3f613c12021-03-31 09:34:22 +0100277 mbedtls_test_fail( "'low' error code is greater than 7 bits",
278 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000279 }
280 else if ( low > 0 )
281 {
Chris Jones3f613c12021-03-31 09:34:22 +0100282 mbedtls_test_fail( "'low' error code is greater than zero",
283 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000284 }
285}
286#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskineebc49e52021-06-11 14:13:53 +0200287
288#if defined(MBEDTLS_BIGNUM_C)
289int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s )
290{
291 /* mbedtls_mpi_read_string() currently retains leading zeros.
292 * It always allocates at least one limb for the value 0. */
293 if( s[0] == 0 )
294 {
295 mbedtls_mpi_free( X );
296 return( 0 );
297 }
298 else
299 return( mbedtls_mpi_read_string( X, radix, s ) );
300}
301#endif