Skip to content

Commit 86afa44

Browse files
committed
Reduce amount of unsafe and use new imports
1 parent 5c108da commit 86afa44

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+120
-137
lines changed

.github/actions/cross-tests/action.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ runs:
2222
override: true
2323
- name: Install precompiled cross
2424
run: |
25-
export URL=$(curl -s https://api.github.com/repos/rust-embedded/cross/releases/latest | \
25+
export URL=$(curl -s https://api.github.com/repos/cross-rs/cross/releases/latest | \
2626
jq -r '.assets[] | select(.name | contains("x86_64-unknown-linux-gnu.tar.gz")) | .browser_download_url')
2727
wget -O /tmp/binaries.tar.gz $URL
2828
tar -C /tmp -xzf /tmp/binaries.tar.gz
2929
mv /tmp/cross ~/.cargo/bin
3030
shell: bash
31-
- run: |
32-
cd ${{ inputs.package }}
33-
cross test --target ${{ inputs.target }} --no-default-features \
34-
--features ${{ inputs.features }}
31+
- run: cross test
32+
--package ${{ inputs.package }}
33+
--target ${{ inputs.target }}
34+
--no-default-features
35+
--features ${{ inputs.features }}
3536
shell: bash

.github/workflows/sha1.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,10 @@ jobs:
137137

138138
# Cross-compiled tests
139139
cross:
140-
needs: set-msrv
141140
strategy:
142141
matrix:
143142
rust:
144-
- ${{needs.set-msrv.outputs.msrv}}
143+
- 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest
145144
- stable
146145
target:
147146
- aarch64-unknown-linux-gnu

.github/workflows/sha2.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,10 @@ jobs:
134134

135135
# Cross-compiled tests
136136
cross:
137-
needs: set-msrv
138137
strategy:
139138
matrix:
140139
rust:
141-
- ${{needs.set-msrv.outputs.msrv}}
140+
- 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest
142141
- stable
143142
target:
144143
- aarch64-unknown-linux-gnu

.github/workflows/sha3.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,10 @@ jobs:
7171

7272
# Cross-compiled tests
7373
cross:
74-
needs: set-msrv
7574
strategy:
7675
matrix:
7776
rust:
78-
- ${{needs.set-msrv.outputs.msrv}}
77+
- 1.51 # 1.41-1.50 `--features` can't be used inside virtual manifest
7978
- stable
8079
target:
8180
- aarch64-unknown-linux-gnu

Cargo.lock

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blake2/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ keywords = ["crypto", "blake2", "hash", "digest"]
1212
categories = ["cryptography", "no-std"]
1313

1414
[dependencies]
15-
digest = { version = "0.10", features = ["mac"] }
15+
digest = { version = "0.10.2", features = ["mac"] }
1616

1717
[dev-dependencies]
18-
digest = { version = "0.10", features = ["dev"] }
18+
digest = { version = "0.10.2", features = ["dev"] }
1919
hex-literal = "0.2"
2020

2121
[features]

blake2/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,8 @@ use digest::{
9494
VariableOutputCore,
9595
},
9696
crypto_common::{InvalidLength, Key, KeyInit, KeySizeUser},
97-
generic_array::{
98-
typenum::{IsLessOrEqual, LeEq, NonZero, Unsigned},
99-
ArrayLength, GenericArray,
100-
},
97+
generic_array::{ArrayLength, GenericArray},
98+
typenum::{IsLessOrEqual, LeEq, NonZero, Unsigned},
10199
FixedOutput, HashMarker, InvalidOutputSize, MacMarker, Output, Update,
102100
};
103101
#[cfg(feature = "reset")]

fsb/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ keywords = ["crypto", "fsb", "hash", "digest"]
1111
categories = ["cryptography", "no-std"]
1212

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

1717
[dev-dependencies]
18-
digest = { version = "0.10", features = ["dev"] }
18+
digest = { version = "0.10.2", features = ["dev"] }
1919
hex-literal = "0.2"
2020

2121
[features]

fsb/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
4444
html_root_url = "https://docs.rs/fsb/0.1.0"
4545
)]
46+
#![forbid(unsafe_code)]
4647
#![warn(missing_docs, rust_2018_idioms)]
4748
#![allow(non_snake_case)]
4849

