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