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