Skip to content

Commit 1fafc32

Browse files
committed
aes_gcm: Remove ghash! macro.
The macro wasn't doing much. This minimizes the scope of `unsafe` and paves the way for some future improvements.
1 parent 78b5720 commit 1fafc32

File tree

5 files changed

+71
-59
lines changed

5 files changed

+71
-59
lines changed

src/aead/gcm/clmul.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ impl UpdateBlock for Key {
9494
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
9595
impl UpdateBlocks for Key {
9696
fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, { BLOCK_LEN }>) {
97-
unsafe { ghash!(gcm_ghash_clmul, xi, &self.h_table, input) }
97+
prefixed_extern! {
98+
fn gcm_ghash_clmul(
99+
xi: &mut Xi,
100+
Htable: &HTable,
101+
inp: *const u8,
102+
len: crate::c::NonZero_size_t,
103+
);
104+
}
105+
let htable = &self.h_table;
106+
super::ffi::with_non_dangling_ptr(input, |input, len| unsafe {
107+
gcm_ghash_clmul(xi, htable, input, len)
108+
})
98109
}
99110
}

src/aead/gcm/clmulavxmovbe.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
#![cfg(target_arch = "x86_64")]
1616

17-
use super::{HTable, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN};
18-
use crate::{cpu::intel, polyfill::slice::AsChunks};
17+
use super::{ffi, HTable, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN};
18+
use crate::{c, cpu::intel, polyfill::slice::AsChunks};
1919

2020
#[derive(Clone)]
2121
pub struct Key {
@@ -49,6 +49,17 @@ impl UpdateBlock for Key {
4949

5050
impl UpdateBlocks for Key {
5151
fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, BLOCK_LEN>) {
52-
unsafe { ghash!(gcm_ghash_avx, xi, self.inner(), input) }
52+
prefixed_extern! {
53+
fn gcm_ghash_avx(
54+
xi: &mut Xi,
55+
Htable: &HTable,
56+
inp: *const u8,
57+
len: c::NonZero_size_t,
58+
);
59+
}
60+
let htable = self.inner();
61+
ffi::with_non_dangling_ptr(input, |input, len| unsafe {
62+
gcm_ghash_avx(xi, htable, input, len)
63+
})
5364
}
5465
}

src/aead/gcm/ffi.rs

+14-50
Original file line numberDiff line numberDiff line change
@@ -21,30 +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-
/// SAFETY:
25-
/// * The function `$name` must meet the contract of the `f` paramweter of
26-
/// `ghash()`.
27-
#[cfg(any(
28-
all(target_arch = "aarch64", target_endian = "little"),
29-
all(target_arch = "arm", target_endian = "little"),
30-
target_arch = "x86",
31-
target_arch = "x86_64"
32-
))]
33-
macro_rules! ghash {
34-
( $name:ident, $xi:expr, $h_table:expr, $input:expr $(,)? ) => {{
35-
use crate::aead::gcm::ffi::{HTable, Xi};
36-
prefixed_extern! {
37-
fn $name(
38-
xi: &mut Xi,
39-
Htable: &HTable,
40-
inp: *const u8,
41-
len: crate::c::NonZero_size_t,
42-
);
43-
}
44-
$h_table.ghash($name, $xi, $input)
45-
}};
46-
}
47-
4824
#[repr(transparent)]
4925
pub(in super::super) struct KeyValue([u64; 2]);
5026

@@ -88,36 +64,24 @@ impl HTable {
8864
) {
8965
unsafe { f(xi, self) }
9066
}
67+
}
9168

