blob: fe35bb15af4636857a286d128cbe64427ba1b4d1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Self-test demonstration program
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * 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:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
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.
Paul Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
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 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/entropy.h"
Simon Butcherb6a73c92016-06-18 22:45:37 +010054#include "mbedtls/entropy_poll.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#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"
Robert Cragiedc5c7b92015-12-11 15:49:45 +000060#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#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"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000072#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030073#include "mbedtls/chacha20.h"
74#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020075#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#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"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020083#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000084#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030085#include "mbedtls/nist_kw.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Rich Evans18b78c72015-02-11 14:06:19 +000087#include <string.h>
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000090#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000091#else
92#include <stdio.h>
Janos Follath2e3aca22016-03-18 16:25:52 +000093#include <stdlib.h>
Gilles Peskine7430d232019-09-17 18:18:58 +020094#define mbedtls_calloc calloc
95#define mbedtls_free free
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#define mbedtls_printf printf
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020097#define mbedtls_snprintf snprintf
Janos Follath2e3aca22016-03-18 16:25:52 +000098#define mbedtls_exit exit
99#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
100#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +0000101#endif
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +0000104#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +0200105#endif
106
Simon Butcher63cb97e2018-12-06 17:43:31 +0000107
Gilles Peskine7430d232019-09-17 18:18:58 +0200108#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). */
112static 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
Rodrigo Dias Corread5526302020-11-10 02:28:50 -0300188static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200189{
190 int ret;
191 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200192 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200193
194 ret = mbedtls_snprintf( buf, n, "%s", "123" );
195 if( ret < 0 || (size_t) ret >= n )
196 ret = -1;
197
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200198 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
199 ref_ret != ret ||
200 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200201 {
202 return( 1 );
203 }
204
205 return( 0 );
206}
207
208static int run_test_snprintf( void )
209{
210 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200211 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 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200216}
217
Simon Butcherb6a73c92016-06-18 22:45:37 +0100218/*
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 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100223#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
224#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100225static 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
Gilles Peskine319ac802017-12-15 14:57:18 +0100252int 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)
263int 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
276typedef struct
277{
278 const char *name;
279 int ( *function )( int );
280} selftest_t;
281
282const selftest_t selftests[] =
283{
Gilles Peskine7430d232019-09-17 18:18:58 +0200284 {"calloc", calloc_self_test},
Gilles Peskine319ac802017-12-15 14:57:18 +0100285#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
Ron Eldor9ab746c2018-07-15 09:33:07 +0300321#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
322 {"nist_kw", mbedtls_nist_kw_self_test},
323#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100324#if defined(MBEDTLS_CMAC_C)
325 {"cmac", mbedtls_cmac_self_test},
326#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300327#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
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200333#if defined(MBEDTLS_CHACHAPOLY_C)
334 {"chacha20-poly1305", mbedtls_chachapoly_self_test},
Daniel King4d8f87b2016-05-18 10:09:28 -0300335#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100336#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
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000354#if defined(MBEDTLS_ARIA_C)
355 {"aria", mbedtls_aria_self_test},
356#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100357#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};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100388#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100389
Paul Bakker5121ce52009-01-03 21:22:43 +0000390int main( int argc, char *argv[] )
391{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100392#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100393 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100394#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100395 char **argp;
396 int v = 1; /* v=1 for verbose mode */
397 int exclude_mode = 0;
398 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100399#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200400 unsigned char buf[1000000];
401#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200402 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" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000413 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200414 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200416 /*
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" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000422 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200423 }
424
Gilles Peskineff79d272017-12-20 18:09:27 +0100425 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
SimonB5a8afb82016-03-11 00:40:54 +0000426 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100427 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;
SimonB5a8afb82016-03-11 00:40:54 +0000439 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100440
441 if( v != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
447 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200448#endif
449
Gilles Peskineff79d272017-12-20 18:09:27 +0100450 if( *argp != NULL && exclude_mode == 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000451 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100452 /* Run the specified tests */
453 for( ; *argp != NULL; argp++ )
Gilles Peskine319ac802017-12-15 14:57:18 +0100454 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100455 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 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100472 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100473 }
474 else
475 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100476 /* Run all the tests except excluded ones */
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100477 for( test = selftests; test->name != NULL; test++ )
478 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100479 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 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100494 if( test->function( v ) != 0 )
495 {
496 suites_failed++;
497 }
498 suites_tested++;
499 }
SimonB5a8afb82016-03-11 00:40:54 +0000500 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100501
Paul Bakker70940ca2016-07-14 14:30:45 +0100502#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100503 (void) exclude_mode;
Paul Bakker70940ca2016-07-14 14:30:45 +0100504 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
505#endif
506
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100507 if( v != 0 )
508 {
Simon Butcherf1547632016-03-14 23:09:39 +0000509 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
SimonB5a8afb82016-03-11 00:40:54 +0000510
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 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000519#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521 fflush( stdout ); getchar();
522#endif
523 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
SimonB5a8afb82016-03-11 00:40:54 +0000525 if( suites_failed > 0)
Janos Follath2e3aca22016-03-18 16:25:52 +0000526 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB5a8afb82016-03-11 00:40:54 +0000527
k-stachowiak6d10a562019-08-16 08:15:04 +0200528 mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
Paul Bakker5121ce52009-01-03 21:22:43 +0000529}