CS318 - Pintos
Pintos source browser for JHU CS318 course
random.c
Go to the documentation of this file.
1 #include "random.h"
2 #include <stdbool.h>
3 #include <stdint.h>
4 #include "debug.h"
5 
6 /** RC4-based pseudo-random number generator (PRNG).
7 
8  RC4 is a stream cipher. We're not using it here for its
9  cryptographic properties, but because it is easy to implement
10  and its output is plenty random for non-cryptographic
11  purposes.
12 
13  See http://en.wikipedia.org/wiki/RC4_(cipher) for information
14  on RC4.*/
15 
16 /** RC4 state. */
17 static uint8_t s[256]; /**< S[]. */
18 static uint8_t s_i, s_j; /**< i, j. */
19 
20 /** Already initialized? */
21 static bool inited;
22 
23 /** Swaps the bytes pointed to by A and B. */
24 static inline void
26 {
27  uint8_t t = *a;
28  *a = *b;
29  *b = t;
30 }
31 
32 /** Initializes or reinitializes the PRNG with the given SEED. */
33 void
34 random_init (unsigned seed)
35 {
36  uint8_t *seedp = (uint8_t *) &seed;
37  int i;
38  uint8_t j;
39 
40  for (i = 0; i < 256; i++)
41  s[i] = i;
42  for (i = j = 0; i < 256; i++)
43  {
44  j += s[i] + seedp[i % sizeof seed];
45  swap_byte (s + i, s + j);
46  }
47 
48  s_i = s_j = 0;
49  inited = true;
50 }
51 
52 /** Writes SIZE random bytes into BUF. */
53 void
54 random_bytes (void *buf_, size_t size)
55 {
56  uint8_t *buf;
57 
58  if (!inited)
59  random_init (0);
60 
61  for (buf = buf_; size-- > 0; buf++)
62  {
63  uint8_t s_k;
64 
65  s_i++;
66  s_j += s[s_i];
67  swap_byte (s + s_i, s + s_j);
68 
69  s_k = s[s_i] + s[s_j];
70  *buf = s[s_k];
71  }
72 }
73 
74 /** Returns a pseudo-random unsigned long.
75  Use random_ulong() % n to obtain a random number in the range
76  0...n (exclusive). */
77 unsigned long
78 random_ulong (void)
79 {
80  unsigned long ul;
81  random_bytes (&ul, sizeof ul);
82  return ul;
83 }
uint8_t
unsigned char uint8_t
Definition: stdint.h:20
swap_byte
static void swap_byte(uint8_t *a, uint8_t *b)
Swaps the bytes pointed to by A and B.
Definition: random.c:25
buf
static char buf[BUF_SIZE]
Definition: child-syn-read.c:16
random_ulong
unsigned long random_ulong(void)
Returns a pseudo-random unsigned long.
Definition: random.c:78
stdbool.h
random_bytes
void random_bytes(void *buf_, size_t size)
Writes SIZE random bytes into BUF.
Definition: random.c:54
random.h
random_init
void random_init(unsigned seed)
Initializes or reinitializes the PRNG with the given SEED.
Definition: random.c:34
s_i
static uint8_t s_i
Definition: random.c:18
stdint.h
inited
static bool inited
Already initialized?
Definition: random.c:21
s
static uint8_t s[256]
RC4-based pseudo-random number generator (PRNG).
Definition: random.c:17
s_j
static uint8_t s_j
i, j.
Definition: random.c:18
debug.h