CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
entropy.h
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <concepts>
4#include <cstdint>
5#include <ds/xoroshiro.h>
6
7/**
8 * \file
9 * Concept for an Entropy sources and a trivial, insecure implementation
10 * using xoroshiro PRNG.
11 */
12template<typename T>
13concept IsEntropySource = requires(T source) {
14 /**
15 * Must export a flag indicating whether this is a cryptographically
16 * secure random number.
17 */
18 { T::IsSecure } -> std::convertible_to<const bool>;
19
20 /**
21 * Must return a random number. All bits of the value type are assumed to
22 * be random, this should use a narrower type if it guarantees less
23 * entropy.
24 */
25 { source() } -> std::same_as<typename T::ValueType>;
26
27 /**
28 * Must provide a method that reseeds the entropy source. If this is a
29 * hardware whitening engine that is continuously fed with entropy, this
30 * may be an empty function. There are no constraints on the return type
31 * of this.
32 */
33 { source.reseed() };
34};
35
36/**
37 * A simple entropy source. This wraps a few weak entropy sources to seed a
38 * PRNG. It is absolutely not secure and should not be used for anything that
39 * depends on cryptographically secure random numbers! Unfortunately, there is
40 * nothing on the Sonata instantiation of CHERIoT SAFE that can be used as a
41 * secure entropy source.
42 *
43 * This is provided as a generic implementation that can be used with an
44 * interrupt source to provide a slightly better version for testing on FPGA
45 * and simulation environments that don't have anything secure.
46 */
48{
50
51 protected:
52 /// Reseed the PRNG with a provided set of jumps.
53 void reseed(uint16_t jumps)
54 {
55 // Start from a not very random seed
56 uint64_t seed = rdcycle64();
57 prng.set_state(seed, seed >> 24);
58 // Permute it with another not-very-random number
59 for (uint32_t i = 0; i < ((jumps & 0xff00) >> 8); i++)
60 {
61 prng.long_jump();
62 }
63 for (uint32_t i = 0; i < (jumps & 0xff); i++)
64 {
65 prng.jump();
66 }
67 // At this point, our random number is in a fairly predictable state,
68 // but with a fairly low probability of being the same predictable
69 // state as before.
70 }
71
72 public:
73 using ValueType = uint64_t;
74
75 /// Definitely not secure!
76 static constexpr bool IsSecure = false;
77
78 /// Constructor, tries to generate an independent sequence of random numbers
83
84 /// Reseed the PRNG.
85 void reseed()
86 {
87 reseed(rdcycle64());
88 }
89
90 /// Get the next value from the PRNG.
91 ValueType operator()()
92 {
93 return prng();
94 }
95};
static constexpr bool IsSecure
Definitely not secure!
Definition entropy.h:76
TrivialInsecureEntropySource()
Constructor, tries to generate an independent sequence of random numbers.
Definition entropy.h:79
ValueType operator()()
Get the next value from the PRNG.
Definition entropy.h:91
void reseed()
Reseed the PRNG.
Definition entropy.h:85
void reseed(uint16_t jumps)
Reseed the PRNG with a provided set of jumps.
Definition entropy.h:53
Pseudo random number generator based on Xoroshiro algorithm.
detail::XorOshiro< uint64_t, uint64_t, 24, 16, 37, 0xdf900294d8f554a5, 0x170865df4b3201fc, 0xd2a98b26625eee7b, 0xdddf9b1090aa7ac1 > P128R64
xoroshiro128+ with 64-bit output using the 2018 parameters.
Definition xoroshiro.h:158