blob: cc5e00ed3b14512d6a7d6c6ed04991c1a180f853 [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
Mateusz Starzyk6c2e9b62021-05-19 17:54:54 +020020#define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
Bence Szépkútic662b362021-05-27 11:25:03 +020022#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/entropy.h"
25#include "mbedtls/hmac_drbg.h"
26#include "mbedtls/ctr_drbg.h"
27#include "mbedtls/dhm.h"
28#include "mbedtls/gcm.h"
29#include "mbedtls/ccm.h"
Robert Cragiedc5c7b92015-12-11 15:49:45 +000030#include "mbedtls/cmac.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/md5.h"
32#include "mbedtls/ripemd160.h"
33#include "mbedtls/sha1.h"
34#include "mbedtls/sha256.h"
35#include "mbedtls/sha512.h"
Pol Henarejos7dbd5d12022-05-20 20:42:33 +020036#include "mbedtls/sha3.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/des.h"
38#include "mbedtls/aes.h"
39#include "mbedtls/camellia.h"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000040#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030041#include "mbedtls/chacha20.h"
42#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020043#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/base64.h"
45#include "mbedtls/bignum.h"
46#include "mbedtls/rsa.h"
47#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020050#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030052#include "mbedtls/nist_kw.h"
Dave Rodgman8f5a29a2022-04-11 12:59:45 +010053#include "mbedtls/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Hanno Becker4ab38502018-10-16 13:22:44 +010055#include <limits.h>
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <string.h>
57
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020062#endif
63
Simon Butcher63cb97e2018-12-06 17:43:31 +000064
Gilles Peskine6fc21f62019-09-17 18:18:58 +020065#if defined MBEDTLS_SELF_TEST
66/* Sanity check for malloc. This is not expected to fail, and is rather
67 * intended to display potentially useful information about the platform,
68 * in particular the behavior of malloc(0). */
Gilles Peskine449bd832023-01-11 14:50:10 +010069static int calloc_self_test(int verbose)
Gilles Peskine6fc21f62019-09-17 18:18:58 +020070{
71 int failures = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010072 void *empty1 = mbedtls_calloc(0, 1);
73 void *empty2 = mbedtls_calloc(0, 1);
74 void *buffer1 = mbedtls_calloc(1, 1);
75 void *buffer2 = mbedtls_calloc(1, 1);
Andrzej Kureke35f3a22023-05-04 17:29:55 -040076 unsigned int buffer_3_size = 256;
77 unsigned int buffer_4_size = 4097; /* Allocate more than the usual page size */
78 unsigned char *buffer3 = mbedtls_calloc(buffer_3_size, 1);
79 unsigned char *buffer4 = mbedtls_calloc(buffer_4_size, 1);
Gilles Peskine6fc21f62019-09-17 18:18:58 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (empty1 == NULL && empty2 == NULL) {
82 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040083 mbedtls_printf(" CALLOC(0,1): passed (NULL)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010084 }
85 } else if (empty1 == NULL || empty2 == NULL) {
86 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040087 mbedtls_printf(" CALLOC(0,1): failed (mix of NULL and non-NULL)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010088 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020089 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 } else if (empty1 == empty2) {
91 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040092 mbedtls_printf(" CALLOC(0,1): passed (same non-null)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010093 }
94 } else {
95 if (verbose) {
Andrzej Kurek90327112023-04-27 09:30:18 -040096 mbedtls_printf(" CALLOC(0,1): passed (distinct non-null)\n");
97 }
98 }
99
100 mbedtls_free(empty1);
101 mbedtls_free(empty2);
102
103 empty1 = mbedtls_calloc(1, 0);
104 empty2 = mbedtls_calloc(1, 0);
105 if (empty1 == NULL && empty2 == NULL) {
106 if (verbose) {
107 mbedtls_printf(" CALLOC(1,0): passed (NULL)\n");
108 }
109 } else if (empty1 == NULL || empty2 == NULL) {
110 if (verbose) {
111 mbedtls_printf(" CALLOC(1,0): failed (mix of NULL and non-NULL)\n");
112 }
113 ++failures;
114 } else if (empty1 == empty2) {
115 if (verbose) {
116 mbedtls_printf(" CALLOC(1,0): passed (same non-null)\n");
117 }
118 } else {
119 if (verbose) {
120 mbedtls_printf(" CALLOC(1,0): passed (distinct non-null)\n");
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (buffer1 == NULL || buffer2 == NULL) {
125 if (verbose) {
126 mbedtls_printf(" CALLOC(1): failed (NULL)\n");
127 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200128 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 } else if (buffer1 == buffer2) {
130 if (verbose) {
131 mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
132 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200133 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 } else {
135 if (verbose) {
136 mbedtls_printf(" CALLOC(1): passed\n");
137 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200138 }
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_free(buffer1);
141 buffer1 = mbedtls_calloc(1, 1);
142 if (buffer1 == NULL) {
143 if (verbose) {
144 mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
145 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200146 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 } else {
148 if (verbose) {
149 mbedtls_printf(" CALLOC(1 again): passed\n");
150 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200151 }
152
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400153 for (unsigned int i = 0; i < buffer_3_size; i++) {
Andrzej Kurek90327112023-04-27 09:30:18 -0400154 if (buffer3[i] != 0) {
155 ++failures;
156 if (verbose) {
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400157 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
158 buffer_3_size);
159 }
160 break;
161 }
162 }
163
164 for (unsigned int i = 0; i < buffer_4_size; i++) {
165 if (buffer4[i] != 0) {
166 ++failures;
167 if (verbose) {
168 mbedtls_printf(" CALLOC(%u): failed (memory not initialized to 0)\n",
169 buffer_4_size);
Andrzej Kurek90327112023-04-27 09:30:18 -0400170 }
171 break;
172 }
173 }
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (verbose) {
176 mbedtls_printf("\n");
177 }
178 mbedtls_free(empty1);
179 mbedtls_free(empty2);
180 mbedtls_free(buffer1);
181 mbedtls_free(buffer2);
Andrzej Kurek90327112023-04-27 09:30:18 -0400182 mbedtls_free(buffer3);
Andrzej Kureke35f3a22023-05-04 17:29:55 -0400183 mbedtls_free(buffer4);
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return failures;
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200185}
186#endif /* MBEDTLS_SELF_TEST */
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200189{
190 int ret;
191 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200192 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 ret = mbedtls_snprintf(buf, n, "%s", "123");
195 if (ret < 0 || (size_t) ret >= n) {
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200196 ret = -1;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200197 }
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
200 ref_ret != ret ||
201 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
202 return 1;
203 }
204
205 return 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200206}
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208static int run_test_snprintf(void)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200209{
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
211 test_snprintf(1, "", -1) != 0 ||
212 test_snprintf(2, "1", -1) != 0 ||
213 test_snprintf(3, "12", -1) != 0 ||
214 test_snprintf(4, "123", 3) != 0 ||
215 test_snprintf(5, "123", 3) != 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200216}
217
Simon Butcherb6a73c92016-06-18 22:45:37 +0100218/*
219 * Check if a seed file is present, and if not create one for the entropy
220 * self-test. If this fails, we attempt the test anyway, so no error is passed
221 * back.
222 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100223#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
224#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100225static void create_entropy_seed_file(void)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100226{
227 int result;
228 size_t output_len = 0;
229 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
230
231 /* Attempt to read the entropy seed file. If this fails - attempt to write
232 * to the file to ensure one is present. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 result = mbedtls_platform_std_nv_seed_read(seed_value,
234 MBEDTLS_ENTROPY_BLOCK_SIZE);
235 if (0 == result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100236 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 result = mbedtls_platform_entropy_poll(NULL,
240 seed_value,
241 MBEDTLS_ENTROPY_BLOCK_SIZE,
242 &output_len);
243 if (0 != result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100244 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100248 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Simon Butcherb6a73c92016-06-18 22:45:37 +0100252}
253#endif
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255int mbedtls_entropy_self_test_wrapper(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100256{
257#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 create_entropy_seed_file();
Gilles Peskine319ac802017-12-15 14:57:18 +0100259#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return mbedtls_entropy_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100261}
262#endif
263
264#if defined(MBEDTLS_SELF_TEST)
265#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100266int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100267{
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (verbose != 0) {
Gilles Peskine319ac802017-12-15 14:57:18 +0100269#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_memory_buffer_alloc_status();
Gilles Peskine319ac802017-12-15 14:57:18 +0100271#endif
272 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 mbedtls_memory_buffer_alloc_free();
274 return mbedtls_memory_buffer_alloc_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100275}
276#endif
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278typedef struct {
Gilles Peskine319ac802017-12-15 14:57:18 +0100279 const char *name;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 int (*function)(int);
Gilles Peskine319ac802017-12-15 14:57:18 +0100281} selftest_t;
282
283const selftest_t selftests[] =
284{
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 { "calloc", calloc_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100286#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 { "md5", mbedtls_md5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100288#endif
289#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 { "ripemd160", mbedtls_ripemd160_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100291#endif
292#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 { "sha1", mbedtls_sha1_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100294#endif
Valerio Setti46e8fd82022-12-14 10:58:02 +0100295#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 { "sha224", mbedtls_sha224_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100297#endif
298#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 { "sha256", mbedtls_sha256_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100300#endif
Valerio Setti898e7a32022-12-14 08:55:53 +0100301#if defined(MBEDTLS_SHA384_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 { "sha384", mbedtls_sha384_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100303#endif
304#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 { "sha512", mbedtls_sha512_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100306#endif
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200307#if defined(MBEDTLS_SHA3_C)
Pol Henarejosb3b220c2023-02-08 12:52:18 +0100308 { "sha3", mbedtls_sha3_self_test },
Pol Henarejos7dbd5d12022-05-20 20:42:33 +0200309#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100310#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 { "des", mbedtls_des_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100312#endif
313#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 { "aes", mbedtls_aes_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100315#endif
316#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 { "gcm", mbedtls_gcm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100318#endif
319#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 { "ccm", mbedtls_ccm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100321#endif
Ron Eldor9ab746c2018-07-15 09:33:07 +0300322#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 { "nist_kw", mbedtls_nist_kw_self_test },
Ron Eldor9ab746c2018-07-15 09:33:07 +0300324#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100325#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 { "cmac", mbedtls_cmac_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100327#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300328#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 { "chacha20", mbedtls_chacha20_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300330#endif
331#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 { "poly1305", mbedtls_poly1305_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300333#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200334#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 { "chacha20-poly1305", mbedtls_chachapoly_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300336#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100337#if defined(MBEDTLS_BASE64_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 { "base64", mbedtls_base64_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100339#endif
340#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 { "mpi", mbedtls_mpi_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100342#endif
343#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 { "rsa", mbedtls_rsa_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100345#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100346#if defined(MBEDTLS_CAMELLIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 { "camellia", mbedtls_camellia_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100348#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000349#if defined(MBEDTLS_ARIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 { "aria", mbedtls_aria_self_test },
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000351#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100352#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 { "ctr_drbg", mbedtls_ctr_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100354#endif
355#if defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 { "hmac_drbg", mbedtls_hmac_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100357#endif
358#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 { "ecp", mbedtls_ecp_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100360#endif
361#if defined(MBEDTLS_ECJPAKE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 { "ecjpake", mbedtls_ecjpake_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100363#endif
364#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 { "dhm", mbedtls_dhm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100366#endif
367#if defined(MBEDTLS_ENTROPY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 { "entropy", mbedtls_entropy_self_test_wrapper },
Gilles Peskine319ac802017-12-15 14:57:18 +0100369#endif
370#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 { "pkcs5", mbedtls_pkcs5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100372#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100373/* Heap test comes last */
374#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100376#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 { NULL, NULL }
Gilles Peskine319ac802017-12-15 14:57:18 +0100378};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100379#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000382{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100383#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100384 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100385#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100386 char **argp;
387 int v = 1; /* v=1 for verbose mode */
388 int exclude_mode = 0;
389 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100390#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200391 unsigned char buf[1000000];
392#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200393 void *pointer;
394
395 /*
Dave Rodgman54647732023-02-10 16:16:17 +0000396 * Check some basic platform requirements as specified in README.md
397 */
398 if (SIZE_MAX < INT_MAX || SIZE_MAX < UINT_MAX) {
399 mbedtls_printf("SIZE_MAX must be at least as big as INT_MAX and UINT_MAX\n");
400 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
401 }
402
403 if (sizeof(int) < 4) {
404 mbedtls_printf("int must be at least 32 bits\n");
405 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
406 }
407
408 if (sizeof(size_t) < 4) {
409 mbedtls_printf("size_t must be at least 32 bits\n");
410 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
411 }
412
413 uint32_t endian_test = 0x12345678;
414 char *p = (char *) &endian_test;
415 if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78) &&
416 !(p[3] == 0x12 && p[2] == 0x34 && p[1] == 0x56 && p[0] == 0x78)) {
417 mbedtls_printf("Mixed-endian platforms are not supported\n");
418 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
419 }
420
421 /*
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200422 * The C standard doesn't guarantee that all-bits-0 is the representation
423 * of a NULL pointer. We do however use that in our code for initializing
424 * structures, which should work on every modern platform. Let's be sure.
425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 memset(&pointer, 0, sizeof(void *));
427 if (pointer != NULL) {
428 mbedtls_printf("all-bits-zero is not a NULL pointer\n");
429 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200430 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000431
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200432 /*
Hanno Becker4ab38502018-10-16 13:22:44 +0100433 * The C standard allows padding bits in the representation
434 * of standard integer types, but our code does currently not
435 * support them.
436 *
437 * Here we check that the underlying C implementation doesn't
438 * use padding bits, and fail cleanly if it does.
439 *
440 * The check works by casting the maximum value representable
441 * by a given integer type into the unpadded integer type of the
442 * same bit-width and checking that it agrees with the maximum value
443 * of that unpadded type. For example, for a 4-byte int,
444 * MAX_INT should be 0x7fffffff in int32_t. This assumes that
445 * CHAR_BIT == 8, which is checked in check_config.h.
Hanno Beckerbaae59c2018-10-19 17:32:45 +0100446 *
447 * We assume that [u]intxx_t exist and that they don't
448 * have padding bits, as the standard requires.
Hanno Becker4ab38502018-10-16 13:22:44 +0100449 */
450
451#define CHECK_PADDING_SIGNED(TYPE, NAME) \
452 do \
453 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (sizeof(TYPE) == 2 || sizeof(TYPE) == 4 || \
455 sizeof(TYPE) == 8) { \
456 if ((sizeof(TYPE) == 2 && \
457 (int16_t) NAME ## _MAX != 0x7FFF) || \
458 (sizeof(TYPE) == 4 && \
459 (int32_t) NAME ## _MAX != 0x7FFFFFFF) || \
460 (sizeof(TYPE) == 8 && \
461 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF)) \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100462 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
464 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100465 } \
466 } else { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 mbedtls_printf("Padding checks only implemented for types of size 2, 4 or 8" \
468 " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n", \
469 sizeof(TYPE)); \
470 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100471 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100473
474#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
475 do \
476 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if ((sizeof(TYPE) == 2 && \
478 (uint16_t) NAME ## _MAX != 0xFFFF) || \
479 (sizeof(TYPE) == 4 && \
480 (uint32_t) NAME ## _MAX != 0xFFFFFFFF) || \
481 (sizeof(TYPE) == 8 && \
482 (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF)) \
Hanno Becker4ab38502018-10-16 13:22:44 +0100483 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
485 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100486 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 CHECK_PADDING_SIGNED(short, SHRT);
490 CHECK_PADDING_SIGNED(int, INT);
491 CHECK_PADDING_SIGNED(long, LONG);
492 CHECK_PADDING_SIGNED(long long, LLONG);
493 CHECK_PADDING_SIGNED(ptrdiff_t, PTRDIFF);
Hanno Becker4ab38502018-10-16 13:22:44 +0100494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 CHECK_PADDING_UNSIGNED(unsigned short, USHRT);
496 CHECK_PADDING_UNSIGNED(unsigned, UINT);
497 CHECK_PADDING_UNSIGNED(unsigned long, ULONG);
498 CHECK_PADDING_UNSIGNED(unsigned long long, ULLONG);
499 CHECK_PADDING_UNSIGNED(size_t, SIZE);
Hanno Becker4ab38502018-10-16 13:22:44 +0100500
501#undef CHECK_PADDING_SIGNED
502#undef CHECK_PADDING_UNSIGNED
503
504 /*
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200505 * Make sure we have a snprintf that correctly zero-terminates
506 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (run_test_snprintf() != 0) {
508 mbedtls_printf("the snprintf implementation is broken\n");
509 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200510 }
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
513 if (strcmp(*argp, "--quiet") == 0 ||
514 strcmp(*argp, "-q") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100515 v = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 } else if (strcmp(*argp, "--exclude") == 0 ||
517 strcmp(*argp, "-x") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100518 exclude_mode = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100520 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 }
SimonB5a8afb82016-03-11 00:40:54 +0000522 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (v != 0) {
525 mbedtls_printf("\n");
526 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Paul Bakker6e339b52013-07-03 13:37:05 +0200532#endif
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (*argp != NULL && exclude_mode == 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100535 /* Run the specified tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 for (; *argp != NULL; argp++) {
537 for (test = selftests; test->name != NULL; test++) {
538 if (!strcmp(*argp, test->name)) {
539 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100540 suites_failed++;
541 }
542 suites_tested++;
543 break;
544 }
545 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (test->name == NULL) {
547 mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp);
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100548 suites_failed++;
549 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100550 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100552 /* Run all the tests except excluded ones */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 for (test = selftests; test->name != NULL; test++) {
554 if (exclude_mode) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100555 char **excluded;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 for (excluded = argp; *excluded != NULL; ++excluded) {
557 if (!strcmp(*excluded, test->name)) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100558 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100560 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (*excluded) {
562 if (v) {
563 mbedtls_printf(" Skip: %s\n", test->name);
564 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100565 continue;
566 }
567 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100569 suites_failed++;
570 }
571 suites_tested++;
572 }
SimonB5a8afb82016-03-11 00:40:54 +0000573 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100574
Paul Bakker70940ca2016-07-14 14:30:45 +0100575#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100576 (void) exclude_mode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
Paul Bakker70940ca2016-07-14 14:30:45 +0100578#endif
579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (v != 0) {
581 mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
SimonB5a8afb82016-03-11 00:40:54 +0000582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (suites_failed > 0) {
584 mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
585 } else {
586 mbedtls_printf(" [ All tests PASS ]\n\n");
SimonB5a8afb82016-03-11 00:40:54 +0000587 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (suites_failed > 0) {
591 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
592 }
SimonB5a8afb82016-03-11 00:40:54 +0000593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
Paul Bakker5121ce52009-01-03 21:22:43 +0000595}