blob: 830155a32b00f1c84b9f7eaf6c26d678336cd6e8 [file] [log] [blame]
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +01001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/hmac_drbg.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01003#include "string.h"
Rich Evans00ab4702015-02-06 13:43:58 +00004
Gilles Peskine449bd832023-01-11 14:50:10 +01005typedef struct {
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +01006 unsigned char *p;
7 size_t len;
8} entropy_ctx;
9
Gilles Peskine449bd832023-01-11 14:50:10 +010010static int mbedtls_test_entropy_func(void *data, unsigned char *buf, size_t len)
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +010011{
12 entropy_ctx *ctx = (entropy_ctx *) data;
13
Gilles Peskine449bd832023-01-11 14:50:10 +010014 if (len > ctx->len) {
15 return -1;
16 }
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +010017
Gilles Peskine449bd832023-01-11 14:50:10 +010018 memcpy(buf, ctx->p, len);
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +010019
20 ctx->p += len;
21 ctx->len -= len;
22
Gilles Peskine449bd832023-01-11 14:50:10 +010023 return 0;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +010024}
25/* END_HEADER */
26
27/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 * depends_on:MBEDTLS_HMAC_DRBG_C
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +010029 * END_DEPENDENCIES
30 */
31
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010032/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010033void hmac_drbg_entropy_usage(int md_alg)
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010034{
35 unsigned char out[16];
36 unsigned char buf[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 const mbedtls_md_info_t *md_info;
38 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010039 entropy_ctx entropy;
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020040 size_t i, reps = 10;
41 size_t default_entropy_len;
42 size_t expected_consumed_entropy = 0;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010043
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_hmac_drbg_init(&ctx);
45 memset(buf, 0, sizeof(buf));
46 memset(out, 0, sizeof(out));
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010047
Gilles Peskine449bd832023-01-11 14:50:10 +010048 entropy.len = sizeof(buf);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010049 entropy.p = buf;
50
Gilles Peskine449bd832023-01-11 14:50:10 +010051 md_info = mbedtls_md_info_from_type(md_alg);
52 TEST_ASSERT(md_info != NULL);
53 if (mbedtls_md_get_size(md_info) <= 20) {
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020054 default_entropy_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +010055 } else if (mbedtls_md_get_size(md_info) <= 28) {
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020056 default_entropy_len = 24;
Gilles Peskine449bd832023-01-11 14:50:10 +010057 } else {
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020058 default_entropy_len = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +010059 }
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010060
Gavin Acquroff6aceb512020-03-01 17:06:11 -080061 /* Set reseed interval before seed */
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_hmac_drbg_set_reseed_interval(&ctx, 2 * reps);
Gavin Acquroff6aceb512020-03-01 17:06:11 -080063
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010064 /* Init must use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +010065 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &entropy,
66 NULL, 0) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020067 /* default_entropy_len of entropy, plus half as much for the nonce */
68 expected_consumed_entropy += default_entropy_len * 3 / 2;
Gilles Peskine449bd832023-01-11 14:50:10 +010069 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010070
Gavin Acquroff6aceb512020-03-01 17:06:11 -080071 /* By default, PR is off, and reseed interval was set to
72 * 2 * reps so the next few calls should not use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +010073 for (i = 0; i < reps; i++) {
74 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out) - 4) == 0);
75 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, out, sizeof(out) - 4,
76 buf, 16) == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010077 }
Gilles Peskine449bd832023-01-11 14:50:10 +010078 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010079
80 /* While at it, make sure we didn't write past the requested length */
Gilles Peskine449bd832023-01-11 14:50:10 +010081 TEST_ASSERT(out[sizeof(out) - 4] == 0);
82 TEST_ASSERT(out[sizeof(out) - 3] == 0);
83 TEST_ASSERT(out[sizeof(out) - 2] == 0);
84 TEST_ASSERT(out[sizeof(out) - 1] == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010085
Gavin Acquroff6aceb512020-03-01 17:06:11 -080086 /* There have been 2 * reps calls to random. The next call should reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +010087 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020088 expected_consumed_entropy += default_entropy_len;
Gilles Peskine449bd832023-01-11 14:50:10 +010089 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010090
Gavin Acquroff6aceb512020-03-01 17:06:11 -080091 /* Set reseed interval after seed */
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_hmac_drbg_set_reseed_interval(&ctx, 4 * reps + 1);
Gavin Acquroff6aceb512020-03-01 17:06:11 -080093
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010094 /* The new few calls should not reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +010095 for (i = 0; i < (2 * reps); i++) {
96 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
97 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, out, sizeof(out),
98 buf, 16) == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010099 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100101
102 /* Now enable PR, so the next few calls should all reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
104 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200105 expected_consumed_entropy += default_entropy_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100107
108 /* Finally, check setting entropy_len */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_hmac_drbg_set_entropy_len(&ctx, 42);
110 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200111 expected_consumed_entropy += 42;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_hmac_drbg_set_entropy_len(&ctx, 13);
115 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200116 expected_consumed_entropy += 13;
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200118
119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100121}
122/* END_CASE */
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125void hmac_drbg_seed_file(int md_alg, char *path, int ret)
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 const mbedtls_md_info_t *md_info;
128 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 md_info = mbedtls_md_info_from_type(md_alg);
133 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info,
136 mbedtls_test_rnd_std_rand, NULL,
137 NULL, 0) == 0);
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 TEST_ASSERT(mbedtls_hmac_drbg_write_seed_file(&ctx, path) == ret);
140 TEST_ASSERT(mbedtls_hmac_drbg_update_seed_file(&ctx, path) == ret);
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100141
Paul Bakkerbd51b262014-07-10 15:26:12 +0200142exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100144}
145/* END_CASE */
146
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100147/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148void hmac_drbg_buf(int md_alg)
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100149{
150 unsigned char out[16];
151 unsigned char buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 const mbedtls_md_info_t *md_info;
153 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100154 size_t i;
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_hmac_drbg_init(&ctx);
157 memset(buf, 0, sizeof(buf));
158 memset(out, 0, sizeof(out));
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 md_info = mbedtls_md_info_from_type(md_alg);
161 TEST_ASSERT(md_info != NULL);
162 TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info, buf, sizeof(buf)) == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100163
164 /* Make sure it never tries to reseed (would segfault otherwise) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_hmac_drbg_set_reseed_interval(&ctx, 3);
166 mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 for (i = 0; i < 30; i++) {
169 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
170 }
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100171
Paul Bakkerbd51b262014-07-10 15:26:12 +0200172exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100174}
175/* END_CASE */
176
177/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178void hmac_drbg_no_reseed(int md_alg, data_t *entropy,
179 data_t *custom, data_t *add1,
180 data_t *add2, data_t *output)
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100181{
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100182 unsigned char data[1024];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100183 unsigned char my_output[512];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100184 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 const mbedtls_md_info_t *md_info;
186 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100189
Azim Khand30ca132017-06-09 04:32:58 +0100190 p_entropy.p = entropy->x;
191 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 md_info = mbedtls_md_info_from_type(md_alg);
194 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100195
196 /* Test the simplified buffer-based variant */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 memcpy(data, entropy->x, p_entropy.len);
198 memcpy(data + p_entropy.len, custom->x, custom->len);
199 TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info,
200 data, p_entropy.len + custom->len) == 0);
201 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
202 add1->x, add1->len) == 0);
203 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
204 add2->x, add2->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200205
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800206 /* Reset context for second run */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100210
211 /* And now the normal entropy-based variant */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
213 custom->x, custom->len) == 0);
214 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
215 add1->x, add1->len) == 0);
216 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
217 add2->x, add2->len) == 0);
218 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100219
Paul Bakkerbd51b262014-07-10 15:26:12 +0200220exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100222}
223/* END_CASE */
224
225/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226void hmac_drbg_nopr(int md_alg, data_t *entropy, data_t *custom,
227 data_t *add1, data_t *add2, data_t *add3,
228 data_t *output)
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100229{
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100230 unsigned char my_output[512];
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100231 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 const mbedtls_md_info_t *md_info;
233 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100236
Azim Khand30ca132017-06-09 04:32:58 +0100237 p_entropy.p = entropy->x;
238 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 md_info = mbedtls_md_info_from_type(md_alg);
241 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
244 custom->x, custom->len) == 0);
245 TEST_ASSERT(mbedtls_hmac_drbg_reseed(&ctx, add1->x, add1->len) == 0);
246 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
247 add2->x, add2->len) == 0);
248 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
249 add3->x, add3->len) == 0);
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100252
Paul Bakkerbd51b262014-07-10 15:26:12 +0200253exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100255}
256/* END_CASE */
257
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100258/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259void hmac_drbg_pr(int md_alg, data_t *entropy, data_t *custom,
260 data_t *add1, data_t *add2, data_t *output)
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100261{
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100262 unsigned char my_output[512];
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100263 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 const mbedtls_md_info_t *md_info;
265 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100268
Azim Khand30ca132017-06-09 04:32:58 +0100269 p_entropy.p = entropy->x;
270 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 md_info = mbedtls_md_info_from_type(md_alg);
273 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
276 custom->x, custom->len) == 0);
277 mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
278 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
279 add1->x, add1->len) == 0);
280 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
281 add2->x, add2->len) == 0);
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200284
285exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100287}
288/* END_CASE */
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291void hmac_drbg_selftest()
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100292{
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 TEST_ASSERT(mbedtls_hmac_drbg_self_test(1) == 0);
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100294}
295/* END_CASE */