Skip to content

Commit 915310a

Browse files
committed
crypto: Use RustCrypto's GHash as well
1 parent 8fe6931 commit 915310a

File tree

10 files changed

+26
-443
lines changed

10 files changed

+26
-443
lines changed

Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/crypto/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ polyval = "0.4"
1515
subtle = "2.3"
1616
cipher = "0.2"
1717
generic-array = "0.14"
18+
ghash = { version = "0.4.2", features = ["armv8"] }
1819
hmac = "0.9.0"
1920
rand = "0.7.3"
2021
sha-1 = "0.9"
2122
sha2 = "0.9"
2223

23-
[target.'cfg(all(target_arch = "aarch64", any(target_os = "linux")))'.dependencies]
24-
libc = "0.2.93" # for getauxval
25-
2624
[dev-dependencies]
2725
serde = { version = "1.0", features = ["derive"] }
2826
serde_json = "1.0"

rust/crypto/src/aes_gcm.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
// SPDX-License-Identifier: AGPL-3.0-only
44
//
55

6-
use crate::ghash::Ghash;
76
use crate::{Aes256Ctr32, Error, Result};
87
use aes::{Aes256, BlockEncrypt, NewBlockCipher};
98
use generic_array::GenericArray;
9+
use ghash::universal_hash::{NewUniversalHash, UniversalHash};
10+
use ghash::GHash;
1011
use subtle::ConstantTimeEq;
1112

1213
pub const TAG_SIZE: usize = 16;
1314
pub const NONCE_SIZE: usize = 12;
1415

1516
#[derive(Clone)]
1617
struct GcmGhash {
17-
ghash: Ghash,
18+
ghash: GHash,
1819
ghash_pad: [u8; TAG_SIZE],
1920
msg_buf: [u8; TAG_SIZE],
2021
msg_buf_offset: usize,
@@ -24,9 +25,9 @@ struct GcmGhash {
2425

2526
impl GcmGhash {
2627
fn new(h: &[u8; TAG_SIZE], ghash_pad: [u8; TAG_SIZE], associated_data: &[u8]) -> Result<Self> {
27-
let mut ghash = Ghash::new(h)?;
28+
let mut ghash = GHash::new(h.into());
2829

29-
ghash.update_padded(associated_data)?;
30+
ghash.update_padded(associated_data);
3031

3132
Ok(Self {
3233
ghash,
@@ -49,7 +50,7 @@ impl GcmGhash {
4950
self.msg_len += taking;
5051

5152
if self.msg_buf_offset == TAG_SIZE {
52-
self.ghash.update(&self.msg_buf)?;
53+
self.ghash.update(&self.msg_buf.into());
5354
self.msg_buf_offset = 0;
5455
return self.update(&msg[taking..]);
5556
} else {
@@ -64,7 +65,9 @@ impl GcmGhash {
6465
let leftover = msg.len() - 16 * full_blocks;
6566
assert!(leftover < TAG_SIZE);
6667
if full_blocks > 0 {
67-
self.ghash.update(&msg[..full_blocks * 16])?;
68+
for block in msg[..full_blocks * 16].chunks_exact(16) {
69+
self.ghash.update(block.into());
70+
}
6871
}
6972

7073
self.msg_buf[0..leftover].copy_from_slice(&msg[full_blocks * 16..]);
@@ -76,24 +79,22 @@ impl GcmGhash {
7679

7780
fn finalize(mut self) -> Result<[u8; TAG_SIZE]> {
7881
if self.msg_buf_offset > 0 {
79-
for i in self.msg_buf_offset..TAG_SIZE {
80-
self.msg_buf[i] = 0;
81-
}
82-
self.ghash.update(&self.msg_buf)?;
82+
self.ghash
83+
.update_padded(&self.msg_buf[..self.msg_buf_offset]);
8384
}
8485

8586
let mut final_block = [0u8; 16];
8687
final_block[..8].copy_from_slice(&(8 * self.ad_len).to_be_bytes());
8788
final_block[8..].copy_from_slice(&(8 * self.msg_len).to_be_bytes());
8889

89-
self.ghash.update(&final_block)?;
90-
let mut hash = self.ghash.finalize()?;
90+
self.ghash.update(&final_block.into());
91+
let mut hash = self.ghash.finalize().into_bytes();
9192

9293
for (i, b) in hash.iter_mut().enumerate() {
9394
*b ^= self.ghash_pad[i];
9495
}
9596

96-
Ok(hash)
97+
Ok(hash.into())
9798
}
9899
}
99100

rust/crypto/src/cpuid.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

rust/crypto/src/ghash.rs

Lines changed: 0 additions & 84 deletions
This file was deleted.

rust/crypto/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33
// SPDX-License-Identifier: AGPL-3.0-only
44
//
55

6-
#![cfg_attr(target_arch = "aarch64", feature(stdsimd))]
7-
#![cfg_attr(target_arch = "aarch64", feature(aarch64_target_feature))]
86
#![deny(clippy::unwrap_used)]
97

108
mod error;
119
mod hash;
1210

1311
mod aes_ctr;
1412
mod aes_gcm;
15-
mod cpuid;
16-
mod ghash;
17-
mod polyval;
1813

1914
pub use {
2015
aes_ctr::Aes256Ctr32,

rust/crypto/src/polyval.rs

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)