Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Self-test demonstration program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 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. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
| 32 | #define polarssl_printf printf |
| 33 | #define polarssl_fprintf fprintf |
| 34 | #define polarssl_malloc malloc |
| 35 | #define polarssl_free free |
| 36 | #endif |
| 37 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | #include <string.h> |
| 39 | #include <stdio.h> |
| 40 | |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 41 | #include "polarssl/entropy.h" |
Manuel Pégourié-Gonnard | 79afaa0 | 2014-01-31 11:12:09 +0100 | [diff] [blame] | 42 | #include "polarssl/hmac_drbg.h" |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 43 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 44 | #include "polarssl/dhm.h" |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 45 | #include "polarssl/gcm.h" |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 46 | #include "polarssl/ccm.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 47 | #include "polarssl/md2.h" |
| 48 | #include "polarssl/md4.h" |
| 49 | #include "polarssl/md5.h" |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 50 | #include "polarssl/ripemd160.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 51 | #include "polarssl/sha1.h" |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 52 | #include "polarssl/sha256.h" |
| 53 | #include "polarssl/sha512.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 54 | #include "polarssl/arc4.h" |
| 55 | #include "polarssl/des.h" |
| 56 | #include "polarssl/aes.h" |
Paul Bakker | 026c03b | 2009-03-28 17:53:03 +0000 | [diff] [blame] | 57 | #include "polarssl/camellia.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 58 | #include "polarssl/base64.h" |
| 59 | #include "polarssl/bignum.h" |
| 60 | #include "polarssl/rsa.h" |
| 61 | #include "polarssl/x509.h" |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 62 | #include "polarssl/xtea.h" |
Manuel Pégourié-Gonnard | 486485b | 2014-03-20 09:59:51 +0100 | [diff] [blame] | 63 | #include "polarssl/pkcs5.h" |
Manuel Pégourié-Gonnard | 388dac4 | 2014-03-27 18:57:52 +0100 | [diff] [blame] | 64 | #include "polarssl/pbkdf2.h" |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 65 | #include "polarssl/ecp.h" |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 66 | #include "polarssl/timing.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 68 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
Manuel Pégourié-Gonnard | d43ccb6 | 2015-01-23 17:38:09 +0000 | [diff] [blame] | 69 | #include "polarssl/memory_buffer_alloc.h" |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 70 | #endif |
| 71 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | int main( int argc, char *argv[] ) |
| 73 | { |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 74 | int ret = 0, v; |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 75 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 76 | unsigned char buf[1000000]; |
| 77 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | |
| 79 | if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 ) |
| 80 | v = 0; |
| 81 | else |
| 82 | { |
| 83 | v = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 84 | polarssl_printf( "\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 87 | #if defined(POLARSSL_SELF_TEST) |
| 88 | |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 89 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 90 | memory_buffer_alloc_init( buf, sizeof(buf) ); |
| 91 | #endif |
| 92 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 93 | #if defined(POLARSSL_MD2_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | if( ( ret = md2_self_test( v ) ) != 0 ) |
| 95 | return( ret ); |
| 96 | #endif |
| 97 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 98 | #if defined(POLARSSL_MD4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | if( ( ret = md4_self_test( v ) ) != 0 ) |
| 100 | return( ret ); |
| 101 | #endif |
| 102 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 103 | #if defined(POLARSSL_MD5_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 104 | if( ( ret = md5_self_test( v ) ) != 0 ) |
| 105 | return( ret ); |
| 106 | #endif |
| 107 | |
Paul Bakker | 61b699e | 2014-01-22 13:35:29 +0100 | [diff] [blame] | 108 | #if defined(POLARSSL_RIPEMD160_C) |
| 109 | if( ( ret = ripemd160_self_test( v ) ) != 0 ) |
Manuel Pégourié-Gonnard | 1744d72 | 2014-01-17 14:44:38 +0100 | [diff] [blame] | 110 | return( ret ); |
| 111 | #endif |
| 112 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 113 | #if defined(POLARSSL_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | if( ( ret = sha1_self_test( v ) ) != 0 ) |
| 115 | return( ret ); |
| 116 | #endif |
| 117 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 118 | #if defined(POLARSSL_SHA256_C) |
| 119 | if( ( ret = sha256_self_test( v ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | return( ret ); |
| 121 | #endif |
| 122 | |
Paul Bakker | 5a1d687 | 2014-03-26 11:20:05 +0100 | [diff] [blame] | 123 | #if defined(POLARSSL_SHA512_C) |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 124 | if( ( ret = sha512_self_test( v ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | return( ret ); |
| 126 | #endif |
| 127 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 128 | #if defined(POLARSSL_ARC4_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | if( ( ret = arc4_self_test( v ) ) != 0 ) |
| 130 | return( ret ); |
| 131 | #endif |
| 132 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 133 | #if defined(POLARSSL_DES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | if( ( ret = des_self_test( v ) ) != 0 ) |
| 135 | return( ret ); |
| 136 | #endif |
| 137 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 138 | #if defined(POLARSSL_AES_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | if( ( ret = aes_self_test( v ) ) != 0 ) |
| 140 | return( ret ); |
| 141 | #endif |
| 142 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 143 | #if defined(POLARSSL_GCM_C) && defined(POLARSSL_AES_C) |
Paul Bakker | 89e80c9 | 2012-03-20 13:50:09 +0000 | [diff] [blame] | 144 | if( ( ret = gcm_self_test( v ) ) != 0 ) |
| 145 | return( ret ); |
| 146 | #endif |
| 147 | |
Manuel Pégourié-Gonnard | a6916fa | 2014-05-02 15:17:29 +0200 | [diff] [blame] | 148 | #if defined(POLARSSL_CCM_C) && defined(POLARSSL_AES_C) |
| 149 | if( ( ret = ccm_self_test( v ) ) != 0 ) |
| 150 | return( ret ); |
| 151 | #endif |
| 152 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 153 | #if defined(POLARSSL_BASE64_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | if( ( ret = base64_self_test( v ) ) != 0 ) |
| 155 | return( ret ); |
| 156 | #endif |
| 157 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 158 | #if defined(POLARSSL_BIGNUM_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | if( ( ret = mpi_self_test( v ) ) != 0 ) |
| 160 | return( ret ); |
| 161 | #endif |
| 162 | |
Manuel Pégourié-Gonnard | 79afaa0 | 2014-01-31 11:12:09 +0100 | [diff] [blame] | 163 | #if defined(POLARSSL_RSA_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | if( ( ret = rsa_self_test( v ) ) != 0 ) |
| 165 | return( ret ); |
| 166 | #endif |
| 167 | |
Paul Bakker | 7504d7f | 2013-09-16 22:56:18 +0200 | [diff] [blame] | 168 | #if defined(POLARSSL_X509_USE_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | if( ( ret = x509_self_test( v ) ) != 0 ) |
| 170 | return( ret ); |
| 171 | #endif |
| 172 | |
Paul Bakker | 7a7c78f | 2009-01-04 18:15:48 +0000 | [diff] [blame] | 173 | #if defined(POLARSSL_XTEA_C) |
| 174 | if( ( ret = xtea_self_test( v ) ) != 0 ) |
| 175 | return( ret ); |
| 176 | #endif |
| 177 | |
Paul Bakker | 38119b1 | 2009-01-10 23:31:23 +0000 | [diff] [blame] | 178 | #if defined(POLARSSL_CAMELLIA_C) |
| 179 | if( ( ret = camellia_self_test( v ) ) != 0 ) |
| 180 | return( ret ); |
| 181 | #endif |
| 182 | |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 183 | #if defined(POLARSSL_CTR_DRBG_C) |
| 184 | if( ( ret = ctr_drbg_self_test( v ) ) != 0 ) |
| 185 | return( ret ); |
| 186 | #endif |
| 187 | |
Manuel Pégourié-Gonnard | 79afaa0 | 2014-01-31 11:12:09 +0100 | [diff] [blame] | 188 | #if defined(POLARSSL_HMAC_DRBG_C) |
| 189 | if( ( ret = hmac_drbg_self_test( v ) ) != 0 ) |
| 190 | return( ret ); |
| 191 | #endif |
| 192 | |
Manuel Pégourié-Gonnard | b4a310b | 2012-11-13 20:57:00 +0100 | [diff] [blame] | 193 | #if defined(POLARSSL_ECP_C) |
| 194 | if( ( ret = ecp_self_test( v ) ) != 0 ) |
| 195 | return( ret ); |
| 196 | #endif |
| 197 | |
Paul Bakker | 40ce79f | 2013-09-15 17:43:54 +0200 | [diff] [blame] | 198 | #if defined(POLARSSL_DHM_C) |
| 199 | if( ( ret = dhm_self_test( v ) ) != 0 ) |
| 200 | return( ret ); |
| 201 | #endif |
| 202 | |
Manuel Pégourié-Gonnard | 4dd7392 | 2014-05-30 10:34:15 +0200 | [diff] [blame] | 203 | #if defined(POLARSSL_ENTROPY_C) |
| 204 | if( ( ret = entropy_self_test( v ) ) != 0 ) |
| 205 | return( ret ); |
| 206 | #endif |
| 207 | |
Manuel Pégourié-Gonnard | 13a1ef8 | 2014-03-27 20:12:44 +0100 | [diff] [blame] | 208 | /* Slow tests last */ |
| 209 | |
| 210 | #if defined(POLARSSL_PBKDF2_C) |
| 211 | if( ( ret = pbkdf2_self_test( v ) ) != 0 ) |
| 212 | return( ret ); |
| 213 | #else |
| 214 | #if defined(POLARSSL_PKCS5_C) |
| 215 | if( ( ret = pkcs5_self_test( v ) ) != 0 ) |
| 216 | return( ret ); |
| 217 | #endif |
| 218 | #endif |
| 219 | |
Manuel Pégourié-Gonnard | 7680698 | 2014-06-13 18:04:42 +0200 | [diff] [blame] | 220 | /* Not stable enough on Windows and FreeBSD yet */ |
| 221 | #if __linux__ && defined(POLARSSL_TIMING_C) |
Manuel Pégourié-Gonnard | 470fc93 | 2014-03-27 20:07:08 +0100 | [diff] [blame] | 222 | if( ( ret = timing_self_test( v ) ) != 0 ) |
| 223 | return( ret ); |
| 224 | #endif |
| 225 | |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 226 | #else |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 227 | polarssl_printf( " POLARSSL_SELF_TEST not defined.\n" ); |
Paul Bakker | 135b98e | 2011-05-25 11:13:47 +0000 | [diff] [blame] | 228 | #endif |
| 229 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 230 | if( v != 0 ) |
| 231 | { |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 232 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG) |
| 233 | memory_buffer_alloc_status(); |
| 234 | #endif |
Manuel Pégourié-Gonnard | 5ba1d52 | 2014-11-27 11:33:55 +0100 | [diff] [blame] | 235 | } |
Paul Bakker | 6e339b5 | 2013-07-03 13:37:05 +0200 | [diff] [blame] | 236 | |
Manuel Pégourié-Gonnard | 5ba1d52 | 2014-11-27 11:33:55 +0100 | [diff] [blame] | 237 | #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) |
| 238 | memory_buffer_alloc_free(); |
| 239 | |
| 240 | if( ( ret = memory_buffer_alloc_self_test( v ) ) != 0 ) |
| 241 | return( ret ); |
| 242 | #endif |
| 243 | |
| 244 | if( v != 0 ) |
| 245 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 246 | polarssl_printf( " [ All tests passed ]\n\n" ); |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 247 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame^] | 248 | polarssl_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 249 | fflush( stdout ); getchar(); |
| 250 | #endif |
| 251 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 | |
| 253 | return( ret ); |
| 254 | } |