blob: 2db7da5d542f02b7b80110d0c4dfef3949c79a22 [file] [log] [blame]
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +02001/*
2 * Self-test demonstration program
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +020045 */
46
47#if !defined(POLARSSL_CONFIG_FILE)
48#include "mbedtls/config.h"
49#else
50#include MBEDTLS_CONFIG_FILE
51#endif
52
53#include "mbedtls/entropy.h"
54#include "mbedtls/hmac_drbg.h"
55#include "mbedtls/ctr_drbg.h"
56#include "mbedtls/dhm.h"
57#include "mbedtls/gcm.h"
58#include "mbedtls/ccm.h"
59#include "mbedtls/md2.h"
60#include "mbedtls/md4.h"
61#include "mbedtls/md5.h"
62#include "mbedtls/ripemd160.h"
63#include "mbedtls/sha1.h"
64#include "mbedtls/sha256.h"
65#include "mbedtls/sha512.h"
66#include "mbedtls/arc4.h"
67#include "mbedtls/des.h"
68#include "mbedtls/aes.h"
69#include "mbedtls/camellia.h"
70#include "mbedtls/base64.h"
71#include "mbedtls/bignum.h"
72#include "mbedtls/rsa.h"
73#include "mbedtls/x509.h"
74#include "mbedtls/xtea.h"
75#include "mbedtls/pkcs5.h"
76#include "mbedtls/ecp.h"
77
78#include <stdio.h>
79#include <string.h>
80
81#if defined(MBEDTLS_PLATFORM_C)
82#include "mbedtls/platform.h"
83#else
84#include <stdio.h>
85#define mbedtls_printf printf
86#endif
87
88#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
89#include "mbedtls/memory_buffer_alloc.h"
90#endif
91
92int selftest( int argc, char *argv[] )
93{
94 int ret = 0, v;
95#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
96 unsigned char buf[1000000];
97#endif
98
99 if( argc == 2 && strcmp( argv[1], "-quiet" ) == 0 )
100 v = 0;
101 else
102 {
103 v = 1;
104 mbedtls_printf( "\n" );
105 }
106
107#if defined(MBEDTLS_SELF_TEST)
108
109#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
110 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
111#endif
112
113#if defined(MBEDTLS_MD2_C)
114 if( ( ret = mbedtls_md2_self_test( v ) ) != 0 )
115 return( ret );
116#endif
117
118#if defined(MBEDTLS_MD4_C)
119 if( ( ret = mbedtls_md4_self_test( v ) ) != 0 )
120 return( ret );
121#endif
122
123#if defined(MBEDTLS_MD5_C)
124 if( ( ret = mbedtls_md5_self_test( v ) ) != 0 )
125 return( ret );
126#endif
127
128#if defined(MBEDTLS_RIPEMD160_C)
129 if( ( ret = mbedtls_ripemd160_self_test( v ) ) != 0 )
130 return( ret );
131#endif
132
133#if defined(MBEDTLS_SHA1_C)
134 if( ( ret = mbedtls_sha1_self_test( v ) ) != 0 )
135 return( ret );
136#endif
137
138#if defined(MBEDTLS_SHA256_C)
139 if( ( ret = mbedtls_sha256_self_test( v ) ) != 0 )
140 return( ret );
141#endif
142
143#if defined(MBEDTLS_SHA512_C)
144 if( ( ret = mbedtls_sha512_self_test( v ) ) != 0 )
145 return( ret );
146#endif
147
148#if defined(MBEDTLS_ARC4_C)
149 if( ( ret = mbedtls_arc4_self_test( v ) ) != 0 )
150 return( ret );
151#endif
152
153#if defined(MBEDTLS_DES_C)
154 if( ( ret = mbedtls_des_self_test( v ) ) != 0 )
155 return( ret );
156#endif
157
158#if defined(MBEDTLS_AES_C)
159 if( ( ret = mbedtls_aes_self_test( v ) ) != 0 )
160 return( ret );
161#endif
162
163#if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
164 if( ( ret = mbedtls_gcm_self_test( v ) ) != 0 )
165 return( ret );
166#endif
167
168#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
169 if( ( ret = mbedtls_ccm_self_test( v ) ) != 0 )
170 return( ret );
171#endif
172
173#if defined(MBEDTLS_BASE64_C)
174 if( ( ret = mbedtls_base64_self_test( v ) ) != 0 )
175 return( ret );
176#endif
177
178#if defined(MBEDTLS_BIGNUM_C)
179 if( ( ret = mbedtls_mpi_self_test( v ) ) != 0 )
180 return( ret );
181#endif
182
183#if defined(MBEDTLS_RSA_C)
184 if( ( ret = mbedtls_rsa_self_test( v ) ) != 0 )
185 return( ret );
186#endif
187
188#if defined(MBEDTLS_X509_USE_C)
189 if( ( ret = mbedtls_x509_self_test( v ) ) != 0 )
190 return( ret );
191#endif
192
193#if defined(MBEDTLS_XTEA_C)
194 if( ( ret = mbedtls_xtea_self_test( v ) ) != 0 )
195 return( ret );
196#endif
197
198#if defined(MBEDTLS_CAMELLIA_C)
199 if( ( ret = mbedtls_camellia_self_test( v ) ) != 0 )
200 return( ret );
201#endif
202
203#if defined(MBEDTLS_CTR_DRBG_C)
204 if( ( ret = mbedtls_ctr_drbg_self_test( v ) ) != 0 )
205 return( ret );
206#endif
207
208#if defined(MBEDTLS_HMAC_DRBG_C)
209 if( ( ret = mbedtls_hmac_drbg_self_test( v ) ) != 0 )
210 return( ret );
211#endif
212
213#if defined(MBEDTLS_ECP_C)
214 if( ( ret = mbedtls_ecp_self_test( v ) ) != 0 )
215 return( ret );
216#endif
217
218#if defined(MBEDTLS_DHM_C)
219 if( ( ret = mbedtls_dhm_self_test( v ) ) != 0 )
220 return( ret );
221#endif
222
223#if defined(MBEDTLS_ENTROPY_C)
224 if( ( ret = mbedtls_entropy_self_test( v ) ) != 0 )
225 return( ret );
226#endif
227
228#if defined(MBEDTLS_PKCS5_C)
229 if( ( ret = mbedtls_pkcs5_self_test( v ) ) != 0 )
230 return( ret );
231#endif
232
233#if defined(MBEDTLS_TIMING_C)
234 if( ( ret = mbedtls_timing_self_test( v ) ) != 0 )
235 return( ret );
236#endif
237
238#else
239 mbedtls_printf( " POLARSSL_SELF_TEST not defined.\n" );
240#endif
241
242 if( v != 0 )
243 {
244#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_MEMORY_DEBUG)
245 mbedtls_memory_buffer_alloc_status();
246#endif
247 }
248
249#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
250 mbedtls_memory_buffer_alloc_free();
251
252 if( ( ret = mbedtls_memory_buffer_alloc_self_test( v ) ) != 0 )
253 return( ret );
254#endif
255
256 if( v != 0 )
257 {
258 mbedtls_printf( " [ All tests passed ]\n\n" );
259#if defined(_WIN32)
260 mbedtls_printf( " Press Enter to exit this program.\n" );
261 fflush( stdout ); getchar();
262#endif
263 }
264
265 return( ret );
266}
267
268#if defined(TARGET_LIKE_MBED)
269
Manuel Pégourié-Gonnard71956c92015-10-21 17:59:05 +0200270#include "mbed-drivers/test_env.h"
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200271#include "minar/minar.h"
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200272
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200273static void run() {
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200274 MBED_HOSTTEST_TIMEOUT(40);
275 MBED_HOSTTEST_SELECT(default);
276 MBED_HOSTTEST_DESCRIPTION(mbed TLS selftest program);
277 MBED_HOSTTEST_START("MBEDTLS_SELFTEST");
278 MBED_HOSTTEST_RESULT(selftest(0, NULL) == 0);
279}
280
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200281void app_start(int, char*[]) {
Janos Follath60ddf162016-03-17 13:55:07 +0000282 /* Use 115200 bps for consistency with other examples */
283 get_stdio_serial().baud(115200);
Manuel Pégourié-Gonnardca4fb712015-09-18 14:36:57 +0200284 minar::Scheduler::postCallback(mbed::util::FunctionPointer0<void>(run).bind());
Manuel Pégourié-Gonnarde87b04c2015-08-11 04:09:44 +0200285}
286
Manuel Pégourié-Gonnard63e7eba2015-07-28 14:17:48 +0200287#else
288
289int main() {
290 return selftest(0, NULL);
291}
292
293#endif