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