blob: a314bd22dcc513ec101d86f16de0c33f9db2140c [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"
Dave Rodgman8f5a29a2022-04-11 12:59:45 +010052#include "mbedtls/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Hanno Becker4ab38502018-10-16 13:22:44 +010054#include <limits.h>
Rich Evans18b78c72015-02-11 14:06:19 +000055#include <string.h>
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000059#else
60#include <stdio.h>
Janos Follath2e3aca22016-03-18 16:25:52 +000061#include <stdlib.h>
Gilles Peskine6fc21f62019-09-17 18:18:58 +020062#define mbedtls_calloc calloc
63#define mbedtls_free free
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
Simon Butcher63cb97e2018-12-06 17:43:31 +000075
Gilles Peskine6fc21f62019-09-17 18:18:58 +020076#if defined MBEDTLS_SELF_TEST
77/* Sanity check for malloc. This is not expected to fail, and is rather
78 * intended to display potentially useful information about the platform,
79 * in particular the behavior of malloc(0). */
80static int calloc_self_test( int verbose )
81{
82 int failures = 0;
83 void *empty1 = mbedtls_calloc( 0, 1 );
84 void *empty2 = mbedtls_calloc( 0, 1 );
85 void *buffer1 = mbedtls_calloc( 1, 1 );
86 void *buffer2 = mbedtls_calloc( 1, 1 );
87 uintptr_t old_buffer1;
88
89 if( empty1 == NULL && empty2 == NULL )
90 {
91 if( verbose )
92 mbedtls_printf( " CALLOC(0): passed (NULL)\n" );
93 }
94 else if( empty1 == NULL || empty2 == NULL )
95 {
96 if( verbose )
97 mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" );
98 ++failures;
99 }
100 else if( empty1 == empty2 )
101 {
102 if( verbose )
103 mbedtls_printf( " CALLOC(0): passed (same non-null)\n" );
104 }
105 else
106 {
107 if( verbose )
108 mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" );
109 }
110
111 if( buffer1 == NULL || buffer2 == NULL )
112 {
113 if( verbose )
114 mbedtls_printf( " CALLOC(1): failed (NULL)\n" );
115 ++failures;
116 }
117 else if( buffer1 == buffer2 )
118 {
119 if( verbose )
120 mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" );
121 ++failures;
122 }
123 else
124 {
125 if( verbose )
126 mbedtls_printf( " CALLOC(1): passed\n" );
127 }
128
129 old_buffer1 = (uintptr_t) buffer1;
130 mbedtls_free( buffer1 );
131 buffer1 = mbedtls_calloc( 1, 1 );
132 if( buffer1 == NULL )
133 {
134 if( verbose )
135 mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" );
136 ++failures;
137 }
138 else
139 {
140 if( verbose )
141 mbedtls_printf( " CALLOC(1 again): passed (%s address)\n",
142 (uintptr_t) old_buffer1 == (uintptr_t) buffer1 ?
143 "same" : "different" );
144 }
145
146 if( verbose )
147 mbedtls_printf( "\n" );
148 mbedtls_free( empty1 );
149 mbedtls_free( empty2 );
150 mbedtls_free( buffer1 );
151 mbedtls_free( buffer2 );
152 return( failures );
153}
154#endif /* MBEDTLS_SELF_TEST */
155
Rodrigo Dias Correa80448aa2020-11-10 02:28:50 -0300156static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200157{
158 int ret;
159 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200160 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200161
162 ret = mbedtls_snprintf( buf, n, "%s", "123" );
163 if( ret < 0 || (size_t) ret >= n )
164 ret = -1;
165
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200166 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
167 ref_ret != ret ||
168 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200169 {
170 return( 1 );
171 }
172
173 return( 0 );
174}
175
176static int run_test_snprintf( void )
177{
178 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200179 test_snprintf( 1, "", -1 ) != 0 ||
180 test_snprintf( 2, "1", -1 ) != 0 ||
181 test_snprintf( 3, "12", -1 ) != 0 ||
182 test_snprintf( 4, "123", 3 ) != 0 ||
183 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200184}
185
Simon Butcherb6a73c92016-06-18 22:45:37 +0100186/*
187 * Check if a seed file is present, and if not create one for the entropy
188 * self-test. If this fails, we attempt the test anyway, so no error is passed
189 * back.
190 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100191#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
192#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100193static void create_entropy_seed_file( void )
194{
195 int result;
196 size_t output_len = 0;
197 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
198
199 /* Attempt to read the entropy seed file. If this fails - attempt to write
200 * to the file to ensure one is present. */
201 result = mbedtls_platform_std_nv_seed_read( seed_value,
202 MBEDTLS_ENTROPY_BLOCK_SIZE );
203 if( 0 == result )
204 return;
205
206 result = mbedtls_platform_entropy_poll( NULL,
207 seed_value,
208 MBEDTLS_ENTROPY_BLOCK_SIZE,
209 &output_len );
210 if( 0 != result )
211 return;
212
213 if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
214 return;
215
216 mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
217}
218#endif
219
Gilles Peskine319ac802017-12-15 14:57:18 +0100220int mbedtls_entropy_self_test_wrapper( int verbose )
221{
222#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
223 create_entropy_seed_file( );
224#endif
225 return( mbedtls_entropy_self_test( verbose ) );
226}
227#endif
228
229#if defined(MBEDTLS_SELF_TEST)
230#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
231int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
232{
233 if( verbose != 0 )
234 {
235#if defined(MBEDTLS_MEMORY_DEBUG)
236 mbedtls_memory_buffer_alloc_status( );
237#endif
238 }
239 mbedtls_memory_buffer_alloc_free( );
240 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
241}
242#endif
243
244typedef struct
245{
246 const char *name;
247 int ( *function )( int );
248} selftest_t;
249
250const selftest_t selftests[] =
251{
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200252 {"calloc", calloc_self_test},
Gilles Peskine319ac802017-12-15 14:57:18 +0100253#if defined(MBEDTLS_MD5_C)
254 {"md5", mbedtls_md5_self_test},
255#endif
256#if defined(MBEDTLS_RIPEMD160_C)
257 {"ripemd160", mbedtls_ripemd160_self_test},
258#endif
259#if defined(MBEDTLS_SHA1_C)
260 {"sha1", mbedtls_sha1_self_test},
261#endif
262#if defined(MBEDTLS_SHA256_C)
263 {"sha256", mbedtls_sha256_self_test},
264#endif
265#if defined(MBEDTLS_SHA512_C)
266 {"sha512", mbedtls_sha512_self_test},
267#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100268#if defined(MBEDTLS_DES_C)
269 {"des", mbedtls_des_self_test},
270#endif
271#if defined(MBEDTLS_AES_C)
272 {"aes", mbedtls_aes_self_test},
273#endif
274#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
275 {"gcm", mbedtls_gcm_self_test},
276#endif
277#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
278 {"ccm", mbedtls_ccm_self_test},
279#endif
Ron Eldor9ab746c2018-07-15 09:33:07 +0300280#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
281 {"nist_kw", mbedtls_nist_kw_self_test},
282#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100283#if defined(MBEDTLS_CMAC_C)
284 {"cmac", mbedtls_cmac_self_test},
285#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300286#if defined(MBEDTLS_CHACHA20_C)
287 {"chacha20", mbedtls_chacha20_self_test},
288#endif
289#if defined(MBEDTLS_POLY1305_C)
290 {"poly1305", mbedtls_poly1305_self_test},
291#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200292#if defined(MBEDTLS_CHACHAPOLY_C)
293 {"chacha20-poly1305", mbedtls_chachapoly_self_test},
Daniel King4d8f87b2016-05-18 10:09:28 -0300294#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100295#if defined(MBEDTLS_BASE64_C)
296 {"base64", mbedtls_base64_self_test},
297#endif
298#if defined(MBEDTLS_BIGNUM_C)
299 {"mpi", mbedtls_mpi_self_test},
300#endif
301#if defined(MBEDTLS_RSA_C)
302 {"rsa", mbedtls_rsa_self_test},
303#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100304#if defined(MBEDTLS_CAMELLIA_C)
305 {"camellia", mbedtls_camellia_self_test},
306#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000307#if defined(MBEDTLS_ARIA_C)
308 {"aria", mbedtls_aria_self_test},
309#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100310#if defined(MBEDTLS_CTR_DRBG_C)
311 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
312#endif
313#if defined(MBEDTLS_HMAC_DRBG_C)
314 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
315#endif
316#if defined(MBEDTLS_ECP_C)
317 {"ecp", mbedtls_ecp_self_test},
318#endif
319#if defined(MBEDTLS_ECJPAKE_C)
320 {"ecjpake", mbedtls_ecjpake_self_test},
321#endif
322#if defined(MBEDTLS_DHM_C)
323 {"dhm", mbedtls_dhm_self_test},
324#endif
325#if defined(MBEDTLS_ENTROPY_C)
326 {"entropy", mbedtls_entropy_self_test_wrapper},
327#endif
328#if defined(MBEDTLS_PKCS5_C)
329 {"pkcs5", mbedtls_pkcs5_self_test},
330#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100331/* Heap test comes last */
332#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
333 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
334#endif
335 {NULL, NULL}
336};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100337#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100338
Paul Bakker5121ce52009-01-03 21:22:43 +0000339int main( int argc, char *argv[] )
340{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100341#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100342 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100343#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100344 char **argp;
345 int v = 1; /* v=1 for verbose mode */
346 int exclude_mode = 0;
347 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100348#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200349 unsigned char buf[1000000];
350#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200351 void *pointer;
352
353 /*
354 * The C standard doesn't guarantee that all-bits-0 is the representation
355 * of a NULL pointer. We do however use that in our code for initializing
356 * structures, which should work on every modern platform. Let's be sure.
357 */
358 memset( &pointer, 0, sizeof( void * ) );
359 if( pointer != NULL )
360 {
361 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000362 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200363 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200365 /*
Hanno Becker4ab38502018-10-16 13:22:44 +0100366 * The C standard allows padding bits in the representation
367 * of standard integer types, but our code does currently not
368 * support them.
369 *
370 * Here we check that the underlying C implementation doesn't
371 * use padding bits, and fail cleanly if it does.
372 *
373 * The check works by casting the maximum value representable
374 * by a given integer type into the unpadded integer type of the
375 * same bit-width and checking that it agrees with the maximum value
376 * of that unpadded type. For example, for a 4-byte int,
377 * MAX_INT should be 0x7fffffff in int32_t. This assumes that
378 * CHAR_BIT == 8, which is checked in check_config.h.
Hanno Beckerbaae59c2018-10-19 17:32:45 +0100379 *
380 * We assume that [u]intxx_t exist and that they don't
381 * have padding bits, as the standard requires.
Hanno Becker4ab38502018-10-16 13:22:44 +0100382 */
383
384#define CHECK_PADDING_SIGNED(TYPE, NAME) \
385 do \
386 { \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100387 if( sizeof( TYPE ) == 2 || sizeof( TYPE ) == 4 || \
388 sizeof( TYPE ) == 8 ) { \
389 if( ( sizeof( TYPE ) == 2 && \
390 (int16_t) NAME ## _MAX != 0x7FFF ) || \
391 ( sizeof( TYPE ) == 4 && \
392 (int32_t) NAME ## _MAX != 0x7FFFFFFF ) || \
393 ( sizeof( TYPE ) == 8 && \
394 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF ) ) \
395 { \
396 mbedtls_printf( "Type '" #TYPE "' has padding bits\n" );\
397 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
398 } \
399 } else { \
400 mbedtls_printf( "Padding checks only implemented for types of size 2, 4 or 8" \
Dave Rodgman8f5a29a2022-04-11 12:59:45 +0100401 " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n", \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100402 sizeof( TYPE ) ); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100403 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
404 } \
405 } while( 0 )
406
407#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
408 do \
409 { \
410 if( ( sizeof( TYPE ) == 2 && \
411 (uint16_t) NAME ## _MAX != 0xFFFF ) || \
412 ( sizeof( TYPE ) == 4 && \
413 (uint32_t) NAME ## _MAX != 0xFFFFFFFF ) || \
414 ( sizeof( TYPE ) == 8 && \
415 (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF ) ) \
416 { \
417 mbedtls_printf( "Type '" #TYPE "' has padding bits\n" ); \
418 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
419 } \
420 } while( 0 )
421
Hanno Becker0d7dd3c2018-10-19 17:32:29 +0100422 CHECK_PADDING_SIGNED( short, SHRT );
423 CHECK_PADDING_SIGNED( int, INT );
424 CHECK_PADDING_SIGNED( long, LONG );
425 CHECK_PADDING_SIGNED( long long, LLONG );
426 CHECK_PADDING_SIGNED( ptrdiff_t, PTRDIFF );
Hanno Becker4ab38502018-10-16 13:22:44 +0100427
428 CHECK_PADDING_UNSIGNED( unsigned short, USHRT );
429 CHECK_PADDING_UNSIGNED( unsigned, UINT );
430 CHECK_PADDING_UNSIGNED( unsigned long, ULONG );
431 CHECK_PADDING_UNSIGNED( unsigned long long, ULLONG );
Hanno Becker0d7dd3c2018-10-19 17:32:29 +0100432 CHECK_PADDING_UNSIGNED( size_t, SIZE );
Hanno Becker4ab38502018-10-16 13:22:44 +0100433
434#undef CHECK_PADDING_SIGNED
435#undef CHECK_PADDING_UNSIGNED
436
437 /*
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200438 * Make sure we have a snprintf that correctly zero-terminates
439 */
440 if( run_test_snprintf() != 0 )
441 {
442 mbedtls_printf( "the snprintf implementation is broken\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000443 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200444 }
445
Gilles Peskineff79d272017-12-20 18:09:27 +0100446 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
SimonB5a8afb82016-03-11 00:40:54 +0000447 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100448 if( strcmp( *argp, "--quiet" ) == 0 ||
449 strcmp( *argp, "-q" ) == 0 )
450 {
451 v = 0;
452 }
453 else if( strcmp( *argp, "--exclude" ) == 0 ||
454 strcmp( *argp, "-x" ) == 0 )
455 {
456 exclude_mode = 1;
457 }
458 else
459 break;
SimonB5a8afb82016-03-11 00:40:54 +0000460 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100461
462 if( v != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
468 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200469#endif
470
Gilles Peskineff79d272017-12-20 18:09:27 +0100471 if( *argp != NULL && exclude_mode == 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000472 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100473 /* Run the specified tests */
474 for( ; *argp != NULL; argp++ )
Gilles Peskine319ac802017-12-15 14:57:18 +0100475 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100476 for( test = selftests; test->name != NULL; test++ )
477 {
478 if( !strcmp( *argp, test->name ) )
479 {
480 if( test->function( v ) != 0 )
481 {
482 suites_failed++;
483 }
484 suites_tested++;
485 break;
486 }
487 }
488 if( test->name == NULL )
489 {
490 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
491 suites_failed++;
492 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100493 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100494 }
495 else
496 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100497 /* Run all the tests except excluded ones */
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100498 for( test = selftests; test->name != NULL; test++ )
499 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100500 if( exclude_mode )
501 {
502 char **excluded;
503 for( excluded = argp; *excluded != NULL; ++excluded )
504 {
505 if( !strcmp( *excluded, test->name ) )
506 break;
507 }
508 if( *excluded )
509 {
510 if( v )
511 mbedtls_printf( " Skip: %s\n", test->name );
512 continue;
513 }
514 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100515 if( test->function( v ) != 0 )
516 {
517 suites_failed++;
518 }
519 suites_tested++;
520 }
SimonB5a8afb82016-03-11 00:40:54 +0000521 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100522
Paul Bakker70940ca2016-07-14 14:30:45 +0100523#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100524 (void) exclude_mode;
Paul Bakker70940ca2016-07-14 14:30:45 +0100525 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
526#endif
527
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100528 if( v != 0 )
529 {
Simon Butcherf1547632016-03-14 23:09:39 +0000530 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
SimonB5a8afb82016-03-11 00:40:54 +0000531
532 if( suites_failed > 0)
533 {
534 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
535 }
536 else
537 {
538 mbedtls_printf( " [ All tests PASS ]\n\n" );
539 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000540 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
SimonB5a8afb82016-03-11 00:40:54 +0000542 if( suites_failed > 0)
Janos Follath2e3aca22016-03-18 16:25:52 +0000543 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB5a8afb82016-03-11 00:40:54 +0000544
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200545 mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546}