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
2 changes: 1 addition & 1 deletion aes/src/armv8.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@
#![allow(clippy::needless_range_loop)]

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

mod decrypt;
mod encrypt;
Expand Down
15 changes: 12 additions & 3 deletions aes/src/armv8/round.rs → aes/src/armv8/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: ARMv8 Cryptography Extensions support.
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -11,7 +11,7 @@ use core::arch::aarch64::*;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -30,7 +30,7 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES equivalent inverse cipher (decrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -45,3 +45,12 @@ pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {

vst1q_u8(block.as_mut_ptr(), state);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
let b = vld1q_u8(block.as_ptr());
let out = vaesimcq_u8(b);
vst1q_u8(block.as_mut_ptr(), out);
}
38 changes: 27 additions & 11 deletions aes/src/round.rs → aes/src/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! ⚠️ Raw AES round function.
//! ⚠️ Low-level "hazmat" AES functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//!
Expand All@@ -14,10 +14,10 @@
use crate::Block;

#[cfg(all(target_arch = "aarch64", feature = "armv8"))]
use crate::armv8::round as intrinsics;
use crate::armv8::hazmat as intrinsics;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::ni::round as intrinsics;
use crate::ni::hazmat as intrinsics;

#[cfg(not(any(
target_arch = "x86_64",
Expand All@@ -43,11 +43,11 @@ cpufeatures::new!(aes_intrinsics, "aes");
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::cipher(block, round_key) };
pub fn cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

Expand All@@ -66,10 +66,26 @@ pub fn cipher(block: &mut Block, round_key: &Block) {
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::equiv_inv_cipher(block, round_key) };
pub fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::equiv_inv_cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

/// ⚠️ AES inverse mix columns function.
///
/// This function is equivalent to the Intel AES-NI `AESIMC` instruction.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn inv_mix_columns(block: &mut Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::inv_mix_columns(block) };
} else {
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}
2 changes: 1 addition & 1 deletion aes/src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,7 +94,7 @@
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(all(feature = "hazmat", not(feature = "force-soft")))]
pub mod round;
pub mod hazmat;

mod soft;

Expand Down
2 changes: 1 addition & 1 deletion aes/src/ni.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ mod aes256;
mod ctr;

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
Expand Down
16 changes: 13 additions & 3 deletions aes/src/ni/round.rs → aes/src/ni/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: AES-NI support.
//! Low-level "hazmat" AES functions: AES-NI support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -10,7 +10,7 @@ use crate::Block;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
Expand All@@ -21,10 +21,20 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
let out = _mm_aesdec_si128(b, k);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let out = _mm_aesimc_si128(b);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}
43 changes: 25 additions & 18 deletions aes/tests/round.rs → aes/tests/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
//! Tests for the raw AES round function.
//! Tests for low-level "hazmat" AES functions.

#![cfg(all(feature = "hazmat", not(feature = "force-soft")))]

use aes::Block;
use hex_literal::hex;

