forked from tinskip/infnoise-openssl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathe_infnoise.c
192 lines (160 loc) · 5.74 KB
/
e_infnoise.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2018 Thomás Inskip. All rights reserved.
// https://github.com/tinskip/infnoise-openssl-engine
//
// Implementation of OpenSSL RAND engine which uses the infnoise TRNG to
// generate true random numbers: https://github.com/waywardgeek/infnoise
#include <libinfnoise.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
static const int kEngineOk = 1;
static const int kEngineFail = 0;
///////////////////
// Configuration
///////////////////
static const int kInfnoiseMultiplier = 1;
static const char *kInfnoiseSerial = NULL;
static const bool kKeccak = true;
static const bool kDebug = true;
////////////////////////////////
// Ring buffer implementation
////////////////////////////////
#define kRingBufferSize (2u * BUFLEN) // So that we do not waste TRNG bytes.
typedef struct {
uint8_t buffer[kRingBufferSize];
uint8_t *r_ptr;
uint8_t *w_ptr;
} RingBuffer;
static void RingBufferInit(RingBuffer *buffer) {
memset(buffer->buffer, 0, sizeof(buffer->buffer));
buffer->r_ptr = buffer->buffer;
buffer->w_ptr = buffer->buffer;
}
static size_t RingBufferRead(RingBuffer *buffer, size_t num_bytes,
uint8_t *output) {
size_t total_bytes_read = 0;
if (buffer->r_ptr > buffer->w_ptr) {
size_t bytes_in_front = kRingBufferSize - (buffer->r_ptr - buffer->buffer);
size_t bytes_read = MIN(num_bytes, bytes_in_front);
memcpy(output, buffer->r_ptr, bytes_read);
if (bytes_read < bytes_in_front) {
buffer->r_ptr += bytes_read;
return bytes_read;
}
buffer->r_ptr = buffer->buffer;
total_bytes_read += bytes_read;
num_bytes -= bytes_read;
}
size_t bytes_read = MIN(num_bytes, (size_t)(buffer->w_ptr - buffer->r_ptr));
memcpy(output, buffer->r_ptr, bytes_read);
buffer->r_ptr += bytes_read;
if ((buffer->r_ptr - buffer->buffer) == sizeof(buffer->buffer)) {
buffer->r_ptr = buffer->buffer;
}
total_bytes_read += bytes_read;
return total_bytes_read;
}
static size_t RingBufferWrite(RingBuffer *buffer, size_t num_bytes,
const uint8_t *input) {
size_t total_bytes_written = 0;
if (buffer->w_ptr > buffer->r_ptr) {
size_t free_bytes_in_front =
kRingBufferSize - (buffer->w_ptr - buffer->buffer);
size_t bytes_write = MIN(num_bytes, free_bytes_in_front);
memcpy(buffer->w_ptr, input, bytes_write);
if (bytes_write < num_bytes) {
buffer->w_ptr += bytes_write;
return bytes_write;
}
buffer->w_ptr = buffer->buffer;
total_bytes_written += bytes_write;
num_bytes -= bytes_write;
}
size_t bytes_write =
MIN(num_bytes, kRingBufferSize - (buffer->w_ptr - buffer->r_ptr));
memcpy(buffer->w_ptr, input, bytes_write);
buffer->w_ptr += bytes_write;
if ((buffer->w_ptr - buffer->buffer) == sizeof(buffer->buffer)) {
buffer->w_ptr = buffer->buffer;
}
total_bytes_written += bytes_write;
return total_bytes_written;
}
///////////////////////////
// Engine implementation
///////////////////////////
typedef struct {
struct infnoise_context trng_context;
RingBuffer ring_buffer;
int status;
} InfnoiseEngineState;
static int InfnoiseEngineStateInit(InfnoiseEngineState *engine_state) {
memset(engine_state, 0, sizeof(*engine_state));
RingBufferInit(&engine_state->ring_buffer);
engine_state->status = initInfnoise(&engine_state->trng_context,
kInfnoiseSerial, kKeccak, !kDebug);
if (!engine_state->status) {
fprintf(stderr, "initInfnoise initialization error: %s\n",
engine_state->trng_context.message
? engine_state->trng_context.message
: "unknown");
}
return engine_state->status;
}
static InfnoiseEngineState engine_state;
static int Bytes(unsigned char *buf, int num) {
unsigned char *w_ptr = buf;
while ((num > 0) && (engine_state.status == kEngineOk)) {
size_t bytes_read = RingBufferRead(&engine_state.ring_buffer, num, w_ptr);
w_ptr += bytes_read;
num -= bytes_read;
if (num > 0) {
// Need more TRNG bytes.
uint8_t rand_buffer[BUFLEN];
size_t rand_bytes = readData(&engine_state.trng_context, rand_buffer,
kKeccak, kInfnoiseMultiplier);
if (engine_state.trng_context.errorFlag) {
fprintf(stderr, "Infnoise error: %s\n",
engine_state.trng_context.message
? engine_state.trng_context.message
: "unknown");
engine_state.status = kEngineFail;
break;
}
size_t bytes_written =
RingBufferWrite(&engine_state.ring_buffer, rand_bytes, rand_buffer);
if (bytes_written != rand_bytes) {
fprintf(stderr, "Invalid infnoise engine buffer state!\n");
engine_state.status = kEngineFail;
break;
}
}
}
return engine_state.status;
}
static int Status(void) { return engine_state.status; }
int infnoise_bind(ENGINE *engine, const char *id) {
static const char kEngineId[] = "infnoise";
static const char kEngineName[] = "RNG engine using the infnoise TRNG";
static RAND_METHOD rand_method = {NULL, &Bytes, NULL, NULL,
&Bytes, // No 'pseudo'.
&Status};
if (ENGINE_set_id(engine, kEngineId) != kEngineOk ||
ENGINE_set_name(engine, kEngineName) != kEngineOk ||
ENGINE_set_RAND(engine, &rand_method) != kEngineOk) {
fprintf(stderr, "infnoise-engine: Binding failed.\n");
return 0;
}
if (InfnoiseEngineStateInit(&engine_state) != kEngineOk) {
exit(-1);
}
fprintf(stderr, "Infnoise engine loaded.\n");
return kEngineOk;
}
IMPLEMENT_DYNAMIC_BIND_FN(infnoise_bind)
IMPLEMENT_DYNAMIC_CHECK_FN()