blob: 7d816080ac6371bc033f48c2fc0df4f2120a08c6 [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;
34 unsigned char buf[64];
35
36 size_t entropy_chunk_len = (size_t) entropy_len_arg;
37
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;
Gilles Peskine449bd832023-01-11 14:50:10 +010041 mbedtls_ctr_drbg_init(&ctx);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030042
43 test_max_idx = entropy->len;
44
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030045 /* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>)
46 * where nonce||perso = nonce[nonce->len] */
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_ctr_drbg_set_entropy_len(&ctx, entropy_chunk_len);
48 mbedtls_ctr_drbg_set_nonce_len(&ctx, 0);
49 TEST_ASSERT(mbedtls_ctr_drbg_seed(
50 &ctx,
51 mbedtls_test_entropy_func, entropy->x,
52 nonce->x, nonce->len) == 0);
53 if (reseed_mode == RESEED_ALWAYS) {
Nir Sonnenschein6275be32018-08-29 10:25:30 +030054 mbedtls_ctr_drbg_set_prediction_resistance(
55 &ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +010056 MBEDTLS_CTR_DRBG_PR_ON);
57 }
Nir Sonnenschein6275be32018-08-29 10:25:30 +030058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 if (reseed_mode == RESEED_FIRST) {
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030060 /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
61 * reseed[:reseed->len]) */
Gilles Peskine449bd832023-01-11 14:50:10 +010062 TEST_ASSERT(mbedtls_ctr_drbg_reseed(
63 &ctx,
64 reseed->x, reseed->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030065 }
66
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030067 /* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1->len]) -> buf */
Nir Sonnenschein6275be32018-08-29 10:25:30 +030068 /* Then reseed if prediction resistance is enabled. */
Gilles Peskine449bd832023-01-11 14:50:10 +010069 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(
70 &ctx,
71 buf, result->len,
72 add1->x, add1->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030073
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (reseed_mode == RESEED_SECOND) {
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030076 /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
77 * reseed[:reseed->len]) */
Gilles Peskine449bd832023-01-11 14:50:10 +010078 TEST_ASSERT(mbedtls_ctr_drbg_reseed(
79 &ctx,
80 reseed->x, reseed->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030081 }
82
83 /* CTR_DRBG_Generate(result->len * 8 bits, add2->x[:add2->len]) -> buf */
84 /* Then reseed if prediction resistance is enabled. */
Gilles Peskine449bd832023-01-11 14:50:10 +010085 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(
86 &ctx,
87 buf, result->len,
88 add2->x, add2->len) == 0);
89 TEST_ASSERT(memcmp(buf, result->x, result->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030090
91exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_ctr_drbg_free(&ctx);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030093}
94
Paul Bakker33b43f12013-08-20 11:48:36 +020095/* END_HEADER */
Paul Bakker0e04d0e2011-11-27 14:46:59 +000096
Paul Bakker33b43f12013-08-20 11:48:36 +020097/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 * depends_on:MBEDTLS_CTR_DRBG_C
Paul Bakker33b43f12013-08-20 11:48:36 +020099 * END_DEPENDENCIES
100 */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000101
Paul Bakker33b43f12013-08-20 11:48:36 +0200102/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103void ctr_drbg_special_behaviours()
Paul Bakker185ccf72016-07-14 13:21:10 +0100104{
105 mbedtls_ctr_drbg_context ctx;
106 unsigned char output[512];
107 unsigned char additional[512];
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_ctr_drbg_init(&ctx);
110 memset(output, 0, sizeof(output));
111 memset(additional, 0, sizeof(additional));
Paul Bakker185ccf72016-07-14 13:21:10 +0100112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx,
114 output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1,
115 additional, 16) ==
116 MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG);
117 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx,
118 output, 16,
119 additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1) ==
120 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Paul Bakker185ccf72016-07-14 13:21:10 +0100121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, additional,
123 MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1) ==
124 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_ctr_drbg_set_entropy_len(&ctx, ~0);
127 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, additional,
128 MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) ==
129 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Paul Bakker185ccf72016-07-14 13:21:10 +0100130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_ctr_drbg_free(&ctx);
Paul Bakker185ccf72016-07-14 13:21:10 +0100132}
133/* END_CASE */
134
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200135
136/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137void ctr_drbg_validate_no_reseed(data_t *add_init, data_t *entropy,
138 data_t *add1, data_t *add2,
139 data_t *result_string)
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200140{
Nir Sonnenscheinacedc912018-08-29 23:57:45 +0300141 data_t empty = { 0, 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 ctr_drbg_validate_internal(RESEED_NEVER, add_init,
143 entropy->len, entropy,
144 &empty, add1, add2,
145 result_string);
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300146 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200147}
148/* END_CASE */
149
150/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151void ctr_drbg_validate_pr(data_t *add_init, data_t *entropy,
152 data_t *add1, data_t *add2,
153 data_t *result_string)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000154{
Nir Sonnenscheinacedc912018-08-29 23:57:45 +0300155 data_t empty = { 0, 0 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 ctr_drbg_validate_internal(RESEED_ALWAYS, add_init,
157 entropy->len / 3, entropy,
158 &empty, add1, add2,
159 result_string);
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300160 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000161}
Paul Bakker33b43f12013-08-20 11:48:36 +0200162/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000163
Paul Bakker33b43f12013-08-20 11:48:36 +0200164/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165void ctr_drbg_validate_reseed_between(data_t *add_init, data_t *entropy,
166 data_t *add1, data_t *add_reseed,
167 data_t *add2, data_t *result_string)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000168{
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 ctr_drbg_validate_internal(RESEED_SECOND, add_init,
170 entropy->len / 2, entropy,
171 add_reseed, add1, add2,
172 result_string);
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300173 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000174}
Paul Bakker33b43f12013-08-20 11:48:36 +0200175/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100176
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100177/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178void ctr_drbg_validate_reseed_first(data_t *add_init, data_t *entropy,
179 data_t *add1, data_t *add_reseed,
180 data_t *add2, data_t *result_string)
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300181{
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 ctr_drbg_validate_internal(RESEED_FIRST, add_init,
183 entropy->len / 2, entropy,
184 add_reseed, add1, add2,
185 result_string);
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300186 goto exit; // goto is needed to avoid warning ( no test assertions in func)
187}
188/* END_CASE */
189
Gilles Peskine69971662019-10-23 19:39:36 +0200190/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191void ctr_drbg_entropy_strength(int expected_bit_strength)
Gilles Peskine69971662019-10-23 19:39:36 +0200192{
193 unsigned char entropy[/*initial entropy*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 /*nonce*/ MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN +
195 /*reseed*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN];
Gilles Peskine69971662019-10-23 19:39:36 +0200196 mbedtls_ctr_drbg_context ctx;
197 size_t last_idx;
198 size_t byte_strength = expected_bit_strength / 8;
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_ctr_drbg_init(&ctx);
Gilles Peskine69971662019-10-23 19:39:36 +0200201 test_offset_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 test_max_idx = sizeof(entropy);
203 memset(entropy, 0, sizeof(entropy));
Gilles Peskine69971662019-10-23 19:39:36 +0200204
205 /* The initial seeding must grab at least byte_strength bytes of entropy
206 * for the entropy input and byte_strength/2 bytes for a nonce. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx,
208 mbedtls_test_entropy_func, entropy,
209 NULL, 0) == 0);
210 TEST_ASSERT(test_offset_idx >= (byte_strength * 3 + 1) / 2);
Gilles Peskine69971662019-10-23 19:39:36 +0200211 last_idx = test_offset_idx;
212
213 /* A reseed must grab at least byte_strength bytes of entropy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, NULL, 0) == 0);
215 TEST_ASSERT(test_offset_idx - last_idx >= byte_strength);
Gilles Peskine69971662019-10-23 19:39:36 +0200216
217exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_ctr_drbg_free(&ctx);
Gilles Peskine69971662019-10-23 19:39:36 +0200219}
220/* END_CASE */
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300221
222/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223void ctr_drbg_entropy_usage(int entropy_nonce_len)
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100224{
225 unsigned char out[16];
226 unsigned char add[16];
227 unsigned char entropy[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100229 size_t i, reps = 10;
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200230 size_t expected_idx = 0;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ctr_drbg_init(&ctx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100233 test_offset_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 test_max_idx = sizeof(entropy);
235 memset(entropy, 0, sizeof(entropy));
236 memset(out, 0, sizeof(out));
237 memset(add, 0, sizeof(add));
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (entropy_nonce_len >= 0) {
240 TEST_ASSERT(mbedtls_ctr_drbg_set_nonce_len(&ctx, entropy_nonce_len) == 0);
241 }
Gilles Peskinec949de02019-10-22 19:14:26 +0200242
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800243 /* Set reseed interval before seed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_ctr_drbg_set_reseed_interval(&ctx, 2 * reps);
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800245
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100246 /* Init must use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_entropy_func, entropy, NULL, 0) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200248 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (entropy_nonce_len >= 0) {
Gilles Peskinec949de02019-10-22 19:14:26 +0200250 expected_idx += entropy_nonce_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 } else {
Gilles Peskine69971662019-10-23 19:39:36 +0200252 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 }
254 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100255
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800256 /* By default, PR is off, and reseed interval was set to
257 * 2 * reps so the next few calls should not use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 for (i = 0; i < reps; i++) {
259 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out) - 4) == 0);
260 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out) - 4,
261 add, sizeof(add)) == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100262 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100264
265 /* While at it, make sure we didn't write past the requested length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 TEST_ASSERT(out[sizeof(out) - 4] == 0);
267 TEST_ASSERT(out[sizeof(out) - 3] == 0);
268 TEST_ASSERT(out[sizeof(out) - 2] == 0);
269 TEST_ASSERT(out[sizeof(out) - 1] == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100270
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800271 /* There have been 2 * reps calls to random. The next call should reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200273 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100275
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800276 /* Set reseed interval after seed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_ctr_drbg_set_reseed_interval(&ctx, 4 * reps + 1);
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800278
279 /* The next few calls should not reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 for (i = 0; i < (2 * reps); i++) {
281 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
282 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out),
283 add, sizeof(add)) == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100284 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100286
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000287 /* Call update with too much data (sizeof(entropy) > MAX(_SEED)_INPUT).
Gilles Peskined9199932018-09-11 16:41:54 +0200288 * Make sure it's detected as an error and doesn't cause memory
289 * corruption. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 TEST_ASSERT(mbedtls_ctr_drbg_update(
291 &ctx, entropy, sizeof(entropy)) != 0);
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100292
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100293 /* Now enable PR, so the next few calls should all reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON);
295 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200296 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100298
299 /* Finally, check setting entropy_len */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 mbedtls_ctr_drbg_set_entropy_len(&ctx, 42);
301 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200302 expected_idx += 42;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 mbedtls_ctr_drbg_set_entropy_len(&ctx, 13);
306 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200307 expected_idx += 13;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 TEST_EQUAL(test_offset_idx, expected_idx);
Paul Bakkera317a982014-06-18 16:44:11 +0200309
Paul Bakkerbd51b262014-07-10 15:26:12 +0200310exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 mbedtls_ctr_drbg_free(&ctx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100312}
313/* END_CASE */
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316void ctr_drbg_seed_file(char *path, int ret)
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100317{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ctr_drbg_init(&ctx);
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_rnd_std_rand,
323 NULL, NULL, 0) == 0);
324 TEST_ASSERT(mbedtls_ctr_drbg_write_seed_file(&ctx, path) == ret);
325 TEST_ASSERT(mbedtls_ctr_drbg_update_seed_file(&ctx, path) == ret);
Paul Bakkera317a982014-06-18 16:44:11 +0200326
Paul Bakkerbd51b262014-07-10 15:26:12 +0200327exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 mbedtls_ctr_drbg_free(&ctx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100329}
330/* END_CASE */
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333void ctr_drbg_selftest()
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100334{
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 TEST_ASSERT(mbedtls_ctr_drbg_self_test(1) == 0);
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100336}
337/* END_CASE */