Coverage Report

Created: 2025-10-10 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssh/openbsd-compat/arc4random.c
Line
Count
Source
1
/*  $OpenBSD: arc4random.c,v 1.58 2022/07/31 13:41:45 tb 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
/* OPENBSD ORIGINAL: lib/libc/crypt/arc4random.c */
27
28
#include "includes.h"
29
30
#include <sys/types.h>
31
32
#include <fcntl.h>
33
#include <limits.h>
34
#include <signal.h>
35
#include <stdint.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <unistd.h>
39
#include <sys/types.h>
40
#include <sys/time.h>
41
42
#ifndef HAVE_ARC4RANDOM
43
44
/*
45
 * Always use the getentropy implementation from bsd-getentropy.c, which
46
 * will call a native getentropy if available then fall back as required.
47
 * We use a different name so that OpenSSL cannot call the wrong getentropy.
48
 */
49
int _ssh_compat_getentropy(void *, size_t);
50
#ifdef getentropy
51
# undef getentropy
52
#endif
53
0
#define getentropy(x, y) (_ssh_compat_getentropy((x), (y)))
54
55
#include "log.h"
56
57
#define KEYSTREAM_ONLY
58
#include "chacha_private.h"
59
60
0
#define minimum(a, b) ((a) < (b) ? (a) : (b))
61
62
#if defined(__GNUC__) || defined(_MSC_VER)
63
#define inline __inline
64
#else       /* __GNUC__ || _MSC_VER */
65
#define inline
66
#endif        /* !__GNUC__ && !_MSC_VER */
67
68
0
#define KEYSZ 32
69
0
#define IVSZ  8
70
#define BLOCKSZ 64
71
#define RSBUFSZ (16*BLOCKSZ)
72
73
0
#define REKEY_BASE  (1024*1024) /* NB. should be a power of 2 */
74
75
/* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
76
static struct _rs {
77
  size_t    rs_have;  /* valid bytes at end of rs_buf */
78
  size_t    rs_count; /* bytes till reseed */
