Skip to content

Commit e9e4511

Browse files
authored
aes: Add some SAFETY comments to the aes::ni module (#288)
Suggested during review of `aes-gcm` for inclusion in Fuchsia OS: https://fuchsia-review.googlesource.com/c/fuchsia/+/585023
1 parent 859ca00 commit e9e4511

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

aes/src/ni/aes192/expand.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ macro_rules! shuffle {
3737
#[inline(always)]
3838
pub(super) fn expand(key: &[u8; 24]) -> (RoundKeys, RoundKeys) {
3939
unsafe {
40+
// SAFETY: `RoundKeys` is a `[__m128i; 13]` which can be initialized
41+
// with all zeroes.
4042
let mut enc_keys: RoundKeys = mem::zeroed();
4143
let mut dec_keys: RoundKeys = mem::zeroed();
4244

aes/src/ni/aes256/expand.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ macro_rules! expand_round_last {
6262

6363
#[inline(always)]
6464
pub(super) fn expand(key: &[u8; 32]) -> (RoundKeys, RoundKeys) {
65-
// Safety: `loadu` and `storeu` support unaligned access
65+
// SAFETY:
66+
// - `RoundKeys` is a `[__m128i; 15]` which can be initialized with all zeroes.
67+
// - `loadu` and `storeu` support unaligned access
6668
#[allow(clippy::cast_ptr_alignment)]
6769
unsafe {
6870
let mut enc_keys: RoundKeys = mem::zeroed();

aes/src/ni/ctr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ macro_rules! impl_ctr {
8383
#[inline(always)]
8484
fn gen_block(&mut self) {
8585
let block = self.cipher.encrypt(swap_bytes(self.ctr));
86+
// SAFETY: All three expansions of this macro have a `$cipher` whose
87+
// `encrypt(...)` method returns an `__m128i`, and `BLOCK_SIZE == 16`.
8688
self.block = unsafe { mem::transmute(block) }
8789
}
8890

@@ -96,6 +98,7 @@ macro_rules! impl_ctr {
9698
#[inline(always)]
9799
fn next_block8(&mut self) -> [__m128i; 8] {
98100
let mut ctr = self.ctr;
101+
// SAFETY: `[__m128i; 8]` can be initialized with all zeroes.
99102
let mut block8: [__m128i; 8] = unsafe { mem::zeroed() };
100103
for i in 0..8 {
101104
block8[i] = swap_bytes(ctr);

0 commit comments

Comments
 (0)