92-
pub(super) unsafe fn ghash(
93-
&self,
94-
f: unsafe extern "C" fn(
95-
xi: &mut Xi,
96-
Htable: &HTable,
97-
inp: *const u8,
98-
len: crate::c::NonZero_size_t,
99-
),
100-
xi: &mut Xi,
101-
input: AsChunks<u8, BLOCK_LEN>,
102-
) {
103-
use core::num::NonZeroUsize;
104-
105-
let input = input.as_flattened();
69+
pub(super) fn with_non_dangling_ptr(
70+
input: AsChunks<u8, BLOCK_LEN>,
71+
f: impl FnOnce(*const u8, crate::c::NonZero_size_t),
72+
) {
73+
use core::num::NonZeroUsize;
10674

107-
let input_len = match NonZeroUsize::new(input.len()) {
108-
Some(len) => len,
109-
None => {
110-
return;
111-
}
112-
};
75+
let input = input.as_flattened();
11376

114-
// SAFETY:
115-
// * There are `input_len: NonZeroUsize` bytes available at `input` for
116-
// `f` to read.
117-
unsafe {
118-
f(xi, self, input.as_ptr(), input_len);
77+
let input_len = match NonZeroUsize::new(input.len()) {
78+
Some(len) => len,
79+
None => {
80+
return;
11981
}
120-
}
82+
};
83+
84+
f(input.as_ptr(), input_len);
12185
}
12286

12387
// The alignment is required by some assembly code, such as `ghash-ssse3-*`.

src/aead/gcm/neon.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
all(target_arch = "arm", target_endian = "little")
1818
))]
1919

20-
use super::{HTable, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN};
21-
use crate::{cpu, polyfill::slice::AsChunks};
20+
use super::{ffi, HTable, KeyValue, UpdateBlock, UpdateBlocks, Xi, BLOCK_LEN};
21+
use crate::{c, cpu, polyfill::slice::AsChunks};
2222

2323
#[derive(Clone)]
2424
pub struct Key {
@@ -59,6 +59,17 @@ impl UpdateBlock for Key {
5959

6060
impl UpdateBlocks for Key {
6161
fn update_blocks(&self, xi: &mut Xi, input: AsChunks<u8, BLOCK_LEN>) {
62-
unsafe { ghash!(gcm_ghash_neon, xi, &self.h_table, input) }
62+
prefixed_extern! {
63+
fn gcm_ghash_neon(
64+
xi: &mut Xi,
65+
Htable: &HTable,
66+
inp: *const u8,
67+
len: c::NonZero_size_t,
68+
);
69+
}
70+
let htable = &self.h_table;
71+
ffi::with_non_dangling_ptr(input, |input, len| unsafe {
72+
gcm_ghash_neon(xi, htable, input, len)
73+
});
6374
}
6475
}

src/aead/gcm/vclmulavx2.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414

1515
#![cfg(target_arch = "x86_64")]
1616

17-
use super::{ffi::KeyValue, HTable, UpdateBlock, Xi};
17+
use super::{
18+
ffi::{self, KeyValue},
19+
HTable, UpdateBlock, Xi,
20+
};
1821
use crate::{
1922
aead::gcm::ffi::BLOCK_LEN,
23+
c,
2024
cpu::intel::{Avx2, VAesClmul},
2125
polyfill::slice::AsChunks,
2226
};
@@ -43,7 +47,18 @@ impl Key {
4347

4448
impl UpdateBlock for Key {
4549
fn update_block(&self, xi: &mut Xi, a: [u8; BLOCK_LEN]) {
50+
prefixed_extern! {
51+
fn gcm_ghash_vpclmulqdq_avx2_16(
52+
xi: &mut Xi,
53+
Htable: &HTable,
54+
inp: *const u8,
55+
len: c::NonZero_size_t,
56+
);
57+
}
4658
let input: AsChunks<u8, BLOCK_LEN> = (&a).into();
47-
unsafe { ghash!(gcm_ghash_vpclmulqdq_avx2_16, xi, &self.h_table, input) }
59+
let htable = self.inner();
60+
ffi::with_non_dangling_ptr(input, |input, len| unsafe {
61+
gcm_ghash_vpclmulqdq_avx2_16(xi, htable, input, len)
62+
})
4863
}
4964
}

0 commit comments

Comments
 (0)