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" |
| 29 | #include "mbedtls/hmac_drbg.h" |
| 30 | #include "mbedtls/ctr_drbg.h" |
| 31 | #include "mbedtls/dhm.h" |
| 32 | #include "mbedtls/gcm.h" |
| 33 | #include "mbedtls/ccm.h" |
| 34 | #include "mbedtls/md2.h" |
| 35 | #include "mbedtls/md4.h" |
| 36 | #include "mbedtls/md5.h" |
| 37 | #include "mbedtls/ripemd160.h" |
| 38 | #include "mbedtls/sha1.h" |
| 39 | #include "mbedtls/sha256.h" |
| 40 | #include "mbedtls/sha512.h" |
| 41 | #include "mbedtls/arc4.h" |
| 42 | #include "mbedtls/des.h" |
| 43 | #include "mbedtls/aes.h" |
| 44 | #include "mbedtls/camellia.h" |
| 45 | #include "mbedtls/base64.h" |
| 46 | #include "mbedtls/bignum.h" |
| 47 | #include "mbedtls/rsa.h" |
| 48 | #include "mbedtls/x509.h" |
| 49 | #include "mbedtls/xtea.h" |
| 50 | #include "mbedtls/pkcs5.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 51 | #include "mbedtls/ecp.h" |
| 52 | #include "mbedtls/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 54 | #include <stdio.h> |
| 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> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 62 | #define mbedtls_snprintf snprintf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 66 | #include "mbedtls/memory_buffer_alloc.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 67 | #endif |
| 68 | |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 69 | static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret ) |
| 70 | { |
| 71 | int ret; |
| 72 | char buf[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 73 | const char ref[10] = "xxxxxxxxx"; |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 74 | |
| 75 | ret = mbedtls_snprintf( buf, n, "%s", "123" ); |
| 76 | if( ret < 0 || (size_t) ret >= n ) |
| 77 | ret = -1; |
| 78 | |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 79 | if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 || |
| 80 | ref_ret != ret || |
| 81 | memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 ) |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 82 | { |
| 83 | return( 1 ); |
| 84 | } |
| 85 | |
| 86 | return( 0 ); |
| 87 | } |
| 88 | |
| 89 | static int run_test_snprintf( void ) |
| 90 | { |
| 91 | return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 || |
Manuel Pégourié-Gonnard | 4b00f08 | 2015-06-26 11:24:32 +0200 | [diff] [blame] | 92 | test_snprintf( 1, "", -1 ) != 0 || |
| 93 | test_snprintf( 2, "1", -1 ) != 0 || |
| 94 | test_snprintf( 3, "12", -1 ) != 0 || |
| 95 | test_snprintf( 4, "123", 3 ) != 0 || |
| 96 | test_snprintf( 5, "123", 3 ) != 0 ); |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 99 | #if defined(MBEDTLS_SELF_TEST) |
| 100 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 101 | int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose ) |
| 102 | { |
| 103 | if( verbose != 0 ) |
| 104 | { |
| 105 | #if defined(MBEDTLS_MEMORY_DEBUG) |
| 106 | mbedtls_memory_buffer_alloc_status( ); |
| 107 | #endif |
| 108 | } |
| 109 | mbedtls_memory_buffer_alloc_free( ); |
| 110 | return( mbedtls_memory_buffer_alloc_self_test( verbose ) ); |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | typedef struct |
| 115 | { |
| 116 | const char *name; |
| 117 | int ( *function )( int ); |
| 118 | } selftest_t; |
| 119 | |
| 120 | const selftest_t selftests[] = |
| 121 | { |
| 122 | #if defined(MBEDTLS_MD2_C) |
| 123 | {"md2", mbedtls_md2_self_test}, |
| 124 | #endif |
| 125 | #if defined(MBEDTLS_MD4_C) |
| 126 | {"md4", mbedtls_md4_self_test}, |
| 127 | #endif |
| 128 | #if defined(MBEDTLS_MD5_C) |
| 129 | {"md5", mbedtls_md5_self_test}, |
| 130 | #endif |
| 131 | #if defined(MBEDTLS_RIPEMD160_C) |
| 132 | {"ripemd160", mbedtls_ripemd160_self_test}, |
| 133 | #endif |
| 134 | #if defined(MBEDTLS_SHA1_C) |
| 135 | {"sha1", mbedtls_sha1_self_test}, |
| 136 | #endif |
| 137 | #if defined(MBEDTLS_SHA256_C) |
| 138 | {"sha256", mbedtls_sha256_self_test}, |
| 139 | #endif |
| 140 | #if defined(MBEDTLS_SHA512_C) |
| 141 | {"sha512", mbedtls_sha512_self_test}, |
| 142 | #endif |
| 143 | #if defined(MBEDTLS_ARC4_C) |
| 144 | {"arc4", mbedtls_arc4_self_test}, |
| 145 | #endif |
| 146 | #if defined(MBEDTLS_DES_C) |
| 147 | {"des", mbedtls_des_self_test}, |
| 148 | #endif |
| 149 | #if defined(MBEDTLS_AES_C) |
| 150 | {"aes", mbedtls_aes_self_test}, |
| 151 | #endif |
| 152 | #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C) |
| 153 | {"gcm", mbedtls_gcm_self_test}, |
| 154 | #endif |
| 155 | #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C) |
| 156 | {"ccm", mbedtls_ccm_self_test}, |
| 157 | #endif |
| 158 | #if defined(MBEDTLS_BASE64_C) |
| 159 | {"base64", mbedtls_base64_self_test}, |
| 160 | #endif |
| 161 | #if defined(MBEDTLS_BIGNUM_C) |
| 162 | {"mpi", mbedtls_mpi_self_test}, |
| 163 | #endif |
| 164 | #if defined(MBEDTLS_RSA_C) |
| 165 | {"rsa", mbedtls_rsa_self_test}, |
| 166 | #endif |
| 167 | #if defined(MBEDTLS_X509_USE_C) |
| 168 | {"x509", mbedtls_x509_self_test}, |
| 169 | #endif |
| 170 | #if defined(MBEDTLS_XTEA_C) |
| 171 | {"xtea", mbedtls_xtea_self_test}, |
| 172 | #endif |
| 173 | #if defined(MBEDTLS_CAMELLIA_C) |
| 174 | {"camellia", mbedtls_camellia_self_test}, |
| 175 | #endif |
| 176 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 177 | {"ctr_drbg", mbedtls_ctr_drbg_self_test}, |
| 178 | #endif |
| 179 | #if defined(MBEDTLS_HMAC_DRBG_C) |
| 180 | {"hmac_drbg", mbedtls_hmac_drbg_self_test}, |
| 181 | #endif |
| 182 | #if defined(MBEDTLS_ECP_C) |
| 183 | {"ecp", mbedtls_ecp_self_test}, |
| 184 | #endif |
| 185 | #if defined(MBEDTLS_DHM_C) |
| 186 | {"dhm", mbedtls_dhm_self_test}, |
| 187 | #endif |
| 188 | #if defined(MBEDTLS_ENTROPY_C) |
| 189 | {"entropy", mbedtls_entropy_self_test}, |
| 190 | #endif |
| 191 | #if defined(MBEDTLS_PKCS5_C) |
| 192 | {"pkcs5", mbedtls_pkcs5_self_test}, |
| 193 | #endif |
| 194 | /* Slower test after the faster ones */ |
| 195 | #if defined(MBEDTLS_TIMING_C) |
| 196 | {"timing", mbedtls_timing_self_test}, |
| 197 | #endif |
| 198 | /* Heap test comes last */ |
| 199 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 200 | {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test}, |
| 201 | #endif |
| 202 | {NULL, NULL} |
| 203 | }; |
| 204 | #endif /* MBEDTLS_SELF_TEST */ |
| 205 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | int main( int argc, char *argv[] ) |
| 207 | { |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 208 | #if defined(MBEDTLS_SELF_TEST) |
| 209 | const selftest_t *test; |
| 210 | #endif /* MBEDTLS_SELF_TEST */ |
Gilles Peskine | edede44 | 2017-12-15 15:01:27 +0100 | [diff] [blame^] | 211 | char **argp = argc >= 1 ? argv + 1 : argv; |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 212 | int v; |
| 213 | int suites_tested = 0, suites_failed = 0; |
Gilles Peskine | edede44 | 2017-12-15 15:01:27 +0100 | [diff] [blame^] | 214 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 215 | unsigned char buf[1000000]; |
| 216 | #endif |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 217 | void *pointer; |
| 218 | |
| 219 | /* |
| 220 | * The C standard doesn't guarantee that all-bits-0 is the representation |
| 221 | * of a NULL pointer. We do however use that in our code for initializing |
| 222 | * structures, which should work on every modern platform. Let's be sure. |
| 223 | */ |
| 224 | memset( &pointer, 0, sizeof( void * ) ); |
| 225 | if( pointer != NULL ) |
| 226 | { |
| 227 | mbedtls_printf( "all-bits-zero is not a NULL pointer\n" ); |
Gilles Peskine | 6d51b63 | 2017-12-20 20:25:03 +0100 | [diff] [blame] | 228 | return( EXIT_FAILURE ); |
Manuel Pégourié-Gonnard | d14acbc | 2015-05-29 11:26:37 +0200 | [diff] [blame] | 229 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 231 | /* |
| 232 | * Make sure we have a snprintf that correctly zero-terminates |
| 233 | */ |
| 234 | if( run_test_snprintf() != 0 ) |
| 235 | { |
| 236 | mbedtls_printf( "the snprintf implementation is broken\n" ); |
Gilles Peskine | 6d51b63 | 2017-12-20 20:25:03 +0100 | [diff] [blame] | 237 | return( EXIT_FAILURE ); |
Manuel Pégourié-Gonnard | 7b6dcbe | 2015-06-22 10:48:01 +0200 | [diff] [blame] | 238 | } |
| 239 | |
Gilles Peskine | edede44 | 2017-12-15 15:01:27 +0100 | [diff] [blame^] | 240 | if( argc >= 2 && ( strcmp( argv[1], "--quiet" ) == 0 || |
| 241 | strcmp( argv[1], "-quiet" ) == 0 || |
| 242 | strcmp( argv[1], "-q" ) == 0 ) ) |
| 243 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | v = 0; |
Gilles Peskine | edede44 | 2017-12-15 15:01:27 +0100 | [diff] [blame^] | 245 | ++argp; |
| 246 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 247 | else |
| 248 | { |
| 249 | v = 1; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 254 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 256 | mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 257 | #endif |
| 258 | |
Gilles Peskine | edede44 | 2017-12-15 15:01:27 +0100 | [diff] [blame^] | 259 | if( *argp != NULL ) |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 260 | { |
Gilles Peskine | edede44 | 2017-12-15 15:01:27 +0100 | [diff] [blame^] | 261 | /* Run the specified tests */ |
| 262 | for( ; *argp != NULL; argp++ ) |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 263 | { |
Gilles Peskine | edede44 | 2017-12-15 15:01:27 +0100 | [diff] [blame^] | 264 | for( test = selftests; test->name != NULL; test++ ) |
| 265 | { |
| 266 | if( !strcmp( *argp, test->name ) ) |
| 267 | { |
| 268 | if( test->function( v ) != 0 ) |
| 269 | { |
| 270 | suites_failed++; |
| 271 | } |
| 272 | suites_tested++; |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | if( test->name == NULL ) |
| 277 | { |
| 278 | mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp ); |
| 279 | suites_failed++; |
| 280 | } |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 281 | } |
Gilles Peskine | edede44 | 2017-12-15 15:01:27 +0100 | [diff] [blame^] | 282 | } |
| 283 | else |
| 284 | { |
| 285 | /* Run all the tests */ |
| 286 | for( test = selftests; test->name != NULL; test++ ) |
| 287 | { |
| 288 | if( test->function( v ) != 0 ) |
| 289 | { |
| 290 | suites_failed++; |
| 291 | } |
| 292 | suites_tested++; |
| 293 | } |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 294 | } |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 295 | |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 296 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" ); |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 298 | #endif |
| 299 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 | if( v != 0 ) |
| 301 | { |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 302 | mbedtls_printf( " Executed %d test suites\n\n", suites_tested ); |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 303 | |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 304 | if( suites_failed > 0) |
| 305 | { |
| 306 | mbedtls_printf( " [ %d tests FAILED ]\n\n", suites_failed ); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | mbedtls_printf( " [ All tests passed ]\n\n" ); |
| 311 | } |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 312 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 313 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 314 | fflush( stdout ); getchar(); |
| 315 | #endif |
| 316 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 317 | |
Gilles Peskine | 9d7dfb7 | 2017-12-20 20:23:46 +0100 | [diff] [blame] | 318 | if( suites_failed > 0) |
| 319 | return( EXIT_FAILURE ); |
| 320 | |
| 321 | return( EXIT_SUCCESS ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 322 | } |