ledger_device_sdk 1.33.1

Ledger device Rust SDK
use super::HMACInit;
use core::mem;
use ledger_secure_sdk_sys::{
    cx_hmac_sha224_init, cx_hmac_sha256_init_no_throw, cx_hmac_sha256_t, cx_hmac_sha384_init,
    cx_hmac_sha512_init_no_throw, cx_hmac_sha512_t, cx_hmac_t,
};

use super::impl_hmac;
impl_hmac!(Sha2_224, cx_hmac_sha256_t, cx_hmac_sha224_init);
impl_hmac!(Sha2_256, cx_hmac_sha256_t, cx_hmac_sha256_init_no_throw);
impl_hmac!(Sha2_384, cx_hmac_sha512_t, cx_hmac_sha384_init);
impl_hmac!(Sha2_512, cx_hmac_sha512_t, cx_hmac_sha512_init_no_throw);

#[cfg(test)]
mod tests {
    use crate::assert_eq_err as assert_eq;
    use crate::hmac::sha2::*;
    use crate::testing::TestType;
    use testmacro::test_item as test;

    const TEST_MSG: &[u8; 29] = b"Not your keys, not your coins";
    const TEST_KEY: &[u8; 16] = b"hmac test key!!!";

    #[test]
    fn test_hmac_sha224() {
        let mut mac = Sha2_224::new(TEST_KEY);

        let mut output: [u8; 28] = [0u8; 28];

        let _ = mac.hmac(TEST_MSG, &mut output);

        let expected = [
            0xc4, 0x64, 0x80, 0xfb, 0xea, 0xc7, 0x75, 0x6d, 0xee, 0xc1, 0x6a, 0xcb, 0x6d, 0xae,
            0x6a, 0xfa, 0x5d, 0x03, 0x17, 0x73, 0xd6, 0x4d, 0x49, 0xea, 0xa8, 0x5e, 0x4c, 0x1d,
        ];
        assert_eq!(&output, &expected);
    }

    #[test]
    fn test_hmac_sha256() {
        let mut mac = Sha2_256::new(TEST_KEY);

        let mut output: [u8; 32] = [0u8; 32];

        let _ = mac.hmac(TEST_MSG, &mut output);

        let expected = [
            0x4d, 0x23, 0x82, 0xff, 0xc3, 0xb0, 0x60, 0x48, 0x59, 0xc0, 0xe5, 0x28, 0xf3, 0x66,
            0xa0, 0xba, 0x5b, 0xcb, 0x2c, 0x24, 0x10, 0x9c, 0x9d, 0x0b, 0x3b, 0x0a, 0x75, 0x8d,
            0x0f, 0x5a, 0x2a, 0x13,
        ];
        assert_eq!(&output, &expected);
    }

    #[test]
    fn test_hmac_sha384() {
        let mut mac = Sha2_384::new(TEST_KEY);

        let mut output: [u8; 48] = [0u8; 48];

        let _ = mac.hmac(TEST_MSG, &mut output);

        let expected = [
            0x20, 0x6d, 0x0d, 0xfd, 0xfd, 0x22, 0x43, 0xde, 0xb0, 0x23, 0xf8, 0x56, 0x63, 0xd1,
            0xa2, 0x1e, 0xc1, 0x6a, 0x72, 0x6b, 0xa7, 0x8e, 0xc2, 0x25, 0xe2, 0x1e, 0x3e, 0x3b,
            0xb2, 0xf0, 0x55, 0x1d, 0x4e, 0xde, 0x5f, 0x81, 0xf6, 0xa1, 0xff, 0x8e, 0x76, 0x86,
            0xf1, 0x0f, 0x7a, 0xec, 0xbe, 0x35,
        ];
        assert_eq!(&output, &expected);
    }

    #[test]
    fn test_hmac_sha512() {
        let mut mac = Sha2_512::new(TEST_KEY);

        let mut output: [u8; 64] = [0u8; 64];

        let _ = mac.hmac(TEST_MSG, &mut output);

        let expected = [
            0x2d, 0x03, 0x14, 0x96, 0x68, 0x0e, 0xcc, 0x41, 0x2a, 0x42, 0xf2, 0x45, 0xf8, 0x0b,
            0x10, 0x87, 0x43, 0x96, 0x4d, 0x80, 0x5d, 0x93, 0x5c, 0xd1, 0x6b, 0x95, 0xc1, 0x7a,
            0xed, 0xbd, 0xd8, 0x8c, 0xf8, 0xa7, 0x60, 0xed, 0x04, 0xa2, 0x5b, 0x8d, 0xd8, 0x3d,
            0xa3, 0x13, 0xa1, 0x6a, 0x07, 0x33, 0x49, 0x06, 0x15, 0x79, 0x70, 0xf3, 0xe9, 0x9a,
            0xff, 0x25, 0xb6, 0x5e, 0x37, 0xd1, 0x7e, 0x2b,
        ];
        assert_eq!(&output, &expected);
    }
}