use libvctrl_sha512::{HKDF, HMAC, Hash};
#[test]
fn test_sha512_abc() {
let expected: [u8; 64] = [
0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41,
0x31, 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55,
0xd3, 0x9a, 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3,
0xfe, 0xeb, 0xbd, 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f,
0xa5, 0x4c, 0xa4, 0x9f,
];
let result = Hash::hash(b"abc");
assert_eq!(result, expected);
}
#[test]
fn test_sha512_empty() {
let expected: [u8; 64] = [
0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd, 0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80,
0x07, 0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc, 0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c,
0xe9, 0xce, 0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0, 0xff, 0x83, 0x18, 0xd2, 0x87,
0x7e, 0xec, 0x2f, 0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81, 0xa5, 0x38, 0x32, 0x7a,
0xf9, 0x27, 0xda, 0x3e,
];
let result = Hash::hash(b"");
assert_eq!(result, expected);
}
#[test]
fn test_sha512_streaming() {
let expected = Hash::hash(b"hello world");
let mut hasher = Hash::new();
hasher.update(b"hello ");
hasher.update(b"world");
let result = hasher.finalize();
assert_eq!(result, expected);
assert!(hasher.verify(&expected));
}
#[test]
fn test_hmac_rfc4231_1() {
let key = [0x0b; 20];
let data = b"Hi There";
let expected: [u8; 64] = [
0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c,
0xb0, 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78, 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1,
0x7c, 0xde, 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02, 0x03, 0x8b, 0x27, 0x4e, 0xae,
0xa3, 0xf4, 0xe4, 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70, 0x2e, 0x69, 0x6c, 0x20,
0x3a, 0x12, 0x68, 0x54,
];
let mac = HMAC::mac(data, key);
assert_eq!(mac, expected);
assert!(HMAC::verify(data, key, &expected));
}
#[test]
fn test_hmac_rfc4231_2() {
let key = b"Jefe";
let data = b"what do ya want for nothing?";
let expected: [u8; 64] = [
0x16, 0x4b, 0x7a, 0x7b, 0xfc, 0xf8, 0x19, 0xe2, 0xe3, 0x95, 0xfb, 0xe7, 0x3b, 0x56, 0xe0,
0xa3, 0x87, 0xbd, 0x64, 0x22, 0x2e, 0x83, 0x1f, 0xd6, 0x10, 0x27, 0x0c, 0xd7, 0xea, 0x25,
0x05, 0x54, 0x97, 0x58, 0xbf, 0x75, 0xc0, 0x5a, 0x99, 0x4a, 0x6d, 0x03, 0x4f, 0x65, 0xf8,
0xf0, 0xe6, 0xfd, 0xca, 0xea, 0xb1, 0xa3, 0x4d, 0x4a, 0x6b, 0x4b, 0x63, 0x6e, 0x07, 0x0a,
0x38, 0xbc, 0xe7, 0x37,
];
let mac = HMAC::mac(data, key);
assert_eq!(mac, expected);
assert!(HMAC::verify(data, key, &expected));
}
#[test]
fn test_hkdf_vectors() {
let ikm = [0x0bu8; 22];
let salt: [u8; 13] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
];
let info: [u8; 10] = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9];
let expected: [u8; 42] = [
0x83, 0x23, 0x90, 0x08, 0x6c, 0xda, 0x71, 0xfb, 0x47, 0x62, 0x5b, 0xb5, 0xce, 0xb1, 0x68,
0xe4, 0xc8, 0xe2, 0x6a, 0x1a, 0x16, 0xed, 0x34, 0xd9, 0xfc, 0x7f, 0xe9, 0x2c, 0x14, 0x81,
0x57, 0x93, 0x38, 0xda, 0x36, 0x2c, 0xb8, 0xd9, 0xf9, 0x25, 0xd7, 0xcb,
];
let prk = HKDF::extract(salt, ikm);
let mut okm = [0u8; 42];
HKDF::expand(&mut okm, prk, info);
assert_eq!(okm, expected);
let ikm = [0x0bu8; 22];
let expected_empty: [u8; 42] = [
0xf5, 0xfa, 0x02, 0xb1, 0x82, 0x98, 0xa7, 0x2a, 0x8c, 0x23, 0x89, 0x8a, 0x87, 0x03, 0x47,
0x2c, 0x6e, 0xb1, 0x79, 0xdc, 0x20, 0x4c, 0x03, 0x42, 0x5c, 0x97, 0x0e, 0x3b, 0x16, 0x4b,
0xf9, 0x0f, 0xff, 0x22, 0xd0, 0x48, 0x36, 0xd0, 0xe2, 0x34, 0x3b, 0xac,
];
let prk = HKDF::extract([], ikm);
let mut okm = [0u8; 42];
HKDF::expand(&mut okm, prk, []);
assert_eq!(okm, expected_empty);
}
#[test]
fn test_hmac_streaming() {
let key = b"secret key";
let message = b"Hello, World!";
let oneshot = HMAC::mac(message, key);
let mut streaming = HMAC::new(key);
streaming.update(b"Hello, ");
streaming.update(b"World!");
let result = streaming.finalize();
assert_eq!(oneshot, result);
let mut streaming = HMAC::new(key);
streaming.update(message);
assert!(streaming.finalize_verify(&oneshot));
}
#[test]
fn test_hmac_verify_fail() {
let key = b"secret";
let data = b"message";
let mac = HMAC::mac(data, key);
let mut wrong = mac;
wrong[0] ^= 0x01;
assert!(!HMAC::verify(data, key, &wrong));
}
#[cfg(feature = "sha384")]
mod sha384_tests {
use libvctrl_sha512::sha384;
#[test]
fn test_sha384_abc() {
let expected: [u8; 48] = [
0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6,
0x50, 0x07, 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a,
0x43, 0xff, 0x5b, 0xed, 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba,
0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7,
];
let result = sha384::Hash::hash(b"abc");
assert_eq!(result, expected);
}
#[test]
fn test_sha384_empty() {
let expected: [u8; 48] = [
0x38, 0xb0, 0x60, 0xa7, 0x51, 0xac, 0x96, 0x38, 0x4c, 0xd9, 0x32, 0x7e, 0xb1, 0xb1,
0xe3, 0x6a, 0x21, 0xfd, 0xb7, 0x11, 0x14, 0xbe, 0x07, 0x43, 0x4c, 0x0c, 0xc7, 0xbf,
0x63, 0xf6, 0xe1, 0xda, 0x27, 0x4e, 0xde, 0xbf, 0xe7, 0x6f, 0x65, 0xfb, 0xd5, 0x1a,
0xd2, 0xf1, 0x48, 0x98, 0xb9, 0x5b,
];
let result = sha384::Hash::hash(b"");
assert_eq!(result, expected);
}
#[test]
fn test_hmac_sha384_rfc4231() {
let key = [0x0b; 20];
let data = b"Hi There";
let expected: [u8; 48] = [
0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62, 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46,
0x90, 0x7f, 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6, 0x82, 0xaa, 0x03, 0x4c,
0x7c, 0xeb, 0xc5, 0x9c, 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f, 0x4a, 0xf1,
0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6,
];
let mac = sha384::HMAC::mac(data, key);
assert_eq!(mac, expected);
assert!(sha384::HMAC::verify(data, key, &expected));
}
#[test]
fn test_hkdf_sha384_vectors() {
let ikm = [0x0bu8; 22];
let salt: [u8; 13] = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
];
let info: [u8; 10] = [0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9];
let expected: [u8; 42] = [
0x9b, 0x50, 0x97, 0xa8, 0x60, 0x38, 0xb8, 0x05, 0x30, 0x90, 0x76, 0xa4, 0x4b, 0x3a,
0x9f, 0x38, 0x06, 0x3e, 0x25, 0xb5, 0x16, 0xdc, 0xbf, 0x36, 0x9f, 0x39, 0x4c, 0xfa,
0xb4, 0x36, 0x85, 0xf7, 0x48, 0xb6, 0x45, 0x77, 0x63, 0xe4, 0xf0, 0x20, 0x4f, 0xc5,
];
let prk = sha384::HKDF::extract(salt, ikm);
let mut okm = [0u8; 42];
sha384::HKDF::expand(&mut okm, prk, info);
assert_eq!(okm, expected);
let expected_empty: [u8; 42] = [
0xc8, 0xc9, 0x6e, 0x71, 0x0f, 0x89, 0xb0, 0xd7, 0x99, 0x0b, 0xca, 0x68, 0xbc, 0xde,
0xc8, 0xcf, 0x85, 0x40, 0x62, 0xe5, 0x4c, 0x73, 0xa7, 0xab, 0xc7, 0x43, 0xfa, 0xde,
0x9b, 0x24, 0x2d, 0xaa, 0xcc, 0x1c, 0xea, 0x56, 0x70, 0x41, 0x5b, 0x52, 0x84, 0x9c,
];
let prk = sha384::HKDF::extract([], ikm);
let mut okm = [0u8; 42];
sha384::HKDF::expand(&mut okm, prk, []);
assert_eq!(okm, expected_empty);
}
#[test]
fn test_hmac_sha384_streaming() {
let key = b"secret key";
let message = b"Hello, World!";
let oneshot = sha384::HMAC::mac(message, key);
let mut streaming = sha384::HMAC::new(key);
streaming.update(b"Hello, ");
streaming.update(b"World!");
let result = streaming.finalize();
assert_eq!(oneshot, result);
let mut streaming = sha384::HMAC::new(key);
streaming.update(message);
assert!(streaming.finalize_verify(&oneshot));
}
}