use std::fmt;
use std::str::FromStr;
use aes::{Aes128, Aes256};
use aes_gcm::aead::{AeadInPlace, KeyInit};
use aes_gcm::{Aes128Gcm, Aes256Gcm, Nonce, Tag};
use base64::engine::general_purpose::STANDARD;
use base64::Engine as _;
use cbc::cipher::block_padding::Pkcs7;
use cbc::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit};
use hmac::{Hmac, Mac};
use sha2::Sha256;
use subtle::ConstantTimeEq;
use zeroize::Zeroizing;
use crate::error::{Error, Result};
use crate::php::{self, Value};
const GCM_TAG_LEN: usize = 16;
const GCM_IV_LEN: usize = 12;
const CBC_IV_LEN: usize = 16;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Cipher {
Aes128Cbc,
#[default]
Aes256Cbc,
Aes128Gcm,
Aes256Gcm,
}
impl Cipher {
pub fn name(self) -> &'static str {
match self {
Cipher::Aes128Cbc => "aes-128-cbc",
Cipher::Aes256Cbc => "aes-256-cbc",
Cipher::Aes128Gcm => "aes-128-gcm",
Cipher::Aes256Gcm => "aes-256-gcm",
}
}
pub fn key_size(self) -> usize {
match self {
Cipher::Aes128Cbc | Cipher::Aes128Gcm => 16,
Cipher::Aes256Cbc | Cipher::Aes256Gcm => 32,
}
}
fn is_aead(self) -> bool {
matches!(self, Cipher::Aes128Gcm | Cipher::Aes256Gcm)
}
fn iv_len(self) -> usize {
if self.is_aead() {
GCM_IV_LEN
} else {
CBC_IV_LEN
}
}
}
impl fmt::Display for Cipher {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
impl FromStr for Cipher {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s.to_ascii_lowercase().as_str() {
"aes-128-cbc" => Ok(Cipher::Aes128Cbc),
"aes-256-cbc" => Ok(Cipher::Aes256Cbc),
"aes-128-gcm" => Ok(Cipher::Aes128Gcm),
"aes-256-gcm" => Ok(Cipher::Aes256Gcm),
_ => Err(Error::UnsupportedCipher),
}
}
}
pub fn parse_key(key: &str) -> Result<Vec<u8>> {
if key.is_empty() {
return Err(Error::InvalidKey);
}
if let Some(encoded) = key.strip_prefix("base64:") {
return STANDARD.decode(encoded).map_err(|_| Error::InvalidKey);
}
match STANDARD.decode(key) {
Ok(decoded) if matches!(decoded.len(), 16 | 32) => Ok(decoded),
_ => Ok(key.as_bytes().to_vec()),
}
}
#[derive(Clone)]
pub struct Encrypter {
cipher: Cipher,
key: Zeroizing<Vec<u8>>,
previous_keys: Vec<Zeroizing<Vec<u8>>>,
}
impl fmt::Debug for Encrypter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Encrypter")
.field("cipher", &self.cipher)
.field("previous_keys", &self.previous_keys.len())
.finish_non_exhaustive()
}
}
impl Encrypter {
pub fn new(key: &[u8], cipher: Cipher) -> Result<Self> {
check_key_length(key, cipher)?;
Ok(Self {
cipher,
key: Zeroizing::new(key.to_vec()),
previous_keys: Vec::new(),
})
}
pub fn from_app_key(key: &str, cipher: Cipher) -> Result<Self> {
Self::new(&Zeroizing::new(parse_key(key)?), cipher)
}
pub fn generate_key(cipher: Cipher) -> Result<Vec<u8>> {
let mut key = vec![0u8; cipher.key_size()];
getrandom::fill(&mut key).map_err(|_| Error::Encrypt)?;
Ok(key)
}
pub fn with_previous_keys<I, K>(mut self, keys: I) -> Result<Self>
where
I: IntoIterator<Item = K>,
K: AsRef<[u8]>,
{
let mut previous = Vec::new();
for key in keys {
check_key_length(key.as_ref(), self.cipher)?;
previous.push(Zeroizing::new(key.as_ref().to_vec()));
}
self.previous_keys = previous;
Ok(self)
}
pub fn cipher(&self) -> Cipher {
self.cipher
}
pub fn key(&self) -> &[u8] {
&self.key
}
pub fn encrypt(&self, value: &Value) -> Result<String> {
self.encrypt_bytes(&php::serialize(value)?)
}
pub fn decrypt(&self, payload: &str) -> Result<Value> {
php::unserialize(&self.decrypt_bytes(payload)?)
}
pub fn encrypt_string(&self, plaintext: &str) -> Result<String> {
self.encrypt_bytes(plaintext.as_bytes())
}
pub fn decrypt_string(&self, payload: &str) -> Result<String> {
String::from_utf8(self.decrypt_bytes(payload)?).map_err(|_| Error::InvalidUtf8)
}
pub fn encrypt_bytes(&self, plaintext: &[u8]) -> Result<String> {
let mut iv = vec![0u8; self.cipher.iv_len()];
getrandom::fill(&mut iv).map_err(|_| Error::Encrypt)?;
let (value, tag) = match self.cipher {
Cipher::Aes128Cbc => (
cbc::Encryptor::<Aes128>::new_from_slices(&self.key, &iv)
.map_err(|_| Error::Encrypt)?
.encrypt_padded_vec_mut::<Pkcs7>(plaintext),
Vec::new(),
),
Cipher::Aes256Cbc => (
cbc::Encryptor::<Aes256>::new_from_slices(&self.key, &iv)
.map_err(|_| Error::Encrypt)?
.encrypt_padded_vec_mut::<Pkcs7>(plaintext),
Vec::new(),
),
Cipher::Aes128Gcm => gcm_encrypt::<Aes128Gcm>(&self.key, &iv, plaintext)?,
Cipher::Aes256Gcm => gcm_encrypt::<Aes256Gcm>(&self.key, &iv, plaintext)?,
};
let iv = STANDARD.encode(iv);
let value = STANDARD.encode(value);
let tag = STANDARD.encode(tag);
let mac = if self.cipher.is_aead() {
String::new()
} else {
hex(&hash(&iv, &value, &self.key))
};
let json = format!(r#"{{"iv":"{iv}","value":"{value}","mac":"{mac}","tag":"{tag}"}}"#);
Ok(STANDARD.encode(json))
}
pub fn decrypt_bytes(&self, payload: &str) -> Result<Vec<u8>> {
let p = self.parse_payload(payload)?;
let keys = std::iter::once(&self.key).chain(&self.previous_keys);
if self.cipher.is_aead() {
let tag = p.tag.as_deref().ok_or(Error::Decrypt)?;
if tag.len() != GCM_TAG_LEN {
return Err(Error::Decrypt);
}
for key in keys {
if let Some(plain) = self.gcm_decrypt(key, &p.iv, &p.value, tag) {
return Ok(plain);
}
}
return Err(Error::Decrypt);
}
if p.tag.is_some() {
return Err(Error::Decrypt);
}
let key = keys
.into_iter()
.find(|key| {
let expected = hex(&hash(&p.iv_b64, &p.value_b64, key));
bool::from(expected.as_bytes().ct_eq(p.mac.as_bytes()))
})
.ok_or(Error::InvalidMac)?;
self.cbc_decrypt(key, &p.iv, &p.value)
}
pub fn appears_encrypted(value: &str) -> bool {
let Some(decoded) = php_base64_decode(value, true) else {
return false;
};
match serde_json::from_slice::<serde_json::Value>(&decoded) {
Ok(serde_json::Value::Object(o)) => ["iv", "value", "mac"]
.iter()
.all(|k| o.get(*k).is_some_and(|v| !v.is_null())),
_ => false,
}
}
fn parse_payload(&self, payload: &str) -> Result<Payload> {
let json = php_base64_decode(payload, false).ok_or(Error::InvalidPayload)?;
let json: serde_json::Value =
serde_json::from_slice(&json).map_err(|_| Error::InvalidPayload)?;
let obj = json.as_object().ok_or(Error::InvalidPayload)?;
let field = |name: &str| -> Result<String> {
match obj.get(name) {
Some(serde_json::Value::String(s)) => Ok(s.clone()),
_ => Err(Error::InvalidPayload),
}
};
let iv_b64 = field("iv")?;
let value_b64 = field("value")?;
let mac = field("mac")?;
let tag_b64 = match obj.get("tag") {
None | Some(serde_json::Value::Null) => String::new(),
Some(serde_json::Value::String(s)) => s.clone(),
Some(_) => return Err(Error::InvalidPayload),
};
let iv = php_base64_decode(&iv_b64, true)
.filter(|iv| iv.len() == self.cipher.iv_len())
.ok_or(Error::InvalidPayload)?;
let value = php_base64_decode(&value_b64, false).ok_or(Error::InvalidPayload)?;
let tag = if tag_b64.is_empty() || tag_b64 == "0" {
None
} else {
php_base64_decode(&tag_b64, false)
};
Ok(Payload {
iv_b64,
value_b64,
mac,
iv,
value,
tag,
})
}
fn cbc_decrypt(&self, key: &[u8], iv: &[u8], value: &[u8]) -> Result<Vec<u8>> {
match self.cipher {
Cipher::Aes128Cbc => cbc::Decryptor::<Aes128>::new_from_slices(key, iv)
.map_err(|_| Error::Decrypt)?
.decrypt_padded_vec_mut::<Pkcs7>(value)
.map_err(|_| Error::Decrypt),
_ => cbc::Decryptor::<Aes256>::new_from_slices(key, iv)
.map_err(|_| Error::Decrypt)?
.decrypt_padded_vec_mut::<Pkcs7>(value)
.map_err(|_| Error::Decrypt),
}
}
fn gcm_decrypt(&self, key: &[u8], iv: &[u8], value: &[u8], tag: &[u8]) -> Option<Vec<u8>> {
match self.cipher {
Cipher::Aes128Gcm => gcm_decrypt::<Aes128Gcm>(key, iv, value, tag),
_ => gcm_decrypt::<Aes256Gcm>(key, iv, value, tag),
}
}
}
struct Payload {
iv_b64: String,
value_b64: String,
mac: String,
iv: Vec<u8>,
value: Vec<u8>,
tag: Option<Vec<u8>>,
}
fn php_base64_decode(input: &str, strict: bool) -> Option<Vec<u8>> {
let mut out = Vec::with_capacity(input.len() / 4 * 3 + 3);
let (mut acc, mut count, mut padding) = (0u32, 0usize, 0usize);
for &c in input.as_bytes() {
if c == b'=' {
padding += 1;
continue;
}
let v = match c {
b'A'..=b'Z' => c - b'A',
b'a'..=b'z' => c - b'a' + 26,
b'0'..=b'9' => c - b'0' + 52,
b'+' => 62,
b'/' => 63,
b'\t' | b'\n' | b'\r' | b' ' => continue,
_ if strict => return None,
_ => continue,
};
if strict && padding > 0 {
return None;
}
acc = (acc << 6) | u32::from(v);
count += 1;
if count % 4 == 0 {
out.extend_from_slice(&[(acc >> 16) as u8, (acc >> 8) as u8, acc as u8]);
acc = 0;
}
}
match count % 4 {
2 => out.push((acc >> 4) as u8),
3 => out.extend_from_slice(&[(acc >> 10) as u8, (acc >> 2) as u8]),
_ => {}
}
if strict {
if count % 4 == 1 {
return None;
}
if padding > 0 && (padding > 2 || (count + padding) % 4 != 0) {
return None;
}
}
Some(out)
}
fn check_key_length(key: &[u8], cipher: Cipher) -> Result<()> {
if key.len() == cipher.key_size() {
Ok(())
} else {
Err(Error::InvalidKeyLength {
expected: cipher.key_size(),
actual: key.len(),
})
}
}
fn hash(iv_b64: &str, value_b64: &str, key: &[u8]) -> Vec<u8> {
let mut mac = <Hmac<Sha256> as Mac>::new_from_slice(key).expect("HMAC accepts any key length");
mac.update(iv_b64.as_bytes());
mac.update(value_b64.as_bytes());
mac.finalize().into_bytes().to_vec()
}
fn hex(bytes: &[u8]) -> String {
const DIGITS: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(bytes.len() * 2);
for b in bytes {
out.push(DIGITS[usize::from(b >> 4)] as char);
out.push(DIGITS[usize::from(b & 0x0f)] as char);
}
out
}
fn gcm_encrypt<C: AeadInPlace + KeyInit>(
key: &[u8],
iv: &[u8],
plaintext: &[u8],
) -> Result<(Vec<u8>, Vec<u8>)> {
let cipher = C::new_from_slice(key).map_err(|_| Error::Encrypt)?;
let mut buf = plaintext.to_vec();
let tag = cipher
.encrypt_in_place_detached(Nonce::from_slice(iv), b"", &mut buf)
.map_err(|_| Error::Encrypt)?;
Ok((buf, tag.to_vec()))
}
fn gcm_decrypt<C: AeadInPlace + KeyInit>(
key: &[u8],
iv: &[u8],
value: &[u8],
tag: &[u8],
) -> Option<Vec<u8>> {
let cipher = C::new_from_slice(key).ok()?;
let mut buf = value.to_vec();
cipher
.decrypt_in_place_detached(Nonce::from_slice(iv), b"", &mut buf, Tag::from_slice(tag))
.ok()?;
Some(buf)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn base64_decoding_matches_php() {
type Case<'a> = (&'a str, Option<&'a [u8]>, &'a [u8]);
let cases: [Case; 24] = [
("", Some(b""), b""),
("A", None, b""),
("AA", Some(b"\x00"), b"\x00"),
("AAAA", Some(b"\x00\x00\x00"), b"\x00\x00\x00"),
("QQ==", Some(b"A"), b"A"),
("QQ=", None, b"A"),
("QQ", Some(b"A"), b"A"),
("Q", None, b""),
("QUJD", Some(b"ABC"), b"ABC"),
("QU JD", Some(b"ABC"), b"ABC"),
("QUJD=", None, b"ABC"),
("QUJD====", None, b"ABC"),
("QQ==QQ==", None, b"A\x04\x10"),
("QQ=Q", None, b"A\x04"),
("Q=Q=", None, b"A"),
("QR==", Some(b"A"), b"A"),
("QUJ*", None, b"AB"),
("!!", None, b""),
("A=", None, b""),
("AA=", None, b"\x00"),
("AAA==", None, b"\x00\x00"),
("====", None, b""),
("QUI=\n", Some(b"AB"), b"AB"),
("Zm9v\nYmFy", Some(b"foobar"), b"foobar"),
];
for (input, strict, loose) in cases {
assert_eq!(
php_base64_decode(input, true).as_deref(),
strict,
"strict {input:?}"
);
assert_eq!(
php_base64_decode(input, false).as_deref(),
Some(loose),
"loose {input:?}"
);
}
}
#[test]
fn base64_decoding_agrees_with_the_base64_crate_on_canonical_input() {
for len in 0..40 {
let data: Vec<u8> = (0..len).map(|i| (i * 37 + 11) as u8).collect();
let encoded = STANDARD.encode(&data);
assert_eq!(php_base64_decode(&encoded, true), Some(data.clone()));
assert_eq!(php_base64_decode(&encoded, false), Some(data));
}
}
}