use aes::{Aes128, Aes192, Aes256};
use cipher::{KeyIvInit, StreamCipher};
use ctr::Ctr128BE;
use js::context::JSContext;
use crate::dom::bindings::codegen::Bindings::CryptoKeyBinding::KeyUsage;
use crate::dom::bindings::codegen::Bindings::SubtleCryptoBinding::KeyFormat;
use crate::dom::bindings::error::Error;
use crate::dom::bindings::root::DomRoot;
use crate::dom::cryptokey::{CryptoKey, Handle};
use crate::dom::globalscope::GlobalScope;
use crate::dom::subtlecrypto::aes_common::AesAlgorithm;
use crate::dom::subtlecrypto::{
ExportedKey, SubtleAesCtrParams, SubtleAesDerivedKeyParams, SubtleAesKeyGenParams, aes_common,
};
pub(crate) fn encrypt(
normalized_algorithm: &SubtleAesCtrParams,
key: &CryptoKey,
plaintext: &[u8],
) -> Result<Vec<u8>, Error> {
if normalized_algorithm.counter.len() != 16 {
return Err(Error::Operation(Some(
"The initial counter block length is not 16 bytes".into(),
)));
}
if normalized_algorithm.length == 0 {
return Err(Error::Operation(Some("The counter length is zero".into())));
}
if normalized_algorithm.length > 128 {
return Err(Error::Operation(Some(
"The counter length is greater than 128".into(),
)));
}
let iv = normalized_algorithm.counter.as_slice();
let mut ciphertext = plaintext.to_vec();
match key.handle() {
Handle::Aes128Key(key) => {
let mut cipher = Ctr128BE::<Aes128>::new(key, iv.into());
cipher.apply_keystream(&mut ciphertext);
},
Handle::Aes192Key(key) => {
let mut cipher = Ctr128BE::<Aes192>::new(key, iv.into());
cipher.apply_keystream(&mut ciphertext);
},
Handle::Aes256Key(key) => {
let mut cipher = Ctr128BE::<Aes256>::new(key, iv.into());
cipher.apply_keystream(&mut ciphertext);
},
_ => {
return Err(Error::Operation(Some(
"The key handle is not representing an AES key".to_string(),
)));
},
};
Ok(ciphertext)
}
pub(crate) fn decrypt(
normalized_algorithm: &SubtleAesCtrParams,
key: &CryptoKey,
ciphertext: &[u8],
) -> Result<Vec<u8>, Error> {
if normalized_algorithm.counter.len() != 16 {
return Err(Error::Operation(Some(
"The initial counter block length is not 16 bytes".into(),
)));
}
if normalized_algorithm.length == 0 {
return Err(Error::Operation(Some("The counter length is zero".into())));
}
if normalized_algorithm.length > 128 {
return Err(Error::Operation(Some(
"The counter length is greater than 128".into(),
)));
}
let iv = normalized_algorithm.counter.as_slice();
let mut plaintext = ciphertext.to_vec();
match key.handle() {
Handle::Aes128Key(key) => {
let mut cipher = Ctr128BE::<Aes128>::new(key, iv.into());
cipher.apply_keystream(&mut plaintext);
},
Handle::Aes192Key(key) => {
let mut cipher = Ctr128BE::<Aes192>::new(key, iv.into());
cipher.apply_keystream(&mut plaintext);
},
Handle::Aes256Key(key) => {
let mut cipher = Ctr128BE::<Aes256>::new(key, iv.into());
cipher.apply_keystream(&mut plaintext);
},
_ => {
return Err(Error::Operation(Some(
"The key handle is not representing an AES key".to_string(),
)));
},
};
Ok(plaintext)
}
pub(crate) fn generate_key(
cx: &mut JSContext,
global: &GlobalScope,
normalized_algorithm: &SubtleAesKeyGenParams,
extractable: bool,
usages: Vec<KeyUsage>,
) -> Result<DomRoot<CryptoKey>, Error> {
aes_common::generate_key(
AesAlgorithm::AesCtr,
cx,
global,
normalized_algorithm,
extractable,
usages,
)
}
pub(crate) fn import_key(
cx: &mut JSContext,
global: &GlobalScope,
format: KeyFormat,
key_data: &[u8],
extractable: bool,
usages: Vec<KeyUsage>,
) -> Result<DomRoot<CryptoKey>, Error> {
aes_common::import_key(
AesAlgorithm::AesCtr,
cx,
global,
format,
key_data,
extractable,
usages,
)
}
pub(crate) fn export_key(format: KeyFormat, key: &CryptoKey) -> Result<ExportedKey, Error> {
aes_common::export_key(AesAlgorithm::AesCtr, format, key)
}
pub(crate) fn get_key_length(
normalized_derived_key_algorithm: &SubtleAesDerivedKeyParams,
) -> Result<Option<u32>, Error> {
aes_common::get_key_length(normalized_derived_key_algorithm)
}