1 /* $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb Exp $ */
4 * Copyright (c) 1996, David Mazieres <dm@uun.org>
5 * Copyright (c) 2008, Damien Miller <djm@openbsd.org>
6 * Copyright (c) 2013, Markus Friedl <markus@openbsd.org>
7 * Copyright (c) 2014, Theo de Raadt <deraadt@openbsd.org>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * ChaCha based random number generator for OpenBSD.
26 #include <sys/cdefs.h>
27 #include "namespace.h"
28 #if defined(__FreeBSD__)
39 #include <sys/types.h>
42 #include "libc_private.h"
43 #include "un-namespace.h"
46 #define KEYSTREAM_ONLY
47 #if defined(__FreeBSD__)
48 #define ARC4RANDOM_FXRNG 1
50 #define ARC4RANDOM_FXRNG 0
54 #define minimum(a, b) ((a) < (b) ? (a) : (b))
56 #if defined(__GNUC__) || defined(_MSC_VER)
57 #define inline __inline
58 #else /* __GNUC__ || _MSC_VER */
60 #endif /* !__GNUC__ && !_MSC_VER */
65 #define RSBUFSZ (16*BLOCKSZ)
67 #define REKEY_BASE (1024*1024) /* NB. should be a power of 2 */
69 /* Marked INHERIT_ZERO, so zero'd out in fork children. */
71 size_t rs_have
; /* valid bytes at end of rs_buf */
72 size_t rs_count
; /* bytes till reseed */
75 /* Maybe be preserved in fork children, if _rs_allocate() decides. */
77 chacha_ctx rs_chacha
; /* chacha context for random keystream */
78 u_char rs_buf
[RSBUFSZ
]; /* keystream blocks */
80 uint32_t rs_seed_generation
; /* 32-bit userspace RNG version */
84 static inline int _rs_allocate(struct _rs
**, struct _rsx
**);
85 static inline void _rs_forkdetect(void);
86 #include "arc4random.h"
88 static inline void _rs_rekey(u_char
*dat
, size_t datlen
);
91 _rs_init(u_char
*buf
, size_t n
)
97 if (_rs_allocate(&rs
, &rsx
) == -1)
101 chacha_keysetup(&rsx
->rs_chacha
, buf
, KEYSZ
* 8);
102 chacha_ivsetup(&rsx
->rs_chacha
, buf
+ KEYSZ
, NULL
);
108 u_char rnd
[KEYSZ
+ IVSZ
];
109 uint32_t rekey_fuzz
= 0;
111 #if defined(__FreeBSD__)
115 * De-couple allocation (which locates the vdso_fxrngp pointer in
116 * auxinfo) from initialization. This allows us to read the root seed
117 * version before we fetch system entropy, maintaining the invariant
118 * that the PRF was seeded with entropy from rs_seed_generation or a
119 * later generation. But never seeded from an earlier generation.
120 * This invariant prevents us from missing a root reseed event.
124 if (_rs_allocate(&rs
, &rsx
) == -1)
129 * Transition period: new userspace on old kernel. This should become
130 * a hard error at some point, if the scheme is adopted.
132 if (vdso_fxrngp
!= NULL
)
133 rsx
->rs_seed_generation
=
134 fxrng_load_acq_generation(&vdso_fxrngp
->fx_generation32
);
137 if (getentropy(rnd
, sizeof rnd
) == -1)
140 #if !defined(__FreeBSD__)
142 _rs_init(rnd
, sizeof(rnd
));
143 #else /* __FreeBSD__ */
146 _rs_init(rnd
, sizeof(rnd
));
149 _rs_rekey(rnd
, sizeof(rnd
));
150 explicit_bzero(rnd
, sizeof(rnd
)); /* discard source seed */
152 /* invalidate rs_buf */
154 memset(rsx
->rs_buf
, 0, sizeof(rsx
->rs_buf
));
156 /* rekey interval should not be predictable */
157 chacha_encrypt_bytes(&rsx
->rs_chacha
, (uint8_t *)&rekey_fuzz
,
158 (uint8_t *)&rekey_fuzz
, sizeof(rekey_fuzz
));
159 rs
->rs_count
= REKEY_BASE
+ (rekey_fuzz
% REKEY_BASE
);
163 _rs_stir_if_needed(size_t len
)
166 if (!rs
|| rs
->rs_count
<= len
)
168 if (rs
->rs_count
<= len
)
175 _rs_rekey(u_char
*dat
, size_t datlen
)
177 #ifndef KEYSTREAM_ONLY
178 memset(rsx
->rs_buf
, 0, sizeof(rsx
->rs_buf
));
180 /* fill rs_buf with the keystream */
181 chacha_encrypt_bytes(&rsx
->rs_chacha
, rsx
->rs_buf
,
182 rsx
->rs_buf
, sizeof(rsx
->rs_buf
));
183 /* mix in optional user provided data */
187 m
= minimum(datlen
, KEYSZ
+ IVSZ
);
188 for (i
= 0; i
< m
; i
++)
189 rsx
->rs_buf
[i
] ^= dat
[i
];
191 /* immediately reinit for backtracking resistance */
192 _rs_init(rsx
->rs_buf
, KEYSZ
+ IVSZ
);
193 memset(rsx
->rs_buf
, 0, KEYSZ
+ IVSZ
);
194 rs
->rs_have
= sizeof(rsx
->rs_buf
) - KEYSZ
- IVSZ
;
198 _rs_random_buf(void *_buf
, size_t n
)
200 u_char
*buf
= (u_char
*)_buf
;
204 _rs_stir_if_needed(n
);
206 if (rs
->rs_have
> 0) {
207 m
= minimum(n
, rs
->rs_have
);
208 keystream
= rsx
->rs_buf
+ sizeof(rsx
->rs_buf
)
210 memcpy(buf
, keystream
, m
);
211 memset(keystream
, 0, m
);
216 if (rs
->rs_have
== 0)
222 _rs_random_u32(uint32_t *val
)
226 _rs_stir_if_needed(sizeof(*val
));
227 if (rs
->rs_have
< sizeof(*val
))
229 keystream
= rsx
->rs_buf
+ sizeof(rsx
->rs_buf
) - rs
->rs_have
;
230 memcpy(val
, keystream
, sizeof(*val
));
231 memset(keystream
, 0, sizeof(*val
));
232 rs
->rs_have
-= sizeof(*val
);
241 _rs_random_u32(&val
);
247 arc4random_buf(void *buf
, size_t n
)
250 _rs_random_buf(buf
, n
);