blob: 63524f25a0b4a786ebe3daa94642256cc7ad0151 [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"
Dave Rodgmanae730342024-01-13 17:31:13 +00005#include "ctr.h"
Rich Evans00ab4702015-02-06 13:43:58 +00006
Paul Elliott20b2efa2023-11-21 14:46:51 +00007#if defined(MBEDTLS_THREADING_PTHREAD)
8#include "mbedtls/threading.h"
9#endif
10
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +020011/* Modes for ctr_drbg_validate */
Gilles Peskine449bd832023-01-11 14:50:10 +010012enum reseed_mode {
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +020013 RESEED_NEVER, /* never reseed */
14 RESEED_FIRST, /* instantiate, reseed, generate, generate */
15 RESEED_SECOND, /* instantiate, generate, reseed, generate */
16 RESEED_ALWAYS /* prediction resistance, no explicit reseed */
17};
18
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030019static size_t test_offset_idx = 0;
20static size_t test_max_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +010021static int mbedtls_test_entropy_func(void *data, unsigned char *buf, size_t len)
Paul Bakker0e04d0e2011-11-27 14:46:59 +000022{
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010023 const unsigned char *p = (unsigned char *) data;
Gilles Peskine449bd832023-01-11 14:50:10 +010024 if (test_offset_idx + len > test_max_idx) {
25 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
26 }
27 memcpy(buf, p + test_offset_idx, len);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +010028 test_offset_idx += len;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 return 0;
Paul Bakker0e04d0e2011-11-27 14:46:59 +000030}
Nir Sonnenschein6275be32018-08-29 10:25:30 +030031
Gilles Peskine449bd832023-01-11 14:50:10 +010032static void ctr_drbg_validate_internal(int reseed_mode, data_t *nonce,
33 int entropy_len_arg, data_t *entropy,
34 data_t *reseed,
35 data_t *add1, data_t *add2,
36 data_t *result)
Nir Sonnenschein6275be32018-08-29 10:25:30 +030037{
38 mbedtls_ctr_drbg_context ctx;
Gilles Peskine21e46b32023-10-17 16:35:20 +020039 mbedtls_ctr_drbg_init(&ctx);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030040 unsigned char buf[64];
41
42 size_t entropy_chunk_len = (size_t) entropy_len_arg;
Gilles Peskine449bd832023-01-11 14:50:10 +010043 TEST_ASSERT(entropy_chunk_len <= sizeof(buf));
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030044
Nir Sonnenschein6275be32018-08-29 10:25:30 +030045 test_offset_idx = 0;
Nir Sonnenschein6275be32018-08-29 10:25:30 +030046 test_max_idx = entropy->len;
47
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030048 /* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>)
49 * where nonce||perso = nonce[nonce->len] */
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_ctr_drbg_set_entropy_len(&ctx, entropy_chunk_len);
51 mbedtls_ctr_drbg_set_nonce_len(&ctx, 0);
52 TEST_ASSERT(mbedtls_ctr_drbg_seed(
53 &ctx,
54 mbedtls_test_entropy_func, entropy->x,
55 nonce->x, nonce->len) == 0);
56 if (reseed_mode == RESEED_ALWAYS) {
Nir Sonnenschein6275be32018-08-29 10:25:30 +030057 mbedtls_ctr_drbg_set_prediction_resistance(
58 &ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +010059 MBEDTLS_CTR_DRBG_PR_ON);
60 }
Nir Sonnenschein6275be32018-08-29 10:25:30 +030061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if (reseed_mode == RESEED_FIRST) {
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030063 /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
64 * reseed[:reseed->len]) */
Gilles Peskine449bd832023-01-11 14:50:10 +010065 TEST_ASSERT(mbedtls_ctr_drbg_reseed(
66 &ctx,
67 reseed->x, reseed->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030068 }
69
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030070 /* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1->len]) -> buf */
Nir Sonnenschein6275be32018-08-29 10:25:30 +030071 /* Then reseed if prediction resistance is enabled. */
Gilles Peskine449bd832023-01-11 14:50:10 +010072 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(
73 &ctx,
74 buf, result->len,
75 add1->x, add1->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030076
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 if (reseed_mode == RESEED_SECOND) {
Nir Sonnenschein85fcb582018-08-29 23:38:57 +030079 /* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
80 * reseed[:reseed->len]) */
Gilles Peskine449bd832023-01-11 14:50:10 +010081 TEST_ASSERT(mbedtls_ctr_drbg_reseed(
82 &ctx,
83 reseed->x, reseed->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030084 }
85
86 /* CTR_DRBG_Generate(result->len * 8 bits, add2->x[:add2->len]) -> buf */
87 /* Then reseed if prediction resistance is enabled. */
Gilles Peskine449bd832023-01-11 14:50:10 +010088 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(
89 &ctx,
90 buf, result->len,
91 add2->x, add2->len) == 0);
92 TEST_ASSERT(memcmp(buf, result->x, result->len) == 0);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030093
94exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_ctr_drbg_free(&ctx);
Nir Sonnenschein6275be32018-08-29 10:25:30 +030096}
97
Janos Follatha16ee6b2023-10-04 19:05:26 +010098static const int thread_random_reps = 10;
Paul Elliott6a997c92023-11-30 14:47:17 +000099void *thread_random_function(void *ctx)
Janos Follatha16ee6b2023-10-04 19:05:26 +0100100{
101 unsigned char out[16];
102 memset(out, 0, sizeof(out));
103
Paul Elliott6a997c92023-11-30 14:47:17 +0000104 for (int i = 0; i < thread_random_reps; i++) {
105 TEST_EQUAL(mbedtls_ctr_drbg_random((mbedtls_ctr_drbg_context *) ctx, out, sizeof(out)), 0);
Janos Follatha16ee6b2023-10-04 19:05:26 +0100106 }
107
108exit:
109 return NULL;
110}
Paul Bakker33b43f12013-08-20 11:48:36 +0200111/* END_HEADER */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000112
Paul Bakker33b43f12013-08-20 11:48:36 +0200113/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 * depends_on:MBEDTLS_CTR_DRBG_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200115 * END_DEPENDENCIES
116 */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000117
Paul Bakker33b43f12013-08-20 11:48:36 +0200118/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119void ctr_drbg_special_behaviours()
Paul Bakker185ccf72016-07-14 13:21:10 +0100120{
121 mbedtls_ctr_drbg_context ctx;
122 unsigned char output[512];
123 unsigned char additional[512];
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_ctr_drbg_init(&ctx);
126 memset(output, 0, sizeof(output));
127 memset(additional, 0, sizeof(additional));
Paul Bakker185ccf72016-07-14 13:21:10 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx,
130 output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1,
131 additional, 16) ==
132 MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG);
133 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx,
134 output, 16,
135 additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1) ==
136 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Paul Bakker185ccf72016-07-14 13:21:10 +0100137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, additional,
139 MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1) ==
140 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Andres Amaya Garcia6a543362017-01-17 23:04:22 +0000141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_ctr_drbg_set_entropy_len(&ctx, ~0);
143 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, additional,
144 MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) ==
145 MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
Paul Bakker185ccf72016-07-14 13:21:10 +0100146exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_ctr_drbg_free(&ctx);
Paul Bakker185ccf72016-07-14 13:21:10 +0100148}
149/* END_CASE */
150
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200151
152/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153void ctr_drbg_validate_no_reseed(data_t *add_init, data_t *entropy,
154 data_t *add1, data_t *add2,
155 data_t *result_string)
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200156{
Nir Sonnenscheinacedc912018-08-29 23:57:45 +0300157 data_t empty = { 0, 0 };
Valerio Settidc32ac22023-11-13 10:27:56 +0100158 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 ctr_drbg_validate_internal(RESEED_NEVER, add_init,
160 entropy->len, entropy,
161 &empty, add1, add2,
162 result_string);
Valerio Settidc32ac22023-11-13 10:27:56 +0100163 AES_PSA_DONE();
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300164 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Gilles Peskine5ef5a9a2018-08-03 20:27:50 +0200165}
166/* END_CASE */
167
168/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void ctr_drbg_validate_pr(data_t *add_init, data_t *entropy,
170 data_t *add1, data_t *add2,
171 data_t *result_string)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000172{
Nir Sonnenscheinacedc912018-08-29 23:57:45 +0300173 data_t empty = { 0, 0 };
Valerio Settidc32ac22023-11-13 10:27:56 +0100174 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 ctr_drbg_validate_internal(RESEED_ALWAYS, add_init,
176 entropy->len / 3, entropy,
177 &empty, add1, add2,
178 result_string);
Valerio Settidc32ac22023-11-13 10:27:56 +0100179 AES_PSA_DONE();
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300180 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000181}
Paul Bakker33b43f12013-08-20 11:48:36 +0200182/* END_CASE */
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000183
Paul Bakker33b43f12013-08-20 11:48:36 +0200184/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185void ctr_drbg_validate_reseed_between(data_t *add_init, data_t *entropy,
186 data_t *add1, data_t *add_reseed,
187 data_t *add2, data_t *result_string)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000188{
Valerio Settidc32ac22023-11-13 10:27:56 +0100189 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ctr_drbg_validate_internal(RESEED_SECOND, add_init,
191 entropy->len / 2, entropy,
192 add_reseed, add1, add2,
193 result_string);
Valerio Settidc32ac22023-11-13 10:27:56 +0100194 AES_PSA_DONE();
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300195 goto exit; // goto is needed to avoid warning ( no test assertions in func)
Paul Bakker0e04d0e2011-11-27 14:46:59 +0000196}
Paul Bakker33b43f12013-08-20 11:48:36 +0200197/* END_CASE */
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100198
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100199/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200void ctr_drbg_validate_reseed_first(data_t *add_init, data_t *entropy,
201 data_t *add1, data_t *add_reseed,
202 data_t *add2, data_t *result_string)
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300203{
Valerio Settidc32ac22023-11-13 10:27:56 +0100204 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 ctr_drbg_validate_internal(RESEED_FIRST, add_init,
206 entropy->len / 2, entropy,
207 add_reseed, add1, add2,
208 result_string);
Valerio Settidc32ac22023-11-13 10:27:56 +0100209 AES_PSA_DONE();
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300210 goto exit; // goto is needed to avoid warning ( no test assertions in func)
211}
212/* END_CASE */
213
Gilles Peskine69971662019-10-23 19:39:36 +0200214/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215void ctr_drbg_entropy_strength(int expected_bit_strength)
Gilles Peskine69971662019-10-23 19:39:36 +0200216{
217 unsigned char entropy[/*initial entropy*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN +
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 /*nonce*/ MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN +
219 /*reseed*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN];
Gilles Peskine69971662019-10-23 19:39:36 +0200220 mbedtls_ctr_drbg_context ctx;
221 size_t last_idx;
222 size_t byte_strength = expected_bit_strength / 8;
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_ctr_drbg_init(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100225
226 AES_PSA_INIT();
Gilles Peskine69971662019-10-23 19:39:36 +0200227 test_offset_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 test_max_idx = sizeof(entropy);
229 memset(entropy, 0, sizeof(entropy));
Gilles Peskine69971662019-10-23 19:39:36 +0200230
231 /* The initial seeding must grab at least byte_strength bytes of entropy
232 * for the entropy input and byte_strength/2 bytes for a nonce. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx,
234 mbedtls_test_entropy_func, entropy,
235 NULL, 0) == 0);
236 TEST_ASSERT(test_offset_idx >= (byte_strength * 3 + 1) / 2);
Gilles Peskine69971662019-10-23 19:39:36 +0200237 last_idx = test_offset_idx;
238
239 /* A reseed must grab at least byte_strength bytes of entropy. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, NULL, 0) == 0);
241 TEST_ASSERT(test_offset_idx - last_idx >= byte_strength);
Gilles Peskine69971662019-10-23 19:39:36 +0200242
243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_ctr_drbg_free(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100245 AES_PSA_DONE();
Gilles Peskine69971662019-10-23 19:39:36 +0200246}
247/* END_CASE */
Nir Sonnenschein85fcb582018-08-29 23:38:57 +0300248
249/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250void ctr_drbg_entropy_usage(int entropy_nonce_len)
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100251{
252 unsigned char out[16];
253 unsigned char add[16];
254 unsigned char entropy[1024];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100256 size_t i, reps = 10;
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200257 size_t expected_idx = 0;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_ctr_drbg_init(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100260
261 AES_PSA_INIT();
262
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100263 test_offset_idx = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 test_max_idx = sizeof(entropy);
265 memset(entropy, 0, sizeof(entropy));
266 memset(out, 0, sizeof(out));
267 memset(add, 0, sizeof(add));
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (entropy_nonce_len >= 0) {
270 TEST_ASSERT(mbedtls_ctr_drbg_set_nonce_len(&ctx, entropy_nonce_len) == 0);
271 }
Gilles Peskinec949de02019-10-22 19:14:26 +0200272
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800273 /* Set reseed interval before seed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_ctr_drbg_set_reseed_interval(&ctx, 2 * reps);
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800275
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100276 /* Init must use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_entropy_func, entropy, NULL, 0) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200278 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (entropy_nonce_len >= 0) {
Gilles Peskinec949de02019-10-22 19:14:26 +0200280 expected_idx += entropy_nonce_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 } else {
Gilles Peskine69971662019-10-23 19:39:36 +0200282 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 }
284 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100285
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800286 /* By default, PR is off, and reseed interval was set to
287 * 2 * reps so the next few calls should not use entropy */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 for (i = 0; i < reps; i++) {
289 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out) - 4) == 0);
290 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out) - 4,
291 add, sizeof(add)) == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100292 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100294
295 /* While at it, make sure we didn't write past the requested length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 TEST_ASSERT(out[sizeof(out) - 4] == 0);
297 TEST_ASSERT(out[sizeof(out) - 3] == 0);
298 TEST_ASSERT(out[sizeof(out) - 2] == 0);
299 TEST_ASSERT(out[sizeof(out) - 1] == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100300
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800301 /* There have been 2 * reps calls to random. The next call should reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200303 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100305
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800306 /* Set reseed interval after seed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 mbedtls_ctr_drbg_set_reseed_interval(&ctx, 4 * reps + 1);
Gavin Acquroff6aceb512020-03-01 17:06:11 -0800308
309 /* The next few calls should not reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 for (i = 0; i < (2 * reps); i++) {
311 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
312 TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out),
313 add, sizeof(add)) == 0);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100314 }
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
Dave Rodgman6dd757a2023-02-02 12:40:50 +0000317 /* Call update with too much data (sizeof(entropy) > MAX(_SEED)_INPUT).
Gilles Peskined9199932018-09-11 16:41:54 +0200318 * Make sure it's detected as an error and doesn't cause memory
319 * corruption. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 TEST_ASSERT(mbedtls_ctr_drbg_update(
321 &ctx, entropy, sizeof(entropy)) != 0);
Manuel Pégourié-Gonnardf5f25b32014-11-27 14:04:56 +0100322
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100323 /* Now enable PR, so the next few calls should all reseed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON);
325 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200326 expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100328
329 /* Finally, check setting entropy_len */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_ctr_drbg_set_entropy_len(&ctx, 42);
331 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200332 expected_idx += 42;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 TEST_EQUAL(test_offset_idx, expected_idx);
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 mbedtls_ctr_drbg_set_entropy_len(&ctx, 13);
336 TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
Gilles Peskine58b56ce2019-10-22 19:10:01 +0200337 expected_idx += 13;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 TEST_EQUAL(test_offset_idx, expected_idx);
Paul Bakkera317a982014-06-18 16:44:11 +0200339
Paul Bakkerbd51b262014-07-10 15:26:12 +0200340exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 mbedtls_ctr_drbg_free(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100342 AES_PSA_DONE();
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100343}
344/* END_CASE */
345
Paul Elliott445af3c2023-12-11 18:05:32 +0000346/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD:!MBEDTLS_CTR_DRBG_USE_128_BIT_KEY:!MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Paul Elliottbb0e48f2023-12-01 18:05:19 +0000347void ctr_drbg_threads(data_t *expected_result, int reseed, int arg_thread_count)
Janos Follatha16ee6b2023-10-04 19:05:26 +0100348{
Paul Elliottbb0e48f2023-12-01 18:05:19 +0000349 size_t thread_count = (size_t) arg_thread_count;
350 pthread_t *threads = NULL;
Janos Follatha16ee6b2023-10-04 19:05:26 +0100351
352 unsigned char out[16];
Paul Elliottbda25dd2023-11-21 17:07:40 +0000353 unsigned char *entropy = NULL;
354
Paul Elliottbb0e48f2023-12-01 18:05:19 +0000355 const size_t n_random_calls = thread_count * thread_random_reps + 1;
Paul Elliottbda25dd2023-11-21 17:07:40 +0000356
Paul Elliottbda577b2024-02-06 17:49:20 +0000357 /* This is a known-answer test, and although tests use a mock entropy
358 * function the input entropy length will still affect the output.
359 * We therefore need to pick a fixed entropy length, rather than using the
360 * default entropy length (MBEDTLS_CTR_DRBG_ENTROPY_LEN). We've chosen to
361 * use the default value of MBEDTLS_CTR_DRBG_ENTROPY_LEN for SHA-512,
362 * as this was the value used when the expected answers were calculated. */
Paul Elliott40f0ec22023-12-11 17:40:54 +0000363 const size_t entropy_len = 48;
364
Paul Elliott22dbaf02023-12-18 18:18:04 +0000365 AES_PSA_INIT();
366
Paul Elliottbb0e48f2023-12-01 18:05:19 +0000367 TEST_CALLOC(threads, sizeof(pthread_t) * thread_count);
Janos Follatha16ee6b2023-10-04 19:05:26 +0100368 memset(out, 0, sizeof(out));
369
Janos Follatha16ee6b2023-10-04 19:05:26 +0100370 mbedtls_ctr_drbg_context ctx;
371 mbedtls_ctr_drbg_init(&ctx);
372
Paul Elliottbda25dd2023-11-21 17:07:40 +0000373 test_offset_idx = 0;
Janos Follatha16ee6b2023-10-04 19:05:26 +0100374
Paul Elliottbda577b2024-02-06 17:49:20 +0000375 /* Need to set a non-default fixed entropy len, to ensure same output across
376 * all configs - see above for details. */
Paul Elliott40f0ec22023-12-11 17:40:54 +0000377 mbedtls_ctr_drbg_set_entropy_len(&ctx, entropy_len);
378
Paul Elliottbda25dd2023-11-21 17:07:40 +0000379 if (reseed == 0) {
380 mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_OFF);
381 mbedtls_ctr_drbg_set_reseed_interval(&ctx, n_random_calls + 1);
382
Paul Elliott40f0ec22023-12-11 17:40:54 +0000383 TEST_CALLOC(entropy, entropy_len + MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN);
384 test_max_idx = entropy_len + MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
Paul Elliottbda25dd2023-11-21 17:07:40 +0000385 } else {
Paul Elliott40f0ec22023-12-11 17:40:54 +0000386 const size_t entropy_size = ((n_random_calls + 1) * entropy_len)
Paul Elliottfed410f2023-11-30 20:40:55 +0000387 + MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
Paul Elliottbda25dd2023-11-21 17:07:40 +0000388
389 mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON);
390
391 TEST_CALLOC(entropy, entropy_size);
392 test_max_idx = entropy_size;
393 }
Janos Follatha16ee6b2023-10-04 19:05:26 +0100394
395 TEST_EQUAL(
396 mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_entropy_func, entropy, NULL, 0),
397 0);
398
Paul Elliottbb0e48f2023-12-01 18:05:19 +0000399 for (size_t i = 0; i < thread_count; i++) {
Janos Follatha16ee6b2023-10-04 19:05:26 +0100400 TEST_EQUAL(
401 pthread_create(&threads[i], NULL,
Paul Elliott6a997c92023-11-30 14:47:17 +0000402 thread_random_function, (void *) &ctx),
Janos Follatha16ee6b2023-10-04 19:05:26 +0100403 0);
404 }
405
Paul Elliottbb0e48f2023-12-01 18:05:19 +0000406 for (size_t i = 0; i < thread_count; i++) {
Janos Follatha16ee6b2023-10-04 19:05:26 +0100407 TEST_EQUAL(pthread_join(threads[i], NULL), 0);
408 }
409
410 /* Take a last output for comparing and thus verifying the DRBG state */
411 TEST_EQUAL(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)), 0);
412
413 TEST_MEMORY_COMPARE(out, sizeof(out), expected_result->x, expected_result->len);
414
415exit:
416 mbedtls_ctr_drbg_free(&ctx);
Paul Elliottbda25dd2023-11-21 17:07:40 +0000417 mbedtls_free(entropy);
Paul Elliottbb0e48f2023-12-01 18:05:19 +0000418 mbedtls_free(threads);
Paul Elliott22dbaf02023-12-18 18:18:04 +0000419
420 AES_PSA_DONE();
Janos Follatha16ee6b2023-10-04 19:05:26 +0100421}
Janos Follatha16ee6b2023-10-04 19:05:26 +0100422/* END_CASE */
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425void ctr_drbg_seed_file(char *path, int ret)
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_ctr_drbg_context ctx;
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 mbedtls_ctr_drbg_init(&ctx);
Manuel Pégourié-Gonnard8d128ef2015-04-28 22:38:08 +0200430
Valerio Settidc32ac22023-11-13 10:27:56 +0100431 AES_PSA_INIT();
432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_rnd_std_rand,
434 NULL, NULL, 0) == 0);
435 TEST_ASSERT(mbedtls_ctr_drbg_write_seed_file(&ctx, path) == ret);
436 TEST_ASSERT(mbedtls_ctr_drbg_update_seed_file(&ctx, path) == ret);
Paul Bakkera317a982014-06-18 16:44:11 +0200437
Paul Bakkerbd51b262014-07-10 15:26:12 +0200438exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_ctr_drbg_free(&ctx);
Valerio Settidc32ac22023-11-13 10:27:56 +0100440 AES_PSA_DONE();
Manuel Pégourié-Gonnard7575daa2014-01-31 12:16:54 +0100441}
442/* END_CASE */
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100445void ctr_drbg_selftest()
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100446{
Valerio Settidc32ac22023-11-13 10:27:56 +0100447 AES_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 TEST_ASSERT(mbedtls_ctr_drbg_self_test(1) == 0);
Valerio Settidc32ac22023-11-13 10:27:56 +0100449 AES_PSA_DONE();
Manuel Pégourié-Gonnardb3b205e2014-01-31 12:04:06 +0100450}
451/* END_CASE */
Dave Rodgmanae730342024-01-13 17:31:13 +0000452
453/* BEGIN_CASE */
454void ctr_increment_rollover()
455{
456 uint8_t c[16];
457 uint8_t r[16];
458
459 // test all increments from 2^n - 1 to 2^n (i.e. where we roll over into the next bit)
460 for (int n = 0; n <= 128; n++) {
461 memset(c, 0, 16);
462 memset(r, 0, 16);
463
464 // set least significant (highest address) n bits to 1, i.e. generate (2^n - 1)
465 for (int i = 0; i < n; i++) {
466 int bit = i % 8;
467 int byte = (i / 8);
468 c[15 - byte] |= 1 << bit;
469 }
470 // increment to get 2^n
471 mbedtls_ctr_increment_counter(c);
472
473 // now generate a reference result equal to 2^n - i.e. set only bit (n + 1)
474 // if n == 127, this will not set any bits (i.e. wraps to 0).
475 int bit = n % 8;
476 int byte = n / 8;
477 if (byte < 16) {
478 r[15 - byte] = 1 << bit;
479 }
480
481 TEST_MEMORY_COMPARE(c, 16, r, 16);
482 }
483
484 uint64_t lsb = 10, msb = 20;
485 MBEDTLS_PUT_UINT64_BE(msb, c, 0);
486 MBEDTLS_PUT_UINT64_BE(lsb, c, 8);
487 memcpy(r, c, 16);
488 mbedtls_ctr_increment_counter(c);
489 for (int i = 15; i >= 0; i--) {
490 r[i] += 1;
491 if (r[i] != 0) {
492 break;
493 }
494 }
495 TEST_MEMORY_COMPARE(c, 16, r, 16);
496}
497/* END_CASE */
498
499/* BEGIN_CASE */
500void ctr_increment(data_t *x)
501{
502 uint8_t c[16];
503 uint8_t r[16];
504
505 // initialise c and r from test argument
506 memset(c, 0, 16);
507 memcpy(c, x->x, x->len);
508 memcpy(r, c, 16);
509
510 // increment c
511 mbedtls_ctr_increment_counter(c);
512 // increment reference
513 for (int i = 15; i >= 0; i--) {
514 r[i] += 1;
515 if (r[i] != 0) {
516 break;
517 }
518 }
519
520 // test that mbedtls_ctr_increment_counter behaviour matches reference
521 TEST_MEMORY_COMPARE(c, 16, r, 16);
522}
523/* END_CASE */