forked from ANSSI-FR/libdrbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drbg.c
663 lines (597 loc) · 16.4 KB
/
drbg.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
/*
* Copyright (C) 2022 - This file is part of libdrbg project
*
* Author: Ryad BENADJILA <ryad.benadjila@ssi.gouv.fr>
* Contributor: Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "drbg.h"
/* Entropy gathering function
* NOTE: XXX: to be implemented by the user!
*/
#include "entropy.h"
#define DRBG_INIT_MAGIC 0x1963422244881273
/* The backends callback methods */
#ifdef WITH_HASH_DRBG
static drbg_methods hash_drbg_methods = {
.instantiate = hash_drbg_instantiate,
.generate = hash_drbg_generate,
.reseed = hash_drbg_reseed,
.uninstantiate = hash_drbg_uninstantiate,
.get_lengths = hash_drbg_get_lengths,
};
#endif
#ifdef WITH_HMAC_DRBG
static drbg_methods hmac_drbg_methods = {
.instantiate = hmac_drbg_instantiate,
.generate = hmac_drbg_generate,
.reseed = hmac_drbg_reseed,
.uninstantiate = hmac_drbg_uninstantiate,
.get_lengths = hmac_drbg_get_lengths,
};
#endif
#ifdef WITH_CTR_DRBG
static drbg_methods ctr_drbg_methods = {
.instantiate = ctr_drbg_instantiate,
.generate = ctr_drbg_generate,
.reseed = ctr_drbg_reseed,
.uninstantiate = ctr_drbg_uninstantiate,
.get_lengths = ctr_drbg_get_lengths,
};
#endif
/* DRBG for PRNG.
* Standardized by NIST SP 800-90A.
* We support HMAC-DRBG, HASH-DRBG and CTR-DRBG
* as possible backends.
*/
static drbg_error drbg_check_initialized(drbg_ctx *ctx)
{
drbg_error ret = DRBG_ERROR;
if(ctx == NULL){
ret = DRBG_ILLEGAL_INPUT;
goto err;
}
if(ctx->magic != DRBG_INIT_MAGIC){
ret = DRBG_NON_INIT;
goto err;
}
ret = DRBG_OK;
err:
return ret;
}
/**************************************/
/* DRBG properties getters */
drbg_error drbg_check_instantiated(drbg_ctx *ctx)
{
drbg_error ret = DRBG_NON_INIT;
if(drbg_check_initialized(ctx) != DRBG_OK){
ret = DRBG_NON_INIT;
goto err;
}
if((ctx->is_instantiated == false) ||
(ctx->engine_is_instantiated == false)){
ret = DRBG_NON_INIT;
goto err;
}
/* Sanity check on internal stuff */
switch(ctx->type){
#ifdef WITH_HASH_DRBG
case DRBG_HASH:{
if((ctx->methods != (&hash_drbg_methods)) ||
(ctx->engine_magic != HASH_DRBG_INIT_MAGIC)){
ret = DRBG_NON_INIT;
goto err;
}
break;
}
#endif
#ifdef WITH_HMAC_DRBG
case DRBG_HMAC:{
if((ctx->methods != (&hmac_drbg_methods)) ||
(ctx->engine_magic != HMAC_DRBG_INIT_MAGIC)){
ret = DRBG_NON_INIT;
goto err;
}
break;
}
#endif
#ifdef WITH_CTR_DRBG
case DRBG_CTR:{
if((ctx->methods != (&ctr_drbg_methods)) ||
(ctx->engine_magic != CTR_DRBG_INIT_MAGIC)){
ret = DRBG_NON_INIT;
goto err;
}
break;
}
#endif
default:{
ret = DRBG_ERROR;
goto err;
}
}
ret = DRBG_OK;
err:
return ret;
}
/*
* Use with caution: this macro returns and requires that
* field in context matches function parameter name. On the
* good side, it avoids a lot of code duplication for getters
* defined below.
*/
#define GET_FROM_CTX_FIELD_OR_ERR(ctx, name) \
do { \
if ((drbg_check_instantiated(ctx) != DRBG_OK) || \
(name == NULL)) { \
return DRBG_ILLEGAL_INPUT; \
} \
\
(*(name)) = ctx->name; \
\
return DRBG_OK; \
} while (0)
drbg_error drbg_get_min_entropy_input_length(drbg_ctx *ctx,
uint32_t *min_entropy_input_length)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, min_entropy_input_length);
}
drbg_error drbg_get_max_entropy_input_length(drbg_ctx *ctx,
uint32_t *max_entropy_input_length)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, max_entropy_input_length);
}
drbg_error drbg_get_max_pers_string_length(drbg_ctx *ctx,
uint32_t *max_pers_string_length)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, max_pers_string_length);
}
drbg_error drbg_get_max_addin_length(drbg_ctx *ctx,
uint32_t *max_addin_length)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, max_addin_length);
}
drbg_error drbg_get_drbg_strength(drbg_ctx *ctx,
uint32_t *drbg_strength)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, drbg_strength);
}
drbg_error drbg_get_prediction_resistance(drbg_ctx *ctx,
bool *prediction_resistance)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, prediction_resistance);
}
drbg_error drbg_get_reseed_required_flag(drbg_ctx *ctx,
bool *reseed_required_flag)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, reseed_required_flag);
}
drbg_error drbg_get_reseed_counter(drbg_ctx *ctx,
uint64_t *reseed_counter)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, reseed_counter);
}
drbg_error drbg_get_reseed_interval(drbg_ctx *ctx,
uint64_t *reseed_interval)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, reseed_interval);
}
drbg_error drbg_get_max_asked_length(drbg_ctx *ctx,
uint32_t *max_asked_length)
{
GET_FROM_CTX_FIELD_OR_ERR(ctx, max_asked_length);
}
/**************************************/
/* DRBG instantiate internal.
*/
static drbg_error _drbg_instantiate(drbg_ctx *ctx,
const uint8_t *pers_string, uint32_t pers_string_len,
const uint8_t *entropy_input, uint32_t entropy_input_len,
const uint8_t *nonce, uint32_t nonce_len,
uint32_t *req_inst_sec_strength,
bool prediction_resistance,
drbg_type type,
drbg_options *opt)
{
drbg_error ret = DRBG_ERROR;
const uint8_t *final_entropy_input;
uint32_t final_entropy_input_len;
const uint8_t *final_nonce;
uint32_t final_nonce_len;
uint32_t min_entropy_input_length;
uint8_t *entropy_pool1 = NULL;
uint8_t *entropy_pool2 = NULL;
if(ctx == NULL){
ret = DRBG_ILLEGAL_INPUT;
goto err;
}
/* Depending on the chosen backend and the requested security strength,
* set the callbacks
*/
switch(type){
#ifdef WITH_HASH_DRBG
case DRBG_HASH:{
ctx->methods = (&hash_drbg_methods);
break;
}
#endif
#ifdef WITH_HMAC_DRBG
case DRBG_HMAC:{
ctx->methods = (&hmac_drbg_methods);
break;
}
#endif
#ifdef WITH_CTR_DRBG
case DRBG_CTR:{
ctx->methods = (&ctr_drbg_methods);
break;
}
#endif
default:{
ret = DRBG_ILLEGAL_INPUT;
goto err;
}
}
/* Get the min_entropy_input_length */
if((ret = ctx->methods->get_lengths(opt,
req_inst_sec_strength,
&min_entropy_input_length, NULL, NULL,
NULL, NULL)) != DRBG_OK){
goto err;
}
/* Now that we have our minimum entropy length, go and get some */
if(min_entropy_input_length > (0xffffffff >> 1)){
ret = DRBG_ENTROPY_ERROR;
goto err;
}
/* If we are provided an entropy input, use it! */
if(entropy_input != NULL){
if(entropy_input_len < min_entropy_input_length){
ret = DRBG_ENTROPY_ERROR;
goto err;
}
final_entropy_input = entropy_input;
final_entropy_input_len = entropy_input_len;
}
else{
if(get_entropy_input(&entropy_pool1, min_entropy_input_length,
prediction_resistance)){
ret = DRBG_ENTROPY_ERROR;
goto err;
}
final_entropy_input = (const uint8_t*)entropy_pool1;
final_entropy_input_len = min_entropy_input_length;
}
/* If we are provided a nonce, use it! */
if(nonce != NULL){
final_nonce = nonce;
final_nonce_len = nonce_len;
}
else{
if(get_entropy_input(&entropy_pool2, min_entropy_input_length,
prediction_resistance)){
ret = DRBG_ENTROPY_ERROR;
goto err;
}
final_nonce = (const uint8_t*)entropy_pool2;
final_nonce_len = min_entropy_input_length;
}
/* Now instantiate the drbg */
if((ret = ctx->methods->instantiate(ctx, final_entropy_input, final_entropy_input_len,
final_nonce, final_nonce_len,
pers_string, pers_string_len,
req_inst_sec_strength,
opt)) != DRBG_OK){
goto err;
}
/* Handle the prediciton resistance and the need reseed flags */
ctx->prediction_resistance = prediction_resistance;
ctx->reseed_required_flag = false;
/* Now we are instantiated */
ctx->is_instantiated = true;
/* Set the init magic */
ctx->magic = DRBG_INIT_MAGIC;
ret = DRBG_OK;
err:
if(entropy_pool1 != NULL){
if(clear_entropy_input(entropy_pool1)){
ret = DRBG_ENTROPY_ERROR;
}
}
if(entropy_pool2 != NULL){
if(clear_entropy_input(entropy_pool2)){
ret = DRBG_ENTROPY_ERROR;
}
}
return ret;
}
/* DRBG reseed internal.
*/
static drbg_error _drbg_reseed(drbg_ctx *ctx,
const uint8_t *entropy_input, uint32_t entropy_input_len,
const uint8_t *addin, uint32_t addin_len,
bool prediction_resistance_req)
{
drbg_error ret = DRBG_ERROR;
const uint8_t *final_entropy_input;
uint32_t final_entropy_input_len;
uint32_t min_entropy_input_length;
bool prediction_resistance_flag;
uint8_t *entropy_pool = NULL;
if(drbg_check_instantiated(ctx)){
ret = DRBG_NON_INIT;
goto err;
}
/* Get parameters */
min_entropy_input_length = ctx->min_entropy_input_length;
prediction_resistance_flag = ctx->prediction_resistance;
if((prediction_resistance_req == true) && (prediction_resistance_flag == false)){
ret = DRBG_ILLEGAL_INPUT;
goto err;
}
if(entropy_input != NULL){
if(entropy_input_len < min_entropy_input_length){
ret = DRBG_ENTROPY_ERROR;
goto err;
}
final_entropy_input = entropy_input;
final_entropy_input_len = entropy_input_len;
}
else{
/* Get entropy */
if(get_entropy_input(&entropy_pool, min_entropy_input_length,
prediction_resistance_req)){
ret = DRBG_ENTROPY_ERROR;
goto err;
}
final_entropy_input = (const uint8_t*)entropy_pool;
final_entropy_input_len = min_entropy_input_length;
}
/* Call the underlying backend reseed function */
/* NOTE: size of additional input is cheched by the backend */
if((ret = ctx->methods->reseed(ctx, final_entropy_input, final_entropy_input_len,
addin, addin_len)) != DRBG_OK){
goto err;
}
ret = DRBG_OK;
err:
if(entropy_pool != NULL){
if(clear_entropy_input(entropy_pool)){
ret = DRBG_ENTROPY_ERROR;
}
}
return ret;
}
/* DRBG generate internal.
*/
static drbg_error _drbg_generate(drbg_ctx *ctx,
const uint8_t *addin, uint32_t addin_len,
const uint8_t *reseed_entropy, uint32_t reseed_entropy_len,
uint8_t *out, uint32_t out_len,
bool prediction_resistance_req)
{
drbg_error ret = DRBG_ERROR;
bool prediction_resistance_flag;
bool local_prediction_resistance_req = prediction_resistance_req;
const uint8_t *local_addin = addin;
uint32_t local_addin_len = addin_len;
bool used_reseed_entropy = false;
if(drbg_check_instantiated(ctx)){
ret = DRBG_NON_INIT;
goto err;
}
/* NOTE: the state automaton below is the one depicted in NIST SP 800-90A
* "Generate Process:".
*/
prediction_resistance_flag = ctx->prediction_resistance;
/* 5. If prediction_resistance_req is set, and prediction_resistance_flag is not set, then
return (ERROR_FLAG, Null)*/
if((local_prediction_resistance_req == true) &&
(prediction_resistance_flag == false)){
ret = DRBG_ILLEGAL_INPUT;
goto err;
}
/* 6. Clear the reseed_required_flag */
ctx->reseed_required_flag = false;
step7:
/* 7. If reseed_required_flag is set, or if prediction_resistance_request is set, then */
if((ctx->reseed_required_flag == true) || (local_prediction_resistance_req == true)){
/* If the user provided entropy and we exhausted it, trigger an error ... */
if(used_reseed_entropy == true){
ret = DRBG_ENTROPY_ERROR;
goto err;
}
/* 7.1 Call reseed */
if((ret = _drbg_reseed(ctx,
reseed_entropy, reseed_entropy_len,
local_addin, local_addin_len,
local_prediction_resistance_req)) != DRBG_OK){
goto err;
}
if(reseed_entropy != NULL){
/* Tell that we have used the user provided entropy */
used_reseed_entropy = true;
}
/* 7.4 additional_input = the Null string */
local_addin = NULL;
local_addin_len = 0;
/* 7.5 Clear the reseed_required_flag */
ctx->reseed_required_flag = false;
}
/* 8. Call generate */
if((ret = ctx->methods->generate(ctx, local_addin, local_addin_len, out, out_len)) != DRBG_OK){
/* If status indicates that a reseed is required before the requested bits can be generated,
* then:
*/
if(ret == DRBG_NEED_RESEED){
/* 9.1 Set the reseed_required_flag */
ctx->reseed_required_flag = true;
/* 9.2 If the prediction_resistance_flag is set,
* then set the prediction_resistance request indication
*/
if(prediction_resistance_flag == true){
local_prediction_resistance_req = true;
}
goto step7;
}
else{
goto err;
}
}
ret = DRBG_OK;
err:
return ret;
}
/**************************************/
/* DRBG get lengths */
drbg_error drbg_get_lengths(drbg_options *options,
uint32_t *drbg_strength,
uint32_t *min_entropy_input_length,
uint32_t *max_entropy_input_length,
uint32_t *max_pers_string_length,
uint32_t *max_addin_length,
uint32_t *max_asked_length,
drbg_type type)
{
drbg_error ret = DRBG_ERROR;
switch(type){
#ifdef WITH_HASH_DRBG
case DRBG_HASH:{
ret = hash_drbg_methods.get_lengths(options, drbg_strength,
min_entropy_input_length,
max_entropy_input_length,
max_pers_string_length,
max_addin_length,
max_asked_length);
break;
}
#endif
#ifdef WITH_HMAC_DRBG
case DRBG_HMAC:{
ret = hmac_drbg_methods.get_lengths(options, drbg_strength,
min_entropy_input_length,
max_entropy_input_length,
max_pers_string_length,
max_addin_length,
max_asked_length);
break;
}
#endif
#ifdef WITH_CTR_DRBG
case DRBG_CTR:{
ret = ctr_drbg_methods.get_lengths(options, drbg_strength,
min_entropy_input_length,
max_entropy_input_length,
max_pers_string_length,
max_addin_length,
max_asked_length);
break;
}
#endif
default:{
ret = DRBG_ILLEGAL_INPUT;
goto err;
}
}
err:
return ret;
}
/* DRBG instantiate:
* As described in NIST SP 800-90A in section 9.1, entropy_input and nonce are
* not provided by the consuming application, but instead extracted inside the
* drbg_instantiate function by a call to get_entropy_input.
*/
drbg_error drbg_instantiate(drbg_ctx *ctx,
const uint8_t *pers_string, uint32_t pers_string_len,
uint32_t *req_inst_sec_strength,
bool prediction_resistance,
drbg_type type,
drbg_options *opt)
{
return _drbg_instantiate(ctx,
pers_string, pers_string_len,
NULL, 0,
NULL, 0,
req_inst_sec_strength, prediction_resistance,
type, opt);
}
drbg_error drbg_instantiate_with_user_entropy(drbg_ctx *ctx,
const uint8_t *pers_string, uint32_t pers_string_len,
const uint8_t *entropy_input, uint32_t entropy_input_len,
const uint8_t *nonce, uint32_t nonce_len,
uint32_t *req_inst_sec_strength,
bool prediction_resistance,
drbg_type type,
drbg_options *opt)
{
return _drbg_instantiate(ctx,
pers_string, pers_string_len,
entropy_input, entropy_input_len,
nonce, nonce_len,
req_inst_sec_strength, prediction_resistance,
type, opt);
}
/* DRBG reseed.
*/
drbg_error drbg_reseed(drbg_ctx *ctx,
const uint8_t *addin, uint32_t addin_len,
bool prediction_resistance_req)
{
return _drbg_reseed(ctx,
NULL, 0,
addin, addin_len,
prediction_resistance_req);
}
drbg_error drbg_reseed_with_user_entropy(drbg_ctx *ctx,
const uint8_t *entropy_input, uint32_t entropy_input_len,
const uint8_t *addin, uint32_t addin_len,
bool prediction_resistance_req)
{
return _drbg_reseed(ctx,
entropy_input, entropy_input_len,
addin, addin_len,
prediction_resistance_req);
}
/* DRBG generate.
*/
drbg_error drbg_generate(drbg_ctx *ctx,
const uint8_t *addin, uint32_t addin_len,
uint8_t *out, uint32_t out_len,
bool prediction_resistance_req)
{
return _drbg_generate(ctx,
addin, addin_len,
NULL, 0,
out, out_len,
prediction_resistance_req);
}
drbg_error drbg_generate_with_user_entropy(drbg_ctx *ctx,
const uint8_t *addin, uint32_t addin_len,
const uint8_t *reseed_entropy, uint32_t reseed_entropy_len,
uint8_t *out, uint32_t out_len,
bool prediction_resistance_req)
{
return _drbg_generate(ctx,
addin, addin_len,
reseed_entropy, reseed_entropy_len,
out, out_len,
prediction_resistance_req);
}
/* DRBG uninstantiate */
drbg_error drbg_uninstantiate(drbg_ctx *ctx)
{
drbg_error ret = DRBG_OK;
if(drbg_check_instantiated(ctx)){
/* NOTE: do not return immediately if an error happened,
* empty the other fields first.
*/
ret = ctx->methods->uninstantiate(ctx);
}
ctx->prediction_resistance = false;
ctx->reseed_required_flag = false;
ctx->is_instantiated = false;
ctx->magic = 0;
return ret;
}