blob: 15a63d0043d5c1c2f7607854c15ef488acd1e63a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Self-test demonstration program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * 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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
29#include <string.h>
30#include <stdio.h>
31
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +020032#include "polarssl/entropy.h"
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +010033#include "polarssl/hmac_drbg.h"
Paul Bakker0e04d0e2011-11-27 14:46:59 +000034#include "polarssl/ctr_drbg.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020035#include "polarssl/dhm.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000036#include "polarssl/gcm.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020037#include "polarssl/ccm.h"
Paul Bakker40e46942009-01-03 21:51:57 +000038#include "polarssl/md2.h"
39#include "polarssl/md4.h"
40#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010041#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000042#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020043#include "polarssl/sha256.h"
44#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/arc4.h"
46#include "polarssl/des.h"
47#include "polarssl/aes.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000048#include "polarssl/camellia.h"
Paul Bakker40e46942009-01-03 21:51:57 +000049#include "polarssl/base64.h"
50#include "polarssl/bignum.h"
51#include "polarssl/rsa.h"
52#include "polarssl/x509.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000053#include "polarssl/xtea.h"
Manuel Pégourié-Gonnard486485b2014-03-20 09:59:51 +010054#include "polarssl/pkcs5.h"
Manuel Pégourié-Gonnard388dac42014-03-27 18:57:52 +010055#include "polarssl/pbkdf2.h"
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010056#include "polarssl/ecp.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010057#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker6e339b52013-07-03 13:37:05 +020059#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnardd43ccb62015-01-23 17:38:09 +000060#include "polarssl/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020061#endif
62
Paul Bakker5121ce52009-01-03 21:22:43 +000063int main( int argc, char *argv[] )
64{
Paul Bakker135b98e2011-05-25 11:13:47 +000065 int ret = 0, v;
Paul Bakker6e339b52013-07-03 13:37:05 +020066#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
67 unsigned char buf[1000000];
68#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
71 v = 0;
72 else
73 {
74 v = 1;
75 printf( "\n" );
76 }
77
Paul Bakker135b98e2011-05-25 11:13:47 +000078#if defined(POLARSSL_SELF_TEST)
79
Paul Bakker6e339b52013-07-03 13:37:05 +020080#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
81 memory_buffer_alloc_init( buf, sizeof(buf) );
82#endif
83
Paul Bakker40e46942009-01-03 21:51:57 +000084#if defined(POLARSSL_MD2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000085 if( ( ret = md2_self_test( v ) ) != 0 )
86 return( ret );
87#endif
88
Paul Bakker40e46942009-01-03 21:51:57 +000089#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000090 if( ( ret = md4_self_test( v ) ) != 0 )
91 return( ret );
92#endif
93
Paul Bakker40e46942009-01-03 21:51:57 +000094#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000095 if( ( ret = md5_self_test( v ) ) != 0 )
96 return( ret );
97#endif
98
Paul Bakker61b699e2014-01-22 13:35:29 +010099#if defined(POLARSSL_RIPEMD160_C)
100 if( ( ret = ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100101 return( ret );
102#endif
103
Paul Bakker40e46942009-01-03 21:51:57 +0000104#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 if( ( ret = sha1_self_test( v ) ) != 0 )
106 return( ret );
107#endif
108
Paul Bakker9e36f042013-06-30 14:34:05 +0200109#if defined(POLARSSL_SHA256_C)
110 if( ( ret = sha256_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 return( ret );
112#endif
113
Paul Bakker5a1d6872014-03-26 11:20:05 +0100114#if defined(POLARSSL_SHA512_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200115 if( ( ret = sha512_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 return( ret );
117#endif
118
Paul Bakker40e46942009-01-03 21:51:57 +0000119#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 if( ( ret = arc4_self_test( v ) ) != 0 )
121 return( ret );
122#endif
123
Paul Bakker40e46942009-01-03 21:51:57 +0000124#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 if( ( ret = des_self_test( v ) ) != 0 )
126 return( ret );
127#endif
128
Paul Bakker40e46942009-01-03 21:51:57 +0000129#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 if( ( ret = aes_self_test( v ) ) != 0 )
131 return( ret );
132#endif
133
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200134#if defined(POLARSSL_GCM_C) && defined(POLARSSL_AES_C)
Paul Bakker89e80c92012-03-20 13:50:09 +0000135 if( ( ret = gcm_self_test( v ) ) != 0 )
136 return( ret );
137#endif
138
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200139#if defined(POLARSSL_CCM_C) && defined(POLARSSL_AES_C)
140 if( ( ret = ccm_self_test( v ) ) != 0 )
141 return( ret );
142#endif
143
Paul Bakker40e46942009-01-03 21:51:57 +0000144#if defined(POLARSSL_BASE64_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 if( ( ret = base64_self_test( v ) ) != 0 )
146 return( ret );
147#endif
148
Paul Bakker40e46942009-01-03 21:51:57 +0000149#if defined(POLARSSL_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 if( ( ret = mpi_self_test( v ) ) != 0 )
151 return( ret );
152#endif
153
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100154#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 if( ( ret = rsa_self_test( v ) ) != 0 )
156 return( ret );
157#endif
158
Paul Bakker7504d7f2013-09-16 22:56:18 +0200159#if defined(POLARSSL_X509_USE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 if( ( ret = x509_self_test( v ) ) != 0 )
161 return( ret );
162#endif
163
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000164#if defined(POLARSSL_XTEA_C)
165 if( ( ret = xtea_self_test( v ) ) != 0 )
166 return( ret );
167#endif
168
Paul Bakker38119b12009-01-10 23:31:23 +0000169#if defined(POLARSSL_CAMELLIA_C)
170 if( ( ret = camellia_self_test( v ) ) != 0 )
171 return( ret );
172#endif
173
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000174#if defined(POLARSSL_CTR_DRBG_C)
175 if( ( ret = ctr_drbg_self_test( v ) ) != 0 )
176 return( ret );
177#endif
178
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100179#if defined(POLARSSL_HMAC_DRBG_C)
180 if( ( ret = hmac_drbg_self_test( v ) ) != 0 )
181 return( ret );
182#endif
183
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100184#if defined(POLARSSL_ECP_C)
185 if( ( ret = ecp_self_test( v ) ) != 0 )
186 return( ret );
187#endif
188
Paul Bakker40ce79f2013-09-15 17:43:54 +0200189#if defined(POLARSSL_DHM_C)
190 if( ( ret = dhm_self_test( v ) ) != 0 )
191 return( ret );
192#endif
193
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200194#if defined(POLARSSL_ENTROPY_C)
195 if( ( ret = entropy_self_test( v ) ) != 0 )
196 return( ret );
197#endif
198
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100199/* Slow tests last */
200
201#if defined(POLARSSL_PBKDF2_C)
202 if( ( ret = pbkdf2_self_test( v ) ) != 0 )
203 return( ret );
204#else
205#if defined(POLARSSL_PKCS5_C)
206 if( ( ret = pkcs5_self_test( v ) ) != 0 )
207 return( ret );
208#endif
209#endif
210
Manuel Pégourié-Gonnard76806982014-06-13 18:04:42 +0200211/* Not stable enough on Windows and FreeBSD yet */
212#if __linux__ && defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100213 if( ( ret = timing_self_test( v ) ) != 0 )
214 return( ret );
215#endif
216
Paul Bakker135b98e2011-05-25 11:13:47 +0000217#else
218 printf( " POLARSSL_SELF_TEST not defined.\n" );
219#endif
220
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 if( v != 0 )
222 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200223#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
224 memory_buffer_alloc_status();
225#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100226 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200227
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100228#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
229 memory_buffer_alloc_free();
230
231 if( ( ret = memory_buffer_alloc_self_test( v ) ) != 0 )
232 return( ret );
233#endif
234
235 if( v != 0 )
236 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 printf( " [ All tests passed ]\n\n" );
Paul Bakkercce9d772011-11-18 14:26:47 +0000238#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 printf( " Press Enter to exit this program.\n" );
240 fflush( stdout ); getchar();
241#endif
242 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244 return( ret );
245}