blob: 07e2b122ab6da59449737e54638b85394975ce6e [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é-Gonnard79afaa02014-01-31 11:12:09 +010035#include "polarssl/hmac_drbg.h"
Paul Bakker0e04d0e2011-11-27 14:46:59 +000036#include "polarssl/ctr_drbg.h"
Paul Bakker40ce79f2013-09-15 17:43:54 +020037#include "polarssl/dhm.h"
Paul Bakker89e80c92012-03-20 13:50:09 +000038#include "polarssl/gcm.h"
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +020039#include "polarssl/ccm.h"
Paul Bakker40e46942009-01-03 21:51:57 +000040#include "polarssl/md2.h"
41#include "polarssl/md4.h"
42#include "polarssl/md5.h"
Paul Bakker61b699e2014-01-22 13:35:29 +010043#include "polarssl/ripemd160.h"
Paul Bakker40e46942009-01-03 21:51:57 +000044#include "polarssl/sha1.h"
Paul Bakkerd2681d82013-06-30 14:49:12 +020045#include "polarssl/sha256.h"
46#include "polarssl/sha512.h"
Paul Bakker40e46942009-01-03 21:51:57 +000047#include "polarssl/arc4.h"
48#include "polarssl/des.h"
49#include "polarssl/aes.h"
Paul Bakker026c03b2009-03-28 17:53:03 +000050#include "polarssl/camellia.h"
Paul Bakker40e46942009-01-03 21:51:57 +000051#include "polarssl/base64.h"
52#include "polarssl/bignum.h"
53#include "polarssl/rsa.h"
54#include "polarssl/x509.h"
Paul Bakker7a7c78f2009-01-04 18:15:48 +000055#include "polarssl/xtea.h"
Manuel Pégourié-Gonnard486485b2014-03-20 09:59:51 +010056#include "polarssl/pkcs5.h"
Manuel Pégourié-Gonnard388dac42014-03-27 18:57:52 +010057#include "polarssl/pbkdf2.h"
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +010058#include "polarssl/ecp.h"
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +010059#include "polarssl/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker6e339b52013-07-03 13:37:05 +020061#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
62#include "polarssl/memory.h"
63#endif
64
Paul Bakker5121ce52009-01-03 21:22:43 +000065int main( int argc, char *argv[] )
66{
Paul Bakker135b98e2011-05-25 11:13:47 +000067 int ret = 0, v;
Paul Bakker6e339b52013-07-03 13:37:05 +020068#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
69 unsigned char buf[1000000];
70#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
73 v = 0;
74 else
75 {
76 v = 1;
77 printf( "\n" );
78 }
79
Paul Bakker135b98e2011-05-25 11:13:47 +000080#if defined(POLARSSL_SELF_TEST)
81
Paul Bakker6e339b52013-07-03 13:37:05 +020082#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
83 memory_buffer_alloc_init( buf, sizeof(buf) );
84#endif
85
Paul Bakker40e46942009-01-03 21:51:57 +000086#if defined(POLARSSL_MD2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000087 if( ( ret = md2_self_test( v ) ) != 0 )
88 return( ret );
89#endif
90
Paul Bakker40e46942009-01-03 21:51:57 +000091#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000092 if( ( ret = md4_self_test( v ) ) != 0 )
93 return( ret );
94#endif
95
Paul Bakker40e46942009-01-03 21:51:57 +000096#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000097 if( ( ret = md5_self_test( v ) ) != 0 )
98 return( ret );
99#endif
100
Paul Bakker61b699e2014-01-22 13:35:29 +0100101#if defined(POLARSSL_RIPEMD160_C)
102 if( ( ret = ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100103 return( ret );
104#endif
105
Paul Bakker40e46942009-01-03 21:51:57 +0000106#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 if( ( ret = sha1_self_test( v ) ) != 0 )
108 return( ret );
109#endif
110
Paul Bakker9e36f042013-06-30 14:34:05 +0200111#if defined(POLARSSL_SHA256_C)
112 if( ( ret = sha256_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 return( ret );
114#endif
115
Paul Bakker5a1d6872014-03-26 11:20:05 +0100116#if defined(POLARSSL_SHA512_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200117 if( ( ret = sha512_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 return( ret );
119#endif
120
Paul Bakker40e46942009-01-03 21:51:57 +0000121#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 if( ( ret = arc4_self_test( v ) ) != 0 )
123 return( ret );
124#endif
125
Paul Bakker40e46942009-01-03 21:51:57 +0000126#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 if( ( ret = des_self_test( v ) ) != 0 )
128 return( ret );
129#endif
130
Paul Bakker40e46942009-01-03 21:51:57 +0000131#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 if( ( ret = aes_self_test( v ) ) != 0 )
133 return( ret );
134#endif
135
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200136#if defined(POLARSSL_GCM_C) && defined(POLARSSL_AES_C)
Paul Bakker89e80c92012-03-20 13:50:09 +0000137 if( ( ret = gcm_self_test( v ) ) != 0 )
138 return( ret );
139#endif
140
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200141#if defined(POLARSSL_CCM_C) && defined(POLARSSL_AES_C)
142 if( ( ret = ccm_self_test( v ) ) != 0 )
143 return( ret );
144#endif
145
Paul Bakker40e46942009-01-03 21:51:57 +0000146#if defined(POLARSSL_BASE64_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 if( ( ret = base64_self_test( v ) ) != 0 )
148 return( ret );
149#endif
150
Paul Bakker40e46942009-01-03 21:51:57 +0000151#if defined(POLARSSL_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 if( ( ret = mpi_self_test( v ) ) != 0 )
153 return( ret );
154#endif
155
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100156#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 if( ( ret = rsa_self_test( v ) ) != 0 )
158 return( ret );
159#endif
160
Paul Bakker7504d7f2013-09-16 22:56:18 +0200161#if defined(POLARSSL_X509_USE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 if( ( ret = x509_self_test( v ) ) != 0 )
163 return( ret );
164#endif
165
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000166#if defined(POLARSSL_XTEA_C)
167 if( ( ret = xtea_self_test( v ) ) != 0 )
168 return( ret );
169#endif
170
Paul Bakker38119b12009-01-10 23:31:23 +0000171#if defined(POLARSSL_CAMELLIA_C)
172 if( ( ret = camellia_self_test( v ) ) != 0 )
173 return( ret );
174#endif
175
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000176#if defined(POLARSSL_CTR_DRBG_C)
177 if( ( ret = ctr_drbg_self_test( v ) ) != 0 )
178 return( ret );
179#endif
180
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100181#if defined(POLARSSL_HMAC_DRBG_C)
182 if( ( ret = hmac_drbg_self_test( v ) ) != 0 )
183 return( ret );
184#endif
185
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100186#if defined(POLARSSL_ECP_C)
187 if( ( ret = ecp_self_test( v ) ) != 0 )
188 return( ret );
189#endif
190
Paul Bakker40ce79f2013-09-15 17:43:54 +0200191#if defined(POLARSSL_DHM_C)
192 if( ( ret = dhm_self_test( v ) ) != 0 )
193 return( ret );
194#endif
195
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100196/* Slow tests last */
197
198#if defined(POLARSSL_PBKDF2_C)
199 if( ( ret = pbkdf2_self_test( v ) ) != 0 )
200 return( ret );
201#else
202#if defined(POLARSSL_PKCS5_C)
203 if( ( ret = pkcs5_self_test( v ) ) != 0 )
204 return( ret );
205#endif
206#endif
207
Manuel Pégourié-Gonnard0f79bab2014-04-09 09:56:16 +0200208/*
209 * Not reliable enough yet
210 */
211#if 0 && defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100212 if( ( ret = timing_self_test( v ) ) != 0 )
213 return( ret );
214#endif
215
Paul Bakker135b98e2011-05-25 11:13:47 +0000216#else
217 printf( " POLARSSL_SELF_TEST not defined.\n" );
218#endif
219
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 if( v != 0 )
221 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200222#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
223 memory_buffer_alloc_status();
224#endif
225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 printf( " [ All tests passed ]\n\n" );
Paul Bakkercce9d772011-11-18 14:26:47 +0000227#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 printf( " Press Enter to exit this program.\n" );
229 fflush( stdout ); getchar();
230#endif
231 }
Paul Bakker1337aff2013-09-29 14:45:34 +0200232#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
233 memory_buffer_alloc_free();
234#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236 return( ret );
237}