Skip to content

Commit 71fbccc

Browse files
Kamil Gaworjukkar
authored andcommitted
[nrf noup] entropy: Add fake entropy nRF PRNG driver
This adds temporary entropy driver simulation for nRF54h20 device since final entropy source is not available yet. TODO: Remove this commit when proper solution will be available. Jira: NCSDK-25947 Signed-off-by: Kamil Gawor <[email protected]> Signed-off-by: Robert Lubos <[email protected]> Signed-off-by: Andreas Moltumyr <[email protected]> Signed-off-by: Karol Lasończyk <[email protected]> (cherry picked from commit 27b137c) (cherry picked from commit e2aa08c)
1 parent 82003f6 commit 71fbccc

File tree

10 files changed

+283
-0
lines changed

10 files changed

+283
-0
lines changed

boards/nordic/nrf54h20dk/nrf54h20dk_nrf54h20_cpuapp.dts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
zephyr,bt-hci = &bt_hci_ipc0;
2828
nordic,802154-spinel-ipc = &ipc0;
2929
zephyr,canbus = &can120;
30+
zephyr,entropy = &prng;
3031
};
3132

3233
aliases {
@@ -107,6 +108,11 @@
107108
pwms = <&pwm130 0 PWM_MSEC(20) PWM_POLARITY_NORMAL>;
108109
};
109110
};
111+
112+
prng: prng {
113+
compatible = "nordic,entropy-prng";
114+
status = "okay";
115+
};
110116
};
111117

112118
&cpuapp_bellboard {

boards/nordic/nrf54h20dk/nrf54h20dk_nrf54h20_cpurad.dts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
zephyr,ieee802154 = &cpurad_ieee802154;
2929
zephyr,bt-hci-ipc = &ipc0;
3030
nordic,802154-spinel-ipc = &ipc0;
31+
zephyr,entropy = &prng;
32+
};
33+
prng: prng {
34+
compatible = "nordic,entropy-prng";
35+
status = "okay";
3136
};
3237

3338
aliases {

boards/nordic/nrf9280pdk/nrf9280pdk_nrf9280_cpuapp.dts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
zephyr,ieee802154 = &cpuapp_ieee802154;
2828
zephyr,bt-hci = &bt_hci_ipc0;
2929
nordic,802154-spinel-ipc = &ipc0;
30+
zephyr,entropy = &prng;
3031
};
3132

3233
aliases {
@@ -108,6 +109,11 @@
108109
pwms = <&pwm130 0 PWM_MSEC(20) PWM_POLARITY_NORMAL>;
109110
};
110111
};
112+
113+
prng: prng {
114+
compatible = "nordic,entropy-prng";
115+
status = "okay";
116+
};
111117
};
112118

113119
&cpuapp_ram0x_region {

boards/nordic/nrf9280pdk/nrf9280pdk_nrf9280_cpurad.dts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
zephyr,ieee802154 = &cpurad_ieee802154;
2929
zephyr,bt-hci-ipc = &ipc0;
3030
nordic,802154-spinel-ipc = &ipc0;
31+
zephyr,entropy = &prng;
32+
};
33+
prng: prng {
34+
compatible = "nordic,entropy-prng";
35+
status = "okay";
3136
};
3237

