Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
846 changes: 295 additions & 551 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ enable_network_functions = ["dep:reqwest", "dep:reqwest-middleware", "dep:reqwes
# Enables cryptographic functions: hashing (MD5, SHA, xxHash, etc.), encryption (AES, ChaCha20, etc.), and MACs.
enable_crypto_functions = [
"dep:aes",
"dep:aes-gcm",
"dep:aes-siv",
"dep:cbc",
"dep:cfb-mode",
Expand Down Expand Up @@ -275,6 +276,7 @@ zstd = { version = "0.13", default-features = false, features = ["wasm"], option

# Cryptography
aes = { version = "0.9", optional = true }
aes-gcm = { version = "0.11", optional = true }
aes-siv = { version = "0.7.0", optional = true }
chacha20poly1305 = { version = "0.11", optional = true }
crypto_secretbox = { version = "0.1", features = ["salsa20"], optional = true }
Expand Down Expand Up @@ -310,8 +312,12 @@ jsonschema = { version = "0.38.1", default-features = false }

# Dependencies used for WASM
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.3", features = ["wasm_js"] }
getrandom04 = { package = "getrandom", version = "0.4", features = ["wasm_js"] }
# Three getrandom overrides are needed because aes-siv/crypto_secretbox/ipcrypt-rs (aead 0.5),
# rand 0.9, and the new RustCrypto stack (aead 0.6) each pin a different major version.
# Track consolidation: https://github.com/vectordotdev/vrl/issues/1845
getrandom02 = { package = "getrandom", version = "0.2", features = ["js"] }
getrandom03 = { package = "getrandom", version = "0.3", features = ["wasm_js"] }
getrandom = { package = "getrandom", version = "0.4", features = ["wasm_js"] }
uuid = { version = "1", features = ["v4", "v7", "js"], optional = true }

[dev-dependencies]
Expand Down
49 changes: 21 additions & 28 deletions LICENSE-3rdparty.csv

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions changelog.d/1641.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Added AES-GCM support to decrypt/encrypt functions.

authors: dodo0822
2 changes: 1 addition & 1 deletion docs/generated/decrypt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"anchor": "decrypt",
"name": "decrypt",
"category": "Cryptography",
"description": "Decrypts a string with a symmetric encryption algorithm.\n\nSupported Algorithms:\n\n* AES-256-CFB (key = 32 bytes, iv = 16 bytes)\n* AES-192-CFB (key = 24 bytes, iv = 16 bytes)\n* AES-128-CFB (key = 16 bytes, iv = 16 bytes)\n* AES-256-OFB (key = 32 bytes, iv = 16 bytes)\n* AES-192-OFB (key = 24 bytes, iv = 16 bytes)\n* AES-128-OFB (key = 16 bytes, iv = 16 bytes)\n* AES-128-SIV (key = 32 bytes, iv = 16 bytes)\n* AES-256-SIV (key = 64 bytes, iv = 16 bytes)\n* Deprecated - AES-256-CTR (key = 32 bytes, iv = 16 bytes)\n* Deprecated - AES-192-CTR (key = 24 bytes, iv = 16 bytes)\n* Deprecated - AES-128-CTR (key = 16 bytes, iv = 16 bytes)\n* AES-256-CTR-LE (key = 32 bytes, iv = 16 bytes)\n* AES-192-CTR-LE (key = 24 bytes, iv = 16 bytes)\n* AES-128-CTR-LE (key = 16 bytes, iv = 16 bytes)\n* AES-256-CTR-BE (key = 32 bytes, iv = 16 bytes)\n* AES-192-CTR-BE (key = 24 bytes, iv = 16 bytes)\n* AES-128-CTR-BE (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-PKCS7 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-PKCS7 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-PKCS7 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ANSIX923 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ANSIX923 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ANSIX923 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ISO7816 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ISO7816 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ISO7816 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ISO10126 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ISO10126 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ISO10126 (key = 16 bytes, iv = 16 bytes)\n* CHACHA20-POLY1305 (key = 32 bytes, iv = 12 bytes)\n* XCHACHA20-POLY1305 (key = 32 bytes, iv = 24 bytes)\n* XSALSA20-POLY1305 (key = 32 bytes, iv = 24 bytes)",
"description": "Decrypts a string with a symmetric encryption algorithm.\n\nSupported Algorithms:\n\n* AES-256-CFB (key = 32 bytes, iv = 16 bytes)\n* AES-192-CFB (key = 24 bytes, iv = 16 bytes)\n* AES-128-CFB (key = 16 bytes, iv = 16 bytes)\n* AES-256-OFB (key = 32 bytes, iv = 16 bytes)\n* AES-192-OFB (key = 24 bytes, iv = 16 bytes)\n* AES-128-OFB (key = 16 bytes, iv = 16 bytes)\n* AES-128-SIV (key = 32 bytes, iv = 16 bytes)\n* AES-256-SIV (key = 64 bytes, iv = 16 bytes)\n* Deprecated - AES-256-CTR (key = 32 bytes, iv = 16 bytes)\n* Deprecated - AES-192-CTR (key = 24 bytes, iv = 16 bytes)\n* Deprecated - AES-128-CTR (key = 16 bytes, iv = 16 bytes)\n* AES-256-CTR-LE (key = 32 bytes, iv = 16 bytes)\n* AES-192-CTR-LE (key = 24 bytes, iv = 16 bytes)\n* AES-128-CTR-LE (key = 16 bytes, iv = 16 bytes)\n* AES-256-CTR-BE (key = 32 bytes, iv = 16 bytes)\n* AES-192-CTR-BE (key = 24 bytes, iv = 16 bytes)\n* AES-128-CTR-BE (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-PKCS7 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-PKCS7 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-PKCS7 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ANSIX923 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ANSIX923 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ANSIX923 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ISO7816 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ISO7816 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ISO7816 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ISO10126 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ISO10126 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ISO10126 (key = 16 bytes, iv = 16 bytes)\n* AES-128-GCM (key = 16 bytes, iv = 12 bytes)\n* AES-192-GCM (key = 24 bytes, iv = 12 bytes)\n* AES-256-GCM (key = 32 bytes, iv = 12 bytes)\n* AES-128-GCM-16 (key = 16 bytes, iv = 16 bytes)\n* AES-192-GCM-16 (key = 24 bytes, iv = 16 bytes)\n* AES-256-GCM-16 (key = 32 bytes, iv = 16 bytes)\n* CHACHA20-POLY1305 (key = 32 bytes, iv = 12 bytes)\n* XCHACHA20-POLY1305 (key = 32 bytes, iv = 24 bytes)\n* XSALSA20-POLY1305 (key = 32 bytes, iv = 24 bytes)",
"arguments": [
{
"name": "ciphertext",
Expand Down
2 changes: 1 addition & 1 deletion docs/generated/encrypt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"anchor": "encrypt",
"name": "encrypt",
"category": "Cryptography",
"description": "Encrypts a string with a symmetric encryption algorithm.\n\nSupported Algorithms:\n\n* AES-256-CFB (key = 32 bytes, iv = 16 bytes)\n* AES-192-CFB (key = 24 bytes, iv = 16 bytes)\n* AES-128-CFB (key = 16 bytes, iv = 16 bytes)\n* AES-256-OFB (key = 32 bytes, iv = 16 bytes)\n* AES-192-OFB (key = 24 bytes, iv = 16 bytes)\n* AES-128-OFB (key = 16 bytes, iv = 16 bytes)\n* AES-128-SIV (key = 32 bytes, iv = 16 bytes)\n* AES-256-SIV (key = 64 bytes, iv = 16 bytes)\n* Deprecated - AES-256-CTR (key = 32 bytes, iv = 16 bytes)\n* Deprecated - AES-192-CTR (key = 24 bytes, iv = 16 bytes)\n* Deprecated - AES-128-CTR (key = 16 bytes, iv = 16 bytes)\n* AES-256-CTR-LE (key = 32 bytes, iv = 16 bytes)\n* AES-192-CTR-LE (key = 24 bytes, iv = 16 bytes)\n* AES-128-CTR-LE (key = 16 bytes, iv = 16 bytes)\n* AES-256-CTR-BE (key = 32 bytes, iv = 16 bytes)\n* AES-192-CTR-BE (key = 24 bytes, iv = 16 bytes)\n* AES-128-CTR-BE (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-PKCS7 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-PKCS7 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-PKCS7 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ANSIX923 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ANSIX923 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ANSIX923 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ISO7816 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ISO7816 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ISO7816 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ISO10126 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ISO10126 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ISO10126 (key = 16 bytes, iv = 16 bytes)\n* CHACHA20-POLY1305 (key = 32 bytes, iv = 12 bytes)\n* XCHACHA20-POLY1305 (key = 32 bytes, iv = 24 bytes)\n* XSALSA20-POLY1305 (key = 32 bytes, iv = 24 bytes)",
"description": "Encrypts a string with a symmetric encryption algorithm.\n\nSupported Algorithms:\n\n* AES-256-CFB (key = 32 bytes, iv = 16 bytes)\n* AES-192-CFB (key = 24 bytes, iv = 16 bytes)\n* AES-128-CFB (key = 16 bytes, iv = 16 bytes)\n* AES-256-OFB (key = 32 bytes, iv = 16 bytes)\n* AES-192-OFB (key = 24 bytes, iv = 16 bytes)\n* AES-128-OFB (key = 16 bytes, iv = 16 bytes)\n* AES-128-SIV (key = 32 bytes, iv = 16 bytes)\n* AES-256-SIV (key = 64 bytes, iv = 16 bytes)\n* Deprecated - AES-256-CTR (key = 32 bytes, iv = 16 bytes)\n* Deprecated - AES-192-CTR (key = 24 bytes, iv = 16 bytes)\n* Deprecated - AES-128-CTR (key = 16 bytes, iv = 16 bytes)\n* AES-256-CTR-LE (key = 32 bytes, iv = 16 bytes)\n* AES-192-CTR-LE (key = 24 bytes, iv = 16 bytes)\n* AES-128-CTR-LE (key = 16 bytes, iv = 16 bytes)\n* AES-256-CTR-BE (key = 32 bytes, iv = 16 bytes)\n* AES-192-CTR-BE (key = 24 bytes, iv = 16 bytes)\n* AES-128-CTR-BE (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-PKCS7 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-PKCS7 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-PKCS7 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ANSIX923 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ANSIX923 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ANSIX923 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ISO7816 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ISO7816 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ISO7816 (key = 16 bytes, iv = 16 bytes)\n* AES-256-CBC-ISO10126 (key = 32 bytes, iv = 16 bytes)\n* AES-192-CBC-ISO10126 (key = 24 bytes, iv = 16 bytes)\n* AES-128-CBC-ISO10126 (key = 16 bytes, iv = 16 bytes)\n* AES-128-GCM (key = 16 bytes, iv = 12 bytes)\n* AES-192-GCM (key = 24 bytes, iv = 12 bytes)\n* AES-256-GCM (key = 32 bytes, iv = 12 bytes)\n* AES-128-GCM-16 (key = 16 bytes, iv = 16 bytes)\n* AES-192-GCM-16 (key = 24 bytes, iv = 16 bytes)\n* AES-256-GCM-16 (key = 32 bytes, iv = 16 bytes)\n* CHACHA20-POLY1305 (key = 32 bytes, iv = 12 bytes)\n* XCHACHA20-POLY1305 (key = 32 bytes, iv = 24 bytes)\n* XSALSA20-POLY1305 (key = 32 bytes, iv = 24 bytes)",
"arguments": [
{
"name": "plaintext",
Expand Down
67 changes: 66 additions & 1 deletion src/stdlib/decrypt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::compiler::prelude::*;
use crate::value::Value;
use aes::cipher::{BlockModeDecrypt, Iv, Key, KeyIvInit, StreamCipher};
use aes::cipher::{BlockModeDecrypt, Iv, Key, KeyIvInit, StreamCipher, consts::{U12, U16}};
use aes_gcm::{Aes128Gcm, Aes256Gcm, AesGcm, Key as GcmKey};
use aes_siv::aead::{Aead as Aead5, KeyInit as KeyInit5, generic_array::GenericArray as AeadArray};
use aes_siv::{Aes128SivAead, Aes256SivAead};
use cbc::cipher::block_padding::{AnsiX923, Iso7816, Iso10126, Pkcs7};
Expand All @@ -17,6 +18,12 @@ type Aes128Cbc = cbc::Decryptor<aes::Aes128>;
type Aes192Cbc = cbc::Decryptor<aes::Aes192>;
type Aes256Cbc = cbc::Decryptor<aes::Aes256>;

type Aes192Gcm = AesGcm<aes::Aes192, U12>;

type Aes128Gcm16 = AesGcm<aes::Aes128, U16>;
type Aes192Gcm16 = AesGcm<aes::Aes192, U16>;
type Aes256Gcm16 = AesGcm<aes::Aes256, U16>;

macro_rules! decrypt {
($algorithm:ty, $ciphertext:expr_2021, $key:expr_2021, $iv:expr_2021) => {{
let mut buffer = vec![0; $ciphertext.len()];
Expand Down Expand Up @@ -61,6 +68,16 @@ macro_rules! decrypt_stream {
}};
}

macro_rules! decrypt_gcm {
($algorithm:ty, $ciphertext:expr_2021, $key:expr_2021, $iv:expr_2021) => {{
<$algorithm>::new(&GcmKey::<$algorithm>::from(get_key_bytes($key)?))
.decrypt(
&aes_gcm::aead::Nonce::<$algorithm>::from(get_iv_bytes($iv)?),
$ciphertext.as_ref(),
)
.map_err(|e| format!("Decryption failed: {e}"))?
}};
}

fn decrypt(ciphertext: Value, algorithm: &str, key: Value, iv: Value) -> Resolved {
let ciphertext = ciphertext.try_bytes()?;
Expand Down Expand Up @@ -98,6 +115,12 @@ fn decrypt(ciphertext: Value, algorithm: &str, key: Value, iv: Value) -> Resolve
"AES-128-CBC-ISO10126" => decrypt_padded!(Aes128Cbc, Iso10126, ciphertext, key, iv),
"AES-128-SIV" => decrypt_stream!(Aes128SivAead, ciphertext, key, iv),
"AES-256-SIV" => decrypt_stream!(Aes256SivAead, ciphertext, key, iv),
"AES-128-GCM" => decrypt_gcm!(Aes128Gcm, ciphertext, key, iv),
"AES-192-GCM" => decrypt_gcm!(Aes192Gcm, ciphertext, key, iv),
"AES-256-GCM" => decrypt_gcm!(Aes256Gcm, ciphertext, key, iv),
"AES-128-GCM-16" => decrypt_gcm!(Aes128Gcm16, ciphertext, key, iv),
"AES-192-GCM-16" => decrypt_gcm!(Aes192Gcm16, ciphertext, key, iv),
"AES-256-GCM-16" => decrypt_gcm!(Aes256Gcm16, ciphertext, key, iv),
"CHACHA20-POLY1305" => ChaCha20Poly1305::new(&ChaChaKey::<ChaCha20Poly1305>::from(get_key_bytes(key)?))
.decrypt(&ChaChaNonce::<ChaCha20Poly1305>::from(get_iv_bytes(iv)?), ciphertext.as_ref())
.expect("key/iv sizes were already checked"),
Expand Down Expand Up @@ -154,6 +177,12 @@ impl Function for Decrypt {
* AES-256-CBC-ISO10126 (key = 32 bytes, iv = 16 bytes)
* AES-192-CBC-ISO10126 (key = 24 bytes, iv = 16 bytes)
* AES-128-CBC-ISO10126 (key = 16 bytes, iv = 16 bytes)
* AES-128-GCM (key = 16 bytes, iv = 12 bytes)
* AES-192-GCM (key = 24 bytes, iv = 12 bytes)
* AES-256-GCM (key = 32 bytes, iv = 12 bytes)
* AES-128-GCM-16 (key = 16 bytes, iv = 16 bytes)
* AES-192-GCM-16 (key = 24 bytes, iv = 16 bytes)
* AES-256-GCM-16 (key = 32 bytes, iv = 16 bytes)
* CHACHA20-POLY1305 (key = 32 bytes, iv = 12 bytes)
* XCHACHA20-POLY1305 (key = 32 bytes, iv = 24 bytes)
* XSALSA20-POLY1305 (key = 32 bytes, iv = 24 bytes)
Expand Down Expand Up @@ -437,6 +466,42 @@ mod tests {
tdef: TypeDef::bytes().fallible(),
}

aes_128_gcm {
args: func_args![ciphertext: value!(b"\xc2\xf1\x8b\t\xd5\x0c\xad/B\x08W\xcb\x13\xdb\xe2$\x96A\xcc\xb8T`+\x99f\x0cc/\x08B\x083\xb1m\xb8\x05"), algorithm: "AES-128-GCM", key: b"16_bytes_xxxxxxx", iv: b"12_bytes_xxx"],
want: Ok(value!("morethan1blockofdata")),
tdef: TypeDef::bytes().fallible(),
}

aes_192_gcm {
args: func_args![ciphertext: value!(b",\x8fI\xe6\x15\x8c\xeb\x95lq}\xe52\xfc\x0e\x808\x8b@\xca\\\xe5\xd0uR\x9cS\x02\xf6\xad\xa1\xb2W\xf47\xe2"), algorithm: "AES-192-GCM", key: b"24_bytes_xxxxxxxxxxxxxxx", iv: b"12_bytes_xxx"],
want: Ok(value!("morethan1blockofdata")),
tdef: TypeDef::bytes().fallible(),
}

aes_256_gcm {
args: func_args![ciphertext: value!(b"\xc7\x03\xe0\xbd\xf7=N\x8cg\xc5\x94\xa3[\xa0\x1b<yF\xe9\xe7\xab{\xbc5\xc3\xcb\xc6Em\xb8\x02\xa8\x1ej\x86L"), algorithm: "AES-256-GCM", key: b"32_bytes_xxxxxxxxxxxxxxxxxxxxxxx", iv: b"12_bytes_xxx"],
want: Ok(value!("morethan1blockofdata")),
tdef: TypeDef::bytes().fallible(),
}

aes_128_gcm_16 {
args: func_args![ciphertext: value!(b"PW{y.\x07\xa2.6\x93\x0b\x03z\xe1\xccI\x90\x98A\xb9\xe1\x1d\xc2\xb2Q\xbbl\xf2XX\xe1\x15|\xebKh"), algorithm: "AES-128-GCM-16", key: b"16_bytes_xxxxxxx", iv: b"16_bytes_xxxxxxx"],
want: Ok(value!("morethan1blockofdata")),
tdef: TypeDef::bytes().fallible(),
}

aes_192_gcm_16 {
args: func_args![ciphertext: value!(b"\x01\xb7\xcc\xb14\xb4rM\x1b\xab\x96&~9p~\x038\xcd\xafN\xed\x1f#&\xc5C\xec\xf3\xc4\xc6iA\x07\xa6\xaa"), algorithm: "AES-192-GCM-16", key: b"24_bytes_xxxxxxxxxxxxxxx", iv: b"16_bytes_xxxxxxx"],
want: Ok(value!("morethan1blockofdata")),
tdef: TypeDef::bytes().fallible(),
}

aes_256_gcm_16 {
args: func_args![ciphertext: value!(b"\xd0\xe9@m\r\xc0b/Qg)\x10\x86\x91\xfb\xe7\x86\xa3\xc9C\xff0\xd3\x99\xdb3\xf0\xc3\xce\xf4\x05\xbb\xcc\xa0\x97B"), algorithm: "AES-256-GCM-16", key: b"32_bytes_xxxxxxxxxxxxxxxxxxxxxxx", iv: b"16_bytes_xxxxxxx"],
want: Ok(value!("morethan1blockofdata")),
tdef: TypeDef::bytes().fallible(),
}

chacha20_poly1305 {
args: func_args![ciphertext: value!(b"\x14m\xe3\xc9\xbc!\xafu\xe31\xb9\x17\x8f\x9bOo0}n\xf4{$\x95\x0f\xa0\x820\xb7R\xe3.{\xd7?\x96\x10"), algorithm: "CHACHA20-POLY1305", key: "32_bytes_xxxxxxxxxxxxxxxxxxxxxxx", iv: "12_bytes_xxx"],
want: Ok(value!("morethan1blockofdata")),
Expand Down
Loading
Loading