Class: RFuzz::FuzzRnd
- Inherits:
-
Object
- Object
- RFuzz::FuzzRnd
- Defined in:
- ext/fuzzrnd/fuzzrnd.c
Instance Method Summary collapse
-
#seed ⇒ Object
Returns a String of random bytes of length that you can use for generating randomness.
-
#seed ⇒ Object
constructor
Seeds the global ArcFour random generator with the given seed.
-
#seed ⇒ Object
Seeds the global ArcFour random generator with the given seed.
Constructor Details
#seed ⇒ Object
Seeds the global ArcFour random generator with the given seed. The same seeds should produce the exact same stream of random data so that you can get large amounts of randomness but replay possible interactions using just an initial key.
This function also doubles as the FuzzRnd.initialize method since they do nearly the same thing.
Taken from www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt sample code, but compared with the output of the ArcFour implementation in the Phelix test code to make sure it is the same initialization. The main difference is that this init takes an arbitrary keysize while the original Phelix ArcFour only took a 32bit key.
Returns itself so you can seed and then get data easily.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'ext/fuzzrnd/fuzzrnd.c', line 98
VALUE FuzzRnd_seed(VALUE self, VALUE data) {
unsigned int t, u;
unsigned int keyindex;
unsigned int stateindex;
unsigned char *state;
unsigned int counter;
char *key = NULL;
size_t key_len = 0;
REQUIRE_TYPE(data, T_STRING);
key = RSTRING(data)->ptr;
key_len = RSTRING(data)->len;
state = ArcFour.sbox;
ArcFour.i = 0;
ArcFour.j = 0;
for (counter = 0; counter < 256; counter++)
state[counter] = counter;
keyindex = 0;
stateindex = 0;
for (counter = 0; counter < 256; counter++)
{
t = state[counter];
stateindex = (stateindex + key[keyindex] + t) & 0xff;
u = state[stateindex];
state[stateindex] = t;
state[counter] = u;
if (++keyindex >= key_len)
keyindex = 0;
}
return self;
}
|
Instance Method Details
#seed ⇒ Object
Returns a String of random bytes of length that you can use for generating randomness. It uses the ArcFour cipher to make the randomness, so the same seeds produce the same random bits, and the randomness is reasonably high quality.
Don’t use this for secure random generation. It probably would work if you seeded from a /dev/random that worked, but don’t blame me if you get hacked.
The main motiviation for using ArcFour without automated reseed is to produce lots of random bytes quickly, make them high enough quality for good random tests, and to make sure that we can replay possible sequences if there’s a sequence that we want to test.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'ext/fuzzrnd/fuzzrnd.c', line 47
VALUE FuzzRnd_data(VALUE self, VALUE length)
{
unsigned int n;
unsigned char a,b;
size_t len = 0;
VALUE data;
char *p = NULL;
REQUIRE_TYPE(length, T_FIXNUM);
len = FIX2INT(length);
data = rb_str_buf_new(len);
p = RSTRING(data)->ptr;
rb_str_resize(data, len);
for (n=0;n<len;n++) /* run the ArcFour algorithm as long as it needs */
{
ArcFour.i++;
a = ArcFour.sbox[ArcFour.i];
ArcFour.j = (unsigned char) (ArcFour.j + a); /* avoid MSVC picky compiler warning */
b = ArcFour.sbox[ArcFour.j];
ArcFour.sbox[ArcFour.i] = b;
ArcFour.sbox[ArcFour.j] = a;
p[n] = ArcFour.sbox[(a+b) & 0xFF];
}
return data;
}
|
#seed ⇒ Object
Seeds the global ArcFour random generator with the given seed. The same seeds should produce the exact same stream of random data so that you can get large amounts of randomness but replay possible interactions using just an initial key.
This function also doubles as the FuzzRnd.initialize method since they do nearly the same thing.
Taken from www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt sample code, but compared with the output of the ArcFour implementation in the Phelix test code to make sure it is the same initialization. The main difference is that this init takes an arbitrary keysize while the original Phelix ArcFour only took a 32bit key.
Returns itself so you can seed and then get data easily.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'ext/fuzzrnd/fuzzrnd.c', line 98
VALUE FuzzRnd_seed(VALUE self, VALUE data) {
unsigned int t, u;
unsigned int keyindex;
unsigned int stateindex;
unsigned char *state;
unsigned int counter;
char *key = NULL;
size_t key_len = 0;
REQUIRE_TYPE(data, T_STRING);
key = RSTRING(data)->ptr;
key_len = RSTRING(data)->len;
state = ArcFour.sbox;
ArcFour.i = 0;
ArcFour.j = 0;
for (counter = 0; counter < 256; counter++)
state[counter] = counter;
keyindex = 0;
stateindex = 0;
for (counter = 0; counter < 256; counter++)
{
t = state[counter];
stateindex = (stateindex + key[keyindex] + t) & 0xff;
u = state[stateindex];
state[stateindex] = t;
state[counter] = u;
if (++keyindex >= key_len)
keyindex = 0;
}
return self;
}
|