9 Commits
v0.2.0 ... main

4 changed files with 278 additions and 40 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "aes-gcm-stream"
version = "0.2.0"
version = "0.2.4"
edition = "2021"
authors = ["Hatter Jiang"]
repository = "https://git.hatter.ink/hatter/aes-gcm-stream"
@@ -12,12 +12,12 @@ categories = ["cryptography"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aes = { version = "0.8.3", features = ["zeroize"] }
cipher = "0.4.4"
ghash = "0.5.0"
zeroize = { version = "1.6.0", features = ["zeroize_derive"] }
aes = { version = "0.8", features = ["zeroize"] }
cipher = "0.4"
ghash = "0.5"
zeroize = { version = "1.6", features = ["zeroize_derive"] }
[dev-dependencies]
hex = "0.4.3"
benchmark-simple = "0.1.8"
aes-gcm = { version = "0.10.2", features = ["zeroize"] }
hex = "0.4"
benchmark-simple = "0.1"
aes-gcm = { version = "0.10", features = ["zeroize"] }

View File

@@ -1,6 +1,7 @@
# aes-gcm-stream
[Document](https://docs.rs/aes-gcm-stream/)
[Crates](https://crates.io/crates/aes-gcm-stream)
| [Document](https://docs.rs/aes-gcm-stream/)
## Encrypt
@@ -34,5 +35,17 @@ Ciphertext: 86c22c5122404b39683ca9b79b889fd00a6212d1be2ebc3f4f8f22f90b
Plaintext: Hello World!
```
Benchmark @MacBook Pro (Retina, 15-inch, Late 2013/2 GHz Quad-Core Intel Core i7)
```text
$ cargo run --release --example bench
AES128 encrypt : 483.04 M/s
AES192 encrypt : 466.22 M/s
AES256 encrypt : 451.38 M/s
AES256 en/decrypt : 222.66 M/s
AES256 encrypt aes-gcm : 547.63 M/s
```
> Thanks: https://developer.aliyun.com/article/952809
> Thanks:
> * https://developer.aliyun.com/article/952809
> * https://crates.io/crates/aes-gcm

View File

@@ -1,3 +1,5 @@
use zeroize::Zeroize;
/// This library is created for AES/GCM stream encrypt or decrypt
///
/// Structs for encryption:
@@ -22,22 +24,103 @@ mod util;
mod encryptor;
mod decryptor;
pub fn aes_gcm_encrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
match key.len() {
16 => aes_128_gcm_encrypt(key, nonce, message),
24 => aes_192_gcm_encrypt(key, nonce, message),
32 => aes_256_gcm_encrypt(key, nonce, message),
_ => Err(format!("Bad key length")),
}
}
pub fn aes_gcm_decrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
match key.len() {
16 => aes_128_gcm_decrypt(key, nonce, message),
24 => aes_192_gcm_decrypt(key, nonce, message),
32 => aes_256_gcm_decrypt(key, nonce, message),
_ => Err(format!("Bad key length")),
}
}
pub fn aes_128_gcm_decrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
let mut key: [u8; 16] = key.try_into().map_err(|_| format!("Bad key length"))?;
let mut gcm_stream = Aes128GcmStreamDecryptor::new(key, nonce);
let mut first_block = gcm_stream.update(message);
let final_block = gcm_stream.finalize()?;
first_block.extend_from_slice(&final_block);
key.zeroize();
Ok(first_block)
}
pub fn aes_192_gcm_decrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
let mut key: [u8; 24] = key.try_into().map_err(|_| format!("Bad key length"))?;
let mut gcm_stream = Aes192GcmStreamDecryptor::new(key, nonce);
let mut first_block = gcm_stream.update(message);
let final_block = gcm_stream.finalize()?;
first_block.extend_from_slice(&final_block);
key.zeroize();
Ok(first_block)
}
pub fn aes_256_gcm_decrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
let mut key: [u8; 32] = key.try_into().map_err(|_| format!("Bad key length"))?;
let mut gcm_stream = Aes256GcmStreamDecryptor::new(key, nonce);
let mut first_block = gcm_stream.update(message);
let final_block = gcm_stream.finalize()?;
first_block.extend_from_slice(&final_block);
key.zeroize();
Ok(first_block)
}
pub fn aes_128_gcm_encrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
let mut key: [u8; 16] = key.try_into().map_err(|_| format!("Bad key length"))?;
let mut gcm_stream = Aes128GcmStreamEncryptor::new(key, nonce);
let mut first_block = gcm_stream.update(message);
let (last_block, tag) = gcm_stream.finalize();
first_block.extend_from_slice(&last_block);
first_block.extend_from_slice(&tag);
key.zeroize();
Ok(first_block)
}
pub fn aes_192_gcm_encrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
let mut key: [u8; 24] = key.try_into().map_err(|_| format!("Bad key length"))?;
let mut gcm_stream = Aes192GcmStreamEncryptor::new(key, nonce);
let mut first_block = gcm_stream.update(message);
let (last_block, tag) = gcm_stream.finalize();
first_block.extend_from_slice(&last_block);
first_block.extend_from_slice(&tag);
key.zeroize();
Ok(first_block)
}
pub fn aes_256_gcm_encrypt(key: &[u8], nonce: &[u8], message: &[u8]) -> Result<Vec<u8>, String> {
let mut key: [u8; 32] = key.try_into().map_err(|_| format!("Bad key length"))?;
let mut gcm_stream = Aes256GcmStreamEncryptor::new(key, nonce);
let mut first_block = gcm_stream.update(message);
let (last_block, tag) = gcm_stream.finalize();
first_block.extend_from_slice(&last_block);
first_block.extend_from_slice(&tag);
key.zeroize();
Ok(first_block)
}
#[test]
fn test128() {
use aes_gcm::{aead::{Aead, Nonce, Payload}, Aes128Gcm, KeyInit};
let knp = vec![
([0; 16], [0; 12], &[] as &[u8], b"Hello World!" as &[u8]),
([0; 16], [0; 12], &[1; 16], b"Hello World!" as &[u8]),
([0; 16], [0; 12], &[1; 17], b"Hello World!" as &[u8]),
([0; 16], [0; 12], &[1; 32], b"Hello World!" as &[u8]),
([0; 16], [0; 12], &[1; 64], b"Hello World!" as &[u8]),
([0; 16], [0; 12], &[1, 2, 3], b"Hello World!" as &[u8]),
([1; 16], [0; 12], &[] as &[u8], b"Hello World!" as &[u8]),
([2; 16], [0; 12], &[1; 16], b"Hello World!" as &[u8]),
([3; 16], [0; 12], &[1; 17], b"Hello World!" as &[u8]),
([4; 16], [0; 12], &[1; 32], b"Hello World!" as &[u8]),
([5; 16], [0; 12], &[1; 64], b"Hello World!" as &[u8]),
([6; 16], [0; 12], &[1, 2, 3], b"Hello World!" as &[u8]),
([1; 16], [0; 12], &[], b"Hello World!"),
([0; 16], [1; 12], &[], b"Hello World!"),
([7; 16], [1; 12], &[], b"Hello World!"),
([1; 16], [1; 12], &[], b"Hello World ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!"),
([1; 16], [1; 12], &[0; 129], b"Hello World ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!"),
([0xff; 16], [0; 12], &[], b"Hello World!"),
([0; 16], [0xff; 12], &[], b"Hello World!"),
([8; 16], [0xff; 12], &[], b"Hello World!"),
([0xff; 16], [0xff; 12], &[], b"Hello World ~~~~~~~~~~~~~~~~~\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
@@ -102,18 +185,18 @@ fn test128() {
#[test]
fn test192() {
let knp = vec![
([0; 24], [0; 12], &[] as &[u8], b"Hello World!" as &[u8]),
([0; 24], [0; 12], &[1; 16], b"Hello World!" as &[u8]),
([0; 24], [0; 12], &[1; 17], b"Hello World!" as &[u8]),
([0; 24], [0; 12], &[1; 32], b"Hello World!" as &[u8]),
([0; 24], [0; 12], &[1; 64], b"Hello World!" as &[u8]),
([0; 24], [0; 12], &[1, 2, 3], b"Hello World!" as &[u8]),
([1; 24], [0; 12], &[] as &[u8], b"Hello World!" as &[u8]),
([2; 24], [0; 12], &[1; 16], b"Hello World!" as &[u8]),
([3; 24], [0; 12], &[1; 17], b"Hello World!" as &[u8]),
([4; 24], [0; 12], &[1; 32], b"Hello World!" as &[u8]),
([5; 24], [0; 12], &[1; 64], b"Hello World!" as &[u8]),
([6; 24], [0; 12], &[1, 2, 3], b"Hello World!" as &[u8]),
([1; 24], [0; 12], &[], b"Hello World!"),
([0; 24], [1; 12], &[], b"Hello World!"),
([8; 24], [1; 12], &[], b"Hello World!"),
([1; 24], [1; 12], &[], b"Hello World ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!"),
([1; 24], [1; 12], &[0; 129], b"Hello World ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!"),
([0xff; 24], [0; 12], &[], b"Hello World!"),
([0; 24], [0xff; 12], &[], b"Hello World!"),
([9; 24], [0xff; 12], &[], b"Hello World!"),
([0xff; 24], [0xff; 12], &[], b"Hello World ~~~~~~~~~~~~~~~~~\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
@@ -164,18 +247,18 @@ fn test192() {
fn test256() {
use aes_gcm::{aead::{Aead, Nonce, Payload}, Aes256Gcm, KeyInit};
let knp = vec![
([0; 32], [0; 12], &[] as &[u8], b"Hello World!" as &[u8]),
([0; 32], [0; 12], &[1; 16], b"Hello World!" as &[u8]),
([0; 32], [0; 12], &[1; 17], b"Hello World!" as &[u8]),
([0; 32], [0; 12], &[1; 32], b"Hello World!" as &[u8]),
([0; 32], [0; 12], &[1; 64], b"Hello World!" as &[u8]),
([0; 32], [0; 12], &[1, 2, 3], b"Hello World!" as &[u8]),
([1; 32], [0; 12], &[] as &[u8], b"Hello World!" as &[u8]),
([2; 32], [0; 12], &[1; 16], b"Hello World!" as &[u8]),
([3; 32], [0; 12], &[1; 17], b"Hello World!" as &[u8]),
([4; 32], [0; 12], &[1; 32], b"Hello World!" as &[u8]),
([5; 32], [0; 12], &[1; 64], b"Hello World!" as &[u8]),
([6; 32], [0; 12], &[1, 2, 3], b"Hello World!" as &[u8]),
([1; 32], [0; 12], &[], b"Hello World!"),
([0; 32], [1; 12], &[], b"Hello World!"),
([7; 32], [1; 12], &[], b"Hello World!"),
([1; 32], [1; 12], &[], b"Hello World ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!"),
([1; 32], [1; 12], &[0; 129], b"Hello World ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~!"),
([0xff; 32], [0; 12], &[], b"Hello World!"),
([0; 32], [0xff; 12], &[], b"Hello World!"),
([8; 32], [0xff; 12], &[], b"Hello World!"),
([0xff; 32], [0xff; 12], &[], b"Hello World ~~~~~~~~~~~~~~~~~\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
@@ -279,3 +362,108 @@ fn test256_stream() {
let decrypted_plaintext = cipher.decrypt(&decrypt_nonce, ciphertext.as_slice()).expect("decrypt1");
assert_eq!(plaintext, decrypted_plaintext.as_slice());
}
#[test]
fn test128_stream_and_array() {
let key = [0u8; 16];
let nonce = [0; 12];
let mut plaintext = vec![];
// encrypt
let mut ciphertext = vec![];
let mut encryptor = Aes128GcmStreamEncryptor::new(key.clone(), &nonce);
for i in 0..1025 {
plaintext.extend_from_slice(&[(i % 128) as u8]);
ciphertext.extend_from_slice(&encryptor.update(&[(i % 128) as u8]));
}
let (last_block, tag) = encryptor.finalize();
ciphertext.extend_from_slice(&last_block);
ciphertext.extend_from_slice(&tag);
let encrypted = aes_128_gcm_encrypt(&key, &nonce, &plaintext).unwrap();
let decrypted = aes_128_gcm_decrypt(&key, &nonce, &ciphertext).unwrap();
assert_eq!(ciphertext, encrypted);
assert_eq!(plaintext, decrypted);
let encrypted = aes_gcm_encrypt(&key, &nonce, &plaintext).unwrap();
let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext).unwrap();
assert_eq!(ciphertext, encrypted);
assert_eq!(plaintext, decrypted);
}
#[test]
fn test192_stream_and_array() {
let key = [0u8; 24];
let nonce = [0; 12];
let mut plaintext = vec![];
// encrypt
let mut ciphertext = vec![];
let mut encryptor = Aes192GcmStreamEncryptor::new(key.clone(), &nonce);
for i in 0..1025 {
plaintext.extend_from_slice(&[(i % 128) as u8]);
ciphertext.extend_from_slice(&encryptor.update(&[(i % 128) as u8]));
}
let (last_block, tag) = encryptor.finalize();
ciphertext.extend_from_slice(&last_block);
ciphertext.extend_from_slice(&tag);
let encrypted = aes_192_gcm_encrypt(&key, &nonce, &plaintext).unwrap();
let decrypted = aes_192_gcm_decrypt(&key, &nonce, &ciphertext).unwrap();
assert_eq!(ciphertext, encrypted);
assert_eq!(plaintext, decrypted);
let encrypted = aes_gcm_encrypt(&key, &nonce, &plaintext).unwrap();
let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext).unwrap();
assert_eq!(ciphertext, encrypted);
assert_eq!(plaintext, decrypted);
}
#[test]
fn test256_stream_and_array() {
let key = [0u8; 32];
let nonce = [0; 12];
let mut plaintext = vec![];
// encrypt
let mut ciphertext = vec![];
let mut encryptor = Aes256GcmStreamEncryptor::new(key.clone(), &nonce);
for i in 0..1025 {
plaintext.extend_from_slice(&[(i % 128) as u8]);
ciphertext.extend_from_slice(&encryptor.update(&[(i % 128) as u8]));
}
let (last_block, tag) = encryptor.finalize();
ciphertext.extend_from_slice(&last_block);
ciphertext.extend_from_slice(&tag);
let encrypted = aes_256_gcm_encrypt(&key, &nonce, &plaintext).unwrap();
let decrypted = aes_256_gcm_decrypt(&key, &nonce, &ciphertext).unwrap();
assert_eq!(ciphertext, encrypted);
assert_eq!(plaintext, decrypted);
let encrypted = aes_gcm_encrypt(&key, &nonce, &plaintext).unwrap();
let decrypted = aes_gcm_decrypt(&key, &nonce, &ciphertext).unwrap();
assert_eq!(ciphertext, encrypted);
assert_eq!(plaintext, decrypted);
}
#[test]
fn test125_ab_nonce() {
let key = hex::decode("faf6a891866fac550ef548b4e5f6fbc98fccc6827cd943cc8d7539747f1d87bd").unwrap();
let key: [u8; 32] = key.try_into().map_err(|_| format!("Bad key length")).unwrap();
let nonce = hex::decode("cea500817694e16e734f07df422f6f52582d844e623746a96c5fbb4be2a38a6e").unwrap();
let ciphertext = hex::decode("c52dadf683c02e81d842f6563b").unwrap();
let tag = hex::decode("cc9062944525de37d3aa588c6a5676a2").unwrap();
let aad = b"firstName:";
let mut gcm_stream = Aes256GcmStreamDecryptor::new(key, &nonce);
gcm_stream.init_adata(&aad[..]);
let mut first_block = gcm_stream.update(&ciphertext);
let second_block = gcm_stream.update(&tag);
let final_block = gcm_stream.finalize().unwrap();
first_block.extend_from_slice(&second_block);
first_block.extend_from_slice(&final_block);
assert_eq!("John --- TEST", String::from_utf8(first_block).unwrap());
}

View File

@@ -39,18 +39,29 @@ pub(crate) fn ghash(key: u128, messages: &[u128]) -> u128 {
}
pub(crate) fn normalize_nonce(ghash_key: u128, nonce_bytes: &[u8]) -> (u128, u128) {
let nonce = u8to128(nonce_bytes);
let normalized_nonce = match nonce_bytes.len() == 12 {
true => {
let nonce = u8to128(nonce_bytes);
nonce << 32 | 0x00000001
}
false => {
let mut iv_padding = vec![];
// s = 128[len(iv) / 128] - len(iv)
let s = 128 * (((nonce_bytes.len() * 8) + 128 - 1) / 128) - (nonce_bytes.len() * 8);
iv_padding.push(nonce << s);
iv_padding.push((nonce_bytes.len() * 8) as u128);
ghash(ghash_key, &iv_padding)
iv_padding.extend_from_slice(nonce_bytes);
let left_len = nonce_bytes.len() - 16 * (nonce_bytes.len() / 16);
let tobe_padding_len = if left_len == 0 { 0 } else { 16 - left_len };
for _ in 0..tobe_padding_len { iv_padding.push(0); }
let mut block = ghash::Block::default();
let nonce_bits = (nonce_bytes.len() as u64) * 8;
block[8..].copy_from_slice(&nonce_bits.to_be_bytes());
iv_padding.extend_from_slice(block.as_slice());
let mut iv_padding_u128 = vec![];
let block_count = iv_padding.len() / 16;
for i in 0..block_count {
iv_padding_u128.push(u8to128(&iv_padding[i * 16..(i + 1) * 16]));
}
ghash(ghash_key, &iv_padding_u128)
}
};
(ghash_key, normalized_nonce)
@@ -81,4 +92,30 @@ pub(crate) fn inc_32(bits: u128) -> u128 {
let mut lsb = (bits & 0xffffffff) as u32;
lsb = lsb.wrapping_add(1);
msb << 32 | lsb as u128
}
#[test]
fn test_normalize_nonce() {
use aes_gcm::KeyInit;
use ghash::Key;
use ghash::GHash;
use ghash::universal_hash::UniversalHash;
let ghash_key = [1u8; 16];
let key = Key::from(ghash_key);
let mut ghash = GHash::new(&key);
let nonce = [1u8; 22];
ghash.update_padded(&nonce);
let mut block = ghash::Block::default();
let nonce_bits = (nonce.len() as u64) * 8;
block[8..].copy_from_slice(&nonce_bits.to_be_bytes());
ghash.update(&[block]);
let final_nonce = ghash.finalize();
let final_nonce_bytes = final_nonce.as_slice();
let final_nonce1 = u8to128(final_nonce_bytes);
let (_, final_nonce2) = normalize_nonce(u8to128(&ghash_key), &nonce);
assert_eq!(final_nonce1, final_nonce2);
}