Skip to content

Commit 08c995c

Browse files
authored
Reduce amount of unsafe and use new imports (#351)
1 parent 5c108da commit 08c995c

File tree

47 files changed

+120
-137
lines changed

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
}

0 commit comments

Comments
 (0)