selftest.c 17 KB

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