crypto-async-rs 0.1.3

High-performance pure Rust cryptographic library with async streaming support
Documentation
//! # HMAC (Hash-based Message Authentication Code)
//!
//! This module provides HMAC implementations using various hash functions.
//! HMAC is a mechanism for message authentication using cryptographic hash functions.
//!
//! ## Features
//!
//! - HMAC-SHA256, HMAC-SHA384, HMAC-SHA512 implementations
//! - RFC 2104 compliant
//! - Constant-time operations resistant to timing attacks
//! - Support for keys of any length
//!
//! ## Example
//!
//! ```rust
//! use crypto_async_rs::hmac;
//!
//! let key = b"secret-key";
//! let message = b"Hello, World!";
//!
//! // Compute HMAC-SHA256
//! let hmac_sha256 = hmac::hmac_sha256(key, message);
//! println!("HMAC-SHA256: {:02x?}", hmac_sha256);
//!
//! // Compute HMAC-SHA512
//! let hmac_sha512 = hmac::hmac_sha512(key, message);
//! println!("HMAC-SHA512: {:02x?}", hmac_sha512);
//! ```

use crate::{sha256, sha384, sha512};

/// Compute HMAC-SHA256
///
/// # Arguments
/// * `key` - The secret key (can be any length)
/// * `text` - The message to authenticate
///
/// # Returns
/// A 32-byte HMAC-SHA256 value
///
/// # Example
/// ```rust
/// use crypto_async_rs::hmac;
///
/// let key = b"secret-key";
/// let message = b"Hello, World!";
/// let hmac = hmac::hmac_sha256(key, message);
/// ```
pub fn hmac_sha256(key: &[u8], text: &[u8]) -> [u8; 32] {
    hmac(key, text, 64, sha256::encode)
}

/// Compute HMAC-SHA384
///
/// # Arguments
/// * `key` - The secret key (can be any length)
/// * `text` - The message to authenticate
///
/// # Returns
/// A 48-byte HMAC-SHA384 value
pub fn hmac_sha384(key: &[u8], text: &[u8]) -> [u8; 48] {
    hmac(key, text, 128, sha384::encode)
}

/// Compute HMAC-SHA512
///
/// # Arguments
/// * `key` - The secret key (can be any length)
/// * `text` - The message to authenticate
///
/// # Returns
/// A 64-byte HMAC-SHA512 value
pub fn hmac_sha512(key: &[u8], text: &[u8]) -> [u8; 64] {
    hmac(key, text, 128, sha512::encode)
}

