Skip to content

Commit 4d7fb0e

Browse files
authored
Reduce amount of unsafe (#540)
1 parent 2dd1703 commit 4d7fb0e

33 files changed

Lines changed: 65 additions & 127 deletions

File tree

belt-hash/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ pub fn belt_compress(x1: [u32; 4], x2: [u32; 4], x34: [u32; 8]) -> ([u32; 4], [u
158158

159159
#[inline(always)]
160160
fn xor(a: [u32; 4], b: [u32; 4]) -> [u32; 4] {
161-
// TODO: use array zip on stabilization and MSRV bump
162161
[a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]]
163162
}
164163

blake2/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern crate std;
1313

1414
pub use digest::{self, Digest};
1515

16-
use core::{convert::TryInto, fmt, marker::PhantomData, ops::Div};
16+
use core::{fmt, marker::PhantomData, ops::Div};
1717
use digest::{
1818
array::{Array, ArraySize},
1919
block_buffer::{Lazy, LazyBuffer},

gost94/src/gost94_core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(clippy::many_single_char_names)]
2-
use core::{convert::TryInto, fmt};
2+
use core::fmt;
33
use digest::{
44
block_buffer::Eager,
55
core_api::{

groestl/src/compress1024.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(clippy::needless_range_loop)]
22
use crate::table::TABLE;
3-
use core::{convert::TryInto, u64};
43

54
pub(crate) const COLS: usize = 16;
65
const ROUNDS: u64 = 14;

groestl/src/compress512.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(clippy::needless_range_loop)]
22
use crate::table::TABLE;
3-
use core::{convert::TryInto, u64};
43

54
pub(crate) const COLS: usize = 8;
65
const ROUNDS: u64 = 10;

jh/benches/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ macro_rules! mach_bench {
2222
input: *const [u8; 64],
2323
) {
2424
for _ in 0..160 {
25-
jh::f8_impl(m, state, input as *const _);
25+
jh::f8_impl(m, state, input.cast());
2626
}
2727
}
2828
b.iter(|| unsafe { runner(m, &mut state, &input) });

jh/src/compressor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ union X2Bytes<M: Machine> {
9797
#[doc(hidden)]
9898
pub fn f8_impl<M: Machine>(mach: M, state: &mut [vec128_storage; 8], data: *const u8) {
9999
#[allow(clippy::cast_ptr_alignment)]
100-
let data = data as *const M::u128x1;
100+
let data: *const M::u128x1 = data.cast();
101101
let mut y = X8::<M>(
102102
mach.unpack(state[0]),
103103
mach.unpack(state[1]),

md4/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
pub use digest::{self, Digest};
1111

12-
use core::{convert::TryInto, fmt, num::Wrapping as W};
12+
use core::{fmt, num::Wrapping as W};
1313
#[cfg(feature = "oid")]
1414
use digest::const_oid::{AssociatedOid, ObjectIdentifier};
1515
use digest::{

md5/src/compress/soft.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(clippy::many_single_char_names, clippy::unreadable_literal)]
22
use crate::consts::RC;
3-
use core::convert::TryInto;
43

54
#[inline(always)]
65
fn op_f(w: u32, x: u32, y: u32, z: u32, m: u32, c: u32, s: u32) -> u32 {

md5/src/lib.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::{fmt, slice::from_ref};
1515
#[cfg(feature = "oid")]
1616
use digest::const_oid::{AssociatedOid, ObjectIdentifier};
1717
use digest::{
18+
array::ArrayOps,
1819
block_buffer::Eager,
1920
core_api::{
2021
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
@@ -49,7 +50,8 @@ impl UpdateCore for Md5Core {
4950
#[inline]
5051
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
5152
self.block_len = self.block_len.wrapping_add(blocks.len() as u64);
52-
compress::compress(&mut self.state, convert(blocks))
53+
let blocks = ArrayOps::cast_slice_to_core(blocks);
54+
compress::compress(&mut self.state, blocks)
5355
}
5456
}
5557

@@ -62,9 +64,7 @@ impl FixedOutputCore for Md5Core {
6264
.wrapping_add(buffer.get_pos() as u64)
6365
.wrapping_mul(8);
6466
let mut s = self.state;
65-
buffer.len64_padding_le(bit_len, |b| {
66-
compress::compress(&mut s, convert(from_ref(b)))
67-
});
67+
buffer.len64_padding_le(bit_len, |b| compress::compress(&mut s, from_ref(&b.0)));
6868
for (chunk, v) in out.chunks_exact_mut(4).zip(s.iter()) {
6969
chunk.copy_from_slice(&v.to_le_bytes());
7070
}
@@ -108,13 +108,3 @@ impl AssociatedOid for Md5Core {
108108

109109
/// MD5 hasher state.
110110
pub type Md5 = CoreWrapper<Md5Core>;
111-
112-
const BLOCK_SIZE: usize = <Md5Core as BlockSizeUser>::BlockSize::USIZE;
113-
114-
#[inline(always)]
115-
fn convert(blocks: &[Block<Md5Core>]) -> &[[u8; BLOCK_SIZE]] {
116-
// SAFETY: Array<u8, U64> and [u8; 64] have
117-
// exactly the same memory layout
118-
let p = blocks.as_ptr() as *const [u8; BLOCK_SIZE];
119-
unsafe { core::slice::from_raw_parts(p, blocks.len()) }
120-
}

0 commit comments

Comments
 (0)