blob: f45eb8539a7abee1c0dbd72089676233a901c8c3 [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);
Andrzej Kurekc8bf0592023-05-04 17:29:55 -040080 unsigned int buffer_3_size = 256;
81 unsigned int buffer_4_size = 4097; /* Allocate more than the usual page size */
82 unsigned char *buffer3 = mbedtls_calloc(buffer_3_size, 1);
83 unsigned char *buffer4 = mbedtls_calloc(buffer_4_size, 1);
Andrzej Kurekf35490e2023-07-14 10:12:11 -040084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 if (empty1 == NULL && empty2 == NULL) {
86 if (verbose) {
Andrzej Kurek5ffea9d2023-07-14 09:53:08 -040087 mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 }
89 } else if (empty1 == NULL || empty2 == NULL) {
90 if (verbose) {
Andrzej Kurek5ffea9d2023-07-14 09:53:08 -040091 mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020093 ++failures;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 } else if (empty1 == empty2) {
95 if (verbose) {
Andrzej Kurek5ffea9d2023-07-14 09:53:08 -040096 mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 }
98 } else {
99 if (verbose) {
Andrzej Kurek5ffea9d2023-07-14 09:53:08 -0400100 mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n");
101 }
102 }
103
104 mbedtls_free(empty1);
105 mbedtls_free(empty2);
106
107 empty1 = mbedtls_calloc(1, 0);
108 empty2 = mbedtls_calloc(1, 0);
109 if (empty1 == NULL && empty2 == NULL) {
110 if (verbose) {
111 mbedtls_printf(" CALLOC(1,0): passed (NULL)\n");
112 }
113 } else if (empty1 == NULL || empty2 == NULL) {
114 if (verbose) {
115 mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n");
116 }
117 ++failures;
118 } else if (empty1 == empty2) {
119 if (verbose) {
120 mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n");
121 }
122 } else {
123 if (verbose) {
124 mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200126 }
127
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 if (buffer1 == NULL || buffer2 == NULL) {
129 if (verbose) {
130 mbedtls_printf(" CALLOC(1): failed (NULL)\n");
131 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200132 ++failures;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 } else if (buffer1 == buffer2) {
134 if (verbose) {
135 mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
136 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200137 ++failures;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 } else {
139 if (verbose) {
140 mbedtls_printf(" CALLOC(1): passed\n");
141 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200142 }
143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 mbedtls_free(buffer1);
145 buffer1 = mbedtls_calloc(1, 1);
146 if (buffer1 == NULL) {
147 if (verbose) {
148 mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
149 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200150 ++failures;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 } else {
152 if (verbose) {
153 mbedtls_printf(" CALLOC(1 again): passed\n");
154 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200155 }
156
Andrzej Kurekc8bf0592023-05-04 17:29:55 -0400157 for (unsigned int i = 0; i < buffer_3_size; i++) {
Andrzej Kurek5ffea9d2023-07-14 09:53:08 -0400158 if (buffer3[i] != 0) {
159 ++failures;
160 if (verbose) {
Andrzej Kurekc8bf0592023-05-04 17:29:55 -0400161 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
162 buffer_3_size);
163 }
164 break;
165 }
166 }
167
168 for (unsigned int i = 0; i < buffer_4_size; i++) {
169 if (buffer4[i] != 0) {
170 ++failures;
171 if (verbose) {
172 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
173 buffer_4_size);
Andrzej Kurek5ffea9d2023-07-14 09:53:08 -0400174 }
175 break;
176 }
177 }
178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if (verbose) {
180 mbedtls_printf("\n");
181 }
182 mbedtls_free(empty1);
183 mbedtls_free(empty2);
184 mbedtls_free(buffer1);
185 mbedtls_free(buffer2);
Andrzej Kurek5ffea9d2023-07-14 09:53:08 -0400186 mbedtls_free(buffer3);
Andrzej Kurekc8bf0592023-05-04 17:29:55 -0400187 mbedtls_free(buffer4);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 return failures;
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200189}
190#endif /* MBEDTLS_SELF_TEST */
191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200193{
194 int ret;
195 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200196 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 ret = mbedtls_snprintf(buf, n, "%s", "123");
199 if (ret < 0 || (size_t) ret >= n) {
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200200 ret = -1;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200201 }
202
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
204 ref_ret != ret ||
205 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
206 return 1;
207 }
208
209 return 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200210}
211
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212static int run_test_snprintf(void)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200213{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
215 test_snprintf(1, "", -1) != 0 ||
216 test_snprintf(2, "1", -1) != 0 ||
217 test_snprintf(3, "12", -1) != 0 ||
218 test_snprintf(4, "123", 3) != 0 ||
219 test_snprintf(5, "123", 3) != 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200220}
221
Simon Butcherb6a73c92016-06-18 22:45:37 +0100222/*
223 * Check if a seed file is present, and if not create one for the entropy
224 * self-test. If this fails, we attempt the test anyway, so no error is passed
225 * back.
226 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100227#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
228#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229static void create_entropy_seed_file(void)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100230{
231 int result;
232 size_t output_len = 0;
233 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
234
235 /* Attempt to read the entropy seed file. If this fails - attempt to write
236 * to the file to ensure one is present. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 result = mbedtls_platform_std_nv_seed_read(seed_value,
238 MBEDTLS_ENTROPY_BLOCK_SIZE);
239 if (0 == result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100240 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 result = mbedtls_platform_entropy_poll(NULL,
244 seed_value,
245 MBEDTLS_ENTROPY_BLOCK_SIZE,
246 &output_len);
247 if (0 != result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100248 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100250
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100252 return;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100254
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255 mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Simon Butcherb6a73c92016-06-18 22:45:37 +0100256}
257#endif
258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259int mbedtls_entropy_self_test_wrapper(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100260{
261#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 create_entropy_seed_file();
Gilles Peskine319ac802017-12-15 14:57:18 +0100263#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return mbedtls_entropy_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100265}
266#endif
267
268#if defined(MBEDTLS_SELF_TEST)
269#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100271{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 if (verbose != 0) {
Gilles Peskine319ac802017-12-15 14:57:18 +0100273#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100274 mbedtls_memory_buffer_alloc_status();
Gilles Peskine319ac802017-12-15 14:57:18 +0100275#endif
276 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 mbedtls_memory_buffer_alloc_free();
278 return mbedtls_memory_buffer_alloc_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100279}
280#endif
281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282typedef struct {
Gilles Peskine319ac802017-12-15 14:57:18 +0100283 const char *name;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100284 int (*function)(int);
Gilles Peskine319ac802017-12-15 14:57:18 +0100285} selftest_t;
286
287const selftest_t selftests[] =
288{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 { "calloc", calloc_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100290#if defined(MBEDTLS_MD2_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 { "md2", mbedtls_md2_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100292#endif
293#if defined(MBEDTLS_MD4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 { "md4", mbedtls_md4_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100295#endif
296#if defined(MBEDTLS_MD5_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 { "md5", mbedtls_md5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100298#endif
299#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 { "ripemd160", mbedtls_ripemd160_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100301#endif
302#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100303 { "sha1", mbedtls_sha1_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100304#endif
305#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 { "sha256", mbedtls_sha256_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100307#endif
308#if defined(MBEDTLS_SHA512_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100309 { "sha512", mbedtls_sha512_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100310#endif
311#if defined(MBEDTLS_ARC4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 { "arc4", mbedtls_arc4_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100313#endif
314#if defined(MBEDTLS_DES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100315 { "des", mbedtls_des_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100316#endif
317#if defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 { "aes", mbedtls_aes_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100319#endif
320#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 { "gcm", mbedtls_gcm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100322#endif
323#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 { "ccm", mbedtls_ccm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100325#endif
Ron Eldor9ab746c2018-07-15 09:33:07 +0300326#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 { "nist_kw", mbedtls_nist_kw_self_test },
Ron Eldor9ab746c2018-07-15 09:33:07 +0300328#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100329#if defined(MBEDTLS_CMAC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100330 { "cmac", mbedtls_cmac_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100331#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300332#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 { "chacha20", mbedtls_chacha20_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300334#endif
335#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 { "poly1305", mbedtls_poly1305_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300337#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200338#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 { "chacha20-poly1305", mbedtls_chachapoly_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300340#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100341#if defined(MBEDTLS_BASE64_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 { "base64", mbedtls_base64_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100343#endif
344#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 { "mpi", mbedtls_mpi_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100346#endif
347#if defined(MBEDTLS_RSA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 { "rsa", mbedtls_rsa_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100349#endif
350#if defined(MBEDTLS_X509_USE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 { "x509", mbedtls_x509_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100352#endif
353#if defined(MBEDTLS_XTEA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 { "xtea", mbedtls_xtea_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100355#endif
356#if defined(MBEDTLS_CAMELLIA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 { "camellia", mbedtls_camellia_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100358#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000359#if defined(MBEDTLS_ARIA_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 { "aria", mbedtls_aria_self_test },
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000361#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100362#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 { "ctr_drbg", mbedtls_ctr_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100364#endif
365#if defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 { "hmac_drbg", mbedtls_hmac_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100367#endif
368#if defined(MBEDTLS_ECP_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 { "ecp", mbedtls_ecp_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100370#endif
371#if defined(MBEDTLS_ECJPAKE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100372 { "ecjpake", mbedtls_ecjpake_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100373#endif
374#if defined(MBEDTLS_DHM_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 { "dhm", mbedtls_dhm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100376#endif
377#if defined(MBEDTLS_ENTROPY_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100378 { "entropy", mbedtls_entropy_self_test_wrapper },
Gilles Peskine319ac802017-12-15 14:57:18 +0100379#endif
380#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100381 { "pkcs5", mbedtls_pkcs5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100382#endif
383/* Slower test after the faster ones */
384#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100385 { "timing", mbedtls_timing_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100386#endif
387/* Heap test comes last */
388#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100389 { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100390#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100391 { NULL, NULL }
Gilles Peskine319ac802017-12-15 14:57:18 +0100392};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100393#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100394
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000396{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100397#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100398 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100399#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100400 char **argp;
401 int v = 1; /* v=1 for verbose mode */
402 int exclude_mode = 0;
403 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100404#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200405 unsigned char buf[1000000];
406#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200407 void *pointer;
Tom Cosgrove9149e122023-03-18 14:49:07 +0000408#if defined(_WIN32)
409 int ci = 0; /* ci = 1 => running in CI, so don't wait for a key press */
410#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200411
412 /*
413 * The C standard doesn't guarantee that all-bits-0 is the representation
414 * of a NULL pointer. We do however use that in our code for initializing
415 * structures, which should work on every modern platform. Let's be sure.
416 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100417 memset(&pointer, 0, sizeof(void *));
418 if (pointer != NULL) {
419 mbedtls_printf("all-bits-zero is not a NULL pointer\n");
420 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200421 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000422
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200423 /*
424 * Make sure we have a snprintf that correctly zero-terminates
425 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 if (run_test_snprintf() != 0) {
427 mbedtls_printf("the snprintf implementation is broken\n");
428 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200429 }
430
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100431 for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
432 if (strcmp(*argp, "--quiet") == 0 ||
433 strcmp(*argp, "-q") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100434 v = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 } else if (strcmp(*argp, "--exclude") == 0 ||
436 strcmp(*argp, "-x") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100437 exclude_mode = 1;
Tom Cosgrove9149e122023-03-18 14:49:07 +0000438#if defined(_WIN32)
439 } else if (strcmp(*argp, "--ci") == 0) {
440 ci = 1;
441#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100442 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100443 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 }
SimonB5a8afb82016-03-11 00:40:54 +0000445 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 if (v != 0) {
448 mbedtls_printf("\n");
449 }
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)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100454 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Paul Bakker6e339b52013-07-03 13:37:05 +0200455#endif
456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 if (*argp != NULL && exclude_mode == 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100458 /* Run the specified tests */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 for (; *argp != NULL; argp++) {
460 for (test = selftests; test->name != NULL; test++) {
461 if (!strcmp(*argp, test->name)) {
462 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100463 suites_failed++;
464 }
465 suites_tested++;
466 break;
467 }
468 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 if (test->name == NULL) {
470 mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp);
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100471 suites_failed++;
472 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100473 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100475 /* Run all the tests except excluded ones */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100476 for (test = selftests; test->name != NULL; test++) {
477 if (exclude_mode) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100478 char **excluded;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 for (excluded = argp; *excluded != NULL; ++excluded) {
480 if (!strcmp(*excluded, test->name)) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100481 break;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100482 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100483 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100484 if (*excluded) {
485 if (v) {
486 mbedtls_printf(" Skip: %s\n", test->name);
487 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100488 continue;
489 }
490 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100492 suites_failed++;
493 }
494 suites_tested++;
495 }
SimonB5a8afb82016-03-11 00:40:54 +0000496 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100497
Paul Bakker70940ca2016-07-14 14:30:45 +0100498#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100499 (void) exclude_mode;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100500 mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
Paul Bakker70940ca2016-07-14 14:30:45 +0100501#endif
502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 if (v != 0) {
504 mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
SimonB5a8afb82016-03-11 00:40:54 +0000505
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100506 if (suites_failed > 0) {
507 mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
508 } else {
509 mbedtls_printf(" [ All tests PASS ]\n\n");
SimonB5a8afb82016-03-11 00:40:54 +0000510 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000511#if defined(_WIN32)
Tom Cosgrove9149e122023-03-18 14:49:07 +0000512 if (!ci) {
513 mbedtls_printf(" Press Enter to exit this program.\n");
514 fflush(stdout); getchar();
515 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000516#endif
517 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100519 if (suites_failed > 0) {
520 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
521 }
SimonB5a8afb82016-03-11 00:40:54 +0000522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
Paul Bakker5121ce52009-01-03 21:22:43 +0000524}