1 /*
2  *  Copyright The Mbed TLS Contributors
3  *  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.
16  */
17 
18 #include <test/constant_flow.h>
19 #include <test/helpers.h>
20 #include <test/macros.h>
21 #include <string.h>
22 
23 /*----------------------------------------------------------------------------*/
24 /* Static global variables */
25 
26 #if defined(MBEDTLS_PLATFORM_C)
27 static mbedtls_platform_context platform_ctx;
28 #endif
29 
30 mbedtls_test_info_t mbedtls_test_info;
31 
32 /*----------------------------------------------------------------------------*/
33 /* Helper Functions */
34 
mbedtls_test_platform_setup(void)35 int mbedtls_test_platform_setup( void )
36 {
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 
mbedtls_test_platform_teardown(void)44 void mbedtls_test_platform_teardown( void )
45 {
46 #if defined(MBEDTLS_PLATFORM_C)
47     mbedtls_platform_teardown( &platform_ctx );
48 #endif /* MBEDTLS_PLATFORM_C */
49 }
50 
ascii2uc(const char c,unsigned char * uc)51 static int ascii2uc(const char c, unsigned char *uc)
52 {
53     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 
mbedtls_test_fail(const char * test,int line_no,const char * filename)65 void mbedtls_test_fail( const char *test, int line_no, const char* filename )
66 {
67     if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
68     {
69         /* We've already recorded the test as having failed. Don't
70          * overwrite any previous information about the failure. */
71         return;
72     }
73     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;
77 }
78 
mbedtls_test_skip(const char * test,int line_no,const char * filename)79 void mbedtls_test_skip( const char *test, int line_no, const char* filename )
80 {
81     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;
85 }
86 
mbedtls_test_set_step(unsigned long step)87 void mbedtls_test_set_step( unsigned long step )
88 {
89     mbedtls_test_info.step = step;
90 }
91 
92 #if defined(MBEDTLS_BIGNUM_C)
93 unsigned mbedtls_test_case_uses_negative_0 = 0;
94 #endif
95 
mbedtls_test_info_reset(void)96 void mbedtls_test_info_reset( void )
97 {
98     mbedtls_test_info.result = MBEDTLS_TEST_RESULT_SUCCESS;
99     mbedtls_test_info.step = (unsigned long)( -1 );
100     mbedtls_test_info.test = 0;
101     mbedtls_test_info.line_no = 0;
102     mbedtls_test_info.filename = 0;
103     memset( mbedtls_test_info.line1, 0, sizeof( mbedtls_test_info.line1 ) );
104     memset( mbedtls_test_info.line2, 0, sizeof( mbedtls_test_info.line2 ) );
105 #if defined(MBEDTLS_BIGNUM_C)
106     mbedtls_test_case_uses_negative_0 = 0;
107 #endif
108 }
109 
mbedtls_test_equal(const char * test,int line_no,const char * filename,unsigned long long value1,unsigned long long value2)110 int mbedtls_test_equal( const char *test, int line_no, const char* filename,
111                         unsigned long long value1, unsigned long long value2 )
112 {
113     TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
114     TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
115 
116     if( value1 == value2 )
117         return( 1 );
118 
119     if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
120     {
121         /* We've already recorded the test as having failed. Don't
122          * overwrite any previous information about the failure. */
123         return( 0 );
124     }
125     mbedtls_test_fail( test, line_no, filename );
126     (void) mbedtls_snprintf( mbedtls_test_info.line1,
127                              sizeof( mbedtls_test_info.line1 ),
128                              "lhs = 0x%016llx = %lld",
129                              value1, (long long) value1 );
130     (void) mbedtls_snprintf( mbedtls_test_info.line2,
131                              sizeof( mbedtls_test_info.line2 ),
132                              "rhs = 0x%016llx = %lld",
133                              value2, (long long) value2 );
134     return( 0 );
135 }
136 
mbedtls_test_le_u(const char * test,int line_no,const char * filename,unsigned long long value1,unsigned long long value2)137 int mbedtls_test_le_u( const char *test, int line_no, const char* filename,
138                        unsigned long long value1, unsigned long long value2 )
139 {
140     TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
141     TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
142 
143     if( value1 <= value2 )
144         return( 1 );
145 
146     if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
147     {
148         /* We've already recorded the test as having failed. Don't
149          * overwrite any previous information about the failure. */
150         return( 0 );
151     }
152     mbedtls_test_fail( test, line_no, filename );
153     (void) mbedtls_snprintf( mbedtls_test_info.line1,
154                              sizeof( mbedtls_test_info.line1 ),
155                              "lhs = 0x%016llx = %llu",
156                              value1, value1 );
157     (void) mbedtls_snprintf( mbedtls_test_info.line2,
158                              sizeof( mbedtls_test_info.line2 ),
159                              "rhs = 0x%016llx = %llu",
160                              value2, value2 );
161     return( 0 );
162 }
163 
mbedtls_test_le_s(const char * test,int line_no,const char * filename,long long value1,long long value2)164 int mbedtls_test_le_s( const char *test, int line_no, const char* filename,
165                        long long value1, long long value2 )
166 {
167     TEST_CF_PUBLIC( &value1, sizeof( value1 ) );
168     TEST_CF_PUBLIC( &value2, sizeof( value2 ) );
169 
170     if( value1 <= value2 )
171         return( 1 );
172 
173     if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
174     {
175         /* We've already recorded the test as having failed. Don't
176          * overwrite any previous information about the failure. */
177         return( 0 );
178     }
179     mbedtls_test_fail( test, line_no, filename );
180     (void) mbedtls_snprintf( mbedtls_test_info.line1,
181                              sizeof( mbedtls_test_info.line1 ),
182                              "lhs = 0x%016llx = %lld",
183                              (unsigned long long) value1, value1 );
184     (void) mbedtls_snprintf( mbedtls_test_info.line2,
185                              sizeof( mbedtls_test_info.line2 ),
186                              "rhs = 0x%016llx = %lld",
187                              (unsigned long long) value2, value2 );
188     return( 0 );
189 }
190 
mbedtls_test_unhexify(unsigned char * obuf,size_t obufmax,const char * ibuf,size_t * len)191 int mbedtls_test_unhexify( unsigned char *obuf,
192                            size_t obufmax,
193                            const char *ibuf,
194                            size_t *len )
195 {
196     unsigned char uc, uc2;
197 
198     *len = strlen( ibuf );
199 
200     /* Must be even number of bytes. */
201     if ( ( *len ) & 1 )
202         return( -1 );
203     *len /= 2;
204 
205     if ( (*len) > obufmax )
206         return( -1 );
207 
208     while( *ibuf != 0 )
209     {
210         if ( ascii2uc( *(ibuf++), &uc ) != 0 )
211             return( -1 );
212 
213         if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
214             return( -1 );
215 
216         *(obuf++) = ( uc << 4 ) | uc2;
217     }
218 
219     return( 0 );
220 }
221 
mbedtls_test_hexify(unsigned char * obuf,const unsigned char * ibuf,int len)222 void mbedtls_test_hexify( unsigned char *obuf,
223                           const unsigned char *ibuf,
224                           int len )
225 {
226     unsigned char l, h;
227 
228     while( len != 0 )
229     {
230         h = *ibuf / 16;
231         l = *ibuf % 16;
232 
233         if( h < 10 )
234             *obuf++ = '0' + h;
235         else
236             *obuf++ = 'a' + h - 10;
237 
238         if( l < 10 )
239             *obuf++ = '0' + l;
240         else
241             *obuf++ = 'a' + l - 10;
242 
243         ++ibuf;
244         len--;
245     }
246 }
247 
mbedtls_test_zero_alloc(size_t len)248 unsigned char *mbedtls_test_zero_alloc( size_t len )
249 {
250     void *p;
251     size_t actual_len = ( len != 0 ) ? len : 1;
252 
253     p = mbedtls_calloc( 1, actual_len );
254     TEST_HELPER_ASSERT( p != NULL );
255 
256     memset( p, 0x00, actual_len );
257 
258     return( p );
259 }
260 
mbedtls_test_unhexify_alloc(const char * ibuf,size_t * olen)261 unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
262 {
263     unsigned char *obuf;
264     size_t len;
265 
266     *olen = strlen( ibuf ) / 2;
267 
268     if( *olen == 0 )
269         return( mbedtls_test_zero_alloc( *olen ) );
270 
271     obuf = mbedtls_calloc( 1, *olen );
272     TEST_HELPER_ASSERT( obuf != NULL );
273     TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
274 
275     return( obuf );
276 }
277 
mbedtls_test_hexcmp(uint8_t * a,uint8_t * b,uint32_t a_len,uint32_t b_len)278 int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
279                          uint32_t a_len, uint32_t b_len )
280 {
281     int ret = 0;
282     uint32_t i = 0;
283 
284     if( a_len != b_len )
285         return( -1 );
286 
287     for( i = 0; i < a_len; i++ )
288     {
289         if( a[i] != b[i] )
290         {
291             ret = -1;
292             break;
293         }
294     }
295     return ret;
296 }
297 
298 #if defined(MBEDTLS_TEST_HOOKS)
mbedtls_test_err_add_check(int high,int low,const char * file,int line)299 void mbedtls_test_err_add_check( int high, int low,
300                                  const char *file, int line )
301 {
302     /* Error codes are always negative (a value of zero is a success) however
303      * their positive opposites can be easier to understand. The following
304      * examples given in comments have been made positive for ease of
305      * understanding. The structure of an error code is such:
306      *
307      *                                                shhhhhhhhlllllll
308      *
309      * s = sign bit.
310      * h = high level error code (includes high level module ID (bits 12..14)
311      *     and module-dependent error code (bits 7..11)).
312      * l = low level error code.
313      */
314     if ( high > -0x1000 && high != 0 )
315     /* high < 0001000000000000
316      * No high level module ID bits are set.
317      */
318     {
319         mbedtls_test_fail( "'high' is not a high-level error code",
320                             line, file );
321     }
322     else if ( high < -0x7F80 )
323     /* high > 0111111110000000
324      * Error code is greater than the largest allowed high level module ID.
325      */
326     {
327         mbedtls_test_fail( "'high' error code is greater than 15 bits",
328                             line, file );
329     }
330     else if ( ( high & 0x7F ) != 0 )
331     /* high & 0000000001111111
332      * Error code contains low level error code bits.
333      */
334     {
335         mbedtls_test_fail( "'high' contains a low-level error code",
336                             line, file );
337     }
338     else if ( low < -0x007F )
339     /* low >  0000000001111111
340      * Error code contains high or module level error code bits.
341      */
342     {
343         mbedtls_test_fail( "'low' error code is greater than 7 bits",
344                             line, file );
345     }
346     else if ( low > 0 )
347     {
348         mbedtls_test_fail( "'low' error code is greater than zero",
349                             line, file );
350     }
351 }
352 #endif /* MBEDTLS_TEST_HOOKS */
353 
354 #if defined(MBEDTLS_BIGNUM_C)
355 #include "bignum_core.h"
356 
mbedtls_test_read_mpi_core(mbedtls_mpi_uint ** pX,size_t * plimbs,const char * input)357 int mbedtls_test_read_mpi_core( mbedtls_mpi_uint **pX, size_t *plimbs,
358                                 const char *input )
359 {
360     /* Sanity check */
361     if( *pX != NULL )
362         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
363 
364     size_t hex_len = strlen( input );
365     size_t byte_len = ( hex_len + 1 ) / 2;
366     *plimbs = CHARS_TO_LIMBS( byte_len );
367 
368     /* A core bignum is not allowed to be empty. Forbid it as test data,
369      * this way static analyzers have a chance of knowing we don't expect
370      * the bignum functions to support empty inputs. */
371     if( *plimbs == 0 )
372         return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
373 
374     *pX = mbedtls_calloc( *plimbs, sizeof( **pX ) );
375     if( *pX == NULL )
376         return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
377 
378     unsigned char *byte_start = ( unsigned char * ) *pX;
379     if( byte_len % sizeof( mbedtls_mpi_uint ) != 0 )
380     {
381         byte_start += sizeof( mbedtls_mpi_uint ) - byte_len % sizeof( mbedtls_mpi_uint );
382     }
383     if( ( hex_len & 1 ) != 0 )
384     {
385         /* mbedtls_test_unhexify wants an even number of hex digits */
386         TEST_ASSERT( ascii2uc( *input, byte_start ) == 0 );
387         ++byte_start;
388         ++input;
389         --byte_len;
390     }
391     TEST_ASSERT( mbedtls_test_unhexify( byte_start,
392                                         byte_len,
393                                         input,
394                                         &byte_len ) == 0 );
395 
396     mbedtls_mpi_core_bigendian_to_host( *pX, *plimbs );
397     return( 0 );
398 
399 exit:
400     mbedtls_free( *pX );
401     return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA );
402 }
403 
mbedtls_test_read_mpi(mbedtls_mpi * X,const char * s)404 int mbedtls_test_read_mpi( mbedtls_mpi *X, const char *s )
405 {
406     int negative = 0;
407     /* Always set the sign bit to -1 if the input has a minus sign, even for 0.
408      * This creates an invalid representation, which mbedtls_mpi_read_string()
409      * avoids but we want to be able to create that in test data. */
410     if( s[0] == '-' )
411     {
412         ++s;
413         negative = 1;
414     }
415     /* mbedtls_mpi_read_string() currently retains leading zeros.
416      * It always allocates at least one limb for the value 0. */
417     if( s[0] == 0 )
418     {
419         mbedtls_mpi_free( X );
420         return( 0 );
421     }
422     int ret = mbedtls_mpi_read_string( X, 16, s );
423     if( ret != 0 )
424         return( ret );
425     if( negative )
426     {
427         if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
428             ++mbedtls_test_case_uses_negative_0;
429         X->s = -1;
430     }
431     return( 0 );
432 }
433 #endif
434