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/helpers.h>
19 #include <test/macros.h>
20 #include <string.h>
21 
22 /*----------------------------------------------------------------------------*/
23 /* Static global variables */
24 
25 #if defined(MBEDTLS_PLATFORM_C)
26 static mbedtls_platform_context platform_ctx;
27 #endif
28 
29 mbedtls_test_info_t mbedtls_test_info;
30 
31 /*----------------------------------------------------------------------------*/
32 /* Helper Functions */
33 
mbedtls_test_platform_setup(void)34 int mbedtls_test_platform_setup( void )
35 {
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 
mbedtls_test_platform_teardown(void)43 void mbedtls_test_platform_teardown( void )
44 {
45 #if defined(MBEDTLS_PLATFORM_C)
46     mbedtls_platform_teardown( &platform_ctx );
47 #endif /* MBEDTLS_PLATFORM_C */
48 }
49 
ascii2uc(const char c,unsigned char * uc)50 static int ascii2uc(const char c, unsigned char *uc)
51 {
52     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 
mbedtls_test_fail(const char * test,int line_no,const char * filename)64 void mbedtls_test_fail( const char *test, int line_no, const char* filename )
65 {
66     if( mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED )
67     {
68         /* We've already recorded the test as having failed. Don't
69          * overwrite any previous information about the failure. */
70         return;
71     }
72     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;
76 }
77 
mbedtls_test_skip(const char * test,int line_no,const char * filename)78 void mbedtls_test_skip( const char *test, int line_no, const char* filename )
79 {
80     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;
84 }
85 
mbedtls_test_set_step(unsigned long step)86 void mbedtls_test_set_step( unsigned long step )
87 {
88     mbedtls_test_info.step = step;
89 }
90 
mbedtls_test_info_reset(void)91 void 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     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 
mbedtls_test_equal(const char * test,int line_no,const char * filename,unsigned long long value1,unsigned long long value2)102 int 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 );
123 }
124 
mbedtls_test_unhexify(unsigned char * obuf,size_t obufmax,const char * ibuf,size_t * len)125 int 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 );
141 
142     while( *ibuf != 0 )
143     {
144         if ( ascii2uc( *(ibuf++), &uc ) != 0 )
145             return( -1 );
146 
147         if ( ascii2uc( *(ibuf++), &uc2 ) != 0 )
148             return( -1 );
149 
150         *(obuf++) = ( uc << 4 ) | uc2;
151     }
152 
153     return( 0 );
154 }
155 
mbedtls_test_hexify(unsigned char * obuf,const unsigned char * ibuf,int len)156 void mbedtls_test_hexify( unsigned char *obuf,
157                           const unsigned char *ibuf,
158                           int len )
159 {
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 
mbedtls_test_zero_alloc(size_t len)182 unsigned char *mbedtls_test_zero_alloc( size_t len )
183 {
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 
mbedtls_test_unhexify_alloc(const char * ibuf,size_t * olen)195 unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
196 {
197     unsigned char *obuf;
198     size_t len;
199 
200     *olen = strlen( ibuf ) / 2;
201 
202     if( *olen == 0 )
203         return( mbedtls_test_zero_alloc( *olen ) );
204 
205     obuf = mbedtls_calloc( 1, *olen );
206     TEST_HELPER_ASSERT( obuf != NULL );
207     TEST_HELPER_ASSERT( mbedtls_test_unhexify( obuf, *olen, ibuf, &len ) == 0 );
208 
209     return( obuf );
210 }
211 
mbedtls_test_hexcmp(uint8_t * a,uint8_t * b,uint32_t a_len,uint32_t b_len)212 int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
213                          uint32_t a_len, uint32_t b_len )
214 {
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 }
231 
232 #if defined(MBEDTLS_TEST_HOOKS)
mbedtls_test_err_add_check(int high,int low,const char * file,int line)233 void mbedtls_test_err_add_check( int high, int low,
234                                  const char *file, int line )
235 {
236     /* 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      *
241      *                                                shhhhhhhhlllllll
242      *
243      * s = sign bit.
244      * h = high level error code (includes high level module ID (bits 12..14)
245      *     and module-dependent error code (bits 7..11)).
246      * l = low level error code.
247      */
248     if ( high > -0x1000 && high != 0 )
249     /* high < 0001000000000000
250      * No high level module ID bits are set.
251      */
252     {
253         mbedtls_test_fail( "'high' is not a high-level error code",
254                             line, file );
255     }
256     else if ( high < -0x7F80 )
257     /* high > 0111111110000000
258      * Error code is greater than the largest allowed high level module ID.
259      */
260     {
261         mbedtls_test_fail( "'high' error code is greater than 15 bits",
262                             line, file );
263     }
264     else if ( ( high & 0x7F ) != 0 )
265     /* high & 0000000001111111
266      * Error code contains low level error code bits.
267      */
268     {
269         mbedtls_test_fail( "'high' contains a low-level error code",
270                             line, file );
271     }
272     else if ( low < -0x007F )
273     /* low >  0000000001111111
274      * Error code contains high or module level error code bits.
275      */
276     {
277         mbedtls_test_fail( "'low' error code is greater than 7 bits",
278                             line, file );
279     }
280     else if ( low > 0 )
281     {
282         mbedtls_test_fail( "'low' error code is greater than zero",
283                             line, file );
284     }
285 }
286 #endif /* MBEDTLS_TEST_HOOKS */
287 
288 #if defined(MBEDTLS_BIGNUM_C)
mbedtls_test_read_mpi(mbedtls_mpi * X,int radix,const char * s)289 int 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
302