blob: c46b8e758e9c0d6690d77a87b299f07101a66593 [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
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020063#define mbedtls_snprintf snprintf
Rich Evans18b78c72015-02-11 14:06:19 +000064#endif
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/memory_buffer_alloc.h"
Paul Bakker6e339b52013-07-03 13:37:05 +020068#endif
69
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020070static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
71{
72 int ret;
73 char buf[10] = "xxxxxxxxx";
74
75 ret = mbedtls_snprintf( buf, n, "%s", "123" );
76 if( ret < 0 || (size_t) ret >= n )
77 ret = -1;
78
79 if( memcmp( ref_buf, buf, sizeof buf ) != 0 ||
80 ref_ret != ret )
81 {
82 return( 1 );
83 }
84
85 return( 0 );
86}
87
88static int run_test_snprintf( void )
89{
90 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
91 test_snprintf( 1, "\0xxxxxxxx", -1 ) != 0 ||
92 test_snprintf( 2, "1\0xxxxxxx", -1 ) != 0 ||
93 test_snprintf( 3, "12\0xxxxxx", -1 ) != 0 ||
94 test_snprintf( 4, "123\0xxxxx", 3 ) != 0 ||
95 test_snprintf( 5, "123\0xxxxx", 3 ) != 0 );
96}
97
Paul Bakker5121ce52009-01-03 21:22:43 +000098int main( int argc, char *argv[] )
99{
Paul Bakker135b98e2011-05-25 11:13:47 +0000100 int ret = 0, v;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakker6e339b52013-07-03 13:37:05 +0200102 unsigned char buf[1000000];
103#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200104 void *pointer;
105
106 /*
107 * The C standard doesn't guarantee that all-bits-0 is the representation
108 * of a NULL pointer. We do however use that in our code for initializing
109 * structures, which should work on every modern platform. Let's be sure.
110 */
111 memset( &pointer, 0, sizeof( void * ) );
112 if( pointer != NULL )
113 {
114 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
115 return( 1 );
116 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200118 /*
119 * Make sure we have a snprintf that correctly zero-terminates
120 */
121 if( run_test_snprintf() != 0 )
122 {
123 mbedtls_printf( "the snprintf implementation is broken\n" );
124 return( 0 );
125 }
126
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
128 v = 0;
129 else
130 {
131 v = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
138 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200139#endif
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_MD2_C)
142 if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 return( ret );
144#endif
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#if defined(MBEDTLS_MD4_C)
147 if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 return( ret );
149#endif
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_MD5_C)
152 if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 return( ret );
154#endif
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_RIPEMD160_C)
157 if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100158 return( ret );
159#endif
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_SHA1_C)
162 if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 return( ret );
164#endif
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_SHA256_C)
167 if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 return( ret );
169#endif
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_SHA512_C)
172 if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 return( ret );
174#endif
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_ARC4_C)
177 if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 return( ret );
179#endif
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_DES_C)
182 if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 return( ret );
184#endif
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186#if defined(MBEDTLS_AES_C)
187 if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 return( ret );
189#endif
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
192 if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000193 return( ret );
194#endif
195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
197 if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200198 return( ret );
199#endif
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_BASE64_C)
202 if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 return( ret );
204#endif
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_BIGNUM_C)
207 if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 return( ret );
209#endif
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_RSA_C)
212 if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 return( ret );
214#endif
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#if defined(MBEDTLS_X509_USE_C)
217 if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 return( ret );
219#endif
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_XTEA_C)
222 if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000223 return( ret );
224#endif
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_CAMELLIA_C)
227 if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
Paul Bakker38119b12009-01-10 23:31:23 +0000228 return( ret );
229#endif
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_CTR_DRBG_C)
232 if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000233 return( ret );
234#endif
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_HMAC_DRBG_C)
237 if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100238 return( ret );
239#endif
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#if defined(MBEDTLS_ECP_C)
242 if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100243 return( ret );
244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_DHM_C)
247 if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200248 return( ret );
249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_ENTROPY_C)
252 if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200253 return( ret );
254#endif
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_PKCS5_C)
257 if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100258 return( ret );
259#endif
Manuel Pégourié-Gonnardb6b16bd2015-03-11 11:20:43 +0000260
261/* Slow tests last */
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100262
Manuel Pégourié-Gonnard76806982014-06-13 18:04:42 +0200263/* Not stable enough on Windows and FreeBSD yet */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if __linux__ && defined(MBEDTLS_TIMING_C)
265 if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100266 return( ret );
267#endif
268
Paul Bakker135b98e2011-05-25 11:13:47 +0000269#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
Paul Bakker135b98e2011-05-25 11:13:47 +0000271#endif
272
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 if( v != 0 )
274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
276 mbedtls_memory_buffer_alloc_status();
Paul Bakker6e339b52013-07-03 13:37:05 +0200277#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100278 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
281 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100284 return( ret );
285#endif
286
287 if( v != 0 )
288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_printf( " [ All tests passed ]\n\n" );
Paul Bakkercce9d772011-11-18 14:26:47 +0000290#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 fflush( stdout ); getchar();
293#endif
294 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
296 return( ret );
297}