-
Notifications
You must be signed in to change notification settings - Fork 17
/
rlwe_rand_openssl_rc4.c
63 lines (54 loc) · 1.78 KB
/
rlwe_rand_openssl_rc4.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#warning "RC4 should not be considered a cryptographically secure pseudorandom number generator due to biases in its keystream output. See README.md and the Makefile to switch to an AES-based PRNG."
#include "rlwe_rand.h"
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <stdio.h>
#include <string.h>
int RAND_CHOICE_init(RAND_CTX *rand_ctx) {
unsigned char rc4_key[16];
RAND_bytes(rc4_key, 16);
EVP_CIPHER_CTX_init(rand_ctx);
return EVP_EncryptInit_ex(rand_ctx, EVP_rc4(), NULL, rc4_key, NULL);
}
void RAND_CHOICE_cleanup(RAND_CTX *rand_ctx) {
EVP_CIPHER_CTX_cleanup(rand_ctx);
}
uint8_t RANDOM8(RAND_CTX *rand_ctx) {
uint8_t in = 0;
uint8_t out;
int outlen;
int ret = EVP_EncryptUpdate(rand_ctx, (unsigned char *) &out, &outlen, (unsigned char *) &in, sizeof(uint8_t));
if (ret != 1) {
fprintf(stderr, "Randomness generation failed.\n");
}
return out;
}
uint32_t RANDOM32(RAND_CTX *rand_ctx) {
uint32_t in = 0;
uint32_t out;
int outlen;
int ret = EVP_EncryptUpdate(rand_ctx, (unsigned char *) &out, &outlen, (unsigned char *) &in, sizeof(uint32_t));
if (ret != 1) {
fprintf(stderr, "Randomness generation failed.\n");
}
return out;
}
uint64_t RANDOM64(RAND_CTX *rand_ctx) {
uint64_t in = 0;
uint64_t out;
int outlen;
int ret = EVP_EncryptUpdate(rand_ctx, (unsigned char *) &out, &outlen, (unsigned char *) &in, sizeof(uint64_t));
if (ret != 1) {
fprintf(stderr, "Randomness generation failed.\n");
}
return out;
}
void RANDOM192(uint64_t r[3], RAND_CTX *rand_ctx) {
uint64_t in[] = {0, 0, 0};
int outlen;
int ret = EVP_EncryptUpdate(rand_ctx, (unsigned char *) r, &outlen, (unsigned char *) &in, sizeof(uint64_t));
if (ret != 1) {
fprintf(stderr, "Randomness generation failed.\n");
}
}
void *(*volatile rlwe_memset_volatile)(void *, int, size_t) = memset;