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

Reduce amount of unsafe and use new imports #351

Merged
merged 1 commit into from Feb 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/actions/cross-tests/action.yml
Expand Up @@ -22,14 +22,15 @@ runs:
override: true
- name: Install precompiled cross
run: |
export URL=$(curl -s https://api.github.com/repos/rust-embedded/cross/releases/latest | \
export URL=$(curl -s https://api.github.com/repos/cross-rs/cross/releases/latest | \
jq -r '.assets[] | select(.name | contains("x86_64-unknown-linux-gnu.tar.gz")) | .browser_download_url')
wget -O /tmp/binaries.tar.gz $URL
tar -C /tmp -xzf /tmp/binaries.tar.gz
mv /tmp/cross ~/.cargo/bin
shell: bash
- run: |
cd ${{ inputs.package }}
cross test --target ${{ inputs.target }} --no-default-features \
--features ${{ inputs.features }}
- run: cross test
--package ${{ inputs.package }}
--target ${{ inputs.target }}
--no-default-features
--features ${{ inputs.features }}
shell: bash
3 changes: 1 addition & 2 deletions .github/workflows/sha1.yml
Expand Up @@ -137,11 +137,10 @@ jobs:

# Cross-compiled tests
cross:
needs: set-msrv
strategy:
matrix:
rust:
- ${{needs.set-msrv.outputs.msrv}}
- 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest
Copy link
Contributor

Choose a reason for hiding this comment

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

This was the reason why to change the directory and not to use the package option [1].

[1] 1902274#diff-7989ad9cb1e81934c64205efcfae23eb8bfc6aa827cc5c0f992a9704687a56e8L31

Copy link
Member Author

@newpavlov newpavlov Jan 26, 2022

Choose a reason for hiding this comment

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

Addition of this option was intentional. It is needed in cases when we prototype stuff using [patch.crates-io] in workspace's Cargo.toml, without it cross ignores it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, makes sense 👍. I did not have this in mind.

- stable
target:
- aarch64-unknown-linux-gnu
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/sha2.yml
Expand Up @@ -134,11 +134,10 @@ jobs:

# Cross-compiled tests
cross:
needs: set-msrv
strategy:
matrix:
rust:
- ${{needs.set-msrv.outputs.msrv}}
- 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest
- stable
target:
- aarch64-unknown-linux-gnu
Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/sha3.yml
Expand Up @@ -71,11 +71,10 @@ jobs:

# Cross-compiled tests
cross:
needs: set-msrv
strategy:
matrix:
rust:
- ${{needs.set-msrv.outputs.msrv}}
- 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest
- stable
target:
- aarch64-unknown-linux-gnu
Expand Down
17 changes: 8 additions & 9 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions blake2/Cargo.toml
Expand Up @@ -12,10 +12,10 @@ keywords = ["crypto", "blake2", "hash", "digest"]
categories = ["cryptography", "no-std"]

[dependencies]
digest = { version = "0.10", features = ["mac"] }
digest = { version = "0.10.2", features = ["mac"] }

[dev-dependencies]
digest = { version = "0.10", features = ["dev"] }
digest = { version = "0.10.2", features = ["dev"] }
hex-literal = "0.2"

[features]
Expand Down
6 changes: 2 additions & 4 deletions blake2/src/lib.rs
Expand Up @@ -94,10 +94,8 @@ use digest::{
VariableOutputCore,
},
crypto_common::{InvalidLength, Key, KeyInit, KeySizeUser},
generic_array::{
typenum::{IsLessOrEqual, LeEq, NonZero, Unsigned},
ArrayLength, GenericArray,
},
generic_array::{ArrayLength, GenericArray},
typenum::{IsLessOrEqual, LeEq, NonZero, Unsigned},
FixedOutput, HashMarker, InvalidOutputSize, MacMarker, Output, Update,
};
#[cfg(feature = "reset")]
Expand Down
4 changes: 2 additions & 2 deletions fsb/Cargo.toml
Expand Up @@ -11,11 +11,11 @@ keywords = ["crypto", "fsb", "hash", "digest"]
categories = ["cryptography", "no-std"]

[dependencies]
digest = "0.10"
digest = "0.10.2"
whirlpool = { version = "0.10", path = "../whirlpool", default-features = false }

[dev-dependencies]
digest = { version = "0.10", features = ["dev"] }
digest = { version = "0.10.2", features = ["dev"] }
hex-literal = "0.2"

[features]
Expand Down
3 changes: 2 additions & 1 deletion fsb/src/lib.rs
Expand Up @@ -43,6 +43,7 @@
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_root_url = "https://docs.rs/fsb/0.1.0"
)]
#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]
#![allow(non_snake_case)]

Expand All @@ -61,7 +62,7 @@ use digest::{
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
OutputSizeUser, Reset, UpdateCore,
},
generic_array::{typenum::Unsigned, GenericArray},
typenum::Unsigned,
HashMarker, Output,
};

