diff --git a/share/man/man9/random.9 b/share/man/man9/random.9 index 734e58336b56..98d0ffab684f 100644 --- a/share/man/man9/random.9 +++ b/share/man/man9/random.9 @@ -1,134 +1,152 @@ .\" .\" Copyright (c) 2000 .\" The Regents of the University of California. All rights reserved. .\" .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" .\" $FreeBSD$ .\" " .Dd September 25, 2000 .Os .Dt RANDOM 9 .Sh NAME .Nm arc4random , .Nm random , .Nm read_random , .Nm srandom .Nd supply pseudo-random numbers .Sh SYNOPSIS .In sys/libkern.h .Ft void .Fn srandom "u_long seed" .Ft u_long .Fn random "void" +.Ft void +.Fn arc4rand "void *ptr" "u_int length" "int reseed" .Ft u_int32_t .Fn arc4random "void" .Pp .In sys/random.h .Ft int .Fn read_random "void *buffer" "int count" .Sh DESCRIPTION The .Fn random function will by default produce a sequence of numbers that can be duplicated by calling .Fn srandom with .Ql 1 as the .Ar seed . The .Fn srandom function may be called with any arbitrary .Ar seed value to get slightly more unpredictable numbers. It is important to remember that the .Fn random function is entirely predictable, and is therefore not of use where knowledge of the sequence of numbers may be of benefit to an attacker. .Pp The -.Fn arc4random +.Fn arc4rand function will return very good quality random numbers, slightly better suited for security-related purposes. The random numbers from -.Fn arc4random +.Fn arc4rand are seeded from the entropy device if it is available. +Automatic reseeds happen after a certain timeinterval and after a +certain number of bytes have been delivered. +A forced reseed can be forced by passing a non-zero value in the +.Ar reseed +argument. .Pp The .Fn read_random function is used to return entropy directly from the entropy device if it has been loaded. If the entropy device is not loaded, then the .Ar buffer is filled with output generated by .Fn random . The .Ar buffer is filled with no more than .Ar count bytes. It is advised that .Fn read_random is not used; instead use -.Fn arc4random . +.Fn arc4rand .Pp All the bits generated by .Fn random , -.Fn arc4random +.Fn arc4rand and .Fn read_random are usable. For example, .Sq Li random()&01 will produce a random binary value. +.Pp +The +.Fn arc4random +is a convenience function which calls +.Fn arc4rand +to return a 32 bit pseudo-random integer. .Sh RETURN VALUES The .Fn random function uses a non-linear additive feedback random number generator employing a default table of size 31 long integers to return successive pseudo-random numbers in the range from 0 to .if t 2\u\s731\s10\d\(mi1. .if n (2**31)\(mi1. The period of this random number generator is very large, approximately .if t 16\(mu(2\u\s731\s10\d\(mi1). .if n 16*((2**31)\(mi1). .Pp The +.Fn arc4rand +function uses the RC4 algorithm to generate successive pseudo-random +bytes. +The .Fn arc4random function -uses the RC4 algorithm to generate successive pseudo-random -numbers in the range from 0 to +uses +.Fn arc4rand +to generate pseudo-random numbers in the range from 0 to .if t 2\u\s732\s10\d\(mi1. .if n (2**32)\(mi1. .Pp The .Fn read_random function returns the number of bytes placed in .Ar buffer . .Sh AUTHORS .An Dan Moschuk wrote .Fn arc4random . .An Mark R V Murray wrote .Fn read_random . diff --git a/sys/libkern/arc4random.c b/sys/libkern/arc4random.c index 993f764f2d7b..6d6ed22b5612 100644 --- a/sys/libkern/arc4random.c +++ b/sys/libkern/arc4random.c @@ -1,137 +1,142 @@ /*- * THE BEER-WARE LICENSE * * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you * think this stuff is worth it, you can buy me a beer in return. * * Dan Moschuk * * $FreeBSD$ */ #include #include #include #include -#define ARC4_MAXRUNS 16384 +#define ARC4_RESEED_BYTES 65536 #define ARC4_RESEED_SECONDS 300 -#define ARC4_KEYBYTES 32 /* 256 bit key */ +#define ARC4_KEYBYTES (256 / 8) static u_int8_t arc4_i, arc4_j; static int arc4_initialized = 0; static int arc4_numruns = 0; static u_int8_t arc4_sbox[256]; -static struct timeval arc4_tv_nextreseed; +static time_t arc4_t_reseed; static u_int8_t arc4_randbyte(void); static __inline void arc4_swap(u_int8_t *a, u_int8_t *b) { u_int8_t c; c = *a; *a = *b; *b = c; } /* * Stir our S-box. */ static void arc4_randomstir (void) { u_int8_t key[256]; int r, n; + struct timeval tv_now; /* * XXX read_random() returns unsafe numbers if the entropy * device is not loaded -- MarkM. */ r = read_random(key, ARC4_KEYBYTES); /* If r == 0 || -1, just use what was on the stack. */ - if (r > 0) - { + if (r > 0) { for (n = r; n < sizeof(key); n++) key[n] = key[n % r]; } - for (n = 0; n < 256; n++) - { + for (n = 0; n < 256; n++) { arc4_j = (arc4_j + arc4_sbox[n] + key[n]) % 256; arc4_swap(&arc4_sbox[n], &arc4_sbox[arc4_j]); } /* Reset for next reseed cycle. */ - getmicrotime(&arc4_tv_nextreseed); - arc4_tv_nextreseed.tv_sec += ARC4_RESEED_SECONDS; + getmicrouptime(&tv_now); + arc4_t_reseed = tv_now.tv_sec + ARC4_RESEED_SECONDS; arc4_numruns = 0; } /* * Initialize our S-box to its beginning defaults. */ static void arc4_init(void) { int n; arc4_i = arc4_j = 0; for (n = 0; n < 256; n++) arc4_sbox[n] = (u_int8_t) n; arc4_randomstir(); arc4_initialized = 1; /* * Throw away the first N words of output, as suggested in the * paper "Weaknesses in the Key Scheduling Algorithm of RC4" * by Fluher, Mantin, and Shamir. (N = 256 in our case.) */ for (n = 0; n < 256*4; n++) arc4_randbyte(); } /* * Generate a random byte. */ static u_int8_t arc4_randbyte(void) { u_int8_t arc4_t; arc4_i = (arc4_i + 1) % 256; arc4_j = (arc4_j + arc4_sbox[arc4_i]) % 256; arc4_swap(&arc4_sbox[arc4_i], &arc4_sbox[arc4_j]); arc4_t = (arc4_sbox[arc4_i] + arc4_sbox[arc4_j]) % 256; return arc4_sbox[arc4_t]; } -u_int32_t -arc4random(void) +void +arc4rand(void *ptr, u_int len, int reseed) { - u_int32_t ret; - struct timeval tv_now; + u_char *p; + struct timeval tv; /* Initialize array if needed. */ if (!arc4_initialized) arc4_init(); - getmicrotime(&tv_now); - if ((++arc4_numruns > ARC4_MAXRUNS) || - (tv_now.tv_sec > arc4_tv_nextreseed.tv_sec)) - { + getmicrouptime(&tv); + arc4_numruns += len; + if (reseed || + (arc4_numruns > ARC4_RESEED_BYTES) || + (tv.tv_sec > arc4_t_reseed)) arc4_randomstir(); - } - ret = arc4_randbyte(); - ret |= arc4_randbyte() << 8; - ret |= arc4_randbyte() << 16; - ret |= arc4_randbyte() << 24; + p = ptr; + while (len--) + *p++ = arc4_randbyte(); +} + +uint32_t +arc4random(void) +{ + uint32_t ret; + arc4rand(&ret, sizeof ret, 0); return ret; } diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h index 047184069ac7..65ab341160ac 100644 --- a/sys/sys/libkern.h +++ b/sys/sys/libkern.h @@ -1,130 +1,131 @@ /*- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)libkern.h 8.1 (Berkeley) 6/10/93 * $FreeBSD$ */ #ifndef _SYS_LIBKERN_H_ #define _SYS_LIBKERN_H_ #include #include #ifdef _KERNEL #include #endif /* BCD conversions. */ extern u_char const bcd2bin_data[]; extern u_char const bin2bcd_data[]; extern char const hex2ascii_data[]; #define bcd2bin(bcd) (bcd2bin_data[bcd]) #define bin2bcd(bin) (bin2bcd_data[bin]) #define hex2ascii(hex) (hex2ascii_data[hex]) static __inline int imax(int a, int b) { return (a > b ? a : b); } static __inline int imin(int a, int b) { return (a < b ? a : b); } static __inline long lmax(long a, long b) { return (a > b ? a : b); } static __inline long lmin(long a, long b) { return (a < b ? a : b); } static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); } static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); } static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); } static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); } static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); } static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); } /* Prototypes for non-quad routines. */ -u_int32_t arc4random(void); +uint32_t arc4random(void); +void arc4rand(void *ptr, u_int len, int reseed); int bcmp(const void *, const void *, size_t); void *bsearch(const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); #ifndef HAVE_INLINE_FFS int ffs(int); #endif #ifndef HAVE_INLINE_FLS int fls(int); #endif int fnmatch(const char *, const char *, int); int locc(int, char *, u_int); void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); u_long random(void); char *index(const char *, int); char *rindex(const char *, int); int scanc(u_int, const u_char *, const u_char *, int); int skpc(int, int, char *); void srandom(u_long); char *strcat(char *, const char *); int strcmp(const char *, const char *); char *strcpy(char *, const char *); size_t strlcat(char *, const char *, size_t); size_t strlcpy(char *, const char *, size_t); size_t strlen(const char *); int strncmp(const char *, const char *, size_t); char *strncpy(char *, const char *, size_t); char *strsep(char **, const char *delim); int strvalid(const char *, size_t); static __inline int memcmp(const void *b1, const void *b2, size_t len) { return (bcmp(b1, b2, len)); } static __inline void * memset(void *b, int c, size_t len) { char *bb; if (c == 0) bzero(b, len); else for (bb = (char *)b; len--; ) *bb++ = c; return (b); } /* fnmatch() return values. */ #define FNM_NOMATCH 1 /* Match failed. */ /* fnmatch() flags. */ #define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */ #define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */ #define FNM_PERIOD 0x04 /* Period must be matched by period. */ #define FNM_LEADING_DIR 0x08 /* Ignore / after Imatch. */ #define FNM_CASEFOLD 0x10 /* Case insensitive search. */ #define FNM_IGNORECASE FNM_CASEFOLD #define FNM_FILE_NAME FNM_PATHNAME #endif /* !_SYS_LIBKERN_H_ */