@@ -45,18 +45,19 @@ rng.generate_block(&mut buffer).expect("Failed to generate a block");
4545#![ cfg( random) ]
4646
4747use crate :: sys;
48- use core:: mem:: { size_of_val, MaybeUninit } ;
48+ use core:: mem:: size_of_val;
4949use num_traits:: PrimInt ;
5050
5151/// A cryptographically secure random number generator based on the wolfSSL
5252/// library.
5353///
54- /// This struct wraps the wolfssl `WC_RNG` type, providing a high-level API
55- /// for generating random bytes and blocks of data. The `Drop` implementation
56- /// ensures that the underlying wolfSSL RNG context is correctly freed when the
57- /// `RNG` struct goes out of scope, preventing memory leaks.
54+ /// This struct wraps a pointer to a wolfssl `WC_RNG` allocated on the C heap,
55+ /// providing a high-level API for generating random bytes and blocks of data.
56+ /// The `Drop` implementation ensures that the underlying wolfSSL RNG context is
57+ /// correctly freed when the `RNG` struct goes out of scope, preventing memory
58+ /// leaks.
5859pub struct RNG {
59- pub ( crate ) wc_rng : sys:: WC_RNG ,
60+ pub ( crate ) wc_rng : * mut sys:: WC_RNG ,
6061}
6162
6263impl RNG {
@@ -97,7 +98,7 @@ impl RNG {
9798 return Err ( rc) ;
9899 }
99100 }
100- let mut wc_rng: MaybeUninit < sys:: WC_RNG > = MaybeUninit :: uninit ( ) ;
101+ let mut wc_rng: * mut sys:: WC_RNG = core :: ptr :: null_mut ( ) ;
101102 let heap = match heap {
102103 Some ( heap) => heap,
103104 None => core:: ptr:: null_mut ( ) ,
@@ -107,12 +108,10 @@ impl RNG {
107108 None => sys:: INVALID_DEVID ,
108109 } ;
109110 let rc = unsafe {
110- sys:: wc_InitRng_ex ( wc_rng. as_mut_ptr ( ) , heap, dev_id)
111+ sys:: wc_rng_new_ex ( & mut wc_rng, core :: ptr :: null_mut ( ) , 0 , heap, dev_id)
111112 } ;
112113 if rc == 0 {
113- let wc_rng = unsafe { wc_rng. assume_init ( ) } ;
114- let rng = RNG { wc_rng} ;
115- Ok ( rng)
114+ Ok ( RNG { wc_rng} )
116115 } else {
117116 Err ( rc)
118117 }
@@ -159,7 +158,7 @@ impl RNG {
159158 }
160159 let ptr = nonce. as_mut_ptr ( ) as * mut u8 ;
161160 let size = crate :: buffer_len_to_u32 ( size_of_val ( nonce) ) ?;
162- let mut wc_rng: MaybeUninit < sys:: WC_RNG > = MaybeUninit :: uninit ( ) ;
161+ let mut wc_rng: * mut sys:: WC_RNG = core :: ptr :: null_mut ( ) ;
163162 let heap = match heap {
164163 Some ( heap) => heap,
165164 None => core:: ptr:: null_mut ( ) ,
@@ -169,12 +168,10 @@ impl RNG {
169168 None => sys:: INVALID_DEVID ,
170169 } ;
171170 let rc = unsafe {
172- sys:: wc_InitRngNonce_ex ( wc_rng. as_mut_ptr ( ) , ptr, size, heap, dev_id)
171+ sys:: wc_rng_new_ex ( & mut wc_rng, ptr, size, heap, dev_id)
173172 } ;
174173 if rc == 0 {
175- let wc_rng = unsafe { wc_rng. assume_init ( ) } ;
176- let rng = RNG { wc_rng} ;
177- Ok ( rng)
174+ Ok ( RNG { wc_rng} )
178175 } else {
179176 Err ( rc)
180177 }
@@ -317,7 +314,7 @@ impl RNG {
317314 /// an `Err` with the wolfssl library return code on failure.
318315 pub fn generate_byte ( & mut self ) -> Result < u8 , i32 > {
319316 let mut b: u8 = 0 ;
320- let rc = unsafe { sys:: wc_RNG_GenerateByte ( & mut self . wc_rng , & mut b) } ;
317+ let rc = unsafe { sys:: wc_RNG_GenerateByte ( self . wc_rng , & mut b) } ;
321318 if rc == 0 {
322319 Ok ( b)
323320 } else {
@@ -342,7 +339,7 @@ impl RNG {
342339 pub fn generate_block < T : PrimInt > ( & mut self , buf : & mut [ T ] ) -> Result < ( ) , i32 > {
343340 let ptr = buf. as_mut_ptr ( ) as * mut u8 ;
344341 let size = crate :: buffer_len_to_u32 ( size_of_val ( buf) ) ?;
345- let rc = unsafe { sys:: wc_RNG_GenerateBlock ( & mut self . wc_rng , ptr, size) } ;
342+ let rc = unsafe { sys:: wc_RNG_GenerateBlock ( self . wc_rng , ptr, size) } ;
346343 if rc == 0 {
347344 Ok ( ( ) )
348345 } else {
@@ -374,7 +371,7 @@ impl RNG {
374371 pub fn reseed ( & mut self , seed : & [ u8 ] ) -> Result < ( ) , i32 > {
375372 let seed_size = crate :: buffer_len_to_u32 ( seed. len ( ) ) ?;
376373 let rc = unsafe {
377- sys:: wc_RNG_DRBG_Reseed ( & mut self . wc_rng , seed. as_ptr ( ) , seed_size)
374+ sys:: wc_RNG_DRBG_Reseed ( self . wc_rng , seed. as_ptr ( ) , seed_size)
378375 } ;
379376 if rc != 0 {
380377 return Err ( rc) ;
@@ -411,22 +408,16 @@ impl rand_core::TryRng for RNG {
411408#[ cfg( feature = "rand_core" ) ]
412409impl rand_core:: TryCryptoRng for RNG { }
413410
414- impl RNG {
415- fn zeroize ( & mut self ) {
416- unsafe { crate :: zeroize_raw ( & mut self . wc_rng ) ; }
417- }
418- }
419-
420411impl Drop for RNG {
421412 /// Safely free the underlying wolfSSL RNG context.
422413 ///
423- /// This calls the `wc_FreeRng` wolfssl library function.
414+ /// This calls the `wc_rng_free` wolfssl library function, which frees the
415+ /// C-heap-allocated `WC_RNG` object.
424416 ///
425417 /// The Rust Drop trait guarantees that this method is called when the RNG
426418 /// struct goes out of scope, automatically cleaning up resources and
427419 /// preventing memory leaks.
428420 fn drop ( & mut self ) {
429- unsafe { sys:: wc_FreeRng ( & mut self . wc_rng ) ; }
430- self . zeroize ( ) ;
421+ unsafe { sys:: wc_rng_free ( self . wc_rng ) ; }
431422 }
432423}
0 commit comments