blob: 405d0d89abf3cbf7cc8ca2044bd4673a53e929a6 [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";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020074 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020075
76 ret = mbedtls_snprintf( buf, n, "%s", "123" );
77 if( ret < 0 || (size_t) ret >= n )
78 ret = -1;
79
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020080 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
81 ref_ret != ret ||
82 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020083 {
84 return( 1 );
85 }
86
87 return( 0 );
88}
89
90static int run_test_snprintf( void )
91{
92 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020093 test_snprintf( 1, "", -1 ) != 0 ||
94 test_snprintf( 2, "1", -1 ) != 0 ||
95 test_snprintf( 3, "12", -1 ) != 0 ||
96 test_snprintf( 4, "123", 3 ) != 0 ||
97 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020098}
99
Paul Bakker5121ce52009-01-03 21:22:43 +0000100int main( int argc, char *argv[] )
101{
Paul Bakker135b98e2011-05-25 11:13:47 +0000102 int ret = 0, v;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakker6e339b52013-07-03 13:37:05 +0200104 unsigned char buf[1000000];
105#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200106 void *pointer;
107
108 /*
109 * The C standard doesn't guarantee that all-bits-0 is the representation
110 * of a NULL pointer. We do however use that in our code for initializing
111 * structures, which should work on every modern platform. Let's be sure.
112 */
113 memset( &pointer, 0, sizeof( void * ) );
114 if( pointer != NULL )
115 {
116 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
117 return( 1 );
118 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200120 /*
121 * Make sure we have a snprintf that correctly zero-terminates
122 */
123 if( run_test_snprintf() != 0 )
124 {
125 mbedtls_printf( "the snprintf implementation is broken\n" );
126 return( 0 );
127 }
128
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
130 v = 0;
131 else
132 {
133 v = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 }
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
140 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200141#endif
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_MD2_C)
144 if( ( ret = mbedtls_md2_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_MD4_C)
149 if( ( ret = mbedtls_md4_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_MD5_C)
154 if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 return( ret );
156#endif
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#if defined(MBEDTLS_RIPEMD160_C)
159 if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100160 return( ret );
161#endif
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_SHA1_C)
164 if( ( ret = mbedtls_sha1_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_SHA256_C)
169 if( ( ret = mbedtls_sha256_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_SHA512_C)
174 if( ( ret = mbedtls_sha512_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_ARC4_C)
179 if( ( ret = mbedtls_arc4_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_DES_C)
184 if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 return( ret );
186#endif
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_AES_C)
189 if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 return( ret );
191#endif
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
194 if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000195 return( ret );
196#endif
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
199 if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200200 return( ret );
201#endif
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_BASE64_C)
204 if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 return( ret );
206#endif
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#if defined(MBEDTLS_BIGNUM_C)
209 if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 return( ret );
211#endif
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_RSA_C)
214 if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 return( ret );
216#endif
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_X509_USE_C)
219 if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 return( ret );
221#endif
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_XTEA_C)
224 if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000225 return( ret );
226#endif
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_CAMELLIA_C)
229 if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
Paul Bakker38119b12009-01-10 23:31:23 +0000230 return( ret );
231#endif
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#if defined(MBEDTLS_CTR_DRBG_C)
234 if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000235 return( ret );
236#endif
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#if defined(MBEDTLS_HMAC_DRBG_C)
239 if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100240 return( ret );
241#endif
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_ECP_C)
244 if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100245 return( ret );
246#endif
247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_DHM_C)
249 if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200250 return( ret );
251#endif
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_ENTROPY_C)
254 if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200255 return( ret );
256#endif
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_PKCS5_C)
259 if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100260 return( ret );
261#endif
Manuel Pégourié-Gonnardb6b16bd2015-03-11 11:20:43 +0000262
263/* Slow tests last */
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100264
Manuel Pégourié-Gonnard633c6b62015-06-26 16:17:30 +0200265#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100267 return( ret );
268#endif
269
Paul Bakker135b98e2011-05-25 11:13:47 +0000270#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
Paul Bakker135b98e2011-05-25 11:13:47 +0000272#endif
273
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 if( v != 0 )
275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
277 mbedtls_memory_buffer_alloc_status();
Paul Bakker6e339b52013-07-03 13:37:05 +0200278#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100279 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
282 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100285 return( ret );
286#endif
287
288 if( v != 0 )
289 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_printf( " [ All tests passed ]\n\n" );
Paul Bakkercce9d772011-11-18 14:26:47 +0000291#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 fflush( stdout ); getchar();
294#endif
295 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
297 return( ret );
298}