blob: a545d4861f4be96cbe91e81af847c281a7be2330 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Self-test demonstration program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020020#define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
Bence Szépkútic662b362021-05-27 11:25:03 +020022#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/entropy.h"
25#include "mbedtls/hmac_drbg.h"
26#include "mbedtls/ctr_drbg.h"
27#include "mbedtls/dhm.h"
28#include "mbedtls/gcm.h"
29#include "mbedtls/ccm.h"
Robert Cragiedc5c7b92015-12-11 15:49:45 +000030#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/md5.h"
32#include "mbedtls/ripemd160.h"
33#include "mbedtls/sha1.h"
34#include "mbedtls/sha256.h"
35#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/des.h"
37#include "mbedtls/aes.h"
38#include "mbedtls/camellia.h"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000039#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030040#include "mbedtls/chacha20.h"
41#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020042#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/base64.h"
44#include "mbedtls/bignum.h"
45#include "mbedtls/rsa.h"
46#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020049#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030051#include "mbedtls/nist_kw.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Hanno Becker4ab38502018-10-16 13:22:44 +010053#include <limits.h>
Rich Evans18b78c72015-02-11 14:06:19 +000054#include <string.h>
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000058#else
59#include <stdio.h>
Janos Follath2e3aca22016-03-18 16:25:52 +000060#include <stdlib.h>
Gilles Peskine6fc21f62019-09-17 18:18:58 +020061#define mbedtls_calloc calloc
62#define mbedtls_free free
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define mbedtls_printf printf
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020064#define mbedtls_snprintf snprintf
Janos Follath2e3aca22016-03-18 16:25:52 +000065#define mbedtls_exit exit
66#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
67#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000068#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020072#endif
73
Simon Butcher63cb97e2018-12-06 17:43:31 +000074
Gilles Peskine6fc21f62019-09-17 18:18:58 +020075#if defined MBEDTLS_SELF_TEST
76/* Sanity check for malloc. This is not expected to fail, and is rather
77 * intended to display potentially useful information about the platform,
78 * in particular the behavior of malloc(0). */
79static int calloc_self_test( int verbose )
80{
81 int failures = 0;
82 void *empty1 = mbedtls_calloc( 0, 1 );
83 void *empty2 = mbedtls_calloc( 0, 1 );
84 void *buffer1 = mbedtls_calloc( 1, 1 );
85 void *buffer2 = mbedtls_calloc( 1, 1 );
86 uintptr_t old_buffer1;
87
88 if( empty1 == NULL && empty2 == NULL )
89 {
90 if( verbose )
91 mbedtls_printf( " CALLOC(0): passed (NULL)\n" );
92 }
93 else if( empty1 == NULL || empty2 == NULL )
94 {
95 if( verbose )
96 mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" );
97 ++failures;
98 }
99 else if( empty1 == empty2 )
100 {
101 if( verbose )
102 mbedtls_printf( " CALLOC(0): passed (same non-null)\n" );
103 }
104 else
105 {
106 if( verbose )
107 mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" );
108 }
109
110 if( buffer1 == NULL || buffer2 == NULL )
111 {
112 if( verbose )
113 mbedtls_printf( " CALLOC(1): failed (NULL)\n" );
114 ++failures;
115 }
116 else if( buffer1 == buffer2 )
117 {
118 if( verbose )
119 mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" );
120 ++failures;
121 }
122 else
123 {
124 if( verbose )
125 mbedtls_printf( " CALLOC(1): passed\n" );
126 }
127
128 old_buffer1 = (uintptr_t) buffer1;
129 mbedtls_free( buffer1 );
130 buffer1 = mbedtls_calloc( 1, 1 );
131 if( buffer1 == NULL )
132 {
133 if( verbose )
134 mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" );
135 ++failures;
136 }
137 else
138 {
139 if( verbose )
140 mbedtls_printf( " CALLOC(1 again): passed (%s address)\n",
141 (uintptr_t) old_buffer1 == (uintptr_t) buffer1 ?
142 "same" : "different" );
143 }
144
145 if( verbose )
146 mbedtls_printf( "\n" );
147 mbedtls_free( empty1 );
148 mbedtls_free( empty2 );
149 mbedtls_free( buffer1 );
150 mbedtls_free( buffer2 );
151 return( failures );
152}
153#endif /* MBEDTLS_SELF_TEST */
154
Rodrigo Dias Correa80448aa2020-11-10 02:28:50 -0300155static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200156{
157 int ret;
158 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200159 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200160
161 ret = mbedtls_snprintf( buf, n, "%s", "123" );
162 if( ret < 0 || (size_t) ret >= n )
163 ret = -1;
164
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200165 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
166 ref_ret != ret ||
167 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200168 {
169 return( 1 );
170 }
171
172 return( 0 );
173}
174
175static int run_test_snprintf( void )
176{
177 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200178 test_snprintf( 1, "", -1 ) != 0 ||
179 test_snprintf( 2, "1", -1 ) != 0 ||
180 test_snprintf( 3, "12", -1 ) != 0 ||
181 test_snprintf( 4, "123", 3 ) != 0 ||
182 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200183}
184
Simon Butcherb6a73c92016-06-18 22:45:37 +0100185/*
186 * Check if a seed file is present, and if not create one for the entropy
187 * self-test. If this fails, we attempt the test anyway, so no error is passed
188 * back.
189 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100190#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
191#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100192static void create_entropy_seed_file( void )
193{
194 int result;
195 size_t output_len = 0;
196 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
197
198 /* Attempt to read the entropy seed file. If this fails - attempt to write
199 * to the file to ensure one is present. */
200 result = mbedtls_platform_std_nv_seed_read( seed_value,
201 MBEDTLS_ENTROPY_BLOCK_SIZE );
202 if( 0 == result )
203 return;
204
205 result = mbedtls_platform_entropy_poll( NULL,
206 seed_value,
207 MBEDTLS_ENTROPY_BLOCK_SIZE,
208 &output_len );
209 if( 0 != result )
210 return;
211
212 if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
213 return;
214
215 mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
216}
217#endif
218
Gilles Peskine319ac802017-12-15 14:57:18 +0100219int mbedtls_entropy_self_test_wrapper( int verbose )
220{
221#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
222 create_entropy_seed_file( );
223#endif
224 return( mbedtls_entropy_self_test( verbose ) );
225}
226#endif
227
228#if defined(MBEDTLS_SELF_TEST)
229#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
230int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
231{
232 if( verbose != 0 )
233 {
234#if defined(MBEDTLS_MEMORY_DEBUG)
235 mbedtls_memory_buffer_alloc_status( );
236#endif
237 }
238 mbedtls_memory_buffer_alloc_free( );
239 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
240}
241#endif
242
243typedef struct
244{
245 const char *name;
246 int ( *function )( int );
247} selftest_t;
248
249const selftest_t selftests[] =
250{
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200251 {"calloc", calloc_self_test},
Gilles Peskine319ac802017-12-15 14:57:18 +0100252#if defined(MBEDTLS_MD5_C)
253 {"md5", mbedtls_md5_self_test},
254#endif
255#if defined(MBEDTLS_RIPEMD160_C)
256 {"ripemd160", mbedtls_ripemd160_self_test},
257#endif
258#if defined(MBEDTLS_SHA1_C)
259 {"sha1", mbedtls_sha1_self_test},
260#endif
261#if defined(MBEDTLS_SHA256_C)
262 {"sha256", mbedtls_sha256_self_test},
263#endif
264#if defined(MBEDTLS_SHA512_C)
265 {"sha512", mbedtls_sha512_self_test},
266#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100267#if defined(MBEDTLS_DES_C)
268 {"des", mbedtls_des_self_test},
269#endif
270#if defined(MBEDTLS_AES_C)
271 {"aes", mbedtls_aes_self_test},
272#endif
273#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
274 {"gcm", mbedtls_gcm_self_test},
275#endif
276#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
277 {"ccm", mbedtls_ccm_self_test},
278#endif
Ron Eldor9ab746c2018-07-15 09:33:07 +0300279#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
280 {"nist_kw", mbedtls_nist_kw_self_test},
281#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100282#if defined(MBEDTLS_CMAC_C)
283 {"cmac", mbedtls_cmac_self_test},
284#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300285#if defined(MBEDTLS_CHACHA20_C)
286 {"chacha20", mbedtls_chacha20_self_test},
287#endif
288#if defined(MBEDTLS_POLY1305_C)
289 {"poly1305", mbedtls_poly1305_self_test},
290#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200291#if defined(MBEDTLS_CHACHAPOLY_C)
292 {"chacha20-poly1305", mbedtls_chachapoly_self_test},
Daniel King4d8f87b2016-05-18 10:09:28 -0300293#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100294#if defined(MBEDTLS_BASE64_C)
295 {"base64", mbedtls_base64_self_test},
296#endif
297#if defined(MBEDTLS_BIGNUM_C)
298 {"mpi", mbedtls_mpi_self_test},
299#endif
300#if defined(MBEDTLS_RSA_C)
301 {"rsa", mbedtls_rsa_self_test},
302#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100303#if defined(MBEDTLS_CAMELLIA_C)
304 {"camellia", mbedtls_camellia_self_test},
305#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000306#if defined(MBEDTLS_ARIA_C)
307 {"aria", mbedtls_aria_self_test},
308#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100309#if defined(MBEDTLS_CTR_DRBG_C)
310 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
311#endif
312#if defined(MBEDTLS_HMAC_DRBG_C)
313 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
314#endif
315#if defined(MBEDTLS_ECP_C)
316 {"ecp", mbedtls_ecp_self_test},
317#endif
318#if defined(MBEDTLS_ECJPAKE_C)
319 {"ecjpake", mbedtls_ecjpake_self_test},
320#endif
321#if defined(MBEDTLS_DHM_C)
322 {"dhm", mbedtls_dhm_self_test},
323#endif
324#if defined(MBEDTLS_ENTROPY_C)
325 {"entropy", mbedtls_entropy_self_test_wrapper},
326#endif
327#if defined(MBEDTLS_PKCS5_C)
328 {"pkcs5", mbedtls_pkcs5_self_test},
329#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100330/* Heap test comes last */
331#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
332 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
333#endif
334 {NULL, NULL}
335};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100336#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100337
Paul Bakker5121ce52009-01-03 21:22:43 +0000338int main( int argc, char *argv[] )
339{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100340#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100341 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100342#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100343 char **argp;
344 int v = 1; /* v=1 for verbose mode */
345 int exclude_mode = 0;
346 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100347#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200348 unsigned char buf[1000000];
349#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200350 void *pointer;
351
352 /*
353 * The C standard doesn't guarantee that all-bits-0 is the representation
354 * of a NULL pointer. We do however use that in our code for initializing
355 * structures, which should work on every modern platform. Let's be sure.
356 */
357 memset( &pointer, 0, sizeof( void * ) );
358 if( pointer != NULL )
359 {
360 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000361 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200362 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200364 /*
Hanno Becker4ab38502018-10-16 13:22:44 +0100365 * The C standard allows padding bits in the representation
366 * of standard integer types, but our code does currently not
367 * support them.
368 *
369 * Here we check that the underlying C implementation doesn't
370 * use padding bits, and fail cleanly if it does.
371 *
372 * The check works by casting the maximum value representable
373 * by a given integer type into the unpadded integer type of the
374 * same bit-width and checking that it agrees with the maximum value
375 * of that unpadded type. For example, for a 4-byte int,
376 * MAX_INT should be 0x7fffffff in int32_t. This assumes that
377 * CHAR_BIT == 8, which is checked in check_config.h.
378 */
379
380#define CHECK_PADDING_SIGNED(TYPE, NAME) \
381 do \
382 { \
383 if( ( sizeof( TYPE ) == 2 && \
384 (int16_t) NAME ## _MAX != 0x7FFF ) || \
385 ( sizeof( TYPE ) == 4 && \
386 (int32_t) NAME ## _MAX != 0x7FFFFFFF ) || \
387 ( sizeof( TYPE ) == 8 && \
388 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF ) ) \
389 { \
390 mbedtls_printf( "Type '" #TYPE "' has padding bits\n" ); \
391 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
392 } \
393 } while( 0 )
394
395#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
396 do \
397 { \
398 if( ( sizeof( TYPE ) == 2 && \
399 (uint16_t) NAME ## _MAX != 0xFFFF ) || \
400 ( sizeof( TYPE ) == 4 && \
401 (uint32_t) NAME ## _MAX != 0xFFFFFFFF ) || \
402 ( sizeof( TYPE ) == 8 && \
403 (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF ) ) \
404 { \
405 mbedtls_printf( "Type '" #TYPE "' has padding bits\n" ); \
406 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
407 } \
408 } while( 0 )
409
410 CHECK_PADDING_SIGNED( short, SHRT );
411 CHECK_PADDING_SIGNED( int, INT );
412 CHECK_PADDING_SIGNED( long, LONG );
413 CHECK_PADDING_SIGNED( long long, LLONG );
414
415 CHECK_PADDING_UNSIGNED( unsigned short, USHRT );
416 CHECK_PADDING_UNSIGNED( unsigned, UINT );
417 CHECK_PADDING_UNSIGNED( unsigned long, ULONG );
418 CHECK_PADDING_UNSIGNED( unsigned long long, ULLONG );
419
420#undef CHECK_PADDING_SIGNED
421#undef CHECK_PADDING_UNSIGNED
422
423 /*
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200424 * Make sure we have a snprintf that correctly zero-terminates
425 */
426 if( run_test_snprintf() != 0 )
427 {
428 mbedtls_printf( "the snprintf implementation is broken\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000429 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200430 }
431
Gilles Peskineff79d272017-12-20 18:09:27 +0100432 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
SimonB5a8afb82016-03-11 00:40:54 +0000433 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100434 if( strcmp( *argp, "--quiet" ) == 0 ||
435 strcmp( *argp, "-q" ) == 0 )
436 {
437 v = 0;
438 }
439 else if( strcmp( *argp, "--exclude" ) == 0 ||
440 strcmp( *argp, "-x" ) == 0 )
441 {
442 exclude_mode = 1;
443 }
444 else
445 break;
SimonB5a8afb82016-03-11 00:40:54 +0000446 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100447
448 if( v != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
454 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200455#endif
456
Gilles Peskineff79d272017-12-20 18:09:27 +0100457 if( *argp != NULL && exclude_mode == 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000458 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100459 /* Run the specified tests */
460 for( ; *argp != NULL; argp++ )
Gilles Peskine319ac802017-12-15 14:57:18 +0100461 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100462 for( test = selftests; test->name != NULL; test++ )
463 {
464 if( !strcmp( *argp, test->name ) )
465 {
466 if( test->function( v ) != 0 )
467 {
468 suites_failed++;
469 }
470 suites_tested++;
471 break;
472 }
473 }
474 if( test->name == NULL )
475 {
476 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
477 suites_failed++;
478 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100479 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100480 }
481 else
482 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100483 /* Run all the tests except excluded ones */
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100484 for( test = selftests; test->name != NULL; test++ )
485 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100486 if( exclude_mode )
487 {
488 char **excluded;
489 for( excluded = argp; *excluded != NULL; ++excluded )
490 {
491 if( !strcmp( *excluded, test->name ) )
492 break;
493 }
494 if( *excluded )
495 {
496 if( v )
497 mbedtls_printf( " Skip: %s\n", test->name );
498 continue;
499 }
500 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100501 if( test->function( v ) != 0 )
502 {
503 suites_failed++;
504 }
505 suites_tested++;
506 }
SimonB5a8afb82016-03-11 00:40:54 +0000507 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100508
Paul Bakker70940ca2016-07-14 14:30:45 +0100509#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100510 (void) exclude_mode;
Paul Bakker70940ca2016-07-14 14:30:45 +0100511 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
512#endif
513
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100514 if( v != 0 )
515 {
Simon Butcherf1547632016-03-14 23:09:39 +0000516 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
SimonB5a8afb82016-03-11 00:40:54 +0000517
518 if( suites_failed > 0)
519 {
520 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
521 }
522 else
523 {
524 mbedtls_printf( " [ All tests PASS ]\n\n" );
525 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000526#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528 fflush( stdout ); getchar();
529#endif
530 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000531
SimonB5a8afb82016-03-11 00:40:54 +0000532 if( suites_failed > 0)
Janos Follath2e3aca22016-03-18 16:25:52 +0000533 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB5a8afb82016-03-11 00:40:54 +0000534
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200535 mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536}