renamed crypto impls

This commit is contained in:
Monica Moniot
2023-08-16 12:10:14 -04:00
parent 5739911ddd
commit 5a9296c2ca
7 changed files with 83 additions and 83 deletions
+17 -17
View File
@@ -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<TestApplication> {
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<Option<RatchetStates>, ()> {
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<Vec<u8>>,
alice_in: mpsc::Receiver<Vec<u8>>,
recursive_out: mpsc::SyncSender<Vec<u8>>,
alice_keypair: P384CrateKeyPair,
bob_pubkey: P384CratePublicKey,
alice_keypair: CrateP384KeyPair,
bob_pubkey: CrateP384PublicKey,
) {
let startup_time = std::time::Instant::now();
let context = zssp::Context::<TestApplication>::new(alice_keypair, OsRng);
@@ -251,7 +251,7 @@ fn bob_main(
bob_out: mpsc::SyncSender<Vec<u8>>,
bob_in: mpsc::Receiver<Vec<u8>>,
recursive_out: mpsc::SyncSender<Vec<u8>>,
bob_keypair: P384CrateKeyPair,
bob_keypair: CrateP384KeyPair,
) {
let startup_time = std::time::Instant::now();
let context = zssp::Context::<TestApplication>::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(),
+8 -8
View File
@@ -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<TestApplication> {
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<Option<RatchetStates>, ()> {
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<PooledVec>,
alice_in: mpsc::Receiver<PooledVec>,
alice_keypair: P384CrateKeyPair,
bob_pubkey: P384CratePublicKey,
alice_keypair: CrateP384KeyPair,
bob_pubkey: CrateP384PublicKey,
) {
let startup_time = std::time::Instant::now();
let context = zssp::Context::<TestApplication>::new(alice_keypair, OsRng);
@@ -211,7 +211,7 @@ fn bob_main(
bob_app: &TestApplication,
bob_out: mpsc::SyncSender<PooledVec>,
bob_in: mpsc::Receiver<PooledVec>,
bob_keypair: P384CrateKeyPair,
bob_keypair: CrateP384KeyPair,
) {
let startup_time = std::time::Instant::now();
let context = zssp::Context::<TestApplication>::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() };
+2 -2
View File
@@ -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<Rng: RngCore + CryptoRng> Kyber1024PrivateKey<Rng> for Kyber1024CratePrivateKey {
pub type CrateKyber1024PrivateKey = Zeroizing<[u8; pqc_kyber::KYBER_SECRETKEYBYTES]>;
impl<Rng: RngCore + CryptoRng> Kyber1024PrivateKey<Rng> 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.
+9 -9
View File
@@ -36,15 +36,15 @@ pub trait DefaultCrypto {
#[cfg(feature = "default-crypto")]
impl<C: DefaultCrypto> 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;
+38 -38
View File
@@ -7,18 +7,18 @@ use openssl_sys::*;
use crate::crypto::*;
pub struct CipherCtx(NonNull<openssl_sys::EVP_CIPHER_CTX>);
impl Drop for CipherCtx {
pub struct OpenSSLCtx(NonNull<openssl_sys::EVP_CIPHER_CTX>);
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<Self> {
unsafe { Some(CipherCtx(NonNull::new(EVP_CIPHER_CTX_new())?)) }
unsafe { Some(OpenSSLCtx(NonNull::new(EVP_CIPHER_CTX_new())?)) }
}
pub unsafe fn cipher_init<const ENCRYPT: bool>(
@@ -88,13 +88,13 @@ impl CipherCtx {
}
}
pub struct Aes256OpenSSLEnc(Mutex<CipherCtx>);
unsafe impl Send for Aes256OpenSSLEnc {}
unsafe impl Sync for Aes256OpenSSLEnc {}
pub struct OpenSSLAes256Enc(Mutex<OpenSSLCtx>);
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::<true>(t, key.as_ptr(), ptr::null()));
@@ -118,13 +118,13 @@ impl Aes256Enc for Aes256OpenSSLEnc {
unsafe { assert!(ctx.update::<true>(block, ptr)) }
}
}
pub struct Aes256OpenSSLDec(Mutex<CipherCtx>);
unsafe impl Send for Aes256OpenSSLDec {}
unsafe impl Sync for Aes256OpenSSLDec {}
pub struct OpenSSLAes256Dec(Mutex<OpenSSLCtx>);
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::<false>(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::<true>(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::<false>(data, p)) };
@@ -177,30 +177,30 @@ impl<'a> AesGcmDecContext for AesGcmOpenSSLDec<'a> {
}
}
pub struct AesGcmOpenSSLPool {
enc: [Mutex<CipherCtx>; 8],
dec: [Mutex<CipherCtx>; 8],
pub struct OpenSSLAesGcmPool {
enc: [Mutex<OpenSSLCtx>; 8],
dec: [Mutex<OpenSSLCtx>; 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::<true>(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::<false>(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::<true>(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::<false>(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::<true>(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::<false>(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::<true>(openssl_sys::EVP_aes_128_ecb(), key.as_ptr(), ptr::null()));
openssl_sys::EVP_CIPHER_CTX_set_padding(ctx.as_ptr(), 0);
+4 -4
View File
@@ -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<Self> {
PublicKey::from_sec1_bytes(raw_key).ok()
}
@@ -15,8 +15,8 @@ impl P384PublicKey for P384CratePublicKey {
}
}
pub type P384CrateKeyPair = EphemeralSecret;
impl<Rng: RngCore + CryptoRng> P384KeyPair<Rng> for P384CrateKeyPair {
pub type CrateP384KeyPair = EphemeralSecret;
impl<Rng: RngCore + CryptoRng> P384KeyPair<Rng> for CrateP384KeyPair {
type PublicKey = PublicKey;
fn generate(rng: &mut Rng) -> Self {
+5 -5
View File
@@ -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]) {