#![allow(dead_code)]
use aes::cipher::{BlockDecrypt, KeyInit};
use aes::Aes128;
use aes::Aes256;
use cbc::cipher::{BlockDecryptMut, KeyIvInit};
use cbc::Decryptor;
use md5::Md5;
use sha2::Digest;
use sha2::{Sha256, Sha384, Sha512};
use thiserror::Error;
#[derive(Debug, Error)]
pub(crate) enum PrimitiveError {
#[error("invalid key/IV length")]
InvalidLength,
#[error("invalid PKCS#7 padding")]
PaddingError,
}
pub(crate) fn rc4(key: &[u8], data: &mut [u8]) -> Result<(), PrimitiveError> {
if key.is_empty() {
return Err(PrimitiveError::InvalidLength);
}
if data.is_empty() {
return Ok(());
}
let mut state = [0u8; 256];
for (i, s) in state.iter_mut().enumerate() {
*s = i as u8;
}
let mut j: u8 = 0;
for i in 0..256usize {
j = j.wrapping_add(state[i]).wrapping_add(key[i % key.len()]);
state.swap(i, j as usize);
}
let mut i: u8 = 0;
let mut j: u8 = 0;
for byte in data.iter_mut() {
i = i.wrapping_add(1);
j = j.wrapping_add(state[i as usize]);
state.swap(i as usize, j as usize);
let idx = state[i as usize].wrapping_add(state[j as usize]) as usize;
*byte ^= state[idx];
}
Ok(())
}
pub(crate) fn aes128_cbc_decrypt(
key: &[u8; 16],
iv: &[u8; 16],
ciphertext: &mut Vec<u8>,
) -> Result<(), PrimitiveError> {
if ciphertext.is_empty() || !ciphertext.len().is_multiple_of(16) {
return Err(PrimitiveError::InvalidLength);
}
let dec = <Decryptor<Aes128> as KeyIvInit>::new(key.into(), iv.into());
let pt = dec
.decrypt_padded_mut::<cbc::cipher::block_padding::Pkcs7>(ciphertext)
.map_err(|_| PrimitiveError::PaddingError)?;
let pt_len = pt.len();
ciphertext.truncate(pt_len);
Ok(())
}
pub(crate) fn aes256_cbc_decrypt(
key: &[u8; 32],
iv: &[u8; 16],
ciphertext: &mut Vec<u8>,
) -> Result<(), PrimitiveError> {
if ciphertext.is_empty() || !ciphertext.len().is_multiple_of(16) {
return Err(PrimitiveError::InvalidLength);
}
let dec = <Decryptor<Aes256> as KeyIvInit>::new(key.into(), iv.into());
let pt = dec
.decrypt_padded_mut::<cbc::cipher::block_padding::Pkcs7>(ciphertext)
.map_err(|_| PrimitiveError::PaddingError)?;
let pt_len = pt.len();
ciphertext.truncate(pt_len);
Ok(())
}
pub(crate) fn aes256_ecb_decrypt_block(key: &[u8; 32], block: &mut [u8; 16]) {
let dec = <Aes256 as KeyInit>::new(key.into());
dec.decrypt_block(block.into());
}
pub(crate) fn md5(data: &[u8]) -> [u8; 16] {
let result = Md5::digest(data);
result.into()
}
pub(crate) fn sha256(data: &[u8]) -> [u8; 32] {
let result = Sha256::digest(data);
result.into()
}
pub(crate) fn sha384(data: &[u8]) -> [u8; 48] {
let result = Sha384::digest(data);
result.into()
}
pub(crate) fn sha512(data: &[u8]) -> [u8; 64] {
let result = Sha512::digest(data);
result.into()
}
#[cfg(test)]
mod tests {
use super::*;
fn from_hex(s: &str) -> Vec<u8> {
assert!(
s.len().is_multiple_of(2),
"hex string must have even length"
);
(0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).expect("invalid hex digit"))
.collect()
}
#[test]
fn rc4_rfc6229_key_plaintext() {
let mut data = b"Plaintext".to_vec();
rc4(b"Key", &mut data).unwrap();
assert_eq!(data, [0xBB, 0xF3, 0x16, 0xE8, 0xD9, 0x40, 0xAF, 0x0A, 0xD3]);
}
#[test]
fn rc4_wiki_pedia() {
let mut data = b"pedia".to_vec();
rc4(b"Wiki", &mut data).unwrap();
assert_eq!(data, [0x10, 0x21, 0xBF, 0x04, 0x20]);
}
#[test]
fn rc4_empty_key_returns_invalid_length() {
let mut data = b"abc".to_vec();
let err = rc4(b"", &mut data).unwrap_err();
assert!(matches!(err, PrimitiveError::InvalidLength));
assert_eq!(data, b"abc", "data must be untouched on error");
}
#[test]
fn rc4_empty_data_is_ok_noop() {
let mut data: Vec<u8> = Vec::new();
rc4(b"Key", &mut data).unwrap();
assert!(data.is_empty());
}
#[test]
fn md5_empty() {
let got = md5(b"");
let want = from_hex("d41d8cd98f00b204e9800998ecf8427e");
assert_eq!(got.as_slice(), want.as_slice());
}
#[test]
fn md5_abc() {
let got = md5(b"abc");
let want = from_hex("900150983cd24fb0d6963f7d28e17f72");
assert_eq!(got.as_slice(), want.as_slice());
}
#[test]
fn sha256_abc() {
let got = sha256(b"abc");
let want = from_hex("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
assert_eq!(got.as_slice(), want.as_slice());
}
#[test]
fn sha384_abc() {
let got = sha384(b"abc");
let want = from_hex(
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163\
1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7",
);
assert_eq!(got.as_slice(), want.as_slice());
}
#[test]
fn sha512_abc() {
let got = sha512(b"abc");
let want = from_hex(
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea2\
0a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd\
454d4423643ce80e2a9ac94fa54ca49f",
);
assert_eq!(got.as_slice(), want.as_slice());
}
#[test]
fn aes128_cbc_decrypt_nist() {
let key: [u8; 16] = [
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf,
0x4f, 0x3c,
];
let iv: [u8; 16] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f,
];
let plaintext = from_hex("6bc1bee22e409f96e93d7e117393172a");
let mut ct = from_hex("7649abac8119b246cee98e9b12e9197d8964e0b149c10b7b682e6e39aaeb731c");
aes128_cbc_decrypt(&key, &iv, &mut ct).unwrap();
assert_eq!(ct, plaintext);
}
#[test]
fn aes256_cbc_decrypt_nist() {
let key: [u8; 32] = [
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d,
0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3,
0x09, 0x14, 0xdf, 0xf4,
];
let iv: [u8; 16] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f,
];
let plaintext = from_hex("6bc1bee22e409f96e93d7e117393172a");
let mut ct = from_hex("f58c4c04d6e5f1ba779eabfb5f7bfbd6485a5c81519cf378fa36d42b8547edc0");
aes256_cbc_decrypt(&key, &iv, &mut ct).unwrap();
assert_eq!(ct, plaintext);
}
#[test]
fn aes128_cbc_decrypt_invalid_length() {
let key = [0u8; 16];
let iv = [0u8; 16];
let mut ct = vec![0u8; 31];
let err = aes128_cbc_decrypt(&key, &iv, &mut ct).unwrap_err();
assert!(matches!(err, PrimitiveError::InvalidLength));
}
#[test]
fn aes128_cbc_decrypt_empty_is_invalid_length() {
let key = [0u8; 16];
let iv = [0u8; 16];
let mut ct: Vec<u8> = Vec::new();
let err = aes128_cbc_decrypt(&key, &iv, &mut ct).unwrap_err();
assert!(matches!(err, PrimitiveError::InvalidLength));
}
#[test]
fn aes128_cbc_decrypt_padding_error() {
let key: [u8; 16] = [
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf,
0x4f, 0x3c,
];
let iv: [u8; 16] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f,
];
let mut ct = from_hex("7649abac8119b246cee98e9b12e9197d8964e0b149c10b7b682e6e39aaeb731c");
ct[15] ^= 0x01;
let err = aes128_cbc_decrypt(&key, &iv, &mut ct).unwrap_err();
assert!(matches!(err, PrimitiveError::PaddingError));
}
#[test]
fn aes256_cbc_decrypt_invalid_length() {
let key = [0u8; 32];
let iv = [0u8; 16];
let mut ct = vec![0u8; 31];
let err = aes256_cbc_decrypt(&key, &iv, &mut ct).unwrap_err();
assert!(matches!(err, PrimitiveError::InvalidLength));
}
#[test]
fn aes256_cbc_decrypt_empty_is_invalid_length() {
let key = [0u8; 32];
let iv = [0u8; 16];
let mut ct: Vec<u8> = Vec::new();
let err = aes256_cbc_decrypt(&key, &iv, &mut ct).unwrap_err();
assert!(matches!(err, PrimitiveError::InvalidLength));
}
#[test]
fn aes256_cbc_decrypt_padding_error() {
let key: [u8; 32] = [
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d,
0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3,
0x09, 0x14, 0xdf, 0xf4,
];
let iv: [u8; 16] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
0x0e, 0x0f,
];
let mut ct = from_hex("f58c4c04d6e5f1ba779eabfb5f7bfbd6485a5c81519cf378fa36d42b8547edc0");
ct[15] ^= 0x01;
let err = aes256_cbc_decrypt(&key, &iv, &mut ct).unwrap_err();
assert!(matches!(err, PrimitiveError::PaddingError));
}
}