2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-01-03 06:27:28 -05:00
|
|
|
|
2022-06-20 07:23:57 -04:00
|
|
|
use aes::cipher::block_padding::Pkcs7;
|
|
|
|
use aes::cipher::BlockEncryptMut;
|
|
|
|
use aes::cipher::KeyIvInit;
|
|
|
|
use aes::cipher::StreamCipher;
|
2022-01-05 10:12:30 -05:00
|
|
|
use aes_gcm::aead::generic_array::typenum::U12;
|
2022-03-02 00:26:10 -05:00
|
|
|
use aes_gcm::aead::generic_array::typenum::U16;
|
|
|
|
use aes_gcm::aead::generic_array::ArrayLength;
|
|
|
|
use aes_gcm::aes::Aes128;
|
2022-01-05 10:12:30 -05:00
|
|
|
use aes_gcm::aes::Aes192;
|
2022-03-02 00:26:10 -05:00
|
|
|
use aes_gcm::aes::Aes256;
|
2022-01-05 10:12:30 -05:00
|
|
|
use aes_gcm::AeadInPlace;
|
2022-09-16 19:11:30 -04:00
|
|
|
use aes_gcm::KeyInit;
|
2022-01-05 10:12:30 -05:00
|
|
|
use aes_gcm::Nonce;
|
2022-06-20 07:23:57 -04:00
|
|
|
use ctr::Ctr128BE;
|
|
|
|
use ctr::Ctr32BE;
|
|
|
|
use ctr::Ctr64BE;
|
2023-09-13 11:54:19 -04:00
|
|
|
use deno_core::op2;
|
2023-08-23 19:03:05 -04:00
|
|
|
use deno_core::unsync::spawn_blocking;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::JsBuffer;
|
|
|
|
use deno_core::ToJsBuffer;
|
2021-12-15 16:18:26 -05:00
|
|
|
use rand::rngs::OsRng;
|
2022-06-20 07:23:57 -04:00
|
|
|
use rsa::pkcs1::DecodeRsaPublicKey;
|
2021-12-15 16:18:26 -05:00
|
|
|
use serde::Deserialize;
|
|
|
|
use sha1::Sha1;
|
|
|
|
use sha2::Sha256;
|
|
|
|
use sha2::Sha384;
|
|
|
|
use sha2::Sha512;
|
|
|
|
|
2023-01-13 02:51:32 -05:00
|
|
|
use crate::shared::*;
|
|
|
|
|
2021-12-15 16:18:26 -05:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct EncryptOptions {
|
2023-06-22 17:37:56 -04:00
|
|
|
key: V8RawKeyData,
|
2021-12-15 16:18:26 -05:00
|
|
|
#[serde(flatten)]
|
|
|
|
algorithm: EncryptAlgorithm,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", tag = "algorithm")]
|
|
|
|
pub enum EncryptAlgorithm {
|
|
|
|
#[serde(rename = "RSA-OAEP")]
|
|
|
|
RsaOaep {
|
|
|
|
hash: ShaHash,
|
|
|
|
#[serde(with = "serde_bytes")]
|
|
|
|
label: Vec<u8>,
|
|
|
|
},
|
|
|
|
#[serde(rename = "AES-CBC", rename_all = "camelCase")]
|
|
|
|
AesCbc {
|
|
|
|
#[serde(with = "serde_bytes")]
|
|
|
|
iv: Vec<u8>,
|
|
|
|
length: usize,
|
|
|
|
},
|
2022-01-05 10:12:30 -05:00
|
|
|
#[serde(rename = "AES-GCM", rename_all = "camelCase")]
|
|
|
|
AesGcm {
|
|
|
|
#[serde(with = "serde_bytes")]
|
|
|
|
iv: Vec<u8>,
|
|
|
|
#[serde(with = "serde_bytes")]
|
|
|
|
additional_data: Option<Vec<u8>>,
|
|
|
|
length: usize,
|
|
|
|
tag_length: usize,
|
|
|
|
},
|
2022-01-03 06:27:28 -05:00
|
|
|
#[serde(rename = "AES-CTR", rename_all = "camelCase")]
|
|
|
|
AesCtr {
|
|
|
|
#[serde(with = "serde_bytes")]
|
|
|
|
counter: Vec<u8>,
|
|
|
|
ctr_length: usize,
|
|
|
|
key_length: usize,
|
|
|
|
},
|
2021-12-15 16:18:26 -05:00
|
|
|
}
|
2022-03-14 13:44:15 -04:00
|
|
|
|
2024-10-18 18:23:20 -04:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum EncryptError {
|
|
|
|
#[error(transparent)]
|
|
|
|
General(#[from] SharedError),
|
|
|
|
#[error("invalid length")]
|
|
|
|
InvalidLength,
|
|
|
|
#[error("invalid key or iv")]
|
|
|
|
InvalidKeyOrIv,
|
|
|
|
#[error("iv length not equal to 12 or 16")]
|
|
|
|
InvalidIvLength,
|
|
|
|
#[error("invalid counter length. Currently supported 32/64/128 bits")]
|
|
|
|
InvalidCounterLength,
|
|
|
|
#[error("tried to encrypt too much data")]
|
|
|
|
TooMuchData,
|
|
|
|
#[error("Encryption failed")]
|
|
|
|
Failed,
|
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2021-12-15 16:18:26 -05:00
|
|
|
pub async fn op_crypto_encrypt(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[serde] opts: EncryptOptions,
|
|
|
|
#[buffer] data: JsBuffer,
|
2024-10-18 18:23:20 -04:00
|
|
|
) -> Result<ToJsBuffer, EncryptError> {
|
2021-12-15 16:18:26 -05:00
|
|
|
let key = opts.key;
|
|
|
|
let fun = move || match opts.algorithm {
|
|
|
|
EncryptAlgorithm::RsaOaep { hash, label } => {
|
|
|
|
encrypt_rsa_oaep(key, hash, label, &data)
|
|
|
|
}
|
|
|
|
EncryptAlgorithm::AesCbc { iv, length } => {
|
|
|
|
encrypt_aes_cbc(key, length, iv, &data)
|
|
|
|
}
|
2022-01-05 10:12:30 -05:00
|
|
|
EncryptAlgorithm::AesGcm {
|
|
|
|
iv,
|
|
|
|
additional_data,
|
|
|
|
length,
|
|
|
|
tag_length,
|
|
|
|
} => encrypt_aes_gcm(key, length, tag_length, iv, additional_data, &data),
|
2022-01-03 06:27:28 -05:00
|
|
|
EncryptAlgorithm::AesCtr {
|
|
|
|
counter,
|
|
|
|
ctr_length,
|
|
|
|
key_length,
|
|
|
|
} => encrypt_aes_ctr(key, key_length, &counter, ctr_length, &data),
|
2021-12-15 16:18:26 -05:00
|
|
|
};
|
2023-05-14 17:40:01 -04:00
|
|
|
let buf = spawn_blocking(fun).await.unwrap()?;
|
2021-12-15 16:18:26 -05:00
|
|
|
Ok(buf.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encrypt_rsa_oaep(
|
2023-06-22 17:37:56 -04:00
|
|
|
key: V8RawKeyData,
|
2021-12-15 16:18:26 -05:00
|
|
|
hash: ShaHash,
|
|
|
|
label: Vec<u8>,
|
|
|
|
data: &[u8],
|
2024-10-18 18:23:20 -04:00
|
|
|
) -> Result<Vec<u8>, EncryptError> {
|
2021-12-15 16:18:26 -05:00
|
|
|
let label = String::from_utf8_lossy(&label).to_string();
|
|
|
|
|
|
|
|
let public_key = key.as_rsa_public_key()?;
|
|
|
|
let public_key = rsa::RsaPublicKey::from_pkcs1_der(&public_key)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| SharedError::FailedDecodePublicKey)?;
|
2021-12-15 16:18:26 -05:00
|
|
|
let mut rng = OsRng;
|
|
|
|
let padding = match hash {
|
2023-10-30 11:25:12 -04:00
|
|
|
ShaHash::Sha1 => rsa::Oaep {
|
|
|
|
digest: Box::<Sha1>::default(),
|
|
|
|
mgf_digest: Box::<Sha1>::default(),
|
2021-12-15 16:18:26 -05:00
|
|
|
label: Some(label),
|
|
|
|
},
|
2023-10-30 11:25:12 -04:00
|
|
|
ShaHash::Sha256 => rsa::Oaep {
|
|
|
|
digest: Box::<Sha256>::default(),
|
|
|
|
mgf_digest: Box::<Sha256>::default(),
|
2021-12-15 16:18:26 -05:00
|
|
|
label: Some(label),
|
|
|
|
},
|
2023-10-30 11:25:12 -04:00
|
|
|
ShaHash::Sha384 => rsa::Oaep {
|
|
|
|
digest: Box::<Sha384>::default(),
|
|
|
|
mgf_digest: Box::<Sha384>::default(),
|
2021-12-15 16:18:26 -05:00
|
|
|
label: Some(label),
|
|
|
|
},
|
2023-10-30 11:25:12 -04:00
|
|
|
ShaHash::Sha512 => rsa::Oaep {
|
|
|
|
digest: Box::<Sha512>::default(),
|
|
|
|
mgf_digest: Box::<Sha512>::default(),
|
2021-12-15 16:18:26 -05:00
|
|
|
label: Some(label),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let encrypted = public_key
|
|
|
|
.encrypt(&mut rng, padding, data)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::Failed)?;
|
2021-12-15 16:18:26 -05:00
|
|
|
Ok(encrypted)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encrypt_aes_cbc(
|
2023-06-22 17:37:56 -04:00
|
|
|
key: V8RawKeyData,
|
2021-12-15 16:18:26 -05:00
|
|
|
length: usize,
|
|
|
|
iv: Vec<u8>,
|
|
|
|
data: &[u8],
|
2024-10-18 18:23:20 -04:00
|
|
|
) -> Result<Vec<u8>, EncryptError> {
|
2021-12-15 16:18:26 -05:00
|
|
|
let key = key.as_secret_key()?;
|
|
|
|
let ciphertext = match length {
|
|
|
|
128 => {
|
|
|
|
// Section 10.3 Step 2 of RFC 2315 https://www.rfc-editor.org/rfc/rfc2315
|
2022-06-20 07:23:57 -04:00
|
|
|
type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
|
2021-12-15 16:18:26 -05:00
|
|
|
|
2022-06-20 07:23:57 -04:00
|
|
|
let cipher = Aes128CbcEnc::new_from_slices(key, &iv)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::InvalidKeyOrIv)?;
|
2022-06-20 07:23:57 -04:00
|
|
|
cipher.encrypt_padded_vec_mut::<Pkcs7>(data)
|
2021-12-15 16:18:26 -05:00
|
|
|
}
|
|
|
|
192 => {
|
|
|
|
// Section 10.3 Step 2 of RFC 2315 https://www.rfc-editor.org/rfc/rfc2315
|
2022-06-20 07:23:57 -04:00
|
|
|
type Aes192CbcEnc = cbc::Encryptor<aes::Aes192>;
|
2021-12-15 16:18:26 -05:00
|
|
|
|
2022-06-20 07:23:57 -04:00
|
|
|
let cipher = Aes192CbcEnc::new_from_slices(key, &iv)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::InvalidKeyOrIv)?;
|
2022-06-20 07:23:57 -04:00
|
|
|
cipher.encrypt_padded_vec_mut::<Pkcs7>(data)
|
2021-12-15 16:18:26 -05:00
|
|
|
}
|
|
|
|
256 => {
|
|
|
|
// Section 10.3 Step 2 of RFC 2315 https://www.rfc-editor.org/rfc/rfc2315
|
2022-06-20 07:23:57 -04:00
|
|
|
type Aes256CbcEnc = cbc::Encryptor<aes::Aes256>;
|
2021-12-15 16:18:26 -05:00
|
|
|
|
2022-06-20 07:23:57 -04:00
|
|
|
let cipher = Aes256CbcEnc::new_from_slices(key, &iv)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::InvalidKeyOrIv)?;
|
2022-06-20 07:23:57 -04:00
|
|
|
cipher.encrypt_padded_vec_mut::<Pkcs7>(data)
|
2021-12-15 16:18:26 -05:00
|
|
|
}
|
2024-10-18 18:23:20 -04:00
|
|
|
_ => return Err(EncryptError::InvalidLength),
|
2021-12-15 16:18:26 -05:00
|
|
|
};
|
|
|
|
Ok(ciphertext)
|
|
|
|
}
|
2022-01-03 06:27:28 -05:00
|
|
|
|
2022-03-02 00:26:10 -05:00
|
|
|
fn encrypt_aes_gcm_general<N: ArrayLength<u8>>(
|
|
|
|
key: &[u8],
|
2022-01-05 10:12:30 -05:00
|
|
|
iv: Vec<u8>,
|
2022-03-02 00:26:10 -05:00
|
|
|
length: usize,
|
|
|
|
ciphertext: &mut [u8],
|
|
|
|
additional_data: Vec<u8>,
|
2024-10-18 18:23:20 -04:00
|
|
|
) -> Result<aes_gcm::Tag, EncryptError> {
|
2022-03-02 00:26:10 -05:00
|
|
|
let nonce = Nonce::<N>::from_slice(&iv);
|
2022-01-05 10:12:30 -05:00
|
|
|
let tag = match length {
|
|
|
|
128 => {
|
2022-03-02 00:26:10 -05:00
|
|
|
let cipher = aes_gcm::AesGcm::<Aes128, N>::new_from_slice(key)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::Failed)?;
|
2022-01-05 10:12:30 -05:00
|
|
|
cipher
|
2022-03-02 00:26:10 -05:00
|
|
|
.encrypt_in_place_detached(nonce, &additional_data, ciphertext)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::Failed)?
|
2022-01-05 10:12:30 -05:00
|
|
|
}
|
|
|
|
192 => {
|
2022-03-02 00:26:10 -05:00
|
|
|
let cipher = aes_gcm::AesGcm::<Aes192, N>::new_from_slice(key)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::Failed)?;
|
2022-01-05 10:12:30 -05:00
|
|
|
cipher
|
2022-03-02 00:26:10 -05:00
|
|
|
.encrypt_in_place_detached(nonce, &additional_data, ciphertext)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::Failed)?
|
2022-01-05 10:12:30 -05:00
|
|
|
}
|
|
|
|
256 => {
|
2022-03-02 00:26:10 -05:00
|
|
|
let cipher = aes_gcm::AesGcm::<Aes256, N>::new_from_slice(key)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::Failed)?;
|
2022-01-05 10:12:30 -05:00
|
|
|
cipher
|
2022-03-02 00:26:10 -05:00
|
|
|
.encrypt_in_place_detached(nonce, &additional_data, ciphertext)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::Failed)?
|
2022-01-05 10:12:30 -05:00
|
|
|
}
|
2024-10-18 18:23:20 -04:00
|
|
|
_ => return Err(EncryptError::InvalidLength),
|
2022-01-05 10:12:30 -05:00
|
|
|
};
|
|
|
|
|
2022-03-02 00:26:10 -05:00
|
|
|
Ok(tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encrypt_aes_gcm(
|
2023-06-22 17:37:56 -04:00
|
|
|
key: V8RawKeyData,
|
2022-03-02 00:26:10 -05:00
|
|
|
length: usize,
|
|
|
|
tag_length: usize,
|
|
|
|
iv: Vec<u8>,
|
|
|
|
additional_data: Option<Vec<u8>>,
|
|
|
|
data: &[u8],
|
2024-10-18 18:23:20 -04:00
|
|
|
) -> Result<Vec<u8>, EncryptError> {
|
2022-03-02 00:26:10 -05:00
|
|
|
let key = key.as_secret_key()?;
|
|
|
|
let additional_data = additional_data.unwrap_or_default();
|
|
|
|
|
|
|
|
let mut ciphertext = data.to_vec();
|
|
|
|
// Fixed 96-bit OR 128-bit nonce
|
|
|
|
let tag = match iv.len() {
|
|
|
|
12 => encrypt_aes_gcm_general::<U12>(
|
|
|
|
key,
|
|
|
|
iv,
|
|
|
|
length,
|
|
|
|
&mut ciphertext,
|
|
|
|
additional_data,
|
|
|
|
)?,
|
|
|
|
16 => encrypt_aes_gcm_general::<U16>(
|
|
|
|
key,
|
|
|
|
iv,
|
|
|
|
length,
|
|
|
|
&mut ciphertext,
|
|
|
|
additional_data,
|
|
|
|
)?,
|
2024-10-18 18:23:20 -04:00
|
|
|
_ => return Err(EncryptError::InvalidIvLength),
|
2022-03-02 00:26:10 -05:00
|
|
|
};
|
|
|
|
|
2022-01-05 10:12:30 -05:00
|
|
|
// Truncated tag to the specified tag length.
|
|
|
|
// `tag` is fixed to be 16 bytes long and (tag_length / 8) is always <= 16
|
|
|
|
let tag = &tag[..(tag_length / 8)];
|
|
|
|
|
|
|
|
// C | T
|
|
|
|
ciphertext.extend_from_slice(tag);
|
|
|
|
|
|
|
|
Ok(ciphertext)
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:23:57 -04:00
|
|
|
fn encrypt_aes_ctr_gen<B>(
|
2022-01-03 06:27:28 -05:00
|
|
|
key: &[u8],
|
|
|
|
counter: &[u8],
|
|
|
|
data: &[u8],
|
2024-10-18 18:23:20 -04:00
|
|
|
) -> Result<Vec<u8>, EncryptError>
|
2022-01-03 06:27:28 -05:00
|
|
|
where
|
2022-06-20 07:23:57 -04:00
|
|
|
B: KeyIvInit + StreamCipher,
|
2022-01-03 06:27:28 -05:00
|
|
|
{
|
2022-06-20 07:23:57 -04:00
|
|
|
let mut cipher = B::new(key.into(), counter.into());
|
2022-01-03 06:27:28 -05:00
|
|
|
|
|
|
|
let mut ciphertext = data.to_vec();
|
|
|
|
cipher
|
|
|
|
.try_apply_keystream(&mut ciphertext)
|
2024-10-18 18:23:20 -04:00
|
|
|
.map_err(|_| EncryptError::TooMuchData)?;
|
2022-01-03 06:27:28 -05:00
|
|
|
|
|
|
|
Ok(ciphertext)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn encrypt_aes_ctr(
|
2023-06-22 17:37:56 -04:00
|
|
|
key: V8RawKeyData,
|
2022-01-03 06:27:28 -05:00
|
|
|
key_length: usize,
|
|
|
|
counter: &[u8],
|
|
|
|
ctr_length: usize,
|
|
|
|
data: &[u8],
|
2024-10-18 18:23:20 -04:00
|
|
|
) -> Result<Vec<u8>, EncryptError> {
|
2022-01-03 06:27:28 -05:00
|
|
|
let key = key.as_secret_key()?;
|
|
|
|
|
|
|
|
match ctr_length {
|
|
|
|
32 => match key_length {
|
2022-06-20 07:23:57 -04:00
|
|
|
128 => encrypt_aes_ctr_gen::<Ctr32BE<aes::Aes128>>(key, counter, data),
|
|
|
|
192 => encrypt_aes_ctr_gen::<Ctr32BE<aes::Aes192>>(key, counter, data),
|
|
|
|
256 => encrypt_aes_ctr_gen::<Ctr32BE<aes::Aes256>>(key, counter, data),
|
2024-10-18 18:23:20 -04:00
|
|
|
_ => Err(EncryptError::InvalidLength),
|
2022-01-03 06:27:28 -05:00
|
|
|
},
|
|
|
|
64 => match key_length {
|
2022-06-20 07:23:57 -04:00
|
|
|
128 => encrypt_aes_ctr_gen::<Ctr64BE<aes::Aes128>>(key, counter, data),
|
|
|
|
192 => encrypt_aes_ctr_gen::<Ctr64BE<aes::Aes192>>(key, counter, data),
|
|
|
|
256 => encrypt_aes_ctr_gen::<Ctr64BE<aes::Aes256>>(key, counter, data),
|
2024-10-18 18:23:20 -04:00
|
|
|
_ => Err(EncryptError::InvalidLength),
|
2022-01-03 06:27:28 -05:00
|
|
|
},
|
|
|
|
128 => match key_length {
|
2022-06-20 07:23:57 -04:00
|
|
|
128 => encrypt_aes_ctr_gen::<Ctr128BE<aes::Aes128>>(key, counter, data),
|
|
|
|
192 => encrypt_aes_ctr_gen::<Ctr128BE<aes::Aes192>>(key, counter, data),
|
|
|
|
256 => encrypt_aes_ctr_gen::<Ctr128BE<aes::Aes256>>(key, counter, data),
|
2024-10-18 18:23:20 -04:00
|
|
|
_ => Err(EncryptError::InvalidLength),
|
2022-01-03 06:27:28 -05:00
|
|
|
},
|
2024-10-18 18:23:20 -04:00
|
|
|
_ => Err(EncryptError::InvalidCounterLength),
|
2022-01-03 06:27:28 -05:00
|
|
|
}
|
|
|
|
}
|