blob: 7fd308d4fee50a7e5f52d29861e093ef2fd617a3 [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"
52#include "mbedtls/timing.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Rich Evans18b78c72015-02-11 14:06:19 +000054#include <stdio.h>
55#include <string.h>
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000059#else
60#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define mbedtls_printf printf
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020062#define mbedtls_snprintf snprintf
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
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020069static int test_snprintf( size_t n, const char ref_buf[10], int ref_ret )
70{
71 int ret;
72 char buf[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020073 const char ref[10] = "xxxxxxxxx";
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020074
75 ret = mbedtls_snprintf( buf, n, "%s", "123" );
76 if( ret < 0 || (size_t) ret >= n )
77 ret = -1;
78
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020079 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
80 ref_ret != ret ||
81 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020082 {
83 return( 1 );
84 }
85
86 return( 0 );
87}
88
89static int run_test_snprintf( void )
90{
91 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
Manuel Pégourié-Gonnard4b00f082015-06-26 11:24:32 +020092 test_snprintf( 1, "", -1 ) != 0 ||
93 test_snprintf( 2, "1", -1 ) != 0 ||
94 test_snprintf( 3, "12", -1 ) != 0 ||
95 test_snprintf( 4, "123", 3 ) != 0 ||
96 test_snprintf( 5, "123", 3 ) != 0 );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +020097}
98
Gilles Peskine9d7dfb72017-12-20 20:23:46 +010099#if defined(MBEDTLS_SELF_TEST)
100#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
101int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
102{
103 if( verbose != 0 )
104 {
105#if defined(MBEDTLS_MEMORY_DEBUG)
106 mbedtls_memory_buffer_alloc_status( );
107#endif
108 }
109 mbedtls_memory_buffer_alloc_free( );
110 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
111}
112#endif
113
114typedef struct
115{
116 const char *name;
117 int ( *function )( int );
118} selftest_t;
119
120const selftest_t selftests[] =
121{
122#if defined(MBEDTLS_MD2_C)
123 {"md2", mbedtls_md2_self_test},
124#endif
125#if defined(MBEDTLS_MD4_C)
126 {"md4", mbedtls_md4_self_test},
127#endif
128#if defined(MBEDTLS_MD5_C)
129 {"md5", mbedtls_md5_self_test},
130#endif
131#if defined(MBEDTLS_RIPEMD160_C)
132 {"ripemd160", mbedtls_ripemd160_self_test},
133#endif
134#if defined(MBEDTLS_SHA1_C)
135 {"sha1", mbedtls_sha1_self_test},
136#endif
137#if defined(MBEDTLS_SHA256_C)
138 {"sha256", mbedtls_sha256_self_test},
139#endif
140#if defined(MBEDTLS_SHA512_C)
141 {"sha512", mbedtls_sha512_self_test},
142#endif
143#if defined(MBEDTLS_ARC4_C)
144 {"arc4", mbedtls_arc4_self_test},
145#endif
146#if defined(MBEDTLS_DES_C)
147 {"des", mbedtls_des_self_test},
148#endif
149#if defined(MBEDTLS_AES_C)
150 {"aes", mbedtls_aes_self_test},
151#endif
152#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
153 {"gcm", mbedtls_gcm_self_test},
154#endif
155#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
156 {"ccm", mbedtls_ccm_self_test},
157#endif
158#if defined(MBEDTLS_BASE64_C)
159 {"base64", mbedtls_base64_self_test},
160#endif
161#if defined(MBEDTLS_BIGNUM_C)
162 {"mpi", mbedtls_mpi_self_test},
163#endif
164#if defined(MBEDTLS_RSA_C)
165 {"rsa", mbedtls_rsa_self_test},
166#endif
167#if defined(MBEDTLS_X509_USE_C)
168 {"x509", mbedtls_x509_self_test},
169#endif
170#if defined(MBEDTLS_XTEA_C)
171 {"xtea", mbedtls_xtea_self_test},
172#endif
173#if defined(MBEDTLS_CAMELLIA_C)
174 {"camellia", mbedtls_camellia_self_test},
175#endif
176#if defined(MBEDTLS_CTR_DRBG_C)
177 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
178#endif
179#if defined(MBEDTLS_HMAC_DRBG_C)
180 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
181#endif
182#if defined(MBEDTLS_ECP_C)
183 {"ecp", mbedtls_ecp_self_test},
184#endif
185#if defined(MBEDTLS_DHM_C)
186 {"dhm", mbedtls_dhm_self_test},
187#endif
188#if defined(MBEDTLS_ENTROPY_C)
189 {"entropy", mbedtls_entropy_self_test},
190#endif
191#if defined(MBEDTLS_PKCS5_C)
192 {"pkcs5", mbedtls_pkcs5_self_test},
193#endif
194/* Slower test after the faster ones */
195#if defined(MBEDTLS_TIMING_C)
196 {"timing", mbedtls_timing_self_test},
197#endif
198/* Heap test comes last */
199#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
200 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
201#endif
202 {NULL, NULL}
203};
204#endif /* MBEDTLS_SELF_TEST */
205
Paul Bakker5121ce52009-01-03 21:22:43 +0000206int main( int argc, char *argv[] )
207{
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100208#if defined(MBEDTLS_SELF_TEST)
209 const selftest_t *test;
210#endif /* MBEDTLS_SELF_TEST */
Gilles Peskineedede442017-12-15 15:01:27 +0100211 char **argp = argc >= 1 ? argv + 1 : argv;
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100212 int v;
213 int suites_tested = 0, suites_failed = 0;
Gilles Peskineedede442017-12-15 15:01:27 +0100214#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
Paul Bakker6e339b52013-07-03 13:37:05 +0200215 unsigned char buf[1000000];
216#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200217 void *pointer;
218
219 /*
220 * The C standard doesn't guarantee that all-bits-0 is the representation
221 * of a NULL pointer. We do however use that in our code for initializing
222 * structures, which should work on every modern platform. Let's be sure.
223 */
224 memset( &pointer, 0, sizeof( void * ) );
225 if( pointer != NULL )
226 {
227 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
Gilles Peskine6d51b632017-12-20 20:25:03 +0100228 return( EXIT_FAILURE );
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200229 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200231 /*
232 * Make sure we have a snprintf that correctly zero-terminates
233 */
234 if( run_test_snprintf() != 0 )
235 {
236 mbedtls_printf( "the snprintf implementation is broken\n" );
Gilles Peskine6d51b632017-12-20 20:25:03 +0100237 return( EXIT_FAILURE );
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200238 }
239
Gilles Peskineedede442017-12-15 15:01:27 +0100240 if( argc >= 2 && ( strcmp( argv[1], "--quiet" ) == 0 ||
241 strcmp( argv[1], "-quiet" ) == 0 ||
242 strcmp( argv[1], "-q" ) == 0 ) )
243 {
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 v = 0;
Gilles Peskineedede442017-12-15 15:01:27 +0100245 ++argp;
246 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 else
248 {
249 v = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 }
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
256 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200257#endif
258
Gilles Peskineedede442017-12-15 15:01:27 +0100259 if( *argp != NULL )
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100260 {
Gilles Peskineedede442017-12-15 15:01:27 +0100261 /* Run the specified tests */
262 for( ; *argp != NULL; argp++ )
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100263 {
Gilles Peskineedede442017-12-15 15:01:27 +0100264 for( test = selftests; test->name != NULL; test++ )
265 {
266 if( !strcmp( *argp, test->name ) )
267 {
268 if( test->function( v ) != 0 )
269 {
270 suites_failed++;
271 }
272 suites_tested++;
273 break;
274 }
275 }
276 if( test->name == NULL )
277 {
278 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
279 suites_failed++;
280 }
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100281 }
Gilles Peskineedede442017-12-15 15:01:27 +0100282 }
283 else
284 {
285 /* Run all the tests */
286 for( test = selftests; test->name != NULL; test++ )
287 {
288 if( test->function( v ) != 0 )
289 {
290 suites_failed++;
291 }
292 suites_tested++;
293 }
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100294 }
Manuel Pégourié-Gonnard470fc932014-03-27 20:07:08 +0100295
Paul Bakker135b98e2011-05-25 11:13:47 +0000296#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
Paul Bakker135b98e2011-05-25 11:13:47 +0000298#endif
299
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 if( v != 0 )
301 {
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100302 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
Paul Bakker6e339b52013-07-03 13:37:05 +0200303
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100304 if( suites_failed > 0)
305 {
306 mbedtls_printf( " [ %d tests FAILED ]\n\n", suites_failed );
307 }
308 else
309 {
310 mbedtls_printf( " [ All tests passed ]\n\n" );
311 }
Paul Bakkercce9d772011-11-18 14:26:47 +0000312#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 fflush( stdout ); getchar();
315#endif
316 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
Gilles Peskine9d7dfb72017-12-20 20:23:46 +0100318 if( suites_failed > 0)
319 return( EXIT_FAILURE );
320
321 return( EXIT_SUCCESS );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322}