00001 #include "random.h" 00002 //3225770837496 00003 #include <stdbool.h> 00004 #include <stdint.h> 00005 #include "debug.h" 00006 00007 /* RC4-based pseudo-random number generator (PRNG). 00008 00009 RC4 is a stream cipher. We're not using it here for its 00010 cryptographic properties, but because it is easy to implement 00011 and its output is plenty random for non-cryptographic 00012 purposes. 00013 00014 See http://en.wikipedia.org/wiki/RC4_(cipher) for information 00015 on RC4.*/ 00016 00017 /* RC4 state. */ 00018 static uint8_t s[256]; /* S[]. */ 00019 static uint8_t s_i, s_j; /* i, j. */ 00020 00021 /* Already initialized? */ 00022 static bool inited; 00023 00024 /* Swaps the bytes pointed to by A and B. */ 00025 static inline void 00026 swap_byte (uint8_t *a, uint8_t *b) 00027 { 00028 uint8_t t = *a; 00029 *a = *b; 00030 *b = t; 00031 } 00032 00033 /* Initializes or reinitializes the PRNG with the given SEED. */ 00034 void 00035 random_init (unsigned seed) 00036 { 00037 uint8_t *seedp = (uint8_t *) &seed; 00038 int i; 00039 uint8_t j; 00040 00041 for (i = 0; i < 256; i++) 00042 s[i] = i; 00043 for (i = j = 0; i < 256; i++) 00044 { 00045 j += s[i] + seedp[i % sizeof seed]; 00046 swap_byte (s + i, s + j); 00047 } 00048 00049 s_i = s_j = 0; 00050 inited = true; 00051 } 00052 00053 /* Writes SIZE random bytes into BUF. */ 00054 void 00055 random_bytes (void *buf_, size_t size) 00056 { 00057 uint8_t *buf; 00058 00059 if (!inited) 00060 random_init (0); 00061 00062 for (buf = buf_; size-- > 0; buf++) 00063 { 00064 uint8_t s_k; 00065 00066 s_i++; 00067 s_j += s[s_i]; 00068 swap_byte (s + s_i, s + s_j); 00069 00070 s_k = s[s_i] + s[s_j]; 00071 *buf = s[s_k]; 00072 } 00073 } 00074 00075 /* Returns a pseudo-random unsigned long. 00076 Use random_ulong() % n to obtain a random number in the range 00077 0...n (exclusive). */ 00078 unsigned long 00079 random_ulong (void) 00080 { 00081 unsigned long ul; 00082 random_bytes (&ul, sizeof ul); 00083 return ul; 00084 }