1 /*
2 * Self-test demonstration program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #include "mbedtls/entropy.h"
54 #include "mbedtls/entropy_poll.h"
55 #include "mbedtls/hmac_drbg.h"
56 #include "mbedtls/ctr_drbg.h"
57 #include "mbedtls/dhm.h"
58 #include "mbedtls/gcm.h"
59 #include "mbedtls/ccm.h"
60 #include "mbedtls/cmac.h"
61 #include "mbedtls/md2.h"
62 #include "mbedtls/md4.h"
63 #include "mbedtls/md5.h"
64 #include "mbedtls/ripemd160.h"
65 #include "mbedtls/sha1.h"
66 #include "mbedtls/sha256.h"
67 #include "mbedtls/sha512.h"
68 #include "mbedtls/arc4.h"
69 #include "mbedtls/des.h"
70 #include "mbedtls/aes.h"
71 #include "mbedtls/camellia.h"
72 #include "mbedtls/aria.h"
73 #include "mbedtls/chacha20.h"
74 #include "mbedtls/poly1305.h"
75 #include "mbedtls/chachapoly.h"
76 #include "mbedtls/base64.h"
77 #include "mbedtls/bignum.h"
78 #include "mbedtls/rsa.h"
79 #include "mbedtls/x509.h"
80 #include "mbedtls/xtea.h"
81 #include "mbedtls/pkcs5.h"
82 #include "mbedtls/ecp.h"
83 #include "mbedtls/ecjpake.h"
84 #include "mbedtls/timing.h"
85 #include "mbedtls/nist_kw.h"
86
87 #include <string.h>
88
89 #if defined(MBEDTLS_PLATFORM_C)
90 #include "mbedtls/platform.h"
91 #else
92 #include <stdio.h>
93 #include <stdlib.h>
94 #define mbedtls_calloc calloc
95 #define mbedtls_free free
96 #define mbedtls_printf printf
97 #define mbedtls_snprintf snprintf
98 #define mbedtls_exit exit
99 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
100 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
101 #endif
102
103 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
104 #include "mbedtls/memory_buffer_alloc.h"
105 #endif
106
107
108 #if defined MBEDTLS_SELF_TEST
109 /* Sanity check for malloc. This is not expected to fail, and is rather
110 * intended to display potentially useful information about the platform,
111 * in particular the behavior of malloc(0). */
calloc_self_test(int verbose)112 static int calloc_self_test( int verbose )
113 {
114 int failures = 0;
115 void *empty1 = mbedtls_calloc( 0, 1 );
116 void *empty2 = mbedtls_calloc( 0, 1 );
117 void *buffer1 = mbedtls_calloc( 1, 1 );
118 void *buffer2 = mbedtls_calloc( 1, 1 );
119 uintptr_t old_buffer1;
120
121 if( empty1 == NULL && empty2 == NULL )
122 {
123 if( verbose )
124 mbedtls_printf( " CALLOC(0): passed (NULL)\n" );
125 }
126 else if( empty1 == NULL || empty2 == NULL )
127 {
128 if( verbose )
129 mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" );
130 ++failures;
131 }
132 else if( empty1 == empty2 )
133 {
134 if( verbose )
135 mbedtls_printf( " CALLOC(0): passed (same non-null)\n" );
136 }
137 else
138 {
139 if( verbose )
140 mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" );
141 }
142
143 if( buffer1 == NULL || buffer2 == NULL )
144 {
145 if( verbose )
146 mbedtls_printf( " CALLOC(1): failed (NULL)\n" );
147 ++failures;
148 }
149 else if( buffer1 == buffer2 )
150 {
151 if( verbose )
152 mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" );
153 ++failures;
154 }
155 else
156 {
157 if( verbose )
158 mbedtls_printf( " CALLOC(1): passed\n" );
159 }
160
161 old_buffer1 = (uintptr_t) buffer1;
162 mbedtls_free( buffer1 );
163 buffer1 = mbedtls_calloc( 1, 1 );
164 if( buffer1 == NULL )
165 {
166 if( verbose )
167 mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" );
168 ++failures;
169 }
170 else
171 {
172 if( verbose )
173 mbedtls_printf( " CALLOC(1 again): passed (%s address)\n",
174 (uintptr_t) old_buffer1 == (uintptr_t) buffer1 ?
175 "same" : "different" );
176 }
177
178 if( verbose )
179 mbedtls_printf( "\n" );
180 mbedtls_free( empty1 );
181 mbedtls_free( empty2 );
182 mbedtls_free( buffer1 );
183 mbedtls_free( buffer2 );
184 return( failures );
185 }
186 #endif /* MBEDTLS_SELF_TEST */
187
test_snprintf(size_t n,const char * ref_buf,int ref_ret)188 static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
189 {
190 int ret;
191 char buf[10] = "xxxxxxxxx";
192 const char ref[10] = "xxxxxxxxx";
193
194 ret = mbedtls_snprintf( buf, n, "%s", "123" );
195 if( ret < 0 || (size_t) ret >= n )
196 ret = -1;
197
198 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
199 ref_ret != ret ||
200 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
201 {
202 return( 1 );
203 }
204
205 return( 0 );
206 }
207
run_test_snprintf(void)208 static int run_test_snprintf( void )
209 {
210 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
211 test_snprintf( 1, "", -1 ) != 0 ||
212 test_snprintf( 2, "1", -1 ) != 0 ||
213 test_snprintf( 3, "12", -1 ) != 0 ||
214 test_snprintf( 4, "123", 3 ) != 0 ||
215 test_snprintf( 5, "123", 3 ) != 0 );
216 }
217
218 /*
219 * Check if a seed file is present, and if not create one for the entropy
220 * self-test. If this fails, we attempt the test anyway, so no error is passed
221 * back.
222 */
223 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
224 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
create_entropy_seed_file(void)225 static void create_entropy_seed_file( void )
226 {
227 int result;
228 size_t output_len = 0;
229 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
230
231 /* Attempt to read the entropy seed file. If this fails - attempt to write
232 * to the file to ensure one is present. */
233 result = mbedtls_platform_std_nv_seed_read( seed_value,
234 MBEDTLS_ENTROPY_BLOCK_SIZE );
235 if( 0 == result )
236 return;
237
238 result = mbedtls_platform_entropy_poll( NULL,
239 seed_value,
240 MBEDTLS_ENTROPY_BLOCK_SIZE,
241 &output_len );
242 if( 0 != result )
243 return;
244
245 if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
246 return;
247
248 mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
249 }
250 #endif
251
mbedtls_entropy_self_test_wrapper(int verbose)252 int mbedtls_entropy_self_test_wrapper( int verbose )
253 {
254 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
255 create_entropy_seed_file( );
256 #endif
257 return( mbedtls_entropy_self_test( verbose ) );
258 }
259 #endif
260
261 #if defined(MBEDTLS_SELF_TEST)
262 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)263 int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
264 {
265 if( verbose != 0 )
266 {
267 #if defined(MBEDTLS_MEMORY_DEBUG)
268 mbedtls_memory_buffer_alloc_status( );
269 #endif
270 }
271 mbedtls_memory_buffer_alloc_free( );
272 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
273 }
274 #endif
275
276 typedef struct
277 {
278 const char *name;
279 int ( *function )( int );
280 } selftest_t;
281
282 const selftest_t selftests[] =
283 {
284 {"calloc", calloc_self_test},
285 #if defined(MBEDTLS_MD2_C)
286 {"md2", mbedtls_md2_self_test},
287 #endif
288 #if defined(MBEDTLS_MD4_C)
289 {"md4", mbedtls_md4_self_test},
290 #endif
291 #if defined(MBEDTLS_MD5_C)
292 {"md5", mbedtls_md5_self_test},
293 #endif
294 #if defined(MBEDTLS_RIPEMD160_C)
295 {"ripemd160", mbedtls_ripemd160_self_test},
296 #endif
297 #if defined(MBEDTLS_SHA1_C)
298 {"sha1", mbedtls_sha1_self_test},
299 #endif
300 #if defined(MBEDTLS_SHA256_C)
301 {"sha256", mbedtls_sha256_self_test},
302 #endif
303 #if defined(MBEDTLS_SHA512_C)
304 {"sha512", mbedtls_sha512_self_test},
305 #endif
306 #if defined(MBEDTLS_ARC4_C)
307 {"arc4", mbedtls_arc4_self_test},
308 #endif
309 #if defined(MBEDTLS_DES_C)
310 {"des", mbedtls_des_self_test},
311 #endif
312 #if defined(MBEDTLS_AES_C)
313 {"aes", mbedtls_aes_self_test},
314 #endif
315 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
316 {"gcm", mbedtls_gcm_self_test},
317 #endif
318 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
319 {"ccm", mbedtls_ccm_self_test},
320 #endif
321 #if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
322 {"nist_kw", mbedtls_nist_kw_self_test},
323 #endif
324 #if defined(MBEDTLS_CMAC_C)
325 {"cmac", mbedtls_cmac_self_test},
326 #endif
327 #if defined(MBEDTLS_CHACHA20_C)
328 {"chacha20", mbedtls_chacha20_self_test},
329 #endif
330 #if defined(MBEDTLS_POLY1305_C)
331 {"poly1305", mbedtls_poly1305_self_test},
332 #endif
333 #if defined(MBEDTLS_CHACHAPOLY_C)
334 {"chacha20-poly1305", mbedtls_chachapoly_self_test},
335 #endif
336 #if defined(MBEDTLS_BASE64_C)
337 {"base64", mbedtls_base64_self_test},
338 #endif
339 #if defined(MBEDTLS_BIGNUM_C)
340 {"mpi", mbedtls_mpi_self_test},
341 #endif
342 #if defined(MBEDTLS_RSA_C)
343 {"rsa", mbedtls_rsa_self_test},
344 #endif
345 #if defined(MBEDTLS_X509_USE_C)
346 {"x509", mbedtls_x509_self_test},
347 #endif
348 #if defined(MBEDTLS_XTEA_C)
349 {"xtea", mbedtls_xtea_self_test},
350 #endif
351 #if defined(MBEDTLS_CAMELLIA_C)
352 {"camellia", mbedtls_camellia_self_test},
353 #endif
354 #if defined(MBEDTLS_ARIA_C)
355 {"aria", mbedtls_aria_self_test},
356 #endif
357 #if defined(MBEDTLS_CTR_DRBG_C)
358 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
359 #endif
360 #if defined(MBEDTLS_HMAC_DRBG_C)
361 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
362 #endif
363 #if defined(MBEDTLS_ECP_C)
364 {"ecp", mbedtls_ecp_self_test},
365 #endif
366 #if defined(MBEDTLS_ECJPAKE_C)
367 {"ecjpake", mbedtls_ecjpake_self_test},
368 #endif
369 #if defined(MBEDTLS_DHM_C)
370 {"dhm", mbedtls_dhm_self_test},
371 #endif
372 #if defined(MBEDTLS_ENTROPY_C)
373 {"entropy", mbedtls_entropy_self_test_wrapper},
374 #endif
375 #if defined(MBEDTLS_PKCS5_C)
376 {"pkcs5", mbedtls_pkcs5_self_test},
377 #endif
378 /* Slower test after the faster ones */
379 #if defined(MBEDTLS_TIMING_C)
380 {"timing", mbedtls_timing_self_test},
381 #endif
382 /* Heap test comes last */
383 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
384 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
385 #endif
386 {NULL, NULL}
387 };
388 #endif /* MBEDTLS_SELF_TEST */
389
main(int argc,char * argv[])390 int main( int argc, char *argv[] )
391 {
392 #if defined(MBEDTLS_SELF_TEST)
393 const selftest_t *test;
394 #endif /* MBEDTLS_SELF_TEST */
395 char **argp;
396 int v = 1; /* v=1 for verbose mode */
397 int exclude_mode = 0;
398 int suites_tested = 0, suites_failed = 0;
399 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
400 unsigned char buf[1000000];
401 #endif
402 void *pointer;
403
404 /*
405 * The C standard doesn't guarantee that all-bits-0 is the representation
406 * of a NULL pointer. We do however use that in our code for initializing
407 * structures, which should work on every modern platform. Let's be sure.
408 */
409 memset( &pointer, 0, sizeof( void * ) );
410 if( pointer != NULL )
411 {
412 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
413 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
414 }
415
416 /*
417 * Make sure we have a snprintf that correctly zero-terminates
418 */
419 if( run_test_snprintf() != 0 )
420 {
421 mbedtls_printf( "the snprintf implementation is broken\n" );
422 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
423 }
424
425 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
426 {
427 if( strcmp( *argp, "--quiet" ) == 0 ||
428 strcmp( *argp, "-q" ) == 0 )
429 {
430 v = 0;
431 }
432 else if( strcmp( *argp, "--exclude" ) == 0 ||
433 strcmp( *argp, "-x" ) == 0 )
434 {
435 exclude_mode = 1;
436 }
437 else
438 break;
439 }
440
441 if( v != 0 )
442 mbedtls_printf( "\n" );
443
444 #if defined(MBEDTLS_SELF_TEST)
445
446 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
447 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
448 #endif
449
450 if( *argp != NULL && exclude_mode == 0 )
451 {
452 /* Run the specified tests */
453 for( ; *argp != NULL; argp++ )
454 {
455 for( test = selftests; test->name != NULL; test++ )
456 {
457 if( !strcmp( *argp, test->name ) )
458 {
459 if( test->function( v ) != 0 )
460 {
461 suites_failed++;
462 }
463 suites_tested++;
464 break;
465 }
466 }
467 if( test->name == NULL )
468 {
469 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
470 suites_failed++;
471 }
472 }
473 }
474 else
475 {
476 /* Run all the tests except excluded ones */
477 for( test = selftests; test->name != NULL; test++ )
478 {
479 if( exclude_mode )
480 {
481 char **excluded;
482 for( excluded = argp; *excluded != NULL; ++excluded )
483 {
484 if( !strcmp( *excluded, test->name ) )
485 break;
486 }
487 if( *excluded )
488 {
489 if( v )
490 mbedtls_printf( " Skip: %s\n", test->name );
491 continue;
492 }
493 }
494 if( test->function( v ) != 0 )
495 {
496 suites_failed++;
497 }
498 suites_tested++;
499 }
500 }
501
502 #else
503 (void) exclude_mode;
504 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
505 #endif
506
507 if( v != 0 )
508 {
509 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
510
511 if( suites_failed > 0)
512 {
513 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
514 }
515 else
516 {
517 mbedtls_printf( " [ All tests PASS ]\n\n" );
518 }
519 #if defined(_WIN32)
520 mbedtls_printf( " Press Enter to exit this program.\n" );
521 fflush( stdout ); getchar();
522 #endif
523 }
524
525 if( suites_failed > 0)
526 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
527
528 mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
529 }
530