blob: 372a84dc791b063a72c9ed59b21e9ee63645f9fd [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/entropy.h"
13#include "mbedtls/hmac_drbg.h"
14#include "mbedtls/ctr_drbg.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/gcm.h"
16#include "mbedtls/ccm.h"
Robert Cragiedc5c7b92015-12-11 15:49:45 +000017#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/md5.h"
19#include "mbedtls/ripemd160.h"
20#include "mbedtls/sha1.h"
21#include "mbedtls/sha256.h"
22#include "mbedtls/sha512.h"
Pol Henarejos7dbd5d12022-05-20 20:42:33 +020023#include "mbedtls/sha3.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/aes.h"
25#include "mbedtls/camellia.h"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000026#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030027#include "mbedtls/chacha20.h"
28#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020029#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/base64.h"
31#include "mbedtls/bignum.h"
32#include "mbedtls/rsa.h"
33#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020036#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030038#include "mbedtls/nist_kw.h"
Dave Rodgman8f5a29a2022-04-11 12:59:45 +010039#include "mbedtls/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Hanno Becker4ab38502018-10-16 13:22:44 +010041#include <limits.h>
Rich Evans18b78c72015-02-11 14:06:19 +000042#include <string.h>
43
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020048#endif
49
Simon Butcher63cb97e2018-12-06 17:43:31 +000050
Gilles Peskine6fc21f62019-09-17 18:18:58 +020051#if defined MBEDTLS_SELF_TEST
52/* Sanity check for malloc. This is not expected to fail, and is rather
53 * intended to display potentially useful information about the platform,
54 * in particular the behavior of malloc(0). */
Gilles Peskine449bd832023-01-11 14:50:10 +010055static int calloc_self_test(int verbose)
Gilles Peskine6fc21f62019-09-17 18:18:58 +020056{
57 int failures = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010058 void *empty1 = mbedtls_calloc(0, 1);
59 void *empty2 = mbedtls_calloc(0, 1);
60 void *buffer1 = mbedtls_calloc(1, 1);
61 void *buffer2 = mbedtls_calloc(1, 1);
Andrzej Kureke35f3a22023-05-04 17:29:55 -040062 unsigned int buffer_3_size = 256;
63 unsigned int buffer_4_size = 4097; /* Allocate more than the usual page size */
64 unsigned char *buffer3 = mbedtls_calloc(buffer_3_size, 1);
65 unsigned char *buffer4 = mbedtls_calloc(buffer_4_size, 1);
Andrzej Kurek60de0b12023-05-09 16:38:04 -040066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if (empty1 == NULL && empty2 == NULL) {
68 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040069 mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010070 }
71 } else if (empty1 == NULL || empty2 == NULL) {
72 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040073 mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010074 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020075 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 } else if (empty1 == empty2) {
77 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040078 mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010079 }
David Horstmann64cd2f22023-12-07 14:14:21 +000080 empty2 = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010081 } else {
82 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040083 mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n");
84 }
85 }
86
87 mbedtls_free(empty1);
88 mbedtls_free(empty2);
89
90 empty1 = mbedtls_calloc(1, 0);
91 empty2 = mbedtls_calloc(1, 0);
92 if (empty1 == NULL && empty2 == NULL) {
93 if (verbose) {
94 mbedtls_printf(" CALLOC(1,0): passed (NULL)\n");
95 }
96 } else if (empty1 == NULL || empty2 == NULL) {
97 if (verbose) {
98 mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n");
99 }
100 ++failures;
101 } else if (empty1 == empty2) {
102 if (verbose) {
103 mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n");
104 }
David Horstmann64cd2f22023-12-07 14:14:21 +0000105 empty2 = NULL;
Andrzej Kurek90327112023-04-27 09:30:18 -0400106 } else {
107 if (verbose) {
108 mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200110 }
111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (buffer1 == NULL || buffer2 == NULL) {
113 if (verbose) {
114 mbedtls_printf(" CALLOC(1): failed (NULL)\n");
115 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200116 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 } else if (buffer1 == buffer2) {
118 if (verbose) {
119 mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
120 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200121 ++failures;
David Horstmann64cd2f22023-12-07 14:14:21 +0000122 buffer2 = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 } else {
124 if (verbose) {
125 mbedtls_printf(" CALLOC(1): passed\n");
126 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200127 }
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_free(buffer1);
130 buffer1 = mbedtls_calloc(1, 1);
131 if (buffer1 == NULL) {
132 if (verbose) {
133 mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
134 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200135 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 } else {
137 if (verbose) {
138 mbedtls_printf(" CALLOC(1 again): passed\n");
139 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200140 }
141
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400142 for (unsigned int i = 0; i < buffer_3_size; i++) {
Andrzej Kurek90327112023-04-27 09:30:18 -0400143 if (buffer3[i] != 0) {
144 ++failures;
145 if (verbose) {
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400146 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
147 buffer_3_size);
148 }
149 break;
150 }
151 }
152
153 for (unsigned int i = 0; i < buffer_4_size; i++) {
154 if (buffer4[i] != 0) {
155 ++failures;
156 if (verbose) {
157 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
158 buffer_4_size);
Andrzej Kurek90327112023-04-27 09:30:18 -0400159 }
160 break;
161 }
162 }
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if (verbose) {
165 mbedtls_printf("\n");
166 }
167 mbedtls_free(empty1);
168 mbedtls_free(empty2);
169 mbedtls_free(buffer1);
170 mbedtls_free(buffer2);
Andrzej Kurek90327112023-04-27 09:30:18 -0400171 mbedtls_free(buffer3);
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400172 mbedtls_free(buffer4);
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 return failures;
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200174}
175#endif /* MBEDTLS_SELF_TEST */
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200178{
179 int ret;
180 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200181 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 ret = mbedtls_snprintf(buf, n, "%s", "123");
184 if (ret < 0 || (size_t) ret >= n) {
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200185 ret = -1;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200186 }
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
189 ref_ret != ret ||
190 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
191 return 1;
192 }
193
194 return 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200195}
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197static int run_test_snprintf(void)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200198{
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
200 test_snprintf(1, "", -1) != 0 ||
201 test_snprintf(2, "1", -1) != 0 ||
202 test_snprintf(3, "12", -1) != 0 ||
203 test_snprintf(4, "123", 3) != 0 ||
204 test_snprintf(5, "123", 3) != 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200205}
206
Simon Butcherb6a73c92016-06-18 22:45:37 +0100207/*
208 * Check if a seed file is present, and if not create one for the entropy
209 * self-test. If this fails, we attempt the test anyway, so no error is passed
210 * back.
211 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100212#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
Valerio Setti73bd2102025-04-15 08:56:51 +0200213#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT)
Valerio Setti3775c9b2025-04-15 12:49:17 +0200214static void dummy_entropy(unsigned char *output, size_t output_size)
215{
216 srand(1);
217 for (size_t i = 0; i < output_size; i++) {
218 output[i] = rand();
219 }
220}
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222static void create_entropy_seed_file(void)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100223{
224 int result;
Simon Butcherb6a73c92016-06-18 22:45:37 +0100225 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
226
227 /* Attempt to read the entropy seed file. If this fails - attempt to write
228 * to the file to ensure one is present. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 result = mbedtls_platform_std_nv_seed_read(seed_value,
230 MBEDTLS_ENTROPY_BLOCK_SIZE);
231 if (0 == result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100232 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100234
Valerio Setti3775c9b2025-04-15 12:49:17 +0200235 dummy_entropy(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Simon Butcherb6a73c92016-06-18 22:45:37 +0100237}
238#endif
239
Michael Schuster8db8d612024-06-01 21:15:02 +0200240static int mbedtls_entropy_self_test_wrapper(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100241{
Valerio Setti73bd2102025-04-15 08:56:51 +0200242#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_PLATFORM_GET_ENTROPY_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 create_entropy_seed_file();
Gilles Peskine319ac802017-12-15 14:57:18 +0100244#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return mbedtls_entropy_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100246}
247#endif
248
249#if defined(MBEDTLS_SELF_TEST)
250#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Michael Schuster8db8d612024-06-01 21:15:02 +0200251static int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100252{
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (verbose != 0) {
Gilles Peskine319ac802017-12-15 14:57:18 +0100254#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_memory_buffer_alloc_status();
Gilles Peskine319ac802017-12-15 14:57:18 +0100256#endif
257 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_memory_buffer_alloc_free();
259 return mbedtls_memory_buffer_alloc_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100260}
261#endif
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263typedef struct {
Gilles Peskine319ac802017-12-15 14:57:18 +0100264 const char *name;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 int (*function)(int);
Gilles Peskine319ac802017-12-15 14:57:18 +0100266} selftest_t;
267
268const selftest_t selftests[] =
269{
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 { "calloc", calloc_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100271#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 { "md5", mbedtls_md5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100273#endif
274#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 { "ripemd160", mbedtls_ripemd160_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100276#endif
277#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 { "sha1", mbedtls_sha1_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100279#endif
Valerio Setti46e8fd82022-12-14 10:58:02 +0100280#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 { "sha224", mbedtls_sha224_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100282#endif
283#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 { "sha256", mbedtls_sha256_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100285#endif
Valerio Setti898e7a32022-12-14 08:55:53 +0100286#if defined(MBEDTLS_SHA384_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 { "sha384", mbedtls_sha384_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100288#endif
289#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 { "sha512", mbedtls_sha512_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100291#endif
Gabor Mezei4aa974f2025-04-23 17:04:18 +0200292#if defined(PSA_WANT_ALG_SHA3_224) || \
293 defined(PSA_WANT_ALG_SHA3_256) || \
294 defined(PSA_WANT_ALG_SHA3_384) || \
295 defined(PSA_WANT_ALG_SHA3_512)
Pol Henarejosb3b220c2023-02-08 12:52:18 +0100296 { "sha3", mbedtls_sha3_self_test },
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200297#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100298#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 { "aes", mbedtls_aes_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100300#endif
301#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 { "gcm", mbedtls_gcm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100303#endif
304#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 { "ccm", mbedtls_ccm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100306#endif
307#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 { "cmac", mbedtls_cmac_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100309#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300310#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 { "chacha20", mbedtls_chacha20_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300312#endif
313#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 { "poly1305", mbedtls_poly1305_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300315#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200316#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 { "chacha20-poly1305", mbedtls_chachapoly_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300318#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100319#if defined(MBEDTLS_BASE64_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 { "base64", mbedtls_base64_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100321#endif
322#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 { "mpi", mbedtls_mpi_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100324#endif
325#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 { "rsa", mbedtls_rsa_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100327#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100328#if defined(MBEDTLS_CAMELLIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 { "camellia", mbedtls_camellia_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100330#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000331#if defined(MBEDTLS_ARIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 { "aria", mbedtls_aria_self_test },
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000333#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100334#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 { "ctr_drbg", mbedtls_ctr_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100336#endif
337#if defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 { "hmac_drbg", mbedtls_hmac_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100339#endif
340#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 { "ecp", mbedtls_ecp_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100342#endif
343#if defined(MBEDTLS_ECJPAKE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 { "ecjpake", mbedtls_ecjpake_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100345#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100346#if defined(MBEDTLS_ENTROPY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 { "entropy", mbedtls_entropy_self_test_wrapper },
Gilles Peskine319ac802017-12-15 14:57:18 +0100348#endif
349#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 { "pkcs5", mbedtls_pkcs5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100351#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100352/* Heap test comes last */
353#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100355#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 { NULL, NULL }
Gilles Peskine319ac802017-12-15 14:57:18 +0100357};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100358#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000361{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100362#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100363 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100364#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100365 char **argp;
366 int v = 1; /* v=1 for verbose mode */
367 int exclude_mode = 0;
368 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100369#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200370 unsigned char buf[1000000];
371#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200372 void *pointer;
373
374 /*
Dave Rodgman54647732023-02-10 16:16:17 +0000375 * Check some basic platform requirements as specified in README.md
376 */
377 if (SIZE_MAX < INT_MAX || SIZE_MAX < UINT_MAX) {
378 mbedtls_printf("SIZE_MAX must be at least as big as INT_MAX and UINT_MAX\n");
379 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
380 }
381
382 if (sizeof(int) < 4) {
383 mbedtls_printf("int must be at least 32 bits\n");
384 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
385 }
386
387 if (sizeof(size_t) < 4) {
388 mbedtls_printf("size_t must be at least 32 bits\n");
389 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
390 }
391
392 uint32_t endian_test = 0x12345678;
393 char *p = (char *) &endian_test;
394 if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78) &&
395 !(p[3] == 0x12 && p[2] == 0x34 && p[1] == 0x56 && p[0] == 0x78)) {
396 mbedtls_printf("Mixed-endian platforms are not supported\n");
397 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
398 }
399
400 /*
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200401 * The C standard doesn't guarantee that all-bits-0 is the representation
402 * of a NULL pointer. We do however use that in our code for initializing
403 * structures, which should work on every modern platform. Let's be sure.
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 memset(&pointer, 0, sizeof(void *));
406 if (pointer != NULL) {
407 mbedtls_printf("all-bits-zero is not a NULL pointer\n");
408 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200409 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200411 /*
Hanno Becker4ab38502018-10-16 13:22:44 +0100412 * The C standard allows padding bits in the representation
413 * of standard integer types, but our code does currently not
414 * support them.
415 *
416 * Here we check that the underlying C implementation doesn't
417 * use padding bits, and fail cleanly if it does.
418 *
419 * The check works by casting the maximum value representable
420 * by a given integer type into the unpadded integer type of the
421 * same bit-width and checking that it agrees with the maximum value
422 * of that unpadded type. For example, for a 4-byte int,
423 * MAX_INT should be 0x7fffffff in int32_t. This assumes that
424 * CHAR_BIT == 8, which is checked in check_config.h.
Hanno Beckerbaae59c2018-10-19 17:32:45 +0100425 *
426 * We assume that [u]intxx_t exist and that they don't
427 * have padding bits, as the standard requires.
Hanno Becker4ab38502018-10-16 13:22:44 +0100428 */
429
430#define CHECK_PADDING_SIGNED(TYPE, NAME) \
431 do \
432 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (sizeof(TYPE) == 2 || sizeof(TYPE) == 4 || \
434 sizeof(TYPE) == 8) { \
435 if ((sizeof(TYPE) == 2 && \
436 (int16_t) NAME ## _MAX != 0x7FFF) || \
437 (sizeof(TYPE) == 4 && \
438 (int32_t) NAME ## _MAX != 0x7FFFFFFF) || \
439 (sizeof(TYPE) == 8 && \
440 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF)) \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100441 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
443 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100444 } \
445 } else { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 mbedtls_printf("Padding checks only implemented for types of size 2, 4 or 8" \
Ari Weiler-Ofek86422e52025-07-04 14:43:30 +0100447 " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET \
448 "\n", \
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 sizeof(TYPE)); \
450 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100451 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100453
454#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
455 do \
456 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if ((sizeof(TYPE) == 2 && \
458 (uint16_t) NAME ## _MAX != 0xFFFF) || \
459 (sizeof(TYPE) == 4 && \
460 (uint32_t) NAME ## _MAX != 0xFFFFFFFF) || \
461 (sizeof(TYPE) == 8 && \
462 (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF)) \
Hanno Becker4ab38502018-10-16 13:22:44 +0100463 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
465 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100466 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 CHECK_PADDING_SIGNED(short, SHRT);
470 CHECK_PADDING_SIGNED(int, INT);
471 CHECK_PADDING_SIGNED(long, LONG);
472 CHECK_PADDING_SIGNED(long long, LLONG);
473 CHECK_PADDING_SIGNED(ptrdiff_t, PTRDIFF);
Hanno Becker4ab38502018-10-16 13:22:44 +0100474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 CHECK_PADDING_UNSIGNED(unsigned short, USHRT);
476 CHECK_PADDING_UNSIGNED(unsigned, UINT);
477 CHECK_PADDING_UNSIGNED(unsigned long, ULONG);
478 CHECK_PADDING_UNSIGNED(unsigned long long, ULLONG);
479 CHECK_PADDING_UNSIGNED(size_t, SIZE);
Hanno Becker4ab38502018-10-16 13:22:44 +0100480
481#undef CHECK_PADDING_SIGNED
482#undef CHECK_PADDING_UNSIGNED
483
484 /*
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200485 * Make sure we have a snprintf that correctly zero-terminates
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (run_test_snprintf() != 0) {
488 mbedtls_printf("the snprintf implementation is broken\n");
489 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200490 }
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
493 if (strcmp(*argp, "--quiet") == 0 ||
494 strcmp(*argp, "-q") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100495 v = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if (strcmp(*argp, "--exclude") == 0 ||
497 strcmp(*argp, "-x") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100498 exclude_mode = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100500 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 }
SimonB5a8afb82016-03-11 00:40:54 +0000502 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (v != 0) {
505 mbedtls_printf("\n");
506 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Paul Bakker6e339b52013-07-03 13:37:05 +0200512#endif
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 if (*argp != NULL && exclude_mode == 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100515 /* Run the specified tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 for (; *argp != NULL; argp++) {
517 for (test = selftests; test->name != NULL; test++) {
518 if (!strcmp(*argp, test->name)) {
519 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100520 suites_failed++;
521 }
522 suites_tested++;
523 break;
524 }
525 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (test->name == NULL) {
527 mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp);
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100528 suites_failed++;
529 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100530 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100532 /* Run all the tests except excluded ones */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 for (test = selftests; test->name != NULL; test++) {
534 if (exclude_mode) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100535 char **excluded;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 for (excluded = argp; *excluded != NULL; ++excluded) {
537 if (!strcmp(*excluded, test->name)) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100538 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100540 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (*excluded) {
542 if (v) {
543 mbedtls_printf(" Skip: %s\n", test->name);
544 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100545 continue;
546 }
547 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100549 suites_failed++;
550 }
551 suites_tested++;
552 }
SimonB5a8afb82016-03-11 00:40:54 +0000553 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100554
Paul Bakker70940ca2016-07-14 14:30:45 +0100555#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100556 (void) exclude_mode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
Paul Bakker70940ca2016-07-14 14:30:45 +0100558#endif
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (v != 0) {
561 mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
SimonB5a8afb82016-03-11 00:40:54 +0000562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (suites_failed > 0) {
564 mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
565 } else {
566 mbedtls_printf(" [ All tests PASS ]\n\n");
SimonB5a8afb82016-03-11 00:40:54 +0000567 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000568 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (suites_failed > 0) {
571 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
572 }
SimonB5a8afb82016-03-11 00:40:54 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
Paul Bakker5121ce52009-01-03 21:22:43 +0000575}