diff options
| author | seteiro <[email protected]> | 2026-11-10 14:22:59 +0900 |
|---|---|---|
| committer | Rbb666 <[email protected]> | 2026-11-12 15:51:19 +0800 |
| commit | a5a9242efb7c43b0dd264ec1e6d4046cdc67913b (patch) | |
| tree | 3904b3b18b599a8987e94101bf055b497b04f057 | |
| parent | a1c9aa1a74a43999abef1c9052dd0f388de0146f (diff) | |
[drivers][misc] harden rt_random seed and document non-CSPRNG
Fix #8283 / CVE-2024-25389.
| -rw-r--r-- | components/drivers/misc/Kconfig | 6 | ||||
| -rw-r--r-- | components/drivers/misc/rt_random.c | 66 |
2 files changed, 70 insertions, 2 deletions
diff --git a/components/drivers/misc/Kconfig b/components/drivers/misc/Kconfig index 71f42ee67b..0b7c1205f0 100644 --- a/components/drivers/misc/Kconfig +++ b/components/drivers/misc/Kconfig @@ -13,6 +13,12 @@ config RT_USING_ZERO config RT_USING_RANDOM bool "Using RANDOM device drivers" default n + help + Enable the /dev/random and /dev/urandom devices. + + These devices are a non-cryptographic software PRNG. Do not use + them for cryptographic keys, nonces or other security-sensitive + values. menuconfig RT_USING_PWM bool "Using PWM device drivers" diff --git a/components/drivers/misc/rt_random.c b/components/drivers/misc/rt_random.c index 9f4be20902..f7a3cf547d 100644 --- a/components/drivers/misc/rt_random.c +++ b/components/drivers/misc/rt_random.c @@ -4,19 +4,63 @@ * Change Logs: * Date Author Notes * 2020-12-18 quanzhao the first version + * + * NOTE: /dev/random and /dev/urandom are non-cryptographic PRNGs. + * Do not use them for keys, nonces or other security-critical values. */ #include <time.h> #include <string.h> #include <rtthread.h> +#if defined(RT_USING_HWCRYPTO) && defined(RT_HWCRYPTO_USING_RNG) +#include <hw_rng.h> +#endif + static struct rt_device random_dev; static unsigned long seed; +static rt_bool_t seed_ready; + +/* returns 0 when no default hwcrypto RNG is available */ +static rt_uint32_t random_hw_value(void) +{ +#if defined(RT_USING_HWCRYPTO) && defined(RT_HWCRYPTO_USING_RNG) + return rt_hwcrypto_rng_update(); +#else + return 0; +#endif +} + +static unsigned long random_seed_value(const void *salt) +{ + unsigned long value = (unsigned long)rt_tick_get() ^ (unsigned long)(rt_ubase_t)salt; + +#if defined(RT_USING_HWCRYPTO) && defined(RT_HWCRYPTO_USING_RNG) + value ^= (unsigned long)rt_hwcrypto_rng_update(); +#endif + + value ^= value >> 16; + value *= 0x7feb352dUL; + value ^= value >> 15; + value *= 0x846ca68bUL; + value ^= value >> 16; + + return value ? value : 0x9e3779b9UL; +} static rt_uint16_t calc_random(void) { + rt_uint32_t hw_value = random_hw_value(); + + /* seed on first use so rt_tick_get() has advanced since boot */ + if (!seed_ready) + { + seed = random_seed_value(&seed); + seed_ready = RT_TRUE; + } + seed = 214013L * seed + 2531011L; - return (seed >> 16) & 0x7FFF; /* return bits 16~30 */ + return (rt_uint16_t)(((seed >> 16) ^ hw_value) & 0x7FFF); } static rt_ssize_t random_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) @@ -48,6 +92,10 @@ static rt_ssize_t random_write(rt_device_t dev, rt_off_t pos, const void *buffer { ssize_t ret = sizeof(seed) < size ? sizeof(seed) : size; rt_memcpy(&seed, buffer, ret); + if (ret > 0) + { + seed_ready = RT_TRUE; + } return ret; } @@ -103,11 +151,21 @@ INIT_DEVICE_EXPORT(random_device_init); static struct rt_device urandom_dev; static unsigned long useed; +static rt_bool_t useed_ready; static rt_uint16_t calc_urandom(void) { + rt_uint32_t hw_value = random_hw_value(); + + /* seed on first use so rt_tick_get() has advanced since boot */ + if (!useed_ready) + { + useed = random_seed_value(&useed); + useed_ready = RT_TRUE; + } + useed = 214013L * useed + 2531011L; - return (useed >> 16) & 0x7FFF; /* return bits 16~30 */ + return (rt_uint16_t)(((useed >> 16) ^ hw_value) & 0x7FFF); } static rt_ssize_t random_uread(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) @@ -139,6 +197,10 @@ static rt_ssize_t random_uwrite(rt_device_t dev, rt_off_t pos, const void *buffe { ssize_t ret = sizeof(useed) < size ? sizeof(useed) : size; rt_memcpy(&useed, buffer, ret); + if (ret > 0) + { + useed_ready = RT_TRUE; + } return ret; } |
