2024-03-02 14:55:14 +01:00
|
|
|
#[cfg(feature = "encryption")]
|
2024-03-17 14:45:18 +01:00
|
|
|
use crate::encrypted_data::EncryptedData;
|
2024-03-17 14:35:26 +01:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
use crate::error::Result;
|
2024-03-02 14:55:14 +01:00
|
|
|
#[cfg(feature = "encryption")]
|
2024-02-17 16:29:54 +01:00
|
|
|
use crate::kdf::KdfFunction;
|
2024-02-15 10:56:21 +01:00
|
|
|
use crate::Error;
|
|
|
|
|
2024-03-24 12:16:54 +01:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
mod aes;
|
2024-03-02 14:55:14 +01:00
|
|
|
#[cfg(feature = "encryption")]
|
2024-02-25 14:19:56 +01:00
|
|
|
mod blake3;
|
2024-03-02 14:55:14 +01:00
|
|
|
#[cfg(feature = "encryption")]
|
2024-03-24 12:16:54 +01:00
|
|
|
mod sha2;
|
|
|
|
#[cfg(feature = "encryption")]
|
2024-02-25 14:19:56 +01:00
|
|
|
mod xchacha20poly1305;
|
|
|
|
|
2024-03-17 14:35:26 +01:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
pub(crate) type DecryptionFunction = dyn Fn(&[u8], &EncryptedData, &str) -> Result<Vec<u8>>;
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
pub(crate) type EncryptionFunction = dyn Fn(&[u8], &[u8], &[u8], &str) -> Result<EncryptedData>;
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
pub(crate) type GenNonceFunction = dyn Fn() -> Result<Vec<u8>>;
|
2024-02-25 12:03:06 +01:00
|
|
|
pub(crate) type SchemeSerializeType = u32;
|
|
|
|
|
2024-02-15 10:00:06 +01:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
pub enum Scheme {
|
|
|
|
XChaCha20Poly1305WithBlake3 = 1,
|
2024-03-24 12:16:54 +01:00
|
|
|
Aes128GcmWithSha256 = 2,
|
2024-02-15 10:00:06 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 23:45:21 +01:00
|
|
|
impl Scheme {
|
2024-03-02 11:00:59 +01:00
|
|
|
pub(crate) fn get_ikm_size(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
Scheme::XChaCha20Poly1305WithBlake3 => 32,
|
2024-03-24 12:16:54 +01:00
|
|
|
Scheme::Aes128GcmWithSha256 => 32,
|
2024-03-02 11:00:59 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-16 11:22:08 +01:00
|
|
|
}
|
2024-03-02 11:00:59 +01:00
|
|
|
|
2024-03-16 11:22:08 +01:00
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
impl Scheme {
|
2024-02-17 16:29:54 +01:00
|
|
|
pub(crate) fn get_kdf(&self) -> Box<KdfFunction> {
|
2024-02-15 23:45:21 +01:00
|
|
|
match self {
|
2024-02-25 14:19:56 +01:00
|
|
|
Scheme::XChaCha20Poly1305WithBlake3 => Box::new(blake3::blake3_derive),
|
2024-03-24 12:16:54 +01:00
|
|
|
Scheme::Aes128GcmWithSha256 => Box::new(sha2::sha256_derive),
|
2024-02-15 23:45:21 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-17 20:26:45 +01:00
|
|
|
|
2024-02-25 13:40:19 +01:00
|
|
|
pub(crate) fn get_decryption(&self) -> Box<DecryptionFunction> {
|
|
|
|
match self {
|
|
|
|
Scheme::XChaCha20Poly1305WithBlake3 => {
|
2024-02-25 14:19:56 +01:00
|
|
|
Box::new(xchacha20poly1305::xchacha20poly1305_decrypt)
|
2024-02-25 13:40:19 +01:00
|
|
|
}
|
2024-03-24 12:16:54 +01:00
|
|
|
Scheme::Aes128GcmWithSha256 => Box::new(aes::aes128gcm_decrypt),
|
2024-02-25 13:40:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-17 20:26:45 +01:00
|
|
|
pub(crate) fn get_encryption(&self) -> Box<EncryptionFunction> {
|
|
|
|
match self {
|
|
|
|
Scheme::XChaCha20Poly1305WithBlake3 => {
|
2024-02-25 14:19:56 +01:00
|
|
|
Box::new(xchacha20poly1305::xchacha20poly1305_encrypt)
|
2024-02-17 20:26:45 +01:00
|
|
|
}
|
2024-03-24 12:16:54 +01:00
|
|
|
Scheme::Aes128GcmWithSha256 => Box::new(aes::aes128gcm_encrypt),
|
2024-02-17 20:26:45 +01:00
|
|
|
}
|
|
|
|
}
|
2024-03-11 14:55:08 +01:00
|
|
|
|
|
|
|
pub(crate) fn get_gen_nonce(&self) -> Box<GenNonceFunction> {
|
|
|
|
match self {
|
|
|
|
Scheme::XChaCha20Poly1305WithBlake3 => {
|
|
|
|
Box::new(xchacha20poly1305::xchacha20poly1305_gen_nonce)
|
|
|
|
}
|
2024-03-24 12:16:54 +01:00
|
|
|
Scheme::Aes128GcmWithSha256 => Box::new(aes::aes128gcm_gen_nonce),
|
2024-03-11 14:55:08 +01:00
|
|
|
}
|
|
|
|
}
|
2024-02-15 23:45:21 +01:00
|
|
|
}
|
|
|
|
|
2024-02-25 12:03:06 +01:00
|
|
|
impl TryFrom<SchemeSerializeType> for Scheme {
|
2024-02-15 10:56:21 +01:00
|
|
|
type Error = Error;
|
2024-02-15 10:00:06 +01:00
|
|
|
|
2024-02-25 12:03:06 +01:00
|
|
|
fn try_from(value: SchemeSerializeType) -> Result<Self, Self::Error> {
|
2024-02-15 10:00:06 +01:00
|
|
|
match value {
|
|
|
|
1 => Ok(Scheme::XChaCha20Poly1305WithBlake3),
|
2024-03-24 12:16:54 +01:00
|
|
|
2 => Ok(Scheme::Aes128GcmWithSha256),
|
2024-02-25 11:46:08 +01:00
|
|
|
_ => Err(Error::ParsingSchemeUnknownScheme(value)),
|
2024-02-15 10:00:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|