drivers: entropy: use non-cache intermediate buffer for RNG

The CAAM hardware needs to read RNG values into a non-cache
buffer. Since the contract to Zephyr RNG functions do not
require non-cache buffers, we use an intermediate non-cache
buffer to retrieve results.

Added a Kconfig to control the size of the intermediate buffer.

Fixes #53035

Signed-off-by: David Leach <david.leach@nxp.com>
This commit is contained in:
David Leach 2023-01-24 22:49:31 -06:00 committed by Mahesh Mahadevan
commit cde1573619
3 changed files with 35 additions and 7 deletions

View file

@ -63,3 +63,16 @@ endchoice
choice CSPRNG_GENERATOR_CHOICE choice CSPRNG_GENERATOR_CHOICE
default CTR_DRBG_CSPRNG_GENERATOR if ENTROPY_MCUX_TRNG default CTR_DRBG_CSPRNG_GENERATOR if ENTROPY_MCUX_TRNG
endchoice 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

View file

@ -17,7 +17,9 @@ struct mcux_entropy_config {
CAAM_Type *base; 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, static int entropy_mcux_caam_get_entropy(const struct device *dev,
uint8_t *buffer, 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; const struct mcux_entropy_config *config = dev->config;
status_t status; status_t status;
caam_handle_t handle; caam_handle_t handle;
uint16_t read_length = 0;
uint16_t insert_idx = 0;
handle.jobRing = kCAAM_JobRing0; handle.jobRing = kCAAM_JobRing0;
status = CAAM_RNG_GetRandomData( /*
config->base, &handle, kCAAM_RngStateHandle0, * The buffer passed to the CAAM RNG function needs to be in non-cache
buffer, length, kCAAM_RngDataAny, NULL); * memory. Use an intermediate buffer to stage the data to the user
__ASSERT_NO_MSG(!status); * 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; return 0;
} }
@ -52,7 +67,7 @@ static int entropy_mcux_caam_init(const struct device *dev)
status_t status; status_t status;
CAAM_GetDefaultConfig(&conf); CAAM_GetDefaultConfig(&conf);
conf.jobRingInterface[0] = &jrif; conf.jobRingInterface[0] = &jrif0;
status = CAAM_Init(config->base, &conf); status = CAAM_Init(config->base, &conf);
__ASSERT_NO_MSG(!status); __ASSERT_NO_MSG(!status);

View file

@ -93,7 +93,7 @@ manifest:
groups: groups:
- hal - hal
- name: hal_nxp - name: hal_nxp
revision: 3ee6020efc1f8323d0fdc85615d3477161f0aa1f revision: d957472002a3758154f402e982d95ce90cf58e12
path: modules/hal/nxp path: modules/hal/nxp
groups: groups:
- hal - hal