Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions blake2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ default = ["alloc"]
alloc = ["digest/alloc"]
zeroize = ["digest/zeroize"]
reset = [] # Enable reset functionality
#simd = []
#simd_opt = ["simd"]
#simd_asm = ["simd_opt"]
size_opt = [] # Optimize for code size. Removes some `inline(always)`

[package.metadata.docs.rs]
Expand Down
32 changes: 0 additions & 32 deletions blake2/src/as_bytes.rs

This file was deleted.

18 changes: 0 additions & 18 deletions blake2/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(clippy::unreadable_literal)]

pub(crate) static SIGMA: [[usize; 16]; 12] = [
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
[14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],
Expand All @@ -26,22 +24,6 @@ pub(crate) static BLAKE2B_IV: [u64; 8] = [
0x5be0cd19137e2179,
];

/*
pub const BLAKE2B_BLOCKBYTES : usize = 128;
pub const BLAKE2B_OUTBYTES : usize = 64;
pub const BLAKE2B_KEYBYTES : usize = 64;
pub const BLAKE2B_SALTBYTES : usize = 16;
pub const BLAKE2B_PERSONALBYTES : usize = 16;
*/

pub(crate) static BLAKE2S_IV: [u32; 8] = [
0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
];

/*
pub const BLAKE2S_BLOCKBYTES : usize = 64;
pub const BLAKE2S_OUTBYTES : usize = 32;
pub const BLAKE2S_KEYBYTES : usize = 32;
pub const BLAKE2S_SALTBYTES : usize = 8;
pub const BLAKE2S_PERSONALBYTES : usize = 8;
*/
8 changes: 1 addition & 7 deletions blake2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(unexpected_cfgs)] // `simd` feature is broken
#![warn(missing_docs, unreachable_pub)]
#![cfg_attr(feature = "simd", feature(platform_intrinsics, repr_simd))]
#![cfg_attr(feature = "simd", allow(incomplete_features))]

pub use digest::{self, Digest};

Expand All @@ -31,17 +27,15 @@ use digest::{FixedOutputReset, Reset};
#[cfg(feature = "zeroize")]
use digest::zeroize::{Zeroize, ZeroizeOnDrop};

mod as_bytes;
mod consts;

mod simd;

#[macro_use]
mod macros;

use as_bytes::AsBytes;
use consts::{BLAKE2B_IV, BLAKE2S_IV};
use simd::{Vector4, u32x4, u64x4};
use simd::{u32x4, u64x4};

