diff options
Diffstat (limited to 'src/lib/libc/crypt/arc4random.c')
| -rw-r--r-- | src/lib/libc/crypt/arc4random.c | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c new file mode 100644 index 0000000000..a41001bcc3 --- /dev/null +++ b/src/lib/libc/crypt/arc4random.c | |||
| @@ -0,0 +1,195 @@ | |||
| 1 | /* $OpenBSD: arc4random.c,v 1.7 2003/02/14 17:12:54 deraadt Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Arc4 random number generator for OpenBSD. | ||
| 5 | * Copyright 1996 David Mazieres <dm@lcs.mit.edu>. | ||
| 6 | * | ||
| 7 | * Modification and redistribution in source and binary forms is | ||
| 8 | * permitted provided that due credit is given to the author and the | ||
| 9 | * OpenBSD project by leaving this copyright notice intact. | ||
| 10 | */ | ||
| 11 | |||
| 12 | /* | ||
| 13 | * This code is derived from section 17.1 of Applied Cryptography, | ||
| 14 | * second edition, which describes a stream cipher allegedly | ||
| 15 | * compatible with RSA Labs "RC4" cipher (the actual description of | ||
| 16 | * which is a trade secret). The same algorithm is used as a stream | ||
| 17 | * cipher called "arcfour" in Tatu Ylonen's ssh package. | ||
| 18 | * | ||
| 19 | * Here the stream cipher has been modified always to include the time | ||
| 20 | * when initializing the state. That makes it impossible to | ||
| 21 | * regenerate the same random sequence twice, so this can't be used | ||
| 22 | * for encryption, but will generate good random numbers. | ||
| 23 | * | ||
| 24 | * RC4 is a registered trademark of RSA Laboratories. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include <fcntl.h> | ||
| 28 | #include <stdlib.h> | ||
| 29 | #include <unistd.h> | ||
| 30 | #include <sys/types.h> | ||
| 31 | #include <sys/param.h> | ||
| 32 | #include <sys/time.h> | ||
| 33 | #include <sys/sysctl.h> | ||
| 34 | |||
| 35 | #ifdef __GNUC__ | ||
| 36 | #define inline __inline | ||
| 37 | #else /* !__GNUC__ */ | ||
| 38 | #define inline | ||
| 39 | #endif /* !__GNUC__ */ | ||
| 40 | |||
| 41 | struct arc4_stream { | ||
| 42 | u_int8_t i; | ||
| 43 | u_int8_t j; | ||
| 44 | u_int8_t s[256]; | ||
| 45 | }; | ||
| 46 | |||
| 47 | static int rs_initialized; | ||
| 48 | static struct arc4_stream rs; | ||
| 49 | static pid_t arc4_stir_pid; | ||
| 50 | |||
| 51 | static inline void | ||
| 52 | arc4_init(as) | ||
| 53 | struct arc4_stream *as; | ||
| 54 | { | ||
| 55 | int n; | ||
| 56 | |||
| 57 | for (n = 0; n < 256; n++) | ||
| 58 | as->s[n] = n; | ||
| 59 | as->i = 0; | ||
| 60 | as->j = 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | static inline void | ||
| 64 | arc4_addrandom(as, dat, datlen) | ||
| 65 | struct arc4_stream *as; | ||
| 66 | u_char *dat; | ||
| 67 | int datlen; | ||
| 68 | { | ||
| 69 | int n; | ||
| 70 | u_int8_t si; | ||
| 71 | |||
| 72 | as->i--; | ||
| 73 | for (n = 0; n < 256; n++) { | ||
| 74 | as->i = (as->i + 1); | ||
| 75 | si = as->s[as->i]; | ||
| 76 | as->j = (as->j + si + dat[n % datlen]); | ||
| 77 | as->s[as->i] = as->s[as->j]; | ||
| 78 | as->s[as->j] = si; | ||
| 79 | } | ||
| 80 | as->j = as->i; | ||
| 81 | } | ||
| 82 | |||
| 83 | static void | ||
| 84 | arc4_stir(as) | ||
| 85 | struct arc4_stream *as; | ||
| 86 | { | ||
| 87 | int fd; | ||
| 88 | struct { | ||
| 89 | struct timeval tv; | ||
| 90 | u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; | ||
| 91 | } rdat; | ||
| 92 | |||
| 93 | gettimeofday(&rdat.tv, NULL); | ||
| 94 | fd = open("/dev/arandom", O_RDONLY); | ||
| 95 | if (fd != -1) { | ||
| 96 | read(fd, rdat.rnd, sizeof(rdat.rnd)); | ||
| 97 | close(fd); | ||
| 98 | } else { | ||
| 99 | int i, mib[2]; | ||
| 100 | size_t len; | ||
| 101 | |||
| 102 | /* Device could not be opened, we might be chrooted, take | ||
| 103 | * randomness from sysctl. */ | ||
| 104 | |||
| 105 | mib[0] = CTL_KERN; | ||
| 106 | mib[1] = KERN_ARND; | ||
| 107 | |||
| 108 | for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) { | ||
| 109 | len = sizeof(u_int); | ||
| 110 | if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1) | ||
| 111 | break; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | /* fd < 0 or failed sysctl ? Ah, what the heck. We'll just take | ||
| 115 | * whatever was on the stack... */ | ||
| 116 | |||
| 117 | arc4_stir_pid = getpid(); | ||
| 118 | arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); | ||
| 119 | } | ||
| 120 | |||
| 121 | static inline u_int8_t | ||
| 122 | arc4_getbyte(as) | ||
| 123 | struct arc4_stream *as; | ||
| 124 | { | ||
| 125 | u_int8_t si, sj; | ||
| 126 | |||
| 127 | as->i = (as->i + 1); | ||
| 128 | si = as->s[as->i]; | ||
| 129 | as->j = (as->j + si); | ||
| 130 | sj = as->s[as->j]; | ||
| 131 | as->s[as->i] = sj; | ||
| 132 | as->s[as->j] = si; | ||
| 133 | return (as->s[(si + sj) & 0xff]); | ||
| 134 | } | ||
| 135 | |||
| 136 | static inline u_int32_t | ||
| 137 | arc4_getword(as) | ||
| 138 | struct arc4_stream *as; | ||
| 139 | { | ||
| 140 | u_int32_t val; | ||
| 141 | val = arc4_getbyte(as) << 24; | ||
| 142 | val |= arc4_getbyte(as) << 16; | ||
| 143 | val |= arc4_getbyte(as) << 8; | ||
| 144 | val |= arc4_getbyte(as); | ||
| 145 | return val; | ||
| 146 | } | ||
| 147 | |||
| 148 | void | ||
| 149 | arc4random_stir() | ||
| 150 | { | ||
| 151 | if (!rs_initialized) { | ||
| 152 | arc4_init(&rs); | ||
| 153 | rs_initialized = 1; | ||
| 154 | } | ||
| 155 | arc4_stir(&rs); | ||
| 156 | } | ||
| 157 | |||
| 158 | void | ||
| 159 | arc4random_addrandom(dat, datlen) | ||
| 160 | u_char *dat; | ||
| 161 | int datlen; | ||
| 162 | { | ||
| 163 | if (!rs_initialized) | ||
| 164 | arc4random_stir(); | ||
| 165 | arc4_addrandom(&rs, dat, datlen); | ||
| 166 | } | ||
| 167 | |||
| 168 | u_int32_t | ||
| 169 | arc4random() | ||
| 170 | { | ||
| 171 | if (!rs_initialized || arc4_stir_pid != getpid()) | ||
| 172 | arc4random_stir(); | ||
| 173 | return arc4_getword(&rs); | ||
| 174 | } | ||
| 175 | |||
| 176 | #if 0 | ||
| 177 | /*-------- Test code for i386 --------*/ | ||
| 178 | #include <stdio.h> | ||
| 179 | #include <machine/pctr.h> | ||
| 180 | int | ||
| 181 | main(int argc, char **argv) | ||
| 182 | { | ||
| 183 | const int iter = 1000000; | ||
| 184 | int i; | ||
| 185 | pctrval v; | ||
| 186 | |||
| 187 | v = rdtsc(); | ||
| 188 | for (i = 0; i < iter; i++) | ||
| 189 | arc4random(); | ||
| 190 | v = rdtsc() - v; | ||
| 191 | v /= iter; | ||
| 192 | |||
| 193 | printf("%qd cycles\n", v); | ||
| 194 | } | ||
| 195 | #endif | ||