@@ -61,7 +62,7 @@ use digest::{
6162
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
6263
OutputSizeUser, Reset, UpdateCore,
6364
},
64-
generic_array::{typenum::Unsigned, GenericArray},
65+
typenum::Unsigned,
6566
HashMarker, Output,
6667
};
6768

fsb/src/macros.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! fsb_impl {
3131
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
3232
self.blocks_len += blocks.len() as u64;
3333
for block in blocks {
34-
Self::compress(&mut self.state, Self::convert(block));
34+
Self::compress(&mut self.state, block);
3535
}
3636
}
3737
}
@@ -42,7 +42,7 @@ macro_rules! fsb_impl {
4242
let block_bytes = self.blocks_len * Self::BlockSize::U64;
4343
let bit_len = 8 * (block_bytes + buffer.get_pos() as u64);
4444
let mut h = self.state;
45-
buffer.len64_padding_be(bit_len, |b| Self::compress(&mut h, Self::convert(b)));
45+
buffer.len64_padding_be(bit_len, |b| Self::compress(&mut h, b));
4646

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

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

173167
let w_indices = Self::computing_w_indices(hash, message_block);
@@ -284,10 +278,6 @@ macro_rules! fsb_impl {
284278
}
285279
truncated
286280
}
287-
288-
fn convert(block: &GenericArray<u8, $blocksize>) -> &[u8; Self::SIZE_MSG_CHUNKS] {
289-
unsafe { &*(block.as_ptr() as *const [u8; Self::SIZE_MSG_CHUNKS]) }
290-
}
291281
}
292282
};
293283
}

gost94/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ keywords = ["crypto", "gost94", "gost", "hash", "digest"]
1212
categories = ["cryptography", "no-std"]
1313

1414
[dependencies]
15-
digest = "0.10"
15+
digest = "0.10.2"
1616

1717
[dev-dependencies]
18-
digest = { version = "0.10", features = ["dev"] }
18+
digest = { version = "0.10.2", features = ["dev"] }
1919
hex-literal = "0.2"
2020

2121
[features]

gost94/src/gost94_core.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
use core::{convert::TryInto, fmt};
33
use digest::{
44
block_buffer::Eager,
5-
consts::U32,
65
core_api::{
76
AlgorithmName, Block as TBlock, BlockSizeUser, Buffer, BufferKindUser, FixedOutputCore,
87
OutputSizeUser, Reset, UpdateCore,
98
},
10-
generic_array::{typenum::Unsigned, GenericArray},
9+
typenum::{Unsigned, U32},
1110
HashMarker, Output,
1211
};
1312

@@ -191,8 +190,7 @@ impl<P: Gost94Params> Gost94Core<P> {
191190
}
192191

193192
#[inline(always)]
194-
fn compress(&mut self, block: &GenericArray<u8, U32>) {
195-
let block = unsafe { &*(block.as_ptr() as *const [u8; 32]) };
193+
fn compress(&mut self, block: &[u8; 32]) {
196194
self.f(block);
197195
self.update_sigma(block);
198196
}
@@ -217,7 +215,7 @@ impl<P: Gost94Params> UpdateCore for Gost94Core<P> {
217215
fn update_blocks(&mut self, blocks: &[TBlock<Self>]) {
218216
let len = Self::BlockSize::USIZE * blocks.len();
219217
self.update_n(len);
220-
blocks.iter().for_each(|b| self.compress(b));
218+
blocks.iter().for_each(|b| self.compress(b.as_ref()));
221219
}
222220
}
223221

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

232230
let mut buf = Block::default();

gost94/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
html_root_url = "https://docs.rs/gost94/0.10.0"
3232
)]
3333
#![warn(missing_docs, rust_2018_idioms)]
34+
#![forbid(unsafe_code)]
3435

3536
#[cfg(feature = "std")]
3637
extern crate std;

groestl/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ keywords = ["crypto", "groestl", "grostl", "hash", "digest"]
1212
categories = ["cryptography", "no-std"]
1313

1414
[dependencies]
15-
digest = "0.10"
15+
digest = "0.10.2"
1616

1717
[dev-dependencies]
18-
digest = { version = "0.10", features = ["dev"] }
18+
digest = { version = "0.10.2", features = ["dev"] }
1919
hex-literal = "0.2"
2020

2121
[features]

