The Pedigree Project 0.1
SecureRandom.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef PEDIGREE_SECURE_RANDOM_H
3#define PEDIGREE_SECURE_RANDOM_H
4
5#include <stddef.h>
6#include <stdint.h>
7
8// Original implementations of RFC 8439 section 2.3 (June 2018), SHA-256 as
9// specified in RFC 6234 (May 2011), and HMAC from RFC 2104 (February 1997).
10// This header is also used by the standalone persistent-seed bootstrap tool.
11namespace pedigree_random {
12inline void erase(void* memory, size_t length) {
13 volatile uint8_t* bytes = static_cast<volatile uint8_t*>(memory);
14 while (length--)
15 *bytes++ = 0;
16}
17
18namespace detail {
19inline uint32_t little32(const uint8_t* bytes) {
20 return static_cast<uint32_t>(bytes[0]) | (static_cast<uint32_t>(bytes[1]) << 8) |
21 (static_cast<uint32_t>(bytes[2]) << 16) | (static_cast<uint32_t>(bytes[3]) << 24);
22}
23
24inline uint32_t rotateLeft(uint32_t word, unsigned bits) {
25 return (word << bits) | (word >> (32 - bits));
26}
27
28inline void quarter(uint32_t& a, uint32_t& b, uint32_t& c, uint32_t& d) {
29 a += b;
30 d = rotateLeft(d ^ a, 16);
31 c += d;
32 b = rotateLeft(b ^ c, 12);
33 a += b;
34 d = rotateLeft(d ^ a, 8);
35 c += d;
36 b = rotateLeft(b ^ c, 7);
37}
38
39class Sha256 {
40 public:
41 void update(const void* data, size_t length) {
42 const uint8_t* bytes = static_cast<const uint8_t*>(data);
43 m_Length += length;
44 while (length--) {
45 m_Block[m_Used++] = *bytes++;
46 if (m_Used == sizeof(m_Block)) {
47 transform();
48 m_Used = 0;
49 }
50 }
51 }
52
53 void finish(uint8_t output[32]) {
54 const uint64_t bits = m_Length * 8;
55 uint8_t padding = 0x80;
56 update(&padding, 1);
57 padding = 0;
58 while (m_Used != 56)
59 update(&padding, 1);
60 uint8_t length[8];
61 for (size_t i = 0; i < sizeof(length); ++i)
62 length[i] = static_cast<uint8_t>(bits >> (56 - 8 * i));
63 update(length, sizeof(length));
64 for (size_t i = 0; i < 8; ++i)
65 for (size_t j = 0; j < 4; ++j)
66 output[4 * i + j] = static_cast<uint8_t>(m_State[i] >> (24 - 8 * j));
67 erase(length, sizeof(length));
68 erase(this, sizeof(*this));
69 }
70
71 private:
72 void transform() {
73 static const uint32_t constants[64] = {
74 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
75 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
76 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
77 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
78 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
79 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
80 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,
81 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
82 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
83 0xc67178f2};
84 uint32_t schedule[64];
85 for (size_t i = 0; i < 16; ++i) {
86 schedule[i] = 0;
87 for (size_t j = 0; j < 4; ++j)
88 schedule[i] = (schedule[i] << 8) | m_Block[i * 4 + j];
89 }
90 for (size_t i = 16; i < 64; ++i) {
91 const uint32_t a = schedule[i - 15], b = schedule[i - 2];
92 const uint32_t s0 = rotateLeft(a, 25) ^ rotateLeft(a, 14) ^ (a >> 3);
93 const uint32_t s1 = rotateLeft(b, 15) ^ rotateLeft(b, 13) ^ (b >> 10);
94 schedule[i] = schedule[i - 16] + s0 + schedule[i - 7] + s1;
95 }
96 uint32_t work[8];
97 for (size_t i = 0; i < 8; ++i)
98 work[i] = m_State[i];
99 for (size_t i = 0; i < 64; ++i) {
100 const uint32_t s1 =
101 rotateLeft(work[4], 26) ^ rotateLeft(work[4], 21) ^ rotateLeft(work[4], 7);
102 const uint32_t choose = (work[4] & work[5]) ^ (~work[4] & work[6]);
103 const uint32_t t1 = work[7] + s1 + choose + constants[i] + schedule[i];
104 const uint32_t s0 =
105 rotateLeft(work[0], 30) ^ rotateLeft(work[0], 19) ^ rotateLeft(work[0], 10);
106 const uint32_t majority = (work[0] & work[1]) ^ (work[0] & work[2]) ^ (work[1] & work[2]);
107 for (size_t j = 7; j > 0; --j)
108 work[j] = work[j - 1];
109 work[4] += t1;
110 work[0] = t1 + s0 + majority;
111 }
112 for (size_t i = 0; i < 8; ++i)
113 m_State[i] += work[i];
114 erase(schedule, sizeof(schedule));
115 erase(work, sizeof(work));
116 }
117
118 uint32_t m_State[8] = {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
119 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
120 uint8_t m_Block[64] = {};
121 size_t m_Used = 0;
122 uint64_t m_Length = 0;
123};
124} // namespace detail
125
126inline void chacha20_block(const uint8_t key[32], const uint8_t nonce[12], uint32_t counter,
127 uint8_t output[64]) {
128 uint32_t initial[16] = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574};
129 for (size_t i = 0; i < 8; ++i)
130 initial[4 + i] = detail::little32(key + i * 4);
131 initial[12] = counter;
132 for (size_t i = 0; i < 3; ++i)
133 initial[13 + i] = detail::little32(nonce + i * 4);
134 uint32_t work[16];
135 for (size_t i = 0; i < 16; ++i)
136 work[i] = initial[i];
137 for (size_t round = 0; round < 10; ++round) {
138 detail::quarter(work[0], work[4], work[8], work[12]);
139 detail::quarter(work[1], work[5], work[9], work[13]);
140 detail::quarter(work[2], work[6], work[10], work[14]);
141 detail::quarter(work[3], work[7], work[11], work[15]);
142 detail::quarter(work[0], work[5], work[10], work[15]);
143 detail::quarter(work[1], work[6], work[11], work[12]);
144 detail::quarter(work[2], work[7], work[8], work[13]);
145 detail::quarter(work[3], work[4], work[9], work[14]);
146 }
147 for (size_t i = 0; i < 16; ++i) {
148 work[i] += initial[i];
149 for (size_t j = 0; j < 4; ++j)
150 output[4 * i + j] = static_cast<uint8_t>(work[i] >> (8 * j));
151 }
152 erase(work, sizeof(work));
153 erase(initial, sizeof(initial));
154}
155
156// Fixed 256-bit keys match the entropy and persistent-seed interfaces.
157inline void hmac_sha256(const uint8_t key[32], const void* data, size_t length,
158 uint8_t output[32]) {
159 uint8_t pad[64], digest[32];
160 for (size_t i = 0; i < sizeof(pad); ++i)
161 pad[i] = (i < 32 ? key[i] : 0) ^ 0x36;
162 detail::Sha256 inner;
163 inner.update(pad, sizeof(pad));
164 inner.update(data, length);
165 inner.finish(digest);
166 for (size_t i = 0; i < sizeof(pad); ++i)
167 pad[i] ^= 0x36 ^ 0x5c;
168 detail::Sha256 outer;
169 outer.update(pad, sizeof(pad));
170 outer.update(digest, sizeof(digest));
171 outer.finish(output);
172 erase(digest, sizeof(digest));
173 erase(pad, sizeof(pad));
174}
175} // namespace pedigree_random
176
177#endif