blob: 16ff3102da697f39c47038e3b5ac6792b3c346d8 [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"
47#include "mbedtls/base64.h"
48#include "mbedtls/bignum.h"
49#include "mbedtls/rsa.h"
50#include "mbedtls/x509.h"
51#include "mbedtls/xtea.h"
52#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020054#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Rich Evans18b78c72015-02-11 14:06:19 +000057#include <string.h>
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000061#else
62#include <stdio.h>
Janos Follath2e3aca22016-03-18 16:25:52 +000063#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020065#define mbedtls_snprintf snprintf
Janos Follath2e3aca22016-03-18 16:25:52 +000066#define mbedtls_exit exit
67#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
68#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000069#endif
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020073#endif
74
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020075static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
76{
77 int ret;
78 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020079 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020080
81 ret = mbedtls_snprintf( buf, n, "%s", "123" );
82 if( ret < 0 || (size_t) ret >= n )
83 ret = -1;
84
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020085 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
86 ref_ret != ret ||
87 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020088 {
89 return( 1 );
90 }
91
92 return( 0 );
93}
94
95static int run_test_snprintf( void )
96{
97 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020098 test_snprintf( 1, "", -1 ) != 0 ||
99 test_snprintf( 2, "1", -1 ) != 0 ||
100 test_snprintf( 3, "12", -1 ) != 0 ||
101 test_snprintf( 4, "123", 3 ) != 0 ||
102 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200103}
104
Simon Butcherb6a73c92016-06-18 22:45:37 +0100105/*
106 * Check if a seed file is present, and if not create one for the entropy
107 * self-test. If this fails, we attempt the test anyway, so no error is passed
108 * back.
109 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100110#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
111#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100112static void create_entropy_seed_file( void )
113{
114 int result;
115 size_t output_len = 0;
116 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
117
118 /* Attempt to read the entropy seed file. If this fails - attempt to write
119 * to the file to ensure one is present. */
120 result = mbedtls_platform_std_nv_seed_read( seed_value,
121 MBEDTLS_ENTROPY_BLOCK_SIZE );
122 if( 0 == result )
123 return;
124
125 result = mbedtls_platform_entropy_poll( NULL,
126 seed_value,
127 MBEDTLS_ENTROPY_BLOCK_SIZE,
128 &output_len );
129 if( 0 != result )
130 return;
131
132 if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
133 return;
134
135 mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
136}
137#endif
138
Gilles Peskine319ac802017-12-15 14:57:18 +0100139int mbedtls_entropy_self_test_wrapper( int verbose )
140{
141#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
142 create_entropy_seed_file( );
143#endif
144 return( mbedtls_entropy_self_test( verbose ) );
145}
146#endif
147
148#if defined(MBEDTLS_SELF_TEST)
149#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
150int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
151{
152 if( verbose != 0 )
153 {
154#if defined(MBEDTLS_MEMORY_DEBUG)
155 mbedtls_memory_buffer_alloc_status( );
156#endif
157 }
158 mbedtls_memory_buffer_alloc_free( );
159 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
160}
161#endif
162
163typedef struct
164{
165 const char *name;
166 int ( *function )( int );
167} selftest_t;
168
169const selftest_t selftests[] =
170{
171#if defined(MBEDTLS_MD2_C)
172 {"md2", mbedtls_md2_self_test},
173#endif
174#if defined(MBEDTLS_MD4_C)
175 {"md4", mbedtls_md4_self_test},
176#endif
177#if defined(MBEDTLS_MD5_C)
178 {"md5", mbedtls_md5_self_test},
179#endif
180#if defined(MBEDTLS_RIPEMD160_C)
181 {"ripemd160", mbedtls_ripemd160_self_test},
182#endif
183#if defined(MBEDTLS_SHA1_C)
184 {"sha1", mbedtls_sha1_self_test},
185#endif
186#if defined(MBEDTLS_SHA256_C)
187 {"sha256", mbedtls_sha256_self_test},
188#endif
189#if defined(MBEDTLS_SHA512_C)
190 {"sha512", mbedtls_sha512_self_test},
191#endif
192#if defined(MBEDTLS_ARC4_C)
193 {"arc4", mbedtls_arc4_self_test},
194#endif
195#if defined(MBEDTLS_DES_C)
196 {"des", mbedtls_des_self_test},
197#endif
198#if defined(MBEDTLS_AES_C)
199 {"aes", mbedtls_aes_self_test},
200#endif
201#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
202 {"gcm", mbedtls_gcm_self_test},
203#endif
204#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
205 {"ccm", mbedtls_ccm_self_test},
206#endif
207#if defined(MBEDTLS_CMAC_C)
208 {"cmac", mbedtls_cmac_self_test},
209#endif
210#if defined(MBEDTLS_BASE64_C)
211 {"base64", mbedtls_base64_self_test},
212#endif
213#if defined(MBEDTLS_BIGNUM_C)
214 {"mpi", mbedtls_mpi_self_test},
215#endif
216#if defined(MBEDTLS_RSA_C)
217 {"rsa", mbedtls_rsa_self_test},
218#endif
219#if defined(MBEDTLS_X509_USE_C)
220 {"x509", mbedtls_x509_self_test},
221#endif
222#if defined(MBEDTLS_XTEA_C)
223 {"xtea", mbedtls_xtea_self_test},
224#endif
225#if defined(MBEDTLS_CAMELLIA_C)
226 {"camellia", mbedtls_camellia_self_test},
227#endif
228#if defined(MBEDTLS_CTR_DRBG_C)
229 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
230#endif
231#if defined(MBEDTLS_HMAC_DRBG_C)
232 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
233#endif
234#if defined(MBEDTLS_ECP_C)
235 {"ecp", mbedtls_ecp_self_test},
236#endif
237#if defined(MBEDTLS_ECJPAKE_C)
238 {"ecjpake", mbedtls_ecjpake_self_test},
239#endif
240#if defined(MBEDTLS_DHM_C)
241 {"dhm", mbedtls_dhm_self_test},
242#endif
243#if defined(MBEDTLS_ENTROPY_C)
244 {"entropy", mbedtls_entropy_self_test_wrapper},
245#endif
246#if defined(MBEDTLS_PKCS5_C)
247 {"pkcs5", mbedtls_pkcs5_self_test},
248#endif
249/* Slower test after the faster ones */
250#if defined(MBEDTLS_TIMING_C)
251 {"timing", mbedtls_timing_self_test},
252#endif
253/* Heap test comes last */
254#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
255 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
256#endif
257 {NULL, NULL}
258};
259
Paul Bakker5121ce52009-01-03 21:22:43 +0000260int main( int argc, char *argv[] )
261{
Gilles Peskine319ac802017-12-15 14:57:18 +0100262 const selftest_t *test;
Janos Follath2e3aca22016-03-18 16:25:52 +0000263 int v, suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100264#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200265 unsigned char buf[1000000];
266#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200267 void *pointer;
268
269 /*
270 * The C standard doesn't guarantee that all-bits-0 is the representation
271 * of a NULL pointer. We do however use that in our code for initializing
272 * structures, which should work on every modern platform. Let's be sure.
273 */
274 memset( &pointer, 0, sizeof( void * ) );
275 if( pointer != NULL )
276 {
277 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000278 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200279 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200281 /*
282 * Make sure we have a snprintf that correctly zero-terminates
283 */
284 if( run_test_snprintf() != 0 )
285 {
286 mbedtls_printf( "the snprintf implementation is broken\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000287 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200288 }
289
SimonB5a8afb82016-03-11 00:40:54 +0000290 if( argc == 2 && ( strcmp( argv[1], "--quiet" ) == 0 ||
291 strcmp( argv[1], "-q" ) == 0 ) )
292 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 v = 0;
SimonB5a8afb82016-03-11 00:40:54 +0000294 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 else
296 {
297 v = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 }
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
304 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200305#endif
306
Gilles Peskine319ac802017-12-15 14:57:18 +0100307 for( test = selftests; test->name != NULL; test++ )
SimonB5a8afb82016-03-11 00:40:54 +0000308 {
Gilles Peskine319ac802017-12-15 14:57:18 +0100309 if( test->function( v ) != 0 )
310 {
311 suites_failed++;
312 }
313 suites_tested++;
SimonB5a8afb82016-03-11 00:40:54 +0000314 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100315
Paul Bakker70940ca2016-07-14 14:30:45 +0100316#else
317 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
318#endif
319
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100320 if( v != 0 )
321 {
Simon Butcherf1547632016-03-14 23:09:39 +0000322 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
SimonB5a8afb82016-03-11 00:40:54 +0000323
324 if( suites_failed > 0)
325 {
326 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
327 }
328 else
329 {
330 mbedtls_printf( " [ All tests PASS ]\n\n" );
331 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000332#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 fflush( stdout ); getchar();
335#endif
336 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000337
SimonB5a8afb82016-03-11 00:40:54 +0000338 if( suites_failed > 0)
Janos Follath2e3aca22016-03-18 16:25:52 +0000339 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB5a8afb82016-03-11 00:40:54 +0000340
Janos Follath98e28a72016-05-31 14:03:54 +0100341 /* return() is here to prevent compiler warnings */
Janos Follath0c539442016-04-18 09:59:16 +0100342 return( MBEDTLS_EXIT_SUCCESS );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343}
SimonB5a8afb82016-03-11 00:40:54 +0000344