|
| 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); |
0 commit comments