blob: b91bd07b5f28c7900caa22c9f3680cde71d38c54 [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"
Pol Henarejos7dbd5d12022-05-20 20:42:33 +020036#include "mbedtls/sha3.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/des.h"
38#include "mbedtls/aes.h"
39#include "mbedtls/camellia.h"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000040#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030041#include "mbedtls/chacha20.h"
42#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020043#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/base64.h"
45#include "mbedtls/bignum.h"
46#include "mbedtls/rsa.h"
47#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020050#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030052#include "mbedtls/nist_kw.h"
Dave Rodgman8f5a29a2022-04-11 12:59:45 +010053#include "mbedtls/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Hanno Becker4ab38502018-10-16 13:22:44 +010055#include <limits.h>
Rich Evans18b78c72015-02-11 14:06:19 +000056#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>
Janos Follath2e3aca22016-03-18 16:25:52 +000062#include <stdlib.h>
Gilles Peskine6fc21f62019-09-17 18:18:58 +020063#define mbedtls_calloc calloc
64#define mbedtls_free free
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define mbedtls_printf printf
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020066#define mbedtls_snprintf snprintf
Janos Follath2e3aca22016-03-18 16:25:52 +000067#define mbedtls_exit exit
68#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
69#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000070#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020074#endif
75
Simon Butcher63cb97e2018-12-06 17:43:31 +000076
Gilles Peskine6fc21f62019-09-17 18:18:58 +020077#if defined MBEDTLS_SELF_TEST
78/* Sanity check for malloc. This is not expected to fail, and is rather
79 * intended to display potentially useful information about the platform,
80 * in particular the behavior of malloc(0). */
81static int calloc_self_test( int verbose )
82{
83 int failures = 0;
84 void *empty1 = mbedtls_calloc( 0, 1 );
85 void *empty2 = mbedtls_calloc( 0, 1 );
86 void *buffer1 = mbedtls_calloc( 1, 1 );
87 void *buffer2 = mbedtls_calloc( 1, 1 );
88 uintptr_t old_buffer1;
89
90 if( empty1 == NULL && empty2 == NULL )
91 {
92 if( verbose )
93 mbedtls_printf( " CALLOC(0): passed (NULL)\n" );
94 }
95 else if( empty1 == NULL || empty2 == NULL )
96 {
97 if( verbose )
98 mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" );
99 ++failures;
100 }
101 else if( empty1 == empty2 )
102 {
103 if( verbose )
104 mbedtls_printf( " CALLOC(0): passed (same non-null)\n" );
105 }
106 else
107 {
108 if( verbose )
109 mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" );
110 }
111
112 if( buffer1 == NULL || buffer2 == NULL )
113 {
114 if( verbose )
115 mbedtls_printf( " CALLOC(1): failed (NULL)\n" );
116 ++failures;
117 }
118 else if( buffer1 == buffer2 )
119 {
120 if( verbose )
121 mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" );
122 ++failures;
123 }
124 else
125 {
126 if( verbose )
127 mbedtls_printf( " CALLOC(1): passed\n" );
128 }
129
130 old_buffer1 = (uintptr_t) buffer1;
131 mbedtls_free( buffer1 );
132 buffer1 = mbedtls_calloc( 1, 1 );
133 if( buffer1 == NULL )
134 {
135 if( verbose )
136 mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" );
137 ++failures;
138 }
139 else
140 {
141 if( verbose )
142 mbedtls_printf( " CALLOC(1 again): passed (%s address)\n",
143 (uintptr_t) old_buffer1 == (uintptr_t) buffer1 ?
144 "same" : "different" );
145 }
146
147 if( verbose )
148 mbedtls_printf( "\n" );
149 mbedtls_free( empty1 );
150 mbedtls_free( empty2 );
151 mbedtls_free( buffer1 );
152 mbedtls_free( buffer2 );
153 return( failures );
154}
155#endif /* MBEDTLS_SELF_TEST */
156
Rodrigo Dias Correa80448aa2020-11-10 02:28:50 -0300157static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200158{
159 int ret;
160 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200161 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200162
163 ret = mbedtls_snprintf( buf, n, "%s", "123" );
164 if( ret < 0 || (size_t) ret >= n )
165 ret = -1;
166
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200167 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
168 ref_ret != ret ||
169 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200170 {
171 return( 1 );
172 }
173
174 return( 0 );
175}
176
177static int run_test_snprintf( void )
178{
179 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200180 test_snprintf( 1, "", -1 ) != 0 ||
181 test_snprintf( 2, "1", -1 ) != 0 ||
182 test_snprintf( 3, "12", -1 ) != 0 ||
183 test_snprintf( 4, "123", 3 ) != 0 ||
184 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200185}
186
Simon Butcherb6a73c92016-06-18 22:45:37 +0100187/*
188 * Check if a seed file is present, and if not create one for the entropy
189 * self-test. If this fails, we attempt the test anyway, so no error is passed
190 * back.
191 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100192#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
193#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100194static void create_entropy_seed_file( void )
195{
196 int result;
197 size_t output_len = 0;
198 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
199
200 /* Attempt to read the entropy seed file. If this fails - attempt to write
201 * to the file to ensure one is present. */
202 result = mbedtls_platform_std_nv_seed_read( seed_value,
203 MBEDTLS_ENTROPY_BLOCK_SIZE );
204 if( 0 == result )
205 return;
206
207 result = mbedtls_platform_entropy_poll( NULL,
208 seed_value,
209 MBEDTLS_ENTROPY_BLOCK_SIZE,
210 &output_len );
211 if( 0 != result )
212 return;
213
214 if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
215 return;
216
217 mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
218}
219#endif
220
Gilles Peskine319ac802017-12-15 14:57:18 +0100221int mbedtls_entropy_self_test_wrapper( int verbose )
222{
223#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
224 create_entropy_seed_file( );
225#endif
226 return( mbedtls_entropy_self_test( verbose ) );
227}
228#endif
229
230#if defined(MBEDTLS_SELF_TEST)
231#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
232int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
233{
234 if( verbose != 0 )
235 {
236#if defined(MBEDTLS_MEMORY_DEBUG)
237 mbedtls_memory_buffer_alloc_status( );
238#endif
239 }
240 mbedtls_memory_buffer_alloc_free( );
241 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
242}
243#endif
244
245typedef struct
246{
247 const char *name;
248 int ( *function )( int );
249} selftest_t;
250
251const selftest_t selftests[] =
252{
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200253 {"calloc", calloc_self_test},
Gilles Peskine319ac802017-12-15 14:57:18 +0100254#if defined(MBEDTLS_MD5_C)
255 {"md5", mbedtls_md5_self_test},
256#endif
257#if defined(MBEDTLS_RIPEMD160_C)
258 {"ripemd160", mbedtls_ripemd160_self_test},
259#endif
260#if defined(MBEDTLS_SHA1_C)
261 {"sha1", mbedtls_sha1_self_test},
262#endif
263#if defined(MBEDTLS_SHA256_C)
264 {"sha256", mbedtls_sha256_self_test},
265#endif
266#if defined(MBEDTLS_SHA512_C)
267 {"sha512", mbedtls_sha512_self_test},
268#endif
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200269#if defined(MBEDTLS_SHA3_C)
270 {"sha3", mbedtls_sha3_self_test},
271#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100272#if defined(MBEDTLS_DES_C)
273 {"des", mbedtls_des_self_test},
274#endif
275#if defined(MBEDTLS_AES_C)
276 {"aes", mbedtls_aes_self_test},
277#endif
278#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
279 {"gcm", mbedtls_gcm_self_test},
280#endif
281#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
282 {"ccm", mbedtls_ccm_self_test},
283#endif
Ron Eldor9ab746c2018-07-15 09:33:07 +0300284#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
285 {"nist_kw", mbedtls_nist_kw_self_test},
286#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100287#if defined(MBEDTLS_CMAC_C)
288 {"cmac", mbedtls_cmac_self_test},
289#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300290#if defined(MBEDTLS_CHACHA20_C)
291 {"chacha20", mbedtls_chacha20_self_test},
292#endif
293#if defined(MBEDTLS_POLY1305_C)
294 {"poly1305", mbedtls_poly1305_self_test},
295#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200296#if defined(MBEDTLS_CHACHAPOLY_C)
297 {"chacha20-poly1305", mbedtls_chachapoly_self_test},
Daniel King4d8f87b2016-05-18 10:09:28 -0300298#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100299#if defined(MBEDTLS_BASE64_C)
300 {"base64", mbedtls_base64_self_test},
301#endif
302#if defined(MBEDTLS_BIGNUM_C)
303 {"mpi", mbedtls_mpi_self_test},
304#endif
305#if defined(MBEDTLS_RSA_C)
306 {"rsa", mbedtls_rsa_self_test},
307#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100308#if defined(MBEDTLS_CAMELLIA_C)
309 {"camellia", mbedtls_camellia_self_test},
310#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000311#if defined(MBEDTLS_ARIA_C)
312 {"aria", mbedtls_aria_self_test},
313#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100314#if defined(MBEDTLS_CTR_DRBG_C)
315 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
316#endif
317#if defined(MBEDTLS_HMAC_DRBG_C)
318 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
319#endif
320#if defined(MBEDTLS_ECP_C)
321 {"ecp", mbedtls_ecp_self_test},
322#endif
323#if defined(MBEDTLS_ECJPAKE_C)
324 {"ecjpake", mbedtls_ecjpake_self_test},
325#endif
326#if defined(MBEDTLS_DHM_C)
327 {"dhm", mbedtls_dhm_self_test},
328#endif
329#if defined(MBEDTLS_ENTROPY_C)
330 {"entropy", mbedtls_entropy_self_test_wrapper},
331#endif
332#if defined(MBEDTLS_PKCS5_C)
333 {"pkcs5", mbedtls_pkcs5_self_test},
334#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100335/* Heap test comes last */
336#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
337 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
338#endif
339 {NULL, NULL}
340};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100341#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100342
Paul Bakker5121ce52009-01-03 21:22:43 +0000343int main( int argc, char *argv[] )
344{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100345#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100346 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100347#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100348 char **argp;
349 int v = 1; /* v=1 for verbose mode */
350 int exclude_mode = 0;
351 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100352#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200353 unsigned char buf[1000000];
354#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200355 void *pointer;
356
357 /*
358 * The C standard doesn't guarantee that all-bits-0 is the representation
359 * of a NULL pointer. We do however use that in our code for initializing
360 * structures, which should work on every modern platform. Let's be sure.
361 */
362 memset( &pointer, 0, sizeof( void * ) );
363 if( pointer != NULL )
364 {
365 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000366 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200367 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200369 /*
Hanno Becker4ab38502018-10-16 13:22:44 +0100370 * The C standard allows padding bits in the representation
371 * of standard integer types, but our code does currently not
372 * support them.
373 *
374 * Here we check that the underlying C implementation doesn't
375 * use padding bits, and fail cleanly if it does.
376 *
377 * The check works by casting the maximum value representable
378 * by a given integer type into the unpadded integer type of the
379 * same bit-width and checking that it agrees with the maximum value
380 * of that unpadded type. For example, for a 4-byte int,
381 * MAX_INT should be 0x7fffffff in int32_t. This assumes that
382 * CHAR_BIT == 8, which is checked in check_config.h.
Hanno Beckerbaae59c2018-10-19 17:32:45 +0100383 *
384 * We assume that [u]intxx_t exist and that they don't
385 * have padding bits, as the standard requires.
Hanno Becker4ab38502018-10-16 13:22:44 +0100386 */
387
388#define CHECK_PADDING_SIGNED(TYPE, NAME) \
389 do \
390 { \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100391 if( sizeof( TYPE ) == 2 || sizeof( TYPE ) == 4 || \
392 sizeof( TYPE ) == 8 ) { \
393 if( ( sizeof( TYPE ) == 2 && \
394 (int16_t) NAME ## _MAX != 0x7FFF ) || \
395 ( sizeof( TYPE ) == 4 && \
396 (int32_t) NAME ## _MAX != 0x7FFFFFFF ) || \
397 ( sizeof( TYPE ) == 8 && \
398 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF ) ) \
399 { \
400 mbedtls_printf( "Type '" #TYPE "' has padding bits\n" );\
401 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
402 } \
403 } else { \
404 mbedtls_printf( "Padding checks only implemented for types of size 2, 4 or 8" \
Dave Rodgman8f5a29a2022-04-11 12:59:45 +0100405 " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n", \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100406 sizeof( TYPE ) ); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100407 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
408 } \
409 } while( 0 )
410
411#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
412 do \
413 { \
414 if( ( sizeof( TYPE ) == 2 && \
415 (uint16_t) NAME ## _MAX != 0xFFFF ) || \
416 ( sizeof( TYPE ) == 4 && \
417 (uint32_t) NAME ## _MAX != 0xFFFFFFFF ) || \
418 ( sizeof( TYPE ) == 8 && \
419 (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF ) ) \
420 { \
421 mbedtls_printf( "Type '" #TYPE "' has padding bits\n" ); \
422 mbedtls_exit( MBEDTLS_EXIT_FAILURE ); \
423 } \
424 } while( 0 )
425
Hanno Becker0d7dd3c2018-10-19 17:32:29 +0100426 CHECK_PADDING_SIGNED( short, SHRT );
427 CHECK_PADDING_SIGNED( int, INT );
428 CHECK_PADDING_SIGNED( long, LONG );
429 CHECK_PADDING_SIGNED( long long, LLONG );
430 CHECK_PADDING_SIGNED( ptrdiff_t, PTRDIFF );
Hanno Becker4ab38502018-10-16 13:22:44 +0100431
432 CHECK_PADDING_UNSIGNED( unsigned short, USHRT );
433 CHECK_PADDING_UNSIGNED( unsigned, UINT );
434 CHECK_PADDING_UNSIGNED( unsigned long, ULONG );
435 CHECK_PADDING_UNSIGNED( unsigned long long, ULLONG );
Hanno Becker0d7dd3c2018-10-19 17:32:29 +0100436 CHECK_PADDING_UNSIGNED( size_t, SIZE );
Hanno Becker4ab38502018-10-16 13:22:44 +0100437
438#undef CHECK_PADDING_SIGNED
439#undef CHECK_PADDING_UNSIGNED
440
441 /*
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200442 * Make sure we have a snprintf that correctly zero-terminates
443 */
444 if( run_test_snprintf() != 0 )
445 {
446 mbedtls_printf( "the snprintf implementation is broken\n" );
Janos Follath2e3aca22016-03-18 16:25:52 +0000447 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200448 }
449
Gilles Peskineff79d272017-12-20 18:09:27 +0100450 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
SimonB5a8afb82016-03-11 00:40:54 +0000451 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100452 if( strcmp( *argp, "--quiet" ) == 0 ||
453 strcmp( *argp, "-q" ) == 0 )
454 {
455 v = 0;
456 }
457 else if( strcmp( *argp, "--exclude" ) == 0 ||
458 strcmp( *argp, "-x" ) == 0 )
459 {
460 exclude_mode = 1;
461 }
462 else
463 break;
SimonB5a8afb82016-03-11 00:40:54 +0000464 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100465
466 if( v != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
472 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200473#endif
474
Gilles Peskineff79d272017-12-20 18:09:27 +0100475 if( *argp != NULL && exclude_mode == 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000476 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100477 /* Run the specified tests */
478 for( ; *argp != NULL; argp++ )
Gilles Peskine319ac802017-12-15 14:57:18 +0100479 {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100480 for( test = selftests; test->name != NULL; test++ )
481 {
482 if( !strcmp( *argp, test->name ) )
483 {
484 if( test->function( v ) != 0 )
485 {
486 suites_failed++;
487 }
488 suites_tested++;
489 break;
490 }
491 }
492 if( test->name == NULL )
493 {
494 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
495 suites_failed++;
496 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100497 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100498 }
499 else
500 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100501 /* Run all the tests except excluded ones */
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100502 for( test = selftests; test->name != NULL; test++ )
503 {
Gilles Peskineff79d272017-12-20 18:09:27 +0100504 if( exclude_mode )
505 {
506 char **excluded;
507 for( excluded = argp; *excluded != NULL; ++excluded )
508 {
509 if( !strcmp( *excluded, test->name ) )
510 break;
511 }
512 if( *excluded )
513 {
514 if( v )
515 mbedtls_printf( " Skip: %s\n", test->name );
516 continue;
517 }
518 }
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100519 if( test->function( v ) != 0 )
520 {
521 suites_failed++;
522 }
523 suites_tested++;
524 }
SimonB5a8afb82016-03-11 00:40:54 +0000525 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100526
Paul Bakker70940ca2016-07-14 14:30:45 +0100527#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100528 (void) exclude_mode;
Paul Bakker70940ca2016-07-14 14:30:45 +0100529 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
530#endif
531
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100532 if( v != 0 )
533 {
Simon Butcherf1547632016-03-14 23:09:39 +0000534 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
SimonB5a8afb82016-03-11 00:40:54 +0000535
536 if( suites_failed > 0)
537 {
538 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
539 }
540 else
541 {
542 mbedtls_printf( " [ All tests PASS ]\n\n" );
543 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000544#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 fflush( stdout ); getchar();
547#endif
548 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
SimonB5a8afb82016-03-11 00:40:54 +0000550 if( suites_failed > 0)
Janos Follath2e3aca22016-03-18 16:25:52 +0000551 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
SimonB5a8afb82016-03-11 00:40:54 +0000552
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200553 mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554}