blake2_impl!(
Blake2bVarCore,
Expand Down
5 changes: 3 additions & 2 deletions blake2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ macro_rules! blake2_impl {
out: &mut Output<Self>,
) {
self.compress(final_block, !0, flag);
let buf = [self.h[0].to_le(), self.h[1].to_le()];
out.copy_from_slice(buf.as_bytes())
let n = size_of::<$vec>();
out[..n].copy_from_slice(self.h[0].to_le().as_bytes());
out[n..].copy_from_slice(self.h[1].to_le().as_bytes());
}

fn compress(&mut self, block: &Block<Self>, f0: $word, f1: $word) {
Expand Down
196 changes: 121 additions & 75 deletions blake2/src/simd.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,46 @@
// Copyright 2015 blake2-rfc Developers
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

mod simd_opt;
mod simdint;
mod simdop;
mod simdty;

pub(crate) use self::simdty::{u32x4, u64x4};

pub(crate) trait Vector4<T>: Copy {
fn gather(src: &[T], i0: usize, i1: usize, i2: usize, i3: usize) -> Self;

#[allow(clippy::wrong_self_convention)]
fn from_le(self) -> Self;
fn to_le(self) -> Self;

fn wrapping_add(self, rhs: Self) -> Self;

fn rotate_right_const(self, n: u32) -> Self;

fn shuffle_left_1(self) -> Self;
fn shuffle_left_2(self) -> Self;
fn shuffle_left_3(self) -> Self;

#[inline(always)]
fn shuffle_right_1(self) -> Self {
self.shuffle_left_3()
}
#[inline(always)]
fn shuffle_right_2(self) -> Self {
self.shuffle_left_2()
}
#[inline(always)]
fn shuffle_right_3(self) -> Self {
self.shuffle_left_1()
}
}
use core::ops::{Add, BitXor, Shl, Shr};

#[cfg(feature = "zeroize")]
use digest::zeroize::Zeroize;

macro_rules! impl_vector4 {
($vec:ident, $word:ident) => {
impl Vector4<$word> for $vec {
#[derive(Clone, Copy, Debug)]
#[repr(C)]
pub(crate) struct $vec(
pub(crate) $word,
pub(crate) $word,
pub(crate) $word,
pub(crate) $word,
);

impl $vec {
#[inline(always)]
pub(crate) fn new(e0: $word, e1: $word, e2: $word, e3: $word) -> Self {
Self(e0, e1, e2, e3)
}

#[inline(always)]
fn gather(src: &[$word], i0: usize, i1: usize, i2: usize, i3: usize) -> Self {
pub(crate) fn gather(
src: &[$word],
i0: usize,
i1: usize,
i2: usize,
i3: usize,
) -> Self {
$vec::new(src[i0], src[i1], src[i2], src[i3])
}

#[cfg(target_endian = "little")]
#[inline(always)]
fn from_le(self) -> Self {
#[allow(clippy::wrong_self_convention)]
pub(crate) fn from_le(self) -> Self {
self
}

#[cfg(not(target_endian = "little"))]
#[inline(always)]
fn from_le(self) -> Self {
pub(crate) fn from_le(self) -> Self {
$vec::new(
$word::from_le(self.0),
$word::from_le(self.1),
Expand All @@ -68,13 +51,13 @@ macro_rules! impl_vector4 {

#[cfg(target_endian = "little")]
#[inline(always)]
fn to_le(self) -> Self {
pub(crate) fn to_le(self) -> Self {
self
}

#[cfg(not(target_endian = "little"))]
#[inline(always)]
fn to_le(self) -> Self {
pub(crate) fn to_le(self) -> Self {
$vec::new(
self.0.to_le(),
self.1.to_le(),
Expand All @@ -84,55 +67,118 @@ macro_rules! impl_vector4 {
}

#[inline(always)]
fn wrapping_add(self, rhs: Self) -> Self {
pub(crate) fn wrapping_add(self, rhs: Self) -> Self {
self + rhs
}

#[inline(always)]
fn rotate_right_const(self, n: u32) -> Self {
simd_opt::$vec::rotate_right_const(self, n)
pub(crate) fn rotate_right_const(self, n: u32) -> Self {
$vec::new(
self.0.rotate_right(n),
self.1.rotate_right(n),
self.2.rotate_right(n),
self.3.rotate_right(n),
)
}

#[cfg(feature = "simd")]
#[inline(always)]
fn shuffle_left_1(self) -> Self {
use crate::simd::simdint::simd_shuffle4;
const IDX: [u32; 4] = [1, 2, 3, 0];
unsafe { simd_shuffle4(self, self, IDX) }
pub(crate) fn shuffle_left_1(self) -> Self {
$vec::new(self.1, self.2, self.3, self.0)
}

#[cfg(not(feature = "simd"))]
#[inline(always)]
fn shuffle_left_1(self) -> Self {
$vec::new(self.1, self.2, self.3, self.0)
pub(crate) fn shuffle_left_2(self) -> Self {
$vec::new(self.2, self.3, self.0, self.1)
}

#[cfg(feature = "simd")]
#[inline(always)]
fn shuffle_left_2(self) -> Self {
use crate::simd::simdint::simd_shuffle4;
const IDX: [u32; 4] = [2, 3, 0, 1];
unsafe { simd_shuffle4(self, self, IDX) }
pub(crate) fn shuffle_left_3(self) -> Self {
$vec::new(self.3, self.0, self.1, self.2)
}

#[cfg(not(feature = "simd"))]
#[inline(always)]
fn shuffle_left_2(self) -> Self {
$vec::new(self.2, self.3, self.0, self.1)
pub(crate) fn shuffle_right_1(self) -> Self {
self.shuffle_left_3()
}
#[inline(always)]
pub(crate) fn shuffle_right_2(self) -> Self {
self.shuffle_left_2()
}
#[inline(always)]
pub(crate) fn shuffle_right_3(self) -> Self {
self.shuffle_left_1()
}

#[cfg(feature = "simd")]
#[inline(always)]
fn shuffle_left_3(self) -> Self {
use crate::simd::simdint::simd_shuffle4;
const IDX: [u32; 4] = [3, 0, 1, 2];
unsafe { simd_shuffle4(self, self, IDX) }
pub(crate) fn as_bytes(&self) -> &[u8] {
let p = self as *const Self as *const u8;
unsafe { core::slice::from_raw_parts(p, core::mem::size_of::<Self>()) }
}
}

impl Add for $vec {
type Output = Self;

#[cfg(not(feature = "simd"))]
#[inline(always)]
fn shuffle_left_3(self) -> Self {
$vec::new(self.3, self.0, self.1, self.2)
fn add(self, rhs: Self) -> Self::Output {
$vec::new(
self.0.wrapping_add(rhs.0),
self.1.wrapping_add(rhs.1),
self.2.wrapping_add(rhs.2),
self.3.wrapping_add(rhs.3),
)
}
}

impl BitXor for $vec {
type Output = Self;

#[inline(always)]
fn bitxor(self, rhs: Self) -> Self::Output {
$vec::new(
self.0 ^ rhs.0,
self.1 ^ rhs.1,
self.2 ^ rhs.2,
self.3 ^ rhs.3,
)
}
}

impl Shl<$vec> for $vec {
type Output = Self;

#[inline(always)]
fn shl(self, rhs: Self) -> Self::Output {
$vec::new(
self.0 << rhs.0,
self.1 << rhs.1,
self.2 << rhs.2,
self.3 << rhs.3,
)
}
}

impl Shr<$vec> for $vec {
type Output = Self;

#[inline(always)]
fn shr(self, rhs: Self) -> Self::Output {
$vec::new(
self.0 >> rhs.0,
self.1 >> rhs.1,
self.2 >> rhs.2,
self.3 >> rhs.3,
)
}
}

#[cfg(feature = "zeroize")]
impl Zeroize for $vec {
fn zeroize(&mut self) {
self.0.zeroize();
self.1.zeroize();
self.2.zeroize();
self.3.zeroize();
}
}
};
Expand Down
Loading
Loading