blob: 066e70b3522f3bccc25c4f1660348023080b8b8e [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 };
Valerio Settidc32ac22023-11-13 10:27:56 +0100140 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 ctr_drbg_validate_internal(RESEED_NEVER, add_init,
142 entropy->len, entropy,
143 &empty, add1, add2,
144 result_string);
Valerio Settidc32ac22023-11-13 10:27:56 +0100145 AES_PSA_DONE();
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 };
Valerio Settidc32ac22023-11-13 10:27:56 +0100156 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 ctr_drbg_validate_internal(RESEED_ALWAYS, add_init,
158 entropy->len / 3, entropy,
159 &empty, add1, add2,
160 result_string);
Valerio Settidc32ac22023-11-13 10:27:56 +0100161 AES_PSA_DONE();
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300162 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000163}
Paul Bakker33b43f12013-08-20 11:48:36 +0200164/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000165
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167void ctr_drbg_validate_reseed_between(data_t *add_init, data_t *entropy,
168 data_t *add1, data_t *add_reseed,
169 data_t *add2, data_t *result_string)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000170{
Valerio Settidc32ac22023-11-13 10:27:56 +0100171 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 ctr_drbg_validate_internal(RESEED_SECOND, add_init,
173 entropy->len / 2, entropy,
174 add_reseed, add1, add2,
175 result_string);
Valerio Settidc32ac22023-11-13 10:27:56 +0100176 AES_PSA_DONE();
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300177 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000178}
Paul Bakker33b43f12013-08-20 11:48:36 +0200179/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100180
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100181/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100182void ctr_drbg_validate_reseed_first(data_t *add_init, data_t *entropy,
183 data_t *add1, data_t *add_reseed,
184 data_t *add2, data_t *result_string)
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300185{
Valerio Settidc32ac22023-11-13 10:27:56 +0100186 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 ctr_drbg_validate_internal(RESEED_FIRST, add_init,
188 entropy->len / 2, entropy,
189 add_reseed, add1, add2,
190 result_string);
Valerio Settidc32ac22023-11-13 10:27:56 +0100191 AES_PSA_DONE();
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300192 goto exit; // goto is needed to avoid warning ( no test assertions in func)
193}
194/* END_CASE */
195
Gilles Peskine69971662019-10-23 19:39:36 +0200196/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197void ctr_drbg_entropy_strength(int expected_bit_strength)
Gilles Peskine69971662019-10-23 19:39:36 +0200198{
199 unsigned char entropy[/*initial entropy*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 /*nonce*/ MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN +
201 /*reseed*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN];
Gilles Peskine69971662019-10-23 19:39:36 +0200202 mbedtls_ctr_drbg_context ctx;
203 size_t last_idx;
204 size_t byte_strength = expected_bit_strength / 8;
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 mbedtls_ctr_drbg_init(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100207
208 AES_PSA_INIT();
Gilles Peskine69971662019-10-23 19:39:36 +0200209 test_offset_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 test_max_idx = sizeof(entropy);
211 memset(entropy, 0, sizeof(entropy));
Gilles Peskine69971662019-10-23 19:39:36 +0200212
213 /* The initial seeding must grab at least byte_strength bytes of entropy
214 * for the entropy input and byte_strength/2 bytes for a nonce. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx,
216 mbedtls_test_entropy_func, entropy,
217 NULL, 0) == 0);
218 TEST_ASSERT(test_offset_idx >= (byte_strength * 3 + 1) / 2);
Gilles Peskine69971662019-10-23 19:39:36 +0200219 last_idx = test_offset_idx;
220
221 /* A reseed must grab at least byte_strength bytes of entropy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, NULL, 0) == 0);
223 TEST_ASSERT(test_offset_idx - last_idx >= byte_strength);
Gilles Peskine69971662019-10-23 19:39:36 +0200224
225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_ctr_drbg_free(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100227 AES_PSA_DONE();
Gilles Peskine69971662019-10-23 19:39:36 +0200228}
229/* END_CASE */
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300230
231/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232void ctr_drbg_entropy_usage(int entropy_nonce_len)
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100233{
234 unsigned char out[16];
235 unsigned char add[16];
236 unsigned char entropy[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100238 size_t i, reps = 10;
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200239 size_t expected_idx = 0;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_ctr_drbg_init(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100242
243 AES_PSA_INIT();
244
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100245 test_offset_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 test_max_idx = sizeof(entropy);
247 memset(entropy, 0, sizeof(entropy));
248 memset(out, 0, sizeof(out));
249 memset(add, 0, sizeof(add));
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (entropy_nonce_len >= 0) {
252 TEST_ASSERT(mbedtls_ctr_drbg_set_nonce_len(&ctx, entropy_nonce_len) == 0);
253 }
Gilles Peskinec949de02019-10-22 19:14:26 +0200254
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800255 /* Set reseed interval before seed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 mbedtls_ctr_drbg_set_reseed_interval(&ctx, 2 * reps);
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800257
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100258 /* Init must use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_entropy_func, entropy, NULL, 0) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200260 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (entropy_nonce_len >= 0) {
Gilles Peskinec949de02019-10-22 19:14:26 +0200262 expected_idx += entropy_nonce_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 } else {
Gilles Peskine69971662019-10-23 19:39:36 +0200264 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 }
266 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100267
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800268 /* By default, PR is off, and reseed interval was set to
269 * 2 * reps so the next few calls should not use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 for (i = 0; i < reps; i++) {
271 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out) - 4) == 0);
272 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out) - 4,
273 add, sizeof(add)) == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100274 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100276
277 /* While at it, make sure we didn't write past the requested length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 TEST_ASSERT(out[sizeof(out) - 4] == 0);
279 TEST_ASSERT(out[sizeof(out) - 3] == 0);
280 TEST_ASSERT(out[sizeof(out) - 2] == 0);
281 TEST_ASSERT(out[sizeof(out) - 1] == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100282
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800283 /* There have been 2 * reps calls to random. The next call should reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200285 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100287
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800288 /* Set reseed interval after seed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 mbedtls_ctr_drbg_set_reseed_interval(&ctx, 4 * reps + 1);
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800290
291 /* The next few calls should not reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 for (i = 0; i < (2 * reps); i++) {
293 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
294 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out),
295 add, sizeof(add)) == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100296 }
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
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000299 /* Call update with too much data (sizeof(entropy) > MAX(_SEED)_INPUT).
Gilles Peskined9199932018-09-11 16:41:54 +0200300 * Make sure it's detected as an error and doesn't cause memory
301 * corruption. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 TEST_ASSERT(mbedtls_ctr_drbg_update(
303 &ctx, entropy, sizeof(entropy)) != 0);
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100304
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100305 /* Now enable PR, so the next few calls should all reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON);
307 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200308 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100310
311 /* Finally, check setting entropy_len */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 mbedtls_ctr_drbg_set_entropy_len(&ctx, 42);
313 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200314 expected_idx += 42;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 mbedtls_ctr_drbg_set_entropy_len(&ctx, 13);
318 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200319 expected_idx += 13;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 TEST_EQUAL(test_offset_idx, expected_idx);
Paul Bakkera317a982014-06-18 16:44:11 +0200321
Paul Bakkerbd51b262014-07-10 15:26:12 +0200322exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_ctr_drbg_free(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100324 AES_PSA_DONE();
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100325}
326/* END_CASE */
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329void ctr_drbg_seed_file(char *path, int ret)
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 mbedtls_ctr_drbg_init(&ctx);
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200334
Valerio Settidc32ac22023-11-13 10:27:56 +0100335 AES_PSA_INIT();
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_rnd_std_rand,
338 NULL, NULL, 0) == 0);
339 TEST_ASSERT(mbedtls_ctr_drbg_write_seed_file(&ctx, path) == ret);
340 TEST_ASSERT(mbedtls_ctr_drbg_update_seed_file(&ctx, path) == ret);
Paul Bakkera317a982014-06-18 16:44:11 +0200341
Paul Bakkerbd51b262014-07-10 15:26:12 +0200342exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 mbedtls_ctr_drbg_free(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100344 AES_PSA_DONE();
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100345}
346/* END_CASE */
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100349void ctr_drbg_selftest()
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100350{
Valerio Settidc32ac22023-11-13 10:27:56 +0100351 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 TEST_ASSERT(mbedtls_ctr_drbg_self_test(1) == 0);
Valerio Settidc32ac22023-11-13 10:27:56 +0100353 AES_PSA_DONE();
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100354}
355/* END_CASE */