summaryrefslogtreecommitdiff
path: root/util/nvmutil/lib/num.c
blob: bbb5a83e9646b7617eee95d6a1c44372cdf9b76b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* SPDX-License-Identifier: MIT
 * Copyright (c) 2026 Leah Rowe <leah@libreboot.org>
 *
 * Numerical functions.
 */

#ifdef __OpenBSD__
#include <sys/param.h>
#endif
#include <sys/types.h>
#if defined(FALLBACK_RAND_1989) && \
    (FALLBACK_RAND_1989) > 0
#include <sys/time.h>
#endif

#include <errno.h>
#if !((defined(__OpenBSD__) && (OpenBSD) >= 201) || \
    defined(__FreeBSD__) || \
    defined(__NetBSD__) || defined(__APPLE__))
#include <fcntl.h> /* if not arc4random: /dev/urandom */
#endif
#include <limits.h>
#include <stddef.h>
#include <string.h>
#if defined(FALLBACK_RAND_1989) && \
    (FALLBACK_RAND_1989) > 0
#include <time.h>
#endif
#include <unistd.h>

#include "../include/common.h"

unsigned short
hextonum(char ch_s)
{
	unsigned char ch;

	ch = (unsigned char)ch_s;

	if ((unsigned int)(ch - '0') <= 9)
		return ch - '0';

	ch |= 0x20;

	if ((unsigned int)(ch - 'a') <= 5)
		return ch - 'a' + 10;

	if (ch == '?' || ch == 'x')
		return (unsigned short)rlong() & 0xf;

	return 16; /* invalid character */
}

/* Random numbers
 */

unsigned long
rlong(void)
{
#if !(defined(FALLBACK_RAND_1989) && \
    ((FALLBACK_RAND_1989) > 0))
#if (defined(__OpenBSD__) && (OpenBSD) >= 201) || \
    defined(__FreeBSD__) || \
    defined(__NetBSD__) || defined(__APPLE__)

	unsigned long rval;
	arc4random_buf(&rval, sizeof(unsigned long));

	return rval;
#else
	static int fd = -1;
	static long nr = -1;
	static unsigned long off = 0;
#if defined (BUFSIZ)
	static char rbuf[BUFSIZ];
#else
#ifndef PORTABLE
	static char rbuf[4096];
#elif ((PORTABLE) > 0)
	static char rbuf[256]; /* scarce memory on old systems */
#else
	static char rbuf[4096]; /* typical 32-bit BUFSIZ */
#endif
#endif
	unsigned long rval;
	long new_nr;

	int retries = 0;
	int max_retries = 100;

#if defined(__linux__)
#if defined(HAVE_GETRANDOM) || \
    defined(HAVE_GETRANDOM_SYSCALL)

	/* linux getrandom()
	 *
	 * we *can* use arc4random on
	 * modern linux, but not on
	 * every libc. better use the
	 * official linux function
	 *
	 * similar benefits to arc4random
	 * e.g. works in chroot, blocks
	 * until it has enough entropy,
	 * and works even when /dev/urandom
	 * is available (doesn't use it);
	 * it's generally more reliable
	 */

	if (fallback_rand_getrandom(&rval, sizeof(rval)) == 0)
		return rval;

	/*
	 * now fall back to urandom if getrandom failed:
	 */
#endif
#endif

	/* reading from urandom is inherently
	 * unreliable on old systems, even if
	 * newer systems make it more reliable
	 *
	 * modern linux/bsd make it safe, but
	 * we have to assume that someone is
	 * compiling this on linux from 1999
	 *
	 * this logic therefore applies various
	 * tricks to mitigate possible os bugs
	 */

