formatting, aes-gmac-siv crate

This commit is contained in:
Adam Ierymenko 2021-07-14 15:38:37 -04:00
parent 18051ef76a
commit f898029241
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
368 changed files with 5799 additions and 6290 deletions

View file

@ -62,14 +62,14 @@ UseTab: Never
---
Language: Cpp
Standard: c++11
ColumnLimit: 120
ColumnLimit: 360
---
Language: ObjC
ColumnLimit: 120
ColumnLimit: 360
---
Language: Java
ColumnLimit: 120
ColumnLimit: 360
---
Language: CSharp
ColumnLimit: 120
ColumnLimit: 360
...

132
aes-gmac-siv/Cargo.lock generated Normal file
View file

@ -0,0 +1,132 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aes-gmac-siv"
version = "0.1.0"
dependencies = [
"gcrypt",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "cc"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cstr-argument"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20bd4e8067c20c7c3a4dea759ef91d4b18418ddb5bd8837ef6e2f2f93ca7ccbb"
dependencies = [
"cfg-if",
"memchr",
]
[[package]]
name = "gcrypt"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c2ee79dcb8915fc0e9d8364e87d2215555076aa159d0a5d84ba9dba109b0d59"
dependencies = [
"bitflags",
"cstr-argument",
"gpg-error",
"libc",
"libgcrypt-sys",
"once_cell",
]
[[package]]
name = "gpg-error"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7073b9ac823434ae73608715086e944d694a7ce2677371b8c5253300d1f767f1"
dependencies = [
"libgpg-error-sys",
]
[[package]]
name = "libc"
version = "0.2.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
[[package]]
name = "libgcrypt-sys"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62eb5d6d9cd6d8c8adf9641c95b223eb14f07a7a81c082e2d08f0bf3880214e4"
dependencies = [
"cc",
"cfg-if",
"libc",
"libgpg-error-sys",
]
[[package]]
name = "libgpg-error-sys"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffb1aedf0efc5d25fdd08eb52b0759c71d02ac77fd1879b96e95211239528897"
dependencies = [
"libc",
"winreg",
]
[[package]]
name = "memchr"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
[[package]]
name = "once_cell"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "winreg"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69"
dependencies = [
"winapi",
]

12
aes-gmac-siv/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "aes-gmac-siv"
version = "0.1.0"
edition = "2018"
[profile.test]
opt-level = 3
lto = true
codegen-units = 1
[dependencies]
gcrypt = "^0"

View file

@ -0,0 +1,171 @@
use std::io::Write;
/// AES-GMAC-SIV encryptor/decryptor.
#[repr(align(8))] // allow tag and tmp to be accessed as u64 arrays as well
pub struct AesGmacSiv {
tag: [u8; 16],
tmp: [u8; 16],
ctr: gcrypt::cipher::Cipher,
ecb: gcrypt::cipher::Cipher,
gmac: gcrypt::mac::Mac,
}
impl AesGmacSiv {
/// Create a new keyed instance of AES-GMAC-SIV
/// The key may be of size 16, 24, or 32 bytes (128, 192, or 256 bits). Any other size will panic.
#[inline(always)]
pub fn new(key: &[u8]) -> Self {
if key.len() != 32 && key.len() != 24 && key.len() != 16 {
panic!("AES supports 128, 192, or 256 bits keys");
}
let mut c = AesGmacSiv {
tag: [0_u8; 16],
tmp: [0_u8; 16],
ctr: gcrypt::cipher::Cipher::new(gcrypt::cipher::Algorithm::Aes, gcrypt::cipher::Mode::Ctr).unwrap(),
ecb: gcrypt::cipher::Cipher::new(gcrypt::cipher::Algorithm::Aes, gcrypt::cipher::Mode::Ecb).unwrap(),
gmac: gcrypt::mac::Mac::new(gcrypt::mac::Algorithm::GmacAes).unwrap(),
};
c.ctr.set_key(key).expect("AES-CTR init failed");
c.ecb.set_key(key).expect("AES-ECB init failed");
c.gmac.set_key(key).expect("AES-GMAC init failed");
c
}
/// Reset to prepare for another encrypt or decrypt operation.
#[inline(always)]
pub fn reset(&mut self) {
let _ = self.ctr.reset();
// self.ecb is stateless
let _ = self.gmac.reset();
}
/// Initialize for encryption.
#[inline(always)]
pub fn encrypt_init(&mut self, iv: &[u8]) {
self.tag[0..8].copy_from_slice(iv);
self.tag[8..16].fill(0);
let _ = self.gmac.set_iv(&self.tag);
}
/// Set additional authenticated data (data to be authenticated but not encrypted).
/// This can currently only be called once. Multiple calls will result in corrupt data.
#[inline(always)]
pub fn encrypt_set_aad(&mut self, data: &[u8]) {
let _ = self.gmac.update(data);
let pad = data.len() & 0xf;
if pad != 0 {
let _ = self.gmac.update(&crate::ZEROES[0..(16 - pad)]);
}
}
/// Feed plaintext in for the first encryption pass.
/// This may be called more than once.
#[inline(always)]
pub fn encrypt_first_pass(&mut self, plaintext: &[u8]) {
let _ = self.gmac.update(plaintext);
}
/// Finish first pass and begin second pass.
#[inline(always)]
pub fn encrypt_first_pass_finish(&mut self) {
let _ = self.gmac.flush();
let _ = self.gmac.get_mac(&mut self.tmp);
unsafe { // tag[8..16] = tmp[0..8] ^ tmp[8..16]
let tmp = self.tmp.as_mut_ptr().cast::<u64>();
*self.tag.as_mut_ptr().cast::<u64>().offset(1) = *tmp ^ *tmp.offset(1);
}
let _ = self.ecb.encrypt_inplace(&mut self.tag);
self.tmp.copy_from_slice(&self.tag);
self.tmp[12] &= 0x7f;
let _ = self.ctr.set_ctr(&self.tmp);
}
/// Feed plaintext for second pass and write ciphertext to supplied buffer.
/// This may be called more than once.
#[inline(always)]
pub fn encrypt_second_pass(&mut self, plaintext: &[u8], ciphertext: &mut [u8]) {
let _ = self.ctr.encrypt(plaintext, ciphertext);
}
/// Encrypt plaintext in place.
/// This may be called more than once.
#[inline(always)]
pub fn encrypt_second_pass_in_place(&mut self, plaintext_to_ciphertext: &mut [u8]) {
let _ = self.ctr.encrypt_inplace(plaintext_to_ciphertext);
}
/// Finish second pass and return a reference to the tag for this message.
/// The tag returned remains valid until reset() is called.
#[inline(always)]
pub fn encrypt_second_pass_finish(&mut self) -> &[u8; 16] {
return &self.tag;
}
#[inline(always)]
fn decrypt_init_internal(&mut self) {
self.tmp[12] &= 0x7f;
let _ = self.ctr.set_ctr(&self.tmp);
let _ = self.ecb.decrypt_inplace(&mut self.tag);
unsafe { // tmp[0..8] = tag[0..8], tmp[8..16] = 0
let tmp = self.tmp.as_mut_ptr().cast::<u64>();
*tmp = *self.tag.as_mut_ptr().cast::<u64>();
*tmp.offset(1) = 0;
}
let _ = self.gmac.set_iv(&self.tmp);
}
/// Initialize this cipher for decryption.
/// The supplied tag must be 16 bytes in length. Any other length will panic.
#[inline(always)]
pub fn decrypt_init(&mut self, tag: &[u8]) {
self.tmp.copy_from_slice(tag);
self.tag.copy_from_slice(tag);
self.decrypt_init_internal();
}
/// Initialize this cipher for decryption from tag split into two 8-byte chunks (for ZeroTier use).
/// The supplied tag chunks must be 8 bytes in length. Any other length will panic.
#[inline(always)]
pub fn decrypt_init2(&mut self, tag_0_8: &[u8], tag_8_16: &[u8]) {
self.tmp[0..8].copy_from_slice(tag_0_8);
self.tmp[8..16].copy_from_slice(tag_8_16);
self.tag[0..8].copy_from_slice(tag_0_8);
self.tag[8..16].copy_from_slice(tag_8_16);
self.decrypt_init_internal();
}
/// Set additional authenticated data to be checked.
#[inline(always)]
pub fn decrypt_set_aad(&mut self, data: &[u8]) {
self.encrypt_set_aad(data);
}
/// Decrypt ciphertext and write to plaintext.
/// This may be called more than once.
#[inline(always)]
pub fn decrypt(&mut self, ciphertext: &[u8], plaintext: &mut [u8]) {
let _ = self.ctr.decrypt(ciphertext, plaintext);
let _ = self.gmac.update(plaintext);
}
/// Decrypt ciphertext in place.
/// This may be called more than once.
#[inline(always)]
pub fn decrypt_in_place(&mut self, ciphertext_to_plaintext: &mut [u8]) {
let _ = self.ctr.decrypt_inplace(ciphertext_to_plaintext);
let _ = self.gmac.update(ciphertext_to_plaintext);
}
/// Finish decryption and return true if authentication appears valid.
/// If this returns false the message should be dropped.
#[inline(always)]
pub fn decrypt_finish(&mut self) -> bool {
let _ = self.gmac.flush();
let _ = self.gmac.get_mac(&mut self.tmp);
unsafe { // tag[8..16] == tmp[0..8] ^ tmp[8..16]
let tmp = self.tmp.as_mut_ptr().cast::<u64>();
*self.tag.as_mut_ptr().cast::<u64>().offset(1) == *tmp ^ *tmp.offset(1)
}
}
}

View file

@ -0,0 +1,258 @@
use std::os::raw::{c_void, c_int};
use std::ptr::{null_mut, null};
#[allow(non_upper_case_globals)]
const kCCModeECB: i32 = 1;
#[allow(non_upper_case_globals)]
const kCCModeCTR: i32 = 4;
#[allow(non_upper_case_globals)]
const kCCModeGCM: i32 = 11;
#[allow(non_upper_case_globals)]
const kCCEncrypt: i32 = 0;
#[allow(non_upper_case_globals)]
const kCCDecrypt: i32 = 1;
#[allow(non_upper_case_globals)]
const kCCAlgorithmAES: i32 = 0;
#[allow(non_upper_case_globals)]
const kCCOptionECBMode: i32 = 2;
extern "C" {
fn CCCryptorCreateWithMode(op: i32, mode: i32, alg: i32, padding: i32, iv: *const c_void, key: *const c_void, key_len: usize, tweak: *const c_void, tweak_len: usize, num_rounds: c_int, options: i32, cryyptor_ref: *mut *mut c_void) -> i32;
fn CCCryptorUpdate(cryptor_ref: *mut c_void, data_in: *const c_void, data_in_len: usize, data_out: *mut c_void, data_out_len: usize, data_out_written: *mut usize) -> i32;
fn CCCryptorReset(cryptor_ref: *mut c_void, iv: *const c_void) -> i32;
fn CCCryptorRelease(cryptor_ref: *mut c_void) -> i32;
fn CCCryptorGCMSetIV(cryptor_ref: *mut c_void, iv: *const c_void, iv_len: usize) -> i32;
fn CCCryptorGCMAddAAD(cryptor_ref: *mut c_void, aad: *const c_void, len: usize) -> i32;
fn CCCryptorGCMFinalize(cryptor_ref: *mut c_void, tag: *mut c_void, tag_len: usize) -> i32;
fn CCCryptorGCMReset(cryptor_ref: *mut c_void) -> i32;
}
#[repr(align(8))]
pub struct AesGmacSiv {
tag: [u8; 16],
tmp: [u8; 16],
ctr: *mut c_void,
ecb_enc: *mut c_void,
ecb_dec: *mut c_void,
gmac: *mut c_void,
}
impl Drop for AesGmacSiv {
fn drop(&mut self) {
unsafe {
if !self.ctr.is_null() {
CCCryptorRelease(self.ctr);
}
if !self.ecb_enc.is_null() {
CCCryptorRelease(self.ecb_enc);
}
if !self.ecb_dec.is_null() {
CCCryptorRelease(self.ecb_dec);
}
if !self.gmac.is_null() {
CCCryptorRelease(self.gmac);
}
}
}
}
impl AesGmacSiv {
/// Create a new keyed instance of AES-GMAC-SIV
/// The key may be of size 16, 24, or 32 bytes (128, 192, or 256 bits). Any other size will panic.
#[inline(always)]
pub fn new(key: &[u8]) -> Self {
if key.len() != 32 && key.len() != 24 && key.len() != 16 {
panic!("AES supports 128, 192, or 256 bits keys");
}
let mut c: AesGmacSiv = AesGmacSiv {
tag: [0_u8; 16],
tmp: [0_u8; 16],
ctr: null_mut(),
ecb_enc: null_mut(),
ecb_dec: null_mut(),
gmac: null_mut(),
};
unsafe {
let result = CCCryptorCreateWithMode(kCCEncrypt, kCCModeCTR, kCCAlgorithmAES, 0, crate::ZEROES.as_ptr().cast(), key.as_ptr().cast(), key.len(), null(), 0, 0, 0, &mut c.ctr);
if result != 0 {
panic!("CCCryptorCreateWithMode for ECB encrypt mode returned {}", result);
}
let result = CCCryptorCreateWithMode(kCCEncrypt, kCCModeECB, kCCAlgorithmAES, 0, crate::ZEROES.as_ptr().cast(), key.as_ptr().cast(), key.len(), null(), 0, 0, kCCOptionECBMode, &mut c.ecb_enc);
if result != 0 {
panic!("CCCryptorCreateWithMode for ECB encrypt mode returned {}", result);
}
let result = CCCryptorCreateWithMode(kCCDecrypt, kCCModeECB, kCCAlgorithmAES, 0, crate::ZEROES.as_ptr().cast(), key.as_ptr().cast(), key.len(), null(), 0, 0, kCCOptionECBMode, &mut c.ecb_dec);
if result != 0 {
panic!("CCCryptorCreateWithMode for ECB decrypt mode returned {}", result);
}
let result = CCCryptorCreateWithMode(kCCEncrypt, kCCModeGCM, kCCAlgorithmAES, 0, crate::ZEROES.as_ptr().cast(), key.as_ptr().cast(), key.len(), null(), 0, 0, 0, &mut c.gmac);
if result != 0 {
panic!("CCCryptorCreateWithMode for ECB decrypt mode returned {}", result);
}
}
c
}
/// Reset to prepare for another encrypt or decrypt operation.
#[inline(always)]
pub fn reset(&mut self) {
unsafe {
CCCryptorGCMReset(self.gmac);
}
}
/// Initialize for encryption.
#[inline(always)]
pub fn encrypt_init(&mut self, iv: &[u8]) {
self.tag[0..8].copy_from_slice(iv);
self.tag[8..16].fill(0);
unsafe {
CCCryptorGCMSetIV(self.gmac, self.tag.as_ptr().cast(), 16);
}
}
/// Set additional authenticated data (data to be authenticated but not encrypted).
/// This can currently only be called once. Multiple calls will result in corrupt data.
#[inline(always)]
pub fn encrypt_set_aad(&mut self, data: &[u8]) {
unsafe {
CCCryptorGCMAddAAD(self.gmac, data.as_ptr().cast(), data.len());
}
let pad = data.len() & 0xf;
if pad != 0 {
unsafe {
CCCryptorGCMAddAAD(self.gmac, crate::ZEROES.as_ptr().cast(), 16 - pad);
}
}
}
/// Feed plaintext in for the first encryption pass.
/// This may be called more than once.
#[inline(always)]
pub fn encrypt_first_pass(&mut self, plaintext: &[u8]) {
unsafe {
CCCryptorGCMAddAAD(self.gmac, plaintext.as_ptr().cast(), plaintext.len());
}
}
/// Finish first pass and begin second pass.
#[inline(always)]
pub fn encrypt_first_pass_finish(&mut self) {
unsafe {
CCCryptorGCMFinalize(self.gmac, self.tmp.as_mut_ptr().cast(), 16);
let tmp = self.tmp.as_mut_ptr().cast::<u64>();
*self.tag.as_mut_ptr().cast::<u64>().offset(1) = *tmp ^ *tmp.offset(1);
let mut data_out_written: usize = 0;
CCCryptorUpdate(self.ecb_enc, self.tag.as_ptr().cast(), 16, self.tag.as_mut_ptr().cast(), 16, &mut data_out_written);
}
self.tmp.copy_from_slice(&self.tag);
self.tmp[12] &= 0x7f;
unsafe {
if CCCryptorReset(self.ctr, self.tmp.as_ptr().cast()) != 0 {
panic!("CCryptorReset for CTR mode failed (old MacOS bug)");
}
}
}
/// Feed plaintext for second pass and write ciphertext to supplied buffer.
/// This may be called more than once.
#[inline(always)]
pub fn encrypt_second_pass(&mut self, plaintext: &[u8], ciphertext: &mut [u8]) {
unsafe {
let mut data_out_written: usize = 0;
CCCryptorUpdate(self.ctr, plaintext.as_ptr().cast(), plaintext.len(), ciphertext.as_mut_ptr().cast(), ciphertext.len(), &mut data_out_written);
}
}
/// Encrypt plaintext in place.
/// This may be called more than once.
#[inline(always)]
pub fn encrypt_second_pass_in_place(&mut self, plaintext_to_ciphertext: &mut [u8]) {
unsafe {
let mut data_out_written: usize = 0;
CCCryptorUpdate(self.ctr, plaintext_to_ciphertext.as_ptr().cast(), plaintext_to_ciphertext.len(), plaintext_to_ciphertext.as_mut_ptr().cast(), plaintext_to_ciphertext.len(), &mut data_out_written);
}
}
/// Finish second pass and return a reference to the tag for this message.
/// The tag returned remains valid until reset() is called.
#[inline(always)]
pub fn encrypt_second_pass_finish(&mut self) -> &[u8; 16] {
return &self.tag;
}
#[inline(always)]
fn decrypt_init_internal(&mut self) {
self.tmp[12] &= 0x7f;
unsafe {
if CCCryptorReset(self.ctr, self.tmp.as_ptr().cast()) != 0 {
panic!("CCryptorReset for CTR mode failed (old MacOS bug)");
}
let mut data_out_written: usize = 0;
CCCryptorUpdate(self.ecb_dec, self.tag.as_ptr().cast(), 16, self.tag.as_mut_ptr().cast(), 16, &mut data_out_written);
let tmp = self.tmp.as_mut_ptr().cast::<u64>();
*tmp = *self.tag.as_mut_ptr().cast::<u64>();
*tmp.offset(1) = 0;
CCCryptorGCMSetIV(self.gmac, self.tmp.as_ptr().cast(), 16);
}
}
/// Initialize this cipher for decryption.
/// The supplied tag must be 16 bytes in length. Any other length will panic.
#[inline(always)]
pub fn decrypt_init(&mut self, tag: &[u8]) {
self.tmp.copy_from_slice(tag);
self.tag.copy_from_slice(tag);
self.decrypt_init_internal();
}
/// Initialize this cipher for decryption from tag split into two 8-byte chunks (for ZeroTier use).
/// The supplied tag chunks must be 8 bytes in length. Any other length will panic.
#[inline(always)]
pub fn decrypt_init2(&mut self, tag_0_8: &[u8], tag_8_16: &[u8]) {
self.tmp[0..8].copy_from_slice(tag_0_8);
self.tmp[8..16].copy_from_slice(tag_8_16);
self.tag[0..8].copy_from_slice(tag_0_8);
self.tag[8..16].copy_from_slice(tag_8_16);
self.decrypt_init_internal();
}
/// Set additional authenticated data to be checked.
#[inline(always)]
pub fn decrypt_set_aad(&mut self, data: &[u8]) {
self.encrypt_set_aad(data);
}
/// Decrypt ciphertext and write to plaintext.
/// This may be called more than once.
#[inline(always)]
pub fn decrypt(&mut self, ciphertext: &[u8], plaintext: &mut [u8]) {
unsafe {
let mut data_out_written: usize = 0;
CCCryptorUpdate(self.ctr, ciphertext.as_ptr().cast(), ciphertext.len(), plaintext.as_mut_ptr().cast(), plaintext.len(), &mut data_out_written);
CCCryptorGCMAddAAD(self.gmac, plaintext.as_ptr().cast(), plaintext.len());
}
}
/// Decrypt ciphertext in place.
/// This may be called more than once.
#[inline(always)]
pub fn decrypt_in_place(&mut self, ciphertext_to_plaintext: &mut [u8]) {
unsafe {
let mut data_out_written: usize = 0;
CCCryptorUpdate(self.ctr, ciphertext_to_plaintext.as_ptr().cast(), ciphertext_to_plaintext.len(), ciphertext_to_plaintext.as_mut_ptr().cast(), ciphertext_to_plaintext.len(), &mut data_out_written);
CCCryptorGCMAddAAD(self.gmac, ciphertext_to_plaintext.as_ptr().cast(), ciphertext_to_plaintext.len());
}
}
/// Finish decryption and return true if authentication appears valid.
/// If this returns false the message should be dropped.
#[inline(always)]
pub fn decrypt_finish(&mut self) -> bool {
unsafe {
CCCryptorGCMFinalize(self.gmac, self.tmp.as_mut_ptr().cast(), 16);
let tmp = self.tmp.as_mut_ptr().cast::<u64>();
*self.tag.as_mut_ptr().cast::<u64>().offset(1) == *tmp ^ *tmp.offset(1)
}
}
}

89
aes-gmac-siv/src/lib.rs Normal file
View file

@ -0,0 +1,89 @@
#[cfg(target_os = "macos")]
mod impl_macos;
#[cfg(not(target_os = "macos"))]
mod impl_gcrypt;
#[cfg(target_os = "macos")]
pub use impl_macos::AesGmacSiv;
#[cfg(not(target_os = "macos"))]
pub use impl_gcrypt::AesGmacSiv;
pub(crate) const ZEROES: [u8; 16] = [0_u8; 16];
#[cfg(test)]
mod tests {
use crate::AesGmacSiv;
use std::time::SystemTime;
fn to_hex(b: &[u8]) -> String {
let mut s = String::new();
for c in b.iter() {
s = format!("{}{:0>2x}", s, *c);
}
s
}
#[test]
fn encrypt_decrypt() {
let aes_key: [u8; 32] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32];
let iv: [u8; 8] = [0,1,2,3,4,5,6,7];
let mut buf = [0_u8; 12345];
for i in 1..12345 {
buf[i] = i as u8;
}
let mut c = AesGmacSiv::new(&aes_key);
for _ in 1..4 {
c.reset();
c.encrypt_init(&iv);
c.encrypt_first_pass(&buf);
c.encrypt_first_pass_finish();
c.encrypt_second_pass_in_place(&mut buf);
let tag = c.encrypt_second_pass_finish().clone();
let mut sha = [0_u8; 48];
gcrypt::digest::hash(gcrypt::digest::Algorithm::Sha384, &buf, &mut sha);
let sha = to_hex(&sha);
if sha != "f455fa8a1a6badaeccdefe573a10d5d79eb7f4009b84dff3d37f9f1e95ee2b0ba6149737c0701d5ef75f58f793174d3d" {
panic!("encrypt result hash check failed!");
}
println!("Encrypt OK, tag: {}, hash: {}", to_hex(&tag), sha);
c.reset();
c.decrypt_init(&tag);
c.decrypt_in_place(&mut buf);
if !c.decrypt_finish() {
panic!("decrypt tag check failed!");
}
for i in 1..12345 {
if buf[i] != (i & 0xff) as u8 {
panic!("decrypt data check failed!");
}
}
println!("Decrypt OK");
}
let benchmark_iterations: usize = 100000;
let start = SystemTime::now();
for _ in 0..benchmark_iterations {
c.reset();
c.encrypt_init(&iv);
c.encrypt_first_pass(&buf);
c.encrypt_first_pass_finish();
c.encrypt_second_pass_in_place(&mut buf);
let _ = c.encrypt_second_pass_finish();
}
let duration = SystemTime::now().duration_since(start).unwrap();
println!("Encrypt benchmark: {} MiB/sec", (((benchmark_iterations * buf.len()) as f64) / 1048576.0) / duration.as_secs_f64());
let start = SystemTime::now();
for _ in 0..benchmark_iterations {
c.reset();
c.decrypt_init(&buf[0..16]); // we don't care if decryption is successful to benchmark, so anything will do
c.decrypt_in_place(&mut buf);
c.decrypt_finish();
}
let duration = SystemTime::now().duration_since(start).unwrap();
println!("Decrypt benchmark: {} MiB/sec", (((benchmark_iterations * buf.len()) as f64) / 1048576.0) / duration.as_secs_f64());
}
}

