This repository was archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrbg_hash.c
254 lines (202 loc) · 7.84 KB
/
drbg_hash.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//
// Created by Ghost on 2019/9/11.
//
#include "drbg_hash.h"
static bool hash_df(DRBG_HASH_CONF *conf,
const uint8_t *input1, uint32_t input1_length,
const uint8_t *input2, uint32_t input2_length,
const uint8_t *input3, uint32_t input3_length,
uint8_t *output, uint32_t return_length) {
bool ret = true;
uint8_t *temp = malloc(conf->out_len);
uint8_t to_hash[1 + 4];
uint32_t remain;
uint32_t input_length = input1_length + input2_length + input3_length;
// for every round, hash function output outlen bytes data
// since round is calculated to make output full of data returned
// we directly calculate remaining bytes to fill up output
to_hash[0] = 0;
to_hash[1] = (uint8_t) ((input_length >> 24u) & 0xffu);
to_hash[2] = (uint8_t) ((input_length >> 16u) & 0xffu);
to_hash[3] = (uint8_t) ((input_length >> 8u) & 0xffu);
to_hash[4] = (uint8_t) (input_length & 0xffu);
for (remain = return_length;; remain -= conf->out_len) {
to_hash[0]++;
if ((ret = conf->hash(to_hash, 1 + 4, input1, input1_length,
input2, input2_length, input3, input3_length, temp)) != true) goto cleanup;
// use the smaller one size, hash outputs up to hash_size length
if (remain > conf->out_len)
memcpy(&output[return_length - remain], temp, conf->out_len);
else {
// output remains less than out_len, which reaches final block
memcpy(&output[return_length - remain], temp, remain);
break;
}
}
cleanup:
free(temp);
return ret;
}
bool DRBG_HASH_new(DRBG_HASH *drbg, DRBG_HASH_CONF *conf) {
uint8_t *v, *c;
// validate config
if (conf == NULL || conf->hash == NULL ||
conf->seed_len == 0 ||
conf->out_len == 0 ||
conf->reseed_interval == 0)
return false;
drbg->conf = conf;
/* initialize internal state */
v = malloc(conf->seed_len);
memset(v, 0, conf->seed_len);
drbg->V = v;
c = malloc(conf->seed_len);
memset(c, 0, conf->seed_len);
drbg->C = c;
drbg->reseed_counter = 0;
drbg->prediction_resistance_flag = 0;
return true;
}
bool DRBG_HASH_instantiate(DRBG_HASH *drbg,
const uint8_t *entropy, uint32_t entropy_length,
const uint8_t *nonce, uint32_t nonce_length,
const uint8_t *pstr, uint32_t pstr_length) {
uint8_t zero = 0x00;
// seed_material = entropy_input || nonce || personalization_string
// V = Hash_df(seed_material, seed_len)
hash_df(drbg->conf,
entropy, entropy_length, nonce, nonce_length, pstr, pstr_length,
drbg->V, drbg->conf->seed_len);
// C = Hash_df(0x00||V, seed_len)
hash_df(drbg->conf, &zero, 1, drbg->V, drbg->conf->seed_len,
NULL, 0,
drbg->C, drbg->conf->seed_len);
drbg->reseed_counter = 1;
return true;
}
bool DRBG_HASH_reseed(DRBG_HASH *drbg,
uint8_t *entropy, uint32_t entropy_length,
uint8_t *add_input, uint32_t add_length) {
bool ret;
uint8_t one = 0x01;
uint8_t zero = 0x00;
// seed_material = 0x01 || V || entropy_input || additional_input
// V = Hash_df(seed_material, seed_len)
ret = hash_df(drbg->conf,
&one, 1, entropy, entropy_length, add_input, add_length,
drbg->V, drbg->conf->seed_len);
if (ret != true) return ret;
// C = Hash_df(0x00||V, seed_len)
ret = hash_df(drbg->conf, &zero, 1, drbg->V, drbg->conf->seed_len,
NULL, 0,
drbg->C, drbg->conf->seed_len);
drbg->reseed_counter = 1;
return ret;
}
// modify from openssl/crypto/rand/drbg_hash.c
static bool add(uint8_t *dst, uint32_t dst_len,
const uint8_t *in, uint32_t inlen) {
uint32_t i;
uint32_t result;
const uint8_t *add;
uint8_t carry = 0, *d;
if (!(dst_len >= 1 && inlen >= 1 && inlen <= dst_len)) return false;
d = &dst[dst_len - 1];
add = &in[inlen - 1];
for (i = inlen; i > 0; i--, d--, add--) {
result = *d + *add + carry;
carry = (uint8_t) (result >> 8u);
*d = (uint8_t) (result & 0xffu);
}
if (carry != 0) {
/* Add the carry to the top of the dst if inlen is not the same size */
for (i = dst_len - inlen; i > 0; --i, d--) {
*d += 1; /* Carry can only be 1 */
if (*d != 0) /* exit if carry doesnt propagate to the next byte */
break;
}
}
return true;
}
static bool hashgen(DRBG_HASH *drbg, uint32_t return_length, uint8_t *output) {
bool ret;
// data = V
uint32_t remain;
uint8_t one = 1;
uint8_t *data = malloc(drbg->conf->seed_len);
memcpy(data, drbg->V, drbg->conf->seed_len);
// since round is calculated to make output full of data returned
// we directly calculate remaining bytes to fill up output
uint8_t *tmp = malloc(drbg->conf->out_len);
for (remain = return_length;; remain -= drbg->conf->out_len) {
// W = W || hash(data), till full of return length long bytes
if ((ret = drbg->conf->hash(data, drbg->conf->seed_len, NULL, 0, NULL, 0, NULL, 0, tmp)) != true) goto cleanup;
// use the smaller one size, hash outputs up to hash_size length
if (remain > drbg->conf->out_len) memcpy(&output[return_length - remain], tmp, drbg->conf->out_len);
else {
// output remains less than out_len, which reaches final block
memcpy(&output[return_length - remain], tmp, remain);
break;
}
// data = (data + 1) mod 2^seed_len
ret = add(data, drbg->conf->seed_len, &one, 1);
if (ret != true) goto cleanup;
}
cleanup:
free(data);
free(tmp);
return ret;
}
bool DRBG_HASH_generate(DRBG_HASH *drbg,
uint8_t *add_input, uint32_t add_length,
uint8_t *output, uint32_t return_length) {
bool ret;
uint8_t *w = malloc(drbg->conf->out_len);
uint8_t two = 0x02;
uint8_t three = 0x03;
uint8_t *htmp = malloc(1 + drbg->conf->seed_len);
uint8_t *H = malloc(drbg->conf->out_len);
uint8_t counter[4];
uint32_t reseed_counter = drbg->reseed_counter;
if (add_input == NULL) {
// w = hash(0x02||V||additional_input)
ret = drbg->conf->hash(&two, 1, drbg->V, drbg->conf->seed_len, add_input, add_length, NULL, 0, w);
if (ret != true) goto cleanup;
// V = (V + w) mod 2^seed_len
ret = add(drbg->V, drbg->conf->seed_len, w, drbg->conf->out_len);
if (ret != true) goto cleanup;
}
ret = hashgen(drbg, return_length, output);
if (ret != true) goto cleanup;
// H = hash(0x03||V)
htmp[0] = 0x03;
memcpy(&htmp[1], drbg->V, drbg->conf->seed_len);
if ((ret = drbg->conf->hash(&three, 1, drbg->V, drbg->conf->seed_len, NULL, 0, NULL, 0, H)) != true) goto cleanup;
// V = (V + H + C + reseed_counter) mod 2^seed_len
ret = add(drbg->V, drbg->conf->seed_len, H, drbg->conf->out_len);
if (ret != true) goto cleanup;
ret = add(drbg->V, drbg->conf->seed_len, drbg->C, drbg->conf->seed_len);
if (ret != true) goto cleanup;
counter[0] = (uint8_t) ((reseed_counter >> 24u) & 0xffu);
counter[1] = (uint8_t) ((reseed_counter >> 16u) & 0xffu);
counter[2] = (uint8_t) ((reseed_counter >> 8u) & 0xffu);
counter[3] = (uint8_t) (reseed_counter & 0xffu);
ret = add(drbg->V, drbg->conf->seed_len, counter, 4);
drbg->reseed_counter++;
cleanup:
free(w);
free(htmp);
free(H);
return ret;
}
bool DRBG_HASH_uninstantiate(DRBG_HASH *drbg) {
// clear V and C state
memset(drbg->V, 0, drbg->conf->seed_len);
free(drbg->V);
drbg->V = NULL;
memset(drbg->C, 0, drbg->conf->seed_len);
free(drbg->C);
drbg->C = NULL;
drbg->reseed_counter = 0;
return true;
}