hmac_drbg.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * HMAC_DRBG implementation (NIST SP 800-90)
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * The NIST SP 800-90A DRBGs are described in the following publication.
  9. * http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
  10. * References below are based on rev. 1 (January 2012).
  11. */
  12. #include "common.h"
  13. #if defined(MBEDTLS_HMAC_DRBG_C)
  14. #include "mbedtls/hmac_drbg.h"
  15. #include "mbedtls/platform_util.h"
  16. #include "mbedtls/error.h"
  17. #include <string.h>
  18. #if defined(MBEDTLS_FS_IO)
  19. #include <stdio.h>
  20. #endif
  21. #include "mbedtls/platform.h"
  22. /*
  23. * HMAC_DRBG context initialization
  24. */
  25. void mbedtls_hmac_drbg_init(mbedtls_hmac_drbg_context *ctx)
  26. {
  27. memset(ctx, 0, sizeof(mbedtls_hmac_drbg_context));
  28. ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL;
  29. }
  30. /*
  31. * HMAC_DRBG update, using optional additional data (10.1.2.2)
  32. */
  33. int mbedtls_hmac_drbg_update_ret(mbedtls_hmac_drbg_context *ctx,
  34. const unsigned char *additional,
  35. size_t add_len)
  36. {
  37. size_t md_len = mbedtls_md_get_size(ctx->md_ctx.md_info);
  38. unsigned char rounds = (additional != NULL && add_len != 0) ? 2 : 1;
  39. unsigned char sep[1];
  40. unsigned char K[MBEDTLS_MD_MAX_SIZE];
  41. int ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
  42. for (sep[0] = 0; sep[0] < rounds; sep[0]++) {
  43. /* Step 1 or 4 */
  44. if ((ret = mbedtls_md_hmac_reset(&ctx->md_ctx)) != 0) {
  45. goto exit;
  46. }
  47. if ((ret = mbedtls_md_hmac_update(&ctx->md_ctx,
  48. ctx->V, md_len)) != 0) {
  49. goto exit;
  50. }
  51. if ((ret = mbedtls_md_hmac_update(&ctx->md_ctx,
  52. sep, 1)) != 0) {
  53. goto exit;
  54. }
  55. if (rounds == 2) {
  56. if ((ret = mbedtls_md_hmac_update(&ctx->md_ctx,
  57. additional, add_len)) != 0) {
  58. goto exit;
  59. }
  60. }
  61. if ((ret = mbedtls_md_hmac_finish(&ctx->md_ctx, K)) != 0) {
  62. goto exit;
  63. }
  64. /* Step 2 or 5 */
  65. if ((ret = mbedtls_md_hmac_starts(&ctx->md_ctx, K, md_len)) != 0) {
  66. goto exit;
  67. }
  68. if ((ret = mbedtls_md_hmac_update(&ctx->md_ctx,
  69. ctx->V, md_len)) != 0) {
  70. goto exit;
  71. }
  72. if ((ret = mbedtls_md_hmac_finish(&ctx->md_ctx, ctx->V)) != 0) {
  73. goto exit;
  74. }
  75. }
  76. exit:
  77. mbedtls_platform_zeroize(K, sizeof(K));
  78. return ret;
  79. }
  80. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  81. void mbedtls_hmac_drbg_update(mbedtls_hmac_drbg_context *ctx,
  82. const unsigned char *additional,
  83. size_t add_len)
  84. {
  85. (void) mbedtls_hmac_drbg_update_ret(ctx, additional, add_len);
  86. }
  87. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  88. /*
  89. * Simplified HMAC_DRBG initialisation (for use with deterministic ECDSA)
  90. */
  91. int mbedtls_hmac_drbg_seed_buf(mbedtls_hmac_drbg_context *ctx,
  92. const mbedtls_md_info_t *md_info,
  93. const unsigned char *data, size_t data_len)
  94. {
  95. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  96. if ((ret = mbedtls_md_setup(&ctx->md_ctx, md_info, 1)) != 0) {
  97. return ret;
  98. }
  99. #if defined(MBEDTLS_THREADING_C)
  100. mbedtls_mutex_init(&ctx->mutex);
  101. #endif
  102. /*
  103. * Set initial working state.
  104. * Use the V memory location, which is currently all 0, to initialize the
  105. * MD context with an all-zero key. Then set V to its initial value.
  106. */
  107. if ((ret = mbedtls_md_hmac_starts(&ctx->md_ctx, ctx->V,
  108. mbedtls_md_get_size(md_info))) != 0) {
  109. return ret;
  110. }
  111. memset(ctx->V, 0x01, mbedtls_md_get_size(md_info));
  112. if ((ret = mbedtls_hmac_drbg_update_ret(ctx, data, data_len)) != 0) {
  113. return ret;
  114. }
  115. return 0;
  116. }
  117. /*
  118. * Internal function used both for seeding and reseeding the DRBG.
  119. * Comments starting with arabic numbers refer to section 10.1.2.4
  120. * of SP800-90A, while roman numbers refer to section 9.2.
  121. */
  122. static int hmac_drbg_reseed_core(mbedtls_hmac_drbg_context *ctx,
  123. const unsigned char *additional, size_t len,
  124. int use_nonce)
  125. {
  126. unsigned char seed[MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT];
  127. size_t seedlen = 0;
  128. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  129. {
  130. size_t total_entropy_len;
  131. if (use_nonce == 0) {
  132. total_entropy_len = ctx->entropy_len;
  133. } else {
  134. total_entropy_len = ctx->entropy_len * 3 / 2;
  135. }
  136. /* III. Check input length */
  137. if (len > MBEDTLS_HMAC_DRBG_MAX_INPUT ||
  138. total_entropy_len + len > MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT) {
  139. return MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG;
  140. }
  141. }
  142. memset(seed, 0, MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT);
  143. /* IV. Gather entropy_len bytes of entropy for the seed */
  144. if ((ret = ctx->f_entropy(ctx->p_entropy,
  145. seed, ctx->entropy_len)) != 0) {
  146. return MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED;
  147. }
  148. seedlen += ctx->entropy_len;
  149. /* For initial seeding, allow adding of nonce generated
  150. * from the entropy source. See Sect 8.6.7 in SP800-90A. */
  151. if (use_nonce) {
  152. /* Note: We don't merge the two calls to f_entropy() in order
  153. * to avoid requesting too much entropy from f_entropy()
  154. * at once. Specifically, if the underlying digest is not
  155. * SHA-1, 3 / 2 * entropy_len is at least 36 Bytes, which
  156. * is larger than the maximum of 32 Bytes that our own
  157. * entropy source implementation can emit in a single
  158. * call in configurations disabling SHA-512. */
  159. if ((ret = ctx->f_entropy(ctx->p_entropy,
  160. seed + seedlen,
  161. ctx->entropy_len / 2)) != 0) {
  162. return MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED;
  163. }
  164. seedlen += ctx->entropy_len / 2;
  165. }
  166. /* 1. Concatenate entropy and additional data if any */
  167. if (additional != NULL && len != 0) {
  168. memcpy(seed + seedlen, additional, len);
  169. seedlen += len;
  170. }
  171. /* 2. Update state */
  172. if ((ret = mbedtls_hmac_drbg_update_ret(ctx, seed, seedlen)) != 0) {
  173. goto exit;
  174. }
  175. /* 3. Reset reseed_counter */
  176. ctx->reseed_counter = 1;
  177. exit:
  178. /* 4. Done */
  179. mbedtls_platform_zeroize(seed, seedlen);
  180. return ret;
  181. }
  182. /*
  183. * HMAC_DRBG reseeding: 10.1.2.4 + 9.2
  184. */
  185. int mbedtls_hmac_drbg_reseed(mbedtls_hmac_drbg_context *ctx,
  186. const unsigned char *additional, size_t len)
  187. {
  188. return hmac_drbg_reseed_core(ctx, additional, len, 0);
  189. }
  190. /*
  191. * HMAC_DRBG initialisation (10.1.2.3 + 9.1)
  192. *
  193. * The nonce is not passed as a separate parameter but extracted
  194. * from the entropy source as suggested in 8.6.7.
  195. */
  196. int mbedtls_hmac_drbg_seed(mbedtls_hmac_drbg_context *ctx,
  197. const mbedtls_md_info_t *md_info,
  198. int (*f_entropy)(void *, unsigned char *, size_t),
  199. void *p_entropy,
  200. const unsigned char *custom,
  201. size_t len)
  202. {
  203. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  204. size_t md_size;
  205. if ((ret = mbedtls_md_setup(&ctx->md_ctx, md_info, 1)) != 0) {
  206. return ret;
  207. }
  208. /* The mutex is initialized iff the md context is set up. */
  209. #if defined(MBEDTLS_THREADING_C)
  210. mbedtls_mutex_init(&ctx->mutex);
  211. #endif
  212. md_size = mbedtls_md_get_size(md_info);
  213. /*
  214. * Set initial working state.
  215. * Use the V memory location, which is currently all 0, to initialize the
  216. * MD context with an all-zero key. Then set V to its initial value.
  217. */
  218. if ((ret = mbedtls_md_hmac_starts(&ctx->md_ctx, ctx->V, md_size)) != 0) {
  219. return ret;
  220. }
  221. memset(ctx->V, 0x01, md_size);
  222. ctx->f_entropy = f_entropy;
  223. ctx->p_entropy = p_entropy;
  224. if (ctx->entropy_len == 0) {
  225. /*
  226. * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
  227. * each hash function, then according to SP800-90A rev1 10.1 table 2,
  228. * min_entropy_len (in bits) is security_strength.
  229. *
  230. * (This also matches the sizes used in the NIST test vectors.)
  231. */
  232. ctx->entropy_len = md_size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
  233. md_size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
  234. 32; /* better (256+) -> 256 bits */
  235. }
  236. if ((ret = hmac_drbg_reseed_core(ctx, custom, len,
  237. 1 /* add nonce */)) != 0) {
  238. return ret;
  239. }
  240. return 0;
  241. }
  242. /*
  243. * Set prediction resistance
  244. */
  245. void mbedtls_hmac_drbg_set_prediction_resistance(mbedtls_hmac_drbg_context *ctx,
  246. int resistance)
  247. {
  248. ctx->prediction_resistance = resistance;
  249. }
  250. /*
  251. * Set entropy length grabbed for seeding
  252. */
  253. void mbedtls_hmac_drbg_set_entropy_len(mbedtls_hmac_drbg_context *ctx, size_t len)
  254. {
  255. ctx->entropy_len = len;
  256. }
  257. /*
  258. * Set reseed interval
  259. */
  260. void mbedtls_hmac_drbg_set_reseed_interval(mbedtls_hmac_drbg_context *ctx, int interval)
  261. {
  262. ctx->reseed_interval = interval;
  263. }
  264. /*
  265. * HMAC_DRBG random function with optional additional data:
  266. * 10.1.2.5 (arabic) + 9.3 (Roman)
  267. */
  268. int mbedtls_hmac_drbg_random_with_add(void *p_rng,
  269. unsigned char *output, size_t out_len,
  270. const unsigned char *additional, size_t add_len)
  271. {
  272. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  273. mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  274. size_t md_len = mbedtls_md_get_size(ctx->md_ctx.md_info);
  275. size_t left = out_len;
  276. unsigned char *out = output;
  277. /* II. Check request length */
  278. if (out_len > MBEDTLS_HMAC_DRBG_MAX_REQUEST) {
  279. return MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG;
  280. }
  281. /* III. Check input length */
  282. if (add_len > MBEDTLS_HMAC_DRBG_MAX_INPUT) {
  283. return MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG;
  284. }
  285. /* 1. (aka VII and IX) Check reseed counter and PR */
  286. if (ctx->f_entropy != NULL && /* For no-reseeding instances */
  287. (ctx->prediction_resistance == MBEDTLS_HMAC_DRBG_PR_ON ||
  288. ctx->reseed_counter > ctx->reseed_interval)) {
  289. if ((ret = mbedtls_hmac_drbg_reseed(ctx, additional, add_len)) != 0) {
  290. return ret;
  291. }
  292. add_len = 0; /* VII.4 */
  293. }
  294. /* 2. Use additional data if any */
  295. if (additional != NULL && add_len != 0) {
  296. if ((ret = mbedtls_hmac_drbg_update_ret(ctx,
  297. additional, add_len)) != 0) {
  298. goto exit;
  299. }
  300. }
  301. /* 3, 4, 5. Generate bytes */
  302. while (left != 0) {
  303. size_t use_len = left > md_len ? md_len : left;
  304. if ((ret = mbedtls_md_hmac_reset(&ctx->md_ctx)) != 0) {
  305. goto exit;
  306. }
  307. if ((ret = mbedtls_md_hmac_update(&ctx->md_ctx,
  308. ctx->V, md_len)) != 0) {
  309. goto exit;
  310. }
  311. if ((ret = mbedtls_md_hmac_finish(&ctx->md_ctx, ctx->V)) != 0) {
  312. goto exit;
  313. }
  314. memcpy(out, ctx->V, use_len);
  315. out += use_len;
  316. left -= use_len;
  317. }
  318. /* 6. Update */
  319. if ((ret = mbedtls_hmac_drbg_update_ret(ctx,
  320. additional, add_len)) != 0) {
  321. goto exit;
  322. }
  323. /* 7. Update reseed counter */
  324. ctx->reseed_counter++;
  325. exit:
  326. /* 8. Done */
  327. return ret;
  328. }
  329. /*
  330. * HMAC_DRBG random function
  331. */
  332. int mbedtls_hmac_drbg_random(void *p_rng, unsigned char *output, size_t out_len)
  333. {
  334. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  335. mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  336. #if defined(MBEDTLS_THREADING_C)
  337. if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
  338. return ret;
  339. }
  340. #endif
  341. ret = mbedtls_hmac_drbg_random_with_add(ctx, output, out_len, NULL, 0);
  342. #if defined(MBEDTLS_THREADING_C)
  343. if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
  344. return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  345. }
  346. #endif
  347. return ret;
  348. }
  349. /*
  350. * This function resets HMAC_DRBG context to the state immediately
  351. * after initial call of mbedtls_hmac_drbg_init().
  352. */
  353. void mbedtls_hmac_drbg_free(mbedtls_hmac_drbg_context *ctx)
  354. {
  355. if (ctx == NULL) {
  356. return;
  357. }
  358. #if defined(MBEDTLS_THREADING_C)
  359. /* The mutex is initialized iff the md context is set up. */
  360. if (ctx->md_ctx.md_info != NULL) {
  361. mbedtls_mutex_free(&ctx->mutex);
  362. }
  363. #endif
  364. mbedtls_md_free(&ctx->md_ctx);
  365. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_hmac_drbg_context));
  366. ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL;
  367. }
  368. #if defined(MBEDTLS_FS_IO)
  369. int mbedtls_hmac_drbg_write_seed_file(mbedtls_hmac_drbg_context *ctx, const char *path)
  370. {
  371. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  372. FILE *f;
  373. unsigned char buf[MBEDTLS_HMAC_DRBG_MAX_INPUT];
  374. if ((f = fopen(path, "wb")) == NULL) {
  375. return MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  376. }
  377. if ((ret = mbedtls_hmac_drbg_random(ctx, buf, sizeof(buf))) != 0) {
  378. goto exit;
  379. }
  380. if (fwrite(buf, 1, sizeof(buf), f) != sizeof(buf)) {
  381. ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  382. goto exit;
  383. }
  384. ret = 0;
  385. exit:
  386. fclose(f);
  387. mbedtls_platform_zeroize(buf, sizeof(buf));
  388. return ret;
  389. }
  390. int mbedtls_hmac_drbg_update_seed_file(mbedtls_hmac_drbg_context *ctx, const char *path)
  391. {
  392. int ret = 0;
  393. FILE *f = NULL;
  394. size_t n;
  395. unsigned char buf[MBEDTLS_HMAC_DRBG_MAX_INPUT];
  396. unsigned char c;
  397. if ((f = fopen(path, "rb")) == NULL) {
  398. return MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  399. }
  400. n = fread(buf, 1, sizeof(buf), f);
  401. if (fread(&c, 1, 1, f) != 0) {
  402. ret = MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG;
  403. goto exit;
  404. }
  405. if (n == 0 || ferror(f)) {
  406. ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  407. goto exit;
  408. }
  409. fclose(f);
  410. f = NULL;
  411. ret = mbedtls_hmac_drbg_update_ret(ctx, buf, n);
  412. exit:
  413. mbedtls_platform_zeroize(buf, sizeof(buf));
  414. if (f != NULL) {
  415. fclose(f);
  416. }
  417. if (ret != 0) {
  418. return ret;
  419. }
  420. return mbedtls_hmac_drbg_write_seed_file(ctx, path);
  421. }
  422. #endif /* MBEDTLS_FS_IO */
  423. #if defined(MBEDTLS_SELF_TEST)
  424. #if !defined(MBEDTLS_SHA1_C)
  425. /* Dummy checkup routine */
  426. int mbedtls_hmac_drbg_self_test(int verbose)
  427. {
  428. (void) verbose;
  429. return 0;
  430. }
  431. #else
  432. #define OUTPUT_LEN 80
  433. /* From a NIST PR=true test vector */
  434. static const unsigned char entropy_pr[] = {
  435. 0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f,
  436. 0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11,
  437. 0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42,
  438. 0x17, 0x60, 0x99, 0xd4, 0x5e, 0x13, 0x97, 0xdc, 0x40, 0x4d, 0x86, 0xa3,
  439. 0x7b, 0xf5, 0x59, 0x54, 0x75, 0x69, 0x51, 0xe4
  440. };
  441. static const unsigned char result_pr[OUTPUT_LEN] = {
  442. 0x9a, 0x00, 0xa2, 0xd0, 0x0e, 0xd5, 0x9b, 0xfe, 0x31, 0xec, 0xb1, 0x39,
  443. 0x9b, 0x60, 0x81, 0x48, 0xd1, 0x96, 0x9d, 0x25, 0x0d, 0x3c, 0x1e, 0x94,
  444. 0x10, 0x10, 0x98, 0x12, 0x93, 0x25, 0xca, 0xb8, 0xfc, 0xcc, 0x2d, 0x54,
  445. 0x73, 0x19, 0x70, 0xc0, 0x10, 0x7a, 0xa4, 0x89, 0x25, 0x19, 0x95, 0x5e,
  446. 0x4b, 0xc6, 0x00, 0x1d, 0x7f, 0x4e, 0x6a, 0x2b, 0xf8, 0xa3, 0x01, 0xab,
  447. 0x46, 0x05, 0x5c, 0x09, 0xa6, 0x71, 0x88, 0xf1, 0xa7, 0x40, 0xee, 0xf3,
  448. 0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44
  449. };
  450. /* From a NIST PR=false test vector */
  451. static const unsigned char entropy_nopr[] = {
  452. 0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66,
  453. 0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8,
  454. 0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3,
  455. 0xe9, 0x9d, 0xfe, 0xdf
  456. };
  457. static const unsigned char result_nopr[OUTPUT_LEN] = {
  458. 0xc6, 0xa1, 0x6a, 0xb8, 0xd4, 0x20, 0x70, 0x6f, 0x0f, 0x34, 0xab, 0x7f,
  459. 0xec, 0x5a, 0xdc, 0xa9, 0xd8, 0xca, 0x3a, 0x13, 0x3e, 0x15, 0x9c, 0xa6,
  460. 0xac, 0x43, 0xc6, 0xf8, 0xa2, 0xbe, 0x22, 0x83, 0x4a, 0x4c, 0x0a, 0x0a,
  461. 0xff, 0xb1, 0x0d, 0x71, 0x94, 0xf1, 0xc1, 0xa5, 0xcf, 0x73, 0x22, 0xec,
  462. 0x1a, 0xe0, 0x96, 0x4e, 0xd4, 0xbf, 0x12, 0x27, 0x46, 0xe0, 0x87, 0xfd,
  463. 0xb5, 0xb3, 0xe9, 0x1b, 0x34, 0x93, 0xd5, 0xbb, 0x98, 0xfa, 0xed, 0x49,
  464. 0xe8, 0x5f, 0x13, 0x0f, 0xc8, 0xa4, 0x59, 0xb7
  465. };
  466. /* "Entropy" from buffer */
  467. static size_t test_offset;
  468. static int hmac_drbg_self_test_entropy(void *data,
  469. unsigned char *buf, size_t len)
  470. {
  471. const unsigned char *p = data;
  472. memcpy(buf, p + test_offset, len);
  473. test_offset += len;
  474. return 0;
  475. }
  476. #define CHK(c) if ((c) != 0) \
  477. { \
  478. if (verbose != 0) \
  479. mbedtls_printf("failed\n"); \
  480. return 1; \
  481. }
  482. /*
  483. * Checkup routine for HMAC_DRBG with SHA-1
  484. */
  485. int mbedtls_hmac_drbg_self_test(int verbose)
  486. {
  487. mbedtls_hmac_drbg_context ctx;
  488. unsigned char buf[OUTPUT_LEN];
  489. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
  490. mbedtls_hmac_drbg_init(&ctx);
  491. /*
  492. * PR = True
  493. */
  494. if (verbose != 0) {
  495. mbedtls_printf(" HMAC_DRBG (PR = True) : ");
  496. }
  497. test_offset = 0;
  498. CHK(mbedtls_hmac_drbg_seed(&ctx, md_info,
  499. hmac_drbg_self_test_entropy, (void *) entropy_pr,
  500. NULL, 0));
  501. mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
  502. CHK(mbedtls_hmac_drbg_random(&ctx, buf, OUTPUT_LEN));
  503. CHK(mbedtls_hmac_drbg_random(&ctx, buf, OUTPUT_LEN));
  504. CHK(memcmp(buf, result_pr, OUTPUT_LEN));
  505. mbedtls_hmac_drbg_free(&ctx);
  506. mbedtls_hmac_drbg_free(&ctx);
  507. if (verbose != 0) {
  508. mbedtls_printf("passed\n");
  509. }
  510. /*
  511. * PR = False
  512. */
  513. if (verbose != 0) {
  514. mbedtls_printf(" HMAC_DRBG (PR = False) : ");
  515. }
  516. mbedtls_hmac_drbg_init(&ctx);
  517. test_offset = 0;
  518. CHK(mbedtls_hmac_drbg_seed(&ctx, md_info,
  519. hmac_drbg_self_test_entropy, (void *) entropy_nopr,
  520. NULL, 0));
  521. CHK(mbedtls_hmac_drbg_reseed(&ctx, NULL, 0));
  522. CHK(mbedtls_hmac_drbg_random(&ctx, buf, OUTPUT_LEN));
  523. CHK(mbedtls_hmac_drbg_random(&ctx, buf, OUTPUT_LEN));
  524. CHK(memcmp(buf, result_nopr, OUTPUT_LEN));
  525. mbedtls_hmac_drbg_free(&ctx);
  526. mbedtls_hmac_drbg_free(&ctx);
  527. if (verbose != 0) {
  528. mbedtls_printf("passed\n");
  529. }
  530. if (verbose != 0) {
  531. mbedtls_printf("\n");
  532. }
  533. return 0;
  534. }
  535. #endif /* MBEDTLS_SHA1_C */
  536. #endif /* MBEDTLS_SELF_TEST */
  537. #endif /* MBEDTLS_HMAC_DRBG_C */