@@ -62,68 +62,6 @@ void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
6262#endif
6363}
6464
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 , MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
106- {
107- return ( ret );
108- }
109-
110- if ( ( ret = mbedtls_ctr_drbg_reseed ( ctx , custom , len ) ) != 0 )
111- {
112- return ( ret );
113- }
114- return ( 0 );
115- }
116-
117- int mbedtls_ctr_drbg_seed ( mbedtls_ctr_drbg_context * ctx ,
118- int (* f_entropy )(void * , unsigned char * , size_t ),
119- void * p_entropy ,
120- const unsigned char * custom ,
121- size_t len )
122- {
123- return ( mbedtls_ctr_drbg_seed_entropy_len ( ctx , f_entropy , p_entropy , custom , len ,
124- MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
125- }
126-
12765void mbedtls_ctr_drbg_free ( mbedtls_ctr_drbg_context * ctx )
12866{
12967 if ( ctx == NULL )
@@ -427,6 +365,63 @@ int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
427365 return ( ret );
428366}
429367
368+ /* CTR_DRBG_Instantiate with derivation function (SP 800-90A §10.2.1.3.2)
369+ * mbedtls_ctr_drbg_seed(ctx, f_entropy, p_entropy, custom, len)
370+ * implements
371+ * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
372+ * security_strength) -> initial_working_state
373+ * with inputs
374+ * custom[:len] = nonce || personalization_string
375+ * where entropy_input comes from f_entropy for ctx->entropy_len bytes
376+ * and with outputs
377+ * ctx = initial_working_state
378+ */
379+ int mbedtls_ctr_drbg_seed ( mbedtls_ctr_drbg_context * ctx ,
380+ int (* f_entropy )(void * , unsigned char * , size_t ),
381+ void * p_entropy ,
382+ const unsigned char * custom ,
383+ size_t len )
384+ {
385+ int ret ;
386+ unsigned char key [MBEDTLS_CTR_DRBG_KEYSIZE ];
387+
388+ memset ( key , 0 , MBEDTLS_CTR_DRBG_KEYSIZE );
389+
390+ mbedtls_aes_init ( & ctx -> aes_ctx );
391+
392+ ctx -> f_entropy = f_entropy ;
393+ ctx -> p_entropy = p_entropy ;
394+
395+ if ( ctx -> entropy_len == 0 )
396+ ctx -> entropy_len = MBEDTLS_CTR_DRBG_ENTROPY_LEN ;
397+ ctx -> reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL ;
398+
399+ /*
400+ * Initialize with an empty key
401+ */
402+ if ( ( ret = mbedtls_aes_setkey_enc ( & ctx -> aes_ctx , key , MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
403+ {
404+ return ( ret );
405+ }
406+
407+ if ( ( ret = mbedtls_ctr_drbg_reseed ( ctx , custom , len ) ) != 0 )
408+ {
409+ return ( ret );
410+ }
411+ return ( 0 );
412+ }
413+
414+ /* Backward compatibility wrapper */
415+ int mbedtls_ctr_drbg_seed_entropy_len (
416+ mbedtls_ctr_drbg_context * ctx ,
417+ int (* f_entropy )(void * , unsigned char * , size_t ), void * p_entropy ,
418+ const unsigned char * custom , size_t len ,
419+ size_t entropy_len )
420+ {
421+ mbedtls_ctr_drbg_set_entropy_len ( ctx , entropy_len );
422+ return ( mbedtls_ctr_drbg_seed ( ctx , f_entropy , p_entropy , custom , len ) );
423+ }
424+
430425/* CTR_DRBG_Generate with derivation function (SP 800-90A §10.2.1.5.2)
431426 * mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, additional, add_len)
432427 * implements
@@ -678,8 +673,11 @@ int mbedtls_ctr_drbg_self_test( int verbose )
678673 mbedtls_printf ( " CTR_DRBG (PR = TRUE) : " );
679674
680675 test_offset = 0 ;
681- CHK ( mbedtls_ctr_drbg_seed_entropy_len ( & ctx , ctr_drbg_self_test_entropy ,
682- (void * ) entropy_source_pr , nonce_pers_pr , 16 , 32 ) );
676+ mbedtls_ctr_drbg_set_entropy_len ( & ctx , 32 );
677+ CHK ( mbedtls_ctr_drbg_seed ( & ctx ,
678+ ctr_drbg_self_test_entropy ,
679+ (void * ) entropy_source_pr ,
680+ nonce_pers_pr , 16 ) );
683681 mbedtls_ctr_drbg_set_prediction_resistance ( & ctx , MBEDTLS_CTR_DRBG_PR_ON );
684682 CHK ( mbedtls_ctr_drbg_random ( & ctx , buf , MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
685683 CHK ( mbedtls_ctr_drbg_random ( & ctx , buf , MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
@@ -699,8 +697,11 @@ int mbedtls_ctr_drbg_self_test( int verbose )
699697 mbedtls_ctr_drbg_init ( & ctx );
700698
701699 test_offset = 0 ;
702- CHK ( mbedtls_ctr_drbg_seed_entropy_len ( & ctx , ctr_drbg_self_test_entropy ,
703- (void * ) entropy_source_nopr , nonce_pers_nopr , 16 , 32 ) );
700+ mbedtls_ctr_drbg_set_entropy_len ( & ctx , 32 );
701+ CHK ( mbedtls_ctr_drbg_seed ( & ctx ,
702+ ctr_drbg_self_test_entropy ,
703+ (void * ) entropy_source_nopr ,
704+ nonce_pers_nopr , 16 ) );
704705 CHK ( mbedtls_ctr_drbg_random ( & ctx , buf , 16 ) );
705706 CHK ( mbedtls_ctr_drbg_reseed ( & ctx , NULL , 0 ) );
706707 CHK ( mbedtls_ctr_drbg_random ( & ctx , buf , 16 ) );
0 commit comments