blob: fbe1b03dbe531ec8ed6ecd030b37c3f1c90ca734 [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 Peskine8369b4a2024-11-04 18:21:57 +010051 MD_PSA_INIT();
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;
Gilles Peskine8369b4a2024-11-04 18:21:57 +0100132 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100133
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100134 MD_PSA_INIT();
135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 md_info = mbedtls_md_info_from_type(md_alg);
137 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info,
140 mbedtls_test_rnd_std_rand, NULL,
141 NULL, 0) == 0);
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 TEST_ASSERT(mbedtls_hmac_drbg_write_seed_file(&ctx, path) == ret);
144 TEST_ASSERT(mbedtls_hmac_drbg_update_seed_file(&ctx, path) == ret);
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100145
Paul Bakkerbd51b262014-07-10 15:26:12 +0200146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100148 MD_PSA_DONE();
Manuel Pégourié-Gonnard48bc3e82014-01-30 21:11:16 +0100149}
150/* END_CASE */
151
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100152/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153void hmac_drbg_buf(int md_alg)
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100154{
155 unsigned char out[16];
156 unsigned char buf[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 const mbedtls_md_info_t *md_info;
158 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100159 size_t i;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_hmac_drbg_init(&ctx);
162 memset(buf, 0, sizeof(buf));
163 memset(out, 0, sizeof(out));
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100164
Gilles Peskine8369b4a2024-11-04 18:21:57 +0100165 MD_PSA_INIT();
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 md_info = mbedtls_md_info_from_type(md_alg);
168 TEST_ASSERT(md_info != NULL);
169 TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info, buf, sizeof(buf)) == 0);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100170
171 /* Make sure it never tries to reseed (would segfault otherwise) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 mbedtls_hmac_drbg_set_reseed_interval(&ctx, 3);
173 mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 for (i = 0; i < 30; i++) {
176 TEST_ASSERT(mbedtls_hmac_drbg_random(&ctx, out, sizeof(out)) == 0);
177 }
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100178
Paul Bakkerbd51b262014-07-10 15:26:12 +0200179exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100181 MD_PSA_DONE();
Manuel Pégourié-Gonnard4f880a52014-01-30 22:39:42 +0100182}
183/* END_CASE */
184
185/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186void hmac_drbg_no_reseed(int md_alg, data_t *entropy,
187 data_t *custom, data_t *add1,
188 data_t *add2, data_t *output)
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100189{
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100190 unsigned char data[1024];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100191 unsigned char my_output[512];
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100192 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 const mbedtls_md_info_t *md_info;
194 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100197
Azim Khand30ca132017-06-09 04:32:58 +0100198 p_entropy.p = entropy->x;
199 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100200
Gilles Peskine8369b4a2024-11-04 18:21:57 +0100201 MD_PSA_INIT();
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 md_info = mbedtls_md_info_from_type(md_alg);
204 TEST_ASSERT(md_info != NULL);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100205
206 /* Test the simplified buffer-based variant */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 memcpy(data, entropy->x, p_entropy.len);
208 memcpy(data + p_entropy.len, custom->x, custom->len);
209 TEST_ASSERT(mbedtls_hmac_drbg_seed_buf(&ctx, md_info,
210 data, p_entropy.len + custom->len) == 0);
211 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
212 add1->x, add1->len) == 0);
213 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
214 add2->x, add2->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200215
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800216 /* Reset context for second run */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100220
221 /* And now the normal entropy-based variant */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
223 custom->x, custom->len) == 0);
224 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
225 add1->x, add1->len) == 0);
226 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
227 add2->x, add2->len) == 0);
228 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnarde6cdbbd2014-02-01 11:30:03 +0100229
Paul Bakkerbd51b262014-07-10 15:26:12 +0200230exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100232 MD_PSA_DONE();
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100233}
234/* END_CASE */
235
236/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237void hmac_drbg_nopr(int md_alg, data_t *entropy, data_t *custom,
238 data_t *add1, data_t *add2, data_t *add3,
239 data_t *output)
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100240{
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100241 unsigned char my_output[512];
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100242 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 const mbedtls_md_info_t *md_info;
244 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100247
Azim Khand30ca132017-06-09 04:32:58 +0100248 p_entropy.p = entropy->x;
249 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100250
Gilles Peskine8369b4a2024-11-04 18:21:57 +0100251 MD_PSA_INIT();
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 md_info = mbedtls_md_info_from_type(md_alg);
254 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
257 custom->x, custom->len) == 0);
258 TEST_ASSERT(mbedtls_hmac_drbg_reseed(&ctx, add1->x, add1->len) == 0);
259 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
260 add2->x, add2->len) == 0);
261 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
262 add3->x, add3->len) == 0);
Manuel Pégourié-Gonnard24600b72014-01-31 09:54:14 +0100263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100265
Paul Bakkerbd51b262014-07-10 15:26:12 +0200266exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100268 MD_PSA_DONE();
Manuel Pégourié-Gonnard6801f392014-01-30 17:22:14 +0100269}
270/* END_CASE */
271
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100272/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273void hmac_drbg_pr(int md_alg, data_t *entropy, data_t *custom,
274 data_t *add1, data_t *add2, data_t *output)
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100275{
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100276 unsigned char my_output[512];
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100277 entropy_ctx p_entropy;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 const mbedtls_md_info_t *md_info;
279 mbedtls_hmac_drbg_context ctx;
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 mbedtls_hmac_drbg_init(&ctx);
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100282
Azim Khand30ca132017-06-09 04:32:58 +0100283 p_entropy.p = entropy->x;
284 p_entropy.len = entropy->len;
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100285
Gilles Peskine8369b4a2024-11-04 18:21:57 +0100286 MD_PSA_INIT();
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 md_info = mbedtls_md_info_from_type(md_alg);
289 TEST_ASSERT(md_info != NULL);
Paul Bakker94b916c2014-04-17 16:07:20 +0200290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 TEST_ASSERT(mbedtls_hmac_drbg_seed(&ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
292 custom->x, custom->len) == 0);
293 mbedtls_hmac_drbg_set_prediction_resistance(&ctx, MBEDTLS_HMAC_DRBG_PR_ON);
294 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
295 add1->x, add1->len) == 0);
296 TEST_ASSERT(mbedtls_hmac_drbg_random_with_add(&ctx, my_output, output->len,
297 add2->x, add2->len) == 0);
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 TEST_ASSERT(memcmp(my_output, output->x, output->len) == 0);
Paul Bakkerbd51b262014-07-10 15:26:12 +0200300
301exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 mbedtls_hmac_drbg_free(&ctx);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100303 MD_PSA_DONE();
Manuel Pégourié-Gonnard62273b82014-01-31 10:16:57 +0100304}
305/* END_CASE */
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308void hmac_drbg_selftest()
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100309{
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100310 MD_PSA_INIT();
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 TEST_ASSERT(mbedtls_hmac_drbg_self_test(1) == 0);
Manuel Pégourié-Gonnardd111fbd2023-03-20 10:44:44 +0100313
314exit:
315 MD_PSA_DONE();
Manuel Pégourié-Gonnard79afaa02014-01-31 11:12:09 +0100316}
317/* END_CASE */