blob: 13423a1a6af36c39724e79fe196b412f2d060027 [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"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/des.h"
37#include "mbedtls/aes.h"
38#include "mbedtls/camellia.h"
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +000039#include "mbedtls/aria.h"
Daniel King4d8f87b2016-05-18 10:09:28 -030040#include "mbedtls/chacha20.h"
41#include "mbedtls/poly1305.h"
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +020042#include "mbedtls/chachapoly.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/base64.h"
44#include "mbedtls/bignum.h"
45#include "mbedtls/rsa.h"
46#include "mbedtls/x509.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020049#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/timing.h"
Ron Eldor9ab746c2018-07-15 09:33:07 +030051#include "mbedtls/nist_kw.h"
Dave Rodgman8f5a29a2022-04-11 12:59:45 +010052#include "mbedtls/debug.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Hanno Becker4ab38502018-10-16 13:22:44 +010054#include <limits.h>
Rich Evans18b78c72015-02-11 14:06:19 +000055#include <string.h>
56
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020061#endif
62
Simon Butcher63cb97e2018-12-06 17:43:31 +000063
Gilles Peskine6fc21f62019-09-17 18:18:58 +020064#if defined MBEDTLS_SELF_TEST
65/* Sanity check for malloc. This is not expected to fail, and is rather
66 * intended to display potentially useful information about the platform,
67 * in particular the behavior of malloc(0). */
Gilles Peskine449bd832023-01-11 14:50:10 +010068static int calloc_self_test(int verbose)
Gilles Peskine6fc21f62019-09-17 18:18:58 +020069{
70 int failures = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010071 void *empty1 = mbedtls_calloc(0, 1);
72 void *empty2 = mbedtls_calloc(0, 1);
73 void *buffer1 = mbedtls_calloc(1, 1);
74 void *buffer2 = mbedtls_calloc(1, 1);
Gilles Peskine6fc21f62019-09-17 18:18:58 +020075
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if (empty1 == NULL && empty2 == NULL) {
77 if (verbose) {
78 mbedtls_printf(" CALLOC(0): passed (NULL)\n");
79 }
80 } else if (empty1 == NULL || empty2 == NULL) {
81 if (verbose) {
82 mbedtls_printf(" CALLOC(0): failed (mix of NULL and non-NULL)\n");
83 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020084 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 } else if (empty1 == empty2) {
86 if (verbose) {
87 mbedtls_printf(" CALLOC(0): passed (same non-null)\n");
88 }
89 } else {
90 if (verbose) {
91 mbedtls_printf(" CALLOC(0): passed (distinct non-null)\n");
92 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020093 }
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (buffer1 == NULL || buffer2 == NULL) {
96 if (verbose) {
97 mbedtls_printf(" CALLOC(1): failed (NULL)\n");
98 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +020099 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 } else if (buffer1 == buffer2) {
101 if (verbose) {
102 mbedtls_printf(" CALLOC(1): failed (same buffer twice)\n");
103 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200104 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 } else {
106 if (verbose) {
107 mbedtls_printf(" CALLOC(1): passed\n");
108 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200109 }
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_free(buffer1);
112 buffer1 = mbedtls_calloc(1, 1);
113 if (buffer1 == NULL) {
114 if (verbose) {
115 mbedtls_printf(" CALLOC(1 again): failed (NULL)\n");
116 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200117 ++failures;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 } else {
119 if (verbose) {
120 mbedtls_printf(" CALLOC(1 again): passed\n");
121 }
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (verbose) {
125 mbedtls_printf("\n");
126 }
127 mbedtls_free(empty1);
128 mbedtls_free(empty2);
129 mbedtls_free(buffer1);
130 mbedtls_free(buffer2);
131 return failures;
Gilles Peskine6fc21f62019-09-17 18:18:58 +0200132}
133#endif /* MBEDTLS_SELF_TEST */
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135static int test_snprintf(size_t n, const char *ref_buf, int ref_ret)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200136{
137 int ret;
138 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +0200139 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 ret = mbedtls_snprintf(buf, n, "%s", "123");
142 if (ret < 0 || (size_t) ret >= n) {
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200143 ret = -1;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200144 }
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if (strncmp(ref_buf, buf, sizeof(buf)) != 0 ||
147 ref_ret != ret ||
148 memcmp(buf + n, ref + n, sizeof(buf) - n) != 0) {
149 return 1;
150 }
151
152 return 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200153}
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155static int run_test_snprintf(void)
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200156{
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return test_snprintf(0, "xxxxxxxxx", -1) != 0 ||
158 test_snprintf(1, "", -1) != 0 ||
159 test_snprintf(2, "1", -1) != 0 ||
160 test_snprintf(3, "12", -1) != 0 ||
161 test_snprintf(4, "123", 3) != 0 ||
162 test_snprintf(5, "123", 3) != 0;
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200163}
164
Simon Butcherb6a73c92016-06-18 22:45:37 +0100165/*
166 * Check if a seed file is present, and if not create one for the entropy
167 * self-test. If this fails, we attempt the test anyway, so no error is passed
168 * back.
169 */
Gilles Peskine319ac802017-12-15 14:57:18 +0100170#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
171#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static void create_entropy_seed_file(void)
Simon Butcherb6a73c92016-06-18 22:45:37 +0100173{
174 int result;
175 size_t output_len = 0;
176 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
177
178 /* Attempt to read the entropy seed file. If this fails - attempt to write
179 * to the file to ensure one is present. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 result = mbedtls_platform_std_nv_seed_read(seed_value,
181 MBEDTLS_ENTROPY_BLOCK_SIZE);
182 if (0 == result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100183 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 result = mbedtls_platform_entropy_poll(NULL,
187 seed_value,
188 MBEDTLS_ENTROPY_BLOCK_SIZE,
189 &output_len);
190 if (0 != result) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100191 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (MBEDTLS_ENTROPY_BLOCK_SIZE != output_len) {
Simon Butcherb6a73c92016-06-18 22:45:37 +0100195 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 }
Simon Butcherb6a73c92016-06-18 22:45:37 +0100197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_platform_std_nv_seed_write(seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE);
Simon Butcherb6a73c92016-06-18 22:45:37 +0100199}
200#endif
201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202int mbedtls_entropy_self_test_wrapper(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100203{
204#if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 create_entropy_seed_file();
Gilles Peskine319ac802017-12-15 14:57:18 +0100206#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 return mbedtls_entropy_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100208}
209#endif
210
211#if defined(MBEDTLS_SELF_TEST)
212#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100213int mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)
Gilles Peskine319ac802017-12-15 14:57:18 +0100214{
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (verbose != 0) {
Gilles Peskine319ac802017-12-15 14:57:18 +0100216#if defined(MBEDTLS_MEMORY_DEBUG)
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_memory_buffer_alloc_status();
Gilles Peskine319ac802017-12-15 14:57:18 +0100218#endif
219 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_memory_buffer_alloc_free();
221 return mbedtls_memory_buffer_alloc_self_test(verbose);
Gilles Peskine319ac802017-12-15 14:57:18 +0100222}
223#endif
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225typedef struct {
Gilles Peskine319ac802017-12-15 14:57:18 +0100226 const char *name;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 int (*function)(int);
Gilles Peskine319ac802017-12-15 14:57:18 +0100228} selftest_t;
229
230const selftest_t selftests[] =
231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 { "calloc", calloc_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100233#if defined(MBEDTLS_MD5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 { "md5", mbedtls_md5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100235#endif
236#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 { "ripemd160", mbedtls_ripemd160_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100238#endif
239#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 { "sha1", mbedtls_sha1_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100241#endif
Valerio Setti46e8fd82022-12-14 10:58:02 +0100242#if defined(MBEDTLS_SHA224_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 { "sha224", mbedtls_sha224_self_test },
Valerio Setti46e8fd82022-12-14 10:58:02 +0100244#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100245#if defined(MBEDTLS_SHA256_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 { "sha256", mbedtls_sha256_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100247#endif
Valerio Setti898e7a32022-12-14 08:55:53 +0100248#if defined(MBEDTLS_SHA384_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 { "sha384", mbedtls_sha384_self_test },
Valerio Setti898e7a32022-12-14 08:55:53 +0100250#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100251#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 { "sha512", mbedtls_sha512_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100253#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100254#if defined(MBEDTLS_DES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 { "des", mbedtls_des_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100256#endif
257#if defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 { "aes", mbedtls_aes_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100259#endif
260#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 { "gcm", mbedtls_gcm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100262#endif
263#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 { "ccm", mbedtls_ccm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100265#endif
Ron Eldor9ab746c2018-07-15 09:33:07 +0300266#if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 { "nist_kw", mbedtls_nist_kw_self_test },
Ron Eldor9ab746c2018-07-15 09:33:07 +0300268#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100269#if defined(MBEDTLS_CMAC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 { "cmac", mbedtls_cmac_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100271#endif
Daniel King4d8f87b2016-05-18 10:09:28 -0300272#if defined(MBEDTLS_CHACHA20_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 { "chacha20", mbedtls_chacha20_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300274#endif
275#if defined(MBEDTLS_POLY1305_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 { "poly1305", mbedtls_poly1305_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300277#endif
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200278#if defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 { "chacha20-poly1305", mbedtls_chachapoly_self_test },
Daniel King4d8f87b2016-05-18 10:09:28 -0300280#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100281#if defined(MBEDTLS_BASE64_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 { "base64", mbedtls_base64_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100283#endif
284#if defined(MBEDTLS_BIGNUM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 { "mpi", mbedtls_mpi_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100286#endif
287#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 { "rsa", mbedtls_rsa_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100289#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100290#if defined(MBEDTLS_CAMELLIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 { "camellia", mbedtls_camellia_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100292#endif
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000293#if defined(MBEDTLS_ARIA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 { "aria", mbedtls_aria_self_test },
Markku-Juhani O. Saarinen3c0b53b2017-11-30 16:00:34 +0000295#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100296#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 { "ctr_drbg", mbedtls_ctr_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100298#endif
299#if defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 { "hmac_drbg", mbedtls_hmac_drbg_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100301#endif
302#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 { "ecp", mbedtls_ecp_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100304#endif
305#if defined(MBEDTLS_ECJPAKE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 { "ecjpake", mbedtls_ecjpake_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100307#endif
308#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 { "dhm", mbedtls_dhm_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100310#endif
311#if defined(MBEDTLS_ENTROPY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 { "entropy", mbedtls_entropy_self_test_wrapper },
Gilles Peskine319ac802017-12-15 14:57:18 +0100313#endif
314#if defined(MBEDTLS_PKCS5_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 { "pkcs5", mbedtls_pkcs5_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100316#endif
Gilles Peskine319ac802017-12-15 14:57:18 +0100317/* Heap test comes last */
318#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 { "memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test },
Gilles Peskine319ac802017-12-15 14:57:18 +0100320#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 { NULL, NULL }
Gilles Peskine319ac802017-12-15 14:57:18 +0100322};
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100323#endif /* MBEDTLS_SELF_TEST */
Gilles Peskine319ac802017-12-15 14:57:18 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325int main(int argc, char *argv[])
Paul Bakker5121ce52009-01-03 21:22:43 +0000326{
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100327#if defined(MBEDTLS_SELF_TEST)
Gilles Peskine319ac802017-12-15 14:57:18 +0100328 const selftest_t *test;
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100329#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineff79d272017-12-20 18:09:27 +0100330 char **argp;
331 int v = 1; /* v=1 for verbose mode */
332 int exclude_mode = 0;
333 int suites_tested = 0, suites_failed = 0;
Paul Bakker70940ca2016-07-14 14:30:45 +0100334#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200335 unsigned char buf[1000000];
336#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200337 void *pointer;
338
339 /*
Dave Rodgman54647732023-02-10 16:16:17 +0000340 * Check some basic platform requirements as specified in README.md
341 */
342 if (SIZE_MAX < INT_MAX || SIZE_MAX < UINT_MAX) {
343 mbedtls_printf("SIZE_MAX must be at least as big as INT_MAX and UINT_MAX\n");
344 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
345 }
346
347 if (sizeof(int) < 4) {
348 mbedtls_printf("int must be at least 32 bits\n");
349 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
350 }
351
352 if (sizeof(size_t) < 4) {
353 mbedtls_printf("size_t must be at least 32 bits\n");
354 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
355 }
356
357 uint32_t endian_test = 0x12345678;
358 char *p = (char *) &endian_test;
359 if (!(p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78) &&
360 !(p[3] == 0x12 && p[2] == 0x34 && p[1] == 0x56 && p[0] == 0x78)) {
361 mbedtls_printf("Mixed-endian platforms are not supported\n");
362 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
363 }
364
365 /*
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200366 * The C standard doesn't guarantee that all-bits-0 is the representation
367 * of a NULL pointer. We do however use that in our code for initializing
368 * structures, which should work on every modern platform. Let's be sure.
369 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 memset(&pointer, 0, sizeof(void *));
371 if (pointer != NULL) {
372 mbedtls_printf("all-bits-zero is not a NULL pointer\n");
373 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200374 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200376 /*
Hanno Becker4ab38502018-10-16 13:22:44 +0100377 * The C standard allows padding bits in the representation
378 * of standard integer types, but our code does currently not
379 * support them.
380 *
381 * Here we check that the underlying C implementation doesn't
382 * use padding bits, and fail cleanly if it does.
383 *
384 * The check works by casting the maximum value representable
385 * by a given integer type into the unpadded integer type of the
386 * same bit-width and checking that it agrees with the maximum value
387 * of that unpadded type. For example, for a 4-byte int,
388 * MAX_INT should be 0x7fffffff in int32_t. This assumes that
389 * CHAR_BIT == 8, which is checked in check_config.h.
Hanno Beckerbaae59c2018-10-19 17:32:45 +0100390 *
391 * We assume that [u]intxx_t exist and that they don't
392 * have padding bits, as the standard requires.
Hanno Becker4ab38502018-10-16 13:22:44 +0100393 */
394
395#define CHECK_PADDING_SIGNED(TYPE, NAME) \
396 do \
397 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (sizeof(TYPE) == 2 || sizeof(TYPE) == 4 || \
399 sizeof(TYPE) == 8) { \
400 if ((sizeof(TYPE) == 2 && \
401 (int16_t) NAME ## _MAX != 0x7FFF) || \
402 (sizeof(TYPE) == 4 && \
403 (int32_t) NAME ## _MAX != 0x7FFFFFFF) || \
404 (sizeof(TYPE) == 8 && \
405 (int64_t) NAME ## _MAX != 0x7FFFFFFFFFFFFFFF)) \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100406 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
408 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Dave Rodgmane2e7e942022-04-08 12:41:23 +0100409 } \
410 } else { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 mbedtls_printf("Padding checks only implemented for types of size 2, 4 or 8" \
412 " - cannot check type '" #TYPE "' of size %" MBEDTLS_PRINTF_SIZET "\n", \
413 sizeof(TYPE)); \
414 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100415 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100417
418#define CHECK_PADDING_UNSIGNED(TYPE, NAME) \
419 do \
420 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if ((sizeof(TYPE) == 2 && \
422 (uint16_t) NAME ## _MAX != 0xFFFF) || \
423 (sizeof(TYPE) == 4 && \
424 (uint32_t) NAME ## _MAX != 0xFFFFFFFF) || \
425 (sizeof(TYPE) == 8 && \
426 (uint64_t) NAME ## _MAX != 0xFFFFFFFFFFFFFFFF)) \
Hanno Becker4ab38502018-10-16 13:22:44 +0100427 { \
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 mbedtls_printf("Type '" #TYPE "' has padding bits\n"); \
429 mbedtls_exit(MBEDTLS_EXIT_FAILURE); \
Hanno Becker4ab38502018-10-16 13:22:44 +0100430 } \
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 } while (0)
Hanno Becker4ab38502018-10-16 13:22:44 +0100432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 CHECK_PADDING_SIGNED(short, SHRT);
434 CHECK_PADDING_SIGNED(int, INT);
435 CHECK_PADDING_SIGNED(long, LONG);
436 CHECK_PADDING_SIGNED(long long, LLONG);
437 CHECK_PADDING_SIGNED(ptrdiff_t, PTRDIFF);
Hanno Becker4ab38502018-10-16 13:22:44 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 CHECK_PADDING_UNSIGNED(unsigned short, USHRT);
440 CHECK_PADDING_UNSIGNED(unsigned, UINT);
441 CHECK_PADDING_UNSIGNED(unsigned long, ULONG);
442 CHECK_PADDING_UNSIGNED(unsigned long long, ULLONG);
443 CHECK_PADDING_UNSIGNED(size_t, SIZE);
Hanno Becker4ab38502018-10-16 13:22:44 +0100444
445#undef CHECK_PADDING_SIGNED
446#undef CHECK_PADDING_UNSIGNED
447
448 /*
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200449 * Make sure we have a snprintf that correctly zero-terminates
450 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (run_test_snprintf() != 0) {
452 mbedtls_printf("the snprintf implementation is broken\n");
453 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200454 }
455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 for (argp = argv + (argc >= 1 ? 1 : argc); *argp != NULL; ++argp) {
457 if (strcmp(*argp, "--quiet") == 0 ||
458 strcmp(*argp, "-q") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100459 v = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 } else if (strcmp(*argp, "--exclude") == 0 ||
461 strcmp(*argp, "-x") == 0) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100462 exclude_mode = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100464 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 }
SimonB5a8afb82016-03-11 00:40:54 +0000466 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (v != 0) {
469 mbedtls_printf("\n");
470 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 mbedtls_memory_buffer_alloc_init(buf, sizeof(buf));
Paul Bakker6e339b52013-07-03 13:37:05 +0200476#endif
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if (*argp != NULL && exclude_mode == 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100479 /* Run the specified tests */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 for (; *argp != NULL; argp++) {
481 for (test = selftests; test->name != NULL; test++) {
482 if (!strcmp(*argp, test->name)) {
483 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100484 suites_failed++;
485 }
486 suites_tested++;
487 break;
488 }
489 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (test->name == NULL) {
491 mbedtls_printf(" Test suite %s not available -> failed\n\n", *argp);
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100492 suites_failed++;
493 }
Gilles Peskine319ac802017-12-15 14:57:18 +0100494 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 } else {
Gilles Peskineff79d272017-12-20 18:09:27 +0100496 /* Run all the tests except excluded ones */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 for (test = selftests; test->name != NULL; test++) {
498 if (exclude_mode) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100499 char **excluded;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 for (excluded = argp; *excluded != NULL; ++excluded) {
501 if (!strcmp(*excluded, test->name)) {
Gilles Peskineff79d272017-12-20 18:09:27 +0100502 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100504 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (*excluded) {
506 if (v) {
507 mbedtls_printf(" Skip: %s\n", test->name);
508 }
Gilles Peskineff79d272017-12-20 18:09:27 +0100509 continue;
510 }
511 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (test->function(v) != 0) {
Gilles Peskinec82fbb42017-12-15 15:01:27 +0100513 suites_failed++;
514 }
515 suites_tested++;
516 }
SimonB5a8afb82016-03-11 00:40:54 +0000517 }
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100518
Paul Bakker70940ca2016-07-14 14:30:45 +0100519#else
Gilles Peskineff79d272017-12-20 18:09:27 +0100520 (void) exclude_mode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 mbedtls_printf(" MBEDTLS_SELF_TEST not defined.\n");
Paul Bakker70940ca2016-07-14 14:30:45 +0100522#endif
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (v != 0) {
525 mbedtls_printf(" Executed %d test suites\n\n", suites_tested);
SimonB5a8afb82016-03-11 00:40:54 +0000526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (suites_failed > 0) {
528 mbedtls_printf(" [ %d tests FAIL ]\n\n", suites_failed);
529 } else {
530 mbedtls_printf(" [ All tests PASS ]\n\n");
SimonB5a8afb82016-03-11 00:40:54 +0000531 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (suites_failed > 0) {
535 mbedtls_exit(MBEDTLS_EXIT_FAILURE);
536 }
SimonB5a8afb82016-03-11 00:40:54 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 mbedtls_exit(MBEDTLS_EXIT_SUCCESS);
Paul Bakker5121ce52009-01-03 21:22:43 +0000539}