Expand Down
20 changes: 5 additions & 15 deletions fsb/src/macros.rs
Expand Up @@ -31,7 +31,7 @@ macro_rules! fsb_impl {
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.blocks_len += blocks.len() as u64;
for block in blocks {
Self::compress(&mut self.state, Self::convert(block));
Self::compress(&mut self.state, block);
}
}
}
Expand All @@ -42,7 +42,7 @@ macro_rules! fsb_impl {
let block_bytes = self.blocks_len * Self::BlockSize::U64;
let bit_len = 8 * (block_bytes + buffer.get_pos() as u64);
let mut h = self.state;
buffer.len64_padding_be(bit_len, |b| Self::compress(&mut h, Self::convert(b)));
buffer.len64_padding_be(bit_len, |b| Self::compress(&mut h, b));

let res = whirlpool::Whirlpool::digest(&h[..]);
let n = out.len();
Expand Down Expand Up @@ -111,7 +111,7 @@ macro_rules! fsb_impl {
/// $W_i = i \times (n / w) + IV_i + M_i \times 2^{r / w}.
fn computing_w_indices(
input_vector: &[u8; Self::SIZE_OUTPUT_COMPRESS],
message: &[u8; Self::SIZE_MSG_CHUNKS],
message: &Block<Self>,
) -> [u32; $w] {
let mut wind: [u32; $w] = [0; $w];
let divided_message: [u8; $w] = Self::dividing_bits(message, ($s - $r) / $w);
Expand All @@ -129,10 +129,7 @@ macro_rules! fsb_impl {
/// This function servers the purpose presented in table 3, of breaking a bit array into
/// batches of size not multiple of 8. Note that the IV will be broken always in size 8, which
/// is quite convenient. Also, the only numbers we'll have to worry for are 5 and 6.
fn dividing_bits(
input_bits: &[u8; Self::SIZE_MSG_CHUNKS],
size_batches: usize,
) -> [u8; $w] {
fn dividing_bits(input_bits: &Block<Self>, size_batches: usize) -> [u8; $w] {
if size_batches != 5usize && size_batches != 6usize {
panic!(
"Expecting batches of size 5 or 6. Other values do not follow \
Expand Down Expand Up @@ -164,10 +161,7 @@ macro_rules! fsb_impl {
}

/// This function outputs r bits, which are used to chain to the next iteration.
fn compress(
hash: &mut [u8; Self::SIZE_OUTPUT_COMPRESS],
message_block: &[u8; Self::SIZE_MSG_CHUNKS],
) {
fn compress(hash: &mut [u8; Self::SIZE_OUTPUT_COMPRESS], message_block: &Block<Self>) {
let mut initial_vector = [0u8; Self::SIZE_OUTPUT_COMPRESS];

let w_indices = Self::computing_w_indices(hash, message_block);
Expand Down Expand Up @@ -284,10 +278,6 @@ macro_rules! fsb_impl {
}
truncated
}

fn convert(block: &GenericArray<u8, $blocksize>) -> &[u8; Self::SIZE_MSG_CHUNKS] {
unsafe { &*(block.as_ptr() as *const [u8; Self::SIZE_MSG_CHUNKS]) }
}
}
};
}
4 changes: 2 additions & 2 deletions gost94/Cargo.toml
Expand Up @@ -12,10 +12,10 @@ keywords = ["crypto", "gost94", "gost", "hash", "digest"]
categories = ["cryptography", "no-std"]

[dependencies]
digest = "0.10"
digest = "0.10.2"

[dev-dependencies]
digest = { version = "0.10", features = ["dev"] }
digest = { version = "0.10.2", features = ["dev"] }
hex-literal = "0.2"

[features]
Expand Down
10 changes: 4 additions & 6 deletions gost94/src/gost94_core.rs
Expand Up @@ -2,12 +2,11 @@
use core::{convert::TryInto, fmt};
use digest::{
block_buffer::Eager,
consts::U32,
core_api::{
AlgorithmName, Block as TBlock, BlockSizeUser, Buffer, BufferKindUser, FixedOutputCore,
OutputSizeUser, Reset, UpdateCore,
},
generic_array::{typenum::Unsigned, GenericArray},
typenum::{Unsigned, U32},
HashMarker, Output,
};

Expand Down Expand Up @@ -191,8 +190,7 @@ impl<P: Gost94Params> Gost94Core<P> {
}

#[inline(always)]
fn compress(&mut self, block: &GenericArray<u8, U32>) {
let block = unsafe { &*(block.as_ptr() as *const [u8; 32]) };
fn compress(&mut self, block: &[u8; 32]) {
self.f(block);
self.update_sigma(block);
}
Expand All @@ -217,7 +215,7 @@ impl<P: Gost94Params> UpdateCore for Gost94Core<P> {
fn update_blocks(&mut self, blocks: &[TBlock<Self>]) {
let len = Self::BlockSize::USIZE * blocks.len();
self.update_n(len);
blocks.iter().for_each(|b| self.compress(b));
blocks.iter().for_each(|b| self.compress(b.as_ref()));
}
}

Expand All @@ -226,7 +224,7 @@ impl<P: Gost94Params> FixedOutputCore for Gost94Core<P> {
fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
if buffer.get_pos() != 0 {
self.update_n(buffer.get_pos());
self.compress(buffer.pad_with_zeros());
self.compress(buffer.pad_with_zeros().as_ref());
}

let mut buf = Block::default();
Expand Down
1 change: 1 addition & 0 deletions gost94/src/lib.rs
Expand Up @@ -31,6 +31,7 @@
html_root_url = "https://docs.rs/gost94/0.10.0"
)]
#![warn(missing_docs, rust_2018_idioms)]
#![forbid(unsafe_code)]

#[cfg(feature = "std")]
extern crate std;
Expand Down
4 changes: 2 additions & 2 deletions groestl/Cargo.toml
Expand Up @@ -12,10 +12,10 @@ keywords = ["crypto", "groestl", "grostl", "hash", "digest"]
categories = ["cryptography", "no-std"]

[dependencies]
digest = "0.10"
digest = "0.10.2"

[dev-dependencies]
digest = { version = "0.10", features = ["dev"] }
digest = { version = "0.10.2", features = ["dev"] }
hex-literal = "0.2"

[features]
Expand Down
3 changes: 1 addition & 2 deletions groestl/src/compress1024.rs
Expand Up @@ -4,7 +4,6 @@ use core::{convert::TryInto, u64};

pub(crate) const COLS: usize = 16;
const ROUNDS: u64 = 14;
type Block = super::Block<super::GroestlLongVarCore>;

#[inline(always)]
fn column(x: &[u64; COLS], c: [usize; 8]) -> u64 {
Expand Down Expand Up @@ -67,7 +66,7 @@ fn rndp(mut x: [u64; COLS], r: u64) -> [u64; COLS] {
]
}

pub(crate) fn compress(h: &mut [u64; COLS], block: &Block) {
pub(crate) fn compress(h: &mut [u64; COLS], block: &[u8; 128]) {
let mut q = [0u64; COLS];
for (chunk, v) in block.chunks_exact(8).zip(q.iter_mut()) {
*v = u64::from_be_bytes(chunk.try_into().unwrap());
Expand Down
3 changes: 1 addition & 2 deletions groestl/src/compress512.rs
@@ -1,7 +1,6 @@
#![allow(clippy::needless_range_loop)]
use crate::table::TABLE;
use core::{convert::TryInto, u64};
type Block = super::Block<super::GroestlShortVarCore>;

pub(crate) const COLS: usize = 8;
const ROUNDS: u64 = 10;
Expand Down Expand Up @@ -51,7 +50,7 @@ fn rndp(mut x: [u64; COLS], r: u64) -> [u64; COLS] {
]
}

pub(crate) fn compress(h: &mut [u64; COLS], block: &Block) {
pub(crate) fn compress(h: &mut [u64; COLS], block: &[u8; 64]) {
let mut q = [0u64; COLS];
for (chunk, v) in block.chunks_exact(8).zip(q.iter_mut()) {
*v = u64::from_be_bytes(chunk.try_into().unwrap());
Expand Down
16 changes: 10 additions & 6 deletions groestl/src/lib.rs
Expand Up @@ -31,7 +31,7 @@
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
html_root_url = "https://docs.rs/groestl/0.10.0"
)]
#![deny(unsafe_code)]
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms)]

pub use digest::{self, Digest};
Expand All @@ -44,7 +44,7 @@ use digest::{
CtVariableCoreWrapper, OutputSizeUser, RtVariableCoreWrapper, TruncSide, UpdateCore,
VariableOutputCore,
},
generic_array::typenum::{Unsigned, U128, U28, U32, U48, U64},
typenum::{Unsigned, U128, U28, U32, U48, U64},
HashMarker, InvalidOutputSize, Output,
};

Expand Down Expand Up @@ -74,7 +74,7 @@ impl UpdateCore for GroestlShortVarCore {
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.blocks_len += blocks.len() as u64;
for block in blocks {
compress512::compress(&mut self.state, block);
compress512::compress(&mut self.state, block.as_ref());
}
}
}
Expand Down Expand Up @@ -104,7 +104,9 @@ impl VariableOutputCore for GroestlShortVarCore {
} else {
self.blocks_len + 1
};
buffer.len64_padding_be(blocks_len, |b| compress512::compress(&mut self.state, b));
buffer.len64_padding_be(blocks_len, |block| {
compress512::compress(&mut self.state, block.as_ref())
});
let res = compress512::p(&self.state);
let n = compress512::COLS / 2;
for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) {
Expand Down Expand Up @@ -160,7 +162,7 @@ impl UpdateCore for GroestlLongVarCore {
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
self.blocks_len += blocks.len() as u64;
for block in blocks {
compress1024::compress(&mut self.state, block);
compress1024::compress(&mut self.state, block.as_ref());
}
}
}
Expand Down Expand Up @@ -190,7 +192,9 @@ impl VariableOutputCore for GroestlLongVarCore {
} else {
self.blocks_len + 1
};
buffer.len64_padding_be(blocks_len, |b| compress1024::compress(&mut self.state, b));
buffer.len64_padding_be(blocks_len, |block| {
compress1024::compress(&mut self.state, block.as_ref())
});
let res = compress1024::p(&self.state);
let n = compress1024::COLS / 2;
for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) {
Expand Down