Skip to content

Commit

Permalink
subsys/random: Add cryptographically secure random test cases
Browse files Browse the repository at this point in the history
Updated tests/crypto/rand32/ to include cryptographic test cases.
Added config file for rand32_ctr_drbg generator.

Signed-off-by: David Leach <[email protected]>
  • Loading branch information
dleach02 authored and galak committed Nov 5, 2019
1 parent afdc63f commit 1bb1b3e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
3 changes: 3 additions & 0 deletions tests/crypto/rand32/prj_ctr_drbg.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_ZTEST=y
CONFIG_LOG=y
CONFIG_ENTROPY_GENERATOR=y
48 changes: 45 additions & 3 deletions tests/crypto/rand32/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void test_rand32(void)
u32_t gen, last_gen;
int rnd_cnt;
int equal_count = 0;
u32_t buf[N_VALUES];

/* Test early boot random number generation function */
last_gen = z_early_boot_rand32_get();
Expand Down Expand Up @@ -64,14 +65,55 @@ void test_rand32(void)
zassert_false((equal_count > N_VALUES / 2),
"random numbers returned same value with high probability");
}

printk("Generating bulk fill random numbers\n");
memset(buf, 0, sizeof(buf));
sys_rand_get((u8_t *)(&buf[0]), sizeof(buf));

for (rnd_cnt = 0; rnd_cnt < (N_VALUES - 1); rnd_cnt++) {
gen = buf[rnd_cnt];
if (gen == last_gen) {
equal_count++;
}
last_gen = gen;
}

if (equal_count > N_VALUES / 2) {
zassert_false((equal_count > N_VALUES / 2),
"random numbers returned same value with high probability");
}

#if defined(CONFIG_CSPRING_ENABLED)

printk("Generating bulk fill cryptographically secure random numbers\n");

memset(buf, 0, sizeof(buf));
sys_csrand_get(buf, sizeof(buf));

for (rnd_cnt = 0; rnd_cnt < (N_VALUES - 1); rnd_cnt++) {
gen = buf[rnd_cnt];
if (gen == last_gen) {
equal_count++;
}
last_gen = gen;
}

if (equal_count > N_VALUES / 2) {
zassert_false((equal_count > N_VALUES / 2),
"random numbers returned same value with high probability");
}

#else

printk("Cryptographically secure random number APIs not enabled\n");

#endif /* CONFIG_CSPRING_ENABLED */
}


void test_main(void)
{
ztest_test_suite(common_test,
ztest_unit_test(test_rand32)
);
ztest_test_suite(common_test, ztest_unit_test(test_rand32));

ztest_run_test_suite(common_test);
}
5 changes: 5 additions & 0 deletions tests/crypto/rand32/testcase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ tests:
filter: CONFIG_ENTROPY_HAS_DRIVER
tags: crypto security
min_ram: 16
crypto.rand32.random_ctr_drbg:
extra_args: CONF_FILE=prj_ctr_drbg.conf
filter: CONFIG_ENTROPY_HAS_DRIVER
tags: crypto security
min_ram: 16

0 comments on commit 1bb1b3e

Please sign in to comment.