Skip to content
Draft
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,42 @@ members = [

[profile.dev]
opt-level = 2

[workspace.lints.clippy]
borrow_as_ptr = "warn"
cast_lossless = "warn"
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_precision_loss = "warn"
cast_sign_loss = "warn"
checked_conversions = "warn"
doc_markdown = "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 = "warn"
unnecessary_safety_comment = "warn"
unwrap_in_result = "warn"
unwrap_used = "warn"

[workspace.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"
38 changes: 38 additions & 0 deletions aes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,41 @@ check-cfg = [

[package.metadata.docs.rs]
all-features = true

[lints.clippy]
borrow_as_ptr = "warn"
cast_lossless = "warn"
cast_possible_truncation = "warn"
cast_possible_wrap = "warn"
cast_precision_loss = "warn"
cast_sign_loss = "warn"
checked_conversions = "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 = "warn"
unnecessary_safety_comment = "warn"
unwrap_in_result = "warn"
unwrap_used = "warn"

[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"
1 change: 1 addition & 0 deletions aes/benches/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(test)]
#![allow(missing_docs)]
extern crate test;

use cipher::{KeyInit, block_decryptor_bench, block_encryptor_bench};
Expand Down
12 changes: 6 additions & 6 deletions aes/src/backends/fixslice/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,35 +104,35 @@ fn inv_mix_columns_generic<W: Word>(block: &mut Block) {
/// AES cipher (encrypt) round function.
#[inline]
pub(crate) fn cipher_round(block: &mut Block, round_key: &Block) {
cipher_round_generic::<NativeWord>(block, round_key)
cipher_round_generic::<NativeWord>(block, round_key);
}

/// AES cipher (encrypt) round function: parallel version.
#[inline]
pub(crate) fn cipher_round_par(blocks: &mut Block8, round_keys: &Block8) {
cipher_round_par_generic::<NativeWord>(blocks, round_keys)
cipher_round_par_generic::<NativeWord>(blocks, round_keys);
}

/// AES cipher (encrypt) inverse round function.
#[inline]
pub(crate) fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
equiv_inv_cipher_round_generic::<NativeWord>(block, round_key)
equiv_inv_cipher_round_generic::<NativeWord>(block, round_key);
}

/// AES cipher (encrypt) inverse round function: parallel version.
#[inline]
pub(crate) fn equiv_inv_cipher_round_par(blocks: &mut Block8, round_keys: &Block8) {
equiv_inv_cipher_round_par_generic::<NativeWord>(blocks, round_keys)
equiv_inv_cipher_round_par_generic::<NativeWord>(blocks, round_keys);
}

/// AES mix columns function.
#[inline]
pub(crate) fn mix_columns(block: &mut Block) {
mix_columns_generic::<NativeWord>(block)
mix_columns_generic::<NativeWord>(block);
}

/// AES inverse mix columns function.
#[inline]
pub(crate) fn inv_mix_columns(block: &mut Block) {
inv_mix_columns_generic::<NativeWord>(block)
inv_mix_columns_generic::<NativeWord>(block);
}
53 changes: 28 additions & 25 deletions aes/src/backends/fixslice/word.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::cast_possible_truncation)]
use crate::{Block, backends::soft::MinWord};
use cipher::{
Array,
Expand Down Expand Up @@ -72,17 +73,17 @@ impl Word for u16 {

#[inline(always)]
fn uniform_row(b: u8) -> u16 {
(b as u16) * 0x1111
u16::from(b) * 0x1111
}

#[inline(always)]
fn pack_rows(r0: u8, r1: u8, r2: u8, r3: u8) -> u16 {
(r0 as u16) | ((r1 as u16) << 4) | ((r2 as u16) << 8) | ((r3 as u16) << 12)
u16::from(r0) | (u16::from(r1) << 4) | (u16::from(r2) << 8) | (u16::from(r3) << 12)
}

#[inline(always)]
fn byte_repeat(b: u8) -> u16 {
(b as u16) * 0x0101
u16::from(b) * 0x0101
}

/// Bitslice one 128-bit input block into a 128-bit internal state.
Expand Down Expand Up @@ -226,27 +227,29 @@ impl Word for u32 {

#[inline(always)]
fn uniform_row(b: u8) -> u32 {
double_bits_8_to_16(b) as u32 * 0x01010101
u32::from(double_bits_8_to_16(b)) * 0x01010101
}

#[inline(always)]
fn pack_rows(r0: u8, r1: u8, r2: u8, r3: u8) -> u32 {
(double_bits_8_to_16(r0) as u32)
| ((double_bits_8_to_16(r1) as u32) << 8)
| ((double_bits_8_to_16(r2) as u32) << 16)
| ((double_bits_8_to_16(r3) as u32) << 24)
u32::from(double_bits_8_to_16(r0))
| (u32::from(double_bits_8_to_16(r1)) << 8)
| (u32::from(double_bits_8_to_16(r2)) << 16)
| (u32::from(double_bits_8_to_16(r3)) << 24)
}

#[inline(always)]
fn byte_repeat(b: u8) -> u32 {
(b as u32) * 0x01010101
u32::from(b) * 0x01010101
}

/// Bitslice two 128-bit input blocks into a 256-bit internal state.
fn bitslice(output: &mut [u32], input: &Array<Block, U2>) {
debug_assert_eq!(output.len(), 8);
let input0 = input[0].as_slice();
let input1 = input[1].as_slice();
let (input0, tail) = input[0].as_chunks();
assert!(tail.is_empty());
let (input1, tail) = input[1].as_chunks();
assert!(tail.is_empty());

// Bitslicing is a bit index manipulation. 256 bits of data means each bit is positioned at
// an 8-bit index. AES data is 2 blocks, each one a 4x4 column-major matrix of bytes, so the
Expand All @@ -259,14 +262,14 @@ impl Word for u32 {
// Interleave the columns on input (note the order of input)
// b0 c1 c0 __ __ __ __ __ => c1 c0 b0 __ __ __ __ __
let mut t = [
u32::from_le_bytes(input0[0x00..0x04].try_into().unwrap()),
u32::from_le_bytes(input1[0x00..0x04].try_into().unwrap()),
u32::from_le_bytes(input0[0x04..0x08].try_into().unwrap()),
u32::from_le_bytes(input1[0x04..0x08].try_into().unwrap()),
u32::from_le_bytes(input0[0x08..0x0c].try_into().unwrap()),
u32::from_le_bytes(input1[0x08..0x0c].try_into().unwrap()),
u32::from_le_bytes(input0[0x0c..0x10].try_into().unwrap()),
u32::from_le_bytes(input1[0x0c..0x10].try_into().unwrap()),
u32::from_le_bytes(input0[0]),
u32::from_le_bytes(input1[0]),
u32::from_le_bytes(input0[1]),
u32::from_le_bytes(input1[1]),
u32::from_le_bytes(input0[2]),
u32::from_le_bytes(input1[2]),
u32::from_le_bytes(input0[3]),
u32::from_le_bytes(input1[3]),
];

bitslice_swaps(&mut t);
Expand Down Expand Up @@ -326,20 +329,20 @@ impl Word for u64 {

#[inline(always)]
fn uniform_row(b: u8) -> u64 {
quad_bits_16_to_64(b as u16) * 0x0001_0001_0001_0001
quad_bits_16_to_64(u16::from(b)) * 0x0001_0001_0001_0001
}

#[inline(always)]
fn pack_rows(r0: u8, r1: u8, r2: u8, r3: u8) -> u64 {
quad_bits_16_to_64(r0 as u16)
| (quad_bits_16_to_64(r1 as u16) << 16)
| (quad_bits_16_to_64(r2 as u16) << 32)
| (quad_bits_16_to_64(r3 as u16) << 48)
quad_bits_16_to_64(u16::from(r0))
| (quad_bits_16_to_64(u16::from(r1)) << 16)
| (quad_bits_16_to_64(u16::from(r2)) << 32)
| (quad_bits_16_to_64(u16::from(r3)) << 48)
}

#[inline(always)]
fn byte_repeat(b: u8) -> u64 {
(b as u64) * 0x0101010101010101
u64::from(b) * 0x0101010101010101
}

/// Bitslice four 128-bit input blocks into a 512-bit internal state.
Expand Down
10 changes: 5 additions & 5 deletions aes/src/backends/x86_aes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<const RK: usize> BlockCipherEncBackend for Aes<RK> {
fn encrypt_block(&self, block: InOut<'_, '_, Block<Self>>) {
// SAFETY: this trait impl is used only by the `Self::encrypt` method marked with
// `#[target_feature(enable = "aes")]`
unsafe { encdec::encrypt(&self.enc_rk, block) };
unsafe { encrypt(&self.enc_rk, block) };
}

#[inline(always)]
Expand All @@ -78,7 +78,7 @@ impl<const RK: usize> BlockCipherDecBackend for Aes<RK> {
fn decrypt_block(&self, block: InOut<'_, '_, Block<Self>>) {
// SAFETY: this trait impl is used only by the `Self::decrypt` method marked with
// `#[target_feature(enable = "aes")]`
unsafe { encdec::decrypt(&self.dec_rk, block) };
unsafe { decrypt(&self.dec_rk, block) };
}

#[inline(always)]
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<const RK: usize> AesEnc<RK> {
#[inline]
#[target_feature(enable = "aes")]
pub(crate) fn encrypt(&self, f: impl BlockCipherEncClosure<BlockSize = U16>) {
f.call(self)
f.call(self);
}
}

Expand All @@ -130,7 +130,7 @@ impl<const RK: usize> BlockCipherEncBackend for AesEnc<RK> {
fn encrypt_block(&self, block: InOut<'_, '_, Block<Self>>) {
// SAFETY: this trait impl is used only by the `Self::encrypt` method marked with
// `#[target_feature(enable = "aes")]`
unsafe { encdec::encrypt(&self.enc_rk, block) };
unsafe { encrypt(&self.enc_rk, block) };
}

#[inline(always)]
Expand Down Expand Up @@ -167,7 +167,7 @@ impl<const RK: usize> BlockCipherDecBackend for AesDec<RK> {
fn decrypt_block(&self, block: InOut<'_, '_, Block<Self>>) {
// SAFETY: this trait impl is used only by the `Self::decrypt` method marked with
// `#[target_feature(enable = "aes")]`
unsafe { encdec::decrypt(&self.dec_rk, block) };
unsafe { decrypt(&self.dec_rk, block) };
}

#[inline(always)]
Expand Down
2 changes: 0 additions & 2 deletions aes/src/backends/x86_aes/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use core::arch::x86_64::*;
/// AES cipher (encrypt) round function.
#[target_feature(enable = "aes")]
pub(crate) fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = utils::load_block(block);
let k = utils::load_block(round_key);
let b = _mm_aesenc_si128(b, k);
Expand Down Expand Up @@ -59,7 +58,6 @@ pub(crate) fn equiv_inv_cipher_round_par(blocks: &mut Block8, round_keys: &Block
/// AES mix columns function.
#[target_feature(enable = "aes")]
pub(crate) fn mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let mut state = utils::load_block(block);

// Emulate mix columns by performing three inverse mix columns operations
Expand Down
4 changes: 2 additions & 2 deletions aes/src/backends/x86_vaes256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ impl<'a, const RK: usize> Aes<'a, RK> {
pub(crate) fn encrypt(rk: &'a RoundKeys<RK>, f: impl BlockCipherEncClosure<BlockSize = U16>) {
let rk2 = encdec::broadcast_keys(rk);
let backend = Self { rk, rk2 };
f.call(&backend)
f.call(&backend);
}

#[inline]
#[target_feature(enable = "vaes")]
pub(crate) fn decrypt(rk: &'a RoundKeys<RK>, f: impl BlockCipherDecClosure<BlockSize = U16>) {
let rk2 = encdec::broadcast_keys(rk);
let backend = Self { rk, rk2 };
f.call(&backend)
f.call(&backend);
}
}

Expand Down
4 changes: 2 additions & 2 deletions aes/src/backends/x86_vaes512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ impl<'a, const RK: usize> Aes<'a, RK> {
pub(crate) fn encrypt(rk: &'a RoundKeys<RK>, f: impl BlockCipherEncClosure<BlockSize = U16>) {
let rk4 = encdec::broadcast_keys(rk);
let backend = Self { rk, rk4 };
f.call(&backend)
f.call(&backend);
}

#[inline]
#[target_feature(enable = "avx512f,vaes")]
pub(crate) fn decrypt(rk: &'a RoundKeys<RK>, f: impl BlockCipherDecClosure<BlockSize = U16>) {
let rk4 = encdec::broadcast_keys(rk);
let backend = Self { rk, rk4 };
f.call(&backend)
f.call(&backend);
}
}

Expand Down
3 changes: 2 additions & 1 deletion aes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg"
)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs, rust_2018_idioms)]

pub use cipher;

Expand Down Expand Up @@ -196,6 +195,7 @@ cfg_if! {
/// println!("WARNING: using software fallback for AES");
/// }
/// ```
#[must_use]
pub fn hardware_accelerated() -> bool {
cfg_if! {
if #[cfg(aes_backend = "soft")] {
Expand Down Expand Up @@ -420,6 +420,7 @@ macro_rules! common_impls {
#[inline]
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
// SAFETY: the type satisfies the function safety conditions
unsafe {
zeroize::zeroize_flat_type(self);
}
Expand Down
4 changes: 2 additions & 2 deletions aes/tests/hazmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ fn equiv_inv_cipher_round_par_fips197_vectors() {
fn mix_columns_fips197_vector() {
let mut block = Block::from(hex!("6353e08c0960e104cd70b751bacad0e7"));
aes::hazmat::mix_columns(&mut block);
assert_eq!(block.as_slice(), &hex!("5f72641557f5bc92f7be3b291db9f91a"))
assert_eq!(block.as_slice(), &hex!("5f72641557f5bc92f7be3b291db9f91a"));
}

#[test]
fn inv_mix_columns_fips197_vector() {
let mut block = Block::from(hex!("bd6e7c3df2b5779e0b61216e8b10b689"));
aes::hazmat::inv_mix_columns(&mut block);
assert_eq!(block.as_slice(), &hex!("4773b91ff72f354361cb018ea1e6cf2c"))
assert_eq!(block.as_slice(), &hex!("4773b91ff72f354361cb018ea1e6cf2c"));
}
2 changes: 1 addition & 1 deletion aes/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Test vectors are from NESSIE:
//! https://www.cosic.esat.kuleuven.be/nessie/testvectors/
//! <https://www.cosic.esat.kuleuven.be/nessie/testvectors/>
#![cfg(not(miri))]

cipher::block_cipher_test!(aes128, aes::Aes128);
Expand Down
3 changes: 3 additions & 0 deletions blowfish/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ zeroize = ["cipher/zeroize"]

[package.metadata.docs.rs]
all-features = true

[lints]
workspace = true
Loading
Loading