diff --git a/drivers/entropy/Kconfig.mcux b/drivers/entropy/Kconfig.mcux index ab90a22b9a4..40941a20610 100644 --- a/drivers/entropy/Kconfig.mcux +++ b/drivers/entropy/Kconfig.mcux @@ -63,3 +63,16 @@ endchoice choice CSPRNG_GENERATOR_CHOICE default CTR_DRBG_CSPRNG_GENERATOR if ENTROPY_MCUX_TRNG endchoice + +if ENTROPY_MCUX_CAAM + +config ENTRY_MCUX_CAAM_POOL_SIZE + int "CAAM random number pool size" + range 4 1024 + default 256 + help + Buffer length in bytes used to store random bytes generated by + CAAM hardware. Please note, that size of the pool must be a + power of 2. + +endif # ENTROPY_MCUX_CAAM diff --git a/drivers/entropy/entropy_mcux_caam.c b/drivers/entropy/entropy_mcux_caam.c index 084b0d39683..3bef9dcd140 100644 --- a/drivers/entropy/entropy_mcux_caam.c +++ b/drivers/entropy/entropy_mcux_caam.c @@ -17,7 +17,9 @@ struct mcux_entropy_config { CAAM_Type *base; }; -static caam_job_ring_interface_t jrif __attribute__((__section__(".nocache"))); +static caam_job_ring_interface_t jrif0 __attribute__((__section__(".nocache"))); +static uint8_t rng_buff_pool[CONFIG_ENTRY_MCUX_CAAM_POOL_SIZE] + __attribute__((__section__(".nocache"))); static int entropy_mcux_caam_get_entropy(const struct device *dev, uint8_t *buffer, @@ -26,13 +28,26 @@ static int entropy_mcux_caam_get_entropy(const struct device *dev, const struct mcux_entropy_config *config = dev->config; status_t status; caam_handle_t handle; + uint16_t read_length = 0; + uint16_t insert_idx = 0; handle.jobRing = kCAAM_JobRing0; - status = CAAM_RNG_GetRandomData( - config->base, &handle, kCAAM_RngStateHandle0, - buffer, length, kCAAM_RngDataAny, NULL); - __ASSERT_NO_MSG(!status); + /* + * The buffer passed to the CAAM RNG function needs to be in non-cache + * memory. Use an intermediate buffer to stage the data to the user + * buffer. + */ + while (insert_idx < length) { + read_length = MIN(sizeof(rng_buff_pool), (length - insert_idx)); + + status = CAAM_RNG_GetRandomData( + config->base, &handle, kCAAM_RngStateHandle0, + &rng_buff_pool[0], read_length, kCAAM_RngDataAny, NULL); + + memcpy(&buffer[insert_idx], &rng_buff_pool[0], read_length); + insert_idx += read_length; + } return 0; } @@ -52,7 +67,7 @@ static int entropy_mcux_caam_init(const struct device *dev) status_t status; CAAM_GetDefaultConfig(&conf); - conf.jobRingInterface[0] = &jrif; + conf.jobRingInterface[0] = &jrif0; status = CAAM_Init(config->base, &conf); __ASSERT_NO_MSG(!status); diff --git a/west.yml b/west.yml index 2194f2c8bef..b7d02ce38c1 100644 --- a/west.yml +++ b/west.yml @@ -93,7 +93,7 @@ manifest: groups: - hal - name: hal_nxp - revision: 3ee6020efc1f8323d0fdc85615d3477161f0aa1f + revision: d957472002a3758154f402e982d95ce90cf58e12 path: modules/hal/nxp groups: - hal