groestl/src/compress1024.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use core::{convert::TryInto, u64};
44

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

98
#[inline(always)]
109
fn column(x: &[u64; COLS], c: [usize; 8]) -> u64 {
@@ -67,7 +66,7 @@ fn rndp(mut x: [u64; COLS], r: u64) -> [u64; COLS] {
6766
]
6867
}
6968

70-
pub(crate) fn compress(h: &mut [u64; COLS], block: &Block) {
69+
pub(crate) fn compress(h: &mut [u64; COLS], block: &[u8; 128]) {
7170
let mut q = [0u64; COLS];
7271
for (chunk, v) in block.chunks_exact(8).zip(q.iter_mut()) {
7372
*v = u64::from_be_bytes(chunk.try_into().unwrap());

groestl/src/compress512.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![allow(clippy::needless_range_loop)]
22
use crate::table::TABLE;
33
use core::{convert::TryInto, u64};
4-
type Block = super::Block<super::GroestlShortVarCore>;
54

65
pub(crate) const COLS: usize = 8;
76
const ROUNDS: u64 = 10;
@@ -51,7 +50,7 @@ fn rndp(mut x: [u64; COLS], r: u64) -> [u64; COLS] {
5150
]
5251
}
5352

54-
pub(crate) fn compress(h: &mut [u64; COLS], block: &Block) {
53+
pub(crate) fn compress(h: &mut [u64; COLS], block: &[u8; 64]) {
5554
let mut q = [0u64; COLS];
5655
for (chunk, v) in block.chunks_exact(8).zip(q.iter_mut()) {
5756
*v = u64::from_be_bytes(chunk.try_into().unwrap());

groestl/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
3232
html_root_url = "https://docs.rs/groestl/0.10.0"
3333
)]
34-
#![deny(unsafe_code)]
34+
#![forbid(unsafe_code)]
3535
#![warn(rust_2018_idioms)]
3636

3737
pub use digest::{self, Digest};
@@ -44,7 +44,7 @@ use digest::{
4444
CtVariableCoreWrapper, OutputSizeUser, RtVariableCoreWrapper, TruncSide, UpdateCore,
4545
VariableOutputCore,
4646
},
47-
generic_array::typenum::{Unsigned, U128, U28, U32, U48, U64},
47+
typenum::{Unsigned, U128, U28, U32, U48, U64},
4848
HashMarker, InvalidOutputSize, Output,
4949
};
5050

@@ -74,7 +74,7 @@ impl UpdateCore for GroestlShortVarCore {
7474
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
7575
self.blocks_len += blocks.len() as u64;
7676
for block in blocks {
77-
compress512::compress(&mut self.state, block);
77+
compress512::compress(&mut self.state, block.as_ref());
7878
}
7979
}
8080
}
@@ -104,7 +104,9 @@ impl VariableOutputCore for GroestlShortVarCore {
104104
} else {
105105
self.blocks_len + 1
106106
};
107-
buffer.len64_padding_be(blocks_len, |b| compress512::compress(&mut self.state, b));
107+
buffer.len64_padding_be(blocks_len, |block| {
108+
compress512::compress(&mut self.state, block.as_ref())
109+
});
108110
let res = compress512::p(&self.state);
109111
let n = compress512::COLS / 2;
110112
for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) {
@@ -160,7 +162,7 @@ impl UpdateCore for GroestlLongVarCore {
160162
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
161163
self.blocks_len += blocks.len() as u64;
162164
for block in blocks {
163-
compress1024::compress(&mut self.state, block);
165+
compress1024::compress(&mut self.state, block.as_ref());
164166
}
165167
}
166168
}
@@ -190,7 +192,9 @@ impl VariableOutputCore for GroestlLongVarCore {
190192
} else {
191193
self.blocks_len + 1
192194
};
193-
buffer.len64_padding_be(blocks_len, |b| compress1024::compress(&mut self.state, b));
195+
buffer.len64_padding_be(blocks_len, |block| {
196+
compress1024::compress(&mut self.state, block.as_ref())
197+
});
194198
let res = compress1024::p(&self.state);
195199
let n = compress1024::COLS / 2;
196200
for (chunk, v) in out.chunks_exact_mut(8).zip(res[n..].iter()) {

0 commit comments

Comments
 (0)