wlan_rsn/integrity/
hmac_md5.rs

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::integrity::Algorithm;
use crate::Error;
use hmac::Mac as _;

pub struct HmacMd5;

impl HmacMd5 {
    pub fn new() -> Self {
        Self
    }
}

impl Algorithm for HmacMd5 {
    fn compute(&self, key: &[u8], data: &[u8]) -> Result<Vec<u8>, Error> {
        let mut hmac = hmac::Hmac::<md5::Md5>::new_from_slice(key).expect("construct new HmacMd5");
        hmac.update(data);
        let bytes: [u8; 16] = hmac.finalize().into_bytes().into();
        Ok(bytes.into())
    }
}

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

    fn run_test_case(key: &[u8], data: &[u8], expected_hex: &str) {
        let hmac_md5 = HmacMd5 {};
        let result = hmac_md5.compute(key, data);
        assert!(result.is_ok());

        let expected = Vec::from_hex(expected_hex).unwrap();
        assert_eq!(expected, result.unwrap());

        assert!(hmac_md5.verify(key, data, &expected[..]));
    }

    // RFC 2202, Test Case 1
    #[test]
    fn test_test_case_1() {
        let key = Vec::from_hex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b").unwrap();
        let data = "Hi There".as_bytes();
        let expected = "9294727a3638bb1c13f48ef8158bfc9d";
        run_test_case(&key[..], data, expected);
    }

    // RFC 2202, Test Case 2
    #[test]
    fn test_test_case_2() {
        let key = "Jefe".as_bytes();
        let data = "what do ya want for nothing?".as_bytes();
        let expected = "750c783e6ab0b503eaa86e310a5db738";
        run_test_case(key, data, expected);
    }

    // RFC 2202, Test Case 3
    #[test]
    fn test_test_case_3() {
        let key = Vec::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap();
        let data = Vec::from_hex("dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd").unwrap();
        let expected = "56be34521d144c88dbb8c733f0e8b3f6";
        run_test_case(&key[..], &data[..], expected);
    }

    // RFC 2202, Test Case 4
    #[test]
    fn test_test_case_4() {
        let key = Vec::from_hex("0102030405060708090a0b0c0d0e0f10111213141516171819").unwrap();
        let data = Vec::from_hex("cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd").unwrap();
        let expected = "697eaf0aca3a3aea3a75164746ffaa79";
        run_test_case(&key[..], &data[..], expected);
    }

    // RFC 2202, Test Case 5
    #[test]
    fn test_test_case_5() {
        let key = Vec::from_hex("0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c").unwrap();
        let data = "Test With Truncation".as_bytes();
        let expected = "56461ef2342edc00f9bab995690efd4c";
        run_test_case(&key[..], data, expected);
    }

    // RFC 2202, Test Case 6
    #[test]
    fn test_test_case_6() {
        let key = Vec::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap();
        let data = "Test Using Larger Than Block-Size Key - Hash Key First".as_bytes();
        let expected = "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd";
        run_test_case(&key[..], data, expected);
    }

    // RFC 2202, Test Case 7
    #[test]
    fn test_test_case_7() {
        let key = Vec::from_hex("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap();
        let data =
            "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data".as_bytes();
        let expected = "6f630fad67cda0ee1fb1f562db3aa53e";
        run_test_case(&key[..], data, expected);
    }

    #[test]
    fn test_verify_failure() {
        let key = Vec::from_hex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b").unwrap();
        let data = "Hi There".as_bytes();
        let expected = Vec::from_hex("9294727a3638bb1c13f48ef8158bfc9d").unwrap();
        let hmac_md5 = HmacMd5 {};
        let result = hmac_md5.verify(&key[..], data, &expected[..]);
        assert!(result);
        let not_expected = Vec::from_hex("deadbeef3638bb1c13f48ef8158bfc9d").unwrap();
        let result = hmac_md5.verify(&key[..], data, &not_expected[..]);
        assert!(!result);
    }
}