retry_urandom_read:

	if (++retries > max_retries)
		goto rlong_next;

	if (nr < 0 || nr < (long)sizeof(unsigned long)) {

		if (fd < 0) {

			fd = open("/dev/urandom",
			    O_RDONLY | O_BINARY | O_NOFOLLOW |
			    O_CLOEXEC);

#ifdef USE_OLD_DEV_RANDOM
#if (USE_OLD_DEV_RANDOM) > 0
			/* WARNING:
			 *   /dev/random may block
			 *   forever and does **NOT**
			 *   guarantee better entropy
			 *   on old systems
			 *
			 *   only use it if needed
			 */

			if (fd < 0)
				fd = open("/dev/random",
				    O_RDONLY | O_BINARY | O_NOFOLLOW |
				    O_CLOEXEC);
#endif
#endif

			if (fd < 0)
				goto retry_urandom_read;

			retries = 0;
		}

		new_nr = rw_file_exact(fd, (unsigned char *)rbuf,
		    sizeof(rbuf), 0, IO_READ, LOOP_EAGAIN,
		    LOOP_EINTR, MAX_ZERO_RW_RETRY, OFF_ERR);

		if (new_nr < 0 || new_nr < (long)sizeof(rbuf))
			goto retry_urandom_read;

		/* only reset buffer after successful refill */
		nr = new_nr;
		off = 0;

		/* to mitigate file descriptor
		 * injection, we do not re-use
		 * the same descriptor each time
		 */
		(void) close_on_eintr(fd);
		fd = -1;
	}

	fd = -1;
	retries = 0;

	memcpy(&rval, rbuf + off, sizeof(unsigned long));

	nr -= (long)sizeof(unsigned long);
	off += sizeof(unsigned long);

	return rval;

rlong_next:

	fd = -1;
	off = 0;
	nr = -1;

	err(EIO, "Can't read from /dev/[ua]random");
	return 0;

#endif
#else /* FALLBACK_RAND_1989 */
	/* your computer is from a museum
	 */
	unsigned long mix = 0;
	int nr;

	/* 100 times, for entropy
	 */
	for (nr = 0; nr < 100; nr++)
		mix ^= fallback_rand_1989();

	/* 101 times ;)
	 */
	return fallback_rand_1989();
#endif
}

#if !(defined(FALLBACK_RAND_1989) && \
    ((FALLBACK_RAND_1989) > 0))
#if defined(__linux__)
#if defined(HAVE_GETRANDOM) || \
    defined(HAVE_GETRANDOM_SYSCALL)
int
fallback_rand_getrandom(void *buf, unsigned long len)
{
	unsigned long off = 0;
	long rval = -1;

	if (!len)
		return -1;

	if (buf == NULL)
		return -1;

#if defined(HAVE_GETRANDOM) || \
    defined(HAVE_GETRANDOM_SYSCALL)

	while (off < len) {

#if defined(HAVE_GETRANDOM)
		rval = (long)getrandom((char *)buf + off, len - off, 0);
#elif defined(HAVE_GETRANDOM_SYSCALL)
		rval = (long)syscall(SYS_getrandom,
		    (char *)buf + off, len - off, 0);
#endif

		if (rval < 0) {
			if (errno == EINTR)
				continue;

			return -1; /* unsupported by kernel */
		}

		off += (unsigned long)rval;
	}

	return 0;

#else
	(void)buf;
	(void)len;

	return -1;
#endif
}
#endif
#endif
#else
/* nobody should use this
 * (not crypto-safe)
 */
unsigned long
fallback_rand_1989(void)
{
	static unsigned long mix = 0;
	static unsigned long counter = 0;

	struct timeval tv;

	gettimeofday(&tv, NULL);

	mix ^= (unsigned long)tv.tv_sec
	    ^ (unsigned long)tv.tv_usec
	    ^ (unsigned long)getpid()
	    ^ (unsigned long)&mix
	    ^ counter++
	    ^ entropy_jitter();

	/*
	 * Stack addresses can vary between
	 * calls, thus increasing entropy.
	 */
	mix ^= (unsigned long)&mix;
	mix ^= (unsigned long)&tv;
	mix ^= (unsigned long)&counter;

	return mix;
}

unsigned long
entropy_jitter(void)
{
	unsigned long mix;

	struct timeval a, b;
	long mix_diff;

	int c;

	mix = 0;

	gettimeofday(&a, NULL);

	for (c = 0; c < 32; c++) {

		getpid();
		gettimeofday(&b, NULL);

		/*
		 * prevent negative numbers to prevent overflow,
		 * which would bias rand to large numbers
		 */
		mix_diff = (long)(b.tv_usec - a.tv_usec);
		if (mix_diff < 0)
			mix_diff = -mix_diff;

		mix ^= (unsigned long)(mix_diff);

		mix ^= (unsigned long)&mix;

	}

	return mix;
}
#endif

void
check_bin(unsigned long a, const char *a_name)
{
	if (a > 1)
		err(EINVAL, "%s must be 0 or 1, but is %lu",
		    a_name, (unsigned long)a);
}