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

adding zeroize for stream_core.rs (#1494) #1495

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions .github/workflows/async-signature.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
- run: cargo test --all-features --release

minimal-versions:
if: false # Temporarily disabled until signature v2.3.0 is published
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
88 changes: 56 additions & 32 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ members = [
]

[patch.crates-io]
crypto-common = { path = "./crypto-common" }
digest = { path = "./digest" }
signature = { path = "./signature" }
sha2 = { git = "https://github.com/RustCrypto/hashes.git" }
2 changes: 1 addition & 1 deletion aead/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ categories = ["cryptography", "no-std"]
rust-version = "1.65"

[dependencies]
crypto-common = "=0.2.0-pre.4"
crypto-common = "=0.2.0-pre.5"

# optional dependencies
arrayvec = { version = "0.7", optional = true, default-features = false }
Expand Down
4 changes: 2 additions & 2 deletions async-signature/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "async-signature"
description = "Traits for cryptographic signature algorithms (e.g. ECDSA, Ed25519)"
version = "0.6.0-pre.0"
version = "0.6.0-pre.1"
authors = ["RustCrypto Developers"]
license = "Apache-2.0 OR MIT"
documentation = "https://docs.rs/async-signature"
Expand All @@ -13,7 +13,7 @@ edition = "2021"
rust-version = "1.75"

[dependencies]
signature = "=2.3.0-pre.2"
signature = "=2.3.0-pre.3"

[features]
digest = ["signature/digest"]
Expand Down
5 changes: 3 additions & 2 deletions cipher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cipher"
description = "Traits for describing block ciphers and stream ciphers"
version = "0.5.0-pre.2"
version = "0.5.0-pre.3"
authors = ["RustCrypto Developers"]
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand All @@ -13,7 +13,7 @@ keywords = ["crypto", "block-cipher", "stream-cipher", "trait"]
categories = ["cryptography", "no-std"]

[dependencies]
crypto-common = "=0.2.0-pre.4"
crypto-common = "=0.2.0-pre.5"
inout = "=0.2.0-pre.4"

# optional dependencies
Expand All @@ -26,6 +26,7 @@ std = ["alloc", "crypto-common/std", "inout/std"]
block-padding = ["inout/block-padding"]
rand_core = ["crypto-common/rand_core"] # Enable random key and IV generation methods
dev = ["blobby"]
zeroize = ["dep:zeroize", "crypto-common/zeroize"]

[package.metadata.docs.rs]
all-features = true
Expand Down
28 changes: 20 additions & 8 deletions cipher/src/stream_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::{ParBlocks, ParBlocksSizeUser, StreamCipherError};
use crypto_common::{array::Array, typenum::Unsigned, Block, BlockSizeUser, BlockSizes};
use inout::{InOut, InOutBuf};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

/// Trait implemented by stream cipher backends.
pub trait StreamBackend: ParBlocksSizeUser {
/// Generate keystream block.
Expand Down Expand Up @@ -35,7 +38,7 @@ pub trait StreamClosure: BlockSizeUser {
}

/// Block-level synchronous stream ciphers.
pub trait StreamCipherCore: BlockSizeUser + Sized {
pub trait StreamCipherCore: BlockSizeUser + ParBlocksSizeUser + Sized {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I wrote in the issue, this approach is incorrect. StreamCipherCore does not "know" at compile time the number of blocks which can be generated in parallel at runtime. It's known only by backends which can be selected using runtime target feature detection. For example, chacha20 has SSE2 backend which generates only one block and AVX2 which generates 4 blocks. By default on x86 targets the crate uses runtime autodetection to select between those.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach is not ideal, but it would still work if the x86 ParBlocksSize for ChaChaCore was set to U4.

Is there a better approach for choosing the size at runtime? The last time I tried to pick a variable-sized [T; N] at runtime, the compiler was yelling at me because it did not know N at compile time. Is this possible with the array crate, or something similar?

/// Return number of remaining blocks before cipher wraps around.
///
/// Returns `None` if number of remaining blocks can not be computed
Expand Down Expand Up @@ -125,6 +128,10 @@ pub trait StreamCipherCore: BlockSizeUser + Sized {
let t = InOutBuf::from_mut(&mut block);
self.apply_keystream_blocks_inout(t);
buf.get_out().copy_from_slice(&block[..n]);

#[cfg(feature = "zeroize")]
block.zeroize();

Ok(())
}

Expand Down Expand Up @@ -236,6 +243,8 @@ impl<'inp, 'out, BS: BlockSizes> StreamClosure for ApplyBlockCtx<'inp, 'out, BS>
let mut t = Default::default();
backend.gen_ks_block(&mut t);
self.block.xor_in2out(&t);
#[cfg(feature = "zeroize")]
t.zeroize()
}
}

Expand All @@ -253,24 +262,27 @@ impl<'inp, 'out, BS: BlockSizes> StreamClosure for ApplyBlocksCtx<'inp, 'out, BS
fn call<B: StreamBackend<BlockSize = BS>>(self, backend: &mut B) {
if B::ParBlocksSize::USIZE > 1 {
let (chunks, mut tail) = self.blocks.into_chunks::<B::ParBlocksSize>();
let mut buf = Array::<_, B::ParBlocksSize>::default();
for mut chunk in chunks {
let mut tmp = Default::default();
backend.gen_par_ks_blocks(&mut tmp);
chunk.xor_in2out(&tmp);
backend.gen_par_ks_blocks(&mut buf);
chunk.xor_in2out(&buf);
}
let n = tail.len();
let mut buf = Array::<_, B::ParBlocksSize>::default();
let ks = &mut buf[..n];
backend.gen_tail_blocks(ks);
for i in 0..n {
tail.get(i).xor_in2out(&ks[i]);
}
#[cfg(feature = "zeroize")]
buf.zeroize()
} else {
let mut buf = Default::default();
for mut block in self.blocks {
let mut t = Default::default();
backend.gen_ks_block(&mut t);
block.xor_in2out(&t);
backend.gen_ks_block(&mut buf);
block.xor_in2out(&buf);
}
#[cfg(feature = "zeroize")]
buf.zeroize()
}
}
}
Loading
Loading