2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2018-08-03 20:23:09 +02:00
|
|
|
#include "mbedtls/entropy.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ctr_drbg.h"
|
2017-04-06 12:55:43 +02:00
|
|
|
#include "string.h"
|
2015-02-06 14:43:58 +01:00
|
|
|
|
2023-11-21 15:46:51 +01:00
|
|
|
#if defined(MBEDTLS_THREADING_PTHREAD)
|
|
|
|
#include "mbedtls/threading.h"
|
|
|
|
#endif
|
|
|
|
|
2018-08-03 20:27:50 +02:00
|
|
|
/* Modes for ctr_drbg_validate */
|
2023-01-11 14:50:10 +01:00
|
|
|
enum reseed_mode {
|
2018-08-03 20:27:50 +02:00
|
|
|
RESEED_NEVER, /* never reseed */
|
|
|
|
RESEED_FIRST, /* instantiate, reseed, generate, generate */
|
|
|
|
RESEED_SECOND, /* instantiate, generate, reseed, generate */
|
|
|
|
RESEED_ALWAYS /* prediction resistance, no explicit reseed */
|
|
|
|
};
|
|
|
|
|
2018-08-29 22:38:57 +02:00
|
|
|
static size_t test_offset_idx = 0;
|
|
|
|
static size_t test_max_idx = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mbedtls_test_entropy_func(void *data, unsigned char *buf, size_t len)
|
2011-11-27 15:46:59 +01:00
|
|
|
{
|
2014-01-31 12:16:54 +01:00
|
|
|
const unsigned char *p = (unsigned char *) data;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (test_offset_idx + len > test_max_idx) {
|
|
|
|
return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
|
|
|
|
}
|
|
|
|
memcpy(buf, p + test_offset_idx, len);
|
2014-01-31 12:16:54 +01:00
|
|
|
test_offset_idx += len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2011-11-27 15:46:59 +01:00
|
|
|
}
|
2016-07-14 14:21:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ctr_drbg_validate_internal(int reseed_mode, data_t *nonce,
|
|
|
|
int entropy_len_arg, data_t *entropy,
|
|
|
|
data_t *reseed,
|
|
|
|
data_t *add1, data_t *add2,
|
|
|
|
data_t *result)
|
2018-08-03 20:24:54 +02:00
|
|
|
{
|
|
|
|
mbedtls_ctr_drbg_context ctx;
|
2023-10-17 16:35:20 +02:00
|
|
|
mbedtls_ctr_drbg_init(&ctx);
|
2018-08-03 20:27:50 +02:00
|
|
|
unsigned char buf[64];
|
2018-08-29 09:25:30 +02:00
|
|
|
|
2018-08-03 20:27:50 +02:00
|
|
|
size_t entropy_chunk_len = (size_t) entropy_len_arg;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(entropy_chunk_len <= sizeof(buf));
|
2018-08-29 22:38:57 +02:00
|
|
|
|
2018-08-03 20:27:50 +02:00
|
|
|
test_offset_idx = 0;
|
2018-08-29 09:25:30 +02:00
|
|
|
test_max_idx = entropy->len;
|
2018-08-03 20:24:54 +02:00
|
|
|
|
2018-08-29 22:38:57 +02:00
|
|
|
/* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>)
|
|
|
|
* where nonce||perso = nonce[nonce->len] */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_set_entropy_len(&ctx, entropy_chunk_len);
|
|
|
|
mbedtls_ctr_drbg_set_nonce_len(&ctx, 0);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_seed(
|
|
|
|
&ctx,
|
|
|
|
mbedtls_test_entropy_func, entropy->x,
|
|
|
|
nonce->x, nonce->len) == 0);
|
|
|
|
if (reseed_mode == RESEED_ALWAYS) {
|
2018-08-03 20:27:50 +02:00
|
|
|
mbedtls_ctr_drbg_set_prediction_resistance(
|
|
|
|
&ctx,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_CTR_DRBG_PR_ON);
|
|
|
|
}
|
2018-08-03 20:27:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reseed_mode == RESEED_FIRST) {
|
2018-08-29 22:38:57 +02:00
|
|
|
/* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
|
|
|
|
* reseed[:reseed->len]) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_reseed(
|
|
|
|
&ctx,
|
|
|
|
reseed->x, reseed->len) == 0);
|
2018-08-03 20:27:50 +02:00
|
|
|
}
|
2018-08-03 20:24:54 +02:00
|
|
|
|
2018-08-29 22:38:57 +02:00
|
|
|
/* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1->len]) -> buf */
|
2018-08-03 20:27:50 +02:00
|
|
|
/* Then reseed if prediction resistance is enabled. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(
|
|
|
|
&ctx,
|
|
|
|
buf, result->len,
|
|
|
|
add1->x, add1->len) == 0);
|
2018-08-03 20:27:50 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (reseed_mode == RESEED_SECOND) {
|
2018-08-29 22:38:57 +02:00
|
|
|
/* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
|
|
|
|
* reseed[:reseed->len]) */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_reseed(
|
|
|
|
&ctx,
|
|
|
|
reseed->x, reseed->len) == 0);
|
2018-08-03 20:27:50 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 09:25:30 +02:00
|
|
|
/* CTR_DRBG_Generate(result->len * 8 bits, add2->x[:add2->len]) -> buf */
|
2018-08-03 20:27:50 +02:00
|
|
|
/* Then reseed if prediction resistance is enabled. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(
|
|
|
|
&ctx,
|
|
|
|
buf, result->len,
|
|
|
|
add2->x, add2->len) == 0);
|
|
|
|
TEST_ASSERT(memcmp(buf, result->x, result->len) == 0);
|
2018-08-03 20:24:54 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_free(&ctx);
|
2018-08-29 09:25:30 +02:00
|
|
|
}
|
|
|
|
|
2023-10-04 20:05:26 +02:00
|
|
|
static const int thread_random_reps = 10;
|
2023-11-30 15:47:17 +01:00
|
|
|
void *thread_random_function(void *ctx)
|
2023-10-04 20:05:26 +02:00
|
|
|
{
|
|
|
|
unsigned char out[16];
|
|
|
|
memset(out, 0, sizeof(out));
|
|
|
|
|
2023-11-30 15:47:17 +01:00
|
|
|
for (int i = 0; i < thread_random_reps; i++) {
|
|
|
|
TEST_EQUAL(mbedtls_ctr_drbg_random((mbedtls_ctr_drbg_context *) ctx, out, sizeof(out)), 0);
|
2023-10-04 20:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-08-29 09:25:30 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_CTR_DRBG_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_special_behaviours()
|
2018-08-29 09:25:30 +02:00
|
|
|
{
|
|
|
|
mbedtls_ctr_drbg_context ctx;
|
|
|
|
unsigned char output[512];
|
|
|
|
unsigned char additional[512];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&ctx);
|
|
|
|
memset(output, 0, sizeof(output));
|
|
|
|
memset(additional, 0, sizeof(additional));
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx,
|
|
|
|
output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1,
|
|
|
|
additional, 16) ==
|
|
|
|
MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx,
|
|
|
|
output, 16,
|
|
|
|
additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1) ==
|
|
|
|
MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
|
|
|
|
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, additional,
|
|
|
|
MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1) ==
|
|
|
|
MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
|
|
|
|
|
|
|
|
mbedtls_ctr_drbg_set_entropy_len(&ctx, ~0);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, additional,
|
|
|
|
MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) ==
|
|
|
|
MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG);
|
2018-08-29 09:25:30 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_free(&ctx);
|
2018-08-29 09:25:30 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2011-11-27 15:46:59 +01:00
|
|
|
|
2018-08-03 20:27:50 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_validate_no_reseed(data_t *add_init, data_t *entropy,
|
|
|
|
data_t *add1, data_t *add2,
|
|
|
|
data_t *result_string)
|
2018-08-03 20:27:50 +02:00
|
|
|
{
|
2018-08-29 22:57:45 +02:00
|
|
|
data_t empty = { 0, 0 };
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
ctr_drbg_validate_internal(RESEED_NEVER, add_init,
|
|
|
|
entropy->len, entropy,
|
|
|
|
&empty, add1, add2,
|
|
|
|
result_string);
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_DONE();
|
2018-08-29 22:38:57 +02:00
|
|
|
goto exit; // goto is needed to avoid warning ( no test assertions in func)
|
2018-08-03 20:27:50 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2014-06-18 16:44:11 +02:00
|
|
|
|
2018-08-03 20:27:50 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_validate_pr(data_t *add_init, data_t *entropy,
|
|
|
|
data_t *add1, data_t *add2,
|
|
|
|
data_t *result_string)
|
2018-08-03 20:27:50 +02:00
|
|
|
{
|
2018-08-29 22:57:45 +02:00
|
|
|
data_t empty = { 0, 0 };
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
ctr_drbg_validate_internal(RESEED_ALWAYS, add_init,
|
|
|
|
entropy->len / 3, entropy,
|
|
|
|
&empty, add1, add2,
|
|
|
|
result_string);
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_DONE();
|
2018-08-29 22:38:57 +02:00
|
|
|
goto exit; // goto is needed to avoid warning ( no test assertions in func)
|
2011-11-27 15:46:59 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2011-11-27 15:46:59 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_validate_reseed_between(data_t *add_init, data_t *entropy,
|
|
|
|
data_t *add1, data_t *add_reseed,
|
|
|
|
data_t *add2, data_t *result_string)
|
2011-11-27 15:46:59 +01:00
|
|
|
{
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
ctr_drbg_validate_internal(RESEED_SECOND, add_init,
|
|
|
|
entropy->len / 2, entropy,
|
|
|
|
add_reseed, add1, add2,
|
|
|
|
result_string);
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_DONE();
|
2018-08-29 22:38:57 +02:00
|
|
|
goto exit; // goto is needed to avoid warning ( no test assertions in func)
|
2011-11-27 15:46:59 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|
2014-01-31 12:04:06 +01:00
|
|
|
|
2018-08-29 22:38:57 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_validate_reseed_first(data_t *add_init, data_t *entropy,
|
|
|
|
data_t *add1, data_t *add_reseed,
|
|
|
|
data_t *add2, data_t *result_string)
|
2018-08-29 22:38:57 +02:00
|
|
|
{
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
ctr_drbg_validate_internal(RESEED_FIRST, add_init,
|
|
|
|
entropy->len / 2, entropy,
|
|
|
|
add_reseed, add1, add2,
|
|
|
|
result_string);
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_DONE();
|
2018-08-29 22:38:57 +02:00
|
|
|
goto exit; // goto is needed to avoid warning ( no test assertions in func)
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-10-23 19:39:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_entropy_strength(int expected_bit_strength)
|
2019-10-23 19:39:36 +02:00
|
|
|
{
|
|
|
|
unsigned char entropy[/*initial entropy*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN +
|
2023-01-11 14:50:10 +01:00
|
|
|
/*nonce*/ MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN +
|
|
|
|
/*reseed*/ MBEDTLS_CTR_DRBG_ENTROPY_LEN];
|
2019-10-23 19:39:36 +02:00
|
|
|
mbedtls_ctr_drbg_context ctx;
|
|
|
|
size_t last_idx;
|
|
|
|
size_t byte_strength = expected_bit_strength / 8;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&ctx);
|
2023-11-13 10:27:56 +01:00
|
|
|
|
|
|
|
AES_PSA_INIT();
|
2019-10-23 19:39:36 +02:00
|
|
|
test_offset_idx = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
test_max_idx = sizeof(entropy);
|
|
|
|
memset(entropy, 0, sizeof(entropy));
|
2018-08-29 22:38:57 +02:00
|
|
|
|
2019-10-23 19:39:36 +02:00
|
|
|
/* The initial seeding must grab at least byte_strength bytes of entropy
|
|
|
|
* for the entropy input and byte_strength/2 bytes for a nonce. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx,
|
|
|
|
mbedtls_test_entropy_func, entropy,
|
|
|
|
NULL, 0) == 0);
|
|
|
|
TEST_ASSERT(test_offset_idx >= (byte_strength * 3 + 1) / 2);
|
2019-10-23 19:39:36 +02:00
|
|
|
last_idx = test_offset_idx;
|
|
|
|
|
|
|
|
/* A reseed must grab at least byte_strength bytes of entropy. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_reseed(&ctx, NULL, 0) == 0);
|
|
|
|
TEST_ASSERT(test_offset_idx - last_idx >= byte_strength);
|
2019-10-23 19:39:36 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_free(&ctx);
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_DONE();
|
2019-10-23 19:39:36 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2018-08-29 22:38:57 +02:00
|
|
|
|
2014-01-31 12:16:54 +01:00
|
|
|
/* BEGIN_CASE */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_entropy_usage(int entropy_nonce_len)
|
2014-01-31 12:16:54 +01:00
|
|
|
{
|
|
|
|
unsigned char out[16];
|
|
|
|
unsigned char add[16];
|
|
|
|
unsigned char entropy[1024];
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ctr_drbg_context ctx;
|
2014-01-31 12:16:54 +01:00
|
|
|
size_t i, reps = 10;
|
2019-10-22 19:10:01 +02:00
|
|
|
size_t expected_idx = 0;
|
2014-01-31 12:16:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&ctx);
|
2023-11-13 10:27:56 +01:00
|
|
|
|
|
|
|
AES_PSA_INIT();
|
|
|
|
|
2014-01-31 12:16:54 +01:00
|
|
|
test_offset_idx = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
test_max_idx = sizeof(entropy);
|
|
|
|
memset(entropy, 0, sizeof(entropy));
|
|
|
|
memset(out, 0, sizeof(out));
|
|
|
|
memset(add, 0, sizeof(add));
|
2014-01-31 12:16:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (entropy_nonce_len >= 0) {
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_set_nonce_len(&ctx, entropy_nonce_len) == 0);
|
|
|
|
}
|
2019-10-22 19:14:26 +02:00
|
|
|
|
2020-03-02 02:06:11 +01:00
|
|
|
/* Set reseed interval before seed */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_set_reseed_interval(&ctx, 2 * reps);
|
2020-03-02 02:06:11 +01:00
|
|
|
|
2014-01-31 12:16:54 +01:00
|
|
|
/* Init must use entropy */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_entropy_func, entropy, NULL, 0) == 0);
|
2019-10-22 19:10:01 +02:00
|
|
|
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (entropy_nonce_len >= 0) {
|
2019-10-22 19:14:26 +02:00
|
|
|
expected_idx += entropy_nonce_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2019-10-23 19:39:36 +02:00
|
|
|
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
TEST_EQUAL(test_offset_idx, expected_idx);
|
2014-01-31 12:16:54 +01:00
|
|
|
|
2020-03-02 02:06:11 +01:00
|
|
|
/* By default, PR is off, and reseed interval was set to
|
|
|
|
* 2 * reps so the next few calls should not use entropy */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < reps; i++) {
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out) - 4) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out) - 4,
|
|
|
|
add, sizeof(add)) == 0);
|
2014-01-31 12:16:54 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(test_offset_idx, expected_idx);
|
2014-01-31 12:16:54 +01:00
|
|
|
|
|
|
|
/* While at it, make sure we didn't write past the requested length */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(out[sizeof(out) - 4] == 0);
|
|
|
|
TEST_ASSERT(out[sizeof(out) - 3] == 0);
|
|
|
|
TEST_ASSERT(out[sizeof(out) - 2] == 0);
|
|
|
|
TEST_ASSERT(out[sizeof(out) - 1] == 0);
|
2014-01-31 12:16:54 +01:00
|
|
|
|
2020-03-02 02:06:11 +01:00
|
|
|
/* There have been 2 * reps calls to random. The next call should reseed */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
|
2019-10-22 19:10:01 +02:00
|
|
|
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(test_offset_idx, expected_idx);
|
2014-01-31 12:16:54 +01:00
|
|
|
|
2020-03-02 02:06:11 +01:00
|
|
|
/* Set reseed interval after seed */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_set_reseed_interval(&ctx, 4 * reps + 1);
|
2020-03-02 02:06:11 +01:00
|
|
|
|
|
|
|
/* The next few calls should not reseed */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < (2 * reps); i++) {
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random_with_add(&ctx, out, sizeof(out),
|
|
|
|
add, sizeof(add)) == 0);
|
2014-01-31 12:16:54 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(test_offset_idx, expected_idx);
|
2014-01-31 12:16:54 +01:00
|
|
|
|
2023-02-02 13:40:50 +01:00
|
|
|
/* Call update with too much data (sizeof(entropy) > MAX(_SEED)_INPUT).
|
2018-09-11 16:41:54 +02:00
|
|
|
* Make sure it's detected as an error and doesn't cause memory
|
|
|
|
* corruption. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_update(
|
|
|
|
&ctx, entropy, sizeof(entropy)) != 0);
|
2014-11-27 14:04:56 +01:00
|
|
|
|
2014-01-31 12:16:54 +01:00
|
|
|
/* Now enable PR, so the next few calls should all reseed */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
|
2019-10-22 19:10:01 +02:00
|
|
|
expected_idx += MBEDTLS_CTR_DRBG_ENTROPY_LEN;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(test_offset_idx, expected_idx);
|
2014-01-31 12:16:54 +01:00
|
|
|
|
|
|
|
/* Finally, check setting entropy_len */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_set_entropy_len(&ctx, 42);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
|
2019-10-22 19:10:01 +02:00
|
|
|
expected_idx += 42;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(test_offset_idx, expected_idx);
|
2014-01-31 12:16:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_set_entropy_len(&ctx, 13);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)) == 0);
|
2019-10-22 19:10:01 +02:00
|
|
|
expected_idx += 13;
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(test_offset_idx, expected_idx);
|
2014-06-18 16:44:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_free(&ctx);
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_DONE();
|
2014-01-31 12:16:54 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-10-04 20:05:26 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_THREADING_PTHREAD */
|
2023-12-01 19:05:19 +01:00
|
|
|
void ctr_drbg_threads(data_t *expected_result, int reseed, int arg_thread_count)
|
2023-10-04 20:05:26 +02:00
|
|
|
{
|
2023-12-01 19:05:19 +01:00
|
|
|
size_t thread_count = (size_t) arg_thread_count;
|
|
|
|
pthread_t *threads = NULL;
|
2023-10-04 20:05:26 +02:00
|
|
|
|
|
|
|
unsigned char out[16];
|
2023-11-21 18:07:40 +01:00
|
|
|
unsigned char *entropy = NULL;
|
2023-10-04 20:05:26 +02:00
|
|
|
|
2023-12-01 19:05:19 +01:00
|
|
|
const size_t n_random_calls = thread_count * thread_random_reps + 1;
|
2023-10-04 20:05:26 +02:00
|
|
|
|
2023-12-11 18:40:54 +01:00
|
|
|
/* Based on the size of MBEDTLS_CTR_DRBG_ENTROPY_LEN for SHA512. */
|
|
|
|
const size_t entropy_len = 48;
|
|
|
|
|
2023-12-01 19:05:19 +01:00
|
|
|
TEST_CALLOC(threads, sizeof(pthread_t) * thread_count);
|
2023-11-21 18:07:40 +01:00
|
|
|
memset(out, 0, sizeof(out));
|
2023-10-04 20:05:26 +02:00
|
|
|
|
|
|
|
mbedtls_ctr_drbg_context ctx;
|
|
|
|
mbedtls_ctr_drbg_init(&ctx);
|
|
|
|
|
2023-11-21 18:07:40 +01:00
|
|
|
test_offset_idx = 0;
|
|
|
|
|
2023-12-11 18:40:54 +01:00
|
|
|
/* Need to do this, otherwise if we are forced into using SHA256 for
|
|
|
|
* whaever reason, output will differ. */
|
|
|
|
mbedtls_ctr_drbg_set_entropy_len(&ctx, entropy_len);
|
|
|
|
|
2023-11-21 18:07:40 +01:00
|
|
|
if (reseed == 0) {
|
|
|
|
mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_OFF);
|
|
|
|
mbedtls_ctr_drbg_set_reseed_interval(&ctx, n_random_calls + 1);
|
|
|
|
|
2023-12-11 18:40:54 +01:00
|
|
|
TEST_CALLOC(entropy, entropy_len + MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN);
|
|
|
|
test_max_idx = entropy_len + MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
|
2023-11-21 18:07:40 +01:00
|
|
|
} else {
|
2023-12-11 18:40:54 +01:00
|
|
|
const size_t entropy_size = ((n_random_calls + 1) * entropy_len)
|
2023-11-30 21:40:55 +01:00
|
|
|
+ MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN;
|
2023-11-21 18:07:40 +01:00
|
|
|
|
|
|
|
mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON);
|
2023-10-04 20:05:26 +02:00
|
|
|
|
2023-11-21 18:07:40 +01:00
|
|
|
TEST_CALLOC(entropy, entropy_size);
|
|
|
|
test_max_idx = entropy_size;
|
|
|
|
}
|
2023-10-04 20:05:26 +02:00
|
|
|
|
|
|
|
TEST_EQUAL(
|
|
|
|
mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_entropy_func, entropy, NULL, 0),
|
|
|
|
0);
|
|
|
|
|
2023-12-01 19:05:19 +01:00
|
|
|
for (size_t i = 0; i < thread_count; i++) {
|
2023-10-04 20:05:26 +02:00
|
|
|
TEST_EQUAL(
|
|
|
|
pthread_create(&threads[i], NULL,
|
2023-11-30 15:47:17 +01:00
|
|
|
thread_random_function, (void *) &ctx),
|
2023-10-04 20:05:26 +02:00
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2023-12-01 19:05:19 +01:00
|
|
|
for (size_t i = 0; i < thread_count; i++) {
|
2023-10-04 20:05:26 +02:00
|
|
|
TEST_EQUAL(pthread_join(threads[i], NULL), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take a last output for comparing and thus verifying the DRBG state */
|
|
|
|
TEST_EQUAL(mbedtls_ctr_drbg_random(&ctx, out, sizeof(out)), 0);
|
|
|
|
|
|
|
|
TEST_MEMORY_COMPARE(out, sizeof(out), expected_result->x, expected_result->len);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ctr_drbg_free(&ctx);
|
2023-11-21 18:07:40 +01:00
|
|
|
mbedtls_free(entropy);
|
2023-12-01 19:05:19 +01:00
|
|
|
mbedtls_free(threads);
|
2023-10-04 20:05:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_seed_file(char *path, int ret)
|
2014-01-31 12:16:54 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ctr_drbg_context ctx;
|
2014-01-31 12:16:54 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(&ctx);
|
2015-04-28 22:38:08 +02:00
|
|
|
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_INIT();
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_seed(&ctx, mbedtls_test_rnd_std_rand,
|
|
|
|
NULL, NULL, 0) == 0);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_write_seed_file(&ctx, path) == ret);
|
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_update_seed_file(&ctx, path) == ret);
|
2014-06-18 16:44:11 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_free(&ctx);
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_DONE();
|
2014-01-31 12:16:54 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
|
2023-01-11 14:50:10 +01:00
|
|
|
void ctr_drbg_selftest()
|
2014-01-31 12:04:06 +01:00
|
|
|
{
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_INIT();
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_ctr_drbg_self_test(1) == 0);
|
2023-11-13 10:27:56 +01:00
|
|
|
AES_PSA_DONE();
|
2014-01-31 12:04:06 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|