| | varnish-cache/lib/libvarnish/vrnd.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2011 Varnish Software AS |
| 3 |
|
* Copyright (c) 1983, 1993 The Regents of the University of California. |
| 4 |
|
* All rights reserved. |
| 5 |
|
* |
| 6 |
|
* Author: Dag-Erling Smørgrav <des@des.no> |
| 7 |
|
* |
| 8 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 9 |
|
* |
| 10 |
|
* Redistribution and use in source and binary forms, with or without |
| 11 |
|
* modification, are permitted provided that the following conditions |
| 12 |
|
* are met: |
| 13 |
|
* 1. Redistributions of source code must retain the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer. |
| 15 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
|
* notice, this list of conditions and the following disclaimer in the |
| 17 |
|
* documentation and/or other materials provided with the distribution. |
| 18 |
|
* |
| 19 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 20 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 23 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 |
|
* SUCH DAMAGE. |
| 30 |
|
* |
| 31 |
|
* Partially from: $FreeBSD: head/lib/libc/stdlib/random.c 303342 |
| 32 |
|
* |
| 33 |
|
*/ |
| 34 |
|
|
| 35 |
|
#include "config.h" |
| 36 |
|
|
| 37 |
|
#include <fcntl.h> |
| 38 |
|
#include <math.h> |
| 39 |
|
#include <stdint.h> |
| 40 |
|
#include <stdlib.h> |
| 41 |
|
#include <string.h> |
| 42 |
|
#include <unistd.h> |
| 43 |
|
|
| 44 |
|
#include "vdef.h" |
| 45 |
|
|
| 46 |
|
#include "vas.h" |
| 47 |
|
#include "vrnd.h" |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
vrnd_lock_f *VRND_Lock; |
| 51 |
|
vrnd_lock_f *VRND_Unlock; |
| 52 |
|
|
| 53 |
|
/********************************************************************** |
| 54 |
|
* Stripped down random(3) implementation from FreeBSD, to provide |
| 55 |
|
* predictable "random" numbers of testing purposes. |
| 56 |
|
*/ |
| 57 |
|
|
| 58 |
|
#define TYPE_3 3 /* x**31 + x**3 + 1 */ |
| 59 |
|
#define DEG_3 31 |
| 60 |
|
#define SEP_3 3 |
| 61 |
|
|
| 62 |
|
static uint32_t randtbl[DEG_3 + 1] = { |
| 63 |
|
TYPE_3, |
| 64 |
|
0x2cf41758, 0x27bb3711, 0x4916d4d1, 0x7b02f59f, 0x9b8e28eb, 0xc0e80269, |
| 65 |
|
0x696f5c16, 0x878f1ff5, 0x52d9c07f, 0x916a06cd, 0xb50b3a20, 0x2776970a, |
| 66 |
|
0xee4eb2a6, 0xe94640ec, 0xb1d65612, 0x9d1ed968, 0x1043f6b7, 0xa3432a76, |
| 67 |
|
0x17eacbb9, 0x3c09e2eb, 0x4f8c2b3, 0x708a1f57, 0xee341814, 0x95d0e4d2, |
| 68 |
|
0xb06f216c, 0x8bd2e72e, 0x8f7c38d7, 0xcfc6a8fc, 0x2a59495, 0xa20d2a69, |
| 69 |
|
0xe29d12d1 |
| 70 |
|
}; |
| 71 |
|
|
| 72 |
|
static uint32_t *fptr = &randtbl[SEP_3 + 1]; |
| 73 |
|
static uint32_t *rptr = &randtbl[1]; |
| 74 |
|
|
| 75 |
|
static uint32_t * const state = &randtbl[1]; |
| 76 |
|
static const int rand_deg = DEG_3; |
| 77 |
|
static const int rand_sep = SEP_3; |
| 78 |
|
static const uint32_t * const end_ptr = &randtbl[DEG_3 + 1]; |
| 79 |
|
|
| 80 |
|
static inline uint32_t |
| 81 |
150120 |
good_rand(uint32_t ctx) |
| 82 |
|
{ |
| 83 |
|
/* |
| 84 |
|
* Compute x = (7^5 * x) mod (2^31 - 1) |
| 85 |
|
* without overflowing 31 bits: |
| 86 |
|
* (2^31 - 1) = 127773 * (7^5) + 2836 |
| 87 |
|
* From "Random number generators: good ones are hard to find", |
| 88 |
|
* Park and Miller, Communications of the ACM, vol. 31, no. 10, |
| 89 |
|
* October 1988, p. 1195. |
| 90 |
|
*/ |
| 91 |
|
int32_t hi, lo, x; |
| 92 |
|
|
| 93 |
|
/* Transform to [1, 0x7ffffffe] range. */ |
| 94 |
150120 |
x = (ctx % 0x7ffffffe) + 1; |
| 95 |
150120 |
hi = x / 127773; |
| 96 |
150120 |
lo = x % 127773; |
| 97 |
150120 |
x = 16807 * lo - 2836 * hi; |
| 98 |
150120 |
if (x < 0) |
| 99 |
1796 |
x += 0x7fffffff; |
| 100 |
|
/* Transform to [0, 0x7ffffffd] range. */ |
| 101 |
150120 |
return (x - 1); |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
static long |
| 105 |
4871655 |
vrnd_RandomTestable(void) |
| 106 |
|
{ |
| 107 |
|
uint32_t i; |
| 108 |
|
uint32_t *f, *r; |
| 109 |
|
|
| 110 |
|
/* |
| 111 |
|
* Use local variables rather than static variables for speed. |
| 112 |
|
*/ |
| 113 |
4871655 |
f = fptr; r = rptr; |
| 114 |
4871655 |
*f += *r; |
| 115 |
4871655 |
i = *f >> 1; /* chucking least random bit */ |
| 116 |
4871655 |
if (++f >= end_ptr) { |
| 117 |
157145 |
f = state; |
| 118 |
157145 |
++r; |
| 119 |
157145 |
} |
| 120 |
4714510 |
else if (++r >= end_ptr) { |
| 121 |
157145 |
r = state; |
| 122 |
157145 |
} |
| 123 |
|
|
| 124 |
4871655 |
fptr = f; rptr = r; |
| 125 |
4871655 |
return ((long)i); |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
|
| 129 |
|
void |
| 130 |
5004 |
VRND_SeedTestable(unsigned int x) |
| 131 |
|
{ |
| 132 |
|
int i, lim; |
| 133 |
|
|
| 134 |
5004 |
state[0] = (uint32_t)x; |
| 135 |
155124 |
for (i = 1; i < rand_deg; i++) |
| 136 |
150120 |
state[i] = good_rand(state[i - 1]); |
| 137 |
5004 |
fptr = &state[rand_sep]; |
| 138 |
5004 |
rptr = &state[0]; |
| 139 |
5004 |
lim = 10 * rand_deg; |
| 140 |
1556244 |
for (i = 0; i < lim; i++) |
| 141 |
1551240 |
(void)vrnd_RandomTestable(); |
| 142 |
5004 |
} |
| 143 |
|
|
| 144 |
|
long |
| 145 |
3320415 |
VRND_RandomTestable(void) |
| 146 |
|
{ |
| 147 |
|
long l; |
| 148 |
|
|
| 149 |
3320415 |
AN(VRND_Lock); |
| 150 |
3320415 |
VRND_Lock(); |
| 151 |
3320415 |
l = vrnd_RandomTestable(); |
| 152 |
3320415 |
AN(VRND_Unlock); |
| 153 |
3320415 |
VRND_Unlock(); |
| 154 |
3320415 |
return (l); |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
double |
| 158 |
6 |
VRND_RandomTestableDouble(void) |
| 159 |
|
{ |
| 160 |
6 |
return ( |
| 161 |
12 |
ldexp((double)VRND_RandomTestable(), -31) + |
| 162 |
6 |
ldexp((double)VRND_RandomTestable(), -62) |
| 163 |
|
); |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
int |
| 167 |
341031 |
VRND_RandomCrypto(void *ptr, size_t len) |
| 168 |
|
{ |
| 169 |
|
int fd; |
| 170 |
341031 |
char *p = ptr; |
| 171 |
|
ssize_t l; |
| 172 |
|
|
| 173 |
341031 |
AN(ptr); |
| 174 |
341031 |
fd = open("/dev/urandom", O_RDONLY); |
| 175 |
341031 |
if (fd < 0) |
| 176 |
0 |
return (-1); |
| 177 |
682062 |
while (len > 0) { |
| 178 |
341031 |
l = read(fd, p, len); |
| 179 |
341031 |
if (l < 0) |
| 180 |
0 |
break; |
| 181 |
341031 |
p += l; |
| 182 |
341031 |
len -= l; |
| 183 |
|
} |
| 184 |
341031 |
closefd(&fd); |
| 185 |
341031 |
return (len == 0 ? 0 : -1); |
| 186 |
341031 |
} |
| 187 |
|
|
| 188 |
|
void |
| 189 |
5000 |
VRND_SeedAll(void) |
| 190 |
|
{ |
| 191 |
|
unsigned long seed; |
| 192 |
|
|
| 193 |
5000 |
AZ(VRND_RandomCrypto(&seed, sizeof seed)); |
| 194 |
5000 |
srandom(seed); |
| 195 |
5000 |
AZ(VRND_RandomCrypto(&seed, sizeof seed)); |
| 196 |
5000 |
VRND_SeedTestable(seed); |
| 197 |
5000 |
AZ(VRND_RandomCrypto(&seed, sizeof seed)); |
| 198 |
5000 |
srand48((long)seed); |
| 199 |
5000 |
} |