Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Self-test demonstration program |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 22 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 26 | #include "mbedtls/entropy.h" |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 27 | #include "mbedtls/entropy_poll.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/hmac_drbg.h" |
| 29 | #include "mbedtls/ctr_drbg.h" |
| 30 | #include "mbedtls/dhm.h" |
| 31 | #include "mbedtls/gcm.h" |
| 32 | #include "mbedtls/ccm.h" |
Robert Cragie | dc5c7b9 | 2015-12-11 15:49:45 +0000 | [diff] [blame] | 33 | #include "mbedtls/cmac.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 34 | #include "mbedtls/md2.h" |
| 35 | #include "mbedtls/md4.h" |
| 36 | #include "mbedtls/md5.h" |
| 37 | #include "mbedtls/ripemd160.h" |
| 38 | #include "mbedtls/sha1.h" |
| 39 | #include "mbedtls/sha256.h" |
| 40 | #include "mbedtls/sha512.h" |
| 41 | #include "mbedtls/arc4.h" |
| 42 | #include "mbedtls/des.h" |
| 43 | #include "mbedtls/aes.h" |
| 44 | #include "mbedtls/camellia.h" |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 45 | #include "mbedtls/aria.h" |
Daniel King | 4d8f87b | 2016-05-18 10:09:28 -0300 | [diff] [blame] | 46 | #include "mbedtls/chacha20.h" |
| 47 | #include "mbedtls/poly1305.h" |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 48 | #include "mbedtls/chachapoly.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 49 | #include "mbedtls/base64.h" |
| 50 | #include "mbedtls/bignum.h" |
| 51 | #include "mbedtls/rsa.h" |
| 52 | #include "mbedtls/x509.h" |
| 53 | #include "mbedtls/xtea.h" |
| 54 | #include "mbedtls/pkcs5.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/ecp.h" |
Manuel Pégourié-Gonnard | 4d8685b | 2015-08-05 15:44:42 +0200 | [diff] [blame] | 56 | #include "mbedtls/ecjpake.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 57 | #include "mbedtls/timing.h" |
Ron Eldor | 9ab746c | 2018-07-15 09:33:07 +0300 | [diff] [blame] | 58 | #include "mbedtls/nist_kw.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 60 | #include <string.h> |
| 61 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 63 | #include "mbedtls/platform.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 64 | #else |
| 65 | #include <stdio.h> |
Janos Follath | 2e3aca2 | 2016-03-18 16:25:52 +0000 | [diff] [blame] | 66 | #include <stdlib.h> |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 67 | #define mbedtls_calloc calloc |
| 68 | #define mbedtls_free free |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 70 | #define mbedtls_snprintf snprintf |
Janos Follath | 2e3aca2 | 2016-03-18 16:25:52 +0000 | [diff] [blame] | 71 | #define mbedtls_exit exit |
| 72 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
| 73 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 74 | #endif |
| 75 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 77 | #include "mbedtls/memory_buffer_alloc.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 78 | #endif |
| 79 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 80 | |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 81 | #if defined MBEDTLS_SELF_TEST |
| 82 | /* Sanity check for malloc. This is not expected to fail, and is rather |
| 83 | * intended to display potentially useful information about the platform, |
| 84 | * in particular the behavior of malloc(0). */ |
| 85 | static int calloc_self_test( int verbose ) |
| 86 | { |
| 87 | int failures = 0; |
| 88 | void *empty1 = mbedtls_calloc( 0, 1 ); |
| 89 | void *empty2 = mbedtls_calloc( 0, 1 ); |
| 90 | void *buffer1 = mbedtls_calloc( 1, 1 ); |
| 91 | void *buffer2 = mbedtls_calloc( 1, 1 ); |
| 92 | uintptr_t old_buffer1; |
| 93 | |
| 94 | if( empty1 == NULL && empty2 == NULL ) |
| 95 | { |
| 96 | if( verbose ) |
| 97 | mbedtls_printf( " CALLOC(0): passed (NULL)\n" ); |
| 98 | } |
| 99 | else if( empty1 == NULL || empty2 == NULL ) |
| 100 | { |
| 101 | if( verbose ) |
| 102 | mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" ); |
| 103 | ++failures; |
| 104 | } |
| 105 | else if( empty1 == empty2 ) |
| 106 | { |
| 107 | if( verbose ) |
| 108 | mbedtls_printf( " CALLOC(0): passed (same non-null)\n" ); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | if( verbose ) |
| 113 | mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" ); |
| 114 | } |
| 115 | |
| 116 | if( buffer1 == NULL || buffer2 == NULL ) |
| 117 | { |
| 118 | if( verbose ) |
| 119 | mbedtls_printf( " CALLOC(1): failed (NULL)\n" ); |
| 120 | ++failures; |
| 121 | } |
| 122 | else if( buffer1 == buffer2 ) |
| 123 | { |
| 124 | if( verbose ) |
| 125 | mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" ); |
| 126 | ++failures; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | if( verbose ) |
| 131 | mbedtls_printf( " CALLOC(1): passed\n" ); |
| 132 | } |
| 133 | |
| 134 | old_buffer1 = (uintptr_t) buffer1; |
| 135 | mbedtls_free( buffer1 ); |
| 136 | buffer1 = mbedtls_calloc( 1, 1 ); |
| 137 | if( buffer1 == NULL ) |
| 138 | { |
| 139 | if( verbose ) |
| 140 | mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" ); |
| 141 | ++failures; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | if( verbose ) |
| 146 | mbedtls_printf( " CALLOC(1 again): passed (%s address)\n", |
| 147 | (uintptr_t) old_buffer1 == (uintptr_t) buffer1 ? |
| 148 | "same" : "different" ); |
| 149 | } |
| 150 | |
| 151 | if( verbose ) |
| 152 | mbedtls_printf( "\n" ); |
| 153 | mbedtls_free( empty1 ); |
| 154 | mbedtls_free( empty2 ); |
| 155 | mbedtls_free( buffer1 ); |
| 156 | mbedtls_free( buffer2 ); |
| 157 | return( failures ); |
| 158 | } |
| 159 | #endif /* MBEDTLS_SELF_TEST */ |
| 160 | |
Rodrigo Dias Correa | 80448aa | 2020-11-10 02:28:50 -0300 | [diff] [blame] | 161 | static int test_snprintf( size_t n, const char *ref_buf, int ref_ret ) |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 162 | { |
| 163 | int ret; |
| 164 | char buf[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 165 | const char ref[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 166 | |
| 167 | ret = mbedtls_snprintf( buf, n, "%s", "123" ); |
| 168 | if( ret < 0 || (size_t) ret >= n ) |
| 169 | ret = -1; |
| 170 | |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 171 | if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 || |
| 172 | ref_ret != ret || |
| 173 | memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 ) |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 174 | { |
| 175 | return( 1 ); |
| 176 | } |
| 177 | |
| 178 | return( 0 ); |
| 179 | } |
| 180 | |
| 181 | static int run_test_snprintf( void ) |
| 182 | { |
| 183 | return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 || |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 184 | test_snprintf( 1, "", -1 ) != 0 || |
| 185 | test_snprintf( 2, "1", -1 ) != 0 || |
| 186 | test_snprintf( 3, "12", -1 ) != 0 || |
| 187 | test_snprintf( 4, "123", 3 ) != 0 || |
| 188 | test_snprintf( 5, "123", 3 ) != 0 ); |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 189 | } |
| 190 | |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 191 | /* |
| 192 | * Check if a seed file is present, and if not create one for the entropy |
| 193 | * self-test. If this fails, we attempt the test anyway, so no error is passed |
| 194 | * back. |
| 195 | */ |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 196 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C) |
| 197 | #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
Simon Butcher | b6a73c9 | 2016-06-18 22:45:37 +0100 | [diff] [blame] | 198 | static void create_entropy_seed_file( void ) |
| 199 | { |
| 200 | int result; |
| 201 | size_t output_len = 0; |
| 202 | unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 203 | |
| 204 | /* Attempt to read the entropy seed file. If this fails - attempt to write |
| 205 | * to the file to ensure one is present. */ |
| 206 | result = mbedtls_platform_std_nv_seed_read( seed_value, |
| 207 | MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 208 | if( 0 == result ) |
| 209 | return; |
| 210 | |
| 211 | result = mbedtls_platform_entropy_poll( NULL, |
| 212 | seed_value, |
| 213 | MBEDTLS_ENTROPY_BLOCK_SIZE, |
| 214 | &output_len ); |
| 215 | if( 0 != result ) |
| 216 | return; |
| 217 | |
| 218 | if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len ) |
| 219 | return; |
| 220 | |
| 221 | mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 222 | } |
| 223 | #endif |
| 224 | |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 225 | int mbedtls_entropy_self_test_wrapper( int verbose ) |
| 226 | { |
| 227 | #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
| 228 | create_entropy_seed_file( ); |
| 229 | #endif |
| 230 | return( mbedtls_entropy_self_test( verbose ) ); |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | #if defined(MBEDTLS_SELF_TEST) |
| 235 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 236 | int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose ) |
| 237 | { |
| 238 | if( verbose != 0 ) |
| 239 | { |
| 240 | #if defined(MBEDTLS_MEMORY_DEBUG) |
| 241 | mbedtls_memory_buffer_alloc_status( ); |
| 242 | #endif |
| 243 | } |
| 244 | mbedtls_memory_buffer_alloc_free( ); |
| 245 | return( mbedtls_memory_buffer_alloc_self_test( verbose ) ); |
| 246 | } |
| 247 | #endif |
| 248 | |
| 249 | typedef struct |
| 250 | { |
| 251 | const char *name; |
| 252 | int ( *function )( int ); |
| 253 | } selftest_t; |
| 254 | |
| 255 | const selftest_t selftests[] = |
| 256 | { |
Gilles Peskine | 6fc21f6 | 2019-09-17 18:18:58 +0200 | [diff] [blame] | 257 | {"calloc", calloc_self_test}, |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 258 | #if defined(MBEDTLS_MD2_C) |
| 259 | {"md2", mbedtls_md2_self_test}, |
| 260 | #endif |
| 261 | #if defined(MBEDTLS_MD4_C) |
| 262 | {"md4", mbedtls_md4_self_test}, |
| 263 | #endif |
| 264 | #if defined(MBEDTLS_MD5_C) |
| 265 | {"md5", mbedtls_md5_self_test}, |
| 266 | #endif |
| 267 | #if defined(MBEDTLS_RIPEMD160_C) |
| 268 | {"ripemd160", mbedtls_ripemd160_self_test}, |
| 269 | #endif |
| 270 | #if defined(MBEDTLS_SHA1_C) |
| 271 | {"sha1", mbedtls_sha1_self_test}, |
| 272 | #endif |
| 273 | #if defined(MBEDTLS_SHA256_C) |
| 274 | {"sha256", mbedtls_sha256_self_test}, |
| 275 | #endif |
| 276 | #if defined(MBEDTLS_SHA512_C) |
| 277 | {"sha512", mbedtls_sha512_self_test}, |
| 278 | #endif |
| 279 | #if defined(MBEDTLS_ARC4_C) |
| 280 | {"arc4", mbedtls_arc4_self_test}, |
| 281 | #endif |
| 282 | #if defined(MBEDTLS_DES_C) |
| 283 | {"des", mbedtls_des_self_test}, |
| 284 | #endif |
| 285 | #if defined(MBEDTLS_AES_C) |
| 286 | {"aes", mbedtls_aes_self_test}, |
| 287 | #endif |
| 288 | #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C) |
| 289 | {"gcm", mbedtls_gcm_self_test}, |
| 290 | #endif |
| 291 | #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C) |
| 292 | {"ccm", mbedtls_ccm_self_test}, |
| 293 | #endif |
Ron Eldor | 9ab746c | 2018-07-15 09:33:07 +0300 | [diff] [blame] | 294 | #if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C) |
| 295 | {"nist_kw", mbedtls_nist_kw_self_test}, |
| 296 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 297 | #if defined(MBEDTLS_CMAC_C) |
| 298 | {"cmac", mbedtls_cmac_self_test}, |
| 299 | #endif |
Daniel King | 4d8f87b | 2016-05-18 10:09:28 -0300 | [diff] [blame] | 300 | #if defined(MBEDTLS_CHACHA20_C) |
| 301 | {"chacha20", mbedtls_chacha20_self_test}, |
| 302 | #endif |
| 303 | #if defined(MBEDTLS_POLY1305_C) |
| 304 | {"poly1305", mbedtls_poly1305_self_test}, |
| 305 | #endif |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 306 | #if defined(MBEDTLS_CHACHAPOLY_C) |
| 307 | {"chacha20-poly1305", mbedtls_chachapoly_self_test}, |
Daniel King | 4d8f87b | 2016-05-18 10:09:28 -0300 | [diff] [blame] | 308 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 309 | #if defined(MBEDTLS_BASE64_C) |
| 310 | {"base64", mbedtls_base64_self_test}, |
| 311 | #endif |
| 312 | #if defined(MBEDTLS_BIGNUM_C) |
| 313 | {"mpi", mbedtls_mpi_self_test}, |
| 314 | #endif |
| 315 | #if defined(MBEDTLS_RSA_C) |
| 316 | {"rsa", mbedtls_rsa_self_test}, |
| 317 | #endif |
| 318 | #if defined(MBEDTLS_X509_USE_C) |
| 319 | {"x509", mbedtls_x509_self_test}, |
| 320 | #endif |
| 321 | #if defined(MBEDTLS_XTEA_C) |
| 322 | {"xtea", mbedtls_xtea_self_test}, |
| 323 | #endif |
| 324 | #if defined(MBEDTLS_CAMELLIA_C) |
| 325 | {"camellia", mbedtls_camellia_self_test}, |
| 326 | #endif |
Markku-Juhani O. Saarinen | 3c0b53b | 2017-11-30 16:00:34 +0000 | [diff] [blame] | 327 | #if defined(MBEDTLS_ARIA_C) |
| 328 | {"aria", mbedtls_aria_self_test}, |
| 329 | #endif |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 330 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 331 | {"ctr_drbg", mbedtls_ctr_drbg_self_test}, |
| 332 | #endif |
| 333 | #if defined(MBEDTLS_HMAC_DRBG_C) |
| 334 | {"hmac_drbg", mbedtls_hmac_drbg_self_test}, |
| 335 | #endif |
| 336 | #if defined(MBEDTLS_ECP_C) |
| 337 | {"ecp", mbedtls_ecp_self_test}, |
| 338 | #endif |
| 339 | #if defined(MBEDTLS_ECJPAKE_C) |
| 340 | {"ecjpake", mbedtls_ecjpake_self_test}, |
| 341 | #endif |
| 342 | #if defined(MBEDTLS_DHM_C) |
| 343 | {"dhm", mbedtls_dhm_self_test}, |
| 344 | #endif |
| 345 | #if defined(MBEDTLS_ENTROPY_C) |
| 346 | {"entropy", mbedtls_entropy_self_test_wrapper}, |
| 347 | #endif |
| 348 | #if defined(MBEDTLS_PKCS5_C) |
| 349 | {"pkcs5", mbedtls_pkcs5_self_test}, |
| 350 | #endif |
| 351 | /* Slower test after the faster ones */ |
| 352 | #if defined(MBEDTLS_TIMING_C) |
| 353 | {"timing", mbedtls_timing_self_test}, |
| 354 | #endif |
| 355 | /* Heap test comes last */ |
| 356 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 357 | {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test}, |
| 358 | #endif |
| 359 | {NULL, NULL} |
| 360 | }; |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 361 | #endif /* MBEDTLS_SELF_TEST */ |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 362 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | int main( int argc, char *argv[] ) |
| 364 | { |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 365 | #if defined(MBEDTLS_SELF_TEST) |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 366 | const selftest_t *test; |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 367 | #endif /* MBEDTLS_SELF_TEST */ |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 368 | char **argp; |
| 369 | int v = 1; /* v=1 for verbose mode */ |
| 370 | int exclude_mode = 0; |
| 371 | int suites_tested = 0, suites_failed = 0; |
Paul Bakker | 70940ca | 2016-07-14 14:30:45 +0100 | [diff] [blame] | 372 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 373 | unsigned char buf[1000000]; |
| 374 | #endif |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 375 | void *pointer; |
| 376 | |
| 377 | /* |
| 378 | * The C standard doesn't guarantee that all-bits-0 is the representation |
| 379 | * of a NULL pointer. We do however use that in our code for initializing |
| 380 | * structures, which should work on every modern platform. Let's be sure. |
| 381 | */ |
| 382 | memset( &pointer, 0, sizeof( void * ) ); |
| 383 | if( pointer != NULL ) |
| 384 | { |
| 385 | mbedtls_printf( "all-bits-zero is not a NULL pointer\n" ); |
Janos Follath | 2e3aca2 | 2016-03-18 16:25:52 +0000 | [diff] [blame] | 386 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 387 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 388 | |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 389 | /* |
| 390 | * Make sure we have a snprintf that correctly zero-terminates |
| 391 | */ |
| 392 | if( run_test_snprintf() != 0 ) |
| 393 | { |
| 394 | mbedtls_printf( "the snprintf implementation is broken\n" ); |
Janos Follath | 2e3aca2 | 2016-03-18 16:25:52 +0000 | [diff] [blame] | 395 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 398 | for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp ) |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 399 | { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 400 | if( strcmp( *argp, "--quiet" ) == 0 || |
| 401 | strcmp( *argp, "-q" ) == 0 ) |
| 402 | { |
| 403 | v = 0; |
| 404 | } |
| 405 | else if( strcmp( *argp, "--exclude" ) == 0 || |
| 406 | strcmp( *argp, "-x" ) == 0 ) |
| 407 | { |
| 408 | exclude_mode = 1; |
| 409 | } |
| 410 | else |
| 411 | break; |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 412 | } |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 413 | |
| 414 | if( v != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 416 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 417 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 418 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 420 | mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 421 | #endif |
| 422 | |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 423 | if( *argp != NULL && exclude_mode == 0 ) |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 424 | { |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 425 | /* Run the specified tests */ |
| 426 | for( ; *argp != NULL; argp++ ) |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 427 | { |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 428 | for( test = selftests; test->name != NULL; test++ ) |
| 429 | { |
| 430 | if( !strcmp( *argp, test->name ) ) |
| 431 | { |
| 432 | if( test->function( v ) != 0 ) |
| 433 | { |
| 434 | suites_failed++; |
| 435 | } |
| 436 | suites_tested++; |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | if( test->name == NULL ) |
| 441 | { |
| 442 | mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp ); |
| 443 | suites_failed++; |
| 444 | } |
Gilles Peskine | 319ac80 | 2017-12-15 14:57:18 +0100 | [diff] [blame] | 445 | } |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 446 | } |
| 447 | else |
| 448 | { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 449 | /* Run all the tests except excluded ones */ |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 450 | for( test = selftests; test->name != NULL; test++ ) |
| 451 | { |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 452 | if( exclude_mode ) |
| 453 | { |
| 454 | char **excluded; |
| 455 | for( excluded = argp; *excluded != NULL; ++excluded ) |
| 456 | { |
| 457 | if( !strcmp( *excluded, test->name ) ) |
| 458 | break; |
| 459 | } |
| 460 | if( *excluded ) |
| 461 | { |
| 462 | if( v ) |
| 463 | mbedtls_printf( " Skip: %s\n", test->name ); |
| 464 | continue; |
| 465 | } |
| 466 | } |
Gilles Peskine | c82fbb4 | 2017-12-15 15:01:27 +0100 | [diff] [blame] | 467 | if( test->function( v ) != 0 ) |
| 468 | { |
| 469 | suites_failed++; |
| 470 | } |
| 471 | suites_tested++; |
| 472 | } |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 473 | } |
Manuel Pégourié-Gonnard | 5ba1d52 | 2014-11-27 11:33:55 +0100 | [diff] [blame] | 474 | |
Paul Bakker | 70940ca | 2016-07-14 14:30:45 +0100 | [diff] [blame] | 475 | #else |
Gilles Peskine | ff79d27 | 2017-12-20 18:09:27 +0100 | [diff] [blame] | 476 | (void) exclude_mode; |
Paul Bakker | 70940ca | 2016-07-14 14:30:45 +0100 | [diff] [blame] | 477 | mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" ); |
| 478 | #endif |
| 479 | |
Manuel Pégourié-Gonnard | 5ba1d52 | 2014-11-27 11:33:55 +0100 | [diff] [blame] | 480 | if( v != 0 ) |
| 481 | { |
Simon Butcher | f154763 | 2016-03-14 23:09:39 +0000 | [diff] [blame] | 482 | mbedtls_printf( " Executed %d test suites\n\n", suites_tested ); |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 483 | |
| 484 | if( suites_failed > 0) |
| 485 | { |
| 486 | mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed ); |
| 487 | } |
| 488 | else |
| 489 | { |
| 490 | mbedtls_printf( " [ All tests PASS ]\n\n" ); |
| 491 | } |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 492 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 493 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 494 | fflush( stdout ); getchar(); |
| 495 | #endif |
| 496 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 | |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 498 | if( suites_failed > 0) |
Janos Follath | 2e3aca2 | 2016-03-18 16:25:52 +0000 | [diff] [blame] | 499 | mbedtls_exit( MBEDTLS_EXIT_FAILURE ); |
SimonB | 5a8afb8 | 2016-03-11 00:40:54 +0000 | [diff] [blame] | 500 | |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 501 | mbedtls_exit( MBEDTLS_EXIT_SUCCESS ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 502 | } |