blob: b168b7112f62006433a1dddb28dc4facd356cc00 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Self-test demonstration program
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/entropy.h"
29#include "mbedtls/hmac_drbg.h"
30#include "mbedtls/ctr_drbg.h"
31#include "mbedtls/dhm.h"
32#include "mbedtls/gcm.h"
33#include "mbedtls/ccm.h"
34#include "mbedtls/md2.h"
35#include "mbedtls/md4.h"
36#include "mbedtls/md5.h"
37#include "mbedtls/ripemd160.h"
38#include "mbedtls/sha1.h"
39#include "mbedtls/sha256.h"
40#include "mbedtls/sha512.h"
41#include "mbedtls/arc4.h"
42#include "mbedtls/des.h"
43#include "mbedtls/aes.h"
44#include "mbedtls/camellia.h"
45#include "mbedtls/base64.h"
46#include "mbedtls/bignum.h"
47#include "mbedtls/rsa.h"
48#include "mbedtls/x509.h"
49#include "mbedtls/xtea.h"
50#include "mbedtls/pkcs5.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +020052#include "mbedtls/ecjpake.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#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{
Simon Butcherf1547632016-03-14 23:09:39 +0000102 int ret = 0, v, suites_tested = 0, suites_failed = 0,
SimonB5a8afb82016-03-11 00:40:54 +0000103 exitcode = EXIT_SUCCESS;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakker6e339b52013-07-03 13:37:05 +0200105 unsigned char buf[1000000];
106#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200107 void *pointer;
108
109 /*
110 * The C standard doesn't guarantee that all-bits-0 is the representation
111 * of a NULL pointer. We do however use that in our code for initializing
112 * structures, which should work on every modern platform. Let's be sure.
113 */
114 memset( &pointer, 0, sizeof( void * ) );
115 if( pointer != NULL )
116 {
117 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
118 return( 1 );
119 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200121 /*
122 * Make sure we have a snprintf that correctly zero-terminates
123 */
124 if( run_test_snprintf() != 0 )
125 {
126 mbedtls_printf( "the snprintf implementation is broken\n" );
127 return( 0 );
128 }
129
SimonB5a8afb82016-03-11 00:40:54 +0000130 if( argc == 2 && ( strcmp( argv[1], "--quiet" ) == 0 ||
131 strcmp( argv[1], "-q" ) == 0 ) )
132 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 v = 0;
SimonB5a8afb82016-03-11 00:40:54 +0000134 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 else
136 {
137 v = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 }
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
144 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200145#endif
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_MD2_C)
148 if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000149 {
150 suites_failed++;
151 }
152 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000153#endif
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_MD4_C)
156 if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000157 {
158 suites_failed++;
159 }
160 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000161#endif
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163#if defined(MBEDTLS_MD5_C)
164 if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000165 {
166 suites_failed++;
167 }
168 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000169#endif
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_RIPEMD160_C)
172 if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000173 {
174 suites_failed++;
175 }
176 suites_tested++;
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100177#endif
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_SHA1_C)
180 if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000181 {
182 suites_failed++;
183 }
184 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000185#endif
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_SHA256_C)
188 if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000189 {
190 suites_failed++;
191 }
192 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000193#endif
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_SHA512_C)
196 if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000197 {
198 suites_failed++;
199 }
200 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000201#endif
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if defined(MBEDTLS_ARC4_C)
204 if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000205 {
206 suites_failed++;
207 }
208 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000209#endif
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_DES_C)
212 if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000213 {
214 suites_failed++;
215 }
216 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217#endif
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_AES_C)
220 if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000221 {
222 suites_failed++;
223 }
224 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000225#endif
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
228 if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000229 {
230 suites_failed++;
231 }
232 suites_tested++;
Paul Bakker89e80c92012-03-20 13:50:09 +0000233#endif
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
236 if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000237 {
238 suites_failed++;
239 }
240 suites_tested++;
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200241#endif
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_BASE64_C)
244 if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000245 {
246 suites_failed++;
247 }
248 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000249#endif
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_BIGNUM_C)
252 if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000253 {
254 suites_failed++;
255 }
256 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000257#endif
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_RSA_C)
260 if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000261 {
262 suites_failed++;
263 }
264 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000265#endif
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_X509_USE_C)
268 if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000269 {
270 suites_failed++;
271 }
272 suites_tested++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000273#endif
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#if defined(MBEDTLS_XTEA_C)
276 if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000277 {
278 suites_failed++;
279 }
280 suites_tested++;
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000281#endif
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#if defined(MBEDTLS_CAMELLIA_C)
284 if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000285 {
286 suites_failed++;
287 }
288 suites_tested++;
Paul Bakker38119b12009-01-10 23:31:23 +0000289#endif
290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291#if defined(MBEDTLS_CTR_DRBG_C)
292 if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000293 {
294 suites_failed++;
295 }
296 suites_tested++;
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000297#endif
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#if defined(MBEDTLS_HMAC_DRBG_C)
300 if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000301 {
302 suites_failed++;
303 }
304 suites_tested++;
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100305#endif
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_ECP_C)
308 if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000309 {
310 suites_failed++;
311 }
312 suites_tested++;
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100313#endif
314
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200315#if defined(MBEDTLS_ECJPAKE_C)
316 if( ( ret = mbedtls_ecjpake_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000317 {
318 suites_failed++;
319 }
320 suites_tested++;
Manuel Pégourié-Gonnard4d8685b2015-08-05 15:44:42 +0200321#endif
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_DHM_C)
324 if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000325 {
326 suites_failed++;
327 }
328 suites_tested++;
Paul Bakker40ce79f2013-09-15 17:43:54 +0200329#endif
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#if defined(MBEDTLS_ENTROPY_C)
332 if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000333 {
334 suites_failed++;
335 }
336 suites_tested++;
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200337#endif
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#if defined(MBEDTLS_PKCS5_C)
340 if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000341 {
342 suites_failed++;
343 }
344 suites_tested++;
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100345#endif
Manuel Pégourié-Gonnardb6b16bd2015-03-11 11:20:43 +0000346
347/* Slow tests last */
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100348
Manuel Pégourié-Gonnard633c6b62015-06-26 16:17:30 +0200349#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000351 {
352 suites_failed++;
353 }
354 suites_tested++;
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100355#endif
356
Paul Bakker135b98e2011-05-25 11:13:47 +0000357#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
Paul Bakker135b98e2011-05-25 11:13:47 +0000359#endif
360
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 if( v != 0 )
362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
364 mbedtls_memory_buffer_alloc_status();
Paul Bakker6e339b52013-07-03 13:37:05 +0200365#endif
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100366 }
Paul Bakker6e339b52013-07-03 13:37:05 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
369 mbedtls_memory_buffer_alloc_free();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
SimonB5a8afb82016-03-11 00:40:54 +0000371 {
372 suites_failed++;
373 }
374 suites_tested++;
Manuel Pégourié-Gonnard5ba1d522014-11-27 11:33:55 +0100375#endif
376
377 if( v != 0 )
378 {
Simon Butcherf1547632016-03-14 23:09:39 +0000379 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
SimonB5a8afb82016-03-11 00:40:54 +0000380
381 if( suites_failed > 0)
382 {
383 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
384 }
385 else
386 {
387 mbedtls_printf( " [ All tests PASS ]\n\n" );
388 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000389#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 fflush( stdout ); getchar();
392#endif
393 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
SimonB5a8afb82016-03-11 00:40:54 +0000395 if( suites_failed > 0)
396 exitcode = EXIT_FAILURE;
397
Simon Butcherf1547632016-03-14 23:09:39 +0000398 exit( exitcode );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399}
SimonB5a8afb82016-03-11 00:40:54 +0000400