selftest.c 14 KB

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