blob: c6896998eed536baca1cabecc55d1f9675fa78de [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Gilles Peskineef0624f2018-08-03 20:23:09 +02002#include "mbedtls/entropy.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00003#include "mbedtls/ctr_drbg.h"
Mohammad Azim Khan67735d52017-04-06 11:55:43 +01004#include "string.h"
Rich Evans00ab4702015-02-06 13:43:58 +00005
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +02006/* Modes for ctr_drbg_validate */
Gilles Peskine449bd832023-01-11 14:50:10 +01007enum reseed_mode {
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +02008 RESEED_NEVER, /* never reseed */
9 RESEED_FIRST, /* instantiate, reseed, generate, generate */
10 RESEED_SECOND, /* instantiate, generate, reseed, generate */
11 RESEED_ALWAYS /* prediction resistance, no explicit reseed */
12};
13
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030014static size_t test_offset_idx = 0;
15static size_t test_max_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010016static int mbedtls_test_entropy_func(void *data, unsigned char *buf, size_t len)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000017{
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010018 const unsigned char *p = (unsigned char *) data;
Gilles Peskine449bd832023-01-11 14:50:10 +010019 if (test_offset_idx + len > test_max_idx) {
20 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
21 }
22 memcpy(buf, p + test_offset_idx, len);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010023 test_offset_idx += len;
Gilles Peskine449bd832023-01-11 14:50:10 +010024 return 0;
Paul Bakker0e04d0e2011-11-27 14:46:59 +000025}
Nir Sonnenschein6275be32018-08-29 10:25:30 +030026
Gilles Peskine449bd832023-01-11 14:50:10 +010027static void ctr_drbg_validate_internal(int reseed_mode, data_t *nonce,
28 int entropy_len_arg, data_t *entropy,
29 data_t *reseed,
30 data_t *add1, data_t *add2,
31 data_t *result)
Nir Sonnenschein6275be32018-08-29 10:25:30 +030032{
33 mbedtls_ctr_drbg_context ctx;
Gilles Peskine21e46b32023-10-17 16:35:20 +020034 mbedtls_ctr_drbg_init(&ctx);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030035 unsigned char buf[64];
36
37 size_t entropy_chunk_len = (size_t) entropy_len_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +010038 TEST_ASSERT(entropy_chunk_len <= sizeof(buf));
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030039
Nir Sonnenschein6275be32018-08-29 10:25:30 +030040 test_offset_idx = 0;
Nir Sonnenschein6275be32018-08-29 10:25:30 +030041 test_max_idx = entropy->len;
42
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030043 /* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>)
44 * where nonce||perso = nonce[nonce->len] */
Gilles Peskine449bd832023-01-11 14:50:10 +010045 mbedtls_ctr_drbg_set_entropy_len(&ctx, entropy_chunk_len);
46 mbedtls_ctr_drbg_set_nonce_len(&ctx, 0);
47 TEST_ASSERT(mbedtls_ctr_drbg_seed(
48 &ctx,
49 mbedtls_test_entropy_func, entropy->x,
50 nonce->x, nonce->len) == 0);
51 if (reseed_mode == RESEED_ALWAYS) {
Nir Sonnenschein6275be32018-08-29 10:25:30 +030052 mbedtls_ctr_drbg_set_prediction_resistance(
53 &ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +010054 MBEDTLS_CTR_DRBG_PR_ON);
55 }
Nir Sonnenschein6275be32018-08-29 10:25:30 +030056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 if (reseed_mode == RESEED_FIRST) {
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030058 /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
59 * reseed[:reseed->len]) */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 TEST_ASSERT(mbedtls_ctr_drbg_reseed(
61 &ctx,
62 reseed->x, reseed->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030063 }
64
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030065 /* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1->len]) -> buf */
Nir Sonnenschein6275be32018-08-29 10:25:30 +030066 /* Then reseed if prediction resistance is enabled. */
Gilles Peskine449bd832023-01-11 14:50:10 +010067 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(
68 &ctx,
69 buf, result->len,
70 add1->x, add1->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030071
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (reseed_mode == RESEED_SECOND) {
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030074 /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
75 * reseed[:reseed->len]) */
Gilles Peskine449bd832023-01-11 14:50:10 +010076 TEST_ASSERT(mbedtls_ctr_drbg_reseed(
77 &ctx,
78 reseed->x, reseed->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030079 }
80
81 /* CTR_DRBG_Generate(result->len * 8 bits, add2->x[:add2->len]) -> buf */
82 /* Then reseed if prediction resistance is enabled. */
Gilles Peskine449bd832023-01-11 14:50:10 +010083 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(
84 &ctx,
85 buf, result->len,
86 add2->x, add2->len) == 0);
87 TEST_ASSERT(memcmp(buf, result->x, result->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030088
89exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_ctr_drbg_free(&ctx);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030091}
92
Paul Bakker33b43f12013-08-20 11:48:36 +020093/* END_HEADER */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000094
Paul Bakker33b43f12013-08-20 11:48:36 +020095/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 * depends_on:MBEDTLS_CTR_DRBG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020097 * END_DEPENDENCIES
98 */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000099
Paul Bakker33b43f12013-08-20 11:48:36 +0200100/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100101void ctr_drbg_special_behaviours()
Paul Bakker185ccf72016-07-14 13:21:10 +0100102{
103 mbedtls_ctr_drbg_context ctx;
104 unsigned char output[512];
105 unsigned char additional[512];
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 mbedtls_ctr_drbg_init(&ctx);
108 memset(output, 0, sizeof(output));
109 memset(additional, 0, sizeof(additional));
Paul Bakker185ccf72016-07-14 13:21:10 +0100110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx,
112 output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1,
113 additional, 16) ==
114 MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG);
115 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx,
116 output, 16,
117 additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1) ==
118 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Paul Bakker185ccf72016-07-14 13:21:10 +0100119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, additional,
121 MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1) ==
122 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 mbedtls_ctr_drbg_set_entropy_len(&ctx, ~0);
125 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, additional,
126 MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) ==
127 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Paul Bakker185ccf72016-07-14 13:21:10 +0100128exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_ctr_drbg_free(&ctx);
Paul Bakker185ccf72016-07-14 13:21:10 +0100130}
131/* END_CASE */
132
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200133
134/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135void ctr_drbg_validate_no_reseed(data_t *add_init, data_t *entropy,
136 data_t *add1, data_t *add2,
137 data_t *result_string)
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200138{
Nir Sonnenscheinacedc912018-08-29 23:57:45 +0300139 data_t empty = { 0, 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 ctr_drbg_validate_internal(RESEED_NEVER, add_init,
141 entropy->len, entropy,
142 &empty, add1, add2,
143 result_string);
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300144 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200145}
146/* END_CASE */
147
148/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149void ctr_drbg_validate_pr(data_t *add_init, data_t *entropy,
150 data_t *add1, data_t *add2,
151 data_t *result_string)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000152{
Nir Sonnenscheinacedc912018-08-29 23:57:45 +0300153 data_t empty = { 0, 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 ctr_drbg_validate_internal(RESEED_ALWAYS, add_init,
155 entropy->len / 3, entropy,
156 &empty, add1, add2,
157 result_string);
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300158 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000159}
Paul Bakker33b43f12013-08-20 11:48:36 +0200160/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000161
Paul Bakker33b43f12013-08-20 11:48:36 +0200162/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163void ctr_drbg_validate_reseed_between(data_t *add_init, data_t *entropy,
164 data_t *add1, data_t *add_reseed,
165 data_t *add2, data_t *result_string)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000166{
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 ctr_drbg_validate_internal(RESEED_SECOND, add_init,
168 entropy->len / 2, entropy,
169 add_reseed, add1, add2,
170 result_string);
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300171 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000172}
Paul Bakker33b43f12013-08-20 11:48:36 +0200173/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100174
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100175/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100176void ctr_drbg_validate_reseed_first(data_t *add_init, data_t *entropy,
177 data_t *add1, data_t *add_reseed,
178 data_t *add2, data_t *result_string)
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300179{
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 ctr_drbg_validate_internal(RESEED_FIRST, add_init,
181 entropy->len / 2, entropy,
182 add_reseed, add1, add2,
183 result_string);
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300184 goto exit; // goto is needed to avoid warning ( no test assertions in func)
185}
186/* END_CASE */
187
Gilles Peskine69971662019-10-23 19:39:36 +0200188/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189void ctr_drbg_entropy_strength(int expected_bit_strength)
Gilles Peskine69971662019-10-23 19:39:36 +0200190{
191 unsigned char entropy[/*initial entropy*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 /*nonce*/ MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN +
193 /*reseed*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN];
Gilles Peskine69971662019-10-23 19:39:36 +0200194 mbedtls_ctr_drbg_context ctx;
195 size_t last_idx;
196 size_t byte_strength = expected_bit_strength / 8;
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_ctr_drbg_init(&ctx);
Gilles Peskine69971662019-10-23 19:39:36 +0200199 test_offset_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 test_max_idx = sizeof(entropy);
201 memset(entropy, 0, sizeof(entropy));
Gilles Peskine69971662019-10-23 19:39:36 +0200202
203 /* The initial seeding must grab at least byte_strength bytes of entropy
204 * for the entropy input and byte_strength/2 bytes for a nonce. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx,
206 mbedtls_test_entropy_func, entropy,
207 NULL, 0) == 0);
208 TEST_ASSERT(test_offset_idx >= (byte_strength * 3 + 1) / 2);
Gilles Peskine69971662019-10-23 19:39:36 +0200209 last_idx = test_offset_idx;
210
211 /* A reseed must grab at least byte_strength bytes of entropy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, NULL, 0) == 0);
213 TEST_ASSERT(test_offset_idx - last_idx >= byte_strength);
Gilles Peskine69971662019-10-23 19:39:36 +0200214
215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_ctr_drbg_free(&ctx);
Gilles Peskine69971662019-10-23 19:39:36 +0200217}
218/* END_CASE */
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300219
220/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221void ctr_drbg_entropy_usage(int entropy_nonce_len)
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100222{
223 unsigned char out[16];
224 unsigned char add[16];
225 unsigned char entropy[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100227 size_t i, reps = 10;
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200228 size_t expected_idx = 0;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_ctr_drbg_init(&ctx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100231 test_offset_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 test_max_idx = sizeof(entropy);
233 memset(entropy, 0, sizeof(entropy));
234 memset(out, 0, sizeof(out));
235 memset(add, 0, sizeof(add));
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (entropy_nonce_len >= 0) {
238 TEST_ASSERT(mbedtls_ctr_drbg_set_nonce_len(&ctx, entropy_nonce_len) == 0);
239 }
Gilles Peskinec949de02019-10-22 19:14:26 +0200240
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800241 /* Set reseed interval before seed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_ctr_drbg_set_reseed_interval(&ctx, 2 * reps);
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800243
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100244 /* Init must use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_entropy_func, entropy, NULL, 0) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200246 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (entropy_nonce_len >= 0) {
Gilles Peskinec949de02019-10-22 19:14:26 +0200248 expected_idx += entropy_nonce_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 } else {
Gilles Peskine69971662019-10-23 19:39:36 +0200250 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 }
252 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100253
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800254 /* By default, PR is off, and reseed interval was set to
255 * 2 * reps so the next few calls should not use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 for (i = 0; i < reps; i++) {
257 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out) - 4) == 0);
258 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out) - 4,
259 add, sizeof(add)) == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100260 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100262
263 /* While at it, make sure we didn't write past the requested length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 TEST_ASSERT(out[sizeof(out) - 4] == 0);
265 TEST_ASSERT(out[sizeof(out) - 3] == 0);
266 TEST_ASSERT(out[sizeof(out) - 2] == 0);
267 TEST_ASSERT(out[sizeof(out) - 1] == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100268
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800269 /* There have been 2 * reps calls to random. The next call should reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200271 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100273
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800274 /* Set reseed interval after seed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_ctr_drbg_set_reseed_interval(&ctx, 4 * reps + 1);
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800276
277 /* The next few calls should not reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 for (i = 0; i < (2 * reps); i++) {
279 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
280 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out),
281 add, sizeof(add)) == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100282 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100284
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000285 /* Call update with too much data (sizeof(entropy) > MAX(_SEED)_INPUT).
Gilles Peskined9199932018-09-11 16:41:54 +0200286 * Make sure it's detected as an error and doesn't cause memory
287 * corruption. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 TEST_ASSERT(mbedtls_ctr_drbg_update(
289 &ctx, entropy, sizeof(entropy)) != 0);
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100290
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100291 /* Now enable PR, so the next few calls should all reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON);
293 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200294 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100296
297 /* Finally, check setting entropy_len */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_ctr_drbg_set_entropy_len(&ctx, 42);
299 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200300 expected_idx += 42;
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_ctr_drbg_set_entropy_len(&ctx, 13);
304 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200305 expected_idx += 13;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 TEST_EQUAL(test_offset_idx, expected_idx);
Paul Bakkera317a982014-06-18 16:44:11 +0200307
Paul Bakkerbd51b262014-07-10 15:26:12 +0200308exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 mbedtls_ctr_drbg_free(&ctx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100310}
311/* END_CASE */
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314void ctr_drbg_seed_file(char *path, int ret)
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 mbedtls_ctr_drbg_init(&ctx);
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_rnd_std_rand,
321 NULL, NULL, 0) == 0);
322 TEST_ASSERT(mbedtls_ctr_drbg_write_seed_file(&ctx, path) == ret);
323 TEST_ASSERT(mbedtls_ctr_drbg_update_seed_file(&ctx, path) == ret);
Paul Bakkera317a982014-06-18 16:44:11 +0200324
Paul Bakkerbd51b262014-07-10 15:26:12 +0200325exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 mbedtls_ctr_drbg_free(&ctx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100327}
328/* END_CASE */
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331void ctr_drbg_selftest()
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100332{
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 TEST_ASSERT(mbedtls_ctr_drbg_self_test(1) == 0);
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100334}
335/* END_CASE */