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