Skip to content

Commit 97afaf4

Browse files
committed
aes-gcm: Pass GCM key to constructors by reference.
When the constructors delegate to an assembly function that takes a reference, have the constructor also take a reference.
1 parent 5a61573 commit 97afaf4

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

src/aead/aes_gcm.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ impl DynKey {
8585
let aes_key = aes::hw::Key::new(key, aes, cpu.get_feature())?;
8686
let gcm_key_value = derive_gcm_key_value(&aes_key);
8787
let combo = if let Some(cpu) = cpu.get_feature() {
88-
let gcm_key = gcm::vclmulavx2::Key::new(gcm_key_value, cpu);
88+
let gcm_key = gcm::vclmulavx2::Key::new(&gcm_key_value, cpu);
8989
Self::VAesClMulAvx2(Combo { aes_key, gcm_key })
9090
} else if let Some(cpu) = cpu.get_feature() {
91-
let gcm_key = gcm::clmulavxmovbe::Key::new(gcm_key_value, cpu);
91+
let gcm_key = gcm::clmulavxmovbe::Key::new(&gcm_key_value, cpu);
9292
Self::AesHwClMulAvxMovbe(Combo { aes_key, gcm_key })
9393
} else {
94-
let gcm_key = gcm::clmul::Key::new(gcm_key_value, gcm);
94+
let gcm_key = gcm::clmul::Key::new(&gcm_key_value, gcm);
9595
Self::AesHwClMul(Combo { aes_key, gcm_key })
9696
};
9797
return Ok(combo);
@@ -105,7 +105,7 @@ impl DynKey {
105105
if let (Some(aes), Some(gcm)) = (cpu.get_feature(), cpu.get_feature()) {
106106
let aes_key = aes::hw::Key::new(key, aes, cpu.get_feature())?;
107107
let gcm_key_value = derive_gcm_key_value(&aes_key);
108-
let gcm_key = gcm::clmul::Key::new(gcm_key_value, gcm);
108+
let gcm_key = gcm::clmul::Key::new(&gcm_key_value, gcm);
109109
return Ok(Self::AesHwClMul(Combo { aes_key, gcm_key }));
110110
}
111111

@@ -131,15 +131,15 @@ impl DynKey {
131131
fn new_neon(key: aes::KeyBytes, cpu: cpu::aarch64::Neon) -> Result<Self, error::Unspecified> {
132132
let aes_key = aes::vp::Key::new(key, cpu)?;
133133
let gcm_key_value = derive_gcm_key_value(&aes_key);
134-
let gcm_key = gcm::neon::Key::new(gcm_key_value, cpu);
134+
let gcm_key = gcm::neon::Key::new(&gcm_key_value, cpu);
135135
Ok(Self::Simd(Combo { aes_key, gcm_key }))
136136
}
137137

138138
#[cfg(all(target_arch = "arm", target_endian = "little"))]
139139
fn new_neon(key: aes::KeyBytes, cpu: cpu::arm::Neon) -> Result<Self, error::Unspecified> {
140140
let aes_key = aes::vp::Key::new(key, cpu)?;
141141
let gcm_key_value = derive_gcm_key_value(&aes_key);
142-
let gcm_key = gcm::neon::Key::new(gcm_key_value, cpu);
142+
let gcm_key = gcm::neon::Key::new(&gcm_key_value, cpu);
143143
Ok(Self::Simd(Combo { aes_key, gcm_key }))
144144
}
145145

src/aead/gcm/clmul.rs

+7-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};
@@ -30,15 +33,15 @@ pub struct Key {
3033

3134
impl Key {
3235
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
33-
pub(in super::super) fn new(value: KeyValue, _cpu: cpu::aarch64::PMull) -> Self {
36+
pub(in super::super) fn new(value: &KeyValue, _cpu: cpu::aarch64::PMull) -> Self {
3437
Self {
3538
h_table: unsafe { htable_new!(gcm_init_clmul, value) },
3639
}
3740
}
3841

3942
#[cfg(target_arch = "x86")]
4043
pub(in super::super) fn new(
41-
value: KeyValue,
44+
value: &KeyValue,
4245
_cpu: (cpu::intel::ClMul, cpu::intel::Ssse3),
4346
) -> Self {
4447
Self {
@@ -49,7 +52,7 @@ impl Key {
4952
#[cfg(target_arch = "x86_64")]
5053
#[inline(never)]
5154
pub(in super::super) fn new(
52-
value: KeyValue,
55+
value: &KeyValue,
5356
_cpu: (cpu::intel::ClMul, cpu::intel::Ssse3),
5457
) -> Self {
5558
Self {

src/aead/gcm/clmulavxmovbe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct Key {
2525
impl Key {
2626
#[inline(never)]
2727
pub(in super::super) fn new(
28-
value: KeyValue,
28+
value: &KeyValue,
2929
_required_cpu_features: (intel::ClMul, intel::Avx, intel::Movbe),
3030
) -> Self {
3131
Self {

src/aead/gcm/ffi.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! htable_new {
3131
( $name:ident, $value:expr $(,)? ) => {{
3232
use crate::aead::gcm::ffi::HTable;
3333
prefixed_extern! {
34-
fn $name(HTable: &mut HTable, h: &[u64; 2]);
34+
fn $name(HTable: &mut HTable, h: &KeyValue);
3535
}
3636
HTable::new($name, $value)
3737
}};
@@ -61,6 +61,7 @@ macro_rules! ghash {
6161
}};
6262
}
6363

64+
#[repr(transparent)]
6465
pub(in super::super) struct KeyValue([u64; 2]);
6566

6667
impl KeyValue {
@@ -85,13 +86,13 @@ impl KeyValue {
8586
))]
8687
impl HTable {
8788
pub(super) unsafe fn new(
88-
init: unsafe extern "C" fn(HTable: &mut HTable, &[u64; 2]),
89-
value: KeyValue,
89+
init: unsafe extern "C" fn(HTable: &mut HTable, &KeyValue),
90+
value: &KeyValue,
9091
) -> Self {
9192
let mut r = Self {
9293
Htable: [U128 { hi: 0, lo: 0 }; HTABLE_LEN],
9394
};
94-
unsafe { init(&mut r, &value.0) };
95+
unsafe { init(&mut r, &value) };
9596
r
9697
}
9798

src/aead/gcm/neon.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ pub struct Key {
2727

2828
impl Key {
2929
#[cfg(all(target_arch = "aarch64", target_endian = "little"))]
30-
pub(in super::super) fn new(value: KeyValue, _cpu: cpu::aarch64::Neon) -> Self {
30+
pub(in super::super) fn new(value: &KeyValue, _cpu: cpu::aarch64::Neon) -> Self {
3131
Self {
3232
h_table: unsafe { htable_new!(gcm_init_neon, value) },
3333
}
3434
}
3535

3636
#[cfg(all(target_arch = "arm", target_endian = "little"))]
37-
pub(in super::super) fn new(value: KeyValue, _cpu: cpu::arm::Neon) -> Self {
37+
pub(in super::super) fn new(value: &KeyValue, _cpu: cpu::arm::Neon) -> Self {
3838
Self {
3939
h_table: unsafe { htable_new!(gcm_init_neon, value) },
4040
}

src/aead/gcm/vclmulavx2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Key {
2727
}
2828

2929
impl Key {
30-
pub(in super::super) fn new(value: KeyValue, _cpu: (Avx2, VAesClmul)) -> Self {
30+
pub(in super::super) fn new(value: &KeyValue, _cpu: (Avx2, VAesClmul)) -> Self {
3131
Self {
3232
h_table: unsafe { htable_new!(gcm_init_vpclmulqdq_avx2, value) },
3333
}

0 commit comments

Comments
 (0)