diff --git a/examples/basic_test.rs b/examples/basic_test.rs index 58a8ed5..1c5534c 100644 --- a/examples/basic_test.rs +++ b/examples/basic_test.rs @@ -49,15 +49,15 @@ impl CryptoLayer for TestApplication { }; type Rng = OsRng; - type PrpEnc = Aes256OpenSSLEnc; - type PrpDec = Aes256OpenSSLDec; - type Aead = AesGcmOpenSSL; - type AeadPool = AesGcmOpenSSLPool; - type Hash = Sha512Crate; - type Hmac = HmacSha512Crate; - type PublicKey = P384CratePublicKey; - type KeyPair = P384CrateKeyPair; - type Kem = Kyber1024CratePrivateKey; + type PrpEnc = OpenSSLAes256Enc; + type PrpDec = OpenSSLAes256Dec; + type Aead = OpenSSLAesGcm; + type AeadPool = OpenSSLAesGcmPool; + type Hash = CrateSha512; + type Hmac = CrateHmacSha512; + type PublicKey = CrateP384PublicKey; + type KeyPair = CrateP384KeyPair; + type Kem = CrateKyber1024PrivateKey; type SessionData = u128; @@ -81,7 +81,7 @@ impl ApplicationLayer for &TestApplication { fn check_accept_session( &mut self, - remote_static_key: &P384CratePublicKey, + remote_static_key: &CrateP384PublicKey, identity: &[u8], ) -> AcceptAction { AcceptAction { @@ -98,7 +98,7 @@ impl ApplicationLayer for &TestApplication { fn restore_by_identity( &mut self, - remote_static_key: &P384CratePublicKey, + remote_static_key: &CrateP384PublicKey, session_data: &u128, ) -> Result, ()> { let ratchets = self.ratchets.lock().unwrap(); @@ -107,7 +107,7 @@ impl ApplicationLayer for &TestApplication { fn save_ratchet_state( &mut self, - remote_static_key: &P384CratePublicKey, + remote_static_key: &CrateP384PublicKey, session_data: &u128, update_data: RatchetUpdate<'_>, ) -> Result<(), ()> { @@ -144,8 +144,8 @@ fn alice_main( alice_out: mpsc::SyncSender>, alice_in: mpsc::Receiver>, recursive_out: mpsc::SyncSender>, - alice_keypair: P384CrateKeyPair, - bob_pubkey: P384CratePublicKey, + alice_keypair: CrateP384KeyPair, + bob_pubkey: CrateP384PublicKey, ) { let startup_time = std::time::Instant::now(); let context = zssp::Context::::new(alice_keypair, OsRng); @@ -251,7 +251,7 @@ fn bob_main( bob_out: mpsc::SyncSender>, bob_in: mpsc::Receiver>, recursive_out: mpsc::SyncSender>, - bob_keypair: P384CrateKeyPair, + bob_keypair: CrateP384KeyPair, ) { let startup_time = std::time::Instant::now(); let context = zssp::Context::::new(bob_keypair, OsRng); @@ -335,13 +335,13 @@ fn bob_main( fn core(time: u64, packet_success_rate: u32) { let run = &AtomicBool::new(true); - let alice_keypair = P384CrateKeyPair::generate(&mut OsRng); + let alice_keypair = CrateP384KeyPair::generate(&mut OsRng); let alice_app = TestApplication { time: Instant::now(), name: "alice", ratchets: Mutex::new(Ratchets::new()), }; - let bob_keypair = P384CrateKeyPair::generate(&mut OsRng); + let bob_keypair = CrateP384KeyPair::generate(&mut OsRng); let bob_pubkey = bob_keypair.public_key(); let bob_app = TestApplication { time: Instant::now(), diff --git a/examples/benchmark.rs b/examples/benchmark.rs index cc30885..30778ff 100644 --- a/examples/benchmark.rs +++ b/examples/benchmark.rs @@ -75,7 +75,7 @@ impl ApplicationLayer for &TestApplication { fn check_accept_session( &mut self, - remote_static_key: &P384CratePublicKey, + remote_static_key: &CrateP384PublicKey, identity: &[u8], ) -> AcceptAction { AcceptAction { @@ -91,7 +91,7 @@ impl ApplicationLayer for &TestApplication { fn restore_by_identity( &mut self, - remote_static_key: &P384CratePublicKey, + remote_static_key: &CrateP384PublicKey, session_data: &(), ) -> Result, ()> { Ok(None) @@ -99,7 +99,7 @@ impl ApplicationLayer for &TestApplication { fn save_ratchet_state( &mut self, - remote_static_key: &P384CratePublicKey, + remote_static_key: &CrateP384PublicKey, session_data: &(), update_data: RatchetUpdate<'_>, ) -> Result<(), ()> { @@ -117,8 +117,8 @@ fn alice_main( alice_app: &TestApplication, alice_out: mpsc::SyncSender, alice_in: mpsc::Receiver, - alice_keypair: P384CrateKeyPair, - bob_pubkey: P384CratePublicKey, + alice_keypair: CrateP384KeyPair, + bob_pubkey: CrateP384PublicKey, ) { let startup_time = std::time::Instant::now(); let context = zssp::Context::::new(alice_keypair, OsRng); @@ -211,7 +211,7 @@ fn bob_main( bob_app: &TestApplication, bob_out: mpsc::SyncSender, bob_in: mpsc::Receiver, - bob_keypair: P384CrateKeyPair, + bob_keypair: CrateP384KeyPair, ) { let startup_time = std::time::Instant::now(); let context = zssp::Context::::new(bob_keypair, OsRng); @@ -292,9 +292,9 @@ fn bob_main( fn core(time: u64) { let run = &AtomicBool::new(true); - let alice_keypair = P384CrateKeyPair::generate(&mut OsRng); + let alice_keypair = CrateP384KeyPair::generate(&mut OsRng); let alice_app = TestApplication { time: Instant::now() }; - let bob_keypair = P384CrateKeyPair::generate(&mut OsRng); + let bob_keypair = CrateP384KeyPair::generate(&mut OsRng); let bob_pubkey = bob_keypair.public_key(); let bob_app = TestApplication { time: Instant::now() }; diff --git a/src/crypto_impl/kyber1024.rs b/src/crypto_impl/kyber1024.rs index dd50978..ef553c2 100644 --- a/src/crypto_impl/kyber1024.rs +++ b/src/crypto_impl/kyber1024.rs @@ -5,8 +5,8 @@ use crate::crypto::*; /// A wrapper for a buffer the size of a pqc_kyber secret key. /// The crate `pqc_kyber` is low level and operates directly on buffers of bytes. -pub type Kyber1024CratePrivateKey = Zeroizing<[u8; pqc_kyber::KYBER_SECRETKEYBYTES]>; -impl Kyber1024PrivateKey for Kyber1024CratePrivateKey { +pub type CrateKyber1024PrivateKey = Zeroizing<[u8; pqc_kyber::KYBER_SECRETKEYBYTES]>; +impl Kyber1024PrivateKey for CrateKyber1024PrivateKey { fn generate(rng: &mut Rng) -> (Self, [u8; KYBER_PUBLIC_KEY_SIZE]) { // According to the source code this can only fail if the RNG fails. // Idk why rust allows RNG to fail. diff --git a/src/crypto_impl/mod.rs b/src/crypto_impl/mod.rs index e08234b..8aa0ceb 100644 --- a/src/crypto_impl/mod.rs +++ b/src/crypto_impl/mod.rs @@ -36,15 +36,15 @@ pub trait DefaultCrypto { #[cfg(feature = "default-crypto")] impl crate::application::CryptoLayer for C { type Rng = rand_core::OsRng; - type PrpEnc = Aes256OpenSSLEnc; - type PrpDec = Aes256OpenSSLDec; - type Aead = AesGcmOpenSSL; - type AeadPool = AesGcmOpenSSLPool; - type Hash = Sha512Crate; - type Hmac = HmacSha512Crate; - type PublicKey = P384CratePublicKey; - type KeyPair = P384CrateKeyPair; - type Kem = Kyber1024CratePrivateKey; + type PrpEnc = OpenSSLAes256Enc; + type PrpDec = OpenSSLAes256Dec; + type Aead = OpenSSLAesGcm; + type AeadPool = OpenSSLAesGcmPool; + type Hash = CrateSha512; + type Hmac = CrateHmacSha512; + type PublicKey = CrateP384PublicKey; + type KeyPair = CrateP384KeyPair; + type Kem = CrateKyber1024PrivateKey; type SessionData = C::SessionData; type IncomingPacketBuffer = C::IncomingPacketBuffer; diff --git a/src/crypto_impl/openssl.rs b/src/crypto_impl/openssl.rs index 43125ae..8a73e92 100644 --- a/src/crypto_impl/openssl.rs +++ b/src/crypto_impl/openssl.rs @@ -7,18 +7,18 @@ use openssl_sys::*; use crate::crypto::*; -pub struct CipherCtx(NonNull); -impl Drop for CipherCtx { +pub struct OpenSSLCtx(NonNull); +impl Drop for OpenSSLCtx { fn drop(&mut self) { unsafe { EVP_CIPHER_CTX_free(self.0.as_ptr()); } } } -impl CipherCtx { +impl OpenSSLCtx { /// Creates a new context. pub fn new() -> Option { - unsafe { Some(CipherCtx(NonNull::new(EVP_CIPHER_CTX_new())?)) } + unsafe { Some(OpenSSLCtx(NonNull::new(EVP_CIPHER_CTX_new())?)) } } pub unsafe fn cipher_init( @@ -88,13 +88,13 @@ impl CipherCtx { } } -pub struct Aes256OpenSSLEnc(Mutex); -unsafe impl Send for Aes256OpenSSLEnc {} -unsafe impl Sync for Aes256OpenSSLEnc {} +pub struct OpenSSLAes256Enc(Mutex); +unsafe impl Send for OpenSSLAes256Enc {} +unsafe impl Sync for OpenSSLAes256Enc {} -impl Aes256Enc for Aes256OpenSSLEnc { +impl Aes256Enc for OpenSSLAes256Enc { fn new(key: &[u8; AES_256_KEY_SIZE]) -> Self { - let ctx = CipherCtx::new().unwrap(); + let ctx = OpenSSLCtx::new().unwrap(); unsafe { let t = openssl_sys::EVP_aes_256_ecb(); assert!(ctx.cipher_init::(t, key.as_ptr(), ptr::null())); @@ -118,13 +118,13 @@ impl Aes256Enc for Aes256OpenSSLEnc { unsafe { assert!(ctx.update::(block, ptr)) } } } -pub struct Aes256OpenSSLDec(Mutex); -unsafe impl Send for Aes256OpenSSLDec {} -unsafe impl Sync for Aes256OpenSSLDec {} +pub struct OpenSSLAes256Dec(Mutex); +unsafe impl Send for OpenSSLAes256Dec {} +unsafe impl Sync for OpenSSLAes256Dec {} -impl Aes256Dec for Aes256OpenSSLDec { +impl Aes256Dec for OpenSSLAes256Dec { fn new(key: &[u8; AES_256_KEY_SIZE]) -> Self { - let ctx = CipherCtx::new().unwrap(); + let ctx = OpenSSLCtx::new().unwrap(); unsafe { let t = openssl_sys::EVP_aes_256_ecb(); assert!(ctx.cipher_init::(t, key.as_ptr(), ptr::null())); @@ -149,8 +149,8 @@ impl Aes256Dec for Aes256OpenSSLDec { } } -pub struct AesGcmOpenSSLEnc<'a>(MutexGuard<'a, CipherCtx>); -impl<'a> AesGcmEncContext for AesGcmOpenSSLEnc<'a> { +pub struct OpenSSLAesGcmEnc<'a>(MutexGuard<'a, OpenSSLCtx>); +impl<'a> AesGcmEncContext for OpenSSLAesGcmEnc<'a> { fn encrypt(&mut self, input: &[u8], output: &mut [u8]) { unsafe { assert!(self.0.update::(input, output.as_mut_ptr())) }; } @@ -165,8 +165,8 @@ impl<'a> AesGcmEncContext for AesGcmOpenSSLEnc<'a> { } } -pub struct AesGcmOpenSSLDec<'a>(MutexGuard<'a, CipherCtx>); -impl<'a> AesGcmDecContext for AesGcmOpenSSLDec<'a> { +pub struct OpenSSLAesGcmDec<'a>(MutexGuard<'a, OpenSSLCtx>); +impl<'a> AesGcmDecContext for OpenSSLAesGcmDec<'a> { fn decrypt_in_place(&mut self, data: &mut [u8]) { let p = data.as_mut_ptr(); unsafe { assert!(self.0.update::(data, p)) }; @@ -177,30 +177,30 @@ impl<'a> AesGcmDecContext for AesGcmOpenSSLDec<'a> { } } -pub struct AesGcmOpenSSLPool { - enc: [Mutex; 8], - dec: [Mutex; 8], +pub struct OpenSSLAesGcmPool { + enc: [Mutex; 8], + dec: [Mutex; 8], } -unsafe impl Send for AesGcmOpenSSLPool {} -unsafe impl Sync for AesGcmOpenSSLPool {} +unsafe impl Send for OpenSSLAesGcmPool {} +unsafe impl Sync for OpenSSLAesGcmPool {} -impl HighThroughputAesGcmPool for AesGcmOpenSSLPool { - type EncContext<'a> = AesGcmOpenSSLEnc<'a>; +impl HighThroughputAesGcmPool for OpenSSLAesGcmPool { + type EncContext<'a> = OpenSSLAesGcmEnc<'a>; - type DecContext<'a> = AesGcmOpenSSLDec<'a>; + type DecContext<'a> = OpenSSLAesGcmDec<'a>; fn new(encrypt_key: &[u8; AES_256_KEY_SIZE], decrypt_key: &[u8; AES_256_KEY_SIZE]) -> Self { unsafe { - AesGcmOpenSSLPool { + OpenSSLAesGcmPool { enc: std::array::from_fn(|_| { - let ctx = CipherCtx::new().unwrap(); + let ctx = OpenSSLCtx::new().unwrap(); let t = openssl_sys::EVP_aes_256_gcm(); assert!(ctx.cipher_init::(t, encrypt_key.as_ptr(), ptr::null())); openssl_sys::EVP_CIPHER_CTX_set_padding(ctx.as_ptr(), 0); Mutex::new(ctx) }), dec: std::array::from_fn(|_| { - let ctx = CipherCtx::new().unwrap(); + let ctx = OpenSSLCtx::new().unwrap(); let t = openssl_sys::EVP_aes_256_gcm(); assert!(ctx.cipher_init::(t, decrypt_key.as_ptr(), ptr::null())); openssl_sys::EVP_CIPHER_CTX_set_padding(ctx.as_ptr(), 0); @@ -210,27 +210,27 @@ impl HighThroughputAesGcmPool for AesGcmOpenSSLPool { } } - fn start_enc<'a>(&'a self, nonce: &[u8; AES_GCM_NONCE_SIZE]) -> AesGcmOpenSSLEnc { + fn start_enc<'a>(&'a self, nonce: &[u8; AES_GCM_NONCE_SIZE]) -> OpenSSLAesGcmEnc { let i = u64::from_be_bytes(nonce[4..].try_into().unwrap()); let g = self.enc[(i as usize) % self.enc.len()].lock().unwrap(); unsafe { assert!(g.cipher_init::(ptr::null(), ptr::null(), nonce.as_ptr())); } - AesGcmOpenSSLEnc(g) + OpenSSLAesGcmEnc(g) } - fn start_dec<'a>(&'a self, nonce: &[u8; AES_GCM_NONCE_SIZE]) -> AesGcmOpenSSLDec { + fn start_dec<'a>(&'a self, nonce: &[u8; AES_GCM_NONCE_SIZE]) -> OpenSSLAesGcmDec { let i = u64::from_be_bytes(nonce[4..].try_into().unwrap()); let g = self.dec[(i as usize) % self.enc.len()].lock().unwrap(); unsafe { assert!(g.cipher_init::(ptr::null(), ptr::null(), nonce.as_ptr())); } - AesGcmOpenSSLDec(g) + OpenSSLAesGcmDec(g) } } -pub struct AesGcmOpenSSL; -impl LowThroughputAesGcm for AesGcmOpenSSL { +pub struct OpenSSLAesGcm; +impl LowThroughputAesGcm for OpenSSLAesGcm { fn encrypt_in_place( key: &[u8; AES_256_KEY_SIZE], nonce: &[u8; AES_GCM_NONCE_SIZE], @@ -238,7 +238,7 @@ impl LowThroughputAesGcm for AesGcmOpenSSL { data: &mut [u8], ) -> [u8; AES_GCM_TAG_SIZE] { let mut output = [0u8; AES_GCM_TAG_SIZE]; - let ctx = CipherCtx::new().unwrap(); + let ctx = OpenSSLCtx::new().unwrap(); unsafe { let t = openssl_sys::EVP_aes_256_gcm(); assert!(ctx.cipher_init::(t, key.as_ptr(), nonce.as_ptr())); @@ -261,7 +261,7 @@ impl LowThroughputAesGcm for AesGcmOpenSSL { data: &mut [u8], tag: &[u8; AES_GCM_TAG_SIZE], ) -> bool { - let ctx = CipherCtx::new().unwrap(); + let ctx = OpenSSLCtx::new().unwrap(); unsafe { let t = openssl_sys::EVP_aes_256_gcm(); assert!(ctx.cipher_init::(t, key.as_ptr(), nonce.as_ptr())); @@ -282,7 +282,7 @@ mod test { #[test] fn aes_128_ecb() { let key = [1u8; 16]; - let ctx = CipherCtx::new().unwrap(); + let ctx = OpenSSLCtx::new().unwrap(); unsafe { assert!(ctx.cipher_init::(openssl_sys::EVP_aes_128_ecb(), key.as_ptr(), ptr::null())); openssl_sys::EVP_CIPHER_CTX_set_padding(ctx.as_ptr(), 0); diff --git a/src/crypto_impl/p384_impl.rs b/src/crypto_impl/p384_impl.rs index 77c0938..17bfb1b 100644 --- a/src/crypto_impl/p384_impl.rs +++ b/src/crypto_impl/p384_impl.rs @@ -3,8 +3,8 @@ use rand_core::{CryptoRng, RngCore}; use crate::crypto::*; -pub type P384CratePublicKey = PublicKey; -impl P384PublicKey for P384CratePublicKey { +pub type CrateP384PublicKey = PublicKey; +impl P384PublicKey for CrateP384PublicKey { fn from_bytes(raw_key: &[u8; P384_PUBLIC_KEY_SIZE]) -> Option { PublicKey::from_sec1_bytes(raw_key).ok() } @@ -15,8 +15,8 @@ impl P384PublicKey for P384CratePublicKey { } } -pub type P384CrateKeyPair = EphemeralSecret; -impl P384KeyPair for P384CrateKeyPair { +pub type CrateP384KeyPair = EphemeralSecret; +impl P384KeyPair for CrateP384KeyPair { type PublicKey = PublicKey; fn generate(rng: &mut Rng) -> Self { diff --git a/src/crypto_impl/sha512.rs b/src/crypto_impl/sha512.rs index ba932f8..1a1bd4d 100644 --- a/src/crypto_impl/sha512.rs +++ b/src/crypto_impl/sha512.rs @@ -3,8 +3,8 @@ use sha2::{Digest, Sha512}; use crate::crypto::*; -pub type Sha512Crate = Sha512; -impl Sha512Hash for Sha512Crate { +pub type CrateSha512 = Sha512; +impl Sha512Hash for CrateSha512 { fn new() -> Self { Digest::new() } @@ -20,10 +20,10 @@ impl Sha512Hash for Sha512Crate { } } -pub struct HmacSha512Crate; -impl Sha512Hmac for HmacSha512Crate { +pub struct CrateHmacSha512; +impl Sha512Hmac for CrateHmacSha512 { fn new() -> Self { - HmacSha512Crate + CrateHmacSha512 } fn hash(&mut self, key: &[u8], full_input: &[u8], output: &mut [u8; SHA512_HASH_SIZE]) {