blob: 4794cefd2400fbe0871d19696aabfcbc90cb1a17 [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/des.h"
25#include "mbedtls/aes.h"
26#include "mbedtls/camellia.h"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000027#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030028#include "mbedtls/chacha20.h"
29#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020030#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/base64.h"
32#include "mbedtls/bignum.h"
33#include "mbedtls/rsa.h"
34#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020037#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030039#include "mbedtls/nist_kw.h"
Dave Rodgman8f5a29a2022-04-11 12:59:45 +010040#include "mbedtls/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Hanno Becker4ab38502018-10-16 13:22:44 +010042#include <limits.h>
Rich Evans18b78c72015-02-11 14:06:19 +000043#include <string.h>
44
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020049#endif
50
Simon Butcher63cb97e2018-12-06 17:43:31 +000051
Gilles Peskine6fc21f62019-09-17 18:18:58 +020052#if defined MBEDTLS_SELF_TEST
53/* Sanity check for malloc. This is not expected to fail, and is rather
54 * intended to display potentially useful information about the platform,
55 * in particular the behavior of malloc(0). */
Gilles Peskine449bd832023-01-11 14:50:10 +010056static int calloc_self_test(int verbose)
Gilles Peskine6fc21f62019-09-17 18:18:58 +020057{
58 int failures = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010059 void *empty1 = mbedtls_calloc(0, 1);
60 void *empty2 = mbedtls_calloc(0, 1);
61 void *buffer1 = mbedtls_calloc(1, 1);
62 void *buffer2 = mbedtls_calloc(1, 1);
Andrzej Kureke35f3a22023-05-04 17:29:55 -040063 unsigned int buffer_3_size = 256;
64 unsigned int buffer_4_size = 4097; /* Allocate more than the usual page size */
65 unsigned char *buffer3 = mbedtls_calloc(buffer_3_size, 1);
66 unsigned char *buffer4 = mbedtls_calloc(buffer_4_size, 1);
Andrzej Kurek60de0b12023-05-09 16:38:04 -040067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 if (empty1 == NULL && empty2 == NULL) {
69 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040070 mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010071 }
72 } else if (empty1 == NULL || empty2 == NULL) {
73 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040074 mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010075 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020076 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +010077 } else if (empty1 == empty2) {
78 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040079 mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010080 }
David Horstmann64cd2f22023-12-07 14:14:21 +000081 empty2 = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010082 } else {
83 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040084 mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n");
85 }
86 }
87
88 mbedtls_free(empty1);
89 mbedtls_free(empty2);
90
91 empty1 = mbedtls_calloc(1, 0);
92 empty2 = mbedtls_calloc(1, 0);
93 if (empty1 == NULL && empty2 == NULL) {
94 if (verbose) {
95 mbedtls_printf(" CALLOC(1,0): passed (NULL)\n");
96 }
97 } else if (empty1 == NULL || empty2 == NULL) {
98 if (verbose) {
99 mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n");
100 }
101 ++failures;
102 } else if (empty1 == empty2) {
103 if (verbose) {
104 mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n");
105 }
David Horstmann64cd2f22023-12-07 14:14:21 +0000106 empty2 = NULL;
Andrzej Kurek90327112023-04-27 09:30:18 -0400107 } else {
108 if (verbose) {
109 mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200111 }
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (buffer1 == NULL || buffer2 == NULL) {
114 if (verbose) {
115 mbedtls_printf(" CALLOC(1): failed (NULL)\n");
116 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200117 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 } else if (buffer1 == buffer2) {
119 if (verbose) {
120 mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
121 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200122 ++failures;
David Horstmann64cd2f22023-12-07 14:14:21 +0000123 buffer2 = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 } else {
125 if (verbose) {
126 mbedtls_printf(" CALLOC(1): passed\n");
127 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200128 }
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_free(buffer1);
131 buffer1 = mbedtls_calloc(1, 1);
132 if (buffer1 == NULL) {
133 if (verbose) {
134 mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
135 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200136 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 } else {
138 if (verbose) {
139 mbedtls_printf(" CALLOC(1 again): passed\n");
140 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200141 }
142
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400143 for (unsigned int i = 0; i < buffer_3_size; i++) {
Andrzej Kurek90327112023-04-27 09:30:18 -0400144 if (buffer3[i] != 0) {
145 ++failures;
146 if (verbose) {
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400147 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
148 buffer_3_size);
149 }
150 break;
151 }
152 }
153
154 for (unsigned int i = 0; i < buffer_4_size; i++) {
155 if (buffer4[i] != 0) {
156 ++failures;
157 if (verbose) {
158 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
159 buffer_4_size);
Andrzej Kurek90327112023-04-27 09:30:18 -0400160 }
161 break;
162 }
163 }
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (verbose) {
166 mbedtls_printf("\n");
167 }
168 mbedtls_free(empty1);
169 mbedtls_free(empty2);
170 mbedtls_free(buffer1);
171 mbedtls_free(buffer2);
Andrzej Kurek90327112023-04-27 09:30:18 -0400172 mbedtls_free(buffer3);
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400173 mbedtls_free(buffer4);
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return failures;
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200175}
176#endif /* MBEDTLS_SELF_TEST */
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200179{
180 int ret;
181 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200182 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 ret = mbedtls_snprintf(buf, n, "%s", "123");
185 if (ret < 0 || (size_t) ret >= n) {
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200186 ret = -1;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200187 }
188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
190 ref_ret != ret ||
191 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
192 return 1;
193 }
194
195 return 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200196}
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198static int run_test_snprintf(void)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200199{
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
201 test_snprintf(1, "", -1) != 0 ||
202 test_snprintf(2, "1", -1) != 0 ||
203 test_snprintf(3, "12", -1) != 0 ||
204 test_snprintf(4, "123", 3) != 0 ||
205 test_snprintf(5, "123", 3) != 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200206}
207
Simon Butcherb6a73c92016-06-18 22:45:37 +0100208/*
209 * Check if a seed file is present, and if not create one for the entropy
210 * self-test. If this fails, we attempt the test anyway, so no error is passed
211 * back.
212 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100213#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
214#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100215static void create_entropy_seed_file(void)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100216{
217 int result;
218 size_t output_len = 0;
219 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
220
221 /* Attempt to read the entropy seed file. If this fails - attempt to write
222 * to the file to ensure one is present. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 result = mbedtls_platform_std_nv_seed_read(seed_value,
224 MBEDTLS_ENTROPY_BLOCK_SIZE);
225 if (0 == result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100226 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 result = mbedtls_platform_entropy_poll(NULL,
230 seed_value,
231 MBEDTLS_ENTROPY_BLOCK_SIZE,
232 &output_len);
233 if (0 != result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100234 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100238 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Simon Butcherb6a73c92016-06-18 22:45:37 +0100242}
243#endif
244
Michael Schuster8db8d612024-06-01 21:15:02 +0200245static int mbedtls_entropy_self_test_wrapper(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100246{
247#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 create_entropy_seed_file();
Gilles Peskine319ac802017-12-15 14:57:18 +0100249#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 return mbedtls_entropy_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100251}
252#endif
253
254#if defined(MBEDTLS_SELF_TEST)
255#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Michael Schuster8db8d612024-06-01 21:15:02 +0200256static int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100257{
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (verbose != 0) {
Gilles Peskine319ac802017-12-15 14:57:18 +0100259#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 mbedtls_memory_buffer_alloc_status();
Gilles Peskine319ac802017-12-15 14:57:18 +0100261#endif
262 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_memory_buffer_alloc_free();
264 return mbedtls_memory_buffer_alloc_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100265}
266#endif
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268typedef struct {
Gilles Peskine319ac802017-12-15 14:57:18 +0100269 const char *name;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 int (*function)(int);
Gilles Peskine319ac802017-12-15 14:57:18 +0100271} selftest_t;
272
273const selftest_t selftests[] =
274{
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 { "calloc", calloc_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100276#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 { "md5", mbedtls_md5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100278#endif
279#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 { "ripemd160", mbedtls_ripemd160_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100281#endif
282#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 { "sha1", mbedtls_sha1_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100284#endif
Valerio Setti46e8fd82022-12-14 10:58:02 +0100285#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 { "sha224", mbedtls_sha224_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100287#endif
288#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 { "sha256", mbedtls_sha256_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100290#endif
Valerio Setti898e7a32022-12-14 08:55:53 +0100291#if defined(MBEDTLS_SHA384_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 { "sha384", mbedtls_sha384_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100293#endif
294#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 { "sha512", mbedtls_sha512_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100296#endif
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200297#if defined(MBEDTLS_SHA3_C)
Pol Henarejosb3b220c2023-02-08 12:52:18 +0100298 { "sha3", mbedtls_sha3_self_test },
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200299#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100300#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 { "des", mbedtls_des_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100302#endif
303#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 { "aes", mbedtls_aes_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100305#endif
306#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 { "gcm", mbedtls_gcm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100308#endif
309#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 { "ccm", mbedtls_ccm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100311#endif
312#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 { "cmac", mbedtls_cmac_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100314#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300315#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 { "chacha20", mbedtls_chacha20_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300317#endif
318#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 { "poly1305", mbedtls_poly1305_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300320#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200321#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 { "chacha20-poly1305", mbedtls_chachapoly_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300323#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100324#if defined(MBEDTLS_BASE64_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 { "base64", mbedtls_base64_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100326#endif
327#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 { "mpi", mbedtls_mpi_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100329#endif
330#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 { "rsa", mbedtls_rsa_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100332#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100333#if defined(MBEDTLS_CAMELLIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 { "camellia", mbedtls_camellia_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100335#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000336#if defined(MBEDTLS_ARIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 { "aria", mbedtls_aria_self_test },
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000338#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100339#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 { "ctr_drbg", mbedtls_ctr_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100341#endif
342#if defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 { "hmac_drbg", mbedtls_hmac_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100344#endif
345#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 { "ecp", mbedtls_ecp_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100347#endif
348#if defined(MBEDTLS_ECJPAKE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 { "ecjpake", mbedtls_ecjpake_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100350#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100351#if defined(MBEDTLS_ENTROPY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 { "entropy", mbedtls_entropy_self_test_wrapper },
Gilles Peskine319ac802017-12-15 14:57:18 +0100353#endif
354#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 { "pkcs5", mbedtls_pkcs5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100356#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100357/* Heap test comes last */
358#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100360#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 { NULL, NULL }
Gilles Peskine319ac802017-12-15 14:57:18 +0100362};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100363#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000366{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100367#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100368 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100369#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100370 char **argp;
371 int v = 1; /* v=1 for verbose mode */
372 int exclude_mode = 0;
373 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100374#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200375 unsigned char buf[1000000];
376#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200377 void *pointer;
378
379 /*
Dave Rodgman54647732023-02-10 16:16:17 +0000380 * Check some basic platform requirements as specified in README.md
381 */
382 if (SIZE_MAX < INT_MAX || SIZE_MAX < UINT_MAX) {
383 mbedtls_printf("SIZE_MAX must be at least as big as INT_MAX and UINT_MAX\n");
384 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
385 }
386
387 if (sizeof(int) < 4) {
388 mbedtls_printf("int must be at least 32 bits\n");
389 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
390 }
391
392 if (sizeof(size_t) < 4) {
393 mbedtls_printf("size_t must be at least 32 bits\n");
394 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
395 }
396
397 uint32_t endian_test = 0x12345678;
398 char *p = (char *) &endian_test;
399 if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78) &&
400 !(p[3] == 0x12 && p[2] == 0x34 && p[1] == 0x56 && p[0] == 0x78)) {
401 mbedtls_printf("Mixed-endian platforms are not supported\n");
402 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
403 }
404
405 /*
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200406 * The C standard doesn't guarantee that all-bits-0 is the representation
407 * of a NULL pointer. We do however use that in our code for initializing
408 * structures, which should work on every modern platform. Let's be sure.
409 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 memset(&pointer, 0, sizeof(void *));
411 if (pointer != NULL) {
412 mbedtls_printf("all-bits-zero is not a NULL pointer\n");
413 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200414 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200416 /*
Hanno Becker4ab38502018-10-16 13:22:44 +0100417 * The C standard allows padding bits in the representation
418 * of standard integer types, but our code does currently not
419 * support them.
420 *
421 * Here we check that the underlying C implementation doesn't
422 * use padding bits, and fail cleanly if it does.
423 *
424 * The check works by casting the maximum value representable
425 * by a given integer type into the unpadded integer type of the
426 * same bit-width and checking that it agrees with the maximum value
427 * of that unpadded type. For example, for a 4-byte int,
428 * MAX_INT should be 0x7fffffff in int32_t. This assumes that
429 * CHAR_BIT == 8, which is checked in check_config.h.
Hanno Beckerbaae59c2018-10-19 17:32:45 +0100430 *
431 * We assume that [u]intxx_t exist and that they don't
432 * have padding bits, as the standard requires.
Hanno Becker4ab38502018-10-16 13:22:44 +0100433 */
434
435#define CHECK_PADDING_SIGNED(TYPE, NAME) \
436 do \
437 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (sizeof(TYPE) == 2 || sizeof(TYPE) == 4 || \
439 sizeof(TYPE) == 8) { \
440 if ((sizeof(TYPE) == 2 && \
441 (int16_t) NAME ## _MAX != 0x7FFF) || \
442 (sizeof(TYPE) == 4 && \
443 (int32_t) NAME ## _MAX != 0x7FFFFFFF) || \
444 (sizeof(TYPE) == 8 && \
445 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF)) \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100446 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
448 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100449 } \
450 } else { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 mbedtls_printf("Padding checks only implemented for types of size 2, 4 or 8" \
452 " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n", \
453 sizeof(TYPE)); \
454 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100455 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100457
458#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
459 do \
460 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if ((sizeof(TYPE) == 2 && \
462 (uint16_t) NAME ## _MAX != 0xFFFF) || \
463 (sizeof(TYPE) == 4 && \
464 (uint32_t) NAME ## _MAX != 0xFFFFFFFF) || \
465 (sizeof(TYPE) == 8 && \
466 (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF)) \
Hanno Becker4ab38502018-10-16 13:22:44 +0100467 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
469 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100470 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 CHECK_PADDING_SIGNED(short, SHRT);
474 CHECK_PADDING_SIGNED(int, INT);
475 CHECK_PADDING_SIGNED(long, LONG);
476 CHECK_PADDING_SIGNED(long long, LLONG);
477 CHECK_PADDING_SIGNED(ptrdiff_t, PTRDIFF);
Hanno Becker4ab38502018-10-16 13:22:44 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 CHECK_PADDING_UNSIGNED(unsigned short, USHRT);
480 CHECK_PADDING_UNSIGNED(unsigned, UINT);
481 CHECK_PADDING_UNSIGNED(unsigned long, ULONG);
482 CHECK_PADDING_UNSIGNED(unsigned long long, ULLONG);
483 CHECK_PADDING_UNSIGNED(size_t, SIZE);
Hanno Becker4ab38502018-10-16 13:22:44 +0100484
485#undef CHECK_PADDING_SIGNED
486#undef CHECK_PADDING_UNSIGNED
487
488 /*
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200489 * Make sure we have a snprintf that correctly zero-terminates
490 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (run_test_snprintf() != 0) {
492 mbedtls_printf("the snprintf implementation is broken\n");
493 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200494 }
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
497 if (strcmp(*argp, "--quiet") == 0 ||
498 strcmp(*argp, "-q") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100499 v = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 } else if (strcmp(*argp, "--exclude") == 0 ||
501 strcmp(*argp, "-x") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100502 exclude_mode = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100504 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 }
SimonB5a8afb82016-03-11 00:40:54 +0000506 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (v != 0) {
509 mbedtls_printf("\n");
510 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Paul Bakker6e339b52013-07-03 13:37:05 +0200516#endif
517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if (*argp != NULL && exclude_mode == 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100519 /* Run the specified tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 for (; *argp != NULL; argp++) {
521 for (test = selftests; test->name != NULL; test++) {
522 if (!strcmp(*argp, test->name)) {
523 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100524 suites_failed++;
525 }
526 suites_tested++;
527 break;
528 }
529 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (test->name == NULL) {
531 mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp);
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100532 suites_failed++;
533 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100534 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100536 /* Run all the tests except excluded ones */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 for (test = selftests; test->name != NULL; test++) {
538 if (exclude_mode) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100539 char **excluded;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 for (excluded = argp; *excluded != NULL; ++excluded) {
541 if (!strcmp(*excluded, test->name)) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100542 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100544 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (*excluded) {
546 if (v) {
547 mbedtls_printf(" Skip: %s\n", test->name);
548 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100549 continue;
550 }
551 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100553 suites_failed++;
554 }
555 suites_tested++;
556 }
SimonB5a8afb82016-03-11 00:40:54 +0000557 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100558
Paul Bakker70940ca2016-07-14 14:30:45 +0100559#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100560 (void) exclude_mode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
Paul Bakker70940ca2016-07-14 14:30:45 +0100562#endif
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (v != 0) {
565 mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
SimonB5a8afb82016-03-11 00:40:54 +0000566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (suites_failed > 0) {
568 mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
569 } else {
570 mbedtls_printf(" [ All tests PASS ]\n\n");
SimonB5a8afb82016-03-11 00:40:54 +0000571 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (suites_failed > 0) {
575 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
576 }
SimonB5a8afb82016-03-11 00:40:54 +0000577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
Paul Bakker5121ce52009-01-03 21:22:43 +0000579}