1 /*
2  *  Self-test demonstration program
3  *
4  *  Copyright The Mbed TLS Contributors
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 
20 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
21 
22 #include "mbedtls/build_info.h"
23 
24 #include "mbedtls/entropy.h"
25 #include "mbedtls/hmac_drbg.h"
26 #include "mbedtls/ctr_drbg.h"
27 #include "mbedtls/dhm.h"
28 #include "mbedtls/gcm.h"
29 #include "mbedtls/ccm.h"
30 #include "mbedtls/cmac.h"
31 #include "mbedtls/md5.h"
32 #include "mbedtls/ripemd160.h"
33 #include "mbedtls/sha1.h"
34 #include "mbedtls/sha256.h"
35 #include "mbedtls/sha512.h"
36 #include "mbedtls/des.h"
37 #include "mbedtls/aes.h"
38 #include "mbedtls/camellia.h"
39 #include "mbedtls/aria.h"
40 #include "mbedtls/chacha20.h"
41 #include "mbedtls/poly1305.h"
42 #include "mbedtls/chachapoly.h"
43 #include "mbedtls/base64.h"
44 #include "mbedtls/bignum.h"
45 #include "mbedtls/rsa.h"
46 #include "mbedtls/x509.h"
47 #include "mbedtls/pkcs5.h"
48 #include "mbedtls/ecp.h"
49 #include "mbedtls/ecjpake.h"
50 #include "mbedtls/timing.h"
51 #include "mbedtls/nist_kw.h"
52 #include "mbedtls/debug.h"
53 
54 #include <limits.h>
55 #include <string.h>
56 
57 #include "mbedtls/platform.h"
58 
59 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
60 #include "mbedtls/memory_buffer_alloc.h"
61 #endif
62 
63 
64 #if defined MBEDTLS_SELF_TEST
65 /* Sanity check for malloc. This is not expected to fail, and is rather
66  * intended to display potentially useful information about the platform,
67  * in particular the behavior of malloc(0). */
calloc_self_test(int verbose)68 static int calloc_self_test( int verbose )
69 {
70     int failures = 0;
71     void *empty1 = mbedtls_calloc( 0, 1 );
72     void *empty2 = mbedtls_calloc( 0, 1 );
73     void *buffer1 = mbedtls_calloc( 1, 1 );
74     void *buffer2 = mbedtls_calloc( 1, 1 );
75 
76     if( empty1 == NULL && empty2 == NULL )
77     {
78         if( verbose )
79             mbedtls_printf( "  CALLOC(0): passed (NULL)\n" );
80     }
81     else if( empty1 == NULL || empty2 == NULL )
82     {
83         if( verbose )
84             mbedtls_printf( "  CALLOC(0): failed (mix of NULL and non-NULL)\n" );
85         ++failures;
86     }
87     else if( empty1 == empty2 )
88     {
89         if( verbose )
90             mbedtls_printf( "  CALLOC(0): passed (same non-null)\n" );
91     }
92     else
93     {
94         if( verbose )
95             mbedtls_printf( "  CALLOC(0): passed (distinct non-null)\n" );
96     }
97 
98     if( buffer1 == NULL || buffer2 == NULL )
99     {
100         if( verbose )
101             mbedtls_printf( "  CALLOC(1): failed (NULL)\n" );
102         ++failures;
103     }
104     else if( buffer1 == buffer2 )
105     {
106         if( verbose )
107             mbedtls_printf( "  CALLOC(1): failed (same buffer twice)\n" );
108         ++failures;
109     }
110     else
111     {
112         if( verbose )
113             mbedtls_printf( "  CALLOC(1): passed\n" );
114     }
115 
116     mbedtls_free( buffer1 );
117     buffer1 = mbedtls_calloc( 1, 1 );
118     if( buffer1 == NULL )
119     {
120         if( verbose )
121             mbedtls_printf( "  CALLOC(1 again): failed (NULL)\n" );
122         ++failures;
123     }
124     else
125     {
126         if( verbose )
127             mbedtls_printf( "  CALLOC(1 again): passed\n" );
128     }
129 
130     if( verbose )
131         mbedtls_printf( "\n" );
132     mbedtls_free( empty1 );
133     mbedtls_free( empty2 );
134     mbedtls_free( buffer1 );
135     mbedtls_free( buffer2 );
136     return( failures );
137 }
138 #endif /* MBEDTLS_SELF_TEST */
139 
test_snprintf(size_t n,const char * ref_buf,int ref_ret)140 static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
141 {
142     int ret;
143     char buf[10] = "xxxxxxxxx";
144     const char ref[10] = "xxxxxxxxx";
145 
146     ret = mbedtls_snprintf( buf, n, "%s", "123" );
147     if( ret < 0 || (size_t) ret >= n )
148         ret = -1;
149 
150     if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
151         ref_ret != ret ||
152         memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
153     {
154         return( 1 );
155     }
156 
157     return( 0 );
158 }
159 
run_test_snprintf(void)160 static int run_test_snprintf( void )
161 {
162     return( test_snprintf( 0, "xxxxxxxxx",  -1 ) != 0 ||
163             test_snprintf( 1, "",           -1 ) != 0 ||
164             test_snprintf( 2, "1",          -1 ) != 0 ||
165             test_snprintf( 3, "12",         -1 ) != 0 ||
166             test_snprintf( 4, "123",         3 ) != 0 ||
167             test_snprintf( 5, "123",         3 ) != 0 );
168 }
169 
170 /*
171  * Check if a seed file is present, and if not create one for the entropy
172  * self-test. If this fails, we attempt the test anyway, so no error is passed
173  * back.
174  */
175 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
176 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
create_entropy_seed_file(void)177 static void create_entropy_seed_file( void )
178 {
179     int result;
180     size_t output_len = 0;
181     unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
182 
183     /* Attempt to read the entropy seed file. If this fails - attempt to write
184      * to the file to ensure one is present. */
185     result = mbedtls_platform_std_nv_seed_read( seed_value,
186                                                     MBEDTLS_ENTROPY_BLOCK_SIZE );
187     if( 0 == result )
188         return;
189 
190     result = mbedtls_platform_entropy_poll( NULL,
191                                             seed_value,
192                                             MBEDTLS_ENTROPY_BLOCK_SIZE,
193                                             &output_len );
194     if( 0 != result )
195         return;
196 
197     if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
198         return;
199 
200     mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
201 }
202 #endif
203 
mbedtls_entropy_self_test_wrapper(int verbose)204 int mbedtls_entropy_self_test_wrapper( int verbose )
205 {
206 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
207     create_entropy_seed_file( );
208 #endif
209     return( mbedtls_entropy_self_test( verbose ) );
210 }
211 #endif
212 
213 #if defined(MBEDTLS_SELF_TEST)
214 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)215 int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
216 {
217     if( verbose != 0 )
218     {
219 #if defined(MBEDTLS_MEMORY_DEBUG)
220         mbedtls_memory_buffer_alloc_status( );
221 #endif
222     }
223     mbedtls_memory_buffer_alloc_free( );
224     return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
225 }
226 #endif
227 
228 typedef struct
229 {
230     const char *name;
231     int ( *function )( int );
232 } selftest_t;
233 
234 const selftest_t selftests[] =
235 {
236     {"calloc", calloc_self_test},
237 #if defined(MBEDTLS_MD5_C)
238     {"md5", mbedtls_md5_self_test},
239 #endif
240 #if defined(MBEDTLS_RIPEMD160_C)
241     {"ripemd160", mbedtls_ripemd160_self_test},
242 #endif
243 #if defined(MBEDTLS_SHA1_C)
244     {"sha1", mbedtls_sha1_self_test},
245 #endif
246 #if defined(MBEDTLS_SHA256_C)
247     {"sha256", mbedtls_sha256_self_test},
248 #endif
249 #if defined(MBEDTLS_SHA512_C)
250     {"sha512", mbedtls_sha512_self_test},
251 #endif
252 #if defined(MBEDTLS_DES_C)
253     {"des", mbedtls_des_self_test},
254 #endif
255 #if defined(MBEDTLS_AES_C)
256     {"aes", mbedtls_aes_self_test},
257 #endif
258 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
259     {"gcm", mbedtls_gcm_self_test},
260 #endif
261 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
262     {"ccm", mbedtls_ccm_self_test},
263 #endif
264 #if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
265     {"nist_kw", mbedtls_nist_kw_self_test},
266 #endif
267 #if defined(MBEDTLS_CMAC_C)
268     {"cmac", mbedtls_cmac_self_test},
269 #endif
270 #if defined(MBEDTLS_CHACHA20_C)
271     {"chacha20", mbedtls_chacha20_self_test},
272 #endif
273 #if defined(MBEDTLS_POLY1305_C)
274     {"poly1305", mbedtls_poly1305_self_test},
275 #endif
276 #if defined(MBEDTLS_CHACHAPOLY_C)
277     {"chacha20-poly1305", mbedtls_chachapoly_self_test},
278 #endif
279 #if defined(MBEDTLS_BASE64_C)
280     {"base64", mbedtls_base64_self_test},
281 #endif
282 #if defined(MBEDTLS_BIGNUM_C)
283     {"mpi", mbedtls_mpi_self_test},
284 #endif
285 #if defined(MBEDTLS_RSA_C)
286     {"rsa", mbedtls_rsa_self_test},
287 #endif
288 #if defined(MBEDTLS_CAMELLIA_C)
289     {"camellia", mbedtls_camellia_self_test},
290 #endif
291 #if defined(MBEDTLS_ARIA_C)
292     {"aria", mbedtls_aria_self_test},
293 #endif
294 #if defined(MBEDTLS_CTR_DRBG_C)
295     {"ctr_drbg", mbedtls_ctr_drbg_self_test},
296 #endif
297 #if defined(MBEDTLS_HMAC_DRBG_C)
298     {"hmac_drbg", mbedtls_hmac_drbg_self_test},
299 #endif
300 #if defined(MBEDTLS_ECP_C)
301     {"ecp", mbedtls_ecp_self_test},
302 #endif
303 #if defined(MBEDTLS_ECJPAKE_C)
304     {"ecjpake", mbedtls_ecjpake_self_test},
305 #endif
306 #if defined(MBEDTLS_DHM_C)
307     {"dhm", mbedtls_dhm_self_test},
308 #endif
309 #if defined(MBEDTLS_ENTROPY_C)
310     {"entropy", mbedtls_entropy_self_test_wrapper},
311 #endif
312 #if defined(MBEDTLS_PKCS5_C)
313     {"pkcs5", mbedtls_pkcs5_self_test},
314 #endif
315 /* Heap test comes last */
316 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
317     {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
318 #endif
319     {NULL, NULL}
320 };
321 #endif /* MBEDTLS_SELF_TEST */
322 
main(int argc,char * argv[])323 int main( int argc, char *argv[] )
324 {
325 #if defined(MBEDTLS_SELF_TEST)
326     const selftest_t *test;
327 #endif /* MBEDTLS_SELF_TEST */
328     char **argp;
329     int v = 1; /* v=1 for verbose mode */
330     int exclude_mode = 0;
331     int suites_tested = 0, suites_failed = 0;
332 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
333     unsigned char buf[1000000];
334 #endif
335     void *pointer;
336 
337     /*
338      * The C standard doesn't guarantee that all-bits-0 is the representation
339      * of a NULL pointer. We do however use that in our code for initializing
340      * structures, which should work on every modern platform. Let's be sure.
341      */
342     memset( &pointer, 0, sizeof( void * ) );
343     if( pointer != NULL )
344     {
345         mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
346         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
347     }
348 
349     /*
350      * The C standard allows padding bits in the representation
351      * of standard integer types, but our code does currently not
352      * support them.
353      *
354      * Here we check that the underlying C implementation doesn't
355      * use padding bits, and fail cleanly if it does.
356      *
357      * The check works by casting the maximum value representable
358      * by a given integer type into the unpadded integer type of the
359      * same bit-width and checking that it agrees with the maximum value
360      * of that unpadded type. For example, for a 4-byte int,
361      * MAX_INT should be 0x7fffffff in int32_t. This assumes that
362      * CHAR_BIT == 8, which is checked in check_config.h.
363      *
364      * We assume that [u]intxx_t exist and that they don't
365      * have padding bits, as the standard requires.
366      */
367 
368 #define CHECK_PADDING_SIGNED(TYPE, NAME)                                \
369     do                                                                  \
370     {                                                                   \
371         if( sizeof( TYPE ) == 2 || sizeof( TYPE ) == 4 ||               \
372                 sizeof( TYPE ) == 8 ) {                                 \
373             if( ( sizeof( TYPE ) == 2 &&                                \
374                 (int16_t) NAME ## _MAX != 0x7FFF )             ||       \
375                 ( sizeof( TYPE ) == 4 &&                                \
376                 (int32_t) NAME ## _MAX != 0x7FFFFFFF )         ||       \
377                 ( sizeof( TYPE ) == 8 &&                                \
378                 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF ) )        \
379             {                                                           \
380                 mbedtls_printf( "Type '" #TYPE "' has padding bits\n" );\
381                 mbedtls_exit( MBEDTLS_EXIT_FAILURE );                   \
382             }                                                           \
383         } else {                                                        \
384             mbedtls_printf( "Padding checks only implemented for types of size 2, 4 or 8" \
385                 " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n",       \
386                 sizeof( TYPE ) );                                       \
387             mbedtls_exit( MBEDTLS_EXIT_FAILURE );                       \
388         }                                                               \
389     } while( 0 )
390 
391 #define CHECK_PADDING_UNSIGNED(TYPE, NAME)                              \
392     do                                                                  \
393     {                                                                   \
394         if( ( sizeof( TYPE ) == 2 &&                                    \
395               (uint16_t) NAME ## _MAX != 0xFFFF )             ||        \
396             ( sizeof( TYPE ) == 4 &&                                    \
397               (uint32_t) NAME ## _MAX != 0xFFFFFFFF )         ||        \
398             ( sizeof( TYPE ) == 8 &&                                    \
399               (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF ) )         \
400         {                                                               \
401             mbedtls_printf( "Type '" #TYPE "' has padding bits\n" );    \
402             mbedtls_exit( MBEDTLS_EXIT_FAILURE );                       \
403         }                                                               \
404     } while( 0 )
405 
406     CHECK_PADDING_SIGNED( short,        SHRT );
407     CHECK_PADDING_SIGNED( int,           INT );
408     CHECK_PADDING_SIGNED( long,         LONG );
409     CHECK_PADDING_SIGNED( long long,   LLONG );
410     CHECK_PADDING_SIGNED( ptrdiff_t, PTRDIFF );
411 
412     CHECK_PADDING_UNSIGNED( unsigned short,      USHRT );
413     CHECK_PADDING_UNSIGNED( unsigned,             UINT );
414     CHECK_PADDING_UNSIGNED( unsigned long,       ULONG );
415     CHECK_PADDING_UNSIGNED( unsigned long long, ULLONG );
416     CHECK_PADDING_UNSIGNED( size_t,               SIZE );
417 
418 #undef CHECK_PADDING_SIGNED
419 #undef CHECK_PADDING_UNSIGNED
420 
421     /*
422      * Make sure we have a snprintf that correctly zero-terminates
423      */
424     if( run_test_snprintf() != 0 )
425     {
426         mbedtls_printf( "the snprintf implementation is broken\n" );
427         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
428     }
429 
430     for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
431     {
432         if( strcmp( *argp, "--quiet" ) == 0 ||
433             strcmp( *argp, "-q" ) == 0 )
434         {
435             v = 0;
436         }
437         else if( strcmp( *argp, "--exclude" ) == 0 ||
438                  strcmp( *argp, "-x" ) == 0 )
439         {
440             exclude_mode = 1;
441         }
442         else
443             break;
444     }
445 
446     if( v != 0 )
447         mbedtls_printf( "\n" );
448 
449 #if defined(MBEDTLS_SELF_TEST)
450 
451 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
452     mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
453 #endif
454 
455     if( *argp != NULL && exclude_mode == 0 )
456     {
457         /* Run the specified tests */
458         for( ; *argp != NULL; argp++ )
459         {
460             for( test = selftests; test->name != NULL; test++ )
461             {
462                 if( !strcmp( *argp, test->name ) )
463                 {
464                     if( test->function( v )  != 0 )
465                     {
466                         suites_failed++;
467                     }
468                     suites_tested++;
469                     break;
470                 }
471             }
472             if( test->name == NULL )
473             {
474                 mbedtls_printf( "  Test suite %s not available -> failed\n\n", *argp );
475                 suites_failed++;
476             }
477         }
478     }
479     else
480     {
481         /* Run all the tests except excluded ones */
482         for( test = selftests; test->name != NULL; test++ )
483         {
484             if( exclude_mode )
485             {
486                 char **excluded;
487                 for( excluded = argp; *excluded != NULL; ++excluded )
488                 {
489                     if( !strcmp( *excluded, test->name ) )
490                         break;
491                 }
492                 if( *excluded )
493                 {
494                     if( v )
495                         mbedtls_printf( "  Skip: %s\n", test->name );
496                     continue;
497                 }
498             }
499             if( test->function( v )  != 0 )
500             {
501                 suites_failed++;
502             }
503             suites_tested++;
504         }
505     }
506 
507 #else
508     (void) exclude_mode;
509     mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
510 #endif
511 
512     if( v != 0 )
513     {
514         mbedtls_printf( "  Executed %d test suites\n\n", suites_tested );
515 
516         if( suites_failed > 0)
517         {
518             mbedtls_printf( "  [ %d tests FAIL ]\n\n", suites_failed );
519         }
520         else
521         {
522             mbedtls_printf( "  [ All tests PASS ]\n\n" );
523         }
524     }
525 
526     if( suites_failed > 0)
527         mbedtls_exit( MBEDTLS_EXIT_FAILURE );
528 
529     mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
530 }
531