From 478ac941f54b44f2b9684f1b3dfede62d327db56 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 28 Aug 2022 12:29:44 -0600 Subject: [PATCH] argon2: followups to #247 - Bump version to `0.5.0-pre` (#247 contained breaking changes) - Use pointer casts to convert `Block` integer array to byte array - Rename `permutate!` to `permute!` (former isn't in OED, latter is) --- Cargo.lock | 2 +- argon2/Cargo.toml | 2 +- argon2/src/block.rs | 45 +++++++++++++++++++-------------------------- argon2/src/lib.rs | 2 -- 4 files changed, 21 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e9b7404..cf690bea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "argon2" -version = "0.4.1" +version = "0.5.0-pre" dependencies = [ "base64ct", "blake2", diff --git a/argon2/Cargo.toml b/argon2/Cargo.toml index bb0ea398..ab15fc91 100644 --- a/argon2/Cargo.toml +++ b/argon2/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "argon2" -version = "0.4.1" +version = "0.5.0-pre" description = """ Pure Rust implementation of the Argon2 password hashing function with support for the Argon2d, Argon2i, and Argon2id algorithmic variants diff --git a/argon2/src/block.rs b/argon2/src/block.rs index 59f594db..e874c9b8 100644 --- a/argon2/src/block.rs +++ b/argon2/src/block.rs @@ -11,39 +11,36 @@ use zeroize::Zeroize; const TRUNC: u64 = u32::MAX as u64; -macro_rules! permutate_step { +#[rustfmt::skip] +macro_rules! permute_step { ($a:expr, $b:expr, $c:expr, $d:expr) => { - $a = - (Wrapping($a) + Wrapping($b) + (Wrapping(2) * Wrapping(($a & TRUNC) * ($b & TRUNC)))).0; + $a = (Wrapping($a) + Wrapping($b) + (Wrapping(2) * Wrapping(($a & TRUNC) * ($b & TRUNC)))).0; $d = ($d ^ $a).rotate_right(32); - $c = - (Wrapping($c) + Wrapping($d) + (Wrapping(2) * Wrapping(($c & TRUNC) * ($d & TRUNC)))).0; + $c = (Wrapping($c) + Wrapping($d) + (Wrapping(2) * Wrapping(($c & TRUNC) * ($d & TRUNC)))).0; $b = ($b ^ $c).rotate_right(24); - $a = - (Wrapping($a) + Wrapping($b) + (Wrapping(2) * Wrapping(($a & TRUNC) * ($b & TRUNC)))).0; + $a = (Wrapping($a) + Wrapping($b) + (Wrapping(2) * Wrapping(($a & TRUNC) * ($b & TRUNC)))).0; $d = ($d ^ $a).rotate_right(16); - $c = - (Wrapping($c) + Wrapping($d) + (Wrapping(2) * Wrapping(($c & TRUNC) * ($d & TRUNC)))).0; + $c = (Wrapping($c) + Wrapping($d) + (Wrapping(2) * Wrapping(($c & TRUNC) * ($d & TRUNC)))).0; $b = ($b ^ $c).rotate_right(63); }; } -macro_rules! permutate { +macro_rules! permute { ( $v0:expr, $v1:expr, $v2:expr, $v3:expr, $v4:expr, $v5:expr, $v6:expr, $v7:expr, $v8:expr, $v9:expr, $v10:expr, $v11:expr, $v12:expr, $v13:expr, $v14:expr, $v15:expr, ) => { - permutate_step!($v0, $v4, $v8, $v12); - permutate_step!($v1, $v5, $v9, $v13); - permutate_step!($v2, $v6, $v10, $v14); - permutate_step!($v3, $v7, $v11, $v15); - permutate_step!($v0, $v5, $v10, $v15); - permutate_step!($v1, $v6, $v11, $v12); - permutate_step!($v2, $v7, $v8, $v13); - permutate_step!($v3, $v4, $v9, $v14); + permute_step!($v0, $v4, $v8, $v12); + permute_step!($v1, $v5, $v9, $v13); + permute_step!($v2, $v6, $v10, $v14); + permute_step!($v3, $v7, $v11, $v15); + permute_step!($v0, $v5, $v10, $v15); + permute_step!($v1, $v6, $v11, $v12); + permute_step!($v2, $v7, $v8, $v13); + permute_step!($v3, $v4, $v9, $v14); }; } @@ -57,15 +54,11 @@ impl Block { pub const SIZE: usize = 1024; pub(crate) fn as_bytes(&self) -> &[u8; Self::SIZE] { - let ptr = self.0.as_ptr() as *const u8; - let slice = unsafe { core::slice::from_raw_parts(ptr, Self::SIZE) }; - slice.try_into().unwrap() + unsafe { &*(self.0.as_ptr() as *const [u8; Self::SIZE]) } } pub(crate) fn as_mut_bytes(&mut self) -> &mut [u8; Self::SIZE] { - let ptr = self.0.as_mut_ptr() as *mut u8; - let slice = unsafe { core::slice::from_raw_parts_mut(ptr, Self::SIZE) }; - slice.try_into().unwrap() + unsafe { &mut *(self.0.as_mut_ptr() as *mut [u8; Self::SIZE]) } } pub(crate) fn compress(rhs: &Self, lhs: &Self) -> Self { @@ -75,7 +68,7 @@ impl Block { let mut q = r; for chunk in q.0.chunks_exact_mut(16) { #[rustfmt::skip] - permutate!( + permute!( chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7], chunk[8], chunk[9], chunk[10], chunk[11], @@ -88,7 +81,7 @@ impl Block { let b = i * 2; #[rustfmt::skip] - permutate!( + permute!( q.0[b], q.0[b + 1], q.0[b + 16], q.0[b + 17], q.0[b + 32], q.0[b + 33], diff --git a/argon2/src/lib.rs b/argon2/src/lib.rs index a03dfce8..f79e0c44 100644 --- a/argon2/src/lib.rs +++ b/argon2/src/lib.rs @@ -1,6 +1,4 @@ #![no_std] -// TODO(tarcieri): safe parallel implementation -// See: https://github.com/RustCrypto/password-hashes/issues/154 #![cfg_attr(docsrs, feature(doc_cfg))] #![doc = include_str!("../README.md")] #![doc(