Skip to content

Commit c0995c6

Browse files
committed
Use wrapper Zeroizing instead of a drop function for key arrays
1 parent 666164e commit c0995c6

1 file changed

Lines changed: 10 additions & 25 deletions

File tree

  • rustls-wolfcrypt-provider/src/aead

rustls-wolfcrypt-provider/src/aead/quic.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
55
use alloc::vec;
66
use core::mem;
7-
use zeroize::Zeroizing;
87
use foreign_types::ForeignType;
8+
use zeroize::Zeroizing;
99

1010
use crate::error::check_if_zero;
1111
use crate::types::{AesObject, ChaChaObject};
@@ -524,20 +524,14 @@ impl quic::Algorithm for KeyFactory {
524524

525525
pub struct AesCipher {
526526
aes_object: AesObject,
527-
key: Vec<u8>,
528-
}
529-
530-
impl Drop for AesCipher {
531-
fn drop(&mut self) {
532-
self.key.zeroize();
533-
}
527+
key: Zeroizing<Vec<u8>>,
534528
}
535529

536530
impl AesCipher {
537531
pub fn new() -> Result<Self, Error> {
538532
Ok(Self {
539533
aes_object: new_aes_object()?,
540-
key: Vec::new(),
534+
key: Zeroizing::new(Vec::new()),
541535
})
542536
}
543537

@@ -557,7 +551,7 @@ impl AesCipher {
557551
};
558552
check_if_zero(ret)
559553
.map_err(|_| rustls::Error::General("Function AesSetKey failed".into()))?;
560-
self.key = key.to_vec();
554+
self.key = Zeroizing::new(key.to_vec());
561555
Ok(())
562556
}
563557

@@ -665,16 +659,7 @@ impl AesCipher {
665659

666660
pub struct ChaChaCipher {
667661
chacha_cipher: Option<ChaChaObject>,
668-
key: Option<[u8; CHACHA_KEY_LEN]>, // In case of packet protection, no need to initiate a cipher
669-
}
670-
671-
impl Drop for ChaChaCipher {
672-
fn drop(&mut self) {
673-
if let Some(key) = self.key.as_mut() {
674-
key.zeroize();
675-
}
676-
self.key = None;
677-
}
662+
key: Option<Zeroizing<[u8; CHACHA_KEY_LEN]>>, // In case of packet protection, no need to initiate a cipher
678663
}
679664

680665
impl ChaChaCipher {
@@ -686,7 +671,7 @@ impl ChaChaCipher {
686671
}),
687672
Some(key_bytes) => Ok(Self {
688673
chacha_cipher: None,
689-
key: Some(key_bytes),
674+
key: Some(Zeroizing::new(key_bytes)),
690675
}),
691676
}
692677
}
@@ -704,10 +689,10 @@ impl ChaChaCipher {
704689
unsafe { wc_Chacha_SetKey(chacha_cipher.as_ptr(), key.as_ptr(), key.len() as word32) };
705690
check_if_zero(ret)
706691
.map_err(|_| rustls::Error::General("Function wc_Chacha_SetKey failed".into()))?;
707-
self.key = Some(
708-
key.try_into()
709-
.map_err(|_| Error::General("Key must be exactly 32 bytes".into()))?,
710-
);
692+
self.key =
693+
Some(Zeroizing::new(key.try_into().map_err(|_| {
694+
Error::General("Key must be exactly 32 bytes".into())
695+
})?));
711696
Ok(())
712697
}
713698

0 commit comments

Comments
 (0)