1 /*
2 * Self-test demonstration program
3 *
4 * Copyright The Mbed TLS Contributors
5 * 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.
18 */
19
20 #define MBEDTLS_ALLOW_PRIVATE_ACCESS
21
22 #include "mbedtls/build_info.h"
23
24 #include "mbedtls/entropy.h"
25 #include "mbedtls/hmac_drbg.h"
26 #include "mbedtls/ctr_drbg.h"
27 #include "mbedtls/dhm.h"
28 #include "mbedtls/gcm.h"
29 #include "mbedtls/ccm.h"
30 #include "mbedtls/cmac.h"
31 #include "mbedtls/md2.h"
32 #include "mbedtls/md4.h"
33 #include "mbedtls/md5.h"
34 #include "mbedtls/ripemd160.h"
35 #include "mbedtls/sha1.h"
36 #include "mbedtls/sha256.h"
37 #include "mbedtls/sha512.h"
38 #include "mbedtls/des.h"
39 #include "mbedtls/aes.h"
40 #include "mbedtls/camellia.h"
41 #include "mbedtls/aria.h"
42 #include "mbedtls/chacha20.h"
43 #include "mbedtls/poly1305.h"
44 #include "mbedtls/chachapoly.h"
45 #include "mbedtls/base64.h"
46 #include "mbedtls/bignum.h"
47 #include "mbedtls/rsa.h"
48 #include "mbedtls/x509.h"
49 #include "mbedtls/pkcs5.h"
50 #include "mbedtls/ecp.h"
51 #include "mbedtls/ecjpake.h"
52 #include "mbedtls/timing.h"
53 #include "mbedtls/nist_kw.h"
54
55 #include <string.h>
56
57 #if defined(MBEDTLS_PLATFORM_C)
58 #include "mbedtls/platform.h"
59 #else
60 #include <stdio.h>
61 #include <stdlib.h>
62 #define mbedtls_calloc calloc
63 #define mbedtls_free free
64 #define mbedtls_printf printf
65 #define mbedtls_snprintf snprintf
66 #define mbedtls_exit exit
67 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
68 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
69 #endif
70
71 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
72 #include "mbedtls/memory_buffer_alloc.h"
73 #endif
74
75
76 #if defined MBEDTLS_SELF_TEST
77 /* Sanity check for malloc. This is not expected to fail, and is rather
78 * intended to display potentially useful information about the platform,
79 * in particular the behavior of malloc(0). */
calloc_self_test(int verbose)80 static int calloc_self_test( int verbose )
81 {
82 int failures = 0;
83 void *empty1 = mbedtls_calloc( 0, 1 );
84 void *empty2 = mbedtls_calloc( 0, 1 );
85 void *buffer1 = mbedtls_calloc( 1, 1 );
86 void *buffer2 = mbedtls_calloc( 1, 1 );
87 uintptr_t old_buffer1;
88
89 if( empty1 == NULL && empty2 == NULL )
90 {
91 if( verbose )
92 mbedtls_printf( " CALLOC(0): passed (NULL)\n" );
93 }
94 else if( empty1 == NULL || empty2 == NULL )
95 {
96 if( verbose )
97 mbedtls_printf( " CALLOC(0): failed (mix of NULL and non-NULL)\n" );
98 ++failures;
99 }
100 else if( empty1 == empty2 )
101 {
102 if( verbose )
103 mbedtls_printf( " CALLOC(0): passed (same non-null)\n" );
104 }
105 else
106 {
107 if( verbose )
108 mbedtls_printf( " CALLOC(0): passed (distinct non-null)\n" );
109 }
110
111 if( buffer1 == NULL || buffer2 == NULL )
112 {
113 if( verbose )
114 mbedtls_printf( " CALLOC(1): failed (NULL)\n" );
115 ++failures;
116 }
117 else if( buffer1 == buffer2 )
118 {
119 if( verbose )
120 mbedtls_printf( " CALLOC(1): failed (same buffer twice)\n" );
121 ++failures;
122 }
123 else
124 {
125 if( verbose )
126 mbedtls_printf( " CALLOC(1): passed\n" );
127 }
128
129 old_buffer1 = (uintptr_t) buffer1;
130 mbedtls_free( buffer1 );
131 buffer1 = mbedtls_calloc( 1, 1 );
132 if( buffer1 == NULL )
133 {
134 if( verbose )
135 mbedtls_printf( " CALLOC(1 again): failed (NULL)\n" );
136 ++failures;
137 }
138 else
139 {
140 if( verbose )
141 mbedtls_printf( " CALLOC(1 again): passed (%s address)\n",
142 (uintptr_t) old_buffer1 == (uintptr_t) buffer1 ?
143 "same" : "different" );
144 }
145
146 if( verbose )
147 mbedtls_printf( "\n" );
148 mbedtls_free( empty1 );
149 mbedtls_free( empty2 );
150 mbedtls_free( buffer1 );
151 mbedtls_free( buffer2 );
152 return( failures );
153 }
154 #endif /* MBEDTLS_SELF_TEST */
155
test_snprintf(size_t n,const char * ref_buf,int ref_ret)156 static int test_snprintf( size_t n, const char *ref_buf, int ref_ret )
157 {
158 int ret;
159 char buf[10] = "xxxxxxxxx";
160 const char ref[10] = "xxxxxxxxx";
161
162 ret = mbedtls_snprintf( buf, n, "%s", "123" );
163 if( ret < 0 || (size_t) ret >= n )
164 ret = -1;
165
166 if( strncmp( ref_buf, buf, sizeof( buf ) ) != 0 ||
167 ref_ret != ret ||
168 memcmp( buf + n, ref + n, sizeof( buf ) - n ) != 0 )
169 {
170 return( 1 );
171 }
172
173 return( 0 );
174 }
175
run_test_snprintf(void)176 static int run_test_snprintf( void )
177 {
178 return( test_snprintf( 0, "xxxxxxxxx", -1 ) != 0 ||
179 test_snprintf( 1, "", -1 ) != 0 ||
180 test_snprintf( 2, "1", -1 ) != 0 ||
181 test_snprintf( 3, "12", -1 ) != 0 ||
182 test_snprintf( 4, "123", 3 ) != 0 ||
183 test_snprintf( 5, "123", 3 ) != 0 );
184 }
185
186 /*
187 * Check if a seed file is present, and if not create one for the entropy
188 * self-test. If this fails, we attempt the test anyway, so no error is passed
189 * back.
190 */
191 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_ENTROPY_C)
192 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
create_entropy_seed_file(void)193 static void create_entropy_seed_file( void )
194 {
195 int result;
196 size_t output_len = 0;
197 unsigned char seed_value[MBEDTLS_ENTROPY_BLOCK_SIZE];
198
199 /* Attempt to read the entropy seed file. If this fails - attempt to write
200 * to the file to ensure one is present. */
201 result = mbedtls_platform_std_nv_seed_read( seed_value,
202 MBEDTLS_ENTROPY_BLOCK_SIZE );
203 if( 0 == result )
204 return;
205
206 result = mbedtls_platform_entropy_poll( NULL,
207 seed_value,
208 MBEDTLS_ENTROPY_BLOCK_SIZE,
209 &output_len );
210 if( 0 != result )
211 return;
212
213 if( MBEDTLS_ENTROPY_BLOCK_SIZE != output_len )
214 return;
215
216 mbedtls_platform_std_nv_seed_write( seed_value, MBEDTLS_ENTROPY_BLOCK_SIZE );
217 }
218 #endif
219
mbedtls_entropy_self_test_wrapper(int verbose)220 int mbedtls_entropy_self_test_wrapper( int verbose )
221 {
222 #if defined(MBEDTLS_ENTROPY_NV_SEED) && !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
223 create_entropy_seed_file( );
224 #endif
225 return( mbedtls_entropy_self_test( verbose ) );
226 }
227 #endif
228
229 #if defined(MBEDTLS_SELF_TEST)
230 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
mbedtls_memory_buffer_alloc_free_and_self_test(int verbose)231 int mbedtls_memory_buffer_alloc_free_and_self_test( int verbose )
232 {
233 if( verbose != 0 )
234 {
235 #if defined(MBEDTLS_MEMORY_DEBUG)
236 mbedtls_memory_buffer_alloc_status( );
237 #endif
238 }
239 mbedtls_memory_buffer_alloc_free( );
240 return( mbedtls_memory_buffer_alloc_self_test( verbose ) );
241 }
242 #endif
243
244 typedef struct
245 {
246 const char *name;
247 int ( *function )( int );
248 } selftest_t;
249
250 const selftest_t selftests[] =
251 {
252 {"calloc", calloc_self_test},
253 #if defined(MBEDTLS_MD2_C)
254 {"md2", mbedtls_md2_self_test},
255 #endif
256 #if defined(MBEDTLS_MD4_C)
257 {"md4", mbedtls_md4_self_test},
258 #endif
259 #if defined(MBEDTLS_MD5_C)
260 {"md5", mbedtls_md5_self_test},
261 #endif
262 #if defined(MBEDTLS_RIPEMD160_C)
263 {"ripemd160", mbedtls_ripemd160_self_test},
264 #endif
265 #if defined(MBEDTLS_SHA1_C)
266 {"sha1", mbedtls_sha1_self_test},
267 #endif
268 #if defined(MBEDTLS_SHA256_C)
269 {"sha256", mbedtls_sha256_self_test},
270 #endif
271 #if defined(MBEDTLS_SHA512_C)
272 {"sha512", mbedtls_sha512_self_test},
273 #endif
274 #if defined(MBEDTLS_DES_C)
275 {"des", mbedtls_des_self_test},
276 #endif
277 #if defined(MBEDTLS_AES_C)
278 {"aes", mbedtls_aes_self_test},
279 #endif
280 #if defined(MBEDTLS_GCM_C) && defined(MBEDTLS_AES_C)
281 {"gcm", mbedtls_gcm_self_test},
282 #endif
283 #if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_AES_C)
284 {"ccm", mbedtls_ccm_self_test},
285 #endif
286 #if defined(MBEDTLS_NIST_KW_C) && defined(MBEDTLS_AES_C)
287 {"nist_kw", mbedtls_nist_kw_self_test},
288 #endif
289 #if defined(MBEDTLS_CMAC_C)
290 {"cmac", mbedtls_cmac_self_test},
291 #endif
292 #if defined(MBEDTLS_CHACHA20_C)
293 {"chacha20", mbedtls_chacha20_self_test},
294 #endif
295 #if defined(MBEDTLS_POLY1305_C)
296 {"poly1305", mbedtls_poly1305_self_test},
297 #endif
298 #if defined(MBEDTLS_CHACHAPOLY_C)
299 {"chacha20-poly1305", mbedtls_chachapoly_self_test},
300 #endif
301 #if defined(MBEDTLS_BASE64_C)
302 {"base64", mbedtls_base64_self_test},
303 #endif
304 #if defined(MBEDTLS_BIGNUM_C)
305 {"mpi", mbedtls_mpi_self_test},
306 #endif
307 #if defined(MBEDTLS_RSA_C)
308 {"rsa", mbedtls_rsa_self_test},
309 #endif
310 #if defined(MBEDTLS_CAMELLIA_C)
311 {"camellia", mbedtls_camellia_self_test},
312 #endif
313 #if defined(MBEDTLS_ARIA_C)
314 {"aria", mbedtls_aria_self_test},
315 #endif
316 #if defined(MBEDTLS_CTR_DRBG_C)
317 {"ctr_drbg", mbedtls_ctr_drbg_self_test},
318 #endif
319 #if defined(MBEDTLS_HMAC_DRBG_C)
320 {"hmac_drbg", mbedtls_hmac_drbg_self_test},
321 #endif
322 #if defined(MBEDTLS_ECP_C)
323 {"ecp", mbedtls_ecp_self_test},
324 #endif
325 #if defined(MBEDTLS_ECJPAKE_C)
326 {"ecjpake", mbedtls_ecjpake_self_test},
327 #endif
328 #if defined(MBEDTLS_DHM_C)
329 {"dhm", mbedtls_dhm_self_test},
330 #endif
331 #if defined(MBEDTLS_ENTROPY_C)
332 {"entropy", mbedtls_entropy_self_test_wrapper},
333 #endif
334 #if defined(MBEDTLS_PKCS5_C)
335 {"pkcs5", mbedtls_pkcs5_self_test},
336 #endif
337 /* Heap test comes last */
338 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
339 {"memory_buffer_alloc", mbedtls_memory_buffer_alloc_free_and_self_test},
340 #endif
341 {NULL, NULL}
342 };
343 #endif /* MBEDTLS_SELF_TEST */
344
main(int argc,char * argv[])345 int main( int argc, char *argv[] )
346 {
347 #if defined(MBEDTLS_SELF_TEST)
348 const selftest_t *test;
349 #endif /* MBEDTLS_SELF_TEST */
350 char **argp;
351 int v = 1; /* v=1 for verbose mode */
352 int exclude_mode = 0;
353 int suites_tested = 0, suites_failed = 0;
354 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && defined(MBEDTLS_SELF_TEST)
355 unsigned char buf[1000000];
356 #endif
357 void *pointer;
358
359 /*
360 * The C standard doesn't guarantee that all-bits-0 is the representation
361 * of a NULL pointer. We do however use that in our code for initializing
362 * structures, which should work on every modern platform. Let's be sure.
363 */
364 memset( &pointer, 0, sizeof( void * ) );
365 if( pointer != NULL )
366 {
367 mbedtls_printf( "all-bits-zero is not a NULL pointer\n" );
368 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
369 }
370
371 /*
372 * Make sure we have a snprintf that correctly zero-terminates
373 */
374 if( run_test_snprintf() != 0 )
375 {
376 mbedtls_printf( "the snprintf implementation is broken\n" );
377 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
378 }
379
380 for( argp = argv + ( argc >= 1 ? 1 : argc ); *argp != NULL; ++argp )
381 {
382 if( strcmp( *argp, "--quiet" ) == 0 ||
383 strcmp( *argp, "-q" ) == 0 )
384 {
385 v = 0;
386 }
387 else if( strcmp( *argp, "--exclude" ) == 0 ||
388 strcmp( *argp, "-x" ) == 0 )
389 {
390 exclude_mode = 1;
391 }
392 else
393 break;
394 }
395
396 if( v != 0 )
397 mbedtls_printf( "\n" );
398
399 #if defined(MBEDTLS_SELF_TEST)
400
401 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
402 mbedtls_memory_buffer_alloc_init( buf, sizeof(buf) );
403 #endif
404
405 if( *argp != NULL && exclude_mode == 0 )
406 {
407 /* Run the specified tests */
408 for( ; *argp != NULL; argp++ )
409 {
410 for( test = selftests; test->name != NULL; test++ )
411 {
412 if( !strcmp( *argp, test->name ) )
413 {
414 if( test->function( v ) != 0 )
415 {
416 suites_failed++;
417 }
418 suites_tested++;
419 break;
420 }
421 }
422 if( test->name == NULL )
423 {
424 mbedtls_printf( " Test suite %s not available -> failed\n\n", *argp );
425 suites_failed++;
426 }
427 }
428 }
429 else
430 {
431 /* Run all the tests except excluded ones */
432 for( test = selftests; test->name != NULL; test++ )
433 {
434 if( exclude_mode )
435 {
436 char **excluded;
437 for( excluded = argp; *excluded != NULL; ++excluded )
438 {
439 if( !strcmp( *excluded, test->name ) )
440 break;
441 }
442 if( *excluded )
443 {
444 if( v )
445 mbedtls_printf( " Skip: %s\n", test->name );
446 continue;
447 }
448 }
449 if( test->function( v ) != 0 )
450 {
451 suites_failed++;
452 }
453 suites_tested++;
454 }
455 }
456
457 #else
458 (void) exclude_mode;
459 mbedtls_printf( " MBEDTLS_SELF_TEST not defined.\n" );
460 #endif
461
462 if( v != 0 )
463 {
464 mbedtls_printf( " Executed %d test suites\n\n", suites_tested );
465
466 if( suites_failed > 0)
467 {
468 mbedtls_printf( " [ %d tests FAIL ]\n\n", suites_failed );
469 }
470 else
471 {
472 mbedtls_printf( " [ All tests PASS ]\n\n" );
473 }
474 #if defined(_WIN32)
475 mbedtls_printf( " Press Enter to exit this program.\n" );
476 fflush( stdout ); getchar();
477 #endif
478 }
479
480 if( suites_failed > 0)
481 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
482
483 mbedtls_exit( MBEDTLS_EXIT_SUCCESS );
484 }
485