Skip to content

Commit 082908d

Browse files
committed
aes-gcm: Remove htable_new! macro.
Minimize the scope of `unsafe` in `HTable` construction.
1 parent 97afaf4 commit 082908d

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

src/aead/gcm/clmul.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ pub struct Key {
3434
impl Key {
3535
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
3636
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+
}
3740
Self {
38-
h_table: unsafe { htable_new!(gcm_init_clmul, value) },
41+
h_table: HTable::new(|table| unsafe { gcm_init_clmul(table, value) }),
3942
}
4043
}
4144

@@ -44,8 +47,11 @@ impl Key {
4447
value: &KeyValue,
4548
_cpu: (cpu::intel::ClMul, cpu::intel::Ssse3),
4649
) -> Self {
50+
prefixed_extern! {
51+
fn gcm_init_clmul(HTable: *mut HTable, h: &KeyValue);
52+
}
4753
Self {
48-
h_table: unsafe { htable_new!(gcm_init_clmul, value) },
54+
h_table: HTable::new(|htable| unsafe { gcm_init_clmul(htable, value) }),
4955
}
5056
}
5157

@@ -55,8 +61,11 @@ impl Key {
5561
value: &KeyValue,
5662
_cpu: (cpu::intel::ClMul, cpu::intel::Ssse3),
5763
) -> Self {
64+
prefixed_extern! {
65+
fn gcm_init_clmul(HTable: *mut HTable, h: &KeyValue);
66+
}
5867
Self {
59-
h_table: unsafe { htable_new!(gcm_init_clmul, value) },
68+
h_table: HTable::new(|table| unsafe { gcm_init_clmul(table, value) }),
6069
}
6170
}
6271

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

+2-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: &KeyValue);
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()`.
@@ -85,14 +69,11 @@ impl KeyValue {
8569
target_arch = "x86_64"
8670
))]
8771
impl HTable {
88-
pub(super) unsafe fn new(
89-
init: unsafe extern "C" fn(HTable: &mut HTable, &KeyValue),
90-
value: &KeyValue,
91-
) -> Self {
72+
pub(super) fn new(init: impl FnOnce(&mut HTable)) -> Self {
9273
let mut r = Self {
9374
Htable: [U128 { hi: 0, lo: 0 }; HTABLE_LEN],
9475
};
95-
unsafe { init(&mut r, &value) };
76+
init(&mut r);
9677
r
9778
}
9879

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)