xref: /aosp_15_r20/external/pigweed/pw_random/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_random:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker=========
4*61c4878aSAndroid Build Coastguard Workerpw_random
5*61c4878aSAndroid Build Coastguard Worker=========
6*61c4878aSAndroid Build Coastguard WorkerPigweed's ``pw_random`` module provides a generic interface for random number
7*61c4878aSAndroid Build Coastguard Workergenerators, as well as some practical embedded-friendly implementations. While
8*61c4878aSAndroid Build Coastguard Workerthis module does not provide drivers for hardware random number generators, it
9*61c4878aSAndroid Build Coastguard Workeracts as a user-friendly layer that can be used to abstract away such hardware.
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard WorkerEmbedded systems have the propensity to be more deterministic than your typical
12*61c4878aSAndroid Build Coastguard WorkerPC. Sometimes this is a good thing. Other times, it's valuable to have some
13*61c4878aSAndroid Build Coastguard Workerrandom numbers that aren't predictable. In security contexts or areas where
14*61c4878aSAndroid Build Coastguard Workerthings must be marked with a unique ID, this is especially important. Depending
15*61c4878aSAndroid Build Coastguard Workeron the project, true hardware random number generation peripherals may or may
16*61c4878aSAndroid Build Coastguard Workernot be available. Even if RNG hardware is present, it might not always be active
17*61c4878aSAndroid Build Coastguard Workeror accessible. ``pw_random`` provides libraries that make these situations
18*61c4878aSAndroid Build Coastguard Workereasier to manage.
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Worker---------------------
21*61c4878aSAndroid Build Coastguard WorkerUsing RandomGenerator
22*61c4878aSAndroid Build Coastguard Worker---------------------
23*61c4878aSAndroid Build Coastguard WorkerThere's two sides to a RandomGenerator; the input, and the output. The outputs
24*61c4878aSAndroid Build Coastguard Workerare relatively straightforward; ``GetInt(T&)`` randomizes the passed integer
25*61c4878aSAndroid Build Coastguard Workerreference, ``GetInt(T&, T exclusive_upper_bound)`` produces a random integer
26*61c4878aSAndroid Build Coastguard Workerless than ``exclusive_upper_bound``, and ``Get()`` dumps random values into the
27*61c4878aSAndroid Build Coastguard Workerpassed span. The inputs are in the form of the ``InjectEntropy*()`` functions.
28*61c4878aSAndroid Build Coastguard WorkerThese functions are used to "seed" the random generator. In some
29*61c4878aSAndroid Build Coastguard Workerimplementations, this can simply be resetting the seed of a PRNG, while in
30*61c4878aSAndroid Build Coastguard Workerothers it might directly populate a limited buffer of random data. In all cases,
31*61c4878aSAndroid Build Coastguard Workerentropy injection is used to improve the randomness of calls to ``Get*()``.
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard WorkerIt might not be easy to find sources of entropy in a system, but in general a
34*61c4878aSAndroid Build Coastguard Workerfew bits of noise from ADCs or other highly variable inputs can be accumulated
35*61c4878aSAndroid Build Coastguard Workerin a RandomGenerator over time to improve randomness. Such an approach might
36*61c4878aSAndroid Build Coastguard Workernot be sufficient for security, but it could help for less strict uses.
37*61c4878aSAndroid Build Coastguard Worker
38*61c4878aSAndroid Build Coastguard Worker-------------
39*61c4878aSAndroid Build Coastguard WorkerAPI reference
40*61c4878aSAndroid Build Coastguard Worker-------------
41*61c4878aSAndroid Build Coastguard Worker.. doxygennamespace:: pw::random
42*61c4878aSAndroid Build Coastguard Worker   :members:
43*61c4878aSAndroid Build Coastguard Worker
44*61c4878aSAndroid Build Coastguard Worker-----------
45*61c4878aSAndroid Build Coastguard WorkerFuture Work
46*61c4878aSAndroid Build Coastguard Worker-----------
47*61c4878aSAndroid Build Coastguard WorkerA simple "entropy pool" implementation could buffer incoming entropy later use
48*61c4878aSAndroid Build Coastguard Workerinstead of requiring an application to directly poll the hardware RNG peripheral
49*61c4878aSAndroid Build Coastguard Workerwhen the random data is needed. This would let a device collect entropy when
50*61c4878aSAndroid Build Coastguard Workeridling, improving the latency of potentially performance-sensitive areas where
51*61c4878aSAndroid Build Coastguard Workerrandom numbers are needed.
52*61c4878aSAndroid Build Coastguard Worker
53*61c4878aSAndroid Build Coastguard Worker
54*61c4878aSAndroid Build Coastguard Worker.. toctree::
55*61c4878aSAndroid Build Coastguard Worker   :hidden:
56*61c4878aSAndroid Build Coastguard Worker   :maxdepth: 1
57*61c4878aSAndroid Build Coastguard Worker
58*61c4878aSAndroid Build Coastguard Worker   Backends <backends>
59