2021-02-03 18:55:39 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
|
|
|
|
/* Test random generation as a whole. */
|
|
|
|
|
2021-02-03 20:04:08 +01:00
|
|
|
#include "mbedtls/bignum.h"
|
2021-02-03 18:55:39 +01:00
|
|
|
#include "mbedtls/ctr_drbg.h"
|
2021-02-03 20:04:08 +01:00
|
|
|
#include "mbedtls/ecdsa.h"
|
2021-02-03 18:55:39 +01:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
#include "mbedtls/hmac_drbg.h"
|
2021-02-03 20:04:08 +01:00
|
|
|
#include "mbedtls/psa_util.h"
|
2021-02-03 18:55:39 +01:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
|
|
|
|
/* How many bytes to generate in each test case for repeated generation.
|
|
|
|
* This must be high enough that the probability of generating the same
|
|
|
|
* output twice is infinitesimal, but low enough that random generators
|
|
|
|
* are willing to deliver that much. */
|
|
|
|
#define OUTPUT_SIZE 32
|
|
|
|
|
|
|
|
/* END_HEADER */
|
|
|
|
|
2023-04-28 23:41:38 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:!MBEDTLS_PSA_INJECT_ENTROPY:MBEDTLS_CTR_DRBG_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void random_twice_with_ctr_drbg()
|
2021-02-03 18:55:39 +01:00
|
|
|
{
|
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_ctr_drbg_context drbg;
|
|
|
|
unsigned char output1[OUTPUT_SIZE];
|
|
|
|
unsigned char output2[OUTPUT_SIZE];
|
|
|
|
|
2023-11-13 10:27:56 +01:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2023-03-21 18:09:40 +01:00
|
|
|
MD_PSA_INIT();
|
2023-11-13 10:27:56 +01:00
|
|
|
#else
|
|
|
|
USE_PSA_INIT();
|
|
|
|
#endif
|
|
|
|
|
2023-03-21 18:09:40 +01:00
|
|
|
|
2021-02-03 18:55:39 +01:00
|
|
|
/* First round */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&entropy);
|
|
|
|
mbedtls_ctr_drbg_init(&drbg);
|
|
|
|
TEST_EQUAL(0, mbedtls_ctr_drbg_seed(&drbg,
|
|
|
|
mbedtls_entropy_func, &entropy,
|
|
|
|
NULL, 0));
|
|
|
|
TEST_EQUAL(0, mbedtls_ctr_drbg_random(&drbg,
|
|
|
|
output1, sizeof(output1)));
|
|
|
|
mbedtls_ctr_drbg_free(&drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
/* Second round */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&entropy);
|
|
|
|
mbedtls_ctr_drbg_init(&drbg);
|
|
|
|
TEST_EQUAL(0, mbedtls_ctr_drbg_seed(&drbg,
|
|
|
|
mbedtls_entropy_func, &entropy,
|
|
|
|
NULL, 0));
|
|
|
|
TEST_EQUAL(0, mbedtls_ctr_drbg_random(&drbg,
|
|
|
|
output2, sizeof(output2)));
|
|
|
|
mbedtls_ctr_drbg_free(&drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
/* The two rounds must generate different random data. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(output1, output2, OUTPUT_SIZE) != 0);
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_free(&drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
2023-11-13 10:27:56 +01:00
|
|
|
#if defined(MBEDTLS_AES_C)
|
2023-03-21 18:09:40 +01:00
|
|
|
MD_PSA_DONE();
|
2023-11-13 10:27:56 +01:00
|
|
|
#else
|
|
|
|
USE_PSA_DONE();
|
|
|
|
#endif
|
2021-02-03 18:55:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2023-04-28 23:41:38 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:!MBEDTLS_PSA_INJECT_ENTROPY:MBEDTLS_HMAC_DRBG_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void random_twice_with_hmac_drbg(int md_type)
|
2021-02-03 18:55:39 +01:00
|
|
|
{
|
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_hmac_drbg_context drbg;
|
|
|
|
unsigned char output1[OUTPUT_SIZE];
|
|
|
|
unsigned char output2[OUTPUT_SIZE];
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type);
|
2021-02-03 18:55:39 +01:00
|
|
|
|
2023-03-21 18:09:40 +01:00
|
|
|
MD_PSA_INIT();
|
|
|
|
|
2021-02-03 18:55:39 +01:00
|
|
|
/* First round */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&entropy);
|
|
|
|
mbedtls_hmac_drbg_init(&drbg);
|
|
|
|
TEST_EQUAL(0, mbedtls_hmac_drbg_seed(&drbg, md_info,
|
|
|
|
mbedtls_entropy_func, &entropy,
|
|
|
|
NULL, 0));
|
|
|
|
TEST_EQUAL(0, mbedtls_hmac_drbg_random(&drbg,
|
|
|
|
output1, sizeof(output1)));
|
|
|
|
mbedtls_hmac_drbg_free(&drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
/* Second round */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_entropy_init(&entropy);
|
|
|
|
mbedtls_hmac_drbg_init(&drbg);
|
|
|
|
TEST_EQUAL(0, mbedtls_hmac_drbg_seed(&drbg, md_info,
|
|
|
|
mbedtls_entropy_func, &entropy,
|
|
|
|
NULL, 0));
|
|
|
|
TEST_EQUAL(0, mbedtls_hmac_drbg_random(&drbg,
|
|
|
|
output2, sizeof(output2)));
|
|
|
|
mbedtls_hmac_drbg_free(&drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
/* The two rounds must generate different random data. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(output1, output2, OUTPUT_SIZE) != 0);
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_free(&drbg);
|
|
|
|
mbedtls_entropy_free(&entropy);
|
2023-03-21 18:09:40 +01:00
|
|
|
MD_PSA_DONE();
|
2021-02-03 18:55:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-30 13:28:22 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void random_twice_with_psa_from_classic()
|
2021-02-03 20:04:08 +01:00
|
|
|
{
|
|
|
|
unsigned char output1[OUTPUT_SIZE];
|
|
|
|
unsigned char output2[OUTPUT_SIZE];
|
|
|
|
|
|
|
|
/* First round */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
TEST_EQUAL(0, mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
output1, sizeof(output1)));
|
|
|
|
PSA_DONE();
|
2021-02-03 20:04:08 +01:00
|
|
|
|
|
|
|
/* Second round */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
TEST_EQUAL(0, mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
output2, sizeof(output2)));
|
|
|
|
PSA_DONE();
|
2021-02-03 20:04:08 +01:00
|
|
|
|
|
|
|
/* The two rounds must generate different random data. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(output1, output2, OUTPUT_SIZE) != 0);
|
2021-02-03 20:04:08 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_DONE();
|
2021-02-03 20:04:08 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2021-04-30 13:28:22 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
2023-01-11 14:50:10 +01:00
|
|
|
void random_twice_with_psa_from_psa()
|
2021-02-03 18:55:39 +01:00
|
|
|
{
|
|
|
|
unsigned char output1[OUTPUT_SIZE];
|
|
|
|
unsigned char output2[OUTPUT_SIZE];
|
|
|
|
|
|
|
|
/* First round */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
PSA_ASSERT(psa_generate_random(output1, sizeof(output1)));
|
|
|
|
PSA_DONE();
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
/* Second round */
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
PSA_ASSERT(psa_generate_random(output2, sizeof(output2)));
|
|
|
|
PSA_DONE();
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
/* The two rounds must generate different random data. */
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(memcmp(output1, output2, OUTPUT_SIZE) != 0);
|
2021-02-03 18:55:39 +01:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_DONE();
|
2021-02-03 18:55:39 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2021-02-03 20:04:08 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_psa_get_random_no_init()
|
2021-02-03 20:04:08 +01:00
|
|
|
{
|
|
|
|
unsigned char output[1];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_ASSERT(mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
output, sizeof(output)) != 0);
|
2021-02-03 20:04:08 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_psa_get_random_length(int n)
|
2021-02-03 20:04:08 +01:00
|
|
|
{
|
|
|
|
unsigned char *output = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
PSA_ASSERT(psa_crypto_init());
|
2023-07-21 12:31:13 +02:00
|
|
|
TEST_CALLOC(output, n);
|
2021-02-03 20:04:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
TEST_EQUAL(0, mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
output, n));
|
2021-02-03 20:04:08 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(output);
|
|
|
|
PSA_DONE();
|
2021-02-03 20:04:08 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_ECDSA_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_psa_get_random_ecdsa_sign(int curve)
|
2021-02-03 20:04:08 +01:00
|
|
|
{
|
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_mpi d, r, s;
|
|
|
|
unsigned char buf[] = "This is not a hash.";
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_mpi_init(&d);
|
|
|
|
mbedtls_mpi_init(&r);
|
|
|
|
mbedtls_mpi_init(&s);
|
|
|
|
|
|
|
|
TEST_EQUAL(0, mbedtls_mpi_lset(&d, 123456789));
|
|
|
|
TEST_EQUAL(0, mbedtls_ecp_group_load(&grp, curve));
|
|
|
|
PSA_ASSERT(psa_crypto_init());
|
|
|
|
TEST_EQUAL(0, mbedtls_ecdsa_sign(&grp, &r, &s, &d,
|
|
|
|
buf, sizeof(buf),
|
|
|
|
mbedtls_psa_get_random,
|
|
|
|
MBEDTLS_PSA_RANDOM_STATE));
|
2021-02-03 20:04:08 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&d);
|
|
|
|
mbedtls_mpi_free(&r);
|
|
|
|
mbedtls_mpi_free(&s);
|
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
PSA_DONE();
|
2021-02-03 20:04:08 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|