]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/arc4random.c
MFV r355071: libbsdxml (expat) 2.2.9.
[FreeBSD/FreeBSD.git] / lib / libc / gen / arc4random.c
1 /*      $OpenBSD: arc4random.c,v 1.54 2015/09/13 08:31:47 guenther Exp $        */
2
3 /*
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>
8  *
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.
12  *
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.
20  */
21
22 /*
23  * ChaCha based random number generator for OpenBSD.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include "namespace.h"
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <pthread.h>
33 #include <signal.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/time.h>
40  
41 #include "libc_private.h"
42 #include "un-namespace.h"
43
44 #define CHACHA_EMBED
45 #define KEYSTREAM_ONLY
46 #include "chacha.c"
47
48 #define minimum(a, b) ((a) < (b) ? (a) : (b))
49
50 #if defined(__GNUC__) || defined(_MSC_VER)
51 #define inline __inline
52 #else                           /* __GNUC__ || _MSC_VER */
53 #define inline
54 #endif                          /* !__GNUC__ && !_MSC_VER */
55
56 #define KEYSZ   32
57 #define IVSZ    8
58 #define BLOCKSZ 64
59 #define RSBUFSZ (16*BLOCKSZ)
60
61 /* Marked INHERIT_ZERO, so zero'd out in fork children. */
62 static struct _rs {
63         size_t          rs_have;        /* valid bytes at end of rs_buf */
64         size_t          rs_count;       /* bytes till reseed */
65 } *rs;
66
67 /* Maybe be preserved in fork children, if _rs_allocate() decides. */
68 static struct _rsx {
69         chacha_ctx      rs_chacha;      /* chacha context for random keystream */
70         u_char          rs_buf[RSBUFSZ];        /* keystream blocks */
71 } *rsx;
72
73 static inline int _rs_allocate(struct _rs **, struct _rsx **);
74 static inline void _rs_forkdetect(void);
75 #include "arc4random.h"
76
77 static inline void _rs_rekey(u_char *dat, size_t datlen);
78
79 static inline void
80 _rs_init(u_char *buf, size_t n)
81 {
82         if (n < KEYSZ + IVSZ)
83                 return;
84
85         if (rs == NULL) {
86                 if (_rs_allocate(&rs, &rsx) == -1)
87                         abort();
88         }
89
90         chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
91         chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ, NULL);
92 }
93
94 static void
95 _rs_stir(void)
96 {
97         u_char rnd[KEYSZ + IVSZ];
98
99         if (getentropy(rnd, sizeof rnd) == -1)
100                 _getentropy_fail();
101
102         if (!rs)
103                 _rs_init(rnd, sizeof(rnd));
104         else
105                 _rs_rekey(rnd, sizeof(rnd));
106         explicit_bzero(rnd, sizeof(rnd));       /* discard source seed */
107
108         /* invalidate rs_buf */
109         rs->rs_have = 0;
110         memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
111
112         rs->rs_count = 1600000;
113 }
114
115 static inline void
116 _rs_stir_if_needed(size_t len)
117 {
118         _rs_forkdetect();
119         if (!rs || rs->rs_count <= len)
120                 _rs_stir();
121         if (rs->rs_count <= len)
122                 rs->rs_count = 0;
123         else
124                 rs->rs_count -= len;
125 }
126
127 static inline void
128 _rs_rekey(u_char *dat, size_t datlen)
129 {
130 #ifndef KEYSTREAM_ONLY
131         memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
132 #endif
133         /* fill rs_buf with the keystream */
134         chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
135             rsx->rs_buf, sizeof(rsx->rs_buf));
136         /* mix in optional user provided data */
137         if (dat) {
138                 size_t i, m;
139
140                 m = minimum(datlen, KEYSZ + IVSZ);
141                 for (i = 0; i < m; i++)
142                         rsx->rs_buf[i] ^= dat[i];
143         }
144         /* immediately reinit for backtracking resistance */
145         _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
146         memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
147         rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
148 }
149
150 static inline void
151 _rs_random_buf(void *_buf, size_t n)
152 {
153         u_char *buf = (u_char *)_buf;
154         u_char *keystream;
155         size_t m;
156
157         _rs_stir_if_needed(n);
158         while (n > 0) {
159                 if (rs->rs_have > 0) {
160                         m = minimum(n, rs->rs_have);
161                         keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
162                             - rs->rs_have;
163                         memcpy(buf, keystream, m);
164                         memset(keystream, 0, m);
165                         buf += m;
166                         n -= m;
167                         rs->rs_have -= m;
168                 }
169                 if (rs->rs_have == 0)
170                         _rs_rekey(NULL, 0);
171         }
172 }
173
174 static inline void
175 _rs_random_u32(uint32_t *val)
176 {
177         u_char *keystream;
178
179         _rs_stir_if_needed(sizeof(*val));
180         if (rs->rs_have < sizeof(*val))
181                 _rs_rekey(NULL, 0);
182         keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
183         memcpy(val, keystream, sizeof(*val));
184         memset(keystream, 0, sizeof(*val));
185         rs->rs_have -= sizeof(*val);
186 }
187
188 uint32_t
189 arc4random(void)
190 {
191         uint32_t val;
192
193         _ARC4_LOCK();
194         _rs_random_u32(&val);
195         _ARC4_UNLOCK();
196         return val;
197 }
198
199 void
200 arc4random_buf(void *buf, size_t n)
201 {
202         _ARC4_LOCK();
203         _rs_random_buf(buf, n);
204         _ARC4_UNLOCK();
205 }