pub fn hmac<T: AsRef<[u8]>>(
    key: &[u8],
    text: &[u8],
    block_length: usize,
    sha: fn(&[u8]) -> T,
) -> T {
    //    const L:usize = 32;
    const IPAD: u8 = 0x36;
    const OPAD: u8 = 0x5c;
    //let mut K:&[u8] = K;
    let mut internal_key = [0u8; 128];

    if key.len() > block_length {
        let sha_result = sha(key);
        let k = sha_result.as_ref();
        internal_key[..k.len()].copy_from_slice(&k);
    } else {
        internal_key[..key.len()].copy_from_slice(key);
    }

    let mut part_1: Vec<u8> = Vec::with_capacity(block_length + text.len());
    for i in 0..block_length {
        part_1.push(internal_key[i] ^ IPAD);
    }
    part_1.extend_from_slice(text);
    let sha_result = sha(&part_1);
    let part_1: &[u8] = sha_result.as_ref();

    let mut result: Vec<u8> = Vec::with_capacity(block_length + part_1.len());
    for i in 0..block_length {
        result.push(internal_key[i] ^ OPAD);
    }
    result.extend_from_slice(&part_1);
    sha(result.as_slice())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sha1;

    #[test]
    fn test_hmac_sha256() {
        let result = hmac_sha256(b"12345", b"12345");
        assert_eq!(
            result,
            [
                0x2a, 0x1b, 0x7b, 0x2d, 0x9f, 0xf4, 0x7f, 0xb4, 0xf5, 0xa1, 0x8d, 0x28, 0x6a, 0x95,
                0x51, 0x0b, 0xfe, 0x72, 0x9c, 0x3d, 0xfa, 0xcf, 0x84, 0x22, 0x4d, 0x35, 0xbf, 0xb1,
                0xb5, 0xc9, 0xae, 0xc8
            ]
        );

        let result = hmac_sha256(
            b"1234567890123456789012345678901234567890123456789012345678901234567890",
            b"12345",
        );
        assert_eq!(
            result,
            [
                0x0c, 0x29, 0x12, 0x3e, 0x58, 0xae, 0xb5, 0xe8, 0x30, 0xaf, 0x48, 0xda, 0x41, 0x3c,
                0x42, 0x74, 0xcb, 0xaf, 0x6b, 0xbd, 0x07, 0x43, 0x5b, 0x65, 0x96, 0xb3, 0xb3, 0xbd,
                0xee, 0xd6, 0x62, 0xc9
            ]
        );
    }

    #[test]
    fn test_hmac_sha256_2() {
        let text: &[u8] = &[
            0x00, 0x20, 0x11, 0x74, 0x6c, 0x73, 0x31, 0x33, 0x20, 0x31, 0x32, 0x33, 0x34, 0x35,
            0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x20, 0xda, 0x75, 0xce, 0x11, 0x39, 0xac, 0x80,
            0xda, 0xe4, 0x04, 0x4d, 0xa9, 0x32, 0x35, 0x0c, 0xf6, 0x5c, 0x97, 0xcc, 0xc9, 0xe3,
            0x3f, 0x1e, 0x6f, 0x7d, 0x2d, 0x4b, 0x18, 0xb7, 0x36, 0xff, 0xd5, 0x01,
        ];
        let key: &[u8] = &[
            0xfb, 0x9f, 0xc8, 0x6, 0x89, 0xb3, 0xa5, 0xd0, 0x2c, 0x33, 0x24, 0x3b, 0xf6, 0x9a,
            0x1b, 0x1b, 0x20, 0x70, 0x55, 0x88, 0xa7, 0x94, 0x30, 0x4a, 0x6e, 0x71, 0x20, 0x15,
            0x5e, 0xdf, 0x14, 0x9a,
        ];
        let expected: &[u8] = &[
            0xba, 0x98, 0x46, 0x0b, 0xbd, 0xaa, 0x64, 0xc6, 0xd6, 0x3b, 0xdc, 0xf9, 0x87, 0x3d,
            0x8a, 0x47, 0xc0, 0x8b, 0x0e, 0xa8, 0x94, 0xb1, 0x83, 0xf6, 0xb6, 0x7c, 0xb4, 0x34,
            0x3d, 0x9c, 0x08, 0xf8,
        ];
        let result = hmac_sha256(key, text);
        assert_eq!(&result, &expected);
    }

    #[test]
    fn test_hmac_sha256_3() {
        let key: &[u8] = &[
            0xfb, 0x9f, 0xc8, 0x6, 0x89, 0xb3, 0xa5, 0xd0, 0x2c, 0x33, 0x24, 0x3b, 0xf6, 0x9a,
            0x1b, 0x1b, 0x20, 0x70, 0x55, 0x88, 0xa7, 0x94, 0x30, 0x4a, 0x6e, 0x71, 0x20, 0x15,
            0x5e, 0xdf, 0x14, 0x9a,
        ];
        let text: &[u8] = &[
            0x00, 0x20, 0x12, 0x74, 0x6c, 0x73, 0x31, 0x33, 0x20, 0x31, 0x32, 0x33, 0x34, 0x35,
            0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x20, 0xda, 0x75, 0xce, 0x11, 0x39, 0xac,
            0x80, 0xda, 0xe4, 0x04, 0x4d, 0xa9, 0x32, 0x35, 0x0c, 0xf6, 0x5c, 0x97, 0xcc, 0xc9,
            0xe3, 0x3f, 0x1e, 0x6f, 0x7d, 0x2d, 0x4b, 0x18, 0xb7, 0x36, 0xff, 0xd5, 0x01,
        ];
        let expected: &[u8] = &[
            0x5e, 0x05, 0xc8, 0x1d, 0xb1, 0x85, 0x67, 0xd7, 0x93, 0x0a, 0x1a, 0x4b, 0x04, 0x76,
            0x44, 0xdc, 0xa3, 0xdb, 0x8c, 0x77, 0xe6, 0x25, 0x70, 0x9c, 0x6e, 0x03, 0x18, 0x68,
            0x2e, 0x45, 0xc7, 0xcf,
        ];
        let result = hmac_sha256(key, text);
        assert_eq!(&result, &expected);
    }

    #[test]
    fn test_hmac_sha1() {
        let key = [0x0bu8; 20];
        let data = "Hi There";
        let result = hmac(&key, data.as_bytes(), 64, sha1::encode);
        assert_eq!(
            &result,
            &[
                0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37,
                0x8c, 0x8e, 0xf1, 0x46, 0xbe, 0x00
            ]
        );
    }

    #[test]
    fn test_hmac_sha384() {
        let result = hmac_sha384(b"12345", b"12345");
        assert_eq!(
            result,
            [
                0x03, 0x6e, 0x51, 0x5b, 0x4f, 0xef, 0x9d, 0xee, 0x0f, 0x30, 0x09, 0xb0, 0xe2, 0xc0,
                0xb1, 0x03, 0xe2, 0xc8, 0xe7, 0xd2, 0xda, 0xe1, 0x8c, 0x7e, 0xe4, 0xb8, 0xf9, 0xc0,
                0x6b, 0x89, 0xc3, 0x72, 0xf0, 0x39, 0x53, 0x5b, 0xd8, 0xb5, 0x84, 0x96, 0x1b, 0x95,
                0xd7, 0x00, 0x6d, 0x29, 0xb4, 0x03
            ]
        );

        let result = hmac_sha384(
            b"1234567890123456789012345678901234567890123456789012345678901234567890",
            b"12345",
        );
        assert_eq!(
            result,
            [
                0x2e, 0x33, 0xe2, 0xe0, 0x9c, 0x0b, 0x9c, 0x46, 0x17, 0xdf, 0xc9, 0x4f, 0x6b, 0xe0,
                0xb5, 0x8a, 0x14, 0x4c, 0xc4, 0xa9, 0x84, 0x2b, 0xea, 0xe4, 0x6d, 0x92, 0x8e, 0x60,
                0x7c, 0x50, 0x44, 0x7d, 0xd0, 0xf0, 0x73, 0x64, 0x1f, 0x3a, 0xe4, 0x4b, 0x43, 0x0d,
                0xb9, 0xb4, 0xe1, 0x01, 0x89, 0x63
            ]
        );
    }
}