Refactor crypto RNG; add getrandom() support
[tinc] / src / xoshiro.c
1 /*  Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org)
2
3 To the extent possible under law, the author has dedicated all copyright
4 and related and neighboring rights to this software to the public domain
5 worldwide. This software is distributed without any warranty.
6
7 See <http://creativecommons.org/publicdomain/zero/1.0/>. */
8
9 #include "system.h"
10
11 #include "crypto.h"
12 #include "random.h"
13
14 /* This is xoshiro256** 1.0, one of our all-purpose, rock-solid
15    generators. It has excellent (sub-ns) speed, a state (256 bits) that is
16    large enough for any parallel application, and it passes all tests we
17    are aware of.
18
19    For generating just floating-point numbers, xoshiro256+ is even faster.
20
21    The state must be seeded so that it is not everywhere zero. If you have
22    a 64-bit seed, we suggest to seed a splitmix64 generator and use its
23    output to fill s. */
24
25 static inline uint64_t rotl(const uint64_t x, int k) {
26         return (x << k) | (x >> (64 - k));
27 }
28
29 static uint64_t s[4];
30
31 uint64_t xoshiro(void) {
32         const uint64_t result = rotl(s[1] * 5, 7) * 9;
33
34         const uint64_t t = s[1] << 17;
35
36         s[2] ^= s[0];
37         s[3] ^= s[1];
38         s[1] ^= s[2];
39         s[0] ^= s[3];
40
41         s[2] ^= t;
42
43         s[3] = rotl(s[3], 45);
44
45         return result;
46 }
47
48 void prng_init(void) {
49         do {
50                 randomize(s, sizeof(s));
51         } while(!s[0] && !s[1] && !s[2] && !s[3]);
52 }
53
54 void prng_randomize(void *buf, size_t buflen) {
55         uint8_t *p = buf;
56         uint64_t value;
57
58         while(buflen > sizeof(value)) {
59                 value = xoshiro();
60                 memcpy(p, &value, sizeof(value));
61                 p += sizeof(value);
62                 buflen -= sizeof(value);
63         }
64
65         if(!buflen) {
66                 return;
67         }
68
69         value = xoshiro();
70         memcpy(p, &value, buflen);
71 }