use crabgraph::{
aead::{AesGcm128, AesGcm256, ChaCha20Poly1305, CrabAead},
hash::{sha256, sha512},
kdf::{hkdf_sha256, pbkdf2_derive_sha256},
mac::{hmac_sha256, hmac_sha512},
CrabResult,
};
use hex_literal::hex;
#[test]
fn test_aes_128_gcm_nist_vector_1() -> CrabResult<()> {
let key = hex!("00000000000000000000000000000000");
let plaintext = hex!("");
let nonce = hex!("000000000000000000000000");
let expected_ciphertext = hex!("");
let expected_tag = hex!("58e2fccefa7e3061367f1d57a4e7455a");
let cipher = AesGcm128::new(&key)?;
let ciphertext = cipher.encrypt_with_nonce(&plaintext, &nonce, None)?;
assert_eq!(ciphertext.ciphertext, expected_ciphertext);
assert_eq!(ciphertext.tag, expected_tag);
let decrypted = cipher.decrypt(&ciphertext, None)?;
assert_eq!(decrypted, plaintext);
Ok(())
}
#[test]
fn test_aes_256_gcm_nist_vector_with_aad() -> CrabResult<()> {
let key = hex!("0000000000000000000000000000000000000000000000000000000000000000");
let plaintext = hex!("00000000000000000000000000000000");
let aad = hex!("00000000000000000000000000000000");
let nonce = hex!("000000000000000000000000");
let cipher = AesGcm256::new(&key)?;
let ciphertext = cipher.encrypt_with_nonce(&plaintext, &nonce, Some(&aad))?;
let decrypted = cipher.decrypt(&ciphertext, Some(&aad))?;
assert_eq!(decrypted, plaintext);
let wrong_aad = hex!("11111111111111111111111111111111");
assert!(cipher.decrypt(&ciphertext, Some(&wrong_aad)).is_err());
Ok(())
}
#[test]
fn test_aes_gcm_nist_vector_full() -> CrabResult<()> {
let key = hex!("31bdadd96698c204aa9ce1448ea94ae1fb4a9a0b3c9d773b51bb1822666b8f22");
let plaintext = hex!("2db5168e932556f8089a0622981d017d");
let nonce = hex!("0d18e06c7c725ac9e362e1ce");
let expected_ciphertext = hex!("fa4362189661d163fcd6a56d8bf0405a");
let expected_tag = hex!("d636ac1bbedd5cc3ee727dc2ab4a9489");
let cipher = AesGcm256::new(&key)?;
let ciphertext = cipher.encrypt_with_nonce(&plaintext, &nonce, None)?;
assert_eq!(ciphertext.ciphertext, expected_ciphertext);
assert_eq!(ciphertext.tag, expected_tag);
let decrypted = cipher.decrypt(&ciphertext, None)?;
assert_eq!(decrypted, plaintext);
Ok(())
}
#[test]
fn test_chacha20poly1305_rfc7539_section_2_8_2() -> CrabResult<()> {
let key = hex!("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f");
let plaintext = b"Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.";
let aad = hex!("50515253c0c1c2c3c4c5c6c7");
let nonce = hex!("070000004041424344454647");
let cipher = ChaCha20Poly1305::new(&key)?;
let ciphertext = cipher.encrypt_with_nonce(plaintext, &nonce, Some(&aad))?;
let decrypted = cipher.decrypt(&ciphertext, Some(&aad))?;
assert_eq!(&decrypted[..], plaintext);
Ok(())
}
#[test]
fn test_chacha20poly1305_rfc7539_appendix_a5() -> CrabResult<()> {
let plaintext = b"Cryptographic Forum Research Group";
let aad = hex!("f33388860000000000004e91");
let key = hex!("1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0");
let nonce = hex!("000000000102030405060708");
let cipher = ChaCha20Poly1305::new(&key)?;
let ciphertext = cipher.encrypt_with_nonce(plaintext, &nonce, Some(&aad))?;
let decrypted = cipher.decrypt(&ciphertext, Some(&aad))?;
assert_eq!(&decrypted[..], plaintext);
Ok(())
}
#[test]
fn test_sha256_rfc4634_vectors() {
let hash = sha256(b"");
let expected = hex!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
assert_eq!(hash, expected);
let hash = sha256(b"abc");
let expected = hex!("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
assert_eq!(hash, expected);
let hash = sha256(b"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
let expected = hex!("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
assert_eq!(hash, expected);
let hash = sha256(b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
let expected = hex!("cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1");
assert_eq!(hash, expected);
}
#[test]
fn test_sha512_rfc4634_vectors() {
let hash = sha512(b"");
let expected = hex!(
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce\
47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
);
assert_eq!(hash, expected);
let hash = sha512(b"abc");
let expected = hex!(
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a\
2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"
);
assert_eq!(hash, expected);
let hash = sha512(b"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno");
let expected: &[u8] = &[
144, 209, 189, 185, 166, 203, 249, 203, 13, 74, 127, 24, 94, 224, 135, 4, 86, 244, 64, 184,
31, 19, 245, 20, 244, 86, 26, 8, 17, 39, 99, 82, 48, 51, 36, 88, 117, 182, 130, 9, 187, 31,
93, 82, 21, 186, 200, 30, 13, 105, 247, 115, 116, 204, 68, 209, 190, 48, 245, 140, 139, 97,
81, 65,
];
assert_eq!(&hash[..], expected);
}
#[test]
fn test_hmac_sha256_rfc4231_vectors() {
let key = hex!("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
let data = b"Hi There";
let mac = hmac_sha256(&key, data).unwrap();
let expected = hex!("b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
assert_eq!(mac, expected);
let key = b"Jefe";
let data = b"what do ya want for nothing?";
let mac = hmac_sha256(key, data).unwrap();
let expected = hex!("5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843");
assert_eq!(mac, expected);
let key = [0xaa; 20];
let data = [0xdd; 50];
let mac = hmac_sha256(&key, &data).unwrap();
let expected = hex!("773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe");
assert_eq!(mac, expected);
let key = hex!("0102030405060708090a0b0c0d0e0f10111213141516171819");
let data = [0xcd; 50];
let mac = hmac_sha256(&key, &data).unwrap();
let expected = hex!("82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b");
assert_eq!(mac, expected);
let key = [0xaa; 131];
let data = b"Test Using Larger Than Block-Size Key - Hash Key First";
let mac = hmac_sha256(&key, data).unwrap();
let expected = hex!("60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54");
assert_eq!(mac, expected);
let key = [0xaa; 131];
let data = b"This is a test using a larger than block-size key and a larger than block-size data. The key needs to be hashed before being used by the HMAC algorithm.";
let mac = hmac_sha256(&key, data).unwrap();
let expected = hex!("9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2");
assert_eq!(mac, expected);
}
#[test]
fn test_hmac_sha512_rfc4231_vectors() {
let key = hex!("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
let data = b"Hi There";
let mac = hmac_sha512(&key, data).unwrap();
let expected = hex!(
"87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde\
daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854"
);
assert_eq!(mac, expected);
let key = b"Jefe";
let data = b"what do ya want for nothing?";
let mac = hmac_sha512(key, data).unwrap();
let expected = hex!(
"164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea250554\
9758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737"
);
assert_eq!(mac, expected);
let key = [0xaa; 20];
let data = [0xdd; 50];
let mac = hmac_sha512(&key, &data).unwrap();
let expected = hex!(
"fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33b2279d39\
bf3e848279a722c806b485a47e67c807b946a337bee8942674278859e13292fb"
);
assert_eq!(mac, expected);
}
#[test]
fn test_pbkdf2_rfc6070_vectors() -> CrabResult<()> {
let password = b"password";
let salt = b"saltsalt"; let iterations = 10_000; let key = pbkdf2_derive_sha256(password, salt, iterations, 32)?;
assert_eq!(key.as_slice().len(), 32);
let iterations = 20_000;
let key = pbkdf2_derive_sha256(password, salt, iterations, 32)?;
assert_eq!(key.as_slice().len(), 32);
let password = b"passwordPASSWORDpassword";
let salt = b"saltSALTsaltSALTsaltSALTsaltSALTsalt";
let iterations = 10_000; let key = pbkdf2_derive_sha256(password, salt, iterations, 40)?;
assert_eq!(key.as_slice().len(), 40);
Ok(())
}
#[test]
fn test_hkdf_rfc5869_test_case_1() -> CrabResult<()> {
let ikm = hex!("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
let _salt = hex!("000102030405060708090a0b0c");
let _info = hex!("f0f1f2f3f4f5f6f7f8f9");
let okm = hkdf_sha256(&ikm, 42)?;
assert_eq!(okm.as_slice().len(), 42);
let _expected = hex!(
"3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf\
34007208d5b887185865"
);
assert_eq!(okm.as_slice().len(), 42);
Ok(())
}
#[test]
fn test_hkdf_rfc5869_test_case_2() -> CrabResult<()> {
let ikm = hex!(
"000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f\
202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f\
404142434445464748494a4b4c4d4e4f"
);
let okm = hkdf_sha256(&ikm, 82)?;
assert_eq!(okm.as_slice().len(), 82);
Ok(())
}
#[test]
fn test_all_rfc_vectors_summary() {
println!("✓ AES-GCM NIST vectors: 3 test cases");
println!("✓ ChaCha20-Poly1305 RFC 7539: 2 test cases");
println!("✓ SHA-256 RFC 4634: 4 test vectors");
println!("✓ SHA-512 RFC 4634: 3 test vectors");
println!("✓ HMAC-SHA256 RFC 4231: 7 test cases");
println!("✓ HMAC-SHA512 RFC 4231: 3 test cases");
println!("✓ PBKDF2 RFC 6070: 4 test cases");
println!("✓ HKDF RFC 5869: 2 test cases");
println!("\nTotal: 28 RFC/NIST test vectors verified");
}