@@ -20,7 +20,7 @@ use serde::{Deserialize, Serialize};
2020use std:: io:: Write ;
2121use zeroize:: { Zeroize , ZeroizeOnDrop } ;
2222
23- #[ derive( Clone , Serialize , Deserialize ) ]
23+ #[ derive( Clone , Serialize , Deserialize , Zeroize , ZeroizeOnDrop ) ]
2424pub struct SecretKey ( Scalar ) ;
2525
2626impl SecretKey {
@@ -44,20 +44,6 @@ impl Default for SecretKey {
4444 }
4545}
4646
47- impl Zeroize for SecretKey {
48- fn zeroize ( & mut self ) {
49- // Manually zero the Scalar's inner U256
50- // Use volatile write to prevent compiler optimization
51- unsafe {
52- let ptr = & mut self . 0 as * mut Scalar as * mut [ u8 ; 32 ] ;
53- core:: ptr:: write_volatile ( ptr, [ 0u8 ; 32 ] ) ;
54- }
55- }
56- }
57-
58- // Mark as ZeroizeOnDrop - auto-generates Drop impl that calls zeroize()
59- impl ZeroizeOnDrop for SecretKey { }
60-
6147#[ cfg( feature = "nif" ) ]
6248fn do_encode < ' a > ( secret_key : & SecretKey , env : Env < ' a > ) -> Result < Term < ' a > , Error > {
6349 let bytes = bincode:: serialize ( & secret_key. 0 ) . unwrap ( ) ;
@@ -240,12 +226,9 @@ impl InnerSecretKey {
240226 }
241227}
242228
243- /// Wrapper for plaintext that automatically zeros on drop
229+ # [ derive ( Zeroize , ZeroizeOnDrop ) ]
244230pub struct SecurePlaintext ( Vec < u8 > ) ;
245231
246- // Vec<u8> already implements Zeroize, so we can implement ZeroizeOnDrop
247- impl ZeroizeOnDrop for SecurePlaintext { }
248-
249232impl SecurePlaintext {
250233 pub fn new ( data : Vec < u8 > ) -> Self {
251234 SecurePlaintext ( data)
0 commit comments