hmac_drbg.c 19 KB

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