Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh-cipher: add zeroize feature #283

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions ssh-cipher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ chacha20 = { version = "=0.10.0-pre.1", optional = true, default-features = fals
des = { version = "=0.9.0-pre.1", optional = true, default-features = false }
poly1305 = { version = "0.9.0-rc.0", optional = true, default-features = false }
subtle = { version = "2", optional = true, default-features = false }
zeroize = { version = "1", optional = true, default-features = false }

[dev-dependencies]
hex-literal = "0.4"
Expand All @@ -44,6 +45,14 @@ aes-ctr = ["dep:aes", "dep:ctr"]
aes-gcm = ["dep:aead", "dep:aes", "dep:aes-gcm"]
chacha20poly1305 = ["dep:aead", "dep:chacha20", "dep:poly1305", "dep:subtle"]
tdes = ["dep:des", "dep:cbc"]
zeroize = [
"dep:zeroize",
"aes?/zeroize",
"aes-gcm?/zeroize",
"chacha20?/zeroize",
"des?/zeroize",
"poly1305?/zeroize"
]

[package.metadata.docs.rs]
all-features = true
Expand Down
14 changes: 13 additions & 1 deletion ssh-cipher/src/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use cipher::{KeyIvInit, StreamCipher, StreamCipherSeek};
use poly1305::Poly1305;
use subtle::ConstantTimeEq;

#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};

/// Key for `chacha20-poly1305@openssh.com`.
pub type ChaChaKey = chacha20::Key;

Expand All @@ -29,7 +32,6 @@ pub type ChaChaNonce = chacha20::LegacyNonce;
/// [RFC8439]: https://datatracker.ietf.org/doc/html/rfc8439
#[derive(Clone)]
pub struct ChaCha20Poly1305 {
// TODO(tarcieri): zeroize on drop
key: ChaChaKey,
}

Expand Down Expand Up @@ -93,6 +95,16 @@ impl ChaCha20Poly1305 {
}
}

impl Drop for ChaCha20Poly1305 {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
self.key.zeroize();
}
}

#[cfg(feature = "zeroize")]
impl ZeroizeOnDrop for ChaCha20Poly1305 {}

/// Internal type representing a cipher instance.
struct Cipher {
cipher: ChaCha20,
Expand Down
2 changes: 1 addition & 1 deletion ssh-key/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ edition = "2021"
rust-version = "1.73"

[dependencies]
cipher = { package = "ssh-cipher", version = "=0.3.0-pre.2", path = "../ssh-cipher" }
cipher = { package = "ssh-cipher", version = "=0.3.0-pre.2", features = ["zeroize"], path = "../ssh-cipher" }
encoding = { package = "ssh-encoding", version = "=0.3.0-pre.1", features = ["base64", "digest", "pem"], path = "../ssh-encoding" }
sha2 = { version = "=0.11.0-pre.4", default-features = false }
signature = { version = "=2.3.0-pre.4", default-features = false }
Expand Down