blob: 846d34da7ca1d79b23a9c47e17cf26417ba53f29 [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"
29#include "mbedtls/hmac_drbg.h"
30#include "mbedtls/ctr_drbg.h"
31#include "mbedtls/dhm.h"
32#include "mbedtls/gcm.h"
33#include "mbedtls/ccm.h"
34#include "mbedtls/md2.h"
35#include "mbedtls/md4.h"
36#include "mbedtls/md5.h"
37#include "mbedtls/ripemd160.h"
38#include "mbedtls/sha1.h"
39#include "mbedtls/sha256.h"
40#include "mbedtls/sha512.h"
41#include "mbedtls/arc4.h"
42#include "mbedtls/des.h"
43#include "mbedtls/aes.h"
44#include "mbedtls/camellia.h"
45#include "mbedtls/base64.h"
46#include "mbedtls/bignum.h"
47#include "mbedtls/rsa.h"
48#include "mbedtls/x509.h"
49#include "mbedtls/xtea.h"
50#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/ecp.h"
52#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Gilles Peskine83cd34a2017-12-21 11:07:37 +010054#include <stdlib.h>
Rich Evans18b78c72015-02-11 14:06:19 +000055#include <stdio.h>
56#include <string.h>
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000060#else
61#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_printf printf
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020063#define mbedtls_snprintf snprintf
Rich Evans18b78c72015-02-11 14:06:19 +000064#endif
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020068#endif
69
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020070static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
71{
72 int ret;
73 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020074 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020075
76 ret = mbedtls_snprintf( buf, n, "%s", "123" );
77 if( ret < 0 || (size_t) ret >= n )
78 ret = -1;
79
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020080 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
81 ref_ret != ret ||
82 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020083 {
84 return( 1 );
85 }
86
87 return( 0 );
88}
89
90static int run_test_snprintf( void )
91{
92 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020093 test_snprintf( 1, "", -1 ) != 0 ||
94 test_snprintf( 2, "1", -1 ) != 0 ||
95 test_snprintf( 3, "12", -1 ) != 0 ||
96 test_snprintf( 4, "123", 3 ) != 0 ||
97 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020098}
99
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100100#if defined(MBEDTLS_SELF_TEST)
101#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
102int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
103{
104 if( verbose != 0 )
105 {
106#if defined(MBEDTLS_MEMORY_DEBUG)
107 mbedtls_memory_buffer_alloc_status( );
108#endif
109 }
110 mbedtls_memory_buffer_alloc_free( );
111 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
112}
113#endif
114
115typedef struct
116{
117 const char *name;
118 int ( *function )( int );
119} selftest_t;
120
121const selftest_t selftests[] =
122{
123#if defined(MBEDTLS_MD2_C)
124 {"md2", mbedtls_md2_self_test},
125#endif
126#if defined(MBEDTLS_MD4_C)
127 {"md4", mbedtls_md4_self_test},
128#endif
129#if defined(MBEDTLS_MD5_C)
130 {"md5", mbedtls_md5_self_test},
131#endif
132#if defined(MBEDTLS_RIPEMD160_C)
133 {"ripemd160", mbedtls_ripemd160_self_test},
134#endif
135#if defined(MBEDTLS_SHA1_C)
136 {"sha1", mbedtls_sha1_self_test},
137#endif
138#if defined(MBEDTLS_SHA256_C)
139 {"sha256", mbedtls_sha256_self_test},
140#endif
141#if defined(MBEDTLS_SHA512_C)
142 {"sha512", mbedtls_sha512_self_test},
143#endif
144#if defined(MBEDTLS_ARC4_C)
145 {"arc4", mbedtls_arc4_self_test},
146#endif
147#if defined(MBEDTLS_DES_C)
148 {"des", mbedtls_des_self_test},
149#endif
150#if defined(MBEDTLS_AES_C)
151 {"aes", mbedtls_aes_self_test},
152#endif
153#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
154 {"gcm", mbedtls_gcm_self_test},
155#endif
156#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
157 {"ccm", mbedtls_ccm_self_test},
158#endif
159#if defined(MBEDTLS_BASE64_C)
160 {"base64", mbedtls_base64_self_test},
161#endif
162#if defined(MBEDTLS_BIGNUM_C)
163 {"mpi", mbedtls_mpi_self_test},
164#endif
165#if defined(MBEDTLS_RSA_C)
166 {"rsa", mbedtls_rsa_self_test},
167#endif
168#if defined(MBEDTLS_X509_USE_C)
169 {"x509", mbedtls_x509_self_test},
170#endif
171#if defined(MBEDTLS_XTEA_C)
172 {"xtea", mbedtls_xtea_self_test},
173#endif
174#if defined(MBEDTLS_CAMELLIA_C)
175 {"camellia", mbedtls_camellia_self_test},
176#endif
177#if defined(MBEDTLS_CTR_DRBG_C)
178 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
179#endif
180#if defined(MBEDTLS_HMAC_DRBG_C)
181 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
182#endif
183#if defined(MBEDTLS_ECP_C)
184 {"ecp", mbedtls_ecp_self_test},
185#endif
186#if defined(MBEDTLS_DHM_C)
187 {"dhm", mbedtls_dhm_self_test},
188#endif
189#if defined(MBEDTLS_ENTROPY_C)
190 {"entropy", mbedtls_entropy_self_test},
191#endif
192#if defined(MBEDTLS_PKCS5_C)
193 {"pkcs5", mbedtls_pkcs5_self_test},
194#endif
195/* Slower test after the faster ones */
196#if defined(MBEDTLS_TIMING_C)
197 {"timing", mbedtls_timing_self_test},
198#endif
199/* Heap test comes last */
200#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
201 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
202#endif
203 {NULL, NULL}
204};
205#endif /* MBEDTLS_SELF_TEST */
206
Paul Bakker5121ce52009-01-03 21:22:43 +0000207int main( int argc, char *argv[] )
208{
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100209#if defined(MBEDTLS_SELF_TEST)
210 const selftest_t *test;
211#endif /* MBEDTLS_SELF_TEST */
Gilles Peskinea66fb3f2017-12-20 18:09:27 +0100212 char **argp;
213 int v = 1; /* v=1 for verbose mode */
214 int exclude_mode = 0;
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100215 int suites_tested = 0, suites_failed = 0;
Gilles Peskineedede442017-12-15 15:01:27 +0100216#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200217 unsigned char buf[1000000];
218#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200219 void *pointer;
220
221 /*
222 * The C standard doesn't guarantee that all-bits-0 is the representation
223 * of a NULL pointer. We do however use that in our code for initializing
224 * structures, which should work on every modern platform. Let's be sure.
225 */
226 memset( &pointer, 0, sizeof( void * ) );
227 if( pointer != NULL )
228 {
229 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
Gilles Peskine6d51b632017-12-20 20:25:03 +0100230 return( EXIT_FAILURE );
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200231 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200233 /*
234 * Make sure we have a snprintf that correctly zero-terminates
235 */
236 if( run_test_snprintf() != 0 )
237 {
238 mbedtls_printf( "the snprintf implementation is broken\n" );
Gilles Peskine6d51b632017-12-20 20:25:03 +0100239 return( EXIT_FAILURE );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200240 }
241
Gilles Peskinea66fb3f2017-12-20 18:09:27 +0100242 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
Gilles Peskineedede442017-12-15 15:01:27 +0100243 {
Gilles Peskinea66fb3f2017-12-20 18:09:27 +0100244 if( strcmp( *argp, "--quiet" ) == 0 ||
245 strcmp( *argp, "-quiet" ) == 0 ||
246 strcmp( *argp, "-q" ) == 0 )
247 {
248 v = 0;
249 }
250 else if( strcmp( *argp, "--exclude" ) == 0 ||
251 strcmp( *argp, "-x" ) == 0 )
252 {
253 exclude_mode = 1;
254 }
255 else
256 break;
Gilles Peskineedede442017-12-15 15:01:27 +0100257 }
Gilles Peskinea66fb3f2017-12-20 18:09:27 +0100258
259 if( v != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
265 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200266#endif
267
Gilles Peskinea66fb3f2017-12-20 18:09:27 +0100268 if( *argp != NULL && exclude_mode == 0 )
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100269 {
Gilles Peskineedede442017-12-15 15:01:27 +0100270 /* Run the specified tests */
271 for( ; *argp != NULL; argp++ )
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100272 {
Gilles Peskineedede442017-12-15 15:01:27 +0100273 for( test = selftests; test->name != NULL; test++ )
274 {
275 if( !strcmp( *argp, test->name ) )
276 {
277 if( test->function( v ) != 0 )
278 {
279 suites_failed++;
280 }
281 suites_tested++;
282 break;
283 }
284 }
285 if( test->name == NULL )
286 {
287 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
288 suites_failed++;
289 }
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100290 }
Gilles Peskineedede442017-12-15 15:01:27 +0100291 }
292 else
293 {
Gilles Peskinea66fb3f2017-12-20 18:09:27 +0100294 /* Run all the tests except excluded ones */
Gilles Peskineedede442017-12-15 15:01:27 +0100295 for( test = selftests; test->name != NULL; test++ )
296 {
Gilles Peskinea66fb3f2017-12-20 18:09:27 +0100297 if( exclude_mode )
298 {
299 char **excluded;
300 for( excluded = argp; *excluded != NULL; ++excluded )
301 {
302 if( !strcmp( *excluded, test->name ) )
303 break;
304 }
305 if( *excluded )
306 {
307 if( v )
308 mbedtls_printf( " Skip: %s\n", test->name );
309 continue;
310 }
311 }
Gilles Peskineedede442017-12-15 15:01:27 +0100312 if( test->function( v ) != 0 )
313 {
314 suites_failed++;
315 }
316 suites_tested++;
317 }
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100318 }
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100319
Paul Bakker135b98e2011-05-25 11:13:47 +0000320#else
Gilles Peskinea66fb3f2017-12-20 18:09:27 +0100321 (void) exclude_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
Paul Bakker135b98e2011-05-25 11:13:47 +0000323#endif
324
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 if( v != 0 )
326 {
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100327 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
Paul Bakker6e339b52013-07-03 13:37:05 +0200328
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100329 if( suites_failed > 0)
330 {
331 mbedtls_printf( " [ %d tests FAILED ]\n\n", suites_failed );
332 }
333 else
334 {
335 mbedtls_printf( " [ All tests passed ]\n\n" );
336 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000337#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 fflush( stdout ); getchar();
340#endif
341 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100343 if( suites_failed > 0)
344 return( EXIT_FAILURE );
345
346 return( EXIT_SUCCESS );
Paul Bakker5121ce52009-01-03 21:22:43 +0000347}