Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite aes_nohw.c to aes_nohw.rs. #2070

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
aes: Oxidize aes_nohw batch_get.
  • Loading branch information
briansmith committed May 23, 2024
commit f26c4f218d2326db0dfcd762e417d604af201429
1 change: 0 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,6 @@ fn prefix_all_symbols(pp: char, prefix_prefix: &str, prefix: &str) -> String {
"aes_nohw_compact_block",
"aes_nohw_uncompact_block",
"aes_nohw_sub_bytes",
"aes_nohw_batch_get",
"aes_nohw_batch_set",
"aes_nohw_transpose",
"aes_nohw_mix_columns",
Expand Down
32 changes: 0 additions & 32 deletions crypto/fipsmodule/aes/aes_nohw.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,6 @@ typedef struct {
#endif
}

// aes_nohw_batch_get writes the |i|th block of |batch| to |out|. |batch| is in
// compact form.
/*static inline*/ void aes_nohw_batch_get(const AES_NOHW_BATCH *batch,
aes_word_t out[AES_NOHW_BLOCK_WORDS],
size_t i) {
dev_assert_secret(i < AES_NOHW_BATCH_SIZE);
#if defined(OPENSSL_64_BIT)
out[0] = batch->w[i];
out[1] = batch->w[i + 4];
#else
out[0] = batch->w[i];
out[1] = batch->w[i + 2];
out[2] = batch->w[i + 4];
out[3] = batch->w[i + 6];
#endif
}

// aes_nohw_delta_swap returns |a| with bits |a & mask| and
// |a & (mask << shift)| swapped. |mask| and |mask << shift| may not overlap.
static inline aes_word_t aes_nohw_delta_swap(aes_word_t a, aes_word_t mask,
Expand Down Expand Up @@ -400,21 +383,6 @@ void aes_nohw_transpose(AES_NOHW_BATCH *batch) {
#endif
}

// aes_nohw_to_batch writes the first |num_blocks| blocks in |batch| to |out|.
// |num_blocks| must be at most |AES_NOHW_BATCH|.
void aes_nohw_from_batch(uint8_t *out, size_t num_blocks,
const AES_NOHW_BATCH *batch) {
AES_NOHW_BATCH copy = *batch;
aes_nohw_transpose(&copy);

debug_assert_nonsecret(num_blocks <= AES_NOHW_BATCH_SIZE);
for (size_t i = 0; i < num_blocks; i++) {
aes_word_t block[AES_NOHW_BLOCK_WORDS];
aes_nohw_batch_get(&copy, block, i);
aes_nohw_uncompact_block(out + 16 * i, block);
}
}

// AES round steps.

void aes_nohw_sub_bytes(AES_NOHW_BATCH *batch) {
Expand Down
20 changes: 11 additions & 9 deletions src/aead/aes/aes_nohw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use super::{Counter, KeyBytes, AES_KEY, BLOCK_LEN, MAX_ROUNDS};
use crate::{
c, constant_time,
constant_time,
polyfill::{self, usize_from_u32, ArraySplitMap as _},
};
use core::{array, mem::MaybeUninit, ops::RangeFrom};
Expand Down Expand Up @@ -90,16 +90,18 @@ impl Batch {
unsafe { aes_nohw_batch_set(self, input, i) }
}

// aes_nohw_batch_get writes the |i|th block of |batch| to |out|. |batch| is in
// compact form.
fn get(&self, i: usize) -> [Word; BLOCK_WORDS] {
assert!(i < self.w.len());
prefixed_extern! {
fn aes_nohw_batch_get(batch: &Batch, out: *mut [Word; BLOCK_WORDS], i: c::size_t);
}
let mut out = MaybeUninit::uninit();
unsafe {
aes_nohw_batch_get(self, out.as_mut_ptr(), i);
out.assume_init()
}
array::from_fn(|j| {
#[cfg(target_pointer_width = "64")]
const STRIDE: usize = 4;
#[cfg(target_pointer_width = "32")]
const STRIDE: usize = 2;

self.w[i + (j * STRIDE)]
})
}

fn sub_bytes(&mut self) {
Expand Down