blob: 7bbfa50d5806bcd72aafdf157416c0b992c7a13e [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 */
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020040 return ret ;
Ronald Cronf40529d2020-06-09 16:27:37 +020041}
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
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020059 return -1 ;
Ronald Crona0c25392020-06-18 10:10:46 +020060
Mateusz Starzyke36f5b12021-07-22 16:43:35 +020061 return 0 ;
Ronald Crona0c25392020-06-18 10:10:46 +020062}
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;
98}
99
Ronald Crona0c25392020-06-18 10:10:46 +0200100int mbedtls_test_unhexify( unsigned char *obuf,
101 size_t obufmax,
102 const char *ibuf,
103 size_t *len )
104{
105 unsigned char uc, uc2;
106
107 *len = strlen( ibuf );
108
109 /* Must be even number of bytes. */
110 if ( ( *len ) & 1 )
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200111 return -1 ;
Ronald Crona0c25392020-06-18 10:10:46 +0200112 *len /= 2;
113
114 if ( (*len) > obufmax )
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200115 return -1 ;
Ronald Cronf40529d2020-06-09 16:27:37 +0200116
117 while( *ibuf != 0 )
118 {
Ronald Crona0c25392020-06-18 10:10:46 +0200119 if ( ascii2uc( *(ibuf++), &uc ) != 0 )
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200120 return -1 ;
Ronald Cronf40529d2020-06-09 16:27:37 +0200121
Ronald Crona0c25392020-06-18 10:10:46 +0200122 if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200123 return -1 ;
Ronald Cronf40529d2020-06-09 16:27:37 +0200124
Ronald Crona0c25392020-06-18 10:10:46 +0200125 *(obuf++) = ( uc << 4 ) | uc2;
Ronald Cronf40529d2020-06-09 16:27:37 +0200126 }
127
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200128 return 0 ;
Ronald Cronf40529d2020-06-09 16:27:37 +0200129}
130
Ronald Cron72d628f2020-06-08 17:05:57 +0200131void mbedtls_test_hexify( unsigned char *obuf,
132 const unsigned char *ibuf,
133 int len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200134{
135 unsigned char l, h;
136
137 while( len != 0 )
138 {
139 h = *ibuf / 16;
140 l = *ibuf % 16;
141
142 if( h < 10 )
143 *obuf++ = '0' + h;
144 else
145 *obuf++ = 'a' + h - 10;
146
147 if( l < 10 )
148 *obuf++ = '0' + l;
149 else
150 *obuf++ = 'a' + l - 10;
151
152 ++ibuf;
153 len--;
154 }
155}
156
Ronald Cron690f3eb2020-06-10 10:42:18 +0200157unsigned char *mbedtls_test_zero_alloc( size_t len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200158{
159 void *p;
160 size_t actual_len = ( len != 0 ) ? len : 1;
161
162 p = mbedtls_calloc( 1, actual_len );
163 TEST_HELPER_ASSERT( p != NULL );
164
165 memset( p, 0x00, actual_len );
166
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200167 return p ;
Ronald Cronf40529d2020-06-09 16:27:37 +0200168}
169
Ronald Crona256c702020-06-10 10:53:11 +0200170unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
Ronald Cronf40529d2020-06-09 16:27:37 +0200171{
172 unsigned char *obuf;
Ronald Crona0c25392020-06-18 10:10:46 +0200173 size_t len;
Ronald Cronf40529d2020-06-09 16:27:37 +0200174
175 *olen = strlen( ibuf ) / 2;
176
177 if( *olen == 0 )
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200178 return mbedtls_test_zero_alloc( *olen ) ;
Ronald Cronf40529d2020-06-09 16:27:37 +0200179
180 obuf = mbedtls_calloc( 1, *olen );
181 TEST_HELPER_ASSERT( obuf != NULL );
Ronald Crona0c25392020-06-18 10:10:46 +0200182 TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
Ronald Cronf40529d2020-06-09 16:27:37 +0200183
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200184 return obuf ;
Ronald Cronf40529d2020-06-09 16:27:37 +0200185}
186
Ronald Cronde70b162020-06-10 11:03:08 +0200187int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
188 uint32_t a_len, uint32_t b_len )
Ronald Cronf40529d2020-06-09 16:27:37 +0200189{
190 int ret = 0;
191 uint32_t i = 0;
192
193 if( a_len != b_len )
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200194 return -1 ;
Ronald Cronf40529d2020-06-09 16:27:37 +0200195
196 for( i = 0; i < a_len; i++ )
197 {
198 if( a[i] != b[i] )
199 {
200 ret = -1;
201 break;
202 }
203 }
204 return ret;
205}
Ronald Crona1236142020-07-01 16:01:21 +0200206
Chris Jones96ae73b2021-01-08 17:04:59 +0000207#if defined(MBEDTLS_TEST_HOOKS)
208void mbedtls_test_err_add_check( int high, int low,
209 const char *file, int line )
210{
Chris Jones3f613c12021-03-31 09:34:22 +0100211 /* Error codes are always negative (a value of zero is a success) however
212 * their positive opposites can be easier to understand. The following
213 * examples given in comments have been made positive for ease of
214 * understanding. The structure of an error code is such:
215 *
Chris Jonesabded0e2021-04-12 15:44:47 +0100216 * shhhhhhhhlllllll
Chris Jones3f613c12021-03-31 09:34:22 +0100217 *
218 * s = sign bit.
Chris Jones4f91d8d2021-04-23 12:07:25 +0100219 * h = high level error code (includes high level module ID (bits 12..14)
220 * and module-dependent error code (bits 7..11)).
Chris Jones3f613c12021-03-31 09:34:22 +0100221 * l = low level error code.
222 */
Chris Jonese11e8142021-04-22 15:28:56 +0100223 if ( high > -0x1000 && high != 0 )
224 /* high < 0001000000000000
Chris Jones4f91d8d2021-04-23 12:07:25 +0100225 * No high level module ID bits are set.
Chris Jonese11e8142021-04-22 15:28:56 +0100226 */
Chris Jones96ae73b2021-01-08 17:04:59 +0000227 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000228 mbedtls_test_fail( "'high' is not a high-level error code",
229 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000230 }
Chris Jonese11e8142021-04-22 15:28:56 +0100231 else if ( high < -0x7F80 )
232 /* high > 0111111110000000
Chris Jones860f5092021-04-26 16:31:16 +0100233 * Error code is greater than the largest allowed high level module ID.
Chris Jonese11e8142021-04-22 15:28:56 +0100234 */
Chris Jonesa203c382021-01-29 14:56:20 +0000235 {
Chris Jones3f613c12021-03-31 09:34:22 +0100236 mbedtls_test_fail( "'high' error code is greater than 15 bits",
237 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000238 }
Chris Jonese11e8142021-04-22 15:28:56 +0100239 else if ( ( high & 0x7F ) != 0 )
240 /* high & 0000000001111111
241 * Error code contains low level error code bits.
242 */
Chris Jonesa203c382021-01-29 14:56:20 +0000243 {
Chris Jonesfe285f52021-02-08 12:32:41 +0000244 mbedtls_test_fail( "'high' contains a low-level error code",
245 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000246 }
Chris Jonese11e8142021-04-22 15:28:56 +0100247 else if ( low < -0x007F )
248 /* low > 0000000001111111
249 * Error code contains high or module level error code bits.
250 */
Chris Jonesa203c382021-01-29 14:56:20 +0000251 {
Chris Jones3f613c12021-03-31 09:34:22 +0100252 mbedtls_test_fail( "'low' error code is greater than 7 bits",
253 line, file );
Chris Jonesa203c382021-01-29 14:56:20 +0000254 }
255 else if ( low > 0 )
256 {
Chris Jones3f613c12021-03-31 09:34:22 +0100257 mbedtls_test_fail( "'low' error code is greater than zero",
258 line, file );
Chris Jones96ae73b2021-01-08 17:04:59 +0000259 }
260}
261#endif /* MBEDTLS_TEST_HOOKS */
Gilles Peskineebc49e52021-06-11 14:13:53 +0200262
263#if defined(MBEDTLS_BIGNUM_C)
264int mbedtls_test_read_mpi( mbedtls_mpi *X, int radix, const char *s )
265{
266 /* mbedtls_mpi_read_string() currently retains leading zeros.
267 * It always allocates at least one limb for the value 0. */
268 if( s[0] == 0 )
269 {
270 mbedtls_mpi_free( X );
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200271 return 0 ;
Gilles Peskineebc49e52021-06-11 14:13:53 +0200272 }
273 else
Mateusz Starzyke36f5b12021-07-22 16:43:35 +0200274 return mbedtls_mpi_read_string( X, radix, s ) ;
Gilles Peskineebc49e52021-06-11 14:13:53 +0200275}
276#endif