1 /*
2  *  Self-test demonstration program
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #include "mbedtls/entropy.h"
29 #include "mbedtls/entropy_poll.h"
30 #include "mbedtls/hmac_drbg.h"
31 #include "mbedtls/ctr_drbg.h"
32 #include "mbedtls/dhm.h"
33 #include "mbedtls/gcm.h"
34 #include "mbedtls/ccm.h"
35 #include "mbedtls/cmac.h"
36 #include "mbedtls/md2.h"
37 #include "mbedtls/md4.h"
38 #include "mbedtls/md5.h"
39 #include "mbedtls/ripemd160.h"
40 #include "mbedtls/sha1.h"
41 #include "mbedtls/sha256.h"
42 #include "mbedtls/sha512.h"
43 #include "mbedtls/arc4.h"
44 #include "mbedtls/des.h"
45 #include "mbedtls/aes.h"
46 #include "mbedtls/camellia.h"
47 #include "mbedtls/aria.h"
48 #include "mbedtls/chacha20.h"
49 #include "mbedtls/poly1305.h"
50 #include "mbedtls/chachapoly.h"
51 #include "mbedtls/base64.h"
52 #include "mbedtls/bignum.h"
53 #include "mbedtls/rsa.h"
54 #include "mbedtls/x509.h"
55 #include "mbedtls/xtea.h"
56 #include "mbedtls/pkcs5.h"
57 #include "mbedtls/ecp.h"
58 #include "mbedtls/ecjpake.h"
59 #include "mbedtls/timing.h"
60 #include "mbedtls/nist_kw.h"
61 
62 #include <string.h>
63 
64 #if defined(MBEDTLS_PLATFORM_C)
65 #include "mbedtls/platform.h"
66 #else
67 #include <stdio.h>
68 #include <stdlib.h>
69 #define mbedtls_printf     printf
70 #define mbedtls_snprintf   snprintf
71 #define mbedtls_exit       exit
72 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
73 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
74 #endif
75 
76 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
77 #include "mbedtls/memory_buffer_alloc.h"
78 #endif
79 
80 #if defined(MBEDTLS_CHECK_PARAMS)
81 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)82 void mbedtls_param_failed( const char *failure_condition,
83                            const char *file,
84                            int line )
85 {
86     mbedtls_printf( "%s:%i: Input param failed - %s\n",
87                     file, line, failure_condition );
88     mbedtls_exit( MBEDTLS_EXIT_FAILURE );
89 }
90 #endif
91 
test_snprintf(size_t n,const char ref_buf[10],int ref_ret)92 static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
93 {
94     int ret;
95     char buf[10] = "xxxxxxxxx";
96     const char ref[10] = "xxxxxxxxx";
97 
98     ret = mbedtls_snprintf( buf, n, "%s", "123" );
99     if( ret < 0 || (size_t) ret >= n )
100         ret = -1;
101 
102     if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
103         ref_ret != ret ||
104         memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
105     {
106         return( 1 );
107     }
108 
109     return( 0 );
110 }
111 
run_test_snprintf(void)112 static int run_test_snprintf( void )
113 {
114     return( test_snprintf( 0, "xxxxxxxxx",  -1 ) != 0 ||
115             test_snprintf( 1, "",           -1 ) != 0 ||
116             test_snprintf( 2, "1",          -1 ) != 0 ||
117             test_snprintf( 3, "12",         -1 ) != 0 ||
118             test_snprintf( 4, "123",         3 ) != 0 ||
119             test_snprintf( 5, "123",         3 ) != 0 );
120 }
121 
122 /*
123  * Check if a seed file is present, and if not create one for the entropy
124  * self-test. If this fails, we attempt the test anyway, so no error is passed
125  * back.
126  */
127 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
128 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
create_entropy_seed_file(void)129 static void create_entropy_seed_file( void )
130 {
131     int result;
132     size_t output_len = 0;
133     unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
134 
135     /* Attempt to read the entropy seed file. If this fails - attempt to write
136      * to the file to ensure one is present. */
137     result = mbedtls_platform_std_nv_seed_read( seed_value,
138                                                     MBEDTLS_ENTROPY_BLOCK_SIZE );
139     if( 0 == result )
140         return;
141 
142     result = mbedtls_platform_entropy_poll( NULL,
143                                             seed_value,
144                                             MBEDTLS_ENTROPY_BLOCK_SIZE,
145                                             &output_len );
146     if( 0 != result )
147         return;
148 
149     if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
150         return;
151 
152     mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
153 }
154 #endif
155 
mbedtls_entropy_self_test_wrapper(int verbose)156 int mbedtls_entropy_self_test_wrapper( int verbose )
157 {
158 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
159     create_entropy_seed_file( );
160 #endif
161     return( mbedtls_entropy_self_test( verbose ) );
162 }
163 #endif
164 
165 #if defined(MBEDTLS_SELF_TEST)
166 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)167 int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
168 {
169     if( verbose != 0 )
170     {
171 #if defined(MBEDTLS_MEMORY_DEBUG)
172         mbedtls_memory_buffer_alloc_status( );
173 #endif
174     }
175     mbedtls_memory_buffer_alloc_free( );
176     return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
177 }
178 #endif
179 
180 typedef struct
181 {
182     const char *name;
183     int ( *function )( int );
184 } selftest_t;
185 
186 const selftest_t selftests[] =
187 {
188 #if defined(MBEDTLS_MD2_C)
189     {"md2", mbedtls_md2_self_test},
190 #endif
191 #if defined(MBEDTLS_MD4_C)
192     {"md4", mbedtls_md4_self_test},
193 #endif
194 #if defined(MBEDTLS_MD5_C)
195     {"md5", mbedtls_md5_self_test},
196 #endif
197 #if defined(MBEDTLS_RIPEMD160_C)
198     {"ripemd160", mbedtls_ripemd160_self_test},
199 #endif
200 #if defined(MBEDTLS_SHA1_C)
201     {"sha1", mbedtls_sha1_self_test},
202 #endif
203 #if defined(MBEDTLS_SHA256_C)
204     {"sha256", mbedtls_sha256_self_test},
205 #endif
206 #if defined(MBEDTLS_SHA512_C)
207     {"sha512", mbedtls_sha512_self_test},
208 #endif
209 #if defined(MBEDTLS_ARC4_C)
210     {"arc4", mbedtls_arc4_self_test},
211 #endif
212 #if defined(MBEDTLS_DES_C)
213     {"des", mbedtls_des_self_test},
214 #endif
215 #if defined(MBEDTLS_AES_C)
216     {"aes", mbedtls_aes_self_test},
217 #endif
218 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
219     {"gcm", mbedtls_gcm_self_test},
220 #endif
221 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
222     {"ccm", mbedtls_ccm_self_test},
223 #endif
224 #if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
225     {"nist_kw", mbedtls_nist_kw_self_test},
226 #endif
227 #if defined(MBEDTLS_CMAC_C)
228     {"cmac", mbedtls_cmac_self_test},
229 #endif
230 #if defined(MBEDTLS_CHACHA20_C)
231     {"chacha20", mbedtls_chacha20_self_test},
232 #endif
233 #if defined(MBEDTLS_POLY1305_C)
234     {"poly1305", mbedtls_poly1305_self_test},
235 #endif
236 #if defined(MBEDTLS_CHACHAPOLY_C)
237     {"chacha20-poly1305", mbedtls_chachapoly_self_test},
238 #endif
239 #if defined(MBEDTLS_BASE64_C)
240     {"base64", mbedtls_base64_self_test},
241 #endif
242 #if defined(MBEDTLS_BIGNUM_C)
243     {"mpi", mbedtls_mpi_self_test},
244 #endif
245 #if defined(MBEDTLS_RSA_C)
246     {"rsa", mbedtls_rsa_self_test},
247 #endif
248 #if defined(MBEDTLS_X509_USE_C)
249     {"x509", mbedtls_x509_self_test},
250 #endif
251 #if defined(MBEDTLS_XTEA_C)
252     {"xtea", mbedtls_xtea_self_test},
253 #endif
254 #if defined(MBEDTLS_CAMELLIA_C)
255     {"camellia", mbedtls_camellia_self_test},
256 #endif
257 #if defined(MBEDTLS_ARIA_C)
258     {"aria", mbedtls_aria_self_test},
259 #endif
260 #if defined(MBEDTLS_CTR_DRBG_C)
261     {"ctr_drbg", mbedtls_ctr_drbg_self_test},
262 #endif
263 #if defined(MBEDTLS_HMAC_DRBG_C)
264     {"hmac_drbg", mbedtls_hmac_drbg_self_test},
265 #endif
266 #if defined(MBEDTLS_ECP_C)
267     {"ecp", mbedtls_ecp_self_test},
268 #endif
269 #if defined(MBEDTLS_ECJPAKE_C)
270     {"ecjpake", mbedtls_ecjpake_self_test},
271 #endif
272 #if defined(MBEDTLS_DHM_C)
273     {"dhm", mbedtls_dhm_self_test},
274 #endif
275 #if defined(MBEDTLS_ENTROPY_C)
276     {"entropy", mbedtls_entropy_self_test_wrapper},
277 #endif
278 #if defined(MBEDTLS_PKCS5_C)
279     {"pkcs5", mbedtls_pkcs5_self_test},
280 #endif
281 /* Slower test after the faster ones */
282 #if defined(MBEDTLS_TIMING_C)
283     /* Modify for AliOS Things begin. 2019-01-09 */
284     /* {"timing", mbedtls_timing_self_test}, */
285     /* Modify for AliOS Things end. 2019-01-09 */
286 #endif
287 /* Heap test comes last */
288 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
289     {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
290 #endif
291     {NULL, NULL}
292 };
293 #endif /* MBEDTLS_SELF_TEST */
294 
295 /* Modify for AliOS Things begin. 2019-01-09 */
296 /* int main( int argc, char *argv[] ) */
mbedtls_selftest_main(int argc,char * argv[])297 int mbedtls_selftest_main( int argc, char *argv[] )
298 /* Modify for AliOS Things end. 2019-01-09 */
299 {
300 #if defined(MBEDTLS_SELF_TEST)
301     const selftest_t *test;
302 #endif /* MBEDTLS_SELF_TEST */
303     char **argp;
304     int v = 1; /* v=1 for verbose mode */
305     int exclude_mode = 0;
306     int suites_tested = 0, suites_failed = 0;
307 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
308     unsigned char buf[1000000];
309 #endif
310     void *pointer;
311 
312     /*
313      * The C standard doesn't guarantee that all-bits-0 is the representation
314      * of a NULL pointer. We do however use that in our code for initializing
315      * structures, which should work on every modern platform. Let's be sure.
316      */
317     memset( &pointer, 0, sizeof( void * ) );
318     if( pointer != NULL )
319     {
320         mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
321         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
322     }
323 
324     /*
325      * Make sure we have a snprintf that correctly zero-terminates
326      */
327     if( run_test_snprintf() != 0 )
328     {
329         mbedtls_printf( "the snprintf implementation is broken\n" );
330         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
331     }
332 
333     for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
334     {
335         if( strcmp( *argp, "--quiet" ) == 0 ||
336             strcmp( *argp, "-q" ) == 0 )
337         {
338             v = 0;
339         }
340         else if( strcmp( *argp, "--exclude" ) == 0 ||
341                  strcmp( *argp, "-x" ) == 0 )
342         {
343             exclude_mode = 1;
344         }
345         else
346             break;
347     }
348 
349     if( v != 0 )
350         mbedtls_printf( "\n" );
351 
352 #if defined(MBEDTLS_SELF_TEST)
353 
354 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
355     mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
356 #endif
357 
358     if( *argp != NULL && exclude_mode == 0 )
359     {
360         /* Run the specified tests */
361         for( ; *argp != NULL; argp++ )
362         {
363             for( test = selftests; test->name != NULL; test++ )
364             {
365                 if( !strcmp( *argp, test->name ) )
366                 {
367                     if( test->function( v )  != 0 )
368                     {
369                         suites_failed++;
370                     }
371                     suites_tested++;
372                     break;
373                 }
374             }
375             if( test->name == NULL )
376             {
377                 mbedtls_printf( "  Test suite %s not available -> failed\n\n", *argp );
378                 suites_failed++;
379             }
380         }
381     }
382     else
383     {
384         /* Run all the tests except excluded ones */
385         for( test = selftests; test->name != NULL; test++ )
386         {
387             if( exclude_mode )
388             {
389                 char **excluded;
390                 for( excluded = argp; *excluded != NULL; ++excluded )
391                 {
392                     if( !strcmp( *excluded, test->name ) )
393                         break;
394                 }
395                 if( *excluded )
396                 {
397                     if( v )
398                         mbedtls_printf( "  Skip: %s\n", test->name );
399                     continue;
400                 }
401             }
402             if( test->function( v )  != 0 )
403             {
404                 suites_failed++;
405             }
406             suites_tested++;
407         }
408     }
409 
410 #else
411     (void) exclude_mode;
412     mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
413 #endif
414 
415     if( v != 0 )
416     {
417         mbedtls_printf( "  Executed %d test suites\n\n", suites_tested );
418 
419         if( suites_failed > 0)
420         {
421             mbedtls_printf( "  [ %d tests FAIL ]\n\n", suites_failed );
422         }
423         else
424         {
425             mbedtls_printf( "  [ All tests PASS ]\n\n" );
426         }
427 #if defined(_WIN32)
428         mbedtls_printf( "  Press Enter to exit this program.\n" );
429         fflush( stdout ); getchar();
430 #endif
431     }
432 
433     if( suites_failed > 0)
434         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
435 
436     /* return() is here to prevent compiler warnings */
437     return( MBEDTLS_EXIT_SUCCESS );
438 }
439 
440