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