@@ -62,70 +62,6 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
62
62
#endif
63
63
}
64
64
65
- /*
66
- * Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
67
- * NIST tests to succeed (which require known length fixed entropy)
68
- */
69
- /* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2)
70
- * mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy,
71
- * custom, len, entropy_len)
72
- * implements
73
- * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
74
- * security_strength) -> initial_working_state
75
- * with inputs
76
- * custom[:len] = nonce || personalization_string
77
- * where entropy_input comes from f_entropy for entropy_len bytes
78
- * and with outputs
79
- * ctx = initial_working_state
80
- */
81
- int mbedtls_ctr_drbg_seed_entropy_len (
82
- mbedtls_ctr_drbg_context * ctx ,
83
- int (* f_entropy )(void * , unsigned char * , size_t ),
84
- void * p_entropy ,
85
- const unsigned char * custom ,
86
- size_t len ,
87
- size_t entropy_len )
88
- {
89
- int ret ;
90
- unsigned char key [MBEDTLS_CTR_DRBG_KEYSIZE ];
91
-
92
- memset ( key , 0 , MBEDTLS_CTR_DRBG_KEYSIZE );
93
-
94
- mbedtls_aes_init ( & ctx -> aes_ctx );
95
-
96
- ctx -> f_entropy = f_entropy ;
97
- ctx -> p_entropy = p_entropy ;
98
-
99
- ctx -> entropy_len = entropy_len ;
100
- ctx -> reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL ;
101
-
102
- /*
103
- * Initialize with an empty key
104
- */
105
- if ( ( ret = mbedtls_aes_setkey_enc ( & ctx -> aes_ctx , key ,
106
- MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
107
- {
108
- return ( ret );
109
- }
110
-
111
- if ( ( ret = mbedtls_ctr_drbg_reseed ( ctx , custom , len ) ) != 0 )
112
- {
113
- return ( ret );
114
- }
115
- return ( 0 );
116
- }
117
-
118
- int mbedtls_ctr_drbg_seed ( mbedtls_ctr_drbg_context * ctx ,
119
- int (* f_entropy )(void * , unsigned char * , size_t ),
120
- void * p_entropy ,
121
- const unsigned char * custom ,
122
- size_t len )
123
- {
124
- return ( mbedtls_ctr_drbg_seed_entropy_len ( ctx , f_entropy , p_entropy ,
125
- custom , len ,
126
- MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
127
- }
128
-
129
65
void mbedtls_ctr_drbg_free ( mbedtls_ctr_drbg_context * ctx )
130
66
{
131
67
if ( ctx == NULL )
@@ -445,6 +381,54 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
445
381
return ( ret );
446
382
}
447
383
384
+ /* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2)
385
+ * mbedtls_ctr_drbg_seed_entropy_len(ctx, f_entropy, p_entropy,
386
+ * custom, len, entropy_len)
387
+ * implements
388
+ * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
389
+ * security_strength) -> initial_working_state
390
+ * with inputs
391
+ * custom[:len] = nonce || personalization_string
392
+ * where entropy_input comes from f_entropy for entropy_len bytes
393
+ * and with outputs
394
+ * ctx = initial_working_state
395
+ */
396
+ int mbedtls_ctr_drbg_seed ( mbedtls_ctr_drbg_context * ctx ,
397
+ int (* f_entropy )(void * , unsigned char * , size_t ),
398
+ void * p_entropy ,
399
+ const unsigned char * custom ,
400
+ size_t len )
401
+ {
402
+ int ret ;
403
+ unsigned char key [MBEDTLS_CTR_DRBG_KEYSIZE ];
404
+
405
+ memset ( key , 0 , MBEDTLS_CTR_DRBG_KEYSIZE );
406
+
407
+ mbedtls_aes_init ( & ctx -> aes_ctx );
408
+
409
+ ctx -> f_entropy = f_entropy ;
410
+ ctx -> p_entropy = p_entropy ;
411
+
412
+ if ( ctx -> entropy_len == 0 )
413
+ ctx -> entropy_len = MBEDTLS_CTR_DRBG_ENTROPY_LEN ;
414
+ ctx -> reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL ;
415
+
416
+ /*
417
+ * Initialize with an empty key
418
+ */
419
+ if ( ( ret = mbedtls_aes_setkey_enc ( & ctx -> aes_ctx , key ,
420
+ MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
421
+ {
422
+ return ( ret );
423
+ }
424
+
425
+ if ( ( ret = mbedtls_ctr_drbg_reseed ( ctx , custom , len ) ) != 0 )
426
+ {
427
+ return ( ret );
428
+ }
429
+ return ( 0 );
430
+ }
431
+
448
432
/* CTR_DRBG_Generate with derivation function (SP 800-90A §10.2.1.5.2)
449
433
* mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, additional, add_len)
450
434
* implements
@@ -708,8 +692,11 @@ int mbedtls_ctr_drbg_self_test( int verbose )
708
692
mbedtls_printf ( " CTR_DRBG (PR = TRUE) : " );
709
693
710
694
test_offset = 0 ;
711
- CHK ( mbedtls_ctr_drbg_seed_entropy_len ( & ctx , ctr_drbg_self_test_entropy ,
712
- (void * ) entropy_source_pr , nonce_pers_pr , 16 , 32 ) );
695
+ mbedtls_ctr_drbg_set_entropy_len ( & ctx , 32 );
696
+ CHK ( mbedtls_ctr_drbg_seed ( & ctx ,
697
+ ctr_drbg_self_test_entropy ,
698
+ (void * ) entropy_source_pr ,
699
+ nonce_pers_pr , 16 ) );
713
700
mbedtls_ctr_drbg_set_prediction_resistance ( & ctx , MBEDTLS_CTR_DRBG_PR_ON );
714
701
CHK ( mbedtls_ctr_drbg_random ( & ctx , buf , MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
715
702
CHK ( mbedtls_ctr_drbg_random ( & ctx , buf , MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
@@ -729,8 +716,11 @@ int mbedtls_ctr_drbg_self_test( int verbose )
729
716
mbedtls_ctr_drbg_init ( & ctx );
730
717
731
718
test_offset = 0 ;
732
- CHK ( mbedtls_ctr_drbg_seed_entropy_len ( & ctx , ctr_drbg_self_test_entropy ,
733
- (void * ) entropy_source_nopr , nonce_pers_nopr , 16 , 32 ) );
719
+ mbedtls_ctr_drbg_set_entropy_len ( & ctx , 32 );
720
+ CHK ( mbedtls_ctr_drbg_seed ( & ctx ,
721
+ ctr_drbg_self_test_entropy ,
722
+ (void * ) entropy_source_nopr ,
723
+ nonce_pers_nopr , 16 ) );
734
724
CHK ( mbedtls_ctr_drbg_random ( & ctx , buf , 16 ) );
735
725
CHK ( mbedtls_ctr_drbg_reseed ( & ctx , NULL , 0 ) );
736
726
CHK ( mbedtls_ctr_drbg_random ( & ctx , buf , 16 ) );
0 commit comments