View file

@ -0,0 +1 @@
{"rustc_fingerprint":14118114167897737218,"outputs":{"17598535894874457435":{"success":true,"status":"","code":0,"stdout":"rustc 1.53.0 (53cb7b09b 2021-06-17)\nbinary: rustc\ncommit-hash: 53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b\ncommit-date: 2021-06-17\nhost: aarch64-apple-darwin\nrelease: 1.53.0\nLLVM version: 12.0.1\n","stderr":""},"14460901965178315333":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/api/.rustup/toolchains/stable-aarch64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"aarch64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""},"10196090686207889515":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n","stderr":""}},"successes":{}}

View file

@ -0,0 +1,3 @@
Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag created by cargo.
# For information about cache directory tags see https://bford.info/cachedir/

View file

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":16918549075539494504,"profile":16819898484982727116,"path":10872709659218687626,"deps":[[13682942004444382334,"gcrypt",false,9598029516378950314]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/aes-gmac-siv-5b1abd81e5ce9de6/dep-test-lib-aes-gmac-siv"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":7797948686568424061,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
e1202c68d40e27ed

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":16918549075539494504,"profile":15577515711213539963,"path":10872709659218687626,"deps":[[13682942004444382334,"gcrypt",false,9598029516378950314]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/aes-gmac-siv-875e596b10f5485e/dep-lib-aes-gmac-siv"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":7797948686568424061,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
4a424a22d33cfb05

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":16918549075539494504,"profile":15407205952654432191,"path":10872709659218687626,"deps":[[13682942004444382334,"gcrypt",false,10580414839537911539]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/aes-gmac-siv-b13b4d3e539005c7/dep-lib-aes-gmac-siv"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":7797948686568424061,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1,2 @@
{"message":"unused variable: `k`","code":{"code":"unused_variables","explanation":null},"level":"warning","spans":[{"file_name":"src/lib.rs","byte_start":8107,"byte_end":8108,"line_start":228,"line_end":228,"column_start":13,"column_end":14,"is_primary":true,"text":[{"text":" for k in 0..benchmark_iterations {","highlight_start":13,"highlight_end":14}],"label":null,"suggested_replacement":null,"suggestion_applicability":null,"expansion":null}],"children":[{"message":"`#[warn(unused_variables)]` on by default","code":null,"level":"note","spans":[],"children":[],"rendered":null},{"message":"if this is intentional, prefix it with an underscore","code":null,"level":"help","spans":[{"file_name":"src/lib.rs","byte_start":8107,"byte_end":8108,"line_start":228,"line_end":228,"column_start":13,"column_end":14,"is_primary":true,"text":[{"text":" for k in 0..benchmark_iterations {","highlight_start":13,"highlight_end":14}],"label":null,"suggested_replacement":"_k","suggestion_applicability":"MachineApplicable","expansion":null}],"children":[],"rendered":null}],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: unused variable: `k`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m--> \u001b[0m\u001b[0msrc/lib.rs:228:13\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m\u001b[1m\u001b[38;5;12m228\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m for k in 0..benchmark_iterations {\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m| \u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[33m^\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[33mhelp: if this is intentional, prefix it with an underscore: `_k`\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m\n\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m= \u001b[0m\u001b[0m\u001b[1mnote\u001b[0m\u001b[0m: `#[warn(unused_variables)]` on by default\u001b[0m\n\n"}
{"message":"1 warning emitted","code":null,"level":"warning","spans":[],"children":[],"rendered":"\u001b[0m\u001b[1m\u001b[33mwarning\u001b[0m\u001b[0m\u001b[1m: 1 warning emitted\u001b[0m\n\n"}

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":16918549075539494504,"profile":14850011074761641702,"path":10872709659218687626,"deps":[[13682942004444382334,"gcrypt",false,10580414839537911539]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/aes-gmac-siv-bc95c1caa14365d2/dep-test-lib-aes-gmac-siv"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":7797948686568424061,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[\"default\"]","target":10088282520713642473,"profile":15155709024791400979,"path":15258657618928579986,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/bitflags-24eb957c84cdde52/dep-build-script-build-script-build"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":14564035643000669268,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
f90db12176e10867

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[\"default\"]","target":14123478400658042508,"profile":15155709024791400979,"path":6280990280994885824,"deps":[[4117749705314174326,"build_script_build",false,14078376324211107649]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/bitflags-7e8c35729097e448/dep-lib-bitflags"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":14564035643000669268,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
4aefcb9940ee32ac

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[\"default\"]","target":14123478400658042508,"profile":4393393052366255477,"path":6280990280994885824,"deps":[[4117749705314174326,"build_script_build",false,14078376324211107649]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/bitflags-ce7067cc35224a19/dep-lib-bitflags"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":14564035643000669268,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"","target":0,"profile":0,"path":0,"deps":[[4117749705314174326,"build_script_build",false,6694058811859707076]],"local":[{"Precalculated":"1.2.1"}],"rustflags":[],"metadata":0,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
603d9afa1e99fe04

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":8289292967887089307,"profile":15155709024791400979,"path":17942303961296195309,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cc-6b5aa7e0383028dc/dep-lib-cc"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":16504835547841594983,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
d0d901035eeb4a8c

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":834697563557903354,"profile":15155709024791400979,"path":5505450662915570660,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cfg-if-be66a79b7ab460da/dep-lib-cfg-if"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":8462187951337715540,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
02439a6fc06dcff0

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":834697563557903354,"profile":14858020581876302926,"path":5505450662915570660,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cfg-if-e801ab34c04da5fd/dep-lib-cfg-if"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":8462187951337715540,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":6360621662778849253,"profile":15155709024791400979,"path":3201647282673695137,"deps":[[13837234849270857574,"cfg_if",false,10109151102596405712],[13856633986524295345,"memchr",false,11169786365150926174]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cstr-argument-b3206ac6d2945a18/dep-lib-cstr-argument"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":9024000938744404432,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":6360621662778849253,"profile":4393393052366255477,"path":3201647282673695137,"deps":[[13837234849270857574,"cfg_if",false,17352208562555339522],[13856633986524295345,"memchr",false,6597688199072254436]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/cstr-argument-e83a3bce6491455b/dep-lib-cstr-argument"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":9024000938744404432,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
f3c6ed363d2fd592

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":7410414245219738288,"profile":15155709024791400979,"path":3007036316954759984,"deps":[[2309034502573133381,"gpg_error",false,2169790023855475888],[4117749705314174326,"bitflags",false,7424431883207904761],[8575138685625640516,"cstr_argument",false,975035136654576891],[12253354796554471582,"libc",false,7945985838379286731],[17238172427721607708,"ffi",false,8086352383995432089],[18264861260725460857,"once_cell",false,10106597641972154232]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/gcrypt-205a0cd896b30512/dep-lib-gcrypt"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":6407965173056413679,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
aa4a7b1bfe0c3385

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":7410414245219738288,"profile":4393393052366255477,"path":3007036316954759984,"deps":[[2309034502573133381,"gpg_error",false,17817119011893576608],[4117749705314174326,"bitflags",false,12408241884584734538],[8575138685625640516,"cstr_argument",false,5751947610195530987],[12253354796554471582,"libc",false,8014141609551725673],[17238172427721607708,"ffi",false,3303682358718402196],[18264861260725460857,"once_cell",false,10307129930873104395]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/gcrypt-ce56f0cd2515e604/dep-lib-gcrypt"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":6407965173056413679,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"","target":0,"profile":0,"path":0,"deps":[[2309034502573133381,"build_script_build",false,7599940742217959225],[12061559290720406606,"build_script_build",false,4777124933550473066]],"local":[{"Precalculated":"0.5.2"}],"rustflags":[],"metadata":0,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":10429514197457385088,"profile":15155709024791400979,"path":9608983251023421157,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/gpg-error-714413deff4c1a86/dep-build-script-build-script-build"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":13537101048253785926,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
b08c17d987a41c1e

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":12359890186536506945,"profile":15155709024791400979,"path":11139752562894905253,"deps":[[2309034502573133381,"build_script_build",false,4305715003024726191],[12061559290720406606,"ffi",false,17319779069114316303]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/gpg-error-e5dd51e9febc3058/dep-lib-gpg-error"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":13537101048253785926,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
a083c698571f43f7

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[]","target":12359890186536506945,"profile":4393393052366255477,"path":11139752562894905253,"deps":[[2309034502573133381,"build_script_build",false,4305715003024726191],[12061559290720406606,"ffi",false,17669554582533873683]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/gpg-error-ed944ce3b7c456d9/dep-lib-gpg-error"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":13537101048253785926,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
6904024955f3376f

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[\"default\", \"std\"]","target":1047533068072243941,"profile":4393393052366255477,"path":9907868679548414995,"deps":[[12253354796554471582,"build_script_build",false,17798610239902913246]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/libc-17b8aad1382124a9/dep-lib-libc"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":14998826085014762512,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
cbc0e13605d0456e

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[\"default\", \"std\"]","target":1047533068072243941,"profile":15155709024791400979,"path":9907868679548414995,"deps":[[12253354796554471582,"build_script_build",false,17798610239902913246]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/libc-53f181f7c0156696/dep-lib-libc"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":14998826085014762512,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"","target":0,"profile":0,"path":0,"deps":[[12253354796554471582,"build_script_build",false,11333389284193054594]],"local":[{"RerunIfChanged":{"output":"debug/build/libc-733b6b3a50ff152c/output","paths":["build.rs"]}}],"rustflags":[],"metadata":0,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[\"default\", \"std\"]","target":10088282520713642473,"profile":15155709024791400979,"path":5280289430304404351,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/libc-fae2748cdf251f7e/dep-build-script-build-script-build"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":14998826085014762512,"config":0,"compile_kind":0}

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
This file has an mtime of when this was started.

View file

@ -0,0 +1 @@
{"rustc":522493025710894270,"features":"[\"shim\"]","target":265191949276201996,"profile":4393393052366255477,"path":9751790911915974202,"deps":[[12061559290720406606,"libgpg_error_sys",false,17669554582533873683],[12253354796554471582,"libc",false,8014141609551725673],[17238172427721607708,"build_script_build",false,8140724450292777910]],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/libgcrypt-sys-38778a1366bad380/dep-lib-libgcrypt_sys"}}],"rustflags":["-L","/opt/homebrew/lib"],"metadata":13891676770851331566,"config":0,"compile_kind":0}

Some files were not shown because too many files have changed in this diff Show more