use crate::error::{AnkerError, Result};
use aes::Aes128;
use aes_gcm::aead::{AeadInPlace, KeyInit};
use aes_gcm::{Aes128Gcm, Nonce, Tag};
use cbc::cipher::block_padding::Pkcs7;
use cbc::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit};
use p256::ecdh::diffie_hellman;
use p256::{PublicKey, SecretKey};
type Aes128CbcEnc = cbc::Encryptor<Aes128>;
type Aes128CbcDec = cbc::Decryptor<Aes128>;
pub const SECURE_KEY: [u8; 16] =
[0xb8, 0xff, 0x74, 0x22, 0x95, 0x5d, 0x4e, 0xb6, 0xd5, 0x54, 0xa2, 0xc4, 0x70, 0x28, 0x05, 0x59];
pub const SECURE_NONCE: [u8; 12] =
[0x6b, 0xa3, 0xe3, 0xf2, 0xf3, 0xa6, 0x0f, 0x29, 0x71, 0xce, 0x5d, 0x1f];
pub const SECURE_AAD: [u8; 16] =
[0x33, 0x22, 0x11, 0x00, 0x77, 0x66, 0x55, 0x44, 0xbb, 0xaa, 0x99, 0x88, 0xff, 0xee, 0xdd, 0xcc];
pub fn gcm_encrypt(key: &[u8; 16], plaintext: &[u8]) -> Vec<u8> {
gcm_encrypt_nonce(key, &SECURE_NONCE, plaintext)
}
pub fn gcm_encrypt_nonce(key: &[u8; 16], nonce: &[u8; 12], plaintext: &[u8]) -> Vec<u8> {
let cipher = Aes128Gcm::new(key.into());
let mut buf = plaintext.to_vec();
let tag = cipher
.encrypt_in_place_detached(Nonce::from_slice(nonce), &SECURE_AAD, &mut buf)
.expect("gcm encrypt");
buf.extend_from_slice(&tag);
buf
}
pub fn gcm_decrypt_noverify(key: &[u8; 16], data: &[u8]) -> Vec<u8> {
gcm_decrypt_noverify_nonce(key, &SECURE_NONCE, data)
}
pub fn gcm_decrypt_noverify_nonce(key: &[u8; 16], nonce: &[u8; 12], data: &[u8]) -> Vec<u8> {
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{BlockEncrypt, KeyInit};
let ct = &data[..data.len().saturating_sub(16)];
let cipher = Aes128::new(GenericArray::from_slice(key));
let mut out = Vec::with_capacity(ct.len());
let mut counter: u32 = 2; for chunk in ct.chunks(16) {
let mut ks = [0u8; 16];
ks[..12].copy_from_slice(nonce);
ks[12..].copy_from_slice(&counter.to_be_bytes());
let mut block = GenericArray::clone_from_slice(&ks);
cipher.encrypt_block(&mut block);
for (o, k) in chunk.iter().zip(block.iter()) {
out.push(o ^ k);
}
counter += 1;
}
out
}
pub fn gcm_decrypt(key: &[u8; 16], data: &[u8]) -> Result<Vec<u8>> {
if data.len() < 16 {
return Err(AnkerError::Crypto("gcm frame shorter than tag".into()));
}
let (ct, tag) = data.split_at(data.len() - 16);
let cipher = Aes128Gcm::new(key.into());
let mut buf = ct.to_vec();
cipher
.decrypt_in_place_detached(
Nonce::from_slice(&SECURE_NONCE),
&SECURE_AAD,
&mut buf,
Tag::from_slice(tag),
)
.map_err(|_| AnkerError::Crypto("gcm tag verification failed".into()))?;
Ok(buf)
}
pub const PRIVATE_KEY_HEX: &str =
"7dfbea61cd95cee49c458ad7419e817f1ade9a66136de3c7d5787af1458e39f4";
pub fn checksum(bytes: &[u8]) -> u8 {
bytes.iter().fold(0u8, |acc, b| acc ^ b)
}
pub fn client_public_key() -> [u8; 64] {
use p256::elliptic_curve::sec1::ToEncodedPoint;
let priv_bytes = hex::decode(PRIVATE_KEY_HEX).expect("valid priv hex");
let secret = SecretKey::from_slice(&priv_bytes).expect("valid secret");
let point = secret.public_key().to_encoded_point(false);
let mut out = [0u8; 64];
out[..32].copy_from_slice(point.x().expect("x"));
out[32..].copy_from_slice(point.y().expect("y"));
out
}
pub fn derive_shared_secret(device_pubkey_xy: &[u8]) -> Result<[u8; 32]> {
if device_pubkey_xy.len() != 64 {
return Err(AnkerError::Crypto(format!(
"expected 64-byte public key, got {}",
device_pubkey_xy.len()
)));
}
let priv_bytes =
hex::decode(PRIVATE_KEY_HEX).map_err(|e| AnkerError::Crypto(e.to_string()))?;
let secret =
SecretKey::from_slice(&priv_bytes).map_err(|e| AnkerError::Crypto(e.to_string()))?;
let mut sec1 = Vec::with_capacity(65);
sec1.push(0x04);
sec1.extend_from_slice(device_pubkey_xy);
let public = PublicKey::from_sec1_bytes(&sec1)
.map_err(|e| AnkerError::Crypto(format!("bad device point: {e}")))?;
let shared = diffie_hellman(secret.to_nonzero_scalar(), public.as_affine());
let raw = shared.raw_secret_bytes();
let mut out = [0u8; 32];
out.copy_from_slice(raw.as_slice());
Ok(out)
}
pub fn encrypt(secret: &[u8; 32], plaintext: &[u8]) -> Vec<u8> {
Aes128CbcEnc::new(secret[..16].into(), secret[16..].into())
.encrypt_padded_vec_mut::<Pkcs7>(plaintext)
}
pub fn decrypt(secret: &[u8; 32], ciphertext: &[u8]) -> Result<Vec<u8>> {
Aes128CbcDec::new(secret[..16].into(), secret[16..].into())
.decrypt_padded_vec_mut::<Pkcs7>(ciphertext)
.map_err(|e| AnkerError::Crypto(format!("decrypt/unpad failed: {e}")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn checksum_is_xor() {
assert_eq!(checksum(&[0xff, 0x09, 0x00]), 0xf6);
assert_eq!(checksum(&[]), 0x00);
}
#[test]
fn aes_roundtrip() {
let secret = [7u8; 32];
let msg = b"a10121fe0503deadbeef";
let ct = encrypt(&secret, msg);
assert_eq!(ct.len() % 16, 0);
let pt = decrypt(&secret, &ct).unwrap();
assert_eq!(pt, msg);
}
#[test]
fn ecdh_rejects_bad_length() {
assert!(derive_shared_secret(&[0u8; 10]).is_err());
}
}
#[cfg(test)]
mod gcm_tests {
use super::*;
#[test]
fn gcm_matches_captured_negotiation_frame_658() {
let pt = hex::decode("a1047de5606a").unwrap();
let out = gcm_encrypt(&SECURE_KEY, &pt);
assert_eq!(hex::encode(out), "0a8242378650404c5437e809e45236456b0fb0870baa");
}
#[test]
fn gcm_roundtrip() {
let pt = b"hello secure world";
let ct = gcm_encrypt(&SECURE_KEY, pt);
assert_eq!(gcm_decrypt(&SECURE_KEY, &ct).unwrap(), pt);
}
}