Home | History | Annotate | Line # | Download | only in gen
arc4random.c revision 1.10.4.2
      1  1.10.4.2      yamt /*	$NetBSD: arc4random.c,v 1.10.4.2 2012/10/30 18:58:44 yamt Exp $	*/
      2       1.1    itojun /*	$OpenBSD: arc4random.c,v 1.6 2001/06/05 05:05:38 pvalchev Exp $	*/
      3       1.1    itojun 
      4       1.1    itojun /*
      5       1.1    itojun  * Arc4 random number generator for OpenBSD.
      6       1.1    itojun  * Copyright 1996 David Mazieres <dm (at) lcs.mit.edu>.
      7       1.1    itojun  *
      8       1.1    itojun  * Modification and redistribution in source and binary forms is
      9       1.1    itojun  * permitted provided that due credit is given to the author and the
     10       1.1    itojun  * OpenBSD project by leaving this copyright notice intact.
     11       1.1    itojun  */
     12       1.1    itojun 
     13       1.1    itojun /*
     14       1.1    itojun  * This code is derived from section 17.1 of Applied Cryptography,
     15       1.1    itojun  * second edition, which describes a stream cipher allegedly
     16       1.1    itojun  * compatible with RSA Labs "RC4" cipher (the actual description of
     17       1.1    itojun  * which is a trade secret).  The same algorithm is used as a stream
     18       1.1    itojun  * cipher called "arcfour" in Tatu Ylonen's ssh package.
     19       1.1    itojun  *
     20       1.1    itojun  * Here the stream cipher has been modified always to include the time
     21       1.1    itojun  * when initializing the state.  That makes it impossible to
     22       1.1    itojun  * regenerate the same random sequence twice, so this can't be used
     23       1.1    itojun  * for encryption, but will generate good random numbers.
     24       1.1    itojun  *
     25       1.1    itojun  * RC4 is a registered trademark of RSA Laboratories.
     26       1.1    itojun  */
     27       1.1    itojun 
     28       1.8     lukem #include <sys/cdefs.h>
     29       1.8     lukem #if defined(LIBC_SCCS) && !defined(lint)
     30  1.10.4.2      yamt __RCSID("$NetBSD: arc4random.c,v 1.10.4.2 2012/10/30 18:58:44 yamt Exp $");
     31       1.8     lukem #endif /* LIBC_SCCS and not lint */
     32       1.8     lukem 
     33       1.7    kleink #include "namespace.h"
     34  1.10.4.1      yamt #include "reentrant.h"
     35       1.1    itojun #include <fcntl.h>
     36       1.1    itojun #include <stdlib.h>
     37       1.1    itojun #include <unistd.h>
     38       1.1    itojun #include <sys/types.h>
     39       1.1    itojun #include <sys/param.h>
     40       1.1    itojun #include <sys/time.h>
     41       1.1    itojun #include <sys/sysctl.h>
     42       1.1    itojun 
     43       1.7    kleink #ifdef __weak_alias
     44       1.7    kleink __weak_alias(arc4random,_arc4random)
     45  1.10.4.2      yamt __weak_alias(arc4random_addrandom,_arc4random_addrandom)
     46  1.10.4.2      yamt __weak_alias(arc4random_buf,_arc4random_buf)
     47  1.10.4.2      yamt __weak_alias(arc4random_stir,_arc4random_stir)
     48  1.10.4.2      yamt __weak_alias(arc4random_uniform,_arc4random_uniform)
     49       1.7    kleink #endif
     50       1.7    kleink 
     51       1.1    itojun struct arc4_stream {
     52  1.10.4.2      yamt 	uint8_t stirred;
     53  1.10.4.2      yamt 	uint8_t pad;
     54      1.10  christos 	uint8_t i;
     55      1.10  christos 	uint8_t j;
     56  1.10.4.2      yamt 	uint8_t s[(uint8_t)~0u + 1u];	/* 256 to you and me */
     57  1.10.4.2      yamt 	mutex_t mtx;
     58       1.1    itojun };
     59       1.1    itojun 
     60  1.10.4.2      yamt #ifdef _REENTRANT
     61  1.10.4.2      yamt #define LOCK(rs) { \
     62  1.10.4.2      yamt 		int isthreaded = __isthreaded; \
     63  1.10.4.2      yamt 		if (isthreaded)        \
     64  1.10.4.2      yamt 			mutex_lock(&(rs)->mtx);
     65  1.10.4.2      yamt #define UNLOCK(rs) \
     66  1.10.4.2      yamt 		if (isthreaded)        \
     67  1.10.4.2      yamt 			mutex_unlock(&(rs)->mtx);      \
     68  1.10.4.2      yamt 	}
     69  1.10.4.2      yamt #else
     70  1.10.4.2      yamt #define LOCK(rs)
     71  1.10.4.2      yamt #define UNLOCK(rs)
     72  1.10.4.2      yamt #endif
     73  1.10.4.2      yamt 
     74  1.10.4.2      yamt #define S(n) (n)
     75  1.10.4.2      yamt #define S4(n) S(n), S(n + 1), S(n + 2), S(n + 3)
     76  1.10.4.2      yamt #define S16(n) S4(n), S4(n + 4), S4(n + 8), S4(n + 12)
     77  1.10.4.2      yamt #define S64(n) S16(n), S16(n + 16), S16(n + 32), S16(n + 48)
     78  1.10.4.2      yamt #define S256 S64(0), S64(64), S64(128), S64(192)
     79  1.10.4.2      yamt 
     80  1.10.4.2      yamt static struct arc4_stream rs = { .i = 0xff, .j = 0, .s = { S256 },
     81  1.10.4.2      yamt 		.stirred = 0, .mtx = MUTEX_INITIALIZER };
     82  1.10.4.2      yamt 
     83  1.10.4.2      yamt #undef S
     84  1.10.4.2      yamt #undef S4
     85  1.10.4.2      yamt #undef S16
     86  1.10.4.2      yamt #undef S64
     87  1.10.4.2      yamt #undef S256
     88       1.1    itojun 
     89       1.1    itojun static inline void arc4_addrandom(struct arc4_stream *, u_char *, int);
     90  1.10.4.2      yamt static __noinline void arc4_stir(struct arc4_stream *);
     91      1.10  christos static inline uint8_t arc4_getbyte(struct arc4_stream *);
     92      1.10  christos static inline uint32_t arc4_getword(struct arc4_stream *);
     93       1.1    itojun 
     94  1.10.4.2      yamt static inline int
     95  1.10.4.2      yamt arc4_check_init(struct arc4_stream *as)
     96       1.1    itojun {
     97  1.10.4.2      yamt 	if (__predict_true(rs.stirred))
     98  1.10.4.2      yamt 		return 0;
     99  1.10.4.1      yamt 
    100  1.10.4.1      yamt 	arc4_stir(as);
    101  1.10.4.2      yamt 	return 1;
    102       1.1    itojun }
    103       1.1    itojun 
    104       1.1    itojun static inline void
    105      1.10  christos arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen)
    106       1.1    itojun {
    107      1.10  christos 	uint8_t si;
    108  1.10.4.2      yamt 	size_t n;
    109       1.1    itojun 
    110  1.10.4.2      yamt 	for (n = 0; n < __arraycount(as->s); n++) {
    111       1.1    itojun 		as->i = (as->i + 1);
    112       1.1    itojun 		si = as->s[as->i];
    113       1.1    itojun 		as->j = (as->j + si + dat[n % datlen]);
    114       1.1    itojun 		as->s[as->i] = as->s[as->j];
    115       1.1    itojun 		as->s[as->j] = si;
    116       1.1    itojun 	}
    117       1.1    itojun }
    118       1.1    itojun 
    119  1.10.4.2      yamt static __noinline void
    120      1.10  christos arc4_stir(struct arc4_stream *as)
    121       1.1    itojun {
    122  1.10.4.1      yamt 	int rdat[32];
    123  1.10.4.2      yamt 	int mib[] = { CTL_KERN, KERN_URND };
    124  1.10.4.1      yamt 	size_t len;
    125  1.10.4.2      yamt 	size_t i, j;
    126  1.10.4.1      yamt 
    127  1.10.4.1      yamt 	/*
    128  1.10.4.1      yamt 	 * This code once opened and read /dev/urandom on each
    129  1.10.4.1      yamt 	 * call.  That causes repeated rekeying of the kernel stream
    130  1.10.4.1      yamt 	 * generator, which is very wasteful.  Because of application
    131  1.10.4.1      yamt 	 * behavior, caching the fd doesn't really help.  So we just
    132  1.10.4.1      yamt 	 * fill up the tank from sysctl, which is a tiny bit slower
    133  1.10.4.1      yamt 	 * for us but much friendlier to other entropy consumers.
    134  1.10.4.1      yamt 	 */
    135  1.10.4.1      yamt 
    136  1.10.4.2      yamt 	for (i = 0; i < __arraycount(rdat); i++) {
    137  1.10.4.1      yamt 		len = sizeof(rdat[i]);
    138  1.10.4.1      yamt 		if (sysctl(mib, 2, &rdat[i], &len, NULL, 0) == -1)
    139  1.10.4.1      yamt 			abort();
    140       1.1    itojun 	}
    141       1.1    itojun 
    142  1.10.4.1      yamt 	arc4_addrandom(as, (void *) &rdat, (int)sizeof(rdat));
    143       1.3    itojun 
    144       1.3    itojun 	/*
    145       1.3    itojun 	 * Throw away the first N words of output, as suggested in the
    146       1.3    itojun 	 * paper "Weaknesses in the Key Scheduling Algorithm of RC4"
    147       1.3    itojun 	 * by Fluher, Mantin, and Shamir.  (N = 256 in our case.)
    148       1.3    itojun 	 */
    149  1.10.4.2      yamt 	for (j = 0; j < __arraycount(as->s) * 4; j++)
    150       1.3    itojun 		arc4_getbyte(as);
    151  1.10.4.2      yamt 
    152  1.10.4.2      yamt 	as->stirred = 1;
    153       1.1    itojun }
    154       1.1    itojun 
    155  1.10.4.2      yamt static __always_inline uint8_t
    156  1.10.4.2      yamt arc4_getbyte_ij(struct arc4_stream *as, uint8_t *i, uint8_t *j)
    157       1.1    itojun {
    158      1.10  christos 	uint8_t si, sj;
    159       1.1    itojun 
    160  1.10.4.2      yamt 	*i = *i + 1;
    161  1.10.4.2      yamt 	si = as->s[*i];
    162  1.10.4.2      yamt 	*j = *j + si;
    163  1.10.4.2      yamt 	sj = as->s[*j];
    164  1.10.4.2      yamt 	as->s[*i] = sj;
    165  1.10.4.2      yamt 	as->s[*j] = si;
    166       1.1    itojun 	return (as->s[(si + sj) & 0xff]);
    167       1.1    itojun }
    168       1.1    itojun 
    169  1.10.4.2      yamt static inline uint8_t
    170  1.10.4.2      yamt arc4_getbyte(struct arc4_stream *as)
    171  1.10.4.2      yamt {
    172  1.10.4.2      yamt 	return arc4_getbyte_ij(as, &as->i, &as->j);
    173  1.10.4.2      yamt }
    174  1.10.4.2      yamt 
    175      1.10  christos static inline uint32_t
    176      1.10  christos arc4_getword(struct arc4_stream *as)
    177       1.1    itojun {
    178      1.10  christos 	uint32_t val;
    179       1.1    itojun 	val = arc4_getbyte(as) << 24;
    180       1.1    itojun 	val |= arc4_getbyte(as) << 16;
    181       1.1    itojun 	val |= arc4_getbyte(as) << 8;
    182       1.1    itojun 	val |= arc4_getbyte(as);
    183       1.1    itojun 	return val;
    184       1.1    itojun }
    185       1.1    itojun 
    186       1.1    itojun void
    187      1.10  christos arc4random_stir(void)
    188       1.1    itojun {
    189  1.10.4.2      yamt 	LOCK(&rs);
    190  1.10.4.2      yamt 	arc4_stir(&rs);
    191  1.10.4.2      yamt 	UNLOCK(&rs);
    192       1.1    itojun }
    193       1.1    itojun 
    194       1.1    itojun void
    195      1.10  christos arc4random_addrandom(u_char *dat, int datlen)
    196       1.1    itojun {
    197  1.10.4.2      yamt 	LOCK(&rs);
    198  1.10.4.2      yamt 	arc4_check_init(&rs);
    199  1.10.4.2      yamt 	arc4_addrandom(&rs, dat, datlen);
    200  1.10.4.2      yamt 	UNLOCK(&rs);
    201       1.1    itojun }
    202       1.1    itojun 
    203      1.10  christos uint32_t
    204      1.10  christos arc4random(void)
    205       1.1    itojun {
    206  1.10.4.1      yamt 	uint32_t v;
    207  1.10.4.2      yamt 
    208  1.10.4.2      yamt 	LOCK(&rs);
    209  1.10.4.2      yamt 	arc4_check_init(&rs);
    210  1.10.4.2      yamt 	v = arc4_getword(&rs);
    211  1.10.4.2      yamt 	UNLOCK(&rs);
    212  1.10.4.1      yamt 	return v;
    213       1.1    itojun }
    214       1.1    itojun 
    215  1.10.4.2      yamt void
    216  1.10.4.2      yamt arc4random_buf(void *buf, size_t len)
    217      1.10  christos {
    218      1.10  christos 	uint8_t *bp = buf;
    219      1.10  christos 	uint8_t *ep = bp + len;
    220  1.10.4.2      yamt 	uint8_t i, j;
    221      1.10  christos 
    222  1.10.4.2      yamt 	LOCK(&rs);
    223  1.10.4.2      yamt 	arc4_check_init(&rs);
    224  1.10.4.1      yamt 
    225  1.10.4.2      yamt 	/* cache i and j - compiler can't know 'buf' doesn't alias them */
    226  1.10.4.2      yamt 	i = rs.i;
    227  1.10.4.2      yamt 	j = rs.j;
    228      1.10  christos 
    229      1.10  christos 	while (bp < ep)
    230  1.10.4.2      yamt 		*bp++ = arc4_getbyte_ij(&rs, &i, &j);
    231  1.10.4.2      yamt 	rs.i = i;
    232  1.10.4.2      yamt 	rs.j = j;
    233      1.10  christos 
    234  1.10.4.2      yamt 	UNLOCK(&rs);
    235  1.10.4.1      yamt }
    236  1.10.4.1      yamt 
    237      1.10  christos /*-
    238      1.10  christos  * Written by Damien Miller.
    239      1.10  christos  * With simplifications by Jinmei Tatuya.
    240      1.10  christos  */
    241      1.10  christos 
    242      1.10  christos /*
    243      1.10  christos  * Calculate a uniformly distributed random number less than
    244      1.10  christos  * upper_bound avoiding "modulo bias".
    245      1.10  christos  *
    246      1.10  christos  * Uniformity is achieved by generating new random numbers
    247      1.10  christos  * until the one returned is outside the range
    248      1.10  christos  * [0, 2^32 % upper_bound[. This guarantees the selected
    249      1.10  christos  * random number will be inside the range
    250      1.10  christos  * [2^32 % upper_bound, 2^32[ which maps back to
    251      1.10  christos  * [0, upper_bound[ after reduction modulo upper_bound.
    252      1.10  christos  */
    253  1.10.4.2      yamt uint32_t
    254  1.10.4.2      yamt arc4random_uniform(uint32_t upper_bound)
    255      1.10  christos {
    256      1.10  christos 	uint32_t r, min;
    257      1.10  christos 
    258      1.10  christos 	if (upper_bound < 2)
    259      1.10  christos 		return 0;
    260      1.10  christos 
    261      1.10  christos 	/* calculate (2^32 % upper_bound) avoiding 64-bit math */
    262  1.10.4.2      yamt 	/* ((2^32 - x) % x) == (2^32 % x) when x <= 2^31 */
    263  1.10.4.2      yamt 	min = (0xFFFFFFFFU - upper_bound + 1) % upper_bound;
    264  1.10.4.2      yamt 
    265  1.10.4.2      yamt 	LOCK(&rs);
    266  1.10.4.2      yamt 	arc4_check_init(&rs);
    267      1.10  christos 
    268      1.10  christos 	/*
    269      1.10  christos 	 * This could theoretically loop forever but each retry has
    270      1.10  christos 	 * p > 0.5 (worst case, usually far better) of selecting a
    271      1.10  christos 	 * number inside the range we need, so it should rarely need
    272      1.10  christos 	 * to re-roll (at all).
    273      1.10  christos 	 */
    274      1.10  christos 	do
    275      1.10  christos 		r = arc4_getword(&rs);
    276      1.10  christos 	while (r < min);
    277  1.10.4.2      yamt 	UNLOCK(&rs);
    278      1.10  christos 
    279      1.10  christos 	return r % upper_bound;
    280      1.10  christos }
    281