blob: 82e9581f0d7d402d4bb44f57bb03a215414aa1f6 [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
Paul Bakker5121ce52009-01-03 21:22:43 +000099int main( int argc, char *argv[] )
100{
Paul Bakker135b98e2011-05-25 11:13:47 +0000101 int ret = 0, v;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
Paul Bakker6e339b52013-07-03 13:37:05 +0200103 unsigned char buf[1000000];
104#endif
Manuel Pégourié-Gonnardd14acbc2015-05-29 11:26:37 +0200105 void *pointer;
106
107 /*
108 * The C standard doesn't guarantee that all-bits-0 is the representation
109 * of a NULL pointer. We do however use that in our code for initializing
110 * structures, which should work on every modern platform. Let's be sure.
111 */
112 memset( &pointer, 0, sizeof( void * ) );
113 if( pointer != NULL )
114 {
115 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
116 return( 1 );
117 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Manuel Pégourié-Gonnard7b6dcbe2015-06-22 10:48:01 +0200119 /*
120 * Make sure we have a snprintf that correctly zero-terminates
121 */
122 if( run_test_snprintf() != 0 )
123 {
124 mbedtls_printf( "the snprintf implementation is broken\n" );
125 return( 0 );
126 }
127
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
129 v = 0;
130 else
131 {
132 v = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( "\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 }
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136#if defined(MBEDTLS_SELF_TEST)
Paul Bakker135b98e2011-05-25 11:13:47 +0000137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
139 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
Paul Bakker6e339b52013-07-03 13:37:05 +0200140#endif
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#if defined(MBEDTLS_MD2_C)
143 if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 return( ret );
145#endif
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_MD4_C)
148 if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 return( ret );
150#endif
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#if defined(MBEDTLS_MD5_C)
153 if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 return( ret );
155#endif
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_RIPEMD160_C)
158 if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard1744d722014-01-17 14:44:38 +0100159 return( ret );
160#endif
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_SHA1_C)
163 if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 return( ret );
165#endif
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#if defined(MBEDTLS_SHA256_C)
168 if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 return( ret );
170#endif
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_SHA512_C)
173 if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 return( ret );
175#endif
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#if defined(MBEDTLS_ARC4_C)
178 if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 return( ret );
180#endif
181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_DES_C)
183 if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 return( ret );
185#endif
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_AES_C)
188 if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 return( ret );
190#endif
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
193 if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
Paul Bakker89e80c92012-03-20 13:50:09 +0000194 return( ret );
195#endif
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
198 if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnarda6916fa2014-05-02 15:17:29 +0200199 return( ret );
200#endif
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_BASE64_C)
203 if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 return( ret );
205#endif
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_BIGNUM_C)
208 if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 return( ret );
210#endif
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_RSA_C)
213 if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 return( ret );
215#endif
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_X509_USE_C)
218 if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 return( ret );
220#endif
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_XTEA_C)
223 if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
Paul Bakker7a7c78f2009-01-04 18:15:48 +0000224 return( ret );
225#endif
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_CAMELLIA_C)
228 if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
Paul Bakker38119b12009-01-10 23:31:23 +0000229 return( ret );
230#endif
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_CTR_DRBG_C)
233 if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000234 return( ret );
235#endif
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_HMAC_DRBG_C)
238 if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100239 return( ret );
240#endif
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_ECP_C)
243 if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnardb4a310b2012-11-13 20:57:00 +0100244 return( ret );
245#endif
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_DHM_C)
248 if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
Paul Bakker40ce79f2013-09-15 17:43:54 +0200249 return( ret );
250#endif
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_ENTROPY_C)
253 if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200254 return( ret );
255#endif
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#if defined(MBEDTLS_PKCS5_C)
258 if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100259 return( ret );
260#endif
Manuel Pégourié-Gonnardb6b16bd2015-03-11 11:20:43 +0000261
262/* Slow tests last */
Manuel Pégourié-Gonnard13a1ef82014-03-27 20:12:44 +0100263
Manuel Pégourié-Gonnard633c6b62015-06-26 16:17:30 +0200264#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 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}