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