blob: df5634de5c2403cb71035fd30cb180ac33d6ea1a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Self-test demonstration program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/entropy.h"
Simon Butcherb6a73c92016-06-18 22:45:37 +010029#include "mbedtls/entropy_poll.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#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"
Robert Cragiedc5c7b92015-12-11 15:49:45 +000035#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#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"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000047#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030048#include "mbedtls/chacha20.h"
49#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020050#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#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"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020058#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Rich Evans18b78c72015-02-11 14:06:19 +000061#include <string.h>
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000065#else
66#include <stdio.h>
Janos Follath2e3aca22016-03-18 16:25:52 +000067#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#define mbedtls_printf printf
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020069#define mbedtls_snprintf snprintf
Janos Follath2e3aca22016-03-18 16:25:52 +000070#define mbedtls_exit exit
71#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
72#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000073#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020077#endif
78
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020079static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
80{
81 int ret;
82 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020083 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020084
85 ret = mbedtls_snprintf( buf, n, "%s", "123" );
86 if( ret < 0 || (size_t) ret >= n )
87 ret = -1;
88
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020089 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
90 ref_ret != ret ||
91 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020092 {
93 return( 1 );
94 }
95
96 return( 0 );
97}
98
99static int run_test_snprintf( void )
100{
101 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200102 test_snprintf( 1, "", -1 ) != 0 ||
103 test_snprintf( 2, "1", -1 ) != 0 ||
104 test_snprintf( 3, "12", -1 ) != 0 ||
105 test_snprintf( 4, "123", 3 ) != 0 ||
106 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200107}
108
Simon Butcherb6a73c92016-06-18 22:45:37 +0100109/*
110 * Check if a seed file is present, and if not create one for the entropy
111 * self-test. If this fails, we attempt the test anyway, so no error is passed
112 * back.
113 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100114#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
115#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100116static void create_entropy_seed_file( void )
117{
118 int result;
119 size_t output_len = 0;
120 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
121
122 /* Attempt to read the entropy seed file. If this fails - attempt to write
123 * to the file to ensure one is present. */
124 result = mbedtls_platform_std_nv_seed_read( seed_value,
125 MBEDTLS_ENTROPY_BLOCK_SIZE );
126 if( 0 == result )
127 return;
128
129 result = mbedtls_platform_entropy_poll( NULL,
130 seed_value,
131 MBEDTLS_ENTROPY_BLOCK_SIZE,
132 &output_len );
133 if( 0 != result )
134 return;
135
136 if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
137 return;
138
139 mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
140}
141#endif
142
Gilles Peskine319ac802017-12-15 14:57:18 +0100143int mbedtls_entropy_self_test_wrapper( int verbose )
144{
145#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
146 create_entropy_seed_file( );
147#endif
148 return( mbedtls_entropy_self_test( verbose ) );
149}
150#endif
151
152#if defined(MBEDTLS_SELF_TEST)
153#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
154int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
155{
156 if( verbose != 0 )
157 {
158#if defined(MBEDTLS_MEMORY_DEBUG)
159 mbedtls_memory_buffer_alloc_status( );
160#endif
161 }
162 mbedtls_memory_buffer_alloc_free( );
163 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
164}
165#endif
166
167typedef struct
168{
169 const char *name;
170 int ( *function )( int );
171} selftest_t;
172
173const selftest_t selftests[] =
174{
175#if defined(MBEDTLS_MD2_C)
176 {"md2", mbedtls_md2_self_test},
177#endif
178#if defined(MBEDTLS_MD4_C)
179 {"md4", mbedtls_md4_self_test},
180#endif
181#if defined(MBEDTLS_MD5_C)
182 {"md5", mbedtls_md5_self_test},
183#endif
184#if defined(MBEDTLS_RIPEMD160_C)
185 {"ripemd160", mbedtls_ripemd160_self_test},
186#endif
187#if defined(MBEDTLS_SHA1_C)
188 {"sha1", mbedtls_sha1_self_test},
189#endif
190#if defined(MBEDTLS_SHA256_C)
191 {"sha256", mbedtls_sha256_self_test},
192#endif
193#if defined(MBEDTLS_SHA512_C)
194 {"sha512", mbedtls_sha512_self_test},
195#endif
196#if defined(MBEDTLS_ARC4_C)
197 {"arc4", mbedtls_arc4_self_test},
198#endif
199#if defined(MBEDTLS_DES_C)
200 {"des", mbedtls_des_self_test},
201#endif
202#if defined(MBEDTLS_AES_C)
203 {"aes", mbedtls_aes_self_test},
204#endif
205#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
206 {"gcm", mbedtls_gcm_self_test},
207#endif
208#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
209 {"ccm", mbedtls_ccm_self_test},
210#endif
211#if defined(MBEDTLS_CMAC_C)
212 {"cmac", mbedtls_cmac_self_test},
213#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300214#if defined(MBEDTLS_CHACHA20_C)
215 {"chacha20", mbedtls_chacha20_self_test},
216#endif
217#if defined(MBEDTLS_POLY1305_C)
218 {"poly1305", mbedtls_poly1305_self_test},
219#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200220#if defined(MBEDTLS_CHACHAPOLY_C)
221 {"chacha20-poly1305", mbedtls_chachapoly_self_test},
Daniel King4d8f87b2016-05-18 10:09:28 -0300222#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100223#if defined(MBEDTLS_BASE64_C)
224 {"base64", mbedtls_base64_self_test},
225#endif
226#if defined(MBEDTLS_BIGNUM_C)
227 {"mpi", mbedtls_mpi_self_test},
228#endif
229#if defined(MBEDTLS_RSA_C)
230 {"rsa", mbedtls_rsa_self_test},
231#endif
232#if defined(MBEDTLS_X509_USE_C)
233 {"x509", mbedtls_x509_self_test},
234#endif
235#if defined(MBEDTLS_XTEA_C)
236 {"xtea", mbedtls_xtea_self_test},
237#endif
238#if defined(MBEDTLS_CAMELLIA_C)
239 {"camellia", mbedtls_camellia_self_test},
240#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000241#if defined(MBEDTLS_ARIA_C)
242 {"aria", mbedtls_aria_self_test},
243#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100244#if defined(MBEDTLS_CTR_DRBG_C)
245 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
246#endif
247#if defined(MBEDTLS_HMAC_DRBG_C)
248 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
249#endif
250#if defined(MBEDTLS_ECP_C)
251 {"ecp", mbedtls_ecp_self_test},
252#endif
253#if defined(MBEDTLS_ECJPAKE_C)
254 {"ecjpake", mbedtls_ecjpake_self_test},
255#endif
256#if defined(MBEDTLS_DHM_C)
257 {"dhm", mbedtls_dhm_self_test},
258#endif
259#if defined(MBEDTLS_ENTROPY_C)
260 {"entropy", mbedtls_entropy_self_test_wrapper},
261#endif
262#if defined(MBEDTLS_PKCS5_C)
263 {"pkcs5", mbedtls_pkcs5_self_test},
264#endif
265/* Slower test after the faster ones */
266#if defined(MBEDTLS_TIMING_C)
267 {"timing", mbedtls_timing_self_test},
268#endif
269/* Heap test comes last */
270#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
271 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
272#endif
273 {NULL, NULL}
274};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100275#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100276
Paul Bakker5121ce52009-01-03 21:22:43 +0000277int main( int argc, char *argv[] )
278{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100279#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100280 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100281#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100282 char **argp;
283 int v = 1; /* v=1 for verbose mode */
284 int exclude_mode = 0;
285 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100286#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200287 unsigned char buf[1000000];
288#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200289 void *pointer;
290
291 /*
292 * The C standard doesn't guarantee that all-bits-0 is the representation
293 * of a NULL pointer. We do however use that in our code for initializing
294 * structures, which should work on every modern platform. Let's be sure.
295 */
296 memset( &pointer, 0, sizeof( void * ) );
297 if( pointer != NULL )
298 {
299 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000300 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200301 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200303 /*
304 * Make sure we have a snprintf that correctly zero-terminates
305 */
306 if( run_test_snprintf() != 0 )
307 {
308 mbedtls_printf( "the snprintf implementation is broken\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000309 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200310 }
311
Gilles Peskineff79d272017-12-20 18:09:27 +0100312 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
SimonB5a8afb82016-03-11 00:40:54 +0000313 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100314 if( strcmp( *argp, "--quiet" ) == 0 ||
315 strcmp( *argp, "-q" ) == 0 )
316 {
317 v = 0;
318 }
319 else if( strcmp( *argp, "--exclude" ) == 0 ||
320 strcmp( *argp, "-x" ) == 0 )
321 {
322 exclude_mode = 1;
323 }
324 else
325 break;
SimonB5a8afb82016-03-11 00:40:54 +0000326 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100327
328 if( v != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
334 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200335#endif
336
Gilles Peskineff79d272017-12-20 18:09:27 +0100337 if( *argp != NULL && exclude_mode == 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000338 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100339 /* Run the specified tests */
340 for( ; *argp != NULL; argp++ )
Gilles Peskine319ac802017-12-15 14:57:18 +0100341 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100342 for( test = selftests; test->name != NULL; test++ )
343 {
344 if( !strcmp( *argp, test->name ) )
345 {
346 if( test->function( v ) != 0 )
347 {
348 suites_failed++;
349 }
350 suites_tested++;
351 break;
352 }
353 }
354 if( test->name == NULL )
355 {
356 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
357 suites_failed++;
358 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100359 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100360 }
361 else
362 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100363 /* Run all the tests except excluded ones */
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100364 for( test = selftests; test->name != NULL; test++ )
365 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100366 if( exclude_mode )
367 {
368 char **excluded;
369 for( excluded = argp; *excluded != NULL; ++excluded )
370 {
371 if( !strcmp( *excluded, test->name ) )
372 break;
373 }
374 if( *excluded )
375 {
376 if( v )
377 mbedtls_printf( " Skip: %s\n", test->name );
378 continue;
379 }
380 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100381 if( test->function( v ) != 0 )
382 {
383 suites_failed++;
384 }
385 suites_tested++;
386 }
SimonB5a8afb82016-03-11 00:40:54 +0000387 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100388
Paul Bakker70940ca2016-07-14 14:30:45 +0100389#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100390 (void) exclude_mode;
Paul Bakker70940ca2016-07-14 14:30:45 +0100391 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
392#endif
393
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100394 if( v != 0 )
395 {
Simon Butcherf1547632016-03-14 23:09:39 +0000396 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
SimonB5a8afb82016-03-11 00:40:54 +0000397
398 if( suites_failed > 0)
399 {
400 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
401 }
402 else
403 {
404 mbedtls_printf( " [ All tests PASS ]\n\n" );
405 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000406#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 fflush( stdout ); getchar();
409#endif
410 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
SimonB5a8afb82016-03-11 00:40:54 +0000412 if( suites_failed > 0)
Janos Follath2e3aca22016-03-18 16:25:52 +0000413 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB5a8afb82016-03-11 00:40:54 +0000414
Janos Follath98e28a72016-05-31 14:03:54 +0100415 /* return() is here to prevent compiler warnings */
Janos Follath0c539442016-04-18 09:59:16 +0100416 return( MBEDTLS_EXIT_SUCCESS );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417}
SimonB5a8afb82016-03-11 00:40:54 +0000418