1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::cryptography::{hmac_md5, md5};

pub fn checksum_hmac_md5(key: &[u8], key_usage: i32, plaintext: &[u8]) -> Vec<u8> {
    let mut keyword = "signaturekey".to_string().into_bytes();
    keyword.push(0);

    let ksign = hmac_md5(key, &keyword);
    let mut bs = key_usage.to_le_bytes().to_vec();
    bs.append(&mut plaintext.to_vec());
    let tmp = md5(&bs);

    return hmac_md5(&ksign, &tmp);
}

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

    #[test]
    fn test_checksum_hmac_md5() {
        let s4u_byte_array = &[
            0x01, 0x00, 0x00, 0x00, 0x61, 0x64, 0x6D, 0x69, 0x6E, 0x69, 0x73,
            0x74, 0x72, 0x61, 0x64, 0x6F, 0x72, 0x6B, 0x69, 0x6E, 0x67, 0x64,
            0x6F, 0x6D, 0x2E, 0x68, 0x65, 0x61, 0x72, 0x74, 0x73, 0x4B, 0x65,
            0x72, 0x62, 0x65, 0x72, 0x6F, 0x73,
        ];

        let session_key = &[
            0x72, 0xC3, 0x90, 0xC6, 0x5D, 0x71, 0x89, 0xAB, 0x9A, 0x39, 0xC3,
            0xFB, 0xFC, 0xBA, 0x41, 0xB8, 0x5A, 0x5F, 0x72, 0x6E, 0xD6, 0x4C,
            0x0C, 0x8A, 0x5C, 0xAB, 0x5B, 0xEB, 0x64, 0x8C, 0xB8, 0x74,
        ];

        let result = vec![
            0x85, 0xBF, 0xBD, 0x75, 0xEE, 0x4A, 0x87, 0x92, 0x9C, 0x28, 0xBC,
            0x07, 0x24, 0x95, 0x2F, 0x8D,
        ];

        assert_eq!(result, checksum_hmac_md5(session_key, 17, s4u_byte_array))
    }
}