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
39 changes: 39 additions & 0 deletions poly1305/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,48 @@ cpufeatures = "0.3"
[dev-dependencies]
hex-literal = "1"

[lints.rust]
missing_copy_implementations = "warn"
missing_debug_implementations = "warn"
missing_docs = "warn"
trivial_casts = "warn"
trivial_numeric_casts = "warn"
unused_lifetimes = "warn"
unused_qualifications = "warn"
unreachable_pub = "warn"

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = [
'cfg(fuzzing)',
'cfg(poly1305_backend, values("soft"))'
]

[lints.clippy]
borrow_as_ptr = "warn"
cast_lossless = "warn"
cast_possible_truncation = "allow" # TODO(tarcieri): warn
cast_possible_wrap = "warn"
cast_precision_loss = "warn"
cast_sign_loss = "warn"
checked_conversions = "warn"
from_iter_instead_of_collect = "warn"
implicit_saturating_sub = "warn"
manual_assert = "warn"
map_unwrap_or = "warn"
missing_errors_doc = "warn"
missing_panics_doc = "warn"
mod_module_files = "warn"
must_use_candidate = "warn"
needless_range_loop = "allow"
ptr_as_ptr = "warn"
redundant_closure_for_method_calls = "warn"
ref_as_ptr = "warn"
return_self_not_must_use = "warn"
semicolon_if_nothing_returned = "warn"
trivially_copy_pass_by_ref = "warn"
std_instead_of_alloc = "warn"
std_instead_of_core = "warn"
undocumented_unsafe_blocks = "allow" # TODO(tarcieri): warn
unnecessary_safety_comment = "warn"
unwrap_used = "allow" # TODO(tarcieri): warn
2 changes: 2 additions & 0 deletions poly1305/benches/poly1305.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Poly1305 benchmarks.

#![feature(test)]

extern crate test;
Expand Down
6 changes: 3 additions & 3 deletions poly1305/src/backend/autodetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::mem::ManuallyDrop;

cpufeatures::new!(avx2_cpuid, "avx2");

