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