Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/compat/arc4random.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 <fcntl.h> | - | ||||||||||||
27 | #include <limits.h> | - | ||||||||||||
28 | #include <signal.h> | - | ||||||||||||
29 | #include <stdint.h> | - | ||||||||||||
30 | #include <stdlib.h> | - | ||||||||||||
31 | #include <string.h> | - | ||||||||||||
32 | #include <unistd.h> | - | ||||||||||||
33 | #include <sys/types.h> | - | ||||||||||||
34 | #include <sys/time.h> | - | ||||||||||||
35 | - | |||||||||||||
36 | #define KEYSTREAM_ONLY | - | ||||||||||||
37 | #include "chacha_private.h" | - | ||||||||||||
38 | - | |||||||||||||
39 | #define minimum(a, b) ((a) < (b) ? (a) : (b)) | - | ||||||||||||
40 | - | |||||||||||||
41 | #if defined(__GNUC__) || defined(_MSC_VER) | - | ||||||||||||
42 | #define inline __inline | - | ||||||||||||
43 | #else /* __GNUC__ || _MSC_VER */ | - | ||||||||||||
44 | #define inline | - | ||||||||||||
45 | #endif /* !__GNUC__ && !_MSC_VER */ | - | ||||||||||||
46 | - | |||||||||||||
47 | #define KEYSZ 32 | - | ||||||||||||
48 | #define IVSZ 8 | - | ||||||||||||
49 | #define BLOCKSZ 64 | - | ||||||||||||
50 | #define RSBUFSZ (16*BLOCKSZ) | - | ||||||||||||
51 | - | |||||||||||||
52 | /* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */ | - | ||||||||||||
53 | static struct _rs { | - | ||||||||||||
54 | size_t rs_have; /* valid bytes at end of rs_buf */ | - | ||||||||||||
55 | size_t rs_count; /* bytes till reseed */ | - | ||||||||||||
56 | } *rs; | - | ||||||||||||
57 | - | |||||||||||||
58 | /* Maybe be preserved in fork children, if _rs_allocate() decides. */ | - | ||||||||||||
59 | static struct _rsx { | - | ||||||||||||
60 | chacha_ctx rs_chacha; /* chacha context for random keystream */ | - | ||||||||||||
61 | u_char rs_buf[RSBUFSZ]; /* keystream blocks */ | - | ||||||||||||
62 | } *rsx; | - | ||||||||||||
63 | - | |||||||||||||
64 | static inline int _rs_allocate(struct _rs **, struct _rsx **); | - | ||||||||||||
65 | static inline void _rs_forkdetect(void); | - | ||||||||||||
66 | #include "arc4random.h" | - | ||||||||||||
67 | - | |||||||||||||
68 | static inline void _rs_rekey(u_char *dat, size_t datlen); | - | ||||||||||||
69 | - | |||||||||||||
70 | static inline void | - | ||||||||||||
71 | _rs_init(u_char *buf, size_t n) | - | ||||||||||||
72 | { | - | ||||||||||||
73 | if (n < KEYSZ + IVSZ)
| 0-1302 | ||||||||||||
74 | return; never executed: return; | 0 | ||||||||||||
75 | - | |||||||||||||
76 | if (rs == NULL) {
| 182-1120 | ||||||||||||
77 | if (_rs_allocate(&rs, &rsx) == -1)
| 0-182 | ||||||||||||
78 | abort(); never executed: abort(); | 0 | ||||||||||||
79 | } executed 182 times by 27 tests: end of block Executed by:
| 182 | ||||||||||||
80 | - | |||||||||||||
81 | chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8, 0); | - | ||||||||||||
82 | chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ); | - | ||||||||||||
83 | } executed 1302 times by 27 tests: end of block Executed by:
| 1302 | ||||||||||||
84 | - | |||||||||||||
85 | static void | - | ||||||||||||
86 | _rs_stir(void) | - | ||||||||||||
87 | { | - | ||||||||||||
88 | u_char rnd[KEYSZ + IVSZ]; | - | ||||||||||||
89 | - | |||||||||||||
90 | if (getentropy(rnd, sizeof rnd) == -1)
| 0-182 | ||||||||||||
91 | _getentropy_fail(); never executed: _getentropy_fail(); | 0 | ||||||||||||
92 | - | |||||||||||||
93 | if (!rs)
| 0-182 | ||||||||||||
94 | _rs_init(rnd, sizeof(rnd)); executed 182 times by 27 tests: _rs_init(rnd, sizeof(rnd)); Executed by:
| 182 | ||||||||||||
95 | else | - | ||||||||||||
96 | _rs_rekey(rnd, sizeof(rnd)); never executed: _rs_rekey(rnd, sizeof(rnd)); | 0 | ||||||||||||
97 | explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */ | - | ||||||||||||
98 | - | |||||||||||||
99 | /* invalidate rs_buf */ | - | ||||||||||||
100 | rs->rs_have = 0; | - | ||||||||||||
101 | memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf)); | - | ||||||||||||
102 | - | |||||||||||||
103 | rs->rs_count = 1600000; | - | ||||||||||||
104 | } executed 182 times by 27 tests: end of block Executed by:
| 182 | ||||||||||||
105 | - | |||||||||||||
106 | static inline void | - | ||||||||||||
107 | _rs_stir_if_needed(size_t len) | - | ||||||||||||
108 | { | - | ||||||||||||
109 | _rs_forkdetect(); | - | ||||||||||||
110 | if (!rs || rs->rs_count <= len)
| 0-291159 | ||||||||||||
111 | _rs_stir(); executed 182 times by 27 tests: _rs_stir(); Executed by:
| 182 | ||||||||||||
112 | if (rs->rs_count <= len)
| 0-291341 | ||||||||||||
113 | rs->rs_count = 0; never executed: rs->rs_count = 0; | 0 | ||||||||||||
114 | else | - | ||||||||||||
115 | rs->rs_count -= len; executed 291341 times by 27 tests: rs->rs_count -= len; Executed by:
| 291341 | ||||||||||||
116 | } | - | ||||||||||||
117 | - | |||||||||||||
118 | static inline void | - | ||||||||||||
119 | _rs_rekey(u_char *dat, size_t datlen) | - | ||||||||||||
120 | { | - | ||||||||||||
121 | #ifndef KEYSTREAM_ONLY | - | ||||||||||||
122 | memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf)); | - | ||||||||||||
123 | #endif | - | ||||||||||||
124 | /* fill rs_buf with the keystream */ | - | ||||||||||||
125 | chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf, | - | ||||||||||||
126 | rsx->rs_buf, sizeof(rsx->rs_buf)); | - | ||||||||||||
127 | /* mix in optional user provided data */ | - | ||||||||||||
128 | if (dat) {
| 0-1120 | ||||||||||||
129 | size_t i, m; | - | ||||||||||||
130 | - | |||||||||||||
131 | m = minimum(datlen, KEYSZ + IVSZ);
| 0 | ||||||||||||
132 | for (i = 0; i < m; i++)
| 0 | ||||||||||||
133 | rsx->rs_buf[i] ^= dat[i]; never executed: rsx->rs_buf[i] ^= dat[i]; | 0 | ||||||||||||
134 | } never executed: end of block | 0 | ||||||||||||
135 | /* immediately reinit for backtracking resistance */ | - | ||||||||||||
136 | _rs_init(rsx->rs_buf, KEYSZ + IVSZ); | - | ||||||||||||
137 | memset(rsx->rs_buf, 0, KEYSZ + IVSZ); | - | ||||||||||||
138 | rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ; | - | ||||||||||||
139 | } executed 1120 times by 27 tests: end of block Executed by:
| 1120 | ||||||||||||
140 | - | |||||||||||||
141 | static inline void | - | ||||||||||||
142 | _rs_random_buf(void *_buf, size_t n) | - | ||||||||||||
143 | { | - | ||||||||||||
144 | u_char *buf = (u_char *)_buf; | - | ||||||||||||
145 | u_char *keystream; | - | ||||||||||||
146 | size_t m; | - | ||||||||||||
147 | - | |||||||||||||
148 | _rs_stir_if_needed(n); | - | ||||||||||||
149 | while (n > 0) {
| 283140-283802 | ||||||||||||
150 | if (rs->rs_have > 0) {
| 179-283623 | ||||||||||||
151 | m = minimum(n, rs->rs_have);
| 906-282717 | ||||||||||||
152 | keystream = rsx->rs_buf + sizeof(rsx->rs_buf) | - | ||||||||||||
153 | - rs->rs_have; | - | ||||||||||||
154 | memcpy(buf, keystream, m); | - | ||||||||||||
155 | memset(keystream, 0, m); | - | ||||||||||||
156 | buf += m; | - | ||||||||||||
157 | n -= m; | - | ||||||||||||
158 | rs->rs_have -= m; | - | ||||||||||||
159 | } executed 283623 times by 27 tests: end of block Executed by:
| 283623 | ||||||||||||
160 | if (rs->rs_have == 0)
| 1085-282717 | ||||||||||||
161 | _rs_rekey(NULL, 0); executed 1085 times by 27 tests: _rs_rekey( ((void *)0) , 0); Executed by:
| 1085 | ||||||||||||
162 | } executed 283802 times by 27 tests: end of block Executed by:
| 283802 | ||||||||||||
163 | } executed 283140 times by 27 tests: end of block Executed by:
| 283140 | ||||||||||||
164 | - | |||||||||||||
165 | static inline void | - | ||||||||||||
166 | _rs_random_u32(uint32_t *val) | - | ||||||||||||
167 | { | - | ||||||||||||
168 | u_char *keystream; | - | ||||||||||||
169 | - | |||||||||||||
170 | _rs_stir_if_needed(sizeof(*val)); | - | ||||||||||||
171 | if (rs->rs_have < sizeof(*val))
| 35-8166 | ||||||||||||
172 | _rs_rekey(NULL, 0); executed 35 times by 1 test: _rs_rekey( ((void *)0) , 0); Executed by:
| 35 | ||||||||||||
173 | keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have; | - | ||||||||||||
174 | memcpy(val, keystream, sizeof(*val)); | - | ||||||||||||
175 | memset(keystream, 0, sizeof(*val)); | - | ||||||||||||
176 | rs->rs_have -= sizeof(*val); | - | ||||||||||||
177 | } executed 8201 times by 4 tests: end of block Executed by:
| 8201 | ||||||||||||
178 | - | |||||||||||||
179 | uint32_t | - | ||||||||||||
180 | arc4random(void) | - | ||||||||||||
181 | { | - | ||||||||||||
182 | uint32_t val; | - | ||||||||||||
183 | - | |||||||||||||
184 | _ARC4_LOCK(); | - | ||||||||||||
185 | _rs_random_u32(&val); | - | ||||||||||||
186 | _ARC4_UNLOCK(); | - | ||||||||||||
187 | return val; executed 8201 times by 4 tests: return val; Executed by:
| 8201 | ||||||||||||
188 | } | - | ||||||||||||
189 | - | |||||||||||||
190 | void | - | ||||||||||||
191 | arc4random_buf(void *buf, size_t n) | - | ||||||||||||
192 | { | - | ||||||||||||
193 | _ARC4_LOCK(); | - | ||||||||||||
194 | _rs_random_buf(buf, n); | - | ||||||||||||
195 | _ARC4_UNLOCK(); | - | ||||||||||||
196 | } executed 283140 times by 27 tests: end of block Executed by:
| 283140 | ||||||||||||
Source code | Switch to Preprocessed file |