Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Self-test demonstration program |
| 3 | * |
| 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
| 5 | * |
| 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Manuel Pégourié-Gonnard | cdee2d9 | 2015-08-07 09:40:51 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #if !defined(POLARSSL_CONFIG_FILE) |
| 24 | #include "mbedtls/config.h" |
| 25 | #else |
| 26 | #include MBEDTLS_CONFIG_FILE |
| 27 | #endif |
| 28 | |
| 29 | #include "mbedtls/entropy.h" |
| 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" |
| 35 | #include "mbedtls/md2.h" |
| 36 | #include "mbedtls/md4.h" |
| 37 | #include "mbedtls/md5.h" |
| 38 | #include "mbedtls/ripemd160.h" |
| 39 | #include "mbedtls/sha1.h" |
| 40 | #include "mbedtls/sha256.h" |
| 41 | #include "mbedtls/sha512.h" |
| 42 | #include "mbedtls/arc4.h" |
| 43 | #include "mbedtls/des.h" |
| 44 | #include "mbedtls/aes.h" |
| 45 | #include "mbedtls/camellia.h" |
| 46 | #include "mbedtls/base64.h" |
| 47 | #include "mbedtls/bignum.h" |
| 48 | #include "mbedtls/rsa.h" |
| 49 | #include "mbedtls/x509.h" |
| 50 | #include "mbedtls/xtea.h" |
| 51 | #include "mbedtls/pkcs5.h" |
| 52 | #include "mbedtls/ecp.h" |
| 53 | |
| 54 | #include <stdio.h> |
| 55 | #include <string.h> |
| 56 | |
| 57 | #if defined(MBEDTLS_PLATFORM_C) |
| 58 | #include "mbedtls/platform.h" |
| 59 | #else |
| 60 | #include <stdio.h> |
| 61 | #define mbedtls_printf printf |
| 62 | #endif |
| 63 | |
| 64 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 65 | #include "mbedtls/memory_buffer_alloc.h" |
| 66 | #endif |
| 67 | |
| 68 | int selftest( int argc, char *argv[] ) |
| 69 | { |
| 70 | int ret = 0, v; |
| 71 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 72 | unsigned char buf[1000000]; |
| 73 | #endif |
| 74 | |
| 75 | if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 ) |
| 76 | v = 0; |
| 77 | else |
| 78 | { |
| 79 | v = 1; |
| 80 | mbedtls_printf( "\n" ); |
| 81 | } |
| 82 | |
| 83 | #if defined(MBEDTLS_SELF_TEST) |
| 84 | |
| 85 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 86 | mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) ); |
| 87 | #endif |
| 88 | |
| 89 | #if defined(MBEDTLS_MD2_C) |
| 90 | if( ( ret = mbedtls_md2_self_test( v ) ) != 0 ) |
| 91 | return( ret ); |
| 92 | #endif |
| 93 | |
| 94 | #if defined(MBEDTLS_MD4_C) |
| 95 | if( ( ret = mbedtls_md4_self_test( v ) ) != 0 ) |
| 96 | return( ret ); |
| 97 | #endif |
| 98 | |
| 99 | #if defined(MBEDTLS_MD5_C) |
| 100 | if( ( ret = mbedtls_md5_self_test( v ) ) != 0 ) |
| 101 | return( ret ); |
| 102 | #endif |
| 103 | |
| 104 | #if defined(MBEDTLS_RIPEMD160_C) |
| 105 | if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 ) |
| 106 | return( ret ); |
| 107 | #endif |
| 108 | |
| 109 | #if defined(MBEDTLS_SHA1_C) |
| 110 | if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 ) |
| 111 | return( ret ); |
| 112 | #endif |
| 113 | |
| 114 | #if defined(MBEDTLS_SHA256_C) |
| 115 | if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 ) |
| 116 | return( ret ); |
| 117 | #endif |
| 118 | |
| 119 | #if defined(MBEDTLS_SHA512_C) |
| 120 | if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 ) |
| 121 | return( ret ); |
| 122 | #endif |
| 123 | |
| 124 | #if defined(MBEDTLS_ARC4_C) |
| 125 | if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 ) |
| 126 | return( ret ); |
| 127 | #endif |
| 128 | |
| 129 | #if defined(MBEDTLS_DES_C) |
| 130 | if( ( ret = mbedtls_des_self_test( v ) ) != 0 ) |
| 131 | return( ret ); |
| 132 | #endif |
| 133 | |
| 134 | #if defined(MBEDTLS_AES_C) |
| 135 | if( ( ret = mbedtls_aes_self_test( v ) ) != 0 ) |
| 136 | return( ret ); |
| 137 | #endif |
| 138 | |
| 139 | #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C) |
| 140 | if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 ) |
| 141 | return( ret ); |
| 142 | #endif |
| 143 | |
| 144 | #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C) |
| 145 | if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 ) |
| 146 | return( ret ); |
| 147 | #endif |
| 148 | |
| 149 | #if defined(MBEDTLS_BASE64_C) |
| 150 | if( ( ret = mbedtls_base64_self_test( v ) ) != 0 ) |
| 151 | return( ret ); |
| 152 | #endif |
| 153 | |
| 154 | #if defined(MBEDTLS_BIGNUM_C) |
| 155 | if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 ) |
| 156 | return( ret ); |
| 157 | #endif |
| 158 | |
| 159 | #if defined(MBEDTLS_RSA_C) |
| 160 | if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 ) |
| 161 | return( ret ); |
| 162 | #endif |
| 163 | |
| 164 | #if defined(MBEDTLS_X509_USE_C) |
| 165 | if( ( ret = mbedtls_x509_self_test( v ) ) != 0 ) |
| 166 | return( ret ); |
| 167 | #endif |
| 168 | |
| 169 | #if defined(MBEDTLS_XTEA_C) |
| 170 | if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 ) |
| 171 | return( ret ); |
| 172 | #endif |
| 173 | |
| 174 | #if defined(MBEDTLS_CAMELLIA_C) |
| 175 | if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 ) |
| 176 | return( ret ); |
| 177 | #endif |
| 178 | |
| 179 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 180 | if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 ) |
| 181 | return( ret ); |
| 182 | #endif |
| 183 | |
| 184 | #if defined(MBEDTLS_HMAC_DRBG_C) |
| 185 | if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 ) |
| 186 | return( ret ); |
| 187 | #endif |
| 188 | |
| 189 | #if defined(MBEDTLS_ECP_C) |
| 190 | if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 ) |
| 191 | return( ret ); |
| 192 | #endif |
| 193 | |
| 194 | #if defined(MBEDTLS_DHM_C) |
| 195 | if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 ) |
| 196 | return( ret ); |
| 197 | #endif |
| 198 | |
| 199 | #if defined(MBEDTLS_ENTROPY_C) |
| 200 | if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 ) |
| 201 | return( ret ); |
| 202 | #endif |
| 203 | |
| 204 | #if defined(MBEDTLS_PKCS5_C) |
| 205 | if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 ) |
| 206 | return( ret ); |
| 207 | #endif |
| 208 | |
| 209 | #if defined(MBEDTLS_TIMING_C) |
| 210 | if( ( ret = mbedtls_timing_self_test( v ) ) != 0 ) |
| 211 | return( ret ); |
| 212 | #endif |
| 213 | |
| 214 | #else |
| 215 | mbedtls_printf( " POLARSSL_SELF_TEST not defined.\n" ); |
| 216 | #endif |
| 217 | |
| 218 | if( v != 0 ) |
| 219 | { |
| 220 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG) |
| 221 | mbedtls_memory_buffer_alloc_status(); |
| 222 | #endif |
| 223 | } |
| 224 | |
| 225 | #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) |
| 226 | mbedtls_memory_buffer_alloc_free(); |
| 227 | |
| 228 | if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 ) |
| 229 | return( ret ); |
| 230 | #endif |
| 231 | |
| 232 | if( v != 0 ) |
| 233 | { |
| 234 | mbedtls_printf( " [ All tests passed ]\n\n" ); |
| 235 | #if defined(_WIN32) |
| 236 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
| 237 | fflush( stdout ); getchar(); |
| 238 | #endif |
| 239 | } |
| 240 | |
| 241 | return( ret ); |
| 242 | } |
| 243 | |
| 244 | #if defined(TARGET_LIKE_MBED) |
| 245 | |
| 246 | #include "mbed/test_env.h" |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame^] | 247 | #include "minar/minar.h" |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 248 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame^] | 249 | static void run() { |
Manuel Pégourié-Gonnard | bd5bbec | 2015-08-06 18:10:17 +0200 | [diff] [blame] | 250 | /* Use 115200 bps for consistency with other examples */ |
| 251 | Serial pc(USBTX, USBRX); |
| 252 | pc.baud(115200); |
| 253 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 254 | MBED_HOSTTEST_TIMEOUT(40); |
| 255 | MBED_HOSTTEST_SELECT(default); |
| 256 | MBED_HOSTTEST_DESCRIPTION(mbed TLS selftest program); |
| 257 | MBED_HOSTTEST_START("MBEDTLS_SELFTEST"); |
| 258 | MBED_HOSTTEST_RESULT(selftest(0, NULL) == 0); |
| 259 | } |
| 260 | |
Manuel Pégourié-Gonnard | e87b04c | 2015-08-11 04:09:44 +0200 | [diff] [blame^] | 261 | void app_start(int, char*[]) { |
| 262 | minar::Scheduler::postCallback(FunctionPointer0<void>(run).bind()); |
| 263 | } |
| 264 | |
Manuel Pégourié-Gonnard | 63e7eba | 2015-07-28 14:17:48 +0200 | [diff] [blame] | 265 | #else |
| 266 | |
| 267 | int main() { |
| 268 | return selftest(0, NULL); |
| 269 | } |
| 270 | |
| 271 | #endif |