1*0a6a1f1dSLionel Sambuc /* $NetBSD: arc4random.c,v 1.30 2015/05/13 23:15:57 justin Exp $ */
22fe8fb19SBen Gras
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc * All rights reserved.
62fe8fb19SBen Gras *
7*0a6a1f1dSLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc * by Taylor R. Campbell.
9*0a6a1f1dSLionel Sambuc *
10*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc * are met:
13*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc *
19*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras /*
33*0a6a1f1dSLionel Sambuc * Legacy arc4random(3) API from OpenBSD reimplemented using the
34*0a6a1f1dSLionel Sambuc * ChaCha20 PRF, with per-thread state.
352fe8fb19SBen Gras *
36*0a6a1f1dSLionel Sambuc * Security model:
37*0a6a1f1dSLionel Sambuc * - An attacker who sees some outputs cannot predict past or future
38*0a6a1f1dSLionel Sambuc * outputs.
39*0a6a1f1dSLionel Sambuc * - An attacker who sees the PRNG state cannot predict past outputs.
40*0a6a1f1dSLionel Sambuc * - An attacker who sees a child's PRNG state cannot predict past or
41*0a6a1f1dSLionel Sambuc * future outputs in the parent, or in other children.
422fe8fb19SBen Gras *
43*0a6a1f1dSLionel Sambuc * The arc4random(3) API may abort the process if:
44*0a6a1f1dSLionel Sambuc *
45*0a6a1f1dSLionel Sambuc * (a) the crypto self-test fails,
46*0a6a1f1dSLionel Sambuc * (b) pthread_atfork or thr_keycreate fail, or
47*0a6a1f1dSLionel Sambuc * (c) sysctl(KERN_ARND) fails when reseeding the PRNG.
48*0a6a1f1dSLionel Sambuc *
49*0a6a1f1dSLionel Sambuc * The crypto self-test, pthread_atfork, and thr_keycreate occur only
50*0a6a1f1dSLionel Sambuc * once, on the first use of any of the arc4random(3) API. KERN_ARND
51*0a6a1f1dSLionel Sambuc * is unlikely to fail later unless the kernel is seriously broken.
522fe8fb19SBen Gras */
532fe8fb19SBen Gras
542fe8fb19SBen Gras #include <sys/cdefs.h>
55*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: arc4random.c,v 1.30 2015/05/13 23:15:57 justin Exp $");
562fe8fb19SBen Gras
572fe8fb19SBen Gras #include "namespace.h"
58f14fb602SLionel Sambuc #include "reentrant.h"
59*0a6a1f1dSLionel Sambuc
60*0a6a1f1dSLionel Sambuc #include <sys/bitops.h>
61*0a6a1f1dSLionel Sambuc #include <sys/endian.h>
62*0a6a1f1dSLionel Sambuc #include <sys/errno.h>
63*0a6a1f1dSLionel Sambuc #if defined(__minix)
64*0a6a1f1dSLionel Sambuc #include <sys/fcntl.h>
65*0a6a1f1dSLionel Sambuc #endif
66*0a6a1f1dSLionel Sambuc #include <sys/mman.h>
672fe8fb19SBen Gras #include <sys/sysctl.h>
682fe8fb19SBen Gras
69*0a6a1f1dSLionel Sambuc #include <assert.h>
70*0a6a1f1dSLionel Sambuc #include <sha2.h>
71*0a6a1f1dSLionel Sambuc #include <stdbool.h>
72*0a6a1f1dSLionel Sambuc #include <stdint.h>
73*0a6a1f1dSLionel Sambuc #include <stdlib.h>
74*0a6a1f1dSLionel Sambuc #include <string.h>
75*0a6a1f1dSLionel Sambuc #include <unistd.h>
76*0a6a1f1dSLionel Sambuc
772fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(arc4random,_arc4random)782fe8fb19SBen Gras __weak_alias(arc4random,_arc4random)
79f14fb602SLionel Sambuc __weak_alias(arc4random_addrandom,_arc4random_addrandom)
80f14fb602SLionel Sambuc __weak_alias(arc4random_buf,_arc4random_buf)
81f14fb602SLionel Sambuc __weak_alias(arc4random_stir,_arc4random_stir)
82f14fb602SLionel Sambuc __weak_alias(arc4random_uniform,_arc4random_uniform)
832fe8fb19SBen Gras #endif
842fe8fb19SBen Gras
85*0a6a1f1dSLionel Sambuc /*
86*0a6a1f1dSLionel Sambuc * For standard ChaCha, use le32dec/le32enc. We don't need that for
87*0a6a1f1dSLionel Sambuc * the purposes of a nondeterministic random number generator -- we
88*0a6a1f1dSLionel Sambuc * don't need to be bit-for-bit compatible over any wire.
89*0a6a1f1dSLionel Sambuc */
902fe8fb19SBen Gras
91*0a6a1f1dSLionel Sambuc static inline uint32_t
92*0a6a1f1dSLionel Sambuc crypto_le32dec(const void *p)
932fe8fb19SBen Gras {
94*0a6a1f1dSLionel Sambuc uint32_t v;
952fe8fb19SBen Gras
96*0a6a1f1dSLionel Sambuc (void)memcpy(&v, p, sizeof v);
97*0a6a1f1dSLionel Sambuc
98*0a6a1f1dSLionel Sambuc return v;
992fe8fb19SBen Gras }
1002fe8fb19SBen Gras
1012fe8fb19SBen Gras static inline void
crypto_le32enc(void * p,uint32_t v)102*0a6a1f1dSLionel Sambuc crypto_le32enc(void *p, uint32_t v)
1032fe8fb19SBen Gras {
1042fe8fb19SBen Gras
105*0a6a1f1dSLionel Sambuc (void)memcpy(p, &v, sizeof v);
1062fe8fb19SBen Gras }
1072fe8fb19SBen Gras
108*0a6a1f1dSLionel Sambuc /* ChaCha core */
109*0a6a1f1dSLionel Sambuc
110*0a6a1f1dSLionel Sambuc #define crypto_core_OUTPUTBYTES 64
111*0a6a1f1dSLionel Sambuc #define crypto_core_INPUTBYTES 16
112*0a6a1f1dSLionel Sambuc #define crypto_core_KEYBYTES 32
113*0a6a1f1dSLionel Sambuc #define crypto_core_CONSTBYTES 16
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel Sambuc #define crypto_core_ROUNDS 20
116*0a6a1f1dSLionel Sambuc
117*0a6a1f1dSLionel Sambuc static uint32_t
rotate(uint32_t u,unsigned c)118*0a6a1f1dSLionel Sambuc rotate(uint32_t u, unsigned c)
1192fe8fb19SBen Gras {
120*0a6a1f1dSLionel Sambuc
121*0a6a1f1dSLionel Sambuc return (u << c) | (u >> (32 - c));
122*0a6a1f1dSLionel Sambuc }
123*0a6a1f1dSLionel Sambuc
124*0a6a1f1dSLionel Sambuc #define QUARTERROUND(a, b, c, d) do { \
125*0a6a1f1dSLionel Sambuc (a) += (b); (d) ^= (a); (d) = rotate((d), 16); \
126*0a6a1f1dSLionel Sambuc (c) += (d); (b) ^= (c); (b) = rotate((b), 12); \
127*0a6a1f1dSLionel Sambuc (a) += (b); (d) ^= (a); (d) = rotate((d), 8); \
128*0a6a1f1dSLionel Sambuc (c) += (d); (b) ^= (c); (b) = rotate((b), 7); \
129*0a6a1f1dSLionel Sambuc } while (/*CONSTCOND*/0)
130*0a6a1f1dSLionel Sambuc
131*0a6a1f1dSLionel Sambuc const uint8_t crypto_core_constant32[16] = "expand 32-byte k";
132*0a6a1f1dSLionel Sambuc
133*0a6a1f1dSLionel Sambuc static void
crypto_core(uint8_t * out,const uint8_t * in,const uint8_t * k,const uint8_t * c)134*0a6a1f1dSLionel Sambuc crypto_core(uint8_t *out, const uint8_t *in, const uint8_t *k,
135*0a6a1f1dSLionel Sambuc const uint8_t *c)
136*0a6a1f1dSLionel Sambuc {
137*0a6a1f1dSLionel Sambuc uint32_t x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15;
138*0a6a1f1dSLionel Sambuc uint32_t j0,j1,j2,j3,j4,j5,j6,j7,j8,j9,j10,j11,j12,j13,j14,j15;
139*0a6a1f1dSLionel Sambuc int i;
140*0a6a1f1dSLionel Sambuc
141*0a6a1f1dSLionel Sambuc j0 = x0 = crypto_le32dec(c + 0);
142*0a6a1f1dSLionel Sambuc j1 = x1 = crypto_le32dec(c + 4);
143*0a6a1f1dSLionel Sambuc j2 = x2 = crypto_le32dec(c + 8);
144*0a6a1f1dSLionel Sambuc j3 = x3 = crypto_le32dec(c + 12);
145*0a6a1f1dSLionel Sambuc j4 = x4 = crypto_le32dec(k + 0);
146*0a6a1f1dSLionel Sambuc j5 = x5 = crypto_le32dec(k + 4);
147*0a6a1f1dSLionel Sambuc j6 = x6 = crypto_le32dec(k + 8);
148*0a6a1f1dSLionel Sambuc j7 = x7 = crypto_le32dec(k + 12);
149*0a6a1f1dSLionel Sambuc j8 = x8 = crypto_le32dec(k + 16);
150*0a6a1f1dSLionel Sambuc j9 = x9 = crypto_le32dec(k + 20);
151*0a6a1f1dSLionel Sambuc j10 = x10 = crypto_le32dec(k + 24);
152*0a6a1f1dSLionel Sambuc j11 = x11 = crypto_le32dec(k + 28);
153*0a6a1f1dSLionel Sambuc j12 = x12 = crypto_le32dec(in + 0);
154*0a6a1f1dSLionel Sambuc j13 = x13 = crypto_le32dec(in + 4);
155*0a6a1f1dSLionel Sambuc j14 = x14 = crypto_le32dec(in + 8);
156*0a6a1f1dSLionel Sambuc j15 = x15 = crypto_le32dec(in + 12);
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc for (i = crypto_core_ROUNDS; i > 0; i -= 2) {
159*0a6a1f1dSLionel Sambuc QUARTERROUND( x0, x4, x8,x12);
160*0a6a1f1dSLionel Sambuc QUARTERROUND( x1, x5, x9,x13);
161*0a6a1f1dSLionel Sambuc QUARTERROUND( x2, x6,x10,x14);
162*0a6a1f1dSLionel Sambuc QUARTERROUND( x3, x7,x11,x15);
163*0a6a1f1dSLionel Sambuc QUARTERROUND( x0, x5,x10,x15);
164*0a6a1f1dSLionel Sambuc QUARTERROUND( x1, x6,x11,x12);
165*0a6a1f1dSLionel Sambuc QUARTERROUND( x2, x7, x8,x13);
166*0a6a1f1dSLionel Sambuc QUARTERROUND( x3, x4, x9,x14);
167*0a6a1f1dSLionel Sambuc }
168*0a6a1f1dSLionel Sambuc
169*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 0, x0 + j0);
170*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 4, x1 + j1);
171*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 8, x2 + j2);
172*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 12, x3 + j3);
173*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 16, x4 + j4);
174*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 20, x5 + j5);
175*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 24, x6 + j6);
176*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 28, x7 + j7);
177*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 32, x8 + j8);
178*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 36, x9 + j9);
179*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 40, x10 + j10);
180*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 44, x11 + j11);
181*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 48, x12 + j12);
182*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 52, x13 + j13);
183*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 56, x14 + j14);
184*0a6a1f1dSLionel Sambuc crypto_le32enc(out + 60, x15 + j15);
185*0a6a1f1dSLionel Sambuc }
186*0a6a1f1dSLionel Sambuc
187*0a6a1f1dSLionel Sambuc /* ChaCha self-test */
188*0a6a1f1dSLionel Sambuc
189*0a6a1f1dSLionel Sambuc #ifdef _DIAGNOSTIC
190*0a6a1f1dSLionel Sambuc
191*0a6a1f1dSLionel Sambuc /*
192*0a6a1f1dSLionel Sambuc * Test vector for ChaCha20 from
193*0a6a1f1dSLionel Sambuc * <http://tools.ietf.org/html/draft-strombergson-chacha-test-vectors-00>,
194*0a6a1f1dSLionel Sambuc * test vectors for ChaCha12 and ChaCha8 and for big-endian machines
195*0a6a1f1dSLionel Sambuc * generated by the same crypto_core code with crypto_core_ROUNDS and
196*0a6a1f1dSLionel Sambuc * crypto_le32enc/dec varied.
197*0a6a1f1dSLionel Sambuc */
198*0a6a1f1dSLionel Sambuc
199*0a6a1f1dSLionel Sambuc static const uint8_t crypto_core_selftest_vector[64] = {
200*0a6a1f1dSLionel Sambuc #if _BYTE_ORDER == _LITTLE_ENDIAN
201*0a6a1f1dSLionel Sambuc # if crypto_core_ROUNDS == 8
202*0a6a1f1dSLionel Sambuc 0x3e,0x00,0xef,0x2f,0x89,0x5f,0x40,0xd6,
203*0a6a1f1dSLionel Sambuc 0x7f,0x5b,0xb8,0xe8,0x1f,0x09,0xa5,0xa1,
204*0a6a1f1dSLionel Sambuc 0x2c,0x84,0x0e,0xc3,0xce,0x9a,0x7f,0x3b,
205*0a6a1f1dSLionel Sambuc 0x18,0x1b,0xe1,0x88,0xef,0x71,0x1a,0x1e,
206*0a6a1f1dSLionel Sambuc 0x98,0x4c,0xe1,0x72,0xb9,0x21,0x6f,0x41,
207*0a6a1f1dSLionel Sambuc 0x9f,0x44,0x53,0x67,0x45,0x6d,0x56,0x19,
208*0a6a1f1dSLionel Sambuc 0x31,0x4a,0x42,0xa3,0xda,0x86,0xb0,0x01,
209*0a6a1f1dSLionel Sambuc 0x38,0x7b,0xfd,0xb8,0x0e,0x0c,0xfe,0x42,
210*0a6a1f1dSLionel Sambuc # elif crypto_core_ROUNDS == 12
211*0a6a1f1dSLionel Sambuc 0x9b,0xf4,0x9a,0x6a,0x07,0x55,0xf9,0x53,
212*0a6a1f1dSLionel Sambuc 0x81,0x1f,0xce,0x12,0x5f,0x26,0x83,0xd5,
213*0a6a1f1dSLionel Sambuc 0x04,0x29,0xc3,0xbb,0x49,0xe0,0x74,0x14,
214*0a6a1f1dSLionel Sambuc 0x7e,0x00,0x89,0xa5,0x2e,0xae,0x15,0x5f,
215*0a6a1f1dSLionel Sambuc 0x05,0x64,0xf8,0x79,0xd2,0x7a,0xe3,0xc0,
216*0a6a1f1dSLionel Sambuc 0x2c,0xe8,0x28,0x34,0xac,0xfa,0x8c,0x79,
217*0a6a1f1dSLionel Sambuc 0x3a,0x62,0x9f,0x2c,0xa0,0xde,0x69,0x19,
218*0a6a1f1dSLionel Sambuc 0x61,0x0b,0xe8,0x2f,0x41,0x13,0x26,0xbe,
219*0a6a1f1dSLionel Sambuc # elif crypto_core_ROUNDS == 20
220*0a6a1f1dSLionel Sambuc 0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90,
221*0a6a1f1dSLionel Sambuc 0x40,0x5d,0x6a,0xe5,0x53,0x86,0xbd,0x28,
222*0a6a1f1dSLionel Sambuc 0xbd,0xd2,0x19,0xb8,0xa0,0x8d,0xed,0x1a,
223*0a6a1f1dSLionel Sambuc 0xa8,0x36,0xef,0xcc,0x8b,0x77,0x0d,0xc7,
224*0a6a1f1dSLionel Sambuc 0xda,0x41,0x59,0x7c,0x51,0x57,0x48,0x8d,
225*0a6a1f1dSLionel Sambuc 0x77,0x24,0xe0,0x3f,0xb8,0xd8,0x4a,0x37,
226*0a6a1f1dSLionel Sambuc 0x6a,0x43,0xb8,0xf4,0x15,0x18,0xa1,0x1c,
227*0a6a1f1dSLionel Sambuc 0xc3,0x87,0xb6,0x69,0xb2,0xee,0x65,0x86,
228*0a6a1f1dSLionel Sambuc # else
229*0a6a1f1dSLionel Sambuc # error crypto_core_ROUNDS must be 8, 12, or 20.
230*0a6a1f1dSLionel Sambuc # endif
231*0a6a1f1dSLionel Sambuc #elif _BYTE_ORDER == _BIG_ENDIAN
232*0a6a1f1dSLionel Sambuc # if crypto_core_ROUNDS == 8
233*0a6a1f1dSLionel Sambuc 0x9a,0x13,0x07,0xe3,0x38,0x18,0x9e,0x99,
234*0a6a1f1dSLionel Sambuc 0x15,0x37,0x16,0x4d,0x04,0xe6,0x48,0x9a,
235*0a6a1f1dSLionel Sambuc 0x07,0xd6,0xe8,0x7a,0x02,0xf9,0xf5,0xc7,
236*0a6a1f1dSLionel Sambuc 0x3f,0xa9,0xc2,0x0a,0xe1,0xc6,0x62,0xea,
237*0a6a1f1dSLionel Sambuc 0x80,0xaf,0xb6,0x51,0xca,0x52,0x43,0x87,
238*0a6a1f1dSLionel Sambuc 0xe3,0xa6,0xa6,0x61,0x11,0xf5,0xe6,0xcf,
239*0a6a1f1dSLionel Sambuc 0x09,0x0f,0xdc,0x9d,0xc3,0xc3,0xbb,0x43,
240*0a6a1f1dSLionel Sambuc 0xd7,0xfa,0x70,0x42,0xbf,0xa5,0xee,0xa2,
241*0a6a1f1dSLionel Sambuc # elif crypto_core_ROUNDS == 12
242*0a6a1f1dSLionel Sambuc 0xcf,0x6c,0x16,0x48,0xbf,0xf4,0xba,0x85,
243*0a6a1f1dSLionel Sambuc 0x32,0x69,0xd3,0x98,0xc8,0x7d,0xcd,0x3f,
244*0a6a1f1dSLionel Sambuc 0xdc,0x76,0x6b,0xa2,0x7b,0xcb,0x17,0x4d,
245*0a6a1f1dSLionel Sambuc 0x05,0xda,0xdd,0xd8,0x62,0x54,0xbf,0xe0,
246*0a6a1f1dSLionel Sambuc 0x65,0xed,0x0e,0xf4,0x01,0x7e,0x3c,0x05,
247*0a6a1f1dSLionel Sambuc 0x35,0xb2,0x7a,0x60,0xf3,0x8f,0x12,0x33,
248*0a6a1f1dSLionel Sambuc 0x24,0x60,0xcd,0x85,0xfe,0x4c,0xf3,0x39,
249*0a6a1f1dSLionel Sambuc 0xb1,0x0e,0x3e,0xe0,0xba,0xa6,0x2f,0xa9,
250*0a6a1f1dSLionel Sambuc # elif crypto_core_ROUNDS == 20
251*0a6a1f1dSLionel Sambuc 0x83,0x8b,0xf8,0x75,0xf7,0xde,0x9d,0x8c,
252*0a6a1f1dSLionel Sambuc 0x33,0x14,0x72,0x28,0xd1,0xbe,0x88,0xe5,
253*0a6a1f1dSLionel Sambuc 0x94,0xb5,0xed,0xb8,0x56,0xb5,0x9e,0x0c,
254*0a6a1f1dSLionel Sambuc 0x64,0x6a,0xaf,0xd9,0xa7,0x49,0x10,0x59,
255*0a6a1f1dSLionel Sambuc 0xba,0x3a,0x82,0xf8,0x4a,0x70,0x9c,0x00,
256*0a6a1f1dSLionel Sambuc 0x82,0x2c,0xae,0xc6,0xd7,0x1c,0x2e,0xda,
257*0a6a1f1dSLionel Sambuc 0x2a,0xfb,0x61,0x70,0x2b,0xd1,0xbf,0x8b,
258*0a6a1f1dSLionel Sambuc 0x95,0xbc,0x23,0xb6,0x4b,0x60,0x02,0xec,
259*0a6a1f1dSLionel Sambuc # else
260*0a6a1f1dSLionel Sambuc # error crypto_core_ROUNDS must be 8, 12, or 20.
261*0a6a1f1dSLionel Sambuc # endif
262*0a6a1f1dSLionel Sambuc #else
263*0a6a1f1dSLionel Sambuc # error Byte order must be little-endian or big-endian.
264*0a6a1f1dSLionel Sambuc #endif
265*0a6a1f1dSLionel Sambuc };
266*0a6a1f1dSLionel Sambuc
267*0a6a1f1dSLionel Sambuc static int
crypto_core_selftest(void)268*0a6a1f1dSLionel Sambuc crypto_core_selftest(void)
269*0a6a1f1dSLionel Sambuc {
270*0a6a1f1dSLionel Sambuc const uint8_t nonce[crypto_core_INPUTBYTES] = {0};
271*0a6a1f1dSLionel Sambuc const uint8_t key[crypto_core_KEYBYTES] = {0};
272*0a6a1f1dSLionel Sambuc uint8_t block[64];
273*0a6a1f1dSLionel Sambuc unsigned i;
274*0a6a1f1dSLionel Sambuc
275*0a6a1f1dSLionel Sambuc crypto_core(block, nonce, key, crypto_core_constant32);
276*0a6a1f1dSLionel Sambuc for (i = 0; i < 64; i++) {
277*0a6a1f1dSLionel Sambuc if (block[i] != crypto_core_selftest_vector[i])
278*0a6a1f1dSLionel Sambuc return EIO;
279*0a6a1f1dSLionel Sambuc }
280*0a6a1f1dSLionel Sambuc
281*0a6a1f1dSLionel Sambuc return 0;
282*0a6a1f1dSLionel Sambuc }
283*0a6a1f1dSLionel Sambuc
284*0a6a1f1dSLionel Sambuc #else /* !_DIAGNOSTIC */
285*0a6a1f1dSLionel Sambuc
286*0a6a1f1dSLionel Sambuc static int
crypto_core_selftest(void)287*0a6a1f1dSLionel Sambuc crypto_core_selftest(void)
288*0a6a1f1dSLionel Sambuc {
289*0a6a1f1dSLionel Sambuc
290*0a6a1f1dSLionel Sambuc return 0;
291*0a6a1f1dSLionel Sambuc }
292*0a6a1f1dSLionel Sambuc
293*0a6a1f1dSLionel Sambuc #endif
294*0a6a1f1dSLionel Sambuc
295*0a6a1f1dSLionel Sambuc /* PRNG */
296*0a6a1f1dSLionel Sambuc
297*0a6a1f1dSLionel Sambuc /*
298*0a6a1f1dSLionel Sambuc * For a state s, rather than use ChaCha20 as a stream cipher to
299*0a6a1f1dSLionel Sambuc * generate the concatenation ChaCha20_s(0) || ChaCha20_s(1) || ..., we
300*0a6a1f1dSLionel Sambuc * split ChaCha20_s(0) into s' || x and yield x for the first request,
301*0a6a1f1dSLionel Sambuc * split ChaCha20_s'(0) into s'' || y and yield y for the second
302*0a6a1f1dSLionel Sambuc * request, &c. This provides backtracking resistance: an attacker who
303*0a6a1f1dSLionel Sambuc * finds s'' can't recover s' or x.
304*0a6a1f1dSLionel Sambuc */
305*0a6a1f1dSLionel Sambuc
306*0a6a1f1dSLionel Sambuc #define crypto_prng_SEEDBYTES crypto_core_KEYBYTES
307*0a6a1f1dSLionel Sambuc #define crypto_prng_MAXOUTPUTBYTES \
308*0a6a1f1dSLionel Sambuc (crypto_core_OUTPUTBYTES - crypto_prng_SEEDBYTES)
309*0a6a1f1dSLionel Sambuc
310*0a6a1f1dSLionel Sambuc struct crypto_prng {
311*0a6a1f1dSLionel Sambuc uint8_t state[crypto_prng_SEEDBYTES];
312*0a6a1f1dSLionel Sambuc };
313*0a6a1f1dSLionel Sambuc
314*0a6a1f1dSLionel Sambuc static void
crypto_prng_seed(struct crypto_prng * prng,const void * seed)315*0a6a1f1dSLionel Sambuc crypto_prng_seed(struct crypto_prng *prng, const void *seed)
316*0a6a1f1dSLionel Sambuc {
317*0a6a1f1dSLionel Sambuc
318*0a6a1f1dSLionel Sambuc (void)memcpy(prng->state, seed, crypto_prng_SEEDBYTES);
319*0a6a1f1dSLionel Sambuc }
320*0a6a1f1dSLionel Sambuc
321*0a6a1f1dSLionel Sambuc static void
crypto_prng_buf(struct crypto_prng * prng,void * buf,size_t n)322*0a6a1f1dSLionel Sambuc crypto_prng_buf(struct crypto_prng *prng, void *buf, size_t n)
323*0a6a1f1dSLionel Sambuc {
324*0a6a1f1dSLionel Sambuc const uint8_t nonce[crypto_core_INPUTBYTES] = {0};
325*0a6a1f1dSLionel Sambuc uint8_t output[crypto_core_OUTPUTBYTES];
326*0a6a1f1dSLionel Sambuc
327*0a6a1f1dSLionel Sambuc _DIAGASSERT(n <= crypto_prng_MAXOUTPUTBYTES);
328*0a6a1f1dSLionel Sambuc __CTASSERT(sizeof prng->state + crypto_prng_MAXOUTPUTBYTES
329*0a6a1f1dSLionel Sambuc <= sizeof output);
330*0a6a1f1dSLionel Sambuc
331*0a6a1f1dSLionel Sambuc crypto_core(output, nonce, prng->state, crypto_core_constant32);
332*0a6a1f1dSLionel Sambuc (void)memcpy(prng->state, output, sizeof prng->state);
333*0a6a1f1dSLionel Sambuc (void)memcpy(buf, output + sizeof prng->state, n);
334*0a6a1f1dSLionel Sambuc (void)explicit_memset(output, 0, sizeof output);
335*0a6a1f1dSLionel Sambuc }
336*0a6a1f1dSLionel Sambuc
337*0a6a1f1dSLionel Sambuc /* One-time stream: expand short single-use secret into long secret */
338*0a6a1f1dSLionel Sambuc
339*0a6a1f1dSLionel Sambuc #define crypto_onetimestream_SEEDBYTES crypto_core_KEYBYTES
340*0a6a1f1dSLionel Sambuc
341*0a6a1f1dSLionel Sambuc static void
crypto_onetimestream(const void * seed,void * buf,size_t n)342*0a6a1f1dSLionel Sambuc crypto_onetimestream(const void *seed, void *buf, size_t n)
343*0a6a1f1dSLionel Sambuc {
344*0a6a1f1dSLionel Sambuc uint32_t nonce[crypto_core_INPUTBYTES / sizeof(uint32_t)] = {0};
345*0a6a1f1dSLionel Sambuc uint8_t block[crypto_core_OUTPUTBYTES];
346*0a6a1f1dSLionel Sambuc uint8_t *p8, *p32;
347*0a6a1f1dSLionel Sambuc const uint8_t *nonce8 = (const uint8_t *)(void *)nonce;
348*0a6a1f1dSLionel Sambuc size_t ni, nb, nf;
349*0a6a1f1dSLionel Sambuc
350*0a6a1f1dSLionel Sambuc /*
351*0a6a1f1dSLionel Sambuc * Guarantee we can generate up to n bytes. We have
352*0a6a1f1dSLionel Sambuc * 2^(8*INPUTBYTES) possible inputs yielding output of
353*0a6a1f1dSLionel Sambuc * OUTPUTBYTES*2^(8*INPUTBYTES) bytes. It suffices to require
354*0a6a1f1dSLionel Sambuc * that sizeof n > (1/CHAR_BIT) log_2 n be less than
355*0a6a1f1dSLionel Sambuc * (1/CHAR_BIT) log_2 of the total output stream length. We
356*0a6a1f1dSLionel Sambuc * have
357*0a6a1f1dSLionel Sambuc *
358*0a6a1f1dSLionel Sambuc * log_2 (o 2^(8 i)) = log_2 o + log_2 2^(8 i)
359*0a6a1f1dSLionel Sambuc * = log_2 o + 8 i.
360*0a6a1f1dSLionel Sambuc */
361*0a6a1f1dSLionel Sambuc __CTASSERT(CHAR_BIT * sizeof n <=
362*0a6a1f1dSLionel Sambuc (/*LINTED*/ilog2(crypto_core_OUTPUTBYTES) + 8 * crypto_core_INPUTBYTES));
363*0a6a1f1dSLionel Sambuc
364*0a6a1f1dSLionel Sambuc p8 = buf;
365*0a6a1f1dSLionel Sambuc p32 = (uint8_t *)roundup2((uintptr_t)p8, 4);
366*0a6a1f1dSLionel Sambuc ni = p32 - p8;
367*0a6a1f1dSLionel Sambuc if (n < ni)
368*0a6a1f1dSLionel Sambuc ni = n;
369*0a6a1f1dSLionel Sambuc nb = (n - ni) / sizeof block;
370*0a6a1f1dSLionel Sambuc nf = (n - ni) % sizeof block;
371*0a6a1f1dSLionel Sambuc
372*0a6a1f1dSLionel Sambuc _DIAGASSERT(((uintptr_t)p32 & 3) == 0);
373*0a6a1f1dSLionel Sambuc _DIAGASSERT(ni <= n);
374*0a6a1f1dSLionel Sambuc _DIAGASSERT(nb <= (n / sizeof block));
375*0a6a1f1dSLionel Sambuc _DIAGASSERT(nf <= n);
376*0a6a1f1dSLionel Sambuc _DIAGASSERT(n == (ni + (nb * sizeof block) + nf));
377*0a6a1f1dSLionel Sambuc _DIAGASSERT(ni < 4);
378*0a6a1f1dSLionel Sambuc _DIAGASSERT(nf < sizeof block);
379*0a6a1f1dSLionel Sambuc
380*0a6a1f1dSLionel Sambuc if (ni) {
381*0a6a1f1dSLionel Sambuc crypto_core(block, nonce8, seed, crypto_core_constant32);
382*0a6a1f1dSLionel Sambuc nonce[0]++;
383*0a6a1f1dSLionel Sambuc (void)memcpy(p8, block, ni);
384*0a6a1f1dSLionel Sambuc }
385*0a6a1f1dSLionel Sambuc while (nb--) {
386*0a6a1f1dSLionel Sambuc crypto_core(p32, nonce8, seed, crypto_core_constant32);
387*0a6a1f1dSLionel Sambuc if (++nonce[0] == 0)
388*0a6a1f1dSLionel Sambuc nonce[1]++;
389*0a6a1f1dSLionel Sambuc p32 += crypto_core_OUTPUTBYTES;
390*0a6a1f1dSLionel Sambuc }
391*0a6a1f1dSLionel Sambuc if (nf) {
392*0a6a1f1dSLionel Sambuc crypto_core(block, nonce8, seed, crypto_core_constant32);
393*0a6a1f1dSLionel Sambuc if (++nonce[0] == 0)
394*0a6a1f1dSLionel Sambuc nonce[1]++;
395*0a6a1f1dSLionel Sambuc (void)memcpy(p32, block, nf);
396*0a6a1f1dSLionel Sambuc }
397*0a6a1f1dSLionel Sambuc
398*0a6a1f1dSLionel Sambuc if (ni | nf)
399*0a6a1f1dSLionel Sambuc (void)explicit_memset(block, 0, sizeof block);
400*0a6a1f1dSLionel Sambuc }
401*0a6a1f1dSLionel Sambuc
402*0a6a1f1dSLionel Sambuc /* arc4random state: per-thread, per-process (zeroed in child on fork) */
403*0a6a1f1dSLionel Sambuc
404*0a6a1f1dSLionel Sambuc struct arc4random_prng {
405*0a6a1f1dSLionel Sambuc struct crypto_prng arc4_prng;
406*0a6a1f1dSLionel Sambuc bool arc4_seeded;
407*0a6a1f1dSLionel Sambuc };
408*0a6a1f1dSLionel Sambuc
409*0a6a1f1dSLionel Sambuc static void
arc4random_prng_addrandom(struct arc4random_prng * prng,const void * data,size_t datalen)410*0a6a1f1dSLionel Sambuc arc4random_prng_addrandom(struct arc4random_prng *prng, const void *data,
411*0a6a1f1dSLionel Sambuc size_t datalen)
412*0a6a1f1dSLionel Sambuc {
413*0a6a1f1dSLionel Sambuc #if !defined(__minix)
414*0a6a1f1dSLionel Sambuc const int mib[] = { CTL_KERN, KERN_ARND };
415*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
416*0a6a1f1dSLionel Sambuc SHA256_CTX ctx;
417*0a6a1f1dSLionel Sambuc uint8_t buf[crypto_prng_SEEDBYTES];
418*0a6a1f1dSLionel Sambuc size_t buflen = sizeof buf;
419*0a6a1f1dSLionel Sambuc
420*0a6a1f1dSLionel Sambuc __CTASSERT(sizeof buf == SHA256_DIGEST_LENGTH);
421*0a6a1f1dSLionel Sambuc
422*0a6a1f1dSLionel Sambuc SHA256_Init(&ctx);
423*0a6a1f1dSLionel Sambuc
424*0a6a1f1dSLionel Sambuc crypto_prng_buf(&prng->arc4_prng, buf, sizeof buf);
425*0a6a1f1dSLionel Sambuc SHA256_Update(&ctx, buf, sizeof buf);
426*0a6a1f1dSLionel Sambuc
42784d9c625SLionel Sambuc #if defined(__minix)
428f14fb602SLionel Sambuc /* LSC: We do not have a compatibility layer for the
429*0a6a1f1dSLionel Sambuc * KERN_ARND call, so do it the old way... */
4302fe8fb19SBen Gras int fd;
4312fe8fb19SBen Gras
4322fe8fb19SBen Gras fd = open("/dev/urandom", O_RDONLY);
4332fe8fb19SBen Gras if (fd != -1) {
434*0a6a1f1dSLionel Sambuc (void)read(fd, buf, buflen);
4352fe8fb19SBen Gras close(fd);
4362fe8fb19SBen Gras }
4372fe8fb19SBen Gras
4382fe8fb19SBen Gras /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take
4392fe8fb19SBen Gras * whatever was on the stack... */
440f14fb602SLionel Sambuc #else
441*0a6a1f1dSLionel Sambuc if (sysctl(mib, (u_int)__arraycount(mib), buf, &buflen, NULL, 0) == -1)
442f14fb602SLionel Sambuc abort();
44384d9c625SLionel Sambuc #endif /* !defined(__minix) */
444*0a6a1f1dSLionel Sambuc if (buflen != sizeof buf)
445*0a6a1f1dSLionel Sambuc abort();
446*0a6a1f1dSLionel Sambuc SHA256_Update(&ctx, buf, sizeof buf);
447f14fb602SLionel Sambuc
448*0a6a1f1dSLionel Sambuc if (data != NULL)
449*0a6a1f1dSLionel Sambuc SHA256_Update(&ctx, data, datalen);
4502fe8fb19SBen Gras
451*0a6a1f1dSLionel Sambuc SHA256_Final(buf, &ctx);
452*0a6a1f1dSLionel Sambuc (void)explicit_memset(&ctx, 0, sizeof ctx);
453f14fb602SLionel Sambuc
454*0a6a1f1dSLionel Sambuc /* reseed(SHA256(prng() || sysctl(KERN_ARND) || data)) */
455*0a6a1f1dSLionel Sambuc crypto_prng_seed(&prng->arc4_prng, buf);
456*0a6a1f1dSLionel Sambuc (void)explicit_memset(buf, 0, sizeof buf);
457*0a6a1f1dSLionel Sambuc prng->arc4_seeded = true;
4582fe8fb19SBen Gras }
4592fe8fb19SBen Gras
460*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
461*0a6a1f1dSLionel Sambuc static struct arc4random_prng *
arc4random_prng_create(void)462*0a6a1f1dSLionel Sambuc arc4random_prng_create(void)
4632fe8fb19SBen Gras {
464*0a6a1f1dSLionel Sambuc struct arc4random_prng *prng;
465*0a6a1f1dSLionel Sambuc const size_t size = roundup(sizeof(*prng), sysconf(_SC_PAGESIZE));
4662fe8fb19SBen Gras
467*0a6a1f1dSLionel Sambuc prng = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0);
468*0a6a1f1dSLionel Sambuc if (prng == MAP_FAILED)
469*0a6a1f1dSLionel Sambuc goto fail0;
470*0a6a1f1dSLionel Sambuc if (minherit(prng, size, MAP_INHERIT_ZERO) == -1)
471*0a6a1f1dSLionel Sambuc goto fail1;
472*0a6a1f1dSLionel Sambuc
473*0a6a1f1dSLionel Sambuc return prng;
474*0a6a1f1dSLionel Sambuc
475*0a6a1f1dSLionel Sambuc fail1: (void)munmap(prng, size);
476*0a6a1f1dSLionel Sambuc fail0: return NULL;
4772fe8fb19SBen Gras }
478*0a6a1f1dSLionel Sambuc #endif
4792fe8fb19SBen Gras
480*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
481*0a6a1f1dSLionel Sambuc static void
arc4random_prng_destroy(struct arc4random_prng * prng)482*0a6a1f1dSLionel Sambuc arc4random_prng_destroy(struct arc4random_prng *prng)
4832fe8fb19SBen Gras {
484*0a6a1f1dSLionel Sambuc const size_t size = roundup(sizeof(*prng), sysconf(_SC_PAGESIZE));
485*0a6a1f1dSLionel Sambuc
486*0a6a1f1dSLionel Sambuc (void)explicit_memset(prng, 0, sizeof(*prng));
487*0a6a1f1dSLionel Sambuc (void)munmap(prng, size);
488*0a6a1f1dSLionel Sambuc }
489*0a6a1f1dSLionel Sambuc #endif
490*0a6a1f1dSLionel Sambuc
491*0a6a1f1dSLionel Sambuc /* Library state */
492*0a6a1f1dSLionel Sambuc
493*0a6a1f1dSLionel Sambuc static struct arc4random_global {
494*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
495*0a6a1f1dSLionel Sambuc mutex_t lock;
496*0a6a1f1dSLionel Sambuc thread_key_t thread_key;
497*0a6a1f1dSLionel Sambuc #endif
498*0a6a1f1dSLionel Sambuc struct arc4random_prng prng;
499*0a6a1f1dSLionel Sambuc bool initialized;
500*0a6a1f1dSLionel Sambuc } arc4random_global = {
501*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
502*0a6a1f1dSLionel Sambuc .lock = MUTEX_INITIALIZER,
503*0a6a1f1dSLionel Sambuc #endif
504*0a6a1f1dSLionel Sambuc .initialized = false,
505*0a6a1f1dSLionel Sambuc };
506*0a6a1f1dSLionel Sambuc
507*0a6a1f1dSLionel Sambuc static void
arc4random_atfork_prepare(void)508*0a6a1f1dSLionel Sambuc arc4random_atfork_prepare(void)
509*0a6a1f1dSLionel Sambuc {
510*0a6a1f1dSLionel Sambuc
511*0a6a1f1dSLionel Sambuc mutex_lock(&arc4random_global.lock);
512*0a6a1f1dSLionel Sambuc (void)explicit_memset(&arc4random_global.prng, 0,
513*0a6a1f1dSLionel Sambuc sizeof arc4random_global.prng);
514f14fb602SLionel Sambuc }
515f14fb602SLionel Sambuc
516*0a6a1f1dSLionel Sambuc static void
arc4random_atfork_parent(void)517*0a6a1f1dSLionel Sambuc arc4random_atfork_parent(void)
518f14fb602SLionel Sambuc {
519*0a6a1f1dSLionel Sambuc
520*0a6a1f1dSLionel Sambuc mutex_unlock(&arc4random_global.lock);
5212fe8fb19SBen Gras }
5222fe8fb19SBen Gras
523*0a6a1f1dSLionel Sambuc static void
arc4random_atfork_child(void)524*0a6a1f1dSLionel Sambuc arc4random_atfork_child(void)
5252fe8fb19SBen Gras {
526*0a6a1f1dSLionel Sambuc
527*0a6a1f1dSLionel Sambuc mutex_unlock(&arc4random_global.lock);
5282fe8fb19SBen Gras }
5292fe8fb19SBen Gras
530*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
531*0a6a1f1dSLionel Sambuc static void
arc4random_tsd_destructor(void * p)532*0a6a1f1dSLionel Sambuc arc4random_tsd_destructor(void *p)
5332fe8fb19SBen Gras {
534*0a6a1f1dSLionel Sambuc struct arc4random_prng *const prng = p;
535*0a6a1f1dSLionel Sambuc
536*0a6a1f1dSLionel Sambuc arc4random_prng_destroy(prng);
5372fe8fb19SBen Gras }
538*0a6a1f1dSLionel Sambuc #endif
539*0a6a1f1dSLionel Sambuc
540*0a6a1f1dSLionel Sambuc static void
arc4random_initialize(void)541*0a6a1f1dSLionel Sambuc arc4random_initialize(void)
542*0a6a1f1dSLionel Sambuc {
543*0a6a1f1dSLionel Sambuc
544*0a6a1f1dSLionel Sambuc mutex_lock(&arc4random_global.lock);
545*0a6a1f1dSLionel Sambuc if (!arc4random_global.initialized) {
546*0a6a1f1dSLionel Sambuc if (crypto_core_selftest() != 0)
547*0a6a1f1dSLionel Sambuc abort();
548*0a6a1f1dSLionel Sambuc #if !defined(__minix)
549*0a6a1f1dSLionel Sambuc if (pthread_atfork(&arc4random_atfork_prepare,
550*0a6a1f1dSLionel Sambuc &arc4random_atfork_parent, &arc4random_atfork_child)
551*0a6a1f1dSLionel Sambuc != 0)
552*0a6a1f1dSLionel Sambuc abort();
553*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
554*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
555*0a6a1f1dSLionel Sambuc if (thr_keycreate(&arc4random_global.thread_key,
556*0a6a1f1dSLionel Sambuc &arc4random_tsd_destructor) != 0)
557*0a6a1f1dSLionel Sambuc abort();
558*0a6a1f1dSLionel Sambuc #endif
559*0a6a1f1dSLionel Sambuc arc4random_global.initialized = true;
560*0a6a1f1dSLionel Sambuc }
561*0a6a1f1dSLionel Sambuc mutex_unlock(&arc4random_global.lock);
562*0a6a1f1dSLionel Sambuc }
563*0a6a1f1dSLionel Sambuc
564*0a6a1f1dSLionel Sambuc static struct arc4random_prng *
arc4random_prng_get(void)565*0a6a1f1dSLionel Sambuc arc4random_prng_get(void)
566*0a6a1f1dSLionel Sambuc {
567*0a6a1f1dSLionel Sambuc struct arc4random_prng *prng = NULL;
568*0a6a1f1dSLionel Sambuc
569*0a6a1f1dSLionel Sambuc /* Make sure the library is initialized. */
570*0a6a1f1dSLionel Sambuc if (__predict_false(!arc4random_global.initialized))
571*0a6a1f1dSLionel Sambuc arc4random_initialize();
572*0a6a1f1dSLionel Sambuc
573*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
574*0a6a1f1dSLionel Sambuc /* Get or create the per-thread PRNG state. */
575*0a6a1f1dSLionel Sambuc prng = thr_getspecific(arc4random_global.thread_key);
576*0a6a1f1dSLionel Sambuc if (__predict_false(prng == NULL)) {
577*0a6a1f1dSLionel Sambuc prng = arc4random_prng_create();
578*0a6a1f1dSLionel Sambuc thr_setspecific(arc4random_global.thread_key, prng);
579*0a6a1f1dSLionel Sambuc }
580*0a6a1f1dSLionel Sambuc #endif
581*0a6a1f1dSLionel Sambuc
582*0a6a1f1dSLionel Sambuc /* If we can't create it, fall back to the global PRNG. */
583*0a6a1f1dSLionel Sambuc if (__predict_false(prng == NULL)) {
584*0a6a1f1dSLionel Sambuc mutex_lock(&arc4random_global.lock);
585*0a6a1f1dSLionel Sambuc prng = &arc4random_global.prng;
586*0a6a1f1dSLionel Sambuc }
587*0a6a1f1dSLionel Sambuc
588*0a6a1f1dSLionel Sambuc /* Guarantee the PRNG is seeded. */
589*0a6a1f1dSLionel Sambuc if (__predict_false(!prng->arc4_seeded))
590*0a6a1f1dSLionel Sambuc arc4random_prng_addrandom(prng, NULL, 0);
591*0a6a1f1dSLionel Sambuc
592*0a6a1f1dSLionel Sambuc return prng;
593*0a6a1f1dSLionel Sambuc }
594*0a6a1f1dSLionel Sambuc
595*0a6a1f1dSLionel Sambuc static void
arc4random_prng_put(struct arc4random_prng * prng)596*0a6a1f1dSLionel Sambuc arc4random_prng_put(struct arc4random_prng *prng)
597*0a6a1f1dSLionel Sambuc {
598*0a6a1f1dSLionel Sambuc
599*0a6a1f1dSLionel Sambuc /* If we had fallen back to the global PRNG, unlock it. */
600*0a6a1f1dSLionel Sambuc if (__predict_false(prng == &arc4random_global.prng))
601*0a6a1f1dSLionel Sambuc mutex_unlock(&arc4random_global.lock);
602*0a6a1f1dSLionel Sambuc }
603*0a6a1f1dSLionel Sambuc
604*0a6a1f1dSLionel Sambuc /* Public API */
6052fe8fb19SBen Gras
606f14fb602SLionel Sambuc uint32_t
arc4random(void)607f14fb602SLionel Sambuc arc4random(void)
6082fe8fb19SBen Gras {
609*0a6a1f1dSLionel Sambuc struct arc4random_prng *prng;
610f14fb602SLionel Sambuc uint32_t v;
611f14fb602SLionel Sambuc
612*0a6a1f1dSLionel Sambuc prng = arc4random_prng_get();
613*0a6a1f1dSLionel Sambuc crypto_prng_buf(&prng->arc4_prng, &v, sizeof v);
614*0a6a1f1dSLionel Sambuc arc4random_prng_put(prng);
615*0a6a1f1dSLionel Sambuc
616f14fb602SLionel Sambuc return v;
6172fe8fb19SBen Gras }
6182fe8fb19SBen Gras
619f14fb602SLionel Sambuc void
arc4random_buf(void * buf,size_t len)620f14fb602SLionel Sambuc arc4random_buf(void *buf, size_t len)
6212fe8fb19SBen Gras {
622*0a6a1f1dSLionel Sambuc struct arc4random_prng *prng;
6232fe8fb19SBen Gras
624*0a6a1f1dSLionel Sambuc if (len <= crypto_prng_MAXOUTPUTBYTES) {
625*0a6a1f1dSLionel Sambuc prng = arc4random_prng_get();
626*0a6a1f1dSLionel Sambuc crypto_prng_buf(&prng->arc4_prng, buf, len);
627*0a6a1f1dSLionel Sambuc arc4random_prng_put(prng);
628*0a6a1f1dSLionel Sambuc } else {
629*0a6a1f1dSLionel Sambuc uint8_t seed[crypto_onetimestream_SEEDBYTES];
6302fe8fb19SBen Gras
631*0a6a1f1dSLionel Sambuc prng = arc4random_prng_get();
632*0a6a1f1dSLionel Sambuc crypto_prng_buf(&prng->arc4_prng, seed, sizeof seed);
633*0a6a1f1dSLionel Sambuc arc4random_prng_put(prng);
634f14fb602SLionel Sambuc
635*0a6a1f1dSLionel Sambuc crypto_onetimestream(seed, buf, len);
636*0a6a1f1dSLionel Sambuc (void)explicit_memset(seed, 0, sizeof seed);
637*0a6a1f1dSLionel Sambuc }
6382fe8fb19SBen Gras }
639f14fb602SLionel Sambuc
640f14fb602SLionel Sambuc uint32_t
arc4random_uniform(uint32_t bound)641*0a6a1f1dSLionel Sambuc arc4random_uniform(uint32_t bound)
642f14fb602SLionel Sambuc {
643*0a6a1f1dSLionel Sambuc struct arc4random_prng *prng;
644*0a6a1f1dSLionel Sambuc uint32_t minimum, r;
645f14fb602SLionel Sambuc
646f14fb602SLionel Sambuc /*
647*0a6a1f1dSLionel Sambuc * We want a uniform random choice in [0, n), and arc4random()
648*0a6a1f1dSLionel Sambuc * makes a uniform random choice in [0, 2^32). If we reduce
649*0a6a1f1dSLionel Sambuc * that modulo n, values in [0, 2^32 mod n) will be represented
650*0a6a1f1dSLionel Sambuc * slightly more than values in [2^32 mod n, n). Instead we
651*0a6a1f1dSLionel Sambuc * choose only from [2^32 mod n, 2^32) by rejecting samples in
652*0a6a1f1dSLionel Sambuc * [0, 2^32 mod n), to avoid counting the extra representative
653*0a6a1f1dSLionel Sambuc * of [0, 2^32 mod n). To compute 2^32 mod n, note that
654*0a6a1f1dSLionel Sambuc *
655*0a6a1f1dSLionel Sambuc * 2^32 mod n = 2^32 mod n - 0
656*0a6a1f1dSLionel Sambuc * = 2^32 mod n - n mod n
657*0a6a1f1dSLionel Sambuc * = (2^32 - n) mod n,
658*0a6a1f1dSLionel Sambuc *
659*0a6a1f1dSLionel Sambuc * the last of which is what we compute in 32-bit arithmetic.
660f14fb602SLionel Sambuc */
661*0a6a1f1dSLionel Sambuc minimum = (-bound % bound);
662f14fb602SLionel Sambuc
663*0a6a1f1dSLionel Sambuc prng = arc4random_prng_get();
664*0a6a1f1dSLionel Sambuc do crypto_prng_buf(&prng->arc4_prng, &r, sizeof r);
665*0a6a1f1dSLionel Sambuc while (__predict_false(r < minimum));
666*0a6a1f1dSLionel Sambuc arc4random_prng_put(prng);
667*0a6a1f1dSLionel Sambuc
668*0a6a1f1dSLionel Sambuc return (r % bound);
669f14fb602SLionel Sambuc }
670*0a6a1f1dSLionel Sambuc
671*0a6a1f1dSLionel Sambuc void
arc4random_stir(void)672*0a6a1f1dSLionel Sambuc arc4random_stir(void)
673*0a6a1f1dSLionel Sambuc {
674*0a6a1f1dSLionel Sambuc struct arc4random_prng *prng;
675*0a6a1f1dSLionel Sambuc
676*0a6a1f1dSLionel Sambuc prng = arc4random_prng_get();
677*0a6a1f1dSLionel Sambuc arc4random_prng_addrandom(prng, NULL, 0);
678*0a6a1f1dSLionel Sambuc arc4random_prng_put(prng);
679*0a6a1f1dSLionel Sambuc }
680*0a6a1f1dSLionel Sambuc
681*0a6a1f1dSLionel Sambuc /*
682*0a6a1f1dSLionel Sambuc * Silly signature here is for hysterical raisins. Should instead be
683*0a6a1f1dSLionel Sambuc * const void *data and size_t datalen.
684*0a6a1f1dSLionel Sambuc */
685*0a6a1f1dSLionel Sambuc void
arc4random_addrandom(u_char * data,int datalen)686*0a6a1f1dSLionel Sambuc arc4random_addrandom(u_char *data, int datalen)
687*0a6a1f1dSLionel Sambuc {
688*0a6a1f1dSLionel Sambuc struct arc4random_prng *prng;
689*0a6a1f1dSLionel Sambuc
690*0a6a1f1dSLionel Sambuc _DIAGASSERT(0 <= datalen);
691*0a6a1f1dSLionel Sambuc
692*0a6a1f1dSLionel Sambuc prng = arc4random_prng_get();
693*0a6a1f1dSLionel Sambuc arc4random_prng_addrandom(prng, data, datalen);
694*0a6a1f1dSLionel Sambuc arc4random_prng_put(prng);
695*0a6a1f1dSLionel Sambuc }
696*0a6a1f1dSLionel Sambuc
697*0a6a1f1dSLionel Sambuc #ifdef _ARC4RANDOM_TEST
698*0a6a1f1dSLionel Sambuc
699*0a6a1f1dSLionel Sambuc #include <sys/wait.h>
700*0a6a1f1dSLionel Sambuc
701*0a6a1f1dSLionel Sambuc #include <err.h>
702*0a6a1f1dSLionel Sambuc #include <stdio.h>
703*0a6a1f1dSLionel Sambuc
704*0a6a1f1dSLionel Sambuc int
main(int argc __unused,char ** argv __unused)705*0a6a1f1dSLionel Sambuc main(int argc __unused, char **argv __unused)
706*0a6a1f1dSLionel Sambuc {
707*0a6a1f1dSLionel Sambuc unsigned char gubbish[] = "random gubbish";
708*0a6a1f1dSLionel Sambuc const uint8_t zero64[64] = {0};
709*0a6a1f1dSLionel Sambuc uint8_t buf[2048];
710*0a6a1f1dSLionel Sambuc unsigned i, a, n;
711*0a6a1f1dSLionel Sambuc
712*0a6a1f1dSLionel Sambuc /* Test arc4random: should not be deterministic. */
713*0a6a1f1dSLionel Sambuc if (printf("arc4random: %08"PRIx32"\n", arc4random()) < 0)
714*0a6a1f1dSLionel Sambuc err(1, "printf");
715*0a6a1f1dSLionel Sambuc
716*0a6a1f1dSLionel Sambuc /* Test stirring: should definitely not be deterministic. */
717*0a6a1f1dSLionel Sambuc arc4random_stir();
718*0a6a1f1dSLionel Sambuc
719*0a6a1f1dSLionel Sambuc /* Test small buffer. */
720*0a6a1f1dSLionel Sambuc arc4random_buf(buf, 8);
721*0a6a1f1dSLionel Sambuc if (printf("arc4randombuf small:") < 0)
722*0a6a1f1dSLionel Sambuc err(1, "printf");
723*0a6a1f1dSLionel Sambuc for (i = 0; i < 8; i++)
724*0a6a1f1dSLionel Sambuc if (printf(" %02x", buf[i]) < 0)
725*0a6a1f1dSLionel Sambuc err(1, "printf");
726*0a6a1f1dSLionel Sambuc if (printf("\n") < 0)
727*0a6a1f1dSLionel Sambuc err(1, "printf");
728*0a6a1f1dSLionel Sambuc
729*0a6a1f1dSLionel Sambuc /* Test addrandom: should not make the rest deterministic. */
730*0a6a1f1dSLionel Sambuc arc4random_addrandom(gubbish, sizeof gubbish);
731*0a6a1f1dSLionel Sambuc
732*0a6a1f1dSLionel Sambuc /* Test large buffer. */
733*0a6a1f1dSLionel Sambuc arc4random_buf(buf, sizeof buf);
734*0a6a1f1dSLionel Sambuc if (printf("arc4randombuf_large:") < 0)
735*0a6a1f1dSLionel Sambuc err(1, "printf");
736*0a6a1f1dSLionel Sambuc for (i = 0; i < sizeof buf; i++)
737*0a6a1f1dSLionel Sambuc if (printf(" %02x", buf[i]) < 0)
738*0a6a1f1dSLionel Sambuc err(1, "printf");
739*0a6a1f1dSLionel Sambuc if (printf("\n") < 0)
740*0a6a1f1dSLionel Sambuc err(1, "printf");
741*0a6a1f1dSLionel Sambuc
742*0a6a1f1dSLionel Sambuc /* Test misaligned small and large. */
743*0a6a1f1dSLionel Sambuc for (a = 0; a < 64; a++) {
744*0a6a1f1dSLionel Sambuc for (n = a; n < sizeof buf; n++) {
745*0a6a1f1dSLionel Sambuc (void)memset(buf, 0, sizeof buf);
746*0a6a1f1dSLionel Sambuc arc4random_buf(buf, n - a);
747*0a6a1f1dSLionel Sambuc if (memcmp(buf + n - a, zero64, a) != 0)
748*0a6a1f1dSLionel Sambuc errx(1, "arc4random buffer overflow 0");
749*0a6a1f1dSLionel Sambuc
750*0a6a1f1dSLionel Sambuc (void)memset(buf, 0, sizeof buf);
751*0a6a1f1dSLionel Sambuc arc4random_buf(buf + a, n - a);
752*0a6a1f1dSLionel Sambuc if (memcmp(buf, zero64, a) != 0)
753*0a6a1f1dSLionel Sambuc errx(1, "arc4random buffer overflow 1");
754*0a6a1f1dSLionel Sambuc
755*0a6a1f1dSLionel Sambuc if ((2*a) <= n) {
756*0a6a1f1dSLionel Sambuc (void)memset(buf, 0, sizeof buf);
757*0a6a1f1dSLionel Sambuc arc4random_buf(buf + a, n - a - a);
758*0a6a1f1dSLionel Sambuc if (memcmp(buf + n - a, zero64, a) != 0)
759*0a6a1f1dSLionel Sambuc errx(1,
760*0a6a1f1dSLionel Sambuc "arc4random buffer overflow 2");
761*0a6a1f1dSLionel Sambuc }
762*0a6a1f1dSLionel Sambuc }
763*0a6a1f1dSLionel Sambuc }
764*0a6a1f1dSLionel Sambuc
765*0a6a1f1dSLionel Sambuc /* Test fork-safety. */
766*0a6a1f1dSLionel Sambuc {
767*0a6a1f1dSLionel Sambuc pid_t pid, rpid;
768*0a6a1f1dSLionel Sambuc int status;
769*0a6a1f1dSLionel Sambuc
770*0a6a1f1dSLionel Sambuc pid = fork();
771*0a6a1f1dSLionel Sambuc switch (pid) {
772*0a6a1f1dSLionel Sambuc case -1:
773*0a6a1f1dSLionel Sambuc err(1, "fork");
774*0a6a1f1dSLionel Sambuc case 0:
775*0a6a1f1dSLionel Sambuc _exit(arc4random_prng_get()->arc4_seeded);
776*0a6a1f1dSLionel Sambuc default:
777*0a6a1f1dSLionel Sambuc rpid = waitpid(pid, &status, 0);
778*0a6a1f1dSLionel Sambuc if (rpid == -1)
779*0a6a1f1dSLionel Sambuc err(1, "waitpid");
780*0a6a1f1dSLionel Sambuc if (rpid != pid)
781*0a6a1f1dSLionel Sambuc errx(1, "waitpid returned wrong pid"
782*0a6a1f1dSLionel Sambuc ": %"PRIdMAX" != %"PRIdMAX,
783*0a6a1f1dSLionel Sambuc (intmax_t)rpid,
784*0a6a1f1dSLionel Sambuc (intmax_t)pid);
785*0a6a1f1dSLionel Sambuc if (WIFEXITED(status)) {
786*0a6a1f1dSLionel Sambuc if (WEXITSTATUS(status) != 0)
787*0a6a1f1dSLionel Sambuc errx(1, "child exited with %d",
788*0a6a1f1dSLionel Sambuc WEXITSTATUS(status));
789*0a6a1f1dSLionel Sambuc } else if (WIFSIGNALED(status)) {
790*0a6a1f1dSLionel Sambuc errx(1, "child terminated on signal %d",
791*0a6a1f1dSLionel Sambuc WTERMSIG(status));
792*0a6a1f1dSLionel Sambuc } else {
793*0a6a1f1dSLionel Sambuc errx(1, "child died mysteriously: %d", status);
794*0a6a1f1dSLionel Sambuc }
795*0a6a1f1dSLionel Sambuc }
796*0a6a1f1dSLionel Sambuc }
797*0a6a1f1dSLionel Sambuc
798*0a6a1f1dSLionel Sambuc /* XXX Test multithreaded fork safety...? */
799*0a6a1f1dSLionel Sambuc
800*0a6a1f1dSLionel Sambuc return 0;
801*0a6a1f1dSLionel Sambuc }
802*0a6a1f1dSLionel Sambuc #endif
803