CC3XX: Make sure SP800-90B mode is always enabled for entropy
SP800-90B prescribes that the entropy source performs some
testing (continuous health tests) at startup and during each
entropy collection, hence make sure the option is always
enabled when obtaining entropy through get_entropy().
Signed-off-by: Antonio de Angelis <antonio.deangelis@arm.com>
Change-Id: I2aa47734b19b545446a1b520b5a734dd76dc75a7
diff --git a/platform/ext/target/arm/drivers/cc3xx/low_level_driver/include/cc3xx_entropy.h b/platform/ext/target/arm/drivers/cc3xx/low_level_driver/include/cc3xx_entropy.h
index 3dd60dd..4785cbe 100644
--- a/platform/ext/target/arm/drivers/cc3xx/low_level_driver/include/cc3xx_entropy.h
+++ b/platform/ext/target/arm/drivers/cc3xx/low_level_driver/include/cc3xx_entropy.h
@@ -19,25 +19,11 @@
extern "C" {
#endif
-#define CC3XX_ENTROPY_SIZE (CC3XX_TRNG_SAMPLE_SIZE)
-
/**
- * @brief SP800-90B section 4.4 recommends two continuous health tests
- * to be performed at startup and during normal operation of the
- * noise source to verify the quality ot the entropy bits produced,
- * namely the Repetition Count Test (4.4.1) and Adaptive Proportion
- * Test (4.4.2)
- *
- * @param[in] enable Set to \a true to put the RNG entropy source in SP800-90B compatible
- * mode, i.e. enable continuous health tests as recommended by SP800-90B
- *
- * @return cc3xx_err_t CC3XX_ERR_SUCCESS on success, or
- * CC3XX_ERR_NOT_IMPLEMENTED in case the firmware is built
- * without support for the continuous health tests, i.e.
- * \a CC3XX_CONFIG_RNG_CONTINUOUS_HEALTH_TESTS_ENABLE is not
- * set in the CC3XX configuration \a cc3xx_config.h
+ * @brief Size of the sample produced by the underlying source of randomness in a
+ * single reading
*/
-cc3xx_err_t cc3xx_lowlevel_entropy_sp800_90b_mode(bool enable);
+#define CC3XX_ENTROPY_SIZE (CC3XX_TRNG_SAMPLE_SIZE)
/**
* @brief Requires an amount of entropy from the TRNG
diff --git a/platform/ext/target/arm/drivers/cc3xx/low_level_driver/src/cc3xx_entropy.c b/platform/ext/target/arm/drivers/cc3xx/low_level_driver/src/cc3xx_entropy.c
index fb70f66..9bbb8cf 100644
--- a/platform/ext/target/arm/drivers/cc3xx/low_level_driver/src/cc3xx_entropy.c
+++ b/platform/ext/target/arm/drivers/cc3xx/low_level_driver/src/cc3xx_entropy.c
@@ -68,14 +68,13 @@
*/
#define SP800_90B_REPETITION_COUNT_CUTOFF_RATE (81UL)
-/* Static context of the TRNG continuous health tests */
+/* Static context of the entropy source continuous health tests */
static struct health_tests_ctx_t {
size_t total_bits_count; /*!< Number of total bits observed for the Adaptive Proportion Test window */
size_t number_of_0s; /*!< Number of zeros observed in the Adaptive Proportion Test window */
size_t number_of_contiguous_0s; /*!< Number of contiguous zeros observed in the Repetition Count Test */
size_t number_of_contiguous_1s; /*!< Number of contiguous ones observed in the Repetition Count Test */
- bool continuous; /*!< Continous Health tests enabled, i.e. both Adaptive Proportion and Repetition Count */
- bool startup; /*!< Indicates whether a full startup test is performed on next call to get_entropy */
+ bool startup_done; /*!< Indicates whether a full collection on startup has been done already */
} g_entropy_tests = {0};
/* See https://en.wikipedia.org/wiki/Hamming_weight */
@@ -201,19 +200,6 @@
return err;
}
-cc3xx_err_t cc3xx_lowlevel_entropy_sp800_90b_mode(bool enable)
-{
- if (enable) {
- g_entropy_tests = (struct health_tests_ctx_t){.startup = true};
- } else {
- g_entropy_tests = (struct health_tests_ctx_t){0};
- }
-
- cc3xx_lowlevel_trng_sp800_90b_mode(enable);
-
- return CC3XX_ERR_SUCCESS;
-}
-
cc3xx_err_t cc3xx_lowlevel_entropy_get(uint32_t *entropy, size_t entropy_len)
{
cc3xx_err_t err;
@@ -228,12 +214,15 @@
cc3xx_lowlevel_trng_init();
- if (g_entropy_tests.startup) {
+ if (!g_entropy_tests.startup_done) {
+ /* Perform any required configuration on the TRNG first */
+ cc3xx_lowlevel_trng_sp800_90b_mode(true);
+ /* Perform the extensive collection on startup */
err = startup_test(CC3XX_TRNG_SAMPLE_SIZE);
if (err != CC3XX_ERR_SUCCESS) {
goto cleanup;
}
- g_entropy_tests.startup = false;
+ g_entropy_tests.startup_done = true;
}
for (size_t i = 0; i < entropy_len / CC3XX_TRNG_SAMPLE_SIZE; i++) {
@@ -243,12 +232,10 @@
goto cleanup;
}
- if (g_entropy_tests.continuous) {
- err = continuous_health_test(
- &entropy[num_words], CC3XX_TRNG_SAMPLE_SIZE, &g_entropy_tests);
- if (err != CC3XX_ERR_SUCCESS) {
- goto cleanup;
- }
+ /* The entropy source is always in SP 800-90B mode, i.e. continuosly testing itself */
+ err = continuous_health_test(&entropy[num_words], CC3XX_TRNG_SAMPLE_SIZE, &g_entropy_tests);
+ if (err != CC3XX_ERR_SUCCESS) {
+ goto cleanup;
}
num_words += CC3XX_TRNG_SAMPLE_SIZE / sizeof(uint32_t);