-
Notifications
You must be signed in to change notification settings - Fork 8.2k
entropy: rpi_pico: implement entropy driver for RP2350 #83346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ supported: | |
| - clock | ||
| - counter | ||
| - dma | ||
| - entropy | ||
| - gpio | ||
| - hwinfo | ||
| - i2c | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Raspberry Pi Pico entropy generator driver configuration | ||
|
|
||
| # Copyright (c) 2024 Xudong Zheng | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| config ENTROPY_RPI_PICO_RNG | ||
| bool "Raspberry Pi Pico entropy number generator driver" | ||
| default y | ||
| depends on DT_HAS_RASPBERRYPI_PICO_RNG_ENABLED | ||
| depends on SOC_SERIES_RP2350 | ||
| select ENTROPY_HAS_DRIVER | ||
| select PICOSDK_USE_CLAIM | ||
| select PICOSDK_USE_RAND | ||
| select PICOSDK_USE_TIMER |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright (c) 2024 Xudong Zheng | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <string.h> | ||
| #include <pico/rand.h> | ||
| #include <zephyr/drivers/entropy.h> | ||
| #include <zephyr/spinlock.h> | ||
|
|
||
| #define DT_DRV_COMPAT raspberrypi_pico_rng | ||
|
|
||
| static struct k_spinlock entropy_lock; | ||
|
|
||
| static int entropy_rpi_pico_get_entropy(const struct device *dev, uint8_t *buf, uint16_t len) | ||
| { | ||
| __ASSERT_NO_MSG(buf != NULL); | ||
| uint8_t *buf_bytes = buf; | ||
|
|
||
| while (len > 0) { | ||
| uint64_t value; | ||
| size_t to_copy = MIN(sizeof(value), len); | ||
|
|
||
| K_SPINLOCK(&entropy_lock) { | ||
| value = get_rand_64(); | ||
| } | ||
| memcpy(buf_bytes, &value, to_copy); | ||
| buf_bytes += to_copy; | ||
| len -= to_copy; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static DEVICE_API(entropy, entropy_rpi_pico_api_funcs) = { | ||
| .get_entropy = entropy_rpi_pico_get_entropy | ||
| }; | ||
|
|
||
| DEVICE_DT_INST_DEFINE(0, | ||
| NULL, NULL, NULL, NULL, | ||
| PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY, | ||
| &entropy_rpi_pico_api_funcs); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| # Copyright (c) 2024 Xudong Zheng | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| description: Raspberry Pi Pico RNG/Entropy | ||
|
|
||
| compatible: "raspberrypi,pico-rng" | ||
|
|
||
| include: base.yaml |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this required? Maybe SDK functions using this could be avoided?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pico SDK mixes the TRNG output with uninitialized memory: https://github.com/raspberrypi/pico-sdk/blob/a1438dff1d38bd9c65dbd693f0e5db4b9ae91779/src/rp2_common/pico_rand/rand.c#L365-L374 To work around that, one would have to use |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| .uninitialized_data (NOLOAD): { | ||
|
Check warning on line 1 in modules/hal_rpi_pico/uninit_data.ld
|
||
| . = ALIGN(4); | ||
| *(.uninitialized_data*) | ||
| } > RAM | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it idiomatic for an entropy source to actually provide more entropy than it has? It only has a boot-time true random value, while this function is fueled by xoroshiro128 algorithm: https://github.com/raspberrypi/pico-sdk/blob/a1438dff1d38bd9c65dbd693f0e5db4b9ae91779/src/host/pico_rand/rand.c
This is a question to subsys maintainers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at https://github.com/raspberrypi/pico-sdk/blob/a1438dff1d38bd9c65dbd693f0e5db4b9ae91779/src/rp2_common/pico_rand/rand.c#L350-L354, each
get_rand_64()invocation callscapture_additional_trng_samples()for additional entropy.The end result is fed into xoroshiro128 as you mentioned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not sure if that matches subsystem expectations, but that sounds good. Please keep this thread open