pub struct State {
pub(crate) struct State {
inner: Inner,
token: avx2_cpuid::InitToken,
}
Expand Down Expand Up @@ -95,8 +95,8 @@ impl Clone for State {
impl Drop for State {
fn drop(&mut self) {
use zeroize::Zeroize;
const SIZE: usize = core::mem::size_of::<State>();
let state = unsafe { &mut *(self as *mut State as *mut [u8; SIZE]) };
const SIZE: usize = size_of::<State>();
let state = unsafe { &mut *core::ptr::from_mut::<State>(self).cast::<[u8; SIZE]>() };
state.zeroize();
}
}
2 changes: 1 addition & 1 deletion poly1305/src/backend/avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl State {
// finalization).
let (m, r4) = SpacedMultiplier4x130::new(self.r1, self.r2);

self.initialized = Some(Initialized { p, m, r4 })
self.initialized = Some(Initialized { p, m, r4 });
}
}

Expand Down
58 changes: 29 additions & 29 deletions poly1305/src/backend/avx2/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const fn set02(x3: u8, x2: u8, x1: u8, x0: u8) -> i32 {

/// Helper for Display impls of aligned values.
fn write_130(f: &mut fmt::Formatter<'_>, limbs: [u32; 5]) -> fmt::Result {
let r0 = limbs[0] as u128;
let r1 = limbs[1] as u128;
let r2 = limbs[2] as u128;
let r3 = limbs[3] as u128;
let r4 = limbs[4] as u128;
let r0 = u128::from(limbs[0]);
let r1 = u128::from(limbs[1]);
let r2 = u128::from(limbs[2]);
let r3 = u128::from(limbs[3]);
let r4 = u128::from(limbs[4]);

// Reduce into two u128s
let l0 = r0 + (r1 << 26) + (r2 << 52) + (r3 << 78);
Expand All @@ -34,11 +34,11 @@ fn write_130(f: &mut fmt::Formatter<'_>, limbs: [u32; 5]) -> fmt::Result {

/// Helper for Display impls of unreduced values.
fn write_130_wide(f: &mut fmt::Formatter<'_>, limbs: [u64; 5]) -> fmt::Result {
let r0 = limbs[0] as u128;
let r1 = limbs[1] as u128;
let r2 = limbs[2] as u128;
let r3 = limbs[3] as u128;
let r4 = limbs[4] as u128;
let r0 = u128::from(limbs[0]);
let r1 = u128::from(limbs[1]);
let r2 = u128::from(limbs[2]);
let r3 = u128::from(limbs[3]);
let r4 = u128::from(limbs[4]);

// Reduce into two u128s
let l0 = r0 + (r1 << 26) + (r2 << 52);
Expand All @@ -53,7 +53,7 @@ fn write_130_wide(f: &mut fmt::Formatter<'_>, limbs: [u64; 5]) -> fmt::Result {
#[target_feature(enable = "avx2")]
pub(super) unsafe fn prepare_keys(key: &Key) -> (AdditionKey, PrecomputedMultiplier) {
// [k7, k6, k5, k4, k3, k2, k1, k0]
let key = _mm256_loadu_si256(key.as_ptr() as *const _);
let key = _mm256_loadu_si256(key.as_ptr().cast());

// Prepare addition key: [0, k7, 0, k6, 0, k5, 0, k4]
let k = AdditionKey(_mm256_and_si256(
Expand All @@ -80,7 +80,7 @@ impl fmt::Display for Aligned130 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut v0 = [0u8; 32];
unsafe {
_mm256_storeu_si256(v0.as_mut_ptr() as *mut _, self.0);
_mm256_storeu_si256(v0.as_mut_ptr().cast(), self.0);
}

write!(f, "Aligned130(")?;
Expand All @@ -106,7 +106,7 @@ impl Aligned130 {
Aligned130::new(_mm256_or_si256(
_mm256_and_si256(
// Load the 128-bit block into a 256-bit vector.
_mm256_castsi128_si256(_mm_loadu_si128(block.as_ptr() as *const _)),
_mm256_castsi128_si256(_mm_loadu_si128(block.as_ptr().cast())),
// Mask off the upper 128 bits (undefined by _mm256_castsi128_si256).
_mm256_set_epi64x(0, 0, -1, -1),
),
Expand All @@ -122,7 +122,7 @@ impl Aligned130 {
pub(super) unsafe fn from_partial_block(block: &Block) -> Self {
Aligned130::new(_mm256_and_si256(
// Load the 128-bit block into a 256-bit vector.
_mm256_castsi128_si256(_mm_loadu_si128(block.as_ptr() as *const _)),
_mm256_castsi128_si256(_mm_loadu_si128(block.as_ptr().cast())),
// Mask off the upper 128 bits (undefined by _mm256_castsi128_si256).
_mm256_set_epi64x(0, 0, -1, -1),
))
Expand Down Expand Up @@ -419,8 +419,8 @@ impl fmt::Display for Unreduced130 {
let mut v0 = [0u8; 32];
let mut v1 = [0u8; 32];
unsafe {
_mm256_storeu_si256(v0.as_mut_ptr() as *mut _, self.v0);
_mm256_storeu_si256(v1.as_mut_ptr() as *mut _, self.v1);
_mm256_storeu_si256(v0.as_mut_ptr().cast(), self.v0);
_mm256_storeu_si256(v1.as_mut_ptr().cast(), self.v1);
}

write!(f, "Unreduced130(")?;
Expand Down Expand Up @@ -899,9 +899,9 @@ impl fmt::Display for Aligned4x130 {
let mut v1 = [0u8; 32];
let mut v2 = [0u8; 32];
unsafe {
_mm256_storeu_si256(v0.as_mut_ptr() as *mut _, self.v0);
_mm256_storeu_si256(v1.as_mut_ptr() as *mut _, self.v1);
_mm256_storeu_si256(v2.as_mut_ptr() as *mut _, self.v2);
_mm256_storeu_si256(v0.as_mut_ptr().cast(), self.v0);
_mm256_storeu_si256(v1.as_mut_ptr().cast(), self.v1);
_mm256_storeu_si256(v2.as_mut_ptr().cast(), self.v2);
}

writeln!(f, "Aligned4x130([")?;
Expand Down Expand Up @@ -967,8 +967,8 @@ impl Aligned4x130 {
#[target_feature(enable = "avx2")]
pub(super) unsafe fn from_blocks(src: &[Block; 4]) -> Self {
let (lo, hi) = src.split_at(2);
let blocks_23 = _mm256_loadu_si256(hi.as_ptr() as *const _);
let blocks_01 = _mm256_loadu_si256(lo.as_ptr() as *const _);
let blocks_23 = _mm256_loadu_si256(hi.as_ptr().cast());
let blocks_01 = _mm256_loadu_si256(lo.as_ptr().cast());

Self::from_loaded_blocks(blocks_01, blocks_23)
}
Expand All @@ -978,8 +978,8 @@ impl Aligned4x130 {
#[target_feature(enable = "avx2")]
pub(super) unsafe fn from_par_blocks(src: &ParBlocks) -> Self {
let (lo, hi) = src.split_at(2);
let blocks_23 = _mm256_loadu_si256(hi.as_ptr() as *const _);
let blocks_01 = _mm256_loadu_si256(lo.as_ptr() as *const _);
let blocks_23 = _mm256_loadu_si256(hi.as_ptr().cast());
let blocks_01 = _mm256_loadu_si256(lo.as_ptr().cast());

Self::from_loaded_blocks(blocks_01, blocks_23)
}
Expand Down Expand Up @@ -1598,11 +1598,11 @@ impl fmt::Display for Unreduced4x130 {
let mut v3 = [0u8; 32];
let mut v4 = [0u8; 32];
unsafe {
_mm256_storeu_si256(v0.as_mut_ptr() as *mut _, self.v0);
_mm256_storeu_si256(v1.as_mut_ptr() as *mut _, self.v1);
_mm256_storeu_si256(v2.as_mut_ptr() as *mut _, self.v2);
_mm256_storeu_si256(v3.as_mut_ptr() as *mut _, self.v3);
_mm256_storeu_si256(v4.as_mut_ptr() as *mut _, self.v4);
_mm256_storeu_si256(v0.as_mut_ptr().cast(), self.v0);
_mm256_storeu_si256(v1.as_mut_ptr().cast(), self.v1);
_mm256_storeu_si256(v2.as_mut_ptr().cast(), self.v2);
_mm256_storeu_si256(v3.as_mut_ptr().cast(), self.v3);
_mm256_storeu_si256(v4.as_mut_ptr().cast(), self.v4);
}

writeln!(f, "Unreduced4x130([")?;
Expand Down Expand Up @@ -1986,7 +1986,7 @@ impl From<AdditionKey> for IntegerTag {
impl IntegerTag {
pub(super) fn write(self, tag: &mut [u8]) {
unsafe {
_mm_storeu_si128(tag.as_mut_ptr() as *mut _, self.0);
_mm_storeu_si128(tag.as_mut_ptr().cast(), self.0);
}
}
}
2 changes: 1 addition & 1 deletion poly1305/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
)]
#![warn(missing_docs)]

pub use universal_hash;

Expand Down Expand Up @@ -95,6 +94,7 @@ impl Poly1305 {
/// Compute unpadded Poly1305 for the given input data.
///
/// The main use case for this is XSalsa20Poly1305.
#[must_use]
pub fn compute_unpadded(mut self, data: &[u8]) -> Tag {
let (blocks, remaining) = Block::slice_as_chunks(data);

Expand Down
4 changes: 3 additions & 1 deletion poly1305/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//! Poly1305 integration tests.

use core::iter::repeat_n;
use hex_literal::hex;
use poly1305::{
Block, KEY_SIZE, Poly1305,
universal_hash::{KeyInit, UniversalHash},
};
use std::iter::repeat_n;

#[test]
fn test_nacl_vector() {
Expand Down