2 * Self-test demonstration program
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
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"
64 #if defined(MBEDTLS_PLATFORM_C)
65 #include "mbedtls/platform.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
76 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
77 #include "mbedtls/memory_buffer_alloc.h"
80 #if defined(MBEDTLS_CHECK_PARAMS)
81 #include "mbedtls/platform_util.h"
82 void mbedtls_param_failed( const char *failure_condition,
86 mbedtls_printf( "%s:%i: Input param failed - %s\n",
87 file, line, failure_condition );
88 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
92 static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
95 char buf[10] = "xxxxxxxxx";
96 const char ref[10] = "xxxxxxxxx";
98 ret = mbedtls_snprintf( buf, n, "%s", "123" );
99 if( ret < 0 || (size_t) ret >= n )
102 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
104 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
112 static int run_test_snprintf( void )
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 );
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
127 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
128 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
129 static void create_entropy_seed_file( void )
132 size_t output_len = 0;
133 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
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 );
142 result = mbedtls_platform_entropy_poll( NULL,
144 MBEDTLS_ENTROPY_BLOCK_SIZE,
149 if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
152 mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
156 int mbedtls_entropy_self_test_wrapper( int verbose )
158 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
159 create_entropy_seed_file( );
161 return( mbedtls_entropy_self_test( verbose ) );
165 #if defined(MBEDTLS_SELF_TEST)
166 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
167 int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
171 #if defined(MBEDTLS_MEMORY_DEBUG)
172 mbedtls_memory_buffer_alloc_status( );
175 mbedtls_memory_buffer_alloc_free( );
176 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
183 int ( *function )( int );
186 const selftest_t selftests[] =
188 #if defined(MBEDTLS_MD2_C)
189 {"md2", mbedtls_md2_self_test},
191 #if defined(MBEDTLS_MD4_C)
192 {"md4", mbedtls_md4_self_test},
194 #if defined(MBEDTLS_MD5_C)
195 {"md5", mbedtls_md5_self_test},
197 #if defined(MBEDTLS_RIPEMD160_C)
198 {"ripemd160", mbedtls_ripemd160_self_test},
200 #if defined(MBEDTLS_SHA1_C)
201 {"sha1", mbedtls_sha1_self_test},
203 #if defined(MBEDTLS_SHA256_C)
204 {"sha256", mbedtls_sha256_self_test},
206 #if defined(MBEDTLS_SHA512_C)
207 {"sha512", mbedtls_sha512_self_test},
209 #if defined(MBEDTLS_ARC4_C)
210 {"arc4", mbedtls_arc4_self_test},
212 #if defined(MBEDTLS_DES_C)
213 {"des", mbedtls_des_self_test},
215 #if defined(MBEDTLS_AES_C)
216 {"aes", mbedtls_aes_self_test},
218 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
219 {"gcm", mbedtls_gcm_self_test},
221 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
222 {"ccm", mbedtls_ccm_self_test},
224 #if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
225 {"nist_kw", mbedtls_nist_kw_self_test},
227 #if defined(MBEDTLS_CMAC_C)
228 {"cmac", mbedtls_cmac_self_test},
230 #if defined(MBEDTLS_CHACHA20_C)
231 {"chacha20", mbedtls_chacha20_self_test},
233 #if defined(MBEDTLS_POLY1305_C)
234 {"poly1305", mbedtls_poly1305_self_test},
236 #if defined(MBEDTLS_CHACHAPOLY_C)
237 {"chacha20-poly1305", mbedtls_chachapoly_self_test},
239 #if defined(MBEDTLS_BASE64_C)
240 {"base64", mbedtls_base64_self_test},
242 #if defined(MBEDTLS_BIGNUM_C)
243 {"mpi", mbedtls_mpi_self_test},
245 #if defined(MBEDTLS_RSA_C)
246 {"rsa", mbedtls_rsa_self_test},
248 #if defined(MBEDTLS_X509_USE_C)
249 {"x509", mbedtls_x509_self_test},
251 #if defined(MBEDTLS_XTEA_C)
252 {"xtea", mbedtls_xtea_self_test},
254 #if defined(MBEDTLS_CAMELLIA_C)
255 {"camellia", mbedtls_camellia_self_test},
257 #if defined(MBEDTLS_ARIA_C)
258 {"aria", mbedtls_aria_self_test},
260 #if defined(MBEDTLS_CTR_DRBG_C)
261 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
263 #if defined(MBEDTLS_HMAC_DRBG_C)
264 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
266 #if defined(MBEDTLS_ECP_C)
267 {"ecp", mbedtls_ecp_self_test},
269 #if defined(MBEDTLS_ECJPAKE_C)
270 {"ecjpake", mbedtls_ecjpake_self_test},
272 #if defined(MBEDTLS_DHM_C)
273 {"dhm", mbedtls_dhm_self_test},
275 #if defined(MBEDTLS_ENTROPY_C)
276 {"entropy", mbedtls_entropy_self_test_wrapper},
278 #if defined(MBEDTLS_PKCS5_C)
279 {"pkcs5", mbedtls_pkcs5_self_test},
281 /* Slower test after the faster ones */
282 #if defined(MBEDTLS_TIMING_C)
283 {"timing", mbedtls_timing_self_test},
285 /* Heap test comes last */
286 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
287 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
291 #endif /* MBEDTLS_SELF_TEST */
293 int main( int argc, char *argv[] )
295 #if defined(MBEDTLS_SELF_TEST)
296 const selftest_t *test;
297 #endif /* MBEDTLS_SELF_TEST */
299 int v = 1; /* v=1 for verbose mode */
300 int exclude_mode = 0;
301 int suites_tested = 0, suites_failed = 0;
302 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
303 unsigned char buf[1000000];
308 * The C standard doesn't guarantee that all-bits-0 is the representation
309 * of a NULL pointer. We do however use that in our code for initializing
310 * structures, which should work on every modern platform. Let's be sure.
312 memset( &pointer, 0, sizeof( void * ) );
313 if( pointer != NULL )
315 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
316 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
320 * Make sure we have a snprintf that correctly zero-terminates
322 if( run_test_snprintf() != 0 )
324 mbedtls_printf( "the snprintf implementation is broken\n" );
325 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
328 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
330 if( strcmp( *argp, "--quiet" ) == 0 ||
331 strcmp( *argp, "-q" ) == 0 )
335 else if( strcmp( *argp, "--exclude" ) == 0 ||
336 strcmp( *argp, "-x" ) == 0 )
345 mbedtls_printf( "\n" );
347 #if defined(MBEDTLS_SELF_TEST)
349 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
350 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
353 if( *argp != NULL && exclude_mode == 0 )
355 /* Run the specified tests */
356 for( ; *argp != NULL; argp++ )
358 for( test = selftests; test->name != NULL; test++ )
360 if( !strcmp( *argp, test->name ) )
362 if( test->function( v ) != 0 )
370 if( test->name == NULL )
372 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
379 /* Run all the tests except excluded ones */
380 for( test = selftests; test->name != NULL; test++ )
385 for( excluded = argp; *excluded != NULL; ++excluded )
387 if( !strcmp( *excluded, test->name ) )
393 mbedtls_printf( " Skip: %s\n", test->name );
397 if( test->function( v ) != 0 )
407 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
412 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
414 if( suites_failed > 0)
416 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
420 mbedtls_printf( " [ All tests PASS ]\n\n" );
423 mbedtls_printf( " Press Enter to exit this program.\n" );
424 fflush( stdout ); getchar();
428 if( suites_failed > 0)
429 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
431 /* return() is here to prevent compiler warnings */
432 return( MBEDTLS_EXIT_SUCCESS );