Skip to content

Commit cde1573

Browse files
dleach02mmahadevan108
authored andcommitted
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 <[email protected]>
1 parent 3e937da commit cde1573

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

drivers/entropy/Kconfig.mcux

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,16 @@ endchoice
6363
choice CSPRNG_GENERATOR_CHOICE
6464
default CTR_DRBG_CSPRNG_GENERATOR if ENTROPY_MCUX_TRNG
6565
endchoice
66+
67+
if ENTROPY_MCUX_CAAM
68+
69+
config ENTRY_MCUX_CAAM_POOL_SIZE
70+
int "CAAM random number pool size"
71+
range 4 1024
72+
default 256
73+
help
74+
Buffer length in bytes used to store random bytes generated by
75+
CAAM hardware. Please note, that size of the pool must be a
76+
power of 2.
77+
78+
endif # ENTROPY_MCUX_CAAM

drivers/entropy/entropy_mcux_caam.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ struct mcux_entropy_config {
1717
CAAM_Type *base;
1818
};
1919

20-
static caam_job_ring_interface_t jrif __attribute__((__section__(".nocache")));
20+
static caam_job_ring_interface_t jrif0 __attribute__((__section__(".nocache")));
21+
static uint8_t rng_buff_pool[CONFIG_ENTRY_MCUX_CAAM_POOL_SIZE]
22+
__attribute__((__section__(".nocache")));
2123

2224
static int entropy_mcux_caam_get_entropy(const struct device *dev,
2325
uint8_t *buffer,
@@ -26,13 +28,26 @@ static int entropy_mcux_caam_get_entropy(const struct device *dev,
2628
const struct mcux_entropy_config *config = dev->config;
2729
status_t status;
2830
caam_handle_t handle;
31+
uint16_t read_length = 0;
32+
uint16_t insert_idx = 0;
2933

3034
handle.jobRing = kCAAM_JobRing0;
3135

32-
status = CAAM_RNG_GetRandomData(
33-
config->base, &handle, kCAAM_RngStateHandle0,
34-
buffer, length, kCAAM_RngDataAny, NULL);
35-
__ASSERT_NO_MSG(!status);
36+
/*
37+
* The buffer passed to the CAAM RNG function needs to be in non-cache
38+
* memory. Use an intermediate buffer to stage the data to the user
39+
* buffer.
40+
*/
41+
while (insert_idx < length) {
42+
read_length = MIN(sizeof(rng_buff_pool), (length - insert_idx));
43+
44+
status = CAAM_RNG_GetRandomData(
45+
config->base, &handle, kCAAM_RngStateHandle0,
46+
&rng_buff_pool[0], read_length, kCAAM_RngDataAny, NULL);
47+
48+
memcpy(&buffer[insert_idx], &rng_buff_pool[0], read_length);
49+
insert_idx += read_length;
50+
}
3651

3752
return 0;
3853
}
@@ -52,7 +67,7 @@ static int entropy_mcux_caam_init(const struct device *dev)
5267
status_t status;
5368

5469
CAAM_GetDefaultConfig(&conf);
55-
conf.jobRingInterface[0] = &jrif;
70+
conf.jobRingInterface[0] = &jrif0;
5671

5772
status = CAAM_Init(config->base, &conf);
5873
__ASSERT_NO_MSG(!status);

west.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ manifest:
9393
groups:
9494
- hal
9595
- name: hal_nxp
96-
revision: 3ee6020efc1f8323d0fdc85615d3477161f0aa1f
96+
revision: d957472002a3758154f402e982d95ce90cf58e12
9797
path: modules/hal/nxp
9898
groups:
9999
- hal

0 commit comments

Comments
 (0)