blob: 56b711e56802fc756e8fa51eb0bf10a58860b986 [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
Rich Evans18b78c72015-02-11 14:06:19 +000053#include <string.h>
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056# include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000057#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020058# include <stdio.h>
59# include <stdlib.h>
60# define mbedtls_calloc calloc
61# define mbedtls_free free
62# define mbedtls_printf printf
63# define mbedtls_snprintf snprintf
64# define mbedtls_exit exit
65# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
66# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Rich Evans18b78c72015-02-11 14:06:19 +000067#endif
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020070# include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020071#endif
72
Gilles Peskine6fc21f62019-09-17 18:18:58 +020073#if defined MBEDTLS_SELF_TEST
74/* Sanity check for malloc. This is not expected to fail, and is rather
75 * intended to display potentially useful information about the platform,
76 * in particular the behavior of malloc(0). */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020077static int calloc_self_test(int verbose)
Gilles Peskine6fc21f62019-09-17 18:18:58 +020078{
79 int failures = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020080 void *empty1 = mbedtls_calloc(0, 1);
81 void *empty2 = mbedtls_calloc(0, 1);
82 void *buffer1 = mbedtls_calloc(1, 1);
83 void *buffer2 = mbedtls_calloc(1, 1);
Gilles Peskine6fc21f62019-09-17 18:18:58 +020084 uintptr_t old_buffer1;
85
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020086 if (empty1 == NULL && empty2 == NULL) {
87 if (verbose)
88 mbedtls_printf(" CALLOC(0): passed (NULL)\n");
89 } else if (empty1 == NULL || empty2 == NULL) {
90 if (verbose)
91 mbedtls_printf(" CALLOC(0): failed (mix of NULL and non-NULL)\n");
Gilles Peskine6fc21f62019-09-17 18:18:58 +020092 ++failures;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020093 } else if (empty1 == empty2) {
94 if (verbose)
95 mbedtls_printf(" CALLOC(0): passed (same non-null)\n");
96 } else {
97 if (verbose)
98 mbedtls_printf(" CALLOC(0): passed (distinct non-null)\n");
Gilles Peskine6fc21f62019-09-17 18:18:58 +020099 }
100
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101 if (buffer1 == NULL || buffer2 == NULL) {
102 if (verbose)
103 mbedtls_printf(" CALLOC(1): failed (NULL)\n");
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200104 ++failures;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200105 } else if (buffer1 == buffer2) {
106 if (verbose)
107 mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200108 ++failures;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200109 } else {
110 if (verbose)
111 mbedtls_printf(" CALLOC(1): passed\n");
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200112 }
113
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200114 old_buffer1 = (uintptr_t)buffer1;
115 mbedtls_free(buffer1);
116 buffer1 = mbedtls_calloc(1, 1);
117 if (buffer1 == NULL) {
118 if (verbose)
119 mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200120 ++failures;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200121 } else {
122 if (verbose)
123 mbedtls_printf(" CALLOC(1 again): passed (%s address)\n",
124 (uintptr_t)old_buffer1 == (uintptr_t)buffer1 ?
125 "same" :
126 "different");
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200127 }
128
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129 if (verbose)
130 mbedtls_printf("\n");
131 mbedtls_free(empty1);
132 mbedtls_free(empty2);
133 mbedtls_free(buffer1);
134 mbedtls_free(buffer2);
135 return failures;
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200136}
137#endif /* MBEDTLS_SELF_TEST */
138
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200139static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200140{
141 int ret;
142 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200143 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200144
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145 ret = mbedtls_snprintf(buf, n, "%s", "123");
146 if (ret < 0 || (size_t)ret >= n)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200147 ret = -1;
148
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 || ref_ret != ret ||
150 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
151 return 1;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200152 }
153
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200154 return 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200155}
156
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200157static int run_test_snprintf(void)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200158{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200159 return (test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
160 test_snprintf(1, "", -1) != 0 || test_snprintf(2, "1", -1) != 0 ||
161 test_snprintf(3, "12", -1) != 0 ||
162 test_snprintf(4, "123", 3) != 0 || test_snprintf(5, "123", 3) != 0);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200163}
164
Simon Butcherb6a73c92016-06-18 22:45:37 +0100165/*
166 * Check if a seed file is present, and if not create one for the entropy
167 * self-test. If this fails, we attempt the test anyway, so no error is passed
168 * back.
169 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100170#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200171# if defined(MBEDTLS_ENTROPY_NV_SEED) && \
172 !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
173static void create_entropy_seed_file(void)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100174{
175 int result;
176 size_t output_len = 0;
177 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
178
179 /* Attempt to read the entropy seed file. If this fails - attempt to write
180 * to the file to ensure one is present. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200181 result = mbedtls_platform_std_nv_seed_read(seed_value,
182 MBEDTLS_ENTROPY_BLOCK_SIZE);
183 if (0 == result)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100184 return;
185
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200186 result = mbedtls_platform_entropy_poll(
187 NULL, seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE, &output_len);
188 if (0 != result)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100189 return;
190
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200191 if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100192 return;
193
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200194 mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Simon Butcherb6a73c92016-06-18 22:45:37 +0100195}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196# endif
Simon Butcherb6a73c92016-06-18 22:45:37 +0100197
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200198int mbedtls_entropy_self_test_wrapper(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100199{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200200# if defined(MBEDTLS_ENTROPY_NV_SEED) && \
201 !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
202 create_entropy_seed_file();
203# endif
204 return mbedtls_entropy_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100205}
206#endif
207
208#if defined(MBEDTLS_SELF_TEST)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200209# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
210int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100211{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212 if (verbose != 0) {
213# if defined(MBEDTLS_MEMORY_DEBUG)
214 mbedtls_memory_buffer_alloc_status();
215# endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100216 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 mbedtls_memory_buffer_alloc_free();
218 return mbedtls_memory_buffer_alloc_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100219}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200220# endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100221
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200222typedef struct {
Gilles Peskine319ac802017-12-15 14:57:18 +0100223 const char *name;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200224 int (*function)(int);
Gilles Peskine319ac802017-12-15 14:57:18 +0100225} selftest_t;
226
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200227const selftest_t selftests[] = {
228 { "calloc", calloc_self_test },
229# if defined(MBEDTLS_MD5_C)
230 { "md5", mbedtls_md5_self_test },
231# endif
232# if defined(MBEDTLS_RIPEMD160_C)
233 { "ripemd160", mbedtls_ripemd160_self_test },
234# endif
235# if defined(MBEDTLS_SHA1_C)
236 { "sha1", mbedtls_sha1_self_test },
237# endif
238# if defined(MBEDTLS_SHA256_C)
239 { "sha256", mbedtls_sha256_self_test },
240# endif
241# if defined(MBEDTLS_SHA512_C)
242 { "sha512", mbedtls_sha512_self_test },
243# endif
244# if defined(MBEDTLS_DES_C)
245 { "des", mbedtls_des_self_test },
246# endif
247# if defined(MBEDTLS_AES_C)
248 { "aes", mbedtls_aes_self_test },
249# endif
250# if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
251 { "gcm", mbedtls_gcm_self_test },
252# endif
253# if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
254 { "ccm", mbedtls_ccm_self_test },
255# endif
256# if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
257 { "nist_kw", mbedtls_nist_kw_self_test },
258# endif
259# if defined(MBEDTLS_CMAC_C)
260 { "cmac", mbedtls_cmac_self_test },
261# endif
262# if defined(MBEDTLS_CHACHA20_C)
263 { "chacha20", mbedtls_chacha20_self_test },
264# endif
265# if defined(MBEDTLS_POLY1305_C)
266 { "poly1305", mbedtls_poly1305_self_test },
267# endif
268# if defined(MBEDTLS_CHACHAPOLY_C)
269 { "chacha20-poly1305", mbedtls_chachapoly_self_test },
270# endif
271# if defined(MBEDTLS_BASE64_C)
272 { "base64", mbedtls_base64_self_test },
273# endif
274# if defined(MBEDTLS_BIGNUM_C)
275 { "mpi", mbedtls_mpi_self_test },
276# endif
277# if defined(MBEDTLS_RSA_C)
278 { "rsa", mbedtls_rsa_self_test },
279# endif
280# if defined(MBEDTLS_CAMELLIA_C)
281 { "camellia", mbedtls_camellia_self_test },
282# endif
283# if defined(MBEDTLS_ARIA_C)
284 { "aria", mbedtls_aria_self_test },
285# endif
286# if defined(MBEDTLS_CTR_DRBG_C)
287 { "ctr_drbg", mbedtls_ctr_drbg_self_test },
288# endif
289# if defined(MBEDTLS_HMAC_DRBG_C)
290 { "hmac_drbg", mbedtls_hmac_drbg_self_test },
291# endif
292# if defined(MBEDTLS_ECP_C)
293 { "ecp", mbedtls_ecp_self_test },
294# endif
295# if defined(MBEDTLS_ECJPAKE_C)
296 { "ecjpake", mbedtls_ecjpake_self_test },
297# endif
298# if defined(MBEDTLS_DHM_C)
299 { "dhm", mbedtls_dhm_self_test },
300# endif
301# if defined(MBEDTLS_ENTROPY_C)
302 { "entropy", mbedtls_entropy_self_test_wrapper },
303# endif
304# if defined(MBEDTLS_PKCS5_C)
305 { "pkcs5", mbedtls_pkcs5_self_test },
306# endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100307/* Heap test comes last */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200308# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
309 { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
310# endif
311 { NULL, NULL }
Gilles Peskine319ac802017-12-15 14:57:18 +0100312};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100313#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100314
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200315int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000316{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100317#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100318 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100319#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100320 char **argp;
321 int v = 1; /* v=1 for verbose mode */
322 int exclude_mode = 0;
323 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100324#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200325 unsigned char buf[1000000];
326#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200327 void *pointer;
328
329 /*
330 * The C standard doesn't guarantee that all-bits-0 is the representation
331 * of a NULL pointer. We do however use that in our code for initializing
332 * structures, which should work on every modern platform. Let's be sure.
333 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334 memset(&pointer, 0, sizeof(void *));
335 if (pointer != NULL) {
336 mbedtls_printf("all-bits-zero is not a NULL pointer\n");
337 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200338 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200340 /*
341 * Make sure we have a snprintf that correctly zero-terminates
342 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200343 if (run_test_snprintf() != 0) {
344 mbedtls_printf("the snprintf implementation is broken\n");
345 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200346 }
347
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200348 for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
349 if (strcmp(*argp, "--quiet") == 0 || strcmp(*argp, "-q") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100350 v = 0;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351 } else if (strcmp(*argp, "--exclude") == 0 ||
352 strcmp(*argp, "-x") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100353 exclude_mode = 1;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200354 } else
Gilles Peskineff79d272017-12-20 18:09:27 +0100355 break;
SimonB5a8afb82016-03-11 00:40:54 +0000356 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100357
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200358 if (v != 0)
359 mbedtls_printf("\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000362
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200363# if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
364 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
365# endif
Paul Bakker6e339b52013-07-03 13:37:05 +0200366
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200367 if (*argp != NULL && exclude_mode == 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100368 /* Run the specified tests */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200369 for (; *argp != NULL; argp++) {
370 for (test = selftests; test->name != NULL; test++) {
371 if (!strcmp(*argp, test->name)) {
372 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100373 suites_failed++;
374 }
375 suites_tested++;
376 break;
377 }
378 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200379 if (test->name == NULL) {
380 mbedtls_printf(" Test suite %s not available -> failed\n\n",
381 *argp);
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100382 suites_failed++;
383 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100384 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200385 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100386 /* Run all the tests except excluded ones */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387 for (test = selftests; test->name != NULL; test++) {
388 if (exclude_mode) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100389 char **excluded;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200390 for (excluded = argp; *excluded != NULL; ++excluded) {
391 if (!strcmp(*excluded, test->name))
Gilles Peskineff79d272017-12-20 18:09:27 +0100392 break;
393 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394 if (*excluded) {
395 if (v)
396 mbedtls_printf(" Skip: %s\n", test->name);
Gilles Peskineff79d272017-12-20 18:09:27 +0100397 continue;
398 }
399 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200400 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100401 suites_failed++;
402 }
403 suites_tested++;
404 }
SimonB5a8afb82016-03-11 00:40:54 +0000405 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100406
Paul Bakker70940ca2016-07-14 14:30:45 +0100407#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200408 (void)exclude_mode;
409 mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
Paul Bakker70940ca2016-07-14 14:30:45 +0100410#endif
411
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200412 if (v != 0) {
413 mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
SimonB5a8afb82016-03-11 00:40:54 +0000414
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200415 if (suites_failed > 0) {
416 mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
417 } else {
418 mbedtls_printf(" [ All tests PASS ]\n\n");
SimonB5a8afb82016-03-11 00:40:54 +0000419 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000420#if defined(_WIN32)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200421 mbedtls_printf(" Press Enter to exit this program.\n");
422 fflush(stdout);
423 getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000424#endif
425 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200427 if (suites_failed > 0)
428 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
SimonB5a8afb82016-03-11 00:40:54 +0000429
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200430 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
Paul Bakker5121ce52009-01-03 21:22:43 +0000431}