blob: 902225e3374671beba4b4db617fef76bfe745a89 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_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>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_printf printf
Rich Evans18b78c72015-02-11 14:06:19 +000063#endif
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakker6e339b52013-07-03 13:37:05 +020073 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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000082 }
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +000085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
87 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +020088#endif
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_MD2_C)
91 if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000092 return( ret );
93#endif
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#if defined(MBEDTLS_MD4_C)
96 if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000097 return( ret );
98#endif
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#if defined(MBEDTLS_MD5_C)
101 if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 return( ret );
103#endif
104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#if defined(MBEDTLS_RIPEMD160_C)
106 if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100107 return( ret );
108#endif
109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#if defined(MBEDTLS_SHA1_C)
111 if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 return( ret );
113#endif
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_SHA256_C)
116 if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 return( ret );
118#endif
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_SHA512_C)
121 if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 return( ret );
123#endif
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_ARC4_C)
126 if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 return( ret );
128#endif
129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#if defined(MBEDTLS_DES_C)
131 if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 return( ret );
133#endif
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_AES_C)
136 if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 return( ret );
138#endif
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
141 if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000142 return( ret );
143#endif
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
146 if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200147 return( ret );
148#endif
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150#if defined(MBEDTLS_BASE64_C)
151 if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 return( ret );
153#endif
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_BIGNUM_C)
156 if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 return( ret );
158#endif
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_RSA_C)
161 if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 return( ret );
163#endif
164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#if defined(MBEDTLS_X509_USE_C)
166 if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 return( ret );
168#endif
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_XTEA_C)
171 if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000172 return( ret );
173#endif
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_CAMELLIA_C)
176 if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
Paul Bakker38119b12009-01-10 23:31:23 +0000177 return( ret );
178#endif
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_CTR_DRBG_C)
181 if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000182 return( ret );
183#endif
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#if defined(MBEDTLS_HMAC_DRBG_C)
186 if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100187 return( ret );
188#endif
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_ECP_C)
191 if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100192 return( ret );
193#endif
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_DHM_C)
196 if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200197 return( ret );
198#endif
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_ENTROPY_C)
201 if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200202 return( ret );
203#endif
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#if defined(MBEDTLS_PKCS5_C)
206 if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100207 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if __linux__ && defined(MBEDTLS_TIMING_C)
214 if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100215 return( ret );
216#endif
217
Paul Bakker135b98e2011-05-25 11:13:47 +0000218#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_printf( " MBEDTLS_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 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
225 mbedtls_memory_buffer_alloc_status();
Paul Bakker6e339b52013-07-03 13:37:05 +0200226#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100227 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
230 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100233 return( ret );
234#endif
235
236 if( v != 0 )
237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_printf( " [ All tests passed ]\n\n" );
Paul Bakkercce9d772011-11-18 14:26:47 +0000239#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_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}