diff options
Diffstat (limited to 'src/lib/libc/crypt/arc4random.c')
-rw-r--r-- | src/lib/libc/crypt/arc4random.c | 173 |
1 files changed, 173 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..5e3b2925a6 --- /dev/null +++ b/src/lib/libc/crypt/arc4random.c | |||
@@ -0,0 +1,173 @@ | |||
1 | /* $OpenBSD: arc4random.c,v 1.9 2003/08/16 19:07:40 tedu 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(struct arc4_stream *as) | ||
53 | { | ||
54 | int n; | ||
55 | |||
56 | for (n = 0; n < 256; n++) | ||
57 | as->s[n] = n; | ||
58 | as->i = 0; | ||
59 | as->j = 0; | ||
60 | } | ||
61 | |||
62 | static inline void | ||
63 | arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen) | ||
64 | { | ||
65 | int n; | ||
66 | u_int8_t si; | ||
67 | |||
68 | as->i--; | ||
69 | for (n = 0; n < 256; n++) { | ||
70 | as->i = (as->i + 1); | ||
71 | si = as->s[as->i]; | ||
72 | as->j = (as->j + si + dat[n % datlen]); | ||
73 | as->s[as->i] = as->s[as->j]; | ||
74 | as->s[as->j] = si; | ||
75 | } | ||
76 | as->j = as->i; | ||
77 | } | ||
78 | |||
79 | static void | ||
80 | arc4_stir(struct arc4_stream *as) | ||
81 | { | ||
82 | int i, mib[2]; | ||
83 | size_t len; | ||
84 | struct { | ||
85 | struct timeval tv; | ||
86 | u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; | ||
87 | } rdat; | ||
88 | |||
89 | gettimeofday(&rdat.tv, NULL); | ||
90 | mib[0] = CTL_KERN; | ||
91 | mib[1] = KERN_ARND; | ||
92 | |||
93 | for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) { | ||
94 | len = sizeof(u_int); | ||
95 | if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1) | ||
96 | break; | ||
97 | } | ||
98 | |||
99 | arc4_stir_pid = getpid(); | ||
100 | arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); | ||
101 | } | ||
102 | |||
103 | static inline u_int8_t | ||
104 | arc4_getbyte(struct arc4_stream *as) | ||
105 | { | ||
106 | u_int8_t si, sj; | ||
107 | |||
108 | as->i = (as->i + 1); | ||
109 | si = as->s[as->i]; | ||
110 | as->j = (as->j + si); | ||
111 | sj = as->s[as->j]; | ||
112 | as->s[as->i] = sj; | ||
113 | as->s[as->j] = si; | ||
114 | return (as->s[(si + sj) & 0xff]); | ||
115 | } | ||
116 | |||
117 | static inline u_int32_t | ||
118 | arc4_getword(struct arc4_stream *as) | ||
119 | { | ||
120 | u_int32_t val; | ||
121 | val = arc4_getbyte(as) << 24; | ||
122 | val |= arc4_getbyte(as) << 16; | ||
123 | val |= arc4_getbyte(as) << 8; | ||
124 | val |= arc4_getbyte(as); | ||
125 | return val; | ||
126 | } | ||
127 | |||
128 | void | ||
129 | arc4random_stir(void) | ||
130 | { | ||
131 | if (!rs_initialized) { | ||
132 | arc4_init(&rs); | ||
133 | rs_initialized = 1; | ||
134 | } | ||
135 | arc4_stir(&rs); | ||
136 | } | ||
137 | |||
138 | void | ||
139 | arc4random_addrandom(u_char *dat, int datlen) | ||
140 | { | ||
141 | if (!rs_initialized) | ||
142 | arc4random_stir(); | ||
143 | arc4_addrandom(&rs, dat, datlen); | ||
144 | } | ||
145 | |||
146 | u_int32_t | ||
147 | arc4random(void) | ||
148 | { | ||
149 | if (!rs_initialized || arc4_stir_pid != getpid()) | ||
150 | arc4random_stir(); | ||
151 | return arc4_getword(&rs); | ||
152 | } | ||
153 | |||
154 | #if 0 | ||
155 | /*-------- Test code for i386 --------*/ | ||
156 | #include <stdio.h> | ||
157 | #include <machine/pctr.h> | ||
158 | int | ||
159 | main(int argc, char **argv) | ||
160 | { | ||
161 | const int iter = 1000000; | ||
162 | int i; | ||
163 | pctrval v; | ||
164 | |||
165 | v = rdtsc(); | ||
166 | for (i = 0; i < iter; i++) | ||
167 | arc4random(); | ||
168 | v = rdtsc() - v; | ||
169 | v /= iter; | ||
170 | |||
171 | printf("%qd cycles\n", v); | ||
172 | } | ||
173 | #endif | ||