use crate::Result;
use super::{SymmetricCrypto, QuantumCipher};
#[derive(Debug, Clone, Copy)]
#[derive(Default)]
pub enum CipherAlgorithm {
Aes256Gcm,
#[default]
Sha3Stream,
}
#[allow(clippy::large_enum_variant)]
pub enum UniversalCipher {
Aes(SymmetricCrypto),
Sha3(QuantumCipher),
}
impl UniversalCipher {
pub fn from_shared_secret(shared_secret: &[u8], algorithm: CipherAlgorithm) -> Result<Self> {
match algorithm {
CipherAlgorithm::Aes256Gcm => {
Ok(UniversalCipher::Aes(SymmetricCrypto::from_shared_secret(shared_secret)?))
}
CipherAlgorithm::Sha3Stream => {
Ok(UniversalCipher::Sha3(QuantumCipher::from_shared_secret(shared_secret)?))
}
}
}
pub fn encrypt(&mut self, plaintext: &[u8]) -> Result<(Vec<u8>, Vec<u8>)> {
match self {
UniversalCipher::Aes(cipher) => cipher.encrypt(plaintext),
UniversalCipher::Sha3(cipher) => cipher.encrypt(plaintext),
}
}
pub fn decrypt(&self, ciphertext: &[u8], nonce: &[u8]) -> Result<Vec<u8>> {
match self {
UniversalCipher::Aes(cipher) => cipher.decrypt(ciphertext, nonce),
UniversalCipher::Sha3(cipher) => cipher.decrypt(ciphertext, nonce),
}
}
pub fn algorithm_name(&self) -> &str {
match self {
UniversalCipher::Aes(_) => "AES-256-GCM",
UniversalCipher::Sha3(_) => "SHA3-256-CTR",
}
}
}