blob: e132e4c0c5a98470648e50f12a283fc184d4c261 [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
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +02008#define MBEDTLS_ALLOW_PRIVATE_ACCESS
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"
15#include "mbedtls/dhm.h"
16#include "mbedtls/gcm.h"
17#include "mbedtls/ccm.h"
Robert Cragiedc5c7b92015-12-11 15:49:45 +000018#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/md5.h"
20#include "mbedtls/ripemd160.h"
21#include "mbedtls/sha1.h"
22#include "mbedtls/sha256.h"
23#include "mbedtls/sha512.h"
Pol Henarejos7dbd5d12022-05-20 20:42:33 +020024#include "mbedtls/sha3.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/des.h"
26#include "mbedtls/aes.h"
27#include "mbedtls/camellia.h"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000028#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030029#include "mbedtls/chacha20.h"
30#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020031#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/base64.h"
33#include "mbedtls/bignum.h"
34#include "mbedtls/rsa.h"
35#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020038#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030040#include "mbedtls/nist_kw.h"
Dave Rodgman8f5a29a2022-04-11 12:59:45 +010041#include "mbedtls/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Hanno Becker4ab38502018-10-16 13:22:44 +010043#include <limits.h>
Rich Evans18b78c72015-02-11 14:06:19 +000044#include <string.h>
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020050#endif
51
Simon Butcher63cb97e2018-12-06 17:43:31 +000052
Gilles Peskine6fc21f62019-09-17 18:18:58 +020053#if defined MBEDTLS_SELF_TEST
54/* Sanity check for malloc. This is not expected to fail, and is rather
55 * intended to display potentially useful information about the platform,
56 * in particular the behavior of malloc(0). */
Gilles Peskine449bd832023-01-11 14:50:10 +010057static int calloc_self_test(int verbose)
Gilles Peskine6fc21f62019-09-17 18:18:58 +020058{
59 int failures = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010060 void *empty1 = mbedtls_calloc(0, 1);
61 void *empty2 = mbedtls_calloc(0, 1);
62 void *buffer1 = mbedtls_calloc(1, 1);
63 void *buffer2 = mbedtls_calloc(1, 1);
Andrzej Kureke35f3a22023-05-04 17:29:55 -040064 unsigned int buffer_3_size = 256;
65 unsigned int buffer_4_size = 4097; /* Allocate more than the usual page size */
66 unsigned char *buffer3 = mbedtls_calloc(buffer_3_size, 1);
67 unsigned char *buffer4 = mbedtls_calloc(buffer_4_size, 1);
Andrzej Kurek60de0b12023-05-09 16:38:04 -040068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if (empty1 == NULL && empty2 == NULL) {
70 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040071 mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010072 }
73 } else if (empty1 == NULL || empty2 == NULL) {
74 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040075 mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010076 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020077 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 } else if (empty1 == empty2) {
79 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040080 mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010081 }
David Horstmann64cd2f22023-12-07 14:14:21 +000082 empty2 = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010083 } else {
84 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040085 mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n");
86 }
87 }
88
89 mbedtls_free(empty1);
90 mbedtls_free(empty2);
91
92 empty1 = mbedtls_calloc(1, 0);
93 empty2 = mbedtls_calloc(1, 0);
94 if (empty1 == NULL && empty2 == NULL) {
95 if (verbose) {
96 mbedtls_printf(" CALLOC(1,0): passed (NULL)\n");
97 }
98 } else if (empty1 == NULL || empty2 == NULL) {
99 if (verbose) {
100 mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n");
101 }
102 ++failures;
103 } else if (empty1 == empty2) {
104 if (verbose) {
105 mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n");
106 }
David Horstmann64cd2f22023-12-07 14:14:21 +0000107 empty2 = NULL;
Andrzej Kurek90327112023-04-27 09:30:18 -0400108 } else {
109 if (verbose) {
110 mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200112 }
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 if (buffer1 == NULL || buffer2 == NULL) {
115 if (verbose) {
116 mbedtls_printf(" CALLOC(1): failed (NULL)\n");
117 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200118 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 } else if (buffer1 == buffer2) {
120 if (verbose) {
121 mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
122 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200123 ++failures;
David Horstmann64cd2f22023-12-07 14:14:21 +0000124 buffer2 = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 } else {
126 if (verbose) {
127 mbedtls_printf(" CALLOC(1): passed\n");
128 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200129 }
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_free(buffer1);
132 buffer1 = mbedtls_calloc(1, 1);
133 if (buffer1 == NULL) {
134 if (verbose) {
135 mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
136 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200137 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 } else {
139 if (verbose) {
140 mbedtls_printf(" CALLOC(1 again): passed\n");
141 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200142 }
143
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400144 for (unsigned int i = 0; i < buffer_3_size; i++) {
Andrzej Kurek90327112023-04-27 09:30:18 -0400145 if (buffer3[i] != 0) {
146 ++failures;
147 if (verbose) {
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400148 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
149 buffer_3_size);
150 }
151 break;
152 }
153 }
154
155 for (unsigned int i = 0; i < buffer_4_size; i++) {
156 if (buffer4[i] != 0) {
157 ++failures;
158 if (verbose) {
159 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
160 buffer_4_size);
Andrzej Kurek90327112023-04-27 09:30:18 -0400161 }
162 break;
163 }
164 }
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (verbose) {
167 mbedtls_printf("\n");
168 }
169 mbedtls_free(empty1);
170 mbedtls_free(empty2);
171 mbedtls_free(buffer1);
172 mbedtls_free(buffer2);
Andrzej Kurek90327112023-04-27 09:30:18 -0400173 mbedtls_free(buffer3);
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400174 mbedtls_free(buffer4);
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 return failures;
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200176}
177#endif /* MBEDTLS_SELF_TEST */
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200180{
181 int ret;
182 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200183 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 ret = mbedtls_snprintf(buf, n, "%s", "123");
186 if (ret < 0 || (size_t) ret >= n) {
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200187 ret = -1;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200188 }
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
191 ref_ret != ret ||
192 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
193 return 1;
194 }
195
196 return 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200197}
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199static int run_test_snprintf(void)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200200{
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
202 test_snprintf(1, "", -1) != 0 ||
203 test_snprintf(2, "1", -1) != 0 ||
204 test_snprintf(3, "12", -1) != 0 ||
205 test_snprintf(4, "123", 3) != 0 ||
206 test_snprintf(5, "123", 3) != 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200207}
208
Simon Butcherb6a73c92016-06-18 22:45:37 +0100209/*
210 * Check if a seed file is present, and if not create one for the entropy
211 * self-test. If this fails, we attempt the test anyway, so no error is passed
212 * back.
213 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100214#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
215#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static void create_entropy_seed_file(void)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100217{
218 int result;
219 size_t output_len = 0;
220 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
221
222 /* Attempt to read the entropy seed file. If this fails - attempt to write
223 * to the file to ensure one is present. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 result = mbedtls_platform_std_nv_seed_read(seed_value,
225 MBEDTLS_ENTROPY_BLOCK_SIZE);
226 if (0 == result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100227 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 result = mbedtls_platform_entropy_poll(NULL,
231 seed_value,
232 MBEDTLS_ENTROPY_BLOCK_SIZE,
233 &output_len);
234 if (0 != result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100235 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100239 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Simon Butcherb6a73c92016-06-18 22:45:37 +0100243}
244#endif
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246int mbedtls_entropy_self_test_wrapper(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100247{
248#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 create_entropy_seed_file();
Gilles Peskine319ac802017-12-15 14:57:18 +0100250#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return mbedtls_entropy_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100252}
253#endif
254
255#if defined(MBEDTLS_SELF_TEST)
256#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100257int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100258{
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (verbose != 0) {
Gilles Peskine319ac802017-12-15 14:57:18 +0100260#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_memory_buffer_alloc_status();
Gilles Peskine319ac802017-12-15 14:57:18 +0100262#endif
263 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_memory_buffer_alloc_free();
265 return mbedtls_memory_buffer_alloc_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100266}
267#endif
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269typedef struct {
Gilles Peskine319ac802017-12-15 14:57:18 +0100270 const char *name;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 int (*function)(int);
Gilles Peskine319ac802017-12-15 14:57:18 +0100272} selftest_t;
273
274const selftest_t selftests[] =
275{
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 { "calloc", calloc_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100277#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 { "md5", mbedtls_md5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100279#endif
280#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 { "ripemd160", mbedtls_ripemd160_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100282#endif
283#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 { "sha1", mbedtls_sha1_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100285#endif
Valerio Setti46e8fd82022-12-14 10:58:02 +0100286#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 { "sha224", mbedtls_sha224_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100288#endif
289#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 { "sha256", mbedtls_sha256_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100291#endif
Valerio Setti898e7a32022-12-14 08:55:53 +0100292#if defined(MBEDTLS_SHA384_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 { "sha384", mbedtls_sha384_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100294#endif
295#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 { "sha512", mbedtls_sha512_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100297#endif
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200298#if defined(MBEDTLS_SHA3_C)
Pol Henarejosb3b220c2023-02-08 12:52:18 +0100299 { "sha3", mbedtls_sha3_self_test },
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200300#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100301#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 { "des", mbedtls_des_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100303#endif
304#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 { "aes", mbedtls_aes_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100306#endif
307#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 { "gcm", mbedtls_gcm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100309#endif
310#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 { "ccm", mbedtls_ccm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100312#endif
Ron Eldor9ab746c2018-07-15 09:33:07 +0300313#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 { "nist_kw", mbedtls_nist_kw_self_test },
Ron Eldor9ab746c2018-07-15 09:33:07 +0300315#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100316#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 { "cmac", mbedtls_cmac_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100318#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300319#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 { "chacha20", mbedtls_chacha20_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300321#endif
322#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 { "poly1305", mbedtls_poly1305_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300324#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200325#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 { "chacha20-poly1305", mbedtls_chachapoly_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300327#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100328#if defined(MBEDTLS_BASE64_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 { "base64", mbedtls_base64_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100330#endif
331#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 { "mpi", mbedtls_mpi_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100333#endif
334#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 { "rsa", mbedtls_rsa_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100336#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100337#if defined(MBEDTLS_CAMELLIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 { "camellia", mbedtls_camellia_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100339#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000340#if defined(MBEDTLS_ARIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 { "aria", mbedtls_aria_self_test },
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000342#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100343#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 { "ctr_drbg", mbedtls_ctr_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100345#endif
346#if defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 { "hmac_drbg", mbedtls_hmac_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100348#endif
349#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 { "ecp", mbedtls_ecp_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100351#endif
352#if defined(MBEDTLS_ECJPAKE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 { "ecjpake", mbedtls_ecjpake_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100354#endif
355#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 { "dhm", mbedtls_dhm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100357#endif
358#if defined(MBEDTLS_ENTROPY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 { "entropy", mbedtls_entropy_self_test_wrapper },
Gilles Peskine319ac802017-12-15 14:57:18 +0100360#endif
361#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 { "pkcs5", mbedtls_pkcs5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100363#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100364/* Heap test comes last */
365#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100367#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 { NULL, NULL }
Gilles Peskine319ac802017-12-15 14:57:18 +0100369};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100370#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000373{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100374#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100375 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100376#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100377 char **argp;
378 int v = 1; /* v=1 for verbose mode */
379 int exclude_mode = 0;
380 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100381#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200382 unsigned char buf[1000000];
383#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200384 void *pointer;
385
386 /*
Dave Rodgman54647732023-02-10 16:16:17 +0000387 * Check some basic platform requirements as specified in README.md
388 */
389 if (SIZE_MAX < INT_MAX || SIZE_MAX < UINT_MAX) {
390 mbedtls_printf("SIZE_MAX must be at least as big as INT_MAX and UINT_MAX\n");
391 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
392 }
393
394 if (sizeof(int) < 4) {
395 mbedtls_printf("int must be at least 32 bits\n");
396 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
397 }
398
399 if (sizeof(size_t) < 4) {
400 mbedtls_printf("size_t must be at least 32 bits\n");
401 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
402 }
403
404 uint32_t endian_test = 0x12345678;
405 char *p = (char *) &endian_test;
406 if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78) &&
407 !(p[3] == 0x12 && p[2] == 0x34 && p[1] == 0x56 && p[0] == 0x78)) {
408 mbedtls_printf("Mixed-endian platforms are not supported\n");
409 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
410 }
411
412 /*
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200413 * 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 Peskine449bd832023-01-11 14:50:10 +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 /*
Hanno Becker4ab38502018-10-16 13:22:44 +0100424 * The C standard allows padding bits in the representation
425 * of standard integer types, but our code does currently not
426 * support them.
427 *
428 * Here we check that the underlying C implementation doesn't
429 * use padding bits, and fail cleanly if it does.
430 *
431 * The check works by casting the maximum value representable
432 * by a given integer type into the unpadded integer type of the
433 * same bit-width and checking that it agrees with the maximum value
434 * of that unpadded type. For example, for a 4-byte int,
435 * MAX_INT should be 0x7fffffff in int32_t. This assumes that
436 * CHAR_BIT == 8, which is checked in check_config.h.
Hanno Beckerbaae59c2018-10-19 17:32:45 +0100437 *
438 * We assume that [u]intxx_t exist and that they don't
439 * have padding bits, as the standard requires.
Hanno Becker4ab38502018-10-16 13:22:44 +0100440 */
441
442#define CHECK_PADDING_SIGNED(TYPE, NAME) \
443 do \
444 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (sizeof(TYPE) == 2 || sizeof(TYPE) == 4 || \
446 sizeof(TYPE) == 8) { \
447 if ((sizeof(TYPE) == 2 && \
448 (int16_t) NAME ## _MAX != 0x7FFF) || \
449 (sizeof(TYPE) == 4 && \
450 (int32_t) NAME ## _MAX != 0x7FFFFFFF) || \
451 (sizeof(TYPE) == 8 && \
452 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF)) \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100453 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
455 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100456 } \
457 } else { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 mbedtls_printf("Padding checks only implemented for types of size 2, 4 or 8" \
459 " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n", \
460 sizeof(TYPE)); \
461 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100462 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100464
465#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
466 do \
467 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if ((sizeof(TYPE) == 2 && \
469 (uint16_t) NAME ## _MAX != 0xFFFF) || \
470 (sizeof(TYPE) == 4 && \
471 (uint32_t) NAME ## _MAX != 0xFFFFFFFF) || \
472 (sizeof(TYPE) == 8 && \
473 (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF)) \
Hanno Becker4ab38502018-10-16 13:22:44 +0100474 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
476 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100477 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 CHECK_PADDING_SIGNED(short, SHRT);
481 CHECK_PADDING_SIGNED(int, INT);
482 CHECK_PADDING_SIGNED(long, LONG);
483 CHECK_PADDING_SIGNED(long long, LLONG);
484 CHECK_PADDING_SIGNED(ptrdiff_t, PTRDIFF);
Hanno Becker4ab38502018-10-16 13:22:44 +0100485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 CHECK_PADDING_UNSIGNED(unsigned short, USHRT);
487 CHECK_PADDING_UNSIGNED(unsigned, UINT);
488 CHECK_PADDING_UNSIGNED(unsigned long, ULONG);
489 CHECK_PADDING_UNSIGNED(unsigned long long, ULLONG);
490 CHECK_PADDING_UNSIGNED(size_t, SIZE);
Hanno Becker4ab38502018-10-16 13:22:44 +0100491
492#undef CHECK_PADDING_SIGNED
493#undef CHECK_PADDING_UNSIGNED
494
495 /*
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200496 * Make sure we have a snprintf that correctly zero-terminates
497 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (run_test_snprintf() != 0) {
499 mbedtls_printf("the snprintf implementation is broken\n");
500 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200501 }
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
504 if (strcmp(*argp, "--quiet") == 0 ||
505 strcmp(*argp, "-q") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100506 v = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 } else if (strcmp(*argp, "--exclude") == 0 ||
508 strcmp(*argp, "-x") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100509 exclude_mode = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100511 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 }
SimonB5a8afb82016-03-11 00:40:54 +0000513 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (v != 0) {
516 mbedtls_printf("\n");
517 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Paul Bakker6e339b52013-07-03 13:37:05 +0200523#endif
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (*argp != NULL && exclude_mode == 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100526 /* Run the specified tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 for (; *argp != NULL; argp++) {
528 for (test = selftests; test->name != NULL; test++) {
529 if (!strcmp(*argp, test->name)) {
530 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100531 suites_failed++;
532 }
533 suites_tested++;
534 break;
535 }
536 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (test->name == NULL) {
538 mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp);
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100539 suites_failed++;
540 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100541 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100543 /* Run all the tests except excluded ones */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 for (test = selftests; test->name != NULL; test++) {
545 if (exclude_mode) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100546 char **excluded;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 for (excluded = argp; *excluded != NULL; ++excluded) {
548 if (!strcmp(*excluded, test->name)) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100549 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100551 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (*excluded) {
553 if (v) {
554 mbedtls_printf(" Skip: %s\n", test->name);
555 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100556 continue;
557 }
558 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100560 suites_failed++;
561 }
562 suites_tested++;
563 }
SimonB5a8afb82016-03-11 00:40:54 +0000564 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100565
Paul Bakker70940ca2016-07-14 14:30:45 +0100566#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100567 (void) exclude_mode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
Paul Bakker70940ca2016-07-14 14:30:45 +0100569#endif
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (v != 0) {
572 mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
SimonB5a8afb82016-03-11 00:40:54 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (suites_failed > 0) {
575 mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
576 } else {
577 mbedtls_printf(" [ All tests PASS ]\n\n");
SimonB5a8afb82016-03-11 00:40:54 +0000578 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (suites_failed > 0) {
582 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
583 }
SimonB5a8afb82016-03-11 00:40:54 +0000584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
Paul Bakker5121ce52009-01-03 21:22:43 +0000586}