blob: edf3d52d4b00296666035362857c78c70d663670 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Self-test demonstration program
3 *
Paul Bakkerd2681d82013-06-30 14:49:12 +02004 * Copyright (C) 2006-2013, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000031
32#include <string.h>
33#include <stdio.h>
34
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +020035#include "polarssl/entropy.h"
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +010036#include "polarssl/hmac_drbg.h"
Paul Bakker0e04d0e2011-11-27 14:46:59 +000037#include "polarssl/ctr_drbg.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020038#include "polarssl/dhm.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000039#include "polarssl/gcm.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020040#include "polarssl/ccm.h"
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/md2.h"
42#include "polarssl/md4.h"
43#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010044#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000045#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020046#include "polarssl/sha256.h"
47#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000048#include "polarssl/arc4.h"
49#include "polarssl/des.h"
50#include "polarssl/aes.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000051#include "polarssl/camellia.h"
Paul Bakker40e46942009-01-03 21:51:57 +000052#include "polarssl/base64.h"
53#include "polarssl/bignum.h"
54#include "polarssl/rsa.h"
55#include "polarssl/x509.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000056#include "polarssl/xtea.h"
Manuel Pégourié-Gonnard486485b2014-03-20 09:59:51 +010057#include "polarssl/pkcs5.h"
Manuel Pégourié-Gonnard388dac42014-03-27 18:57:52 +010058#include "polarssl/pbkdf2.h"
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010059#include "polarssl/ecp.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010060#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Paul Bakker6e339b52013-07-03 13:37:05 +020062#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
63#include "polarssl/memory.h"
64#endif
65
Paul Bakker5121ce52009-01-03 21:22:43 +000066int main( int argc, char *argv[] )
67{
Paul Bakker135b98e2011-05-25 11:13:47 +000068 int ret = 0, v;
Paul Bakker6e339b52013-07-03 13:37:05 +020069#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
70 unsigned char buf[1000000];
71#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000072
73 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
74 v = 0;
75 else
76 {
77 v = 1;
78 printf( "\n" );
79 }
80
Paul Bakker135b98e2011-05-25 11:13:47 +000081#if defined(POLARSSL_SELF_TEST)
82
Paul Bakker6e339b52013-07-03 13:37:05 +020083#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
84 memory_buffer_alloc_init( buf, sizeof(buf) );
85#endif
86
Paul Bakker40e46942009-01-03 21:51:57 +000087#if defined(POLARSSL_MD2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000088 if( ( ret = md2_self_test( v ) ) != 0 )
89 return( ret );
90#endif
91
Paul Bakker40e46942009-01-03 21:51:57 +000092#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000093 if( ( ret = md4_self_test( v ) ) != 0 )
94 return( ret );
95#endif
96
Paul Bakker40e46942009-01-03 21:51:57 +000097#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000098 if( ( ret = md5_self_test( v ) ) != 0 )
99 return( ret );
100#endif
101
Paul Bakker61b699e2014-01-22 13:35:29 +0100102#if defined(POLARSSL_RIPEMD160_C)
103 if( ( ret = ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100104 return( ret );
105#endif
106
Paul Bakker40e46942009-01-03 21:51:57 +0000107#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 if( ( ret = sha1_self_test( v ) ) != 0 )
109 return( ret );
110#endif
111
Paul Bakker9e36f042013-06-30 14:34:05 +0200112#if defined(POLARSSL_SHA256_C)
113 if( ( ret = sha256_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 return( ret );
115#endif
116
Paul Bakker5a1d6872014-03-26 11:20:05 +0100117#if defined(POLARSSL_SHA512_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200118 if( ( ret = sha512_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 return( ret );
120#endif
121
Paul Bakker40e46942009-01-03 21:51:57 +0000122#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 if( ( ret = arc4_self_test( v ) ) != 0 )
124 return( ret );
125#endif
126
Paul Bakker40e46942009-01-03 21:51:57 +0000127#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 if( ( ret = des_self_test( v ) ) != 0 )
129 return( ret );
130#endif
131
Paul Bakker40e46942009-01-03 21:51:57 +0000132#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 if( ( ret = aes_self_test( v ) ) != 0 )
134 return( ret );
135#endif
136
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200137#if defined(POLARSSL_GCM_C) && defined(POLARSSL_AES_C)
Paul Bakker89e80c92012-03-20 13:50:09 +0000138 if( ( ret = gcm_self_test( v ) ) != 0 )
139 return( ret );
140#endif
141
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200142#if defined(POLARSSL_CCM_C) && defined(POLARSSL_AES_C)
143 if( ( ret = ccm_self_test( v ) ) != 0 )
144 return( ret );
145#endif
146
Paul Bakker40e46942009-01-03 21:51:57 +0000147#if defined(POLARSSL_BASE64_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 if( ( ret = base64_self_test( v ) ) != 0 )
149 return( ret );
150#endif
151
Paul Bakker40e46942009-01-03 21:51:57 +0000152#if defined(POLARSSL_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 if( ( ret = mpi_self_test( v ) ) != 0 )
154 return( ret );
155#endif
156
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100157#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 if( ( ret = rsa_self_test( v ) ) != 0 )
159 return( ret );
160#endif
161
Paul Bakker7504d7f2013-09-16 22:56:18 +0200162#if defined(POLARSSL_X509_USE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 if( ( ret = x509_self_test( v ) ) != 0 )
164 return( ret );
165#endif
166
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000167#if defined(POLARSSL_XTEA_C)
168 if( ( ret = xtea_self_test( v ) ) != 0 )
169 return( ret );
170#endif
171
Paul Bakker38119b12009-01-10 23:31:23 +0000172#if defined(POLARSSL_CAMELLIA_C)
173 if( ( ret = camellia_self_test( v ) ) != 0 )
174 return( ret );
175#endif
176
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000177#if defined(POLARSSL_CTR_DRBG_C)
178 if( ( ret = ctr_drbg_self_test( v ) ) != 0 )
179 return( ret );
180#endif
181
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100182#if defined(POLARSSL_HMAC_DRBG_C)
183 if( ( ret = hmac_drbg_self_test( v ) ) != 0 )
184 return( ret );
185#endif
186
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100187#if defined(POLARSSL_ECP_C)
188 if( ( ret = ecp_self_test( v ) ) != 0 )
189 return( ret );
190#endif
191
Paul Bakker40ce79f2013-09-15 17:43:54 +0200192#if defined(POLARSSL_DHM_C)
193 if( ( ret = dhm_self_test( v ) ) != 0 )
194 return( ret );
195#endif
196
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200197#if defined(POLARSSL_ENTROPY_C)
198 if( ( ret = entropy_self_test( v ) ) != 0 )
199 return( ret );
200#endif
201
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100202/* Slow tests last */
203
204#if defined(POLARSSL_PBKDF2_C)
205 if( ( ret = pbkdf2_self_test( v ) ) != 0 )
206 return( ret );
207#else
208#if defined(POLARSSL_PKCS5_C)
209 if( ( ret = pkcs5_self_test( v ) ) != 0 )
210 return( ret );
211#endif
212#endif
213
Manuel Pégourié-Gonnard76806982014-06-13 18:04:42 +0200214/* Not stable enough on Windows and FreeBSD yet */
215#if __linux__ && defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100216 if( ( ret = timing_self_test( v ) ) != 0 )
217 return( ret );
218#endif
219
Paul Bakker135b98e2011-05-25 11:13:47 +0000220#else
221 printf( " POLARSSL_SELF_TEST not defined.\n" );
222#endif
223
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 if( v != 0 )
225 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200226#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
227 memory_buffer_alloc_status();
228#endif
229
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 printf( " [ All tests passed ]\n\n" );
Paul Bakkercce9d772011-11-18 14:26:47 +0000231#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 printf( " Press Enter to exit this program.\n" );
233 fflush( stdout ); getchar();
234#endif
235 }
Paul Bakker1337aff2013-09-29 14:45:34 +0200236#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
237 memory_buffer_alloc_free();
238#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240 return( ret );
241}