selftest.c 14 KB

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