3338
aliases {

drivers/entropy/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,11 @@ zephyr_library_sources_ifdef(CONFIG_ENTROPY_TELINK_B91_TRNG entropy_b91_trng
4242
zephyr_library_sources_ifdef(CONFIG_ENTROPY_VIRTIO entropy_virtio.c)
4343
# zephyr-keep-sorted-stop
4444

45+
if (CONFIG_FAKE_ENTROPY_NRF_PRNG)
46+
zephyr_library_sources(fake_entropy_nrf_prng.c)
47+
48+
message(WARNING "\nA nRF PRNG is used, which does not produce real random bits."
49+
"This is not secure and should therefore never be used in a product.")
50+
endif()
51+
4552
zephyr_library_link_libraries_ifdef(CONFIG_BUILD_WITH_TFM tfm_api)

drivers/entropy/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ source "drivers/entropy/Kconfig.neorv32"
3636
source "drivers/entropy/Kconfig.npcx"
3737
source "drivers/entropy/Kconfig.nrf5"
3838
source "drivers/entropy/Kconfig.nrf_cracen"
39+
source "drivers/entropy/Kconfig.nrf_prng"
3940
source "drivers/entropy/Kconfig.nxp"
4041
source "drivers/entropy/Kconfig.psa_crypto"
4142
source "drivers/entropy/Kconfig.renesas_ra"

drivers/entropy/Kconfig.nrf_prng

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# nRF fake entropy prng generator driver configuration
2+
3+
# Copyright (c) 2024 Nordic Semiconductor ASA
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
if ENTROPY_GENERATOR
7+
8+
config FAKE_ENTROPY_NRF_PRNG
9+
bool "A fake nRF entropy driver"
10+
default y
11+
depends on DT_HAS_NORDIC_ENTROPY_PRNG_ENABLED
12+
depends on (SOC_SERIES_NRF54HX || SOC_SERIES_NRF92X || SOC_SERIES_NRF54LX)
13+
select ENTROPY_HAS_DRIVER
14+
help
15+
This is a super simple PRNG driver that can be used on nRF platforms that
16+
do not have an entropy source.
17+
This is NOT SAFE to use for cryptographic operations!
18+
19+
endif
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/init.h>
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
#include <assert.h>
12+
#include <zephyr/drivers/entropy.h>
13+
14+
#define DT_DRV_COMPAT nordic_entropy_prng
15+
16+
/* This file implements a pseudo-RNG
17+
* https://vigna.di.unimi.it/xorshift/xoshiro128plus.c
18+
*/
19+
20+
static uint32_t s[4];
21+
22+
static inline uint32_t rotl(const uint32_t x, int k)
23+
{
24+
return (x << k) | (x >> (32 - k));
25+
}
26+
27+
static uint32_t rng_next(void)
28+
{
29+
const uint32_t result = rotl(s[0] + s[3], 7) + s[0];
30+
31+
const uint32_t t = s[1] << 9;
32+
33+
s[2] ^= s[0];
34+
s[3] ^= s[1];
35+
s[1] ^= s[2];
36+
s[0] ^= s[3];
37+
38+
s[2] ^= t;
39+
40+
s[3] = rotl(s[3], 11);
41+
42+
return result;
43+
}
44+
45+
static int entropy_prng_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length)
46+
{
47+
ARG_UNUSED(dev);
48+
49+
while (length) {
50+
/*
51+
* Note that only 1 thread (Zephyr thread or HW models), runs at
52+
* a time, therefore there is no need to use random_r()
53+
*/
54+
uint32_t value = rng_next();
55+
56+
size_t to_copy = MIN(length, sizeof(long));
57+
58+
memcpy(buffer, &value, to_copy);
59+
buffer += to_copy;
60+
length -= to_copy;
61+
}
62+
63+
return 0;
64+
}
65+
66+
static int entropy_prng_get_entropy_isr(const struct device *dev, uint8_t *buf, uint16_t len,
67+
uint32_t flags)
68+
{
69+
ARG_UNUSED(flags);
70+
71+
int err;
72+
73+
/*
74+
* entropy_prng_get_entropy() is also safe for ISRs
75+
* and always produces data.
76+
*/
77+
err = entropy_prng_get_entropy(dev, buf, len);
78+
if (err < 0) {
79+
return err;
80+
} else {
81+
return len;
82+
}
83+
}
84+
85+
static int entropy_prng_init(const struct device *dev)
86+
{
87+
ARG_UNUSED(dev);
88+
89+
/* Picked some arbitrary initial seed. */
90+
s[0] = 0xAF568BC0;
91+
s[1] = 0xAC34307E;
92+
s[2] = 0x9B7F6DD1;
93+
s[3] = 0xD84319FC;
94+
return 0;
95+
}
96+
97+
static const struct entropy_driver_api entropy_prng_api_funcs = {
98+
.get_entropy = entropy_prng_get_entropy, .get_entropy_isr = entropy_prng_get_entropy_isr};
99+
100+
DEVICE_DT_INST_DEFINE(0, entropy_prng_init, NULL, NULL, NULL, PRE_KERNEL_1,
101+
CONFIG_ENTROPY_INIT_PRIORITY, &entropy_prng_api_funcs);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <nordic/nrf54l20.dtsi>
8+
9+
cpu: &cpuapp {};
10+
systick: &cpuapp_systick {};
11+
nvic: &cpuapp_nvic {};
12+
13+
/delete-node/ &cpuflpr;
14+
/delete-node/ &cpuflpr_rram;
15+
/delete-node/ &cpuflpr_sram;
16+
/delete-node/ &cpuflpr_clic;
17+
18+
/ {
19+
chosen {
20+
zephyr,entropy = &prng;
21+
};
22+
23+
soc {
24+
compatible = "simple-bus";
25+
interrupt-parent = <&cpuapp_nvic>;
26+
ranges;
27+
};
28+
29+
psa_rng: psa-rng {
30+
compatible = "zephyr,psa-crypto-rng";
31+
status = "disabled";
32+
};
33+
34+
prng: prng {
35+
compatible = "nordic,entropy-prng";
36+
status = "okay";
37+
};
38+
};
39+
40+
&cpuflpr_vpr {
41+
cpuapp_vevif_rx: mailbox@1 {
42+
compatible = "nordic,nrf-vevif-event-rx";
43+
reg = <0x0 0x1000>;
44+
status = "disabled";
45+
interrupts = <76 NRF_DEFAULT_IRQ_PRIORITY>;
46+
#mbox-cells = <1>;
47+
nordic,events = <1>;
48+
nordic,events-mask = <0x00100000>;
49+
};
50+
51+
cpuapp_vevif_tx: mailbox@0 {
52+
compatible = "nordic,nrf-vevif-task-tx";
53+
reg = <0x0 0x1000>;
54+
#mbox-cells = <1>;
55+
nordic,tasks = <7>;
56+
nordic,tasks-mask = <0x007f0000>;
57+
status = "disabled";
58+
};
59+
};
60+
61+
&cpuapp_ppb {
62+
compatible = "simple-bus";
63+
ranges;
64+
};
65+
66+
&grtc {
67+
interrupts = <228 NRF_DEFAULT_IRQ_PRIORITY>,
68+
<229 NRF_DEFAULT_IRQ_PRIORITY>; /* reserved for Zero Latency IRQs */
69+
};
70+
71+
&gpiote20 {
72+
interrupts = <219 NRF_DEFAULT_IRQ_PRIORITY>;
73+
};
74+
75+
&gpiote30 {
76+
interrupts = <269 NRF_DEFAULT_IRQ_PRIORITY>;
77+
};
78+
79+
&dppic00 {
80+
status = "okay";
81+
};
82+
83+
&dppic10 {
84+
status = "okay";
85+
};
86+
87+
&dppic20 {
88+
status = "okay";
89+
};
90+
91+
&dppic30 {
92+
status = "okay";
93+
};
94+
95+
&ppib00 {
96+
status = "okay";
97+
};
98+
99+
&ppib01 {
100+
status = "okay";
101+
};
102+
103+
&ppib10 {
104+
status = "okay";
105+
};
106+
107+
&ppib11 {
108+
status = "okay";
109+
};
110+
111+
&ppib20 {
112+
status = "okay";
113+
};
114+
115+
&ppib21 {
116+
status = "okay";
117+
};
118+
119+
&ppib22 {
120+
status = "okay";
121+
};
122+
123+
&ppib30 {
124+
status = "okay";
125+
};

dts/bindings/rng/nordic,nrf-prng.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2024 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
3+
4+
description: This is a super simple PRNG
5+
6+
compatible: "nordic,entropy-prng"
7+
8+
include: base.yaml

0 commit comments

Comments
 (0)