use aes::cipher::{BlockCipherEncrypt, KeyInit};
use aes::{Aes128, Aes192, Aes256, Block};
#[allow(clippy::enum_variant_names)]
#[derive(Clone)]
pub(crate) enum AesCipher {
Aes128(Aes128),
Aes192(Aes192),
Aes256(Aes256),
}
impl AesCipher {
pub(crate) fn new(key: &[u8]) -> Self {
match key.len() {
16 => AesCipher::Aes128(Aes128::new_from_slice(key).expect("length checked above")),
24 => AesCipher::Aes192(Aes192::new_from_slice(key).expect("length checked above")),
32 => AesCipher::Aes256(Aes256::new_from_slice(key).expect("length checked above")),
n => unreachable!("AES key must be 16, 24, or 32 bytes, got {n}"),
}
}
fn encrypt_block(&self, block: &mut [u8; 16]) {
let block: &mut Block = block.into();
match self {
AesCipher::Aes128(c) => c.encrypt_block(block),
AesCipher::Aes192(c) => c.encrypt_block(block),
AesCipher::Aes256(c) => c.encrypt_block(block),
}
}
fn prf(&self, data: &[u8]) -> [u8; 16] {
debug_assert_eq!(data.len() % 16, 0, "PRF input must be whole blocks");
let mut acc = [0u8; 16];
for block in data.chunks_exact(16) {
for (a, b) in acc.iter_mut().zip(block) {
*a ^= b;
}
self.encrypt_block(&mut acc);
}
acc
}
}
#[derive(Clone)]
pub(crate) struct Params {
u: usize,
v: usize,
b: usize,
d: usize,
q_prefix: Vec<u8>,
mod_u: u128,
mod_v: u128,
}
pub(crate) fn params(radix: u32, tweak: &[u8], n: usize) -> Params {
let r = u128::from(radix);
assert!((2..=65536).contains(&radix), "radix out of range [2, 2^16]");
assert!(n >= 2, "FF1 message too short");
assert!(
r.checked_pow(n as u32).is_none_or(|x| x >= 100),
"FF1 domain radix^n must be at least 100"
);
let u = n / 2;
let v = n - u;
let mod_v = r
.checked_pow(v as u32)
.expect("radix^v fits in u128 (guaranteed by radix^max_length <= 2^128)");
let mod_u = r
.checked_pow(u as u32)
.expect("radix^u fits in u128 (u <= v)");
let b = (128 - (mod_v - 1).leading_zeros() as usize).div_ceil(8);
let d = 4 * b.div_ceil(4) + 4;
assert!(d <= 16, "S expansion exceeds 128 bits (unreachable within validated configurations)");
let t = tweak.len();
let pad = (16 - (t + b + 1) % 16) % 16;
let mut q_prefix = Vec::with_capacity(16 + t + pad);
q_prefix.extend_from_slice(&[
1,
2,
1,
(radix >> 16) as u8,
(radix >> 8) as u8,
radix as u8,
10,
u as u8,
]);
q_prefix.extend_from_slice(&(n as u32).to_be_bytes());
q_prefix.extend_from_slice(&(t as u32).to_be_bytes());
q_prefix.extend_from_slice(tweak);
q_prefix.resize(q_prefix.len() + pad, 0);
Params { u, v, b, d, q_prefix, mod_u, mod_v }
}
pub(crate) fn num(radix: u128, xs: &[u8]) -> u128 {
xs.iter()
.fold(0u128, |acc, &x| acc.wrapping_mul(radix).wrapping_add(u128::from(x)))
}
fn str_radix(radix: u128, mut value: u128, len: usize) -> Vec<u8> {
let mut out = vec![0u8; len];
for slot in out.iter_mut().rev() {
*slot = (value % radix) as u8;
value /= radix;
}
out
}
fn round_y(cipher: &AesCipher, p: &Params, round: u8, num_val: u128) -> u128 {
let mut data = Vec::with_capacity(p.q_prefix.len() + 16);
data.extend_from_slice(&p.q_prefix);
data.push(round);
data.extend_from_slice(&num_val.to_be_bytes()[16 - p.b..]);
let r = cipher.prf(&data);
let mut s = r.to_vec();
let mut j: u128 = 1;
while s.len() < p.d {
let jb = j.to_be_bytes();
let mut block = [0u8; 16];
for (out, (a, b)) in block.iter_mut().zip(r.iter().zip(jb.iter())) {
*out = a ^ b;
}
cipher.encrypt_block(&mut block);
s.extend_from_slice(&block);
j += 1;
}
s[..p.d]
.iter()
.fold(0u128, |acc, &byte| (acc << 8) | u128::from(byte))
}
pub(crate) fn encrypt(cipher: &AesCipher, p: &Params, radix: u32, x: &[u8]) -> Vec<u8> {
let r = u128::from(radix);
let mut a = x[..p.u].to_vec();
let mut b_half = x[p.u..].to_vec();
for i in 0..10u8 {
let y = round_y(cipher, p, i, num(r, &b_half));
let (m, modulus) = if i % 2 == 0 { (p.u, p.mod_u) } else { (p.v, p.mod_v) };
let c = (num(r, &a) + y % modulus) % modulus;
a = std::mem::replace(&mut b_half, str_radix(r, c, m));
}
a.extend_from_slice(&b_half);
a
}
pub(crate) fn decrypt(cipher: &AesCipher, p: &Params, radix: u32, x: &[u8]) -> Vec<u8> {
let r = u128::from(radix);
let mut a = x[..p.u].to_vec();
let mut b_half = x[p.u..].to_vec();
for i in (0..10u8).rev() {
let y = round_y(cipher, p, i, num(r, &a));
let (m, modulus) = if i % 2 == 0 { (p.u, p.mod_u) } else { (p.v, p.mod_v) };
let c = (num(r, &b_half) + (modulus - y % modulus)) % modulus;
b_half = std::mem::replace(&mut a, str_radix(r, c, m));
}
a.extend_from_slice(&b_half);
a
}
#[cfg(test)]
mod tests {
use super::*;
use serde::Deserialize;
const NIST_ALPHABET: &str = "0123456789abcdefghijklmnopqrstuvwxyz";
#[derive(Deserialize)]
struct File {
vectors: Vec<Vector>,
}
#[derive(Deserialize)]
struct Vector {
sample: u32,
cipher: String,
key_hex: String,
radix: u32,
tweak_hex: String,
plaintext: String,
ciphertext: String,
}
fn unhex(s: &str) -> Vec<u8> {
assert!(s.len() % 2 == 0);
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
.collect()
}
fn to_numerals(s: &str) -> Vec<u8> {
s.chars()
.map(|c| NIST_ALPHABET.find(c).expect("char in NIST alphabet") as u8)
.collect()
}
fn to_string(xs: &[u8]) -> String {
xs.iter()
.map(|&x| NIST_ALPHABET.as_bytes()[x as usize] as char)
.collect()
}
#[test]
fn nist_ff1_sample_vectors() {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/../testvectors/ff1_nist.json");
let file: File =
serde_json::from_str(&std::fs::read_to_string(path).expect("read ff1_nist.json"))
.expect("parse ff1_nist.json");
assert_eq!(file.vectors.len(), 9);
for v in &file.vectors {
let key = unhex(&v.key_hex);
let tweak = unhex(&v.tweak_hex);
let cipher = AesCipher::new(&key);
let p = params(v.radix, &tweak, v.plaintext.chars().count());
let pt = to_numerals(&v.plaintext);
let ct = to_numerals(&v.ciphertext);
let got_ct = encrypt(&cipher, &p, v.radix, &pt);
assert_eq!(
to_string(&got_ct),
v.ciphertext,
"sample {} ({}) encrypt",
v.sample,
v.cipher
);
let got_pt = decrypt(&cipher, &p, v.radix, &ct);
assert_eq!(
to_string(&got_pt),
v.plaintext,
"sample {} ({}) decrypt",
v.sample,
v.cipher
);
}
}
#[test]
fn ff1_roundtrip() {
let cipher = AesCipher::new(&[7u8; 16]);
for &(radix, n) in &[(10u32, 6usize), (16, 16), (36, 19), (62, 12), (94, 4), (2, 20)] {
let p = params(radix, b"dealcode/v1/test", n);
for seed in 0u128..50 {
let x: Vec<u8> = (0..n)
.map(|i| ((seed.wrapping_mul(2654435761) as usize + i * 7) % radix as usize) as u8)
.collect();
let y = encrypt(&cipher, &p, radix, &x);
assert_eq!(y.len(), n);
assert_eq!(decrypt(&cipher, &p, radix, &y), x);
}
}
}
}