Skip to content

Commit 1a33c35

Browse files
committed
adding zeroize to everything (this was rly easy...)
1 parent 78292ec commit 1a33c35

11 files changed

Lines changed: 49 additions & 12 deletions

File tree

blake2/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ crypto-mac = "0.8"
1818
digest = "0.9"
1919
opaque-debug = "0.3"
2020
subtle = { version = ">=2, <2.5", default-features = false }
21+
zeroize = { version = "1.1", features = ["zeroize_derive"] }
2122

2223
[dev-dependencies]
2324
crypto-mac = { version = "0.8", features = ["dev"] }

blake2/src/blake2b.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use digest::{
4444
generic_array::GenericArray,
4545
BlockInput, FixedOutputDirty, InvalidOutputSize, Reset, Update, VariableOutputDirty,
4646
};
47+
use zeroize::ZeroizeOnDrop;
4748

4849
pub(crate) type Word = u64;
4950
pub(crate) type Count = u128;
@@ -108,7 +109,7 @@ pub fn blake2b(input: &[u8]) -> Hash {
108109
}
109110

110111
/// Blake2b instance with a fixed output.
111-
#[derive(Clone, Default)]
112+
#[derive(Clone, Default, ZeroizeOnDrop)]
112113
pub struct Blake2b {
113114
params: Params,
114115
state: State,
@@ -192,7 +193,7 @@ opaque_debug::implement!(Blake2b);
192193
digest::impl_write!(Blake2b);
193194

194195
/// Blake2b instance with a variable output.
195-
#[derive(Clone, Default)]
196+
#[derive(Clone, Default, ZeroizeOnDrop)]
196197
pub struct VarBlake2b {
197198
params: Params,
198199
state: State,

blake2/src/blake2b/backend.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod sse41;
99
use super::*;
1010
use arrayref::array_ref;
1111
use core::cmp;
12+
use zeroize::Zeroize;
1213

1314
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1415
pub const MAX_DEGREE: usize = 4;
@@ -138,6 +139,12 @@ impl Implementation {
138139
}
139140
}
140141

142+
impl Zeroize for Implementation {
143+
fn zeroize(&mut self) {
144+
// Nothing to do.
145+
}
146+
}
147+
141148
pub struct Job<'a, 'b> {
142149
pub input: &'a [u8],
143150
pub words: &'b mut [Word; 8],
@@ -181,6 +188,12 @@ pub enum LastNode {
181188
No,
182189
}
183190

191+
impl Zeroize for LastNode {
192+
fn zeroize(&mut self) {
193+
// Nothing to do.
194+
}
195+
}
196+
184197
impl LastNode {
185198
pub fn yes(&self) -> bool {
186199
match self {

blake2/src/blake2b/params.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{
33
};
44
use arrayref::array_refs;
55
use core::fmt;
6+
use zeroize::ZeroizeOnDrop;
67

78
/// A parameter builder that exposes all the non-default BLAKE2 features.
89
///
@@ -28,7 +29,7 @@ use core::fmt;
2829
/// // Or use those params to build an incremental State.
2930
/// let mut state = params.to_state();
3031
/// ```
31-
#[derive(Clone)]
32+
#[derive(Clone, ZeroizeOnDrop)]
3233
pub struct Params {
3334
pub(super) hash_length: u8,
3435
pub(super) key_length: u8,

blake2/src/blake2b/state.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use super::{backend, Count, Hash, Params, Word, BLOCKBYTES, OUTBYTES};
22
use arrayref::mut_array_refs;
33
use core::{cmp, fmt, mem::size_of};
44

5+
use zeroize::ZeroizeOnDrop;
6+
57
/// An incremental hasher for BLAKE2b.
68
///
79
/// To construct a `State` with non-default parameters, see `Params::to_state`.
@@ -19,7 +21,7 @@ use core::{cmp, fmt, mem::size_of};
1921
/// state.update(b"bar");
2022
/// assert_eq!(blake2b(b"foobar"), state.finalize());
2123
/// ```
22-
#[derive(Clone)]
24+
#[derive(Clone, ZeroizeOnDrop)]
2325
pub struct State {
2426
pub(super) words: [Word; 8],
2527
pub(super) count: Count,

blake2/src/blake2bp.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::blake2b::{
2626
many, state, Count, Hash, Word, BLOCKBYTES, KEYBYTES, OUTBYTES,
2727
};
2828
use core::{cmp, fmt, mem::size_of};
29+
use zeroize::ZeroizeOnDrop;
2930

3031
pub(crate) const DEGREE: usize = 4;
3132

@@ -58,7 +59,7 @@ pub fn blake2bp(input: &[u8]) -> Hash {
5859
/// use blake2::blake2bp;
5960
/// let mut state = blake2bp::Params::new().hash_length(32).to_state();
6061
/// ```
61-
#[derive(Clone)]
62+
#[derive(Clone, ZeroizeOnDrop)]
6263
pub struct Params {
6364
hash_length: u8,
6465
key_length: u8,
@@ -206,7 +207,7 @@ impl fmt::Debug for Params {
206207
/// dfa3205f7f7f71e4f0673d25fa82a368488911f446bccd323af3ab03f53e56e5";
207208
/// assert_eq!(expected, &hash.to_hex());
208209
/// ```
209-
#[derive(Clone)]
210+
#[derive(Clone, ZeroizeOnDrop)]
210211
pub struct State {
211212
leaf_words: [[Word; 8]; DEGREE],
212213
root_words: [Word; 8],

blake2/src/blake2s.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use digest::{
4343
generic_array::GenericArray,
4444
BlockInput, FixedOutputDirty, InvalidOutputSize, Reset, Update, VariableOutputDirty,
4545
};
46+
use zeroize::ZeroizeOnDrop;
4647

4748
pub(crate) type Word = u32;
4849
pub(crate) type Count = u64;
@@ -97,7 +98,7 @@ pub fn blake2s(input: &[u8]) -> Hash {
9798
}
9899

99100
/// Blake2s instance with a fixed output.
100-
#[derive(Clone, Default)]
101+
#[derive(Clone, Default, ZeroizeOnDrop)]
101102
pub struct Blake2s {
102103
params: Params,
103104
state: State,
@@ -181,7 +182,7 @@ opaque_debug::implement!(Blake2s);
181182
digest::impl_write!(Blake2s);
182183

183184
/// Blake2s instance with a variable output.
184-
#[derive(Clone, Default)]
185+
#[derive(Clone, Default, ZeroizeOnDrop)]
185186
pub struct VarBlake2s {
186187
params: Params,
187188
state: State,

blake2/src/blake2s/backend.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ mod sse41;
99
use super::*;
1010
use arrayref::array_ref;
1111
use core::cmp;
12+
use digest::generic_array::typenum::Zero;
13+
use zeroize::Zeroize;
1214

1315
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
1416
pub const MAX_DEGREE: usize = 8;
@@ -136,6 +138,12 @@ impl Implementation {
136138
}
137139
}
138140

141+
impl Zeroize for Implementation {
142+
fn zeroize(&mut self) {
143+
// Nothing to do.
144+
}
145+
}
146+
139147
pub struct Job<'a, 'b> {
140148
pub input: &'a [u8],
141149
pub words: &'b mut [Word; 8],
@@ -179,6 +187,12 @@ pub enum LastNode {
179187
No,
180188
}
181189

190+
impl Zeroize for LastNode {
191+
fn zeroize(&mut self) {
192+
// Nothing to do.
193+
}
194+
}
195+
182196
impl LastNode {
183197
pub fn yes(&self) -> bool {
184198
match self {

blake2/src/blake2s/params.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{
33
};
44
use arrayref::array_refs;
55
use core::fmt;
6+
use zeroize::ZeroizeOnDrop;
67

78
/// A parameter builder that exposes all the non-default BLAKE2 features.
89
///
@@ -28,7 +29,7 @@ use core::fmt;
2829
/// // Or use those params to build an incremental State.
2930
/// let mut state = params.to_state();
3031
/// ```
31-
#[derive(Clone)]
32+
#[derive(Clone, ZeroizeOnDrop)]
3233
pub struct Params {
3334
pub(super) hash_length: u8,
3435
pub(super) key_length: u8,

blake2/src/blake2s/state.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{backend, Count, Hash, Params, Word, BLOCKBYTES, OUTBYTES};
22
use arrayref::mut_array_refs;
33
use core::{cmp, fmt, mem::size_of};
4+
use zeroize::ZeroizeOnDrop;
45

56
/// An incremental hasher for BLAKE2s.
67
///
@@ -19,7 +20,7 @@ use core::{cmp, fmt, mem::size_of};
1920
/// state.update(b"bar");
2021
/// assert_eq!(blake2s(b"foobar"), state.finalize());
2122
/// ```
22-
#[derive(Clone)]
23+
#[derive(Clone, ZeroizeOnDrop)]
2324
pub struct State {
2425
pub(super) words: [Word; 8],
2526
pub(super) count: Count,

0 commit comments

Comments
 (0)