blob: 598c66e144da96563a039a462a76e4e890ccae05 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/entropy.h"
Simon Butcherb6a73c92016-06-18 22:45:37 +010027#include "mbedtls/entropy_poll.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/hmac_drbg.h"
29#include "mbedtls/ctr_drbg.h"
30#include "mbedtls/dhm.h"
31#include "mbedtls/gcm.h"
32#include "mbedtls/ccm.h"
Robert Cragiedc5c7b92015-12-11 15:49:45 +000033#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/md2.h"
35#include "mbedtls/md4.h"
36#include "mbedtls/md5.h"
37#include "mbedtls/ripemd160.h"
38#include "mbedtls/sha1.h"
39#include "mbedtls/sha256.h"
40#include "mbedtls/sha512.h"
41#include "mbedtls/arc4.h"
42#include "mbedtls/des.h"
43#include "mbedtls/aes.h"
44#include "mbedtls/camellia.h"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000045#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030046#include "mbedtls/chacha20.h"
47#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020048#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/base64.h"
50#include "mbedtls/bignum.h"
51#include "mbedtls/rsa.h"
52#include "mbedtls/x509.h"
53#include "mbedtls/xtea.h"
54#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020056#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030058#include "mbedtls/nist_kw.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Rich Evans18b78c72015-02-11 14:06:19 +000060#include <string.h>
61
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020066#endif
67
Simon Butcher63cb97e2018-12-06 17:43:31 +000068
Gilles Peskine6fc21f62019-09-17 18:18:58 +020069#if defined MBEDTLS_SELF_TEST
70/* Sanity check for malloc. This is not expected to fail, and is rather
71 * intended to display potentially useful information about the platform,
72 * in particular the behavior of malloc(0). */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073static int calloc_self_test(int verbose)
Gilles Peskine6fc21f62019-09-17 18:18:58 +020074{
75 int failures = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 void *empty1 = mbedtls_calloc(0, 1);
77 void *empty2 = mbedtls_calloc(0, 1);
78 void *buffer1 = mbedtls_calloc(1, 1);
79 void *buffer2 = mbedtls_calloc(1, 1);
Gilles Peskine6fc21f62019-09-17 18:18:58 +020080
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 if (empty1 == NULL && empty2 == NULL) {
82 if (verbose) {
83 mbedtls_printf(" CALLOC(0): passed (NULL)\n");
84 }
85 } else if (empty1 == NULL || empty2 == NULL) {
86 if (verbose) {
87 mbedtls_printf(" CALLOC(0): failed (mix of NULL and non-NULL)\n");
88 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020089 ++failures;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 } else if (empty1 == empty2) {
91 if (verbose) {
92 mbedtls_printf(" CALLOC(0): passed (same non-null)\n");
93 }
94 } else {
95 if (verbose) {
96 mbedtls_printf(" CALLOC(0): passed (distinct non-null)\n");
97 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020098 }
99
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 if (buffer1 == NULL || buffer2 == NULL) {
101 if (verbose) {
102 mbedtls_printf(" CALLOC(1): failed (NULL)\n");
103 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200104 ++failures;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 } else if (buffer1 == buffer2) {
106 if (verbose) {
107 mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
108 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200109 ++failures;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 } else {
111 if (verbose) {
112 mbedtls_printf(" CALLOC(1): passed\n");
113 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200114 }
115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_free(buffer1);
117 buffer1 = mbedtls_calloc(1, 1);
118 if (buffer1 == NULL) {
119 if (verbose) {
120 mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
121 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200122 ++failures;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 } else {
124 if (verbose) {
125 mbedtls_printf(" CALLOC(1 again): passed\n");
126 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200127 }
128
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129 if (verbose) {
130 mbedtls_printf("\n");
131 }
132 mbedtls_free(empty1);
133 mbedtls_free(empty2);
134 mbedtls_free(buffer1);
135 mbedtls_free(buffer2);
136 return failures;
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200137}
138#endif /* MBEDTLS_SELF_TEST */
139
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100140static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200141{
142 int ret;
143 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200144 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 ret = mbedtls_snprintf(buf, n, "%s", "123");
147 if (ret < 0 || (size_t) ret >= n) {
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200148 ret = -1;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200149 }
150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
152 ref_ret != ret ||
153 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
154 return 1;
155 }
156
157 return 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200158}
159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160static int run_test_snprintf(void)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200161{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100162 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
163 test_snprintf(1, "", -1) != 0 ||
164 test_snprintf(2, "1", -1) != 0 ||
165 test_snprintf(3, "12", -1) != 0 ||
166 test_snprintf(4, "123", 3) != 0 ||
167 test_snprintf(5, "123", 3) != 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200168}
169
Simon Butcherb6a73c92016-06-18 22:45:37 +0100170/*
171 * Check if a seed file is present, and if not create one for the entropy
172 * self-test. If this fails, we attempt the test anyway, so no error is passed
173 * back.
174 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100175#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
176#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177static void create_entropy_seed_file(void)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100178{
179 int result;
180 size_t output_len = 0;
181 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
182
183 /* Attempt to read the entropy seed file. If this fails - attempt to write
184 * to the file to ensure one is present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 result = mbedtls_platform_std_nv_seed_read(seed_value,
186 MBEDTLS_ENTROPY_BLOCK_SIZE);
187 if (0 == result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100188 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100190
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 result = mbedtls_platform_entropy_poll(NULL,
192 seed_value,
193 MBEDTLS_ENTROPY_BLOCK_SIZE,
194 &output_len);
195 if (0 != result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100196 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100200 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Simon Butcherb6a73c92016-06-18 22:45:37 +0100204}
205#endif
206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207int mbedtls_entropy_self_test_wrapper(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100208{
209#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 create_entropy_seed_file();
Gilles Peskine319ac802017-12-15 14:57:18 +0100211#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 return mbedtls_entropy_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100213}
214#endif
215
216#if defined(MBEDTLS_SELF_TEST)
217#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100219{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 if (verbose != 0) {
Gilles Peskine319ac802017-12-15 14:57:18 +0100221#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 mbedtls_memory_buffer_alloc_status();
Gilles Peskine319ac802017-12-15 14:57:18 +0100223#endif
224 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 mbedtls_memory_buffer_alloc_free();
226 return mbedtls_memory_buffer_alloc_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100227}
228#endif
229
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230typedef struct {
Gilles Peskine319ac802017-12-15 14:57:18 +0100231 const char *name;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100232 int (*function)(int);
Gilles Peskine319ac802017-12-15 14:57:18 +0100233} selftest_t;
234
235const selftest_t selftests[] =
236{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 { "calloc", calloc_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100238#if defined(MBEDTLS_MD2_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 { "md2", mbedtls_md2_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100240#endif
241#if defined(MBEDTLS_MD4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 { "md4", mbedtls_md4_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100243#endif
244#if defined(MBEDTLS_MD5_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 { "md5", mbedtls_md5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100246#endif
247#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 { "ripemd160", mbedtls_ripemd160_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100249#endif
250#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 { "sha1", mbedtls_sha1_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100252#endif
253#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 { "sha256", mbedtls_sha256_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100255#endif
256#if defined(MBEDTLS_SHA512_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257 { "sha512", mbedtls_sha512_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100258#endif
259#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 { "arc4", mbedtls_arc4_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100261#endif
262#if defined(MBEDTLS_DES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 { "des", mbedtls_des_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100264#endif
265#if defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 { "aes", mbedtls_aes_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100267#endif
268#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269 { "gcm", mbedtls_gcm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100270#endif
271#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 { "ccm", mbedtls_ccm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100273#endif
Ron Eldor9ab746c2018-07-15 09:33:07 +0300274#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275 { "nist_kw", mbedtls_nist_kw_self_test },
Ron Eldor9ab746c2018-07-15 09:33:07 +0300276#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100277#if defined(MBEDTLS_CMAC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 { "cmac", mbedtls_cmac_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100279#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300280#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100281 { "chacha20", mbedtls_chacha20_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300282#endif
283#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 { "poly1305", mbedtls_poly1305_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300285#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200286#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287 { "chacha20-poly1305", mbedtls_chachapoly_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300288#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100289#if defined(MBEDTLS_BASE64_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290 { "base64", mbedtls_base64_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100291#endif
292#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100293 { "mpi", mbedtls_mpi_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100294#endif
295#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 { "rsa", mbedtls_rsa_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100297#endif
298#if defined(MBEDTLS_X509_USE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 { "x509", mbedtls_x509_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100300#endif
301#if defined(MBEDTLS_XTEA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100302 { "xtea", mbedtls_xtea_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100303#endif
304#if defined(MBEDTLS_CAMELLIA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 { "camellia", mbedtls_camellia_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100306#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000307#if defined(MBEDTLS_ARIA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100308 { "aria", mbedtls_aria_self_test },
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000309#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100310#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 { "ctr_drbg", mbedtls_ctr_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100312#endif
313#if defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 { "hmac_drbg", mbedtls_hmac_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100315#endif
316#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100317 { "ecp", mbedtls_ecp_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100318#endif
319#if defined(MBEDTLS_ECJPAKE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320 { "ecjpake", mbedtls_ecjpake_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100321#endif
322#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 { "dhm", mbedtls_dhm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100324#endif
325#if defined(MBEDTLS_ENTROPY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 { "entropy", mbedtls_entropy_self_test_wrapper },
Gilles Peskine319ac802017-12-15 14:57:18 +0100327#endif
328#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100329 { "pkcs5", mbedtls_pkcs5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100330#endif
331/* Slower test after the faster ones */
332#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 { "timing", mbedtls_timing_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100334#endif
335/* Heap test comes last */
336#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100338#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 { NULL, NULL }
Gilles Peskine319ac802017-12-15 14:57:18 +0100340};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100341#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000344{
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 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 memset(&pointer, 0, sizeof(void *));
363 if (pointer != NULL) {
364 mbedtls_printf("all-bits-zero is not a NULL pointer\n");
365 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200366 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200368 /*
369 * Make sure we have a snprintf that correctly zero-terminates
370 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 if (run_test_snprintf() != 0) {
372 mbedtls_printf("the snprintf implementation is broken\n");
373 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200374 }
375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
377 if (strcmp(*argp, "--quiet") == 0 ||
378 strcmp(*argp, "-q") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100379 v = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100380 } else if (strcmp(*argp, "--exclude") == 0 ||
381 strcmp(*argp, "-x") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100382 exclude_mode = 1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100384 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 }
SimonB5a8afb82016-03-11 00:40:54 +0000386 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 if (v != 0) {
389 mbedtls_printf("\n");
390 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Paul Bakker6e339b52013-07-03 13:37:05 +0200396#endif
397
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 if (*argp != NULL && exclude_mode == 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100399 /* Run the specified tests */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100400 for (; *argp != NULL; argp++) {
401 for (test = selftests; test->name != NULL; test++) {
402 if (!strcmp(*argp, test->name)) {
403 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100404 suites_failed++;
405 }
406 suites_tested++;
407 break;
408 }
409 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 if (test->name == NULL) {
411 mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp);
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100412 suites_failed++;
413 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100414 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100416 /* Run all the tests except excluded ones */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 for (test = selftests; test->name != NULL; test++) {
418 if (exclude_mode) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100419 char **excluded;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100420 for (excluded = argp; *excluded != NULL; ++excluded) {
421 if (!strcmp(*excluded, test->name)) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100422 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100424 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 if (*excluded) {
426 if (v) {
427 mbedtls_printf(" Skip: %s\n", test->name);
428 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100429 continue;
430 }
431 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100433 suites_failed++;
434 }
435 suites_tested++;
436 }
SimonB5a8afb82016-03-11 00:40:54 +0000437 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100438
Paul Bakker70940ca2016-07-14 14:30:45 +0100439#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100440 (void) exclude_mode;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
Paul Bakker70940ca2016-07-14 14:30:45 +0100442#endif
443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 if (v != 0) {
445 mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
SimonB5a8afb82016-03-11 00:40:54 +0000446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 if (suites_failed > 0) {
448 mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
449 } else {
450 mbedtls_printf(" [ All tests PASS ]\n\n");
SimonB5a8afb82016-03-11 00:40:54 +0000451 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000452#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 mbedtls_printf(" Press Enter to exit this program.\n");
454 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000455#endif
456 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000457
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 if (suites_failed > 0) {
459 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
460 }
SimonB5a8afb82016-03-11 00:40:54 +0000461
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100462 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
Paul Bakker5121ce52009-01-03 21:22:43 +0000463}