-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptopals_mersenne.h
43 lines (35 loc) · 1.11 KB
/
cryptopals_mersenne.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#pragma once
extern "C" {
#include "cryptopals_utils.h"
}
#define N 624 // size of state array = degree of recurrence
namespace cryptopals {
class mt19937 {
public:
// Initializes state array from seed.
mt19937(uint32_t seed);
// Alternate constructor: set entire state array.
mt19937(uint32_t seed[N]);
// Does the same thing as the first constructor, but by making
// this a public method, we can reinitialize the same object.
void srand(uint32_t seed);
// Get a random value.
uint32_t rand();
private:
uint32_t state[N];
uint32_t next_state();
void step_state();
static uint32_t right_mult_A(uint32_t x);
static uint32_t temper(uint32_t x);
};
// A bad stream cipher implemented naively from the RNG
class mt19937_cipher {
public:
mt19937_cipher(uint32_t s) : seed(s) {}
byte_array encrypt(const byte_array plain);
byte_array decrypt(const byte_array cipher);
private:
uint32_t seed;
byte_array encrypt_decrypt(const byte_array input);
};
}