79
} *rs;
80
81
/* Maybe be preserved in fork children, if _rs_allocate() decides. */
82
static struct _rsx {
83
  chacha_ctx  rs_chacha;  /* chacha context for random keystream */
84
  u_char    rs_buf[RSBUFSZ];  /* keystream blocks */
85
} *rsx;
86
87
static inline int _rs_allocate(struct _rs **, struct _rsx **);
88
static inline void _rs_forkdetect(void);
89
#include "arc4random.h"
90
91
static inline void _rs_rekey(u_char *dat, size_t datlen);
92
93
static inline void
94
_rs_init(u_char *buf, size_t n)
95
0
{
96
0
  if (n < KEYSZ + IVSZ)
97
0
    return;
98
99
0
  if (rs == NULL) {
100
0
    if (_rs_allocate(&rs, &rsx) == -1)
101
0
      _exit(1);
102
0
  }
103
104
0
  chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
105
0
  chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
106
0
}
107
108
static void
109
_rs_stir(void)
110
0
{
111
0
  u_char rnd[KEYSZ + IVSZ];
112
0
  uint32_t rekey_fuzz = 0;
113
114
0
  if (getentropy(rnd, sizeof rnd) == -1)
115
0
    _getentropy_fail();
116
117
0
  if (!rs)
118
0
    _rs_init(rnd, sizeof(rnd));
119
0
  else
120
0
    _rs_rekey(rnd, sizeof(rnd));
121
0
  explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
122
123
  /* invalidate rs_buf */
124
0
  rs->rs_have = 0;
125
0
  memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
126
127
  /* rekey interval should not be predictable */
128
0
  chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
129
0
      (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
130
0
  rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
131
0
}
132
133
static inline void
134
_rs_stir_if_needed(size_t len)
135
0
{
136
0
  _rs_forkdetect();
137
0
  if (!rs || rs->rs_count <= len)
138
0
    _rs_stir();
139
0
  if (rs->rs_count <= len)
140
0
    rs->rs_count = 0;
141
0
  else
142
0
    rs->rs_count -= len;
143
0
}
144
145
static inline void
146
_rs_rekey(u_char *dat, size_t datlen)
147
0
{
148
#ifndef KEYSTREAM_ONLY
149
  memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
150
#endif
151
  /* fill rs_buf with the keystream */
152
0
  chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
153
0
      rsx->rs_buf, sizeof(rsx->rs_buf));
154
  /* mix in optional user provided data */
155
0
  if (dat) {
156
0
    size_t i, m;
157
158
0
    m = minimum(datlen, KEYSZ + IVSZ);
159
0
    for (i = 0; i < m; i++)
160
0
      rsx->rs_buf[i] ^= dat[i];
161
0
  }
162
  /* immediately reinit for backtracking resistance */
163
0
  _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
164
0
  memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
165
0
  rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
166
0
}
167
168
static inline void
169
_rs_random_buf(void *_buf, size_t n)
170
0
{
171
0
  u_char *buf = (u_char *)_buf;
172
0
  u_char *keystream;
173
0
  size_t m;
174
175
0
  _rs_stir_if_needed(n);
176
0
  while (n > 0) {
177
0
    if (rs->rs_have > 0) {
178
0
      m = minimum(n, rs->rs_have);
179
0
      keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
180
0
          - rs->rs_have;
181
0
      memcpy(buf, keystream, m);
182
0
      memset(keystream, 0, m);
183
0
      buf += m;
184
0
      n -= m;
185
0
      rs->rs_have -= m;
186
0
    }
187
0
    if (rs->rs_have == 0)
188
0
      _rs_rekey(NULL, 0);
189
0
  }
190
0
}
191
192
static inline void
193
_rs_random_u32(uint32_t *val)
194
0
{
195
0
  u_char *keystream;
196
197
0
  _rs_stir_if_needed(sizeof(*val));
198
0
  if (rs->rs_have < sizeof(*val))
199
0
    _rs_rekey(NULL, 0);
200
0
  keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
201
0
  memcpy(val, keystream, sizeof(*val));
202
0
  memset(keystream, 0, sizeof(*val));
203
0
  rs->rs_have -= sizeof(*val);
204
0
}
205
206
uint32_t
207
arc4random(void)
208
0
{
209
0
  uint32_t val;
210
211
0
  _ARC4_LOCK();
212
0
  _rs_random_u32(&val);
213
0
  _ARC4_UNLOCK();
214
0
  return val;
215
0
}
216
DEF_WEAK(arc4random);
217
218
/*
219
 * If we are providing arc4random, then we can provide a more efficient
220
 * arc4random_buf().
221
 */
222
# ifndef HAVE_ARC4RANDOM_BUF
223
void
224
arc4random_buf(void *buf, size_t n)
225
0
{
226
0
  _ARC4_LOCK();
227
0
  _rs_random_buf(buf, n);
228
0
  _ARC4_UNLOCK();
229
0
}
230
DEF_WEAK(arc4random_buf);
231
# endif /* !HAVE_ARC4RANDOM_BUF */
232
#endif /* !HAVE_ARC4RANDOM */
233
234
/* arc4random_buf() that uses platform arc4random() */
235
#if !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM)
236
void
237
arc4random_buf(void *_buf, size_t n)
238
{
239
  size_t i;
240
  u_int32_t r = 0;
241
  char *buf = (char *)_buf;
242
243
  for (i = 0; i < n; i++) {
244
    if (i % 4 == 0)
245
      r = arc4random();
246
    buf[i] = r & 0xff;
247
    r >>= 8;
248
  }
249
  explicit_bzero(&r, sizeof(r));
250
}
251
#endif /* !defined(HAVE_ARC4RANDOM_BUF) && defined(HAVE_ARC4RANDOM) */
252