]>
git.saurik.com Git - apple/libc.git/blob - gen/arc4random.c
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* $FreeBSD: src/lib/libc/gen/arc4random.c,v 1.4 2000/01/27 23:06:13 jasone Exp $ */
26 * Arc4 random number generator for OpenBSD.
27 * Copyright 1996 David Mazieres <dm@lcs.mit.edu>.
29 * Modification and redistribution in source and binary forms is
30 * permitted provided that due credit is given to the author and the
31 * OpenBSD project (for instance by leaving this copyright notice
36 * This code is derived from section 17.1 of Applied Cryptography,
37 * second edition, which describes a stream cipher allegedly
38 * compatible with RSA Labs "RC4" cipher (the actual description of
39 * which is a trade secret). The same algorithm is used as a stream
40 * cipher called "arcfour" in Tatu Ylonen's ssh package.
42 * Here the stream cipher has been modified always to include the time
43 * when initializing the state. That makes it impossible to
44 * regenerate the same random sequence twice, so this can't be used
45 * for encryption, but will generate good random numbers.
47 * RC4 is a registered trademark of RSA Laboratories.
53 #include <sys/types.h>
62 static int rs_initialized
;
63 static struct arc4_stream
*rs
= NULL
;
65 static inline void arc4_init(struct arc4_stream
*) __attribute__((always_inline
));
68 struct arc4_stream
*as
;
72 for (n
= 0; n
< 256; n
++)
78 static inline void arc4_addrandom(struct arc4_stream
*, u_char
*, int) __attribute__((always_inline
));
80 arc4_addrandom(as
, dat
, datlen
)
81 struct arc4_stream
*as
;
89 for (n
= 0; n
< 256; n
++) {
92 as
->j
= (as
->j
+ si
+ dat
[n
% datlen
]);
93 as
->s
[as
->i
] = as
->s
[as
->j
];
100 struct arc4_stream
*as
;
106 u_int8_t rnd
[128 - sizeof(struct timeval
) - sizeof(pid_t
)];
109 gettimeofday(&rdat
.tv
, NULL
);
111 fd
= open("/dev/urandom", O_RDONLY
, 0);
113 (void) read(fd
, rdat
.rnd
, sizeof(rdat
.rnd
));
116 /* fd < 0? Ah, what the heck. We'll just take whatever was on the
119 arc4_addrandom(as
, (void *) &rdat
, sizeof(rdat
));
122 static inline u_int8_t
arc4_getbyte(struct arc4_stream
*) __attribute__((always_inline
));
123 static inline u_int8_t
125 struct arc4_stream
*as
;
131 as
->j
= (as
->j
+ si
);
135 return (as
->s
[(si
+ sj
) & 0xff]);
138 static inline u_int32_t
arc4_getword(struct arc4_stream
*) __attribute__((always_inline
));
139 static inline u_int32_t
141 struct arc4_stream
*as
;
144 val
= arc4_getbyte(as
) << 24;
145 val
|= arc4_getbyte(as
) << 16;
146 val
|= arc4_getbyte(as
) << 8;
147 val
|= arc4_getbyte(as
);
155 rs
= malloc( sizeof(struct arc4_stream
) );
159 if (!rs_initialized
) {
167 arc4random_addrandom(dat
, datlen
)
173 arc4_addrandom(rs
, dat
, datlen
);
181 return arc4_getword(rs
);
185 /*-------- Test code for i386 --------*/
187 #include <machine/pctr.h>
189 main(int argc
, char **argv
)
191 const int iter
= 1000000;
196 for (i
= 0; i
< iter
; i
++)
201 printf("%qd cycles\n", v
);