Coverage Report

Created: 2024-05-20 01:00

/src/openiked-portable/compat/arc4random.c
Line
Count
Source (jump to first uncovered line)
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
#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
0
#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
0
#define KEYSZ 32
48
0
#define IVSZ  8
49
#define BLOCKSZ 64
50
#define RSBUFSZ (16*BLOCKSZ)
51
52
0
#define REKEY_BASE  (1024*1024) /* NB. should be a power of 2 */
53
54
/* Marked MAP_INHERIT_ZERO, so zero'd out in fork children. */
55
static struct _rs {
56
  size_t    rs_have;  /* valid bytes at end of rs_buf */
57
  size_t    rs_count; /* bytes till reseed */
58
} *rs;
59
60
/* Maybe be preserved in fork children, if _rs_allocate() decides. */
61
static struct _rsx {
62
  chacha_ctx  rs_chacha;  /* chacha context for random keystream */
63
  u_char    rs_buf[RSBUFSZ];  /* keystream blocks */
64
} *rsx;
65
66
static inline int _rs_allocate(struct _rs **, struct _rsx **);
67
static inline void _rs_forkdetect(void);
68
#include "arc4random.h"
69
70
static inline void _rs_rekey(u_char *dat, size_t datlen);
71
72
static inline void
73
_rs_init(u_char *buf, size_t n)
74
0
{
75
0
  if (n < KEYSZ + IVSZ)
76
0
    return;
77
78
0
  if (rs == NULL) {
79
0
    if (_rs_allocate(&rs, &rsx) == -1)
80
0
      _exit(1);
81
0
  }
82
83
0
  chacha_keysetup(&rsx->rs_chacha, buf, KEYSZ * 8);
84
0
  chacha_ivsetup(&rsx->rs_chacha, buf + KEYSZ);
85
0
}
86
87
static void
88
_rs_stir(void)
89
0
{
90
0
  u_char rnd[KEYSZ + IVSZ];
91
0
  uint32_t rekey_fuzz = 0;
92
93
0
  if (getentropy(rnd, sizeof rnd) == -1)
94
0
    _getentropy_fail();
95
96
0
  if (!rs)
97
0
    _rs_init(rnd, sizeof(rnd));
98
0
  else
99
0
    _rs_rekey(rnd, sizeof(rnd));
100
0
  explicit_bzero(rnd, sizeof(rnd)); /* discard source seed */
101
102
  /* invalidate rs_buf */
103
0
  rs->rs_have = 0;
104
0
  memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
105
106
  /* rekey interval should not be predictable */
107
0
  chacha_encrypt_bytes(&rsx->rs_chacha, (uint8_t *)&rekey_fuzz,
108
0
      (uint8_t *)&rekey_fuzz, sizeof(rekey_fuzz));
109
0
  rs->rs_count = REKEY_BASE + (rekey_fuzz % REKEY_BASE);
110
0
}
111
112
static inline void
113
_rs_stir_if_needed(size_t len)
114
0
{
115
0
  _rs_forkdetect();
116
0
  if (!rs || rs->rs_count <= len)
117
0
    _rs_stir();
118
0
  if (rs->rs_count <= len)
119
0
    rs->rs_count = 0;
120
0
  else
121
0
    rs->rs_count -= len;
122
0
}
123
124
static inline void
125
_rs_rekey(u_char *dat, size_t datlen)
126
0
{
127
#ifndef KEYSTREAM_ONLY
128
  memset(rsx->rs_buf, 0, sizeof(rsx->rs_buf));
129
#endif
130
  /* fill rs_buf with the keystream */
131
0
  chacha_encrypt_bytes(&rsx->rs_chacha, rsx->rs_buf,
132
0
      rsx->rs_buf, sizeof(rsx->rs_buf));
133
  /* mix in optional user provided data */
134
0
  if (dat) {
135
0
    size_t i, m;
136
137
0
    m = minimum(datlen, KEYSZ + IVSZ);
138
0
    for (i = 0; i < m; i++)
139
0
      rsx->rs_buf[i] ^= dat[i];
140
0
  }
141
  /* immediately reinit for backtracking resistance */
142
0
  _rs_init(rsx->rs_buf, KEYSZ + IVSZ);
143
0
  memset(rsx->rs_buf, 0, KEYSZ + IVSZ);
144
0
  rs->rs_have = sizeof(rsx->rs_buf) - KEYSZ - IVSZ;
145
0
}
146
147
static inline void
148
_rs_random_buf(void *_buf, size_t n)
149
0
{
150
0
  u_char *buf = (u_char *)_buf;
151
0
  u_char *keystream;
152
0
  size_t m;
153
154
0
  _rs_stir_if_needed(n);
155
0
  while (n > 0) {
156
0
    if (rs->rs_have > 0) {
157
0
      m = minimum(n, rs->rs_have);
158
0
      keystream = rsx->rs_buf + sizeof(rsx->rs_buf)
159
0
          - rs->rs_have;
160
0
      memcpy(buf, keystream, m);
161
0
      memset(keystream, 0, m);
162
0
      buf += m;
163
0
      n -= m;
164
0
      rs->rs_have -= m;
165
0
    }
166
0
    if (rs->rs_have == 0)
167
0
      _rs_rekey(NULL, 0);
168
0
  }
169
0
}
170
171
static inline void
172
_rs_random_u32(uint32_t *val)
173
0
{
174
0
  u_char *keystream;
175
176
0
  _rs_stir_if_needed(sizeof(*val));
177
0
  if (rs->rs_have < sizeof(*val))
178
0
    _rs_rekey(NULL, 0);
179
0
  keystream = rsx->rs_buf + sizeof(rsx->rs_buf) - rs->rs_have;
180
0
  memcpy(val, keystream, sizeof(*val));
181
0
  memset(keystream, 0, sizeof(*val));
182
0
  rs->rs_have -= sizeof(*val);
183
0
}
184
185
uint32_t
186
arc4random(void)
187
0
{
188
0
  uint32_t val;
189
190
0
  _ARC4_LOCK();
191
0
  _rs_random_u32(&val);
192
0
  _ARC4_UNLOCK();
193
0
  return val;
194
0
}
195
196
void
197
arc4random_buf(void *buf, size_t n)
198
0
{
199
0
  _ARC4_LOCK();
200
0
  _rs_random_buf(buf, n);
201
0
  _ARC4_UNLOCK();
202
0
}