use openssl::{
rand::rand_priv_bytes,
symm::{Cipher, Crypter, Mode, decrypt, decrypt_aead, encrypt, encrypt_aead},
};
use crate::{
errors::RvError,
modules::crypto::{
AEADCipher, AES, AESKeySize, BlockCipher, CipherMode, crypto_adaptors::common,
},
};
pub struct AdaptorCTX {
ctx: Crypter,
tag_set: bool,
aad_set: bool,
}
impl AES {
pub fn new(
keygen: bool,
size: Option<AESKeySize>,
mode: Option<CipherMode>,
key: Option<Vec<u8>>,
iv: Option<Vec<u8>>,
) -> Result<Self, RvError> {
common_aes_new!(keygen, size, mode, key, iv);
}
pub fn get_key_iv(&self) -> (Vec<u8>, Vec<u8>) {
common_get_key_iv!(self);
}
}
impl BlockCipher for AES {
fn encrypt(&mut self, plaintext: &Vec<u8>) -> Result<Vec<u8>, RvError> {
common_aes_encrypt!(self, plaintext);
}
fn encrypt_update(
&mut self,
plaintext: Vec<u8>,
ciphertext: &mut Vec<u8>,
) -> Result<usize, RvError> {
common_aes_encrypt_update!(self, plaintext, ciphertext);
}
fn encrypt_final(&mut self, ciphertext: &mut Vec<u8>) -> Result<usize, RvError> {
common_aes_encrypt_final!(self, ciphertext);
}
fn decrypt(&mut self, ciphertext: &Vec<u8>) -> Result<Vec<u8>, RvError> {
common_aes_decrypt!(self, ciphertext);
}
fn decrypt_update(
&mut self,
ciphertext: Vec<u8>,
plaintext: &mut Vec<u8>,
) -> Result<usize, RvError> {
common_aes_decrypt_update!(self, ciphertext, plaintext);
}
fn decrypt_final(&mut self, plaintext: &mut Vec<u8>) -> Result<usize, RvError> {
common_aes_decrypt_final!(self, plaintext);
}
}
impl AEADCipher for AES {
fn set_aad(&mut self, aad: Vec<u8>) -> Result<(), RvError> {
common_aes_set_aad!(self, aad);
}
fn get_tag(&mut self) -> Result<Vec<u8>, RvError> {
common_aes_get_tag!(self);
}
fn set_tag(&mut self, tag: Vec<u8>) -> Result<(), RvError> {
common_aes_set_tag!(self, tag);
}
}