Skip to content

Commit 442b0b5

Browse files
committed
Add Zeroize and Drop implementations
1 parent d59145d commit 442b0b5

File tree

8 files changed

+101
-1
lines changed

8 files changed

+101
-1
lines changed

cfb-mode/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ categories = ["cryptography", "no-std"]
1212
[dependencies]
1313
stream-cipher = "0.3"
1414
block-cipher-trait = "0.6"
15+
zeroize = { version = "*", optional = true }
1516

1617
[dev-dependencies]
1718
aes = "0.3"

cfb-mode/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,20 @@
5151
pub extern crate stream_cipher;
5252
extern crate block_cipher_trait;
5353

54+
#[cfg(cargo_feature = "zeroize")]
55+
extern crate zeroize;
56+
5457
use stream_cipher::{StreamCipher, NewStreamCipher, InvalidKeyNonceLength};
5558
use block_cipher_trait::BlockCipher;
5659
use block_cipher_trait::generic_array::GenericArray;
5760
use block_cipher_trait::generic_array::typenum::Unsigned;
5861
use core::slice;
5962

63+
#[cfg(cargo_feature = "zeroize")]
64+
use zeroize::Zeroize;
65+
#[cfg(cargo_feature = "zeroize")]
66+
use std::ops::Drop;
67+
6068
/// CFB self-synchronizing stream cipher instance.
6169
pub struct Cfb<C: BlockCipher> {
6270
cipher: C,
@@ -68,6 +76,22 @@ type Block<C> = GenericArray<u8, <C as BlockCipher>::BlockSize>;
6876
type ParBlocks<C> = GenericArray<Block<C>, <C as BlockCipher>::ParBlocks>;
6977
type Key<C> = GenericArray<u8, <C as BlockCipher>::KeySize>;
7078

79+
#[cfg(cargo_feature = "zeroize")]
80+
impl<C: Zeroize> Zeroize for Cfb<C> {
81+
fn zeroize(&mut self) {
82+
self.cipher.zeroize();
83+
self.iv.zeroize();
84+
self.pos.zeroize();
85+
}
86+
}
87+
88+
#[cfg(cargo_feature = "zeroize")]
89+
impl<C> Drop for Cfb<C> {
90+
fn drop(&mut self) {
91+
self.zeroize();
92+
}
93+
}
94+
7195
impl<C: BlockCipher> NewStreamCipher for Cfb<C> {
7296
type KeySize = C::KeySize;
7397
type NonceSize = C::BlockSize;

cfb8/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ categories = ["cryptography", "no-std"]
1212
[dependencies]
1313
stream-cipher = "0.3"
1414
block-cipher-trait = "0.6"
15+
zeroize = { version = "*", optional = true }
1516

1617
[dev-dependencies]
1718
aes = "0.3"

cfb8/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,40 @@
5151
extern crate block_cipher_trait;
5252
pub extern crate stream_cipher;
5353

54+
#[cfg(cargo_feature = "zeroize")]
55+
extern crate zeroize;
56+
5457
use stream_cipher::{NewStreamCipher, StreamCipher, InvalidKeyNonceLength};
5558
use block_cipher_trait::BlockCipher;
5659
use block_cipher_trait::generic_array::GenericArray;
5760
use block_cipher_trait::generic_array::typenum::Unsigned;
5861

62+
#[cfg(cargo_feature = "zeroize")]
63+
use zeroize::Zeroize;
64+
#[cfg(cargo_feature = "zeroize")]
65+
use std::ops::Drop;
66+
5967
/// CFB self-synchronizing stream cipher instance.
6068
pub struct Cfb8<C: BlockCipher> {
6169
cipher: C,
6270
iv: GenericArray<u8, C::BlockSize>,
6371
}
6472

73+
#[cfg(cargo_feature = "zeroize")]
74+
impl<C: Zeroize> Zeroize for Cfb8<C> {
75+
fn zeroize(&mut self) {
76+
self.cipher.zeroize();
77+
self.iv.zeroize();
78+
}
79+
}
80+
81+
#[cfg(cargo_feature = "zeroize")]
82+
impl<C> Drop for Cfb8<C> {
83+
fn drop(&mut self) {
84+
self.zeroize();
85+
}
86+
}
87+
6588
impl<C: BlockCipher> NewStreamCipher for Cfb8<C> {
6689
type KeySize = C::KeySize;
6790
type NonceSize = C::BlockSize;

ctr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ categories = ["cryptography", "no-std"]
1212
[dependencies]
1313
stream-cipher = "0.3"
1414
block-cipher-trait = "0.6"
15+
zeroize = { version = "*", optional = true }
1516

1617
[dev-dependencies]
1718
aes = "0.3"

ctr/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
pub extern crate stream_cipher;
4242
extern crate block_cipher_trait;
4343

44+
#[cfg(cargo_feature = "zeroize")]
45+
extern crate zeroize;
46+
4447
use stream_cipher::{
4548
SyncStreamCipher, SyncStreamCipherSeek, NewStreamCipher,
4649
LoopError, InvalidKeyNonceLength
@@ -51,6 +54,11 @@ use block_cipher_trait::generic_array::typenum::{U16, Unsigned};
5154
use block_cipher_trait::BlockCipher;
5255
use core::{mem, cmp, fmt, ptr};
5356

57+
#[cfg(cargo_feature = "zeroize")]
58+
use zeroize::Zeroize;
59+
#[cfg(cargo_feature = "zeroize")]
60+
use std::ops::Drop;
61+
5462
#[inline(always)]
5563
fn xor(buf: &mut [u8], key: &[u8]) {
5664
debug_assert_eq!(buf.len(), key.len());
@@ -76,6 +84,24 @@ pub struct Ctr128<C>
7684
pos: Option<u8>,
7785
}
7886

87+
#[cfg(cargo_feature = "zeroize")]
88+
impl<C: Zeroize> Zeroize for Ctr128<C> {
89+
fn zeroize(&mut self) {
90+
self.cipher.zeroize();
91+
self.nonce.zeroize();
92+
self.counter.zeroize();
93+
self.block.zeroize();
94+
self.pos.zeroize();
95+
}
96+
}
97+
98+
#[cfg(cargo_feature = "zeroize")]
99+
impl<C> Drop for Ctr128<C> {
100+
fn drop(&mut self) {
101+
self.zeroize();
102+
}
103+
}
104+
79105
impl<C> Ctr128<C>
80106
where
81107
C: BlockCipher<BlockSize = U16>,

ofb/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ categories = ["cryptography", "no-std"]
1212
[dependencies]
1313
stream-cipher = "0.3"
1414
block-cipher-trait = "0.6"
15+
zeroize = { version = "*", optional = true }
1516

1617
[dev-dependencies]
1718
aes = "0.3"

ofb/src/lib.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,21 @@
5353
pub extern crate stream_cipher;
5454
extern crate block_cipher_trait;
5555

56+
#[cfg(cargo_feature = "zeroize")]
57+
extern crate zeroize;
58+
5659
use stream_cipher::{
5760
SyncStreamCipher, NewStreamCipher, LoopError, InvalidKeyNonceLength,
5861
};
5962
use block_cipher_trait::BlockCipher;
6063
use block_cipher_trait::generic_array::GenericArray;
6164
use block_cipher_trait::generic_array::typenum::Unsigned;
6265

66+
#[cfg(cargo_feature = "zeroize")]
67+
use zeroize::Zeroize;
68+
#[cfg(cargo_feature = "zeroize")]
69+
use std::ops::Drop;
70+
6371
type Block<C> = GenericArray<u8, <C as BlockCipher>::BlockSize>;
6472

6573
/// OFB self-synchronizing stream cipher instance.
@@ -69,6 +77,22 @@ pub struct Ofb<C: BlockCipher> {
6977
pos: usize,
7078
}
7179

80+
#[cfg(cargo_feature = "zeroize")]
81+
impl<C: Zeroize> Zeroize for Cfb8<C> {
82+
fn zeroize(&mut self) {
83+
self.cipher.zeroize();
84+
self.block.zeroize();
85+
self.pos.zeroize();
86+
}
87+
}
88+
89+
#[cfg(cargo_feature = "zeroize")]
90+
impl<C> Drop for Cfb8<C> {
91+
fn drop(&mut self) {
92+
self.zeroize();
93+
}
94+
}
95+
7296
impl<C: BlockCipher> NewStreamCipher for Ofb<C> {
7397
type KeySize = C::KeySize;
7498
type NonceSize = C::BlockSize;
@@ -131,4 +155,3 @@ fn xor(buf1: &mut [u8], buf2: &[u8]) {
131155
*a ^= *b;
132156
}
133157
}
134-

0 commit comments

Comments
 (0)