blob: 5c1d354a8296d56824ca345b1de0985e4e43d40c [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
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +020075 void *pointer;
76
77 /*
78 * The C standard doesn't guarantee that all-bits-0 is the representation
79 * of a NULL pointer. We do however use that in our code for initializing
80 * structures, which should work on every modern platform. Let's be sure.
81 */
82 memset( &pointer, 0, sizeof( void * ) );
83 if( pointer != NULL )
84 {
85 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
86 return( 1 );
87 }
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
90 v = 0;
91 else
92 {
93 v = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +000095 }
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +000098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
100 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200101#endif
102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_MD2_C)
104 if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 return( ret );
106#endif
107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_MD4_C)
109 if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 return( ret );
111#endif
112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_MD5_C)
114 if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 return( ret );
116#endif
117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118#if defined(MBEDTLS_RIPEMD160_C)
119 if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100120 return( ret );
121#endif
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if defined(MBEDTLS_SHA1_C)
124 if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 return( ret );
126#endif
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_SHA256_C)
129 if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 return( ret );
131#endif
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_SHA512_C)
134 if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 return( ret );
136#endif
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_ARC4_C)
139 if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 return( ret );
141#endif
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_DES_C)
144 if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 return( ret );
146#endif
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#if defined(MBEDTLS_AES_C)
149 if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 return( ret );
151#endif
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
154 if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000155 return( ret );
156#endif
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
159 if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200160 return( ret );
161#endif
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_BASE64_C)
164 if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 return( ret );
166#endif
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_BIGNUM_C)
169 if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 return( ret );
171#endif
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#if defined(MBEDTLS_RSA_C)
174 if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 return( ret );
176#endif
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_X509_USE_C)
179 if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 return( ret );
181#endif
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_XTEA_C)
184 if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000185 return( ret );
186#endif
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_CAMELLIA_C)
189 if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
Paul Bakker38119b12009-01-10 23:31:23 +0000190 return( ret );
191#endif
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_CTR_DRBG_C)
194 if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000195 return( ret );
196#endif
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_HMAC_DRBG_C)
199 if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100200 return( ret );
201#endif
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_ECP_C)
204 if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100205 return( ret );
206#endif
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_DHM_C)
209 if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200210 return( ret );
211#endif
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_ENTROPY_C)
214 if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200215 return( ret );
216#endif
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_PKCS5_C)
219 if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100220 return( ret );
221#endif
Manuel Pégourié-Gonnardb6b16bd2015-03-11 11:20:43 +0000222
223/* Slow tests last */
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100224
Manuel Pégourié-Gonnard76806982014-06-13 18:04:42 +0200225/* Not stable enough on Windows and FreeBSD yet */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if __linux__ && defined(MBEDTLS_TIMING_C)
227 if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100228 return( ret );
229#endif
230
Paul Bakker135b98e2011-05-25 11:13:47 +0000231#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
Paul Bakker135b98e2011-05-25 11:13:47 +0000233#endif
234
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 if( v != 0 )
236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
238 mbedtls_memory_buffer_alloc_status();
Paul Bakker6e339b52013-07-03 13:37:05 +0200239#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100240 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
243 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100246 return( ret );
247#endif
248
249 if( v != 0 )
250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( " [ All tests passed ]\n\n" );
Paul Bakkercce9d772011-11-18 14:26:47 +0000252#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 fflush( stdout ); getchar();
255#endif
256 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
258 return( ret );
259}