Skip to content

Commit 5b2ffa7

Browse files
committed
aes-gcm: Remove htable_new! macro.
Minimize the scope of `unsafe` in `HTable` construction.
1 parent 5a61573 commit 5b2ffa7

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

src/aead/gcm/clmul.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
target_arch = "x86_64"
1919
))]
2020

21-
use super::{ffi::{BLOCK_LEN, KeyValue}, HTable, UpdateBlock, Xi};
21+
use super::{
22+
ffi::{KeyValue, BLOCK_LEN},
23+
HTable, UpdateBlock, Xi,
24+
};
2225
use crate::cpu;
2326
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
2427
use {super::UpdateBlocks, crate::polyfill::slice::AsChunks};
@@ -31,8 +34,11 @@ pub struct Key {
3134
impl Key {
3235
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
3336
pub(in super::super) fn new(value: KeyValue, _cpu: cpu::aarch64::PMull) -> Self {
37+
prefixed_extern! {
38+
fn gcm_init_clmul(HTable: *mut HTable, h: &KeyValue);
39+
}
3440
Self {
35-
h_table: unsafe { htable_new!(gcm_init_clmul, value) },
41+
h_table: HTable::new(|table| unsafe { gcm_init_clmul(table, &value) }),
3642
}
3743
}
3844

@@ -41,8 +47,11 @@ impl Key {
4147
value: KeyValue,
4248
_cpu: (cpu::intel::ClMul, cpu::intel::Ssse3),
4349
) -> Self {
50+
prefixed_extern! {
51+
fn gcm_init_clmul(HTable: *mut HTable, h: &KeyValue);
52+
}
4453
Self {
45-
h_table: unsafe { htable_new!(gcm_init_clmul, value) },
54+
h_table: HTable::new(|htable| unsafe { gcm_init_clmul(htable, &value) }),
4655
}
4756
}
4857

@@ -52,8 +61,11 @@ impl Key {
5261
value: KeyValue,
5362
_cpu: (cpu::intel::ClMul, cpu::intel::Ssse3),
5463
) -> Self {
64+
prefixed_extern! {
65+
fn gcm_init_clmul(HTable: *mut HTable, h: &KeyValue);
66+
}
5567
Self {
56-
h_table: unsafe { htable_new!(gcm_init_clmul, value) },
68+
h_table: HTable::new(|table| unsafe { gcm_init_clmul(table, &value) }),
5769
}
5870
}
5971

src/aead/gcm/clmulavxmovbe.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ impl Key {
2828
value: KeyValue,
2929
_required_cpu_features: (intel::ClMul, intel::Avx, intel::Movbe),
3030
) -> Self {
31+
prefixed_extern! {
32+
fn gcm_init_avx(HTable: *mut HTable, h: &KeyValue);
33+
}
3134
Self {
32-
h_table: unsafe { htable_new!(gcm_init_avx, value) },
35+
h_table: HTable::new(|table| unsafe { gcm_init_avx(table, &value) }),
3336
}
3437
}
3538

src/aead/gcm/ffi.rs

+3-21
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,6 @@ pub(in super::super) const BLOCK_LEN: usize = 16;
2121
pub(in super::super) type Block = [u8; BLOCK_LEN];
2222
pub(super) const ZERO_BLOCK: Block = [0u8; BLOCK_LEN];
2323

24-
#[cfg(any(
25-
all(target_arch = "aarch64", target_endian = "little"),
26-
all(target_arch = "arm", target_endian = "little"),
27-
target_arch = "x86",
28-
target_arch = "x86_64"
29-
))]
30-
macro_rules! htable_new {
31-
( $name:ident, $value:expr $(,)? ) => {{
32-
use crate::aead::gcm::ffi::HTable;
33-
prefixed_extern! {
34-
fn $name(HTable: &mut HTable, h: &[u64; 2]);
35-
}
36-
HTable::new($name, $value)
37-
}};
38-
}
39-
4024
/// SAFETY:
4125
/// * The function `$name` must meet the contract of the `f` paramweter of
4226
/// `ghash()`.
@@ -61,6 +45,7 @@ macro_rules! ghash {
6145
}};
6246
}
6347

48+
#[repr(transparent)]
6449
pub(in super::super) struct KeyValue([u64; 2]);
6550

6651
impl KeyValue {
@@ -84,14 +69,11 @@ impl KeyValue {
8469
target_arch = "x86_64"
8570
))]
8671
impl HTable {
87-
pub(super) unsafe fn new(
88-
init: unsafe extern "C" fn(HTable: &mut HTable, &[u64; 2]),
89-
value: KeyValue,
90-
) -> Self {
72+
pub(super) fn new(init: impl FnOnce(&mut HTable)) -> Self {
9173
let mut r = Self {
9274
Htable: [U128 { hi: 0, lo: 0 }; HTABLE_LEN],
9375
};
94-
unsafe { init(&mut r, &value.0) };
76+
init(&mut r);
9577
r
9678
}
9779

src/aead/gcm/neon.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,21 @@ pub struct Key {
2828
impl Key {
2929
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
3030
pub(in super::super) fn new(value: KeyValue, _cpu: cpu::aarch64::Neon) -> Self {
31+
prefixed_extern! {
32+
fn gcm_init_neon(HTable: *mut HTable, h: &KeyValue);
33+
}
3134
Self {
32-
h_table: unsafe { htable_new!(gcm_init_neon, value) },
35+
h_table: HTable::new(|table| unsafe { gcm_init_neon(table, &value) }),
3336
}
3437
}
3538

3639
#[cfg(all(target_arch = "arm", target_endian = "little"))]
3740
pub(in super::super) fn new(value: KeyValue, _cpu: cpu::arm::Neon) -> Self {
41+
prefixed_extern! {
42+
fn gcm_init_neon(HTable: *mut HTable, h: &KeyValue);
43+
}
3844
Self {
39-
h_table: unsafe { htable_new!(gcm_init_neon, value) },
45+
h_table: HTable::new(|table| unsafe { gcm_init_neon(table, &value) }),
4046
}
4147
}
4248
}

src/aead/gcm/vclmulavx2.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ pub struct Key {
2828

2929
impl Key {
3030
pub(in super::super) fn new(value: KeyValue, _cpu: (Avx2, VAesClmul)) -> Self {
31+
prefixed_extern! {
32+
fn gcm_init_vpclmulqdq_avx2(HTable: *mut HTable, h: &KeyValue);
33+
}
3134
Self {
32-
h_table: unsafe { htable_new!(gcm_init_vpclmulqdq_avx2, value) },
35+
h_table: HTable::new(|table| unsafe { gcm_init_vpclmulqdq_avx2(table, &value) }),
3336
}
3437
}
3538

0 commit comments

Comments
 (0)