blob: d4ce1f7836275875fd884975153a495fe37302ce [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é-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#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"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/ecp.h"
53#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Rich Evans18b78c72015-02-11 14:06:19 +000055#include <stdio.h>
56#include <string.h>
57
58#if defined(POLARSSL_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000060#else
61#include <stdio.h>
62#define polarssl_printf printf
63#endif
64
Paul Bakker6e339b52013-07-03 13:37:05 +020065#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020067#endif
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069int main( int argc, char *argv[] )
70{
Paul Bakker135b98e2011-05-25 11:13:47 +000071 int ret = 0, v;
Paul Bakker6e339b52013-07-03 13:37:05 +020072#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
73 unsigned char buf[1000000];
74#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
77 v = 0;
78 else
79 {
80 v = 1;
Rich Evansf90016a2015-01-19 14:26:37 +000081 polarssl_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000082 }
83
Paul Bakker135b98e2011-05-25 11:13:47 +000084#if defined(POLARSSL_SELF_TEST)
85
Paul Bakker6e339b52013-07-03 13:37:05 +020086#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
87 memory_buffer_alloc_init( buf, sizeof(buf) );
88#endif
89
Paul Bakker40e46942009-01-03 21:51:57 +000090#if defined(POLARSSL_MD2_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000091 if( ( ret = md2_self_test( v ) ) != 0 )
92 return( ret );
93#endif
94
Paul Bakker40e46942009-01-03 21:51:57 +000095#if defined(POLARSSL_MD4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000096 if( ( ret = md4_self_test( v ) ) != 0 )
97 return( ret );
98#endif
99
Paul Bakker40e46942009-01-03 21:51:57 +0000100#if defined(POLARSSL_MD5_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 if( ( ret = md5_self_test( v ) ) != 0 )
102 return( ret );
103#endif
104
Paul Bakker61b699e2014-01-22 13:35:29 +0100105#if defined(POLARSSL_RIPEMD160_C)
106 if( ( ret = ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100107 return( ret );
108#endif
109
Paul Bakker40e46942009-01-03 21:51:57 +0000110#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 if( ( ret = sha1_self_test( v ) ) != 0 )
112 return( ret );
113#endif
114
Paul Bakker9e36f042013-06-30 14:34:05 +0200115#if defined(POLARSSL_SHA256_C)
116 if( ( ret = sha256_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 return( ret );
118#endif
119
Paul Bakker5a1d6872014-03-26 11:20:05 +0100120#if defined(POLARSSL_SHA512_C)
Paul Bakker9e36f042013-06-30 14:34:05 +0200121 if( ( ret = sha512_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 return( ret );
123#endif
124
Paul Bakker40e46942009-01-03 21:51:57 +0000125#if defined(POLARSSL_ARC4_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 if( ( ret = arc4_self_test( v ) ) != 0 )
127 return( ret );
128#endif
129
Paul Bakker40e46942009-01-03 21:51:57 +0000130#if defined(POLARSSL_DES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 if( ( ret = des_self_test( v ) ) != 0 )
132 return( ret );
133#endif
134
Paul Bakker40e46942009-01-03 21:51:57 +0000135#if defined(POLARSSL_AES_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 if( ( ret = aes_self_test( v ) ) != 0 )
137 return( ret );
138#endif
139
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200140#if defined(POLARSSL_GCM_C) && defined(POLARSSL_AES_C)
Paul Bakker89e80c92012-03-20 13:50:09 +0000141 if( ( ret = gcm_self_test( v ) ) != 0 )
142 return( ret );
143#endif
144
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200145#if defined(POLARSSL_CCM_C) && defined(POLARSSL_AES_C)
146 if( ( ret = ccm_self_test( v ) ) != 0 )
147 return( ret );
148#endif
149
Paul Bakker40e46942009-01-03 21:51:57 +0000150#if defined(POLARSSL_BASE64_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 if( ( ret = base64_self_test( v ) ) != 0 )
152 return( ret );
153#endif
154
Paul Bakker40e46942009-01-03 21:51:57 +0000155#if defined(POLARSSL_BIGNUM_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 if( ( ret = mpi_self_test( v ) ) != 0 )
157 return( ret );
158#endif
159
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100160#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 if( ( ret = rsa_self_test( v ) ) != 0 )
162 return( ret );
163#endif
164
Paul Bakker7504d7f2013-09-16 22:56:18 +0200165#if defined(POLARSSL_X509_USE_C)
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 if( ( ret = x509_self_test( v ) ) != 0 )
167 return( ret );
168#endif
169
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000170#if defined(POLARSSL_XTEA_C)
171 if( ( ret = xtea_self_test( v ) ) != 0 )
172 return( ret );
173#endif
174
Paul Bakker38119b12009-01-10 23:31:23 +0000175#if defined(POLARSSL_CAMELLIA_C)
176 if( ( ret = camellia_self_test( v ) ) != 0 )
177 return( ret );
178#endif
179
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000180#if defined(POLARSSL_CTR_DRBG_C)
181 if( ( ret = ctr_drbg_self_test( v ) ) != 0 )
182 return( ret );
183#endif
184
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100185#if defined(POLARSSL_HMAC_DRBG_C)
186 if( ( ret = hmac_drbg_self_test( v ) ) != 0 )
187 return( ret );
188#endif
189
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100190#if defined(POLARSSL_ECP_C)
191 if( ( ret = ecp_self_test( v ) ) != 0 )
192 return( ret );
193#endif
194
Paul Bakker40ce79f2013-09-15 17:43:54 +0200195#if defined(POLARSSL_DHM_C)
196 if( ( ret = dhm_self_test( v ) ) != 0 )
197 return( ret );
198#endif
199
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200200#if defined(POLARSSL_ENTROPY_C)
201 if( ( ret = entropy_self_test( v ) ) != 0 )
202 return( ret );
203#endif
204
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100205#if defined(POLARSSL_PKCS5_C)
206 if( ( ret = pkcs5_self_test( v ) ) != 0 )
207 return( ret );
208#endif
Manuel Pégourié-Gonnardb6b16bd2015-03-11 11:20:43 +0000209
210/* Slow tests last */
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100211
Manuel Pégourié-Gonnard76806982014-06-13 18:04:42 +0200212/* Not stable enough on Windows and FreeBSD yet */
213#if __linux__ && defined(POLARSSL_TIMING_C)
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100214 if( ( ret = timing_self_test( v ) ) != 0 )
215 return( ret );
216#endif
217
Paul Bakker135b98e2011-05-25 11:13:47 +0000218#else
Rich Evansf90016a2015-01-19 14:26:37 +0000219 polarssl_printf( " POLARSSL_SELF_TEST not defined.\n" );
Paul Bakker135b98e2011-05-25 11:13:47 +0000220#endif
221
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 if( v != 0 )
223 {
Paul Bakker6e339b52013-07-03 13:37:05 +0200224#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C) && defined(POLARSSL_MEMORY_DEBUG)
225 memory_buffer_alloc_status();
226#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100227 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200228
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100229#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
230 memory_buffer_alloc_free();
231
232 if( ( ret = memory_buffer_alloc_self_test( v ) ) != 0 )
233 return( ret );
234#endif
235
236 if( v != 0 )
237 {
Rich Evansf90016a2015-01-19 14:26:37 +0000238 polarssl_printf( " [ All tests passed ]\n\n" );
Paul Bakkercce9d772011-11-18 14:26:47 +0000239#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000240 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 fflush( stdout ); getchar();
242#endif
243 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245 return( ret );
246}