blob: 0a50c6c5349794fd40d65446fdf1de6014014444 [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
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +010044 MD_PSA_INIT();
45
Gilles Peskine449bd832023-01-11 14:50:10 +010046 mbedtls_hmac_drbg_init(&ctx);
47 memset(buf, 0, sizeof(buf));
48 memset(out, 0, sizeof(out));
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 entropy.len = sizeof(buf);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010051 entropy.p = buf;
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053 md_info = mbedtls_md_info_from_type(md_alg);
54 TEST_ASSERT(md_info != NULL);
55 if (mbedtls_md_get_size(md_info) <= 20) {
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020056 default_entropy_len = 16;
Gilles Peskine449bd832023-01-11 14:50:10 +010057 } else if (mbedtls_md_get_size(md_info) <= 28) {
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020058 default_entropy_len = 24;
Gilles Peskine449bd832023-01-11 14:50:10 +010059 } else {
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020060 default_entropy_len = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +010061 }
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010062
Gavin Acquroff6aceb512020-03-01 17:06:11 -080063 /* Set reseed interval before seed */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 mbedtls_hmac_drbg_set_reseed_interval(&ctx, 2 * reps);
Gavin Acquroff6aceb512020-03-01 17:06:11 -080065
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010066 /* Init must use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +010067 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &entropy,
68 NULL, 0) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020069 /* default_entropy_len of entropy, plus half as much for the nonce */
70 expected_consumed_entropy += default_entropy_len * 3 / 2;
Gilles Peskine449bd832023-01-11 14:50:10 +010071 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010072
Gavin Acquroff6aceb512020-03-01 17:06:11 -080073 /* By default, PR is off, and reseed interval was set to
74 * 2 * reps so the next few calls should not use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +010075 for (i = 0; i < reps; i++) {
76 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out) - 4) == 0);
77 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, out, sizeof(out) - 4,
78 buf, 16) == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010079 }
Gilles Peskine449bd832023-01-11 14:50:10 +010080 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010081
82 /* While at it, make sure we didn't write past the requested length */
Gilles Peskine449bd832023-01-11 14:50:10 +010083 TEST_ASSERT(out[sizeof(out) - 4] == 0);
84 TEST_ASSERT(out[sizeof(out) - 3] == 0);
85 TEST_ASSERT(out[sizeof(out) - 2] == 0);
86 TEST_ASSERT(out[sizeof(out) - 1] == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010087
Gavin Acquroff6aceb512020-03-01 17:06:11 -080088 /* There have been 2 * reps calls to random. The next call should reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +010089 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +020090 expected_consumed_entropy += default_entropy_len;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010092
Gavin Acquroff6aceb512020-03-01 17:06:11 -080093 /* Set reseed interval after seed */
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_hmac_drbg_set_reseed_interval(&ctx, 4 * reps + 1);
Gavin Acquroff6aceb512020-03-01 17:06:11 -080095
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +010096 /* The new few calls should not reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +010097 for (i = 0; i < (2 * reps); i++) {
98 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
99 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, out, sizeof(out),
100 buf, 16) == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100101 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100103
104 /* Now enable PR, so the next few calls should all reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
106 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200107 expected_consumed_entropy += default_entropy_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100109
110 /* Finally, check setting entropy_len */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_hmac_drbg_set_entropy_len(&ctx, 42);
112 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200113 expected_consumed_entropy += 42;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_hmac_drbg_set_entropy_len(&ctx, 13);
117 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine4d2d4ff2019-10-22 19:10:33 +0200118 expected_consumed_entropy += 13;
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_EQUAL(sizeof(buf) - entropy.len, expected_consumed_entropy);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200120
121exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100123 MD_PSA_DONE();
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100124}
125/* END_CASE */
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128void hmac_drbg_seed_file(int md_alg, char *path, int ret)
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 const mbedtls_md_info_t *md_info;
131 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100132
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100133 MD_PSA_INIT();
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnardf9e94812015-04-28 22:07:14 +0200136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 md_info = mbedtls_md_info_from_type(md_alg);
138 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info,
141 mbedtls_test_rnd_std_rand, NULL,
142 NULL, 0) == 0);
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 TEST_ASSERT(mbedtls_hmac_drbg_write_seed_file(&ctx, path) == ret);
145 TEST_ASSERT(mbedtls_hmac_drbg_update_seed_file(&ctx, path) == ret);
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100146
Paul Bakkerbd51b262014-07-10 15:26:12 +0200147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100149 MD_PSA_DONE();
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100150}
151/* END_CASE */
152
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100153/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154void hmac_drbg_buf(int md_alg)
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100155{
156 unsigned char out[16];
157 unsigned char buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 const mbedtls_md_info_t *md_info;
159 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100160 size_t i;
161
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100162 MD_PSA_INIT();
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_hmac_drbg_init(&ctx);
165 memset(buf, 0, sizeof(buf));
166 memset(out, 0, sizeof(out));
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 md_info = mbedtls_md_info_from_type(md_alg);
169 TEST_ASSERT(md_info != NULL);
170 TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info, buf, sizeof(buf)) == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100171
172 /* Make sure it never tries to reseed (would segfault otherwise) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_hmac_drbg_set_reseed_interval(&ctx, 3);
174 mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 for (i = 0; i < 30; i++) {
177 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
178 }
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100179
Paul Bakkerbd51b262014-07-10 15:26:12 +0200180exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100182 MD_PSA_DONE();
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100183}
184/* END_CASE */
185
186/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187void hmac_drbg_no_reseed(int md_alg, data_t *entropy,
188 data_t *custom, data_t *add1,
189 data_t *add2, data_t *output)
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100190{
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100191 unsigned char data[1024];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100192 unsigned char my_output[512];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100193 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 const mbedtls_md_info_t *md_info;
195 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100196
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100197 MD_PSA_INIT();
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100200
Azim Khand30ca132017-06-09 04:32:58 +0100201 p_entropy.p = entropy->x;
202 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 md_info = mbedtls_md_info_from_type(md_alg);
205 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100206
207 /* Test the simplified buffer-based variant */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 memcpy(data, entropy->x, p_entropy.len);
209 memcpy(data + p_entropy.len, custom->x, custom->len);
210 TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info,
211 data, p_entropy.len + custom->len) == 0);
212 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
213 add1->x, add1->len) == 0);
214 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
215 add2->x, add2->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200216
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800217 /* Reset context for second run */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100221
222 /* And now the normal entropy-based variant */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
224 custom->x, custom->len) == 0);
225 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
226 add1->x, add1->len) == 0);
227 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
228 add2->x, add2->len) == 0);
229 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100230
Paul Bakkerbd51b262014-07-10 15:26:12 +0200231exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100233 MD_PSA_DONE();
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100234}
235/* END_CASE */
236
237/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238void hmac_drbg_nopr(int md_alg, data_t *entropy, data_t *custom,
239 data_t *add1, data_t *add2, data_t *add3,
240 data_t *output)
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100241{
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100242 unsigned char my_output[512];
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100243 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 const mbedtls_md_info_t *md_info;
245 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100246
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100247 MD_PSA_INIT();
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100250
Azim Khand30ca132017-06-09 04:32:58 +0100251 p_entropy.p = entropy->x;
252 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 md_info = mbedtls_md_info_from_type(md_alg);
255 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
258 custom->x, custom->len) == 0);
259 TEST_ASSERT(mbedtls_hmac_drbg_reseed(&ctx, add1->x, add1->len) == 0);
260 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
261 add2->x, add2->len) == 0);
262 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
263 add3->x, add3->len) == 0);
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100266
Paul Bakkerbd51b262014-07-10 15:26:12 +0200267exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100269 MD_PSA_DONE();
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100270}
271/* END_CASE */
272
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100273/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274void hmac_drbg_pr(int md_alg, data_t *entropy, data_t *custom,
275 data_t *add1, data_t *add2, data_t *output)
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100276{
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100277 unsigned char my_output[512];
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100278 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 const mbedtls_md_info_t *md_info;
280 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100281
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100282 MD_PSA_INIT();
283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100285
Azim Khand30ca132017-06-09 04:32:58 +0100286 p_entropy.p = entropy->x;
287 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 md_info = mbedtls_md_info_from_type(md_alg);
290 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
293 custom->x, custom->len) == 0);
294 mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
295 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
296 add1->x, add1->len) == 0);
297 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
298 add2->x, add2->len) == 0);
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200301
302exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100304 MD_PSA_DONE();
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100305}
306/* END_CASE */
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309void hmac_drbg_selftest()
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100310{
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100311 MD_PSA_INIT();
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 TEST_ASSERT(mbedtls_hmac_drbg_self_test(1) == 0);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100314
315exit:
316 MD_PSA_DONE();
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100317}
318/* END_CASE */