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