@@ -337,29 +337,53 @@ static int wp_drbg_reseed(wp_DrbgCtx* ctx, int predResist,
337337 const unsigned char * addIn , size_t addInLen )
338338{
339339 int ok = 1 ;
340+ int rc ;
341+ unsigned char * seed = NULL ;
342+ size_t seedLen = 0 ;
340343
341344 WOLFPROV_ENTER (WP_LOG_COMP_RNG , "wp_drbg_reseed" );
342345
343- #if 0
344- /* Calling Hash_DRBG_Instantiate would be better. */
345- int rc ;
346- rc = wc_RNG_DRBG_Reseed (ctx -> rng , entropy , entropyLen );
347- if (rc != 0 ) {
348- ok = 0 ;
346+ /* If no entropy provided, get fresh entropy from the OS source. */
347+ if (entropy == NULL || entropyLen == 0 ) {
348+ seedLen = 48 ;
349+ seed = OPENSSL_malloc (seedLen );
350+ if (seed == NULL ) {
351+ ok = 0 ;
352+ }
353+ if (ok ) {
354+ OS_Seed osSeed ;
355+ rc = wc_GenerateSeed (& osSeed , seed , (word32 )seedLen );
356+ if (rc != 0 ) {
357+ ok = 0 ;
358+ }
359+ else {
360+ entropy = seed ;
361+ entropyLen = seedLen ;
362+ }
363+ }
349364 }
350- if (ok && (addInLen > 0 )) {
351- rc = wc_RNG_DRBG_Reseed (ctx -> rng , addIn , addInLen );
365+
366+ if (ok && entropy != NULL && entropyLen > 0 ) {
367+ rc = wc_RNG_DRBG_Reseed (ctx -> rng , entropy , (word32 )entropyLen );
352368 if (rc != 0 ) {
369+ WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_COMP_RNG ,
370+ "wc_RNG_DRBG_Reseed" , rc );
371+ ok = 0 ;
372+ }
373+ }
374+ if (ok && (addInLen > 0 ) && (addIn != NULL )) {
375+ rc = wc_RNG_DRBG_Reseed (ctx -> rng , addIn , (word32 )addInLen );
376+ if (rc != 0 ) {
377+ WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_COMP_RNG ,
378+ "wc_RNG_DRBG_Reseed" , rc );
353379 ok = 0 ;
354380 }
355381 }
356- #else
357- (void )ctx ;
358- (void )entropy ;
359- (void )entropyLen ;
360- (void )addIn ;
361- (void )addInLen ;
362- #endif
382+
383+ /* Securely clear and free locally allocated seed buffer. */
384+ if (seed != NULL ) {
385+ OPENSSL_clear_free (seed , seedLen );
386+ }
363387
364388 (void )predResist ;
365389
@@ -391,6 +415,7 @@ static int wp_drbg_enable_locking(wp_DrbgCtx* ctx)
391415 if (rc != 0 ) {
392416 WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_COMP_RNG , "wc_InitMutex" , rc );
393417 OPENSSL_free (ctx -> mutex );
418+ ctx -> mutex = NULL ;
394419 ok = 0 ;
395420 }
396421 }
@@ -550,11 +575,16 @@ static int wp_drbg_set_ctx_params(wp_DrbgCtx* ctx, const OSSL_PARAM params[])
550575 */
551576static int wp_drbg_verify_zeroization (wp_DrbgCtx * ctx )
552577{
578+ int ok ;
579+
553580 WOLFPROV_ENTER (WP_LOG_COMP_RNG , "wp_drbg_verify_zeroization" );
554581
555- (void )ctx ;
556- WOLFPROV_LEAVE (WP_LOG_COMP_RNG , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), 1 );
557- return 1 ;
582+ /* After uninstantiate, ctx->rng is freed (with internal state zeroized
583+ * by wolfSSL) and set to NULL. Verify that cleanup occurred. */
584+ ok = (ctx -> rng == NULL );
585+
586+ WOLFPROV_LEAVE (WP_LOG_COMP_RNG , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), ok );
587+ return ok ;
558588}
559589
560590/**
0 commit comments