/// Round function tests vectors.
struct TestVector {
struct RoundTestVector {
/// State at start of `round[r]`.
start: [u8; 16],

Expand All@@ -18,75 +18,82 @@ struct TestVector {
}

/// Cipher round function test vectors from FIPS 197 Appendix C.1.
const CIPHER_TEST_VECTORS: &[TestVector] = &[
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("00102030405060708090a0b0c0d0e0f0"),
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"),
output: hex!("89d810e8855ace682d1843d8cb128fe4"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("89d810e8855ace682d1843d8cb128fe4"),
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"),
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"),
output: hex!("fa636a2825b339c940668a3157244d17"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("fa636a2825b339c940668a3157244d17"),
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"),
output: hex!("247240236966b3fa6ed2753288425b6c"),
},
];

/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1.
const EQUIV_INV_CIPHER_TEST_VECTORS: &[TestVector] = &[
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"),
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"),
output: hex!("54d990a16ba09ab596bbf40ea111702f"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("54d990a16ba09ab596bbf40ea111702f"),
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"),
output: hex!("3e1c22c0b6fcbf768da85067f6170495"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("3e1c22c0b6fcbf768da85067f6170495"),
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"),
output: hex!("b458124c68b68a014b99f82e5f15554c"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("b458124c68b68a014b99f82e5f15554c"),
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"),
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"),
},
];

#[test]
fn cipher_fips197_vectors() {
for vector in CIPHER_TEST_VECTORS {
fn cipher_round_fips197_vectors() {
for vector in CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::cipher(&mut block, &vector.k_sch.into());
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[test]
fn equiv_inv_cipher_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_TEST_VECTORS {
fn equiv_inv_cipher_round_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::equiv_inv_cipher(&mut block, &vector.k_sch.into());
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[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"))
}
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
aes: rename `hazmat` module; add `inv_mix_columns` by tarcieri · Pull Request #259 · RustCrypto/block-ciphers · GitHub
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
2 changes: 1 addition & 1 deletion aes/src/armv8.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@
#![allow(clippy::needless_range_loop)]

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

mod decrypt;
mod encrypt;
Expand Down
15 changes: 12 additions & 3 deletions aes/src/armv8/round.rs → aes/src/armv8/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: ARMv8 Cryptography Extensions support.
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -11,7 +11,7 @@ use core::arch::aarch64::*;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -30,7 +30,7 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES equivalent inverse cipher (decrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -45,3 +45,12 @@ pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {

vst1q_u8(block.as_mut_ptr(), state);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
let b = vld1q_u8(block.as_ptr());
let out = vaesimcq_u8(b);
vst1q_u8(block.as_mut_ptr(), out);
}
38 changes: 27 additions & 11 deletions aes/src/round.rs → aes/src/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! ⚠️ Raw AES round function.
//! ⚠️ Low-level "hazmat" AES functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//!
Expand All@@ -14,10 +14,10 @@
use crate::Block;

#[cfg(all(target_arch = "aarch64", feature = "armv8"))]
use crate::armv8::round as intrinsics;
use crate::armv8::hazmat as intrinsics;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::ni::round as intrinsics;
use crate::ni::hazmat as intrinsics;

#[cfg(not(any(
target_arch = "x86_64",
Expand All@@ -43,11 +43,11 @@ cpufeatures::new!(aes_intrinsics, "aes");
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::cipher(block, round_key) };
pub fn cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

Expand All@@ -66,10 +66,26 @@ pub fn cipher(block: &mut Block, round_key: &Block) {
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::equiv_inv_cipher(block, round_key) };
pub fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::equiv_inv_cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

/// ⚠️ AES inverse mix columns function.
///
/// This function is equivalent to the Intel AES-NI `AESIMC` instruction.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn inv_mix_columns(block: &mut Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::inv_mix_columns(block) };
} else {
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}
2 changes: 1 addition & 1 deletion aes/src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,7 +94,7 @@
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(all(feature = "hazmat", not(feature = "force-soft")))]
pub mod round;
pub mod hazmat;

mod soft;

Expand Down
2 changes: 1 addition & 1 deletion aes/src/ni.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ mod aes256;
mod ctr;

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
Expand Down
16 changes: 13 additions & 3 deletions aes/src/ni/round.rs → aes/src/ni/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: AES-NI support.
//! Low-level "hazmat" AES functions: AES-NI support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -10,7 +10,7 @@ use crate::Block;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
Expand All@@ -21,10 +21,20 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
let out = _mm_aesdec_si128(b, k);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let out = _mm_aesimc_si128(b);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}
43 changes: 25 additions & 18 deletions aes/tests/round.rs → aes/tests/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
//! Tests for the raw AES round function.
//! Tests for low-level "hazmat" AES functions.

#![cfg(all(feature = "hazmat", not(feature = "force-soft")))]

use aes::Block;
use hex_literal::hex;

/// Round function tests vectors.
struct TestVector {
struct RoundTestVector {
/// State at start of `round[r]`.
start: [u8; 16],

Expand All@@ -18,75 +18,82 @@ struct TestVector {
}

/// Cipher round function test vectors from FIPS 197 Appendix C.1.
const CIPHER_TEST_VECTORS: &[TestVector] = &[
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("00102030405060708090a0b0c0d0e0f0"),
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"),
output: hex!("89d810e8855ace682d1843d8cb128fe4"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("89d810e8855ace682d1843d8cb128fe4"),
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"),
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"),
output: hex!("fa636a2825b339c940668a3157244d17"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("fa636a2825b339c940668a3157244d17"),
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"),
output: hex!("247240236966b3fa6ed2753288425b6c"),
},
];

/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1.
const EQUIV_INV_CIPHER_TEST_VECTORS: &[TestVector] = &[
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"),
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"),
output: hex!("54d990a16ba09ab596bbf40ea111702f"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("54d990a16ba09ab596bbf40ea111702f"),
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"),
output: hex!("3e1c22c0b6fcbf768da85067f6170495"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("3e1c22c0b6fcbf768da85067f6170495"),
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"),
output: hex!("b458124c68b68a014b99f82e5f15554c"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("b458124c68b68a014b99f82e5f15554c"),
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"),
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"),
},
];

#[test]
fn cipher_fips197_vectors() {
for vector in CIPHER_TEST_VECTORS {
fn cipher_round_fips197_vectors() {
for vector in CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::cipher(&mut block, &vector.k_sch.into());
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[test]
fn equiv_inv_cipher_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_TEST_VECTORS {
fn equiv_inv_cipher_round_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::equiv_inv_cipher(&mut block, &vector.k_sch.into());
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[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"))
}
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' aes: rename `hazmat` module; add `inv_mix_columns` by tarcieri · Pull Request #259 · RustCrypto/block-ciphers · GitHub
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
2 changes: 1 addition & 1 deletion aes/src/armv8.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@
#![allow(clippy::needless_range_loop)]

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

mod decrypt;
mod encrypt;
Expand Down
15 changes: 12 additions & 3 deletions aes/src/armv8/round.rs → aes/src/armv8/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: ARMv8 Cryptography Extensions support.
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -11,7 +11,7 @@ use core::arch::aarch64::*;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -30,7 +30,7 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES equivalent inverse cipher (decrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -45,3 +45,12 @@ pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {

vst1q_u8(block.as_mut_ptr(), state);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
let b = vld1q_u8(block.as_ptr());
let out = vaesimcq_u8(b);
vst1q_u8(block.as_mut_ptr(), out);
}
38 changes: 27 additions & 11 deletions aes/src/round.rs → aes/src/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! ⚠️ Raw AES round function.
//! ⚠️ Low-level "hazmat" AES functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//!
Expand All@@ -14,10 +14,10 @@
use crate::Block;

#[cfg(all(target_arch = "aarch64", feature = "armv8"))]
use crate::armv8::round as intrinsics;
use crate::armv8::hazmat as intrinsics;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::ni::round as intrinsics;
use crate::ni::hazmat as intrinsics;

#[cfg(not(any(
target_arch = "x86_64",
Expand All@@ -43,11 +43,11 @@ cpufeatures::new!(aes_intrinsics, "aes");
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::cipher(block, round_key) };
pub fn cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

Expand All@@ -66,10 +66,26 @@ pub fn cipher(block: &mut Block, round_key: &Block) {
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::equiv_inv_cipher(block, round_key) };
pub fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::equiv_inv_cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

/// ⚠️ AES inverse mix columns function.
///
/// This function is equivalent to the Intel AES-NI `AESIMC` instruction.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn inv_mix_columns(block: &mut Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::inv_mix_columns(block) };
} else {
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}
2 changes: 1 addition & 1 deletion aes/src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,7 +94,7 @@
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(all(feature = "hazmat", not(feature = "force-soft")))]
pub mod round;
pub mod hazmat;

mod soft;

Expand Down
2 changes: 1 addition & 1 deletion aes/src/ni.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ mod aes256;
mod ctr;

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
Expand Down
16 changes: 13 additions & 3 deletions aes/src/ni/round.rs → aes/src/ni/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: AES-NI support.
//! Low-level "hazmat" AES functions: AES-NI support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -10,7 +10,7 @@ use crate::Block;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
Expand All@@ -21,10 +21,20 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
let out = _mm_aesdec_si128(b, k);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let out = _mm_aesimc_si128(b);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}
43 changes: 25 additions & 18 deletions aes/tests/round.rs → aes/tests/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
//! Tests for the raw AES round function.
//! Tests for low-level "hazmat" AES functions.

#![cfg(all(feature = "hazmat", not(feature = "force-soft")))]

use aes::Block;
use hex_literal::hex;

/// Round function tests vectors.
struct TestVector {
struct RoundTestVector {
/// State at start of `round[r]`.
start: [u8; 16],

Expand All@@ -18,75 +18,82 @@ struct TestVector {
}

/// Cipher round function test vectors from FIPS 197 Appendix C.1.
const CIPHER_TEST_VECTORS: &[TestVector] = &[
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("00102030405060708090a0b0c0d0e0f0"),
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"),
output: hex!("89d810e8855ace682d1843d8cb128fe4"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("89d810e8855ace682d1843d8cb128fe4"),
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"),
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"),
output: hex!("fa636a2825b339c940668a3157244d17"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("fa636a2825b339c940668a3157244d17"),
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"),
output: hex!("247240236966b3fa6ed2753288425b6c"),
},
];

/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1.
const EQUIV_INV_CIPHER_TEST_VECTORS: &[TestVector] = &[
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"),
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"),
output: hex!("54d990a16ba09ab596bbf40ea111702f"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("54d990a16ba09ab596bbf40ea111702f"),
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"),
output: hex!("3e1c22c0b6fcbf768da85067f6170495"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("3e1c22c0b6fcbf768da85067f6170495"),
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"),
output: hex!("b458124c68b68a014b99f82e5f15554c"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("b458124c68b68a014b99f82e5f15554c"),
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"),
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"),
},
];

#[test]
fn cipher_fips197_vectors() {
for vector in CIPHER_TEST_VECTORS {
fn cipher_round_fips197_vectors() {
for vector in CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::cipher(&mut block, &vector.k_sch.into());
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[test]
fn equiv_inv_cipher_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_TEST_VECTORS {
fn equiv_inv_cipher_round_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::equiv_inv_cipher(&mut block, &vector.k_sch.into());
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[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"))
}
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' aes: rename `hazmat` module; add `inv_mix_columns` by tarcieri · Pull Request #259 · RustCrypto/block-ciphers · GitHub
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
2 changes: 1 addition & 1 deletion aes/src/armv8.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@
#![allow(clippy::needless_range_loop)]

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

mod decrypt;
mod encrypt;
Expand Down
15 changes: 12 additions & 3 deletions aes/src/armv8/round.rs → aes/src/armv8/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: ARMv8 Cryptography Extensions support.
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -11,7 +11,7 @@ use core::arch::aarch64::*;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -30,7 +30,7 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES equivalent inverse cipher (decrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -45,3 +45,12 @@ pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {

vst1q_u8(block.as_mut_ptr(), state);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
let b = vld1q_u8(block.as_ptr());
let out = vaesimcq_u8(b);
vst1q_u8(block.as_mut_ptr(), out);
}
38 changes: 27 additions & 11 deletions aes/src/round.rs → aes/src/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! ⚠️ Raw AES round function.
//! ⚠️ Low-level "hazmat" AES functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//!
Expand All@@ -14,10 +14,10 @@
use crate::Block;

#[cfg(all(target_arch = "aarch64", feature = "armv8"))]
use crate::armv8::round as intrinsics;
use crate::armv8::hazmat as intrinsics;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::ni::round as intrinsics;
use crate::ni::hazmat as intrinsics;

#[cfg(not(any(
target_arch = "x86_64",
Expand All@@ -43,11 +43,11 @@ cpufeatures::new!(aes_intrinsics, "aes");
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::cipher(block, round_key) };
pub fn cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

Expand All@@ -66,10 +66,26 @@ pub fn cipher(block: &mut Block, round_key: &Block) {
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::equiv_inv_cipher(block, round_key) };
pub fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::equiv_inv_cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

/// ⚠️ AES inverse mix columns function.
///
/// This function is equivalent to the Intel AES-NI `AESIMC` instruction.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn inv_mix_columns(block: &mut Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::inv_mix_columns(block) };
} else {
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}
2 changes: 1 addition & 1 deletion aes/src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,7 +94,7 @@
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(all(feature = "hazmat", not(feature = "force-soft")))]
pub mod round;
pub mod hazmat;

mod soft;

Expand Down
2 changes: 1 addition & 1 deletion aes/src/ni.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ mod aes256;
mod ctr;

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
Expand Down
16 changes: 13 additions & 3 deletions aes/src/ni/round.rs → aes/src/ni/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: AES-NI support.
//! Low-level "hazmat" AES functions: AES-NI support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -10,7 +10,7 @@ use crate::Block;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
Expand All@@ -21,10 +21,20 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
let out = _mm_aesdec_si128(b, k);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let out = _mm_aesimc_si128(b);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}
43 changes: 25 additions & 18 deletions aes/tests/round.rs → aes/tests/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
//! Tests for the raw AES round function.
//! Tests for low-level "hazmat" AES functions.

#![cfg(all(feature = "hazmat", not(feature = "force-soft")))]

use aes::Block;
use hex_literal::hex;

/// Round function tests vectors.
struct TestVector {
struct RoundTestVector {
/// State at start of `round[r]`.
start: [u8; 16],

Expand All@@ -18,75 +18,82 @@ struct TestVector {
}

/// Cipher round function test vectors from FIPS 197 Appendix C.1.
const CIPHER_TEST_VECTORS: &[TestVector] = &[
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("00102030405060708090a0b0c0d0e0f0"),
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"),
output: hex!("89d810e8855ace682d1843d8cb128fe4"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("89d810e8855ace682d1843d8cb128fe4"),
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"),
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"),
output: hex!("fa636a2825b339c940668a3157244d17"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("fa636a2825b339c940668a3157244d17"),
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"),
output: hex!("247240236966b3fa6ed2753288425b6c"),
},
];

/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1.
const EQUIV_INV_CIPHER_TEST_VECTORS: &[TestVector] = &[
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"),
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"),
output: hex!("54d990a16ba09ab596bbf40ea111702f"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("54d990a16ba09ab596bbf40ea111702f"),
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"),
output: hex!("3e1c22c0b6fcbf768da85067f6170495"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("3e1c22c0b6fcbf768da85067f6170495"),
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"),
output: hex!("b458124c68b68a014b99f82e5f15554c"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("b458124c68b68a014b99f82e5f15554c"),
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"),
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"),
},
];

#[test]
fn cipher_fips197_vectors() {
for vector in CIPHER_TEST_VECTORS {
fn cipher_round_fips197_vectors() {
for vector in CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::cipher(&mut block, &vector.k_sch.into());
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[test]
fn equiv_inv_cipher_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_TEST_VECTORS {
fn equiv_inv_cipher_round_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::equiv_inv_cipher(&mut block, &vector.k_sch.into());
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[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"))
}
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' aes: rename `hazmat` module; add `inv_mix_columns` by tarcieri · Pull Request #259 · RustCrypto/block-ciphers · GitHub
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
2 changes: 1 addition & 1 deletion aes/src/armv8.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@
#![allow(clippy::needless_range_loop)]

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

mod decrypt;
mod encrypt;
Expand Down
15 changes: 12 additions & 3 deletions aes/src/armv8/round.rs → aes/src/armv8/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: ARMv8 Cryptography Extensions support.
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -11,7 +11,7 @@ use core::arch::aarch64::*;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -30,7 +30,7 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES equivalent inverse cipher (decrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -45,3 +45,12 @@ pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {

vst1q_u8(block.as_mut_ptr(), state);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
let b = vld1q_u8(block.as_ptr());
let out = vaesimcq_u8(b);
vst1q_u8(block.as_mut_ptr(), out);
}
38 changes: 27 additions & 11 deletions aes/src/round.rs → aes/src/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! ⚠️ Raw AES round function.
//! ⚠️ Low-level "hazmat" AES functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//!
Expand All@@ -14,10 +14,10 @@
use crate::Block;

#[cfg(all(target_arch = "aarch64", feature = "armv8"))]
use crate::armv8::round as intrinsics;
use crate::armv8::hazmat as intrinsics;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::ni::round as intrinsics;
use crate::ni::hazmat as intrinsics;

#[cfg(not(any(
target_arch = "x86_64",
Expand All@@ -43,11 +43,11 @@ cpufeatures::new!(aes_intrinsics, "aes");
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::cipher(block, round_key) };
pub fn cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

Expand All@@ -66,10 +66,26 @@ pub fn cipher(block: &mut Block, round_key: &Block) {
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::equiv_inv_cipher(block, round_key) };
pub fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::equiv_inv_cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

/// ⚠️ AES inverse mix columns function.
///
/// This function is equivalent to the Intel AES-NI `AESIMC` instruction.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn inv_mix_columns(block: &mut Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::inv_mix_columns(block) };
} else {
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}
2 changes: 1 addition & 1 deletion aes/src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,7 +94,7 @@
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(all(feature = "hazmat", not(feature = "force-soft")))]
pub mod round;
pub mod hazmat;

mod soft;

Expand Down
2 changes: 1 addition & 1 deletion aes/src/ni.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ mod aes256;
mod ctr;

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
Expand Down
16 changes: 13 additions & 3 deletions aes/src/ni/round.rs → aes/src/ni/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: AES-NI support.
//! Low-level "hazmat" AES functions: AES-NI support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -10,7 +10,7 @@ use crate::Block;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
Expand All@@ -21,10 +21,20 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
let out = _mm_aesdec_si128(b, k);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let out = _mm_aesimc_si128(b);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}
43 changes: 25 additions & 18 deletions aes/tests/round.rs → aes/tests/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
//! Tests for the raw AES round function.
//! Tests for low-level "hazmat" AES functions.

#![cfg(all(feature = "hazmat", not(feature = "force-soft")))]

use aes::Block;
use hex_literal::hex;

/// Round function tests vectors.
struct TestVector {
struct RoundTestVector {
/// State at start of `round[r]`.
start: [u8; 16],

Expand All@@ -18,75 +18,82 @@ struct TestVector {
}

/// Cipher round function test vectors from FIPS 197 Appendix C.1.
const CIPHER_TEST_VECTORS: &[TestVector] = &[
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("00102030405060708090a0b0c0d0e0f0"),
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"),
output: hex!("89d810e8855ace682d1843d8cb128fe4"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("89d810e8855ace682d1843d8cb128fe4"),
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"),
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"),
output: hex!("fa636a2825b339c940668a3157244d17"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("fa636a2825b339c940668a3157244d17"),
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"),
output: hex!("247240236966b3fa6ed2753288425b6c"),
},
];

/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1.
const EQUIV_INV_CIPHER_TEST_VECTORS: &[TestVector] = &[
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"),
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"),
output: hex!("54d990a16ba09ab596bbf40ea111702f"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("54d990a16ba09ab596bbf40ea111702f"),
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"),
output: hex!("3e1c22c0b6fcbf768da85067f6170495"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("3e1c22c0b6fcbf768da85067f6170495"),
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"),
output: hex!("b458124c68b68a014b99f82e5f15554c"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("b458124c68b68a014b99f82e5f15554c"),
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"),
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"),
},
];

#[test]
fn cipher_fips197_vectors() {
for vector in CIPHER_TEST_VECTORS {
fn cipher_round_fips197_vectors() {
for vector in CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::cipher(&mut block, &vector.k_sch.into());
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[test]
fn equiv_inv_cipher_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_TEST_VECTORS {
fn equiv_inv_cipher_round_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::equiv_inv_cipher(&mut block, &vector.k_sch.into());
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[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"))
}
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' aes: rename `hazmat` module; add `inv_mix_columns` by tarcieri · Pull Request #259 · RustCrypto/block-ciphers · GitHub
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
2 changes: 1 addition & 1 deletion aes/src/armv8.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@
#![allow(clippy::needless_range_loop)]

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

mod decrypt;
mod encrypt;
Expand Down
15 changes: 12 additions & 3 deletions aes/src/armv8/round.rs → aes/src/armv8/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: ARMv8 Cryptography Extensions support.
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -11,7 +11,7 @@ use core::arch::aarch64::*;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -30,7 +30,7 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES equivalent inverse cipher (decrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -45,3 +45,12 @@ pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {

vst1q_u8(block.as_mut_ptr(), state);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
let b = vld1q_u8(block.as_ptr());
let out = vaesimcq_u8(b);
vst1q_u8(block.as_mut_ptr(), out);
}
38 changes: 27 additions & 11 deletions aes/src/round.rs → aes/src/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! ⚠️ Raw AES round function.
//! ⚠️ Low-level "hazmat" AES functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//!
Expand All@@ -14,10 +14,10 @@
use crate::Block;

#[cfg(all(target_arch = "aarch64", feature = "armv8"))]
use crate::armv8::round as intrinsics;
use crate::armv8::hazmat as intrinsics;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::ni::round as intrinsics;
use crate::ni::hazmat as intrinsics;

#[cfg(not(any(
target_arch = "x86_64",
Expand All@@ -43,11 +43,11 @@ cpufeatures::new!(aes_intrinsics, "aes");
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::cipher(block, round_key) };
pub fn cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

Expand All@@ -66,10 +66,26 @@ pub fn cipher(block: &mut Block, round_key: &Block) {
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::equiv_inv_cipher(block, round_key) };
pub fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::equiv_inv_cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

/// ⚠️ AES inverse mix columns function.
///
/// This function is equivalent to the Intel AES-NI `AESIMC` instruction.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn inv_mix_columns(block: &mut Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::inv_mix_columns(block) };
} else {
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}
2 changes: 1 addition & 1 deletion aes/src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,7 +94,7 @@
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(all(feature = "hazmat", not(feature = "force-soft")))]
pub mod round;
pub mod hazmat;

mod soft;

Expand Down
2 changes: 1 addition & 1 deletion aes/src/ni.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ mod aes256;
mod ctr;

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
Expand Down
16 changes: 13 additions & 3 deletions aes/src/ni/round.rs → aes/src/ni/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: AES-NI support.
//! Low-level "hazmat" AES functions: AES-NI support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -10,7 +10,7 @@ use crate::Block;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
Expand All@@ -21,10 +21,20 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
let out = _mm_aesdec_si128(b, k);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let out = _mm_aesimc_si128(b);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}
43 changes: 25 additions & 18 deletions aes/tests/round.rs → aes/tests/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
//! Tests for the raw AES round function.
//! Tests for low-level "hazmat" AES functions.

#![cfg(all(feature = "hazmat", not(feature = "force-soft")))]

use aes::Block;
use hex_literal::hex;

/// Round function tests vectors.
struct TestVector {
struct RoundTestVector {
/// State at start of `round[r]`.
start: [u8; 16],

Expand All@@ -18,75 +18,82 @@ struct TestVector {
}

/// Cipher round function test vectors from FIPS 197 Appendix C.1.
const CIPHER_TEST_VECTORS: &[TestVector] = &[
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("00102030405060708090a0b0c0d0e0f0"),
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"),
output: hex!("89d810e8855ace682d1843d8cb128fe4"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("89d810e8855ace682d1843d8cb128fe4"),
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"),
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"),
output: hex!("fa636a2825b339c940668a3157244d17"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("fa636a2825b339c940668a3157244d17"),
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"),
output: hex!("247240236966b3fa6ed2753288425b6c"),
},
];

/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1.
const EQUIV_INV_CIPHER_TEST_VECTORS: &[TestVector] = &[
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"),
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"),
output: hex!("54d990a16ba09ab596bbf40ea111702f"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("54d990a16ba09ab596bbf40ea111702f"),
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"),
output: hex!("3e1c22c0b6fcbf768da85067f6170495"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("3e1c22c0b6fcbf768da85067f6170495"),
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"),
output: hex!("b458124c68b68a014b99f82e5f15554c"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("b458124c68b68a014b99f82e5f15554c"),
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"),
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"),
},
];

#[test]
fn cipher_fips197_vectors() {
for vector in CIPHER_TEST_VECTORS {
fn cipher_round_fips197_vectors() {
for vector in CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::cipher(&mut block, &vector.k_sch.into());
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[test]
fn equiv_inv_cipher_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_TEST_VECTORS {
fn equiv_inv_cipher_round_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::equiv_inv_cipher(&mut block, &vector.k_sch.into());
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[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"))
}
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' aes: rename `hazmat` module; add `inv_mix_columns` by tarcieri · Pull Request #259 · RustCrypto/block-ciphers · GitHub
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
2 changes: 1 addition & 1 deletion aes/src/armv8.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@
#![allow(clippy::needless_range_loop)]

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

mod decrypt;
mod encrypt;
Expand Down
15 changes: 12 additions & 3 deletions aes/src/armv8/round.rs → aes/src/armv8/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: ARMv8 Cryptography Extensions support.
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -11,7 +11,7 @@ use core::arch::aarch64::*;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -30,7 +30,7 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES equivalent inverse cipher (decrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -45,3 +45,12 @@ pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {

vst1q_u8(block.as_mut_ptr(), state);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
let b = vld1q_u8(block.as_ptr());
let out = vaesimcq_u8(b);
vst1q_u8(block.as_mut_ptr(), out);
}
38 changes: 27 additions & 11 deletions aes/src/round.rs → aes/src/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! ⚠️ Raw AES round function.
//! ⚠️ Low-level "hazmat" AES functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//!
Expand All@@ -14,10 +14,10 @@
use crate::Block;

#[cfg(all(target_arch = "aarch64", feature = "armv8"))]
use crate::armv8::round as intrinsics;
use crate::armv8::hazmat as intrinsics;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::ni::round as intrinsics;
use crate::ni::hazmat as intrinsics;

#[cfg(not(any(
target_arch = "x86_64",
Expand All@@ -43,11 +43,11 @@ cpufeatures::new!(aes_intrinsics, "aes");
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::cipher(block, round_key) };
pub fn cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

Expand All@@ -66,10 +66,26 @@ pub fn cipher(block: &mut Block, round_key: &Block) {
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::equiv_inv_cipher(block, round_key) };
pub fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::equiv_inv_cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

/// ⚠️ AES inverse mix columns function.
///
/// This function is equivalent to the Intel AES-NI `AESIMC` instruction.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn inv_mix_columns(block: &mut Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::inv_mix_columns(block) };
} else {
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}
2 changes: 1 addition & 1 deletion aes/src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,7 +94,7 @@
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(all(feature = "hazmat", not(feature = "force-soft")))]
pub mod round;
pub mod hazmat;

mod soft;

Expand Down
2 changes: 1 addition & 1 deletion aes/src/ni.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ mod aes256;
mod ctr;

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
Expand Down
16 changes: 13 additions & 3 deletions aes/src/ni/round.rs → aes/src/ni/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: AES-NI support.
//! Low-level "hazmat" AES functions: AES-NI support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -10,7 +10,7 @@ use crate::Block;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
Expand All@@ -21,10 +21,20 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
let out = _mm_aesdec_si128(b, k);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let out = _mm_aesimc_si128(b);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}
43 changes: 25 additions & 18 deletions aes/tests/round.rs → aes/tests/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
//! Tests for the raw AES round function.
//! Tests for low-level "hazmat" AES functions.

#![cfg(all(feature = "hazmat", not(feature = "force-soft")))]

use aes::Block;
use hex_literal::hex;

/// Round function tests vectors.
struct TestVector {
struct RoundTestVector {
/// State at start of `round[r]`.
start: [u8; 16],

Expand All@@ -18,75 +18,82 @@ struct TestVector {
}

/// Cipher round function test vectors from FIPS 197 Appendix C.1.
const CIPHER_TEST_VECTORS: &[TestVector] = &[
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("00102030405060708090a0b0c0d0e0f0"),
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"),
output: hex!("89d810e8855ace682d1843d8cb128fe4"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("89d810e8855ace682d1843d8cb128fe4"),
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"),
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"),
output: hex!("fa636a2825b339c940668a3157244d17"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("fa636a2825b339c940668a3157244d17"),
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"),
output: hex!("247240236966b3fa6ed2753288425b6c"),
},
];

/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1.
const EQUIV_INV_CIPHER_TEST_VECTORS: &[TestVector] = &[
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"),
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"),
output: hex!("54d990a16ba09ab596bbf40ea111702f"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("54d990a16ba09ab596bbf40ea111702f"),
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"),
output: hex!("3e1c22c0b6fcbf768da85067f6170495"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("3e1c22c0b6fcbf768da85067f6170495"),
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"),
output: hex!("b458124c68b68a014b99f82e5f15554c"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("b458124c68b68a014b99f82e5f15554c"),
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"),
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"),
},
];

#[test]
fn cipher_fips197_vectors() {
for vector in CIPHER_TEST_VECTORS {
fn cipher_round_fips197_vectors() {
for vector in CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::cipher(&mut block, &vector.k_sch.into());
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[test]
fn equiv_inv_cipher_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_TEST_VECTORS {
fn equiv_inv_cipher_round_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::equiv_inv_cipher(&mut block, &vector.k_sch.into());
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[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"))
}
, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); aes: rename `hazmat` module; add `inv_mix_columns` by tarcieri · Pull Request #259 · RustCrypto/block-ciphers · GitHub
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
2 changes: 1 addition & 1 deletion aes/src/armv8.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,7 +10,7 @@
#![allow(clippy::needless_range_loop)]

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

mod decrypt;
mod encrypt;
Expand Down
15 changes: 12 additions & 3 deletions aes/src/armv8/round.rs → aes/src/armv8/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: ARMv8 Cryptography Extensions support.
//! Low-level "hazmat" AES functions: ARMv8 Cryptography Extensions support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -11,7 +11,7 @@ use core::arch::aarch64::*;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -30,7 +30,7 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES equivalent inverse cipher (decrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
let b = vld1q_u8(block.as_ptr());
let k = vld1q_u8(round_key.as_ptr());

Expand All@@ -45,3 +45,12 @@ pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {

vst1q_u8(block.as_mut_ptr(), state);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "crypto")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
let b = vld1q_u8(block.as_ptr());
let out = vaesimcq_u8(b);
vst1q_u8(block.as_mut_ptr(), out);
}
38 changes: 27 additions & 11 deletions aes/src/round.rs → aes/src/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! ⚠️ Raw AES round function.
//! ⚠️ Low-level "hazmat" AES functions.
//!
//! # ☢️️ WARNING: HAZARDOUS API ☢️
//!
Expand All@@ -14,10 +14,10 @@
use crate::Block;

#[cfg(all(target_arch = "aarch64", feature = "armv8"))]
use crate::armv8::round as intrinsics;
use crate::armv8::hazmat as intrinsics;

#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
use crate::ni::round as intrinsics;
use crate::ni::hazmat as intrinsics;

#[cfg(not(any(
target_arch = "x86_64",
Expand All@@ -43,11 +43,11 @@ cpufeatures::new!(aes_intrinsics, "aes");
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::cipher(block, round_key) };
pub fn cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

Expand All@@ -66,10 +66,26 @@ pub fn cipher(block: &mut Block, round_key: &Block) {
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
if aes_intrinsics::init_get().1 {
unsafe { intrinsics::equiv_inv_cipher(block, round_key) };
pub fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::equiv_inv_cipher_round(block, round_key) };
} else {
todo!("soft fallback for the raw AES round function API is not yet implemented");
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}

/// ⚠️ AES inverse mix columns function.
///
/// This function is equivalent to the Intel AES-NI `AESIMC` instruction.
///
/// # ☢️️ WARNING: HAZARDOUS API ☢️
///
/// Use this function with great care! See the [module-level documentation][crate::round]
/// for more information.
pub fn inv_mix_columns(block: &mut Block) {
if aes_intrinsics::get() {
unsafe { intrinsics::inv_mix_columns(block) };
} else {
todo!("soft fallback for AES hazmat functions is not yet implemented");
}
}
2 changes: 1 addition & 1 deletion aes/src/lib.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -94,7 +94,7 @@
#![warn(missing_docs, rust_2018_idioms)]

#[cfg(all(feature = "hazmat", not(feature = "force-soft")))]
pub mod round;
pub mod hazmat;

mod soft;

Expand Down
2 changes: 1 addition & 1 deletion aes/src/ni.rs
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ mod aes256;
mod ctr;

#[cfg(feature = "hazmat")]
pub(crate) mod round;
pub(crate) mod hazmat;

#[cfg(target_arch = "x86")]
use core::arch::x86 as arch;
Expand Down
16 changes: 13 additions & 3 deletions aes/src/ni/round.rs → aes/src/ni/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
//! Raw AES round function: AES-NI support.
//! Low-level "hazmat" AES functions: AES-NI support.
//!
//! Note: this isn't actually used in the `Aes128`/`Aes192`/`Aes256`
//! implementations in this crate, but instead provides raw AES-NI accelerated
Expand All@@ -10,7 +10,7 @@ use crate::Block;
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
Expand All@@ -21,10 +21,20 @@ pub(crate) unsafe fn cipher(block: &mut Block, round_key: &Block) {
/// AES cipher (encrypt) round function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn equiv_inv_cipher(block: &mut Block, round_key: &Block) {
pub(crate) unsafe fn equiv_inv_cipher_round(block: &mut Block, round_key: &Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let k = _mm_loadu_si128(round_key.as_ptr() as *const __m128i);
let out = _mm_aesdec_si128(b, k);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}

/// AES inverse mix columns function.
#[allow(clippy::cast_ptr_alignment)]
#[target_feature(enable = "aes")]
pub(crate) unsafe fn inv_mix_columns(block: &mut Block) {
// Safety: `loadu` and `storeu` support unaligned access
let b = _mm_loadu_si128(block.as_ptr() as *const __m128i);
let out = _mm_aesimc_si128(b);
_mm_storeu_si128(block.as_mut_ptr() as *mut __m128i, out);
}
43 changes: 25 additions & 18 deletions aes/tests/round.rs → aes/tests/hazmat.rs
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
//! Tests for the raw AES round function.
//! Tests for low-level "hazmat" AES functions.

#![cfg(all(feature = "hazmat", not(feature = "force-soft")))]

use aes::Block;
use hex_literal::hex;

/// Round function tests vectors.
struct TestVector {
struct RoundTestVector {
/// State at start of `round[r]`.
start: [u8; 16],

Expand All@@ -18,75 +18,82 @@ struct TestVector {
}

/// Cipher round function test vectors from FIPS 197 Appendix C.1.
const CIPHER_TEST_VECTORS: &[TestVector] = &[
const CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("00102030405060708090a0b0c0d0e0f0"),
k_sch: hex!("d6aa74fdd2af72fadaa678f1d6ab76fe"),
output: hex!("89d810e8855ace682d1843d8cb128fe4"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("89d810e8855ace682d1843d8cb128fe4"),
k_sch: hex!("b692cf0b643dbdf1be9bc5006830b3fe"),
output: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("4915598f55e5d7a0daca94fa1f0a63f7"),
k_sch: hex!("b6ff744ed2c2c9bf6c590cbf0469bf41"),
output: hex!("fa636a2825b339c940668a3157244d17"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("fa636a2825b339c940668a3157244d17"),
k_sch: hex!("47f7f7bc95353e03f96c32bcfd058dfd"),
output: hex!("247240236966b3fa6ed2753288425b6c"),
},
];

/// Equivalent Inverse Cipher round function test vectors from FIPS 197 Appendix C.1.
const EQUIV_INV_CIPHER_TEST_VECTORS: &[TestVector] = &[
const EQUIV_INV_CIPHER_ROUND_TEST_VECTORS: &[RoundTestVector] = &[
// round 1
TestVector {
RoundTestVector {
start: hex!("7ad5fda789ef4e272bca100b3d9ff59f"),
k_sch: hex!("13aa29be9c8faff6f770f58000f7bf03"),
output: hex!("54d990a16ba09ab596bbf40ea111702f"),
},
// round 2
TestVector {
RoundTestVector {
start: hex!("54d990a16ba09ab596bbf40ea111702f"),
k_sch: hex!("1362a4638f2586486bff5a76f7874a83"),
output: hex!("3e1c22c0b6fcbf768da85067f6170495"),
},
// round 3
TestVector {
RoundTestVector {
start: hex!("3e1c22c0b6fcbf768da85067f6170495"),
k_sch: hex!("8d82fc749c47222be4dadc3e9c7810f5"),
output: hex!("b458124c68b68a014b99f82e5f15554c"),
},
// round 4
TestVector {
RoundTestVector {
start: hex!("b458124c68b68a014b99f82e5f15554c"),
k_sch: hex!("72e3098d11c5de5f789dfe1578a2cccb"),
output: hex!("e8dab6901477d4653ff7f5e2e747dd4f"),
},
];

#[test]
fn cipher_fips197_vectors() {
for vector in CIPHER_TEST_VECTORS {
fn cipher_round_fips197_vectors() {
for vector in CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::cipher(&mut block, &vector.k_sch.into());
aes::hazmat::cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[test]
fn equiv_inv_cipher_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_TEST_VECTORS {
fn equiv_inv_cipher_round_fips197_vectors() {
for vector in EQUIV_INV_CIPHER_ROUND_TEST_VECTORS {
let mut block = Block::from(vector.start);
aes::round::equiv_inv_cipher(&mut block, &vector.k_sch.into());
aes::hazmat::equiv_inv_cipher_round(&mut block, &vector.k_sch.into());
assert_eq!(block.as_slice(), &vector.output);
}
}

#[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"))
}