From 6807601cb64e7b18b832cab2939cbb107e3727bb Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 17 May 2015 20:13:18 -0400 Subject: implement hmac_sha1 --- src/lib.rs | 1 + src/sha1.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index b659d01..151a323 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,6 +38,7 @@ pub use sha1::pad_sha1; pub use sha1::sha1_padding; pub use sha1::sha1_with_state; pub use sha1::sha1_mac; +pub use sha1::sha1_hmac; pub use crack::BlockCipherMode; pub use crack::find_aes_128_ecb_encrypted_string; pub use crack::detect_ecb_cbc; diff --git a/src/sha1.rs b/src/sha1.rs index 24186ea..95270b0 100644 --- a/src/sha1.rs +++ b/src/sha1.rs @@ -1,5 +1,7 @@ #[cfg(test)] use serialize::hex::ToHex; +use primitives::fixed_xor; + pub fn sha1 (bytes: &[u8]) -> [u8; 20] { sha1_with_state( bytes, @@ -99,6 +101,34 @@ pub fn sha1_mac (bytes: &[u8], key: &[u8]) -> [u8; 20] { return sha1(&full_bytes[..]); } +pub fn sha1_hmac (bytes: &[u8], key: &[u8]) -> [u8; 20] { + let blocksize = 64; + let fixed_key: Vec = if key.len() > blocksize { + sha1(key).iter() + .map(|x| *x) + .chain(::std::iter::repeat(0x00u8).take(44)) + .collect() + } + else { + key.iter() + .map(|x| *x) + .chain(::std::iter::repeat(0x00u8).take(blocksize - key.len())) + .collect() + }; + + let ipad: Vec = ::std::iter::repeat(0x36u8).take(blocksize).collect(); + let opad: Vec = ::std::iter::repeat(0x5cu8).take(blocksize).collect(); + let k_ipad = fixed_xor(&ipad[..], &fixed_key[..]); + let k_opad = fixed_xor(&opad[..], &fixed_key[..]); + + let inner = sha1( + &k_ipad.iter().chain(bytes.iter()).map(|x| *x).collect::>()[..] + ); + return sha1( + &k_opad.iter().chain(inner.iter()).map(|x| *x).collect::>()[..] + ) +} + #[test] fn test_sha1 () { let tests = [ @@ -120,3 +150,9 @@ fn test_sha1 () { assert_eq!(got, expected); } } + +#[test] +fn test_sha1_hmac () { + assert_eq!(&sha1_hmac(b"", b"")[..].to_hex(), "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"); + assert_eq!(&sha1_hmac(b"The quick brown fox jumps over the lazy dog", b"key")[..].to_hex(), "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"); +} -- cgit v1.2.3-54-g00ecf