summaryrefslogtreecommitdiffstats
path: root/third_party/rust/digest/src/dev/mac.rs
blob: 0d4a37dfce91eac5f3598e72e57f8af7cc172e1b (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/// Define MAC test
#[macro_export]
#[cfg(feature = "mac")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "dev", feature = "mac"))))]
macro_rules! new_mac_test {
    ($name:ident, $test_name:expr, $mac:ty $(,)?) => {
        digest::new_mac_test!($name, $test_name, $mac, "");
    };
    ($name:ident, $test_name:expr, $mac:ty, trunc_left $(,)?) => {
        digest::new_mac_test!($name, $test_name, $mac, "left");
    };
    ($name:ident, $test_name:expr, $mac:ty, trunc_right $(,)?) => {
        digest::new_mac_test!($name, $test_name, $mac, "right");
    };
    ($name:ident, $test_name:expr, $mac:ty, $trunc:expr $(,)?) => {
        #[test]
        fn $name() {
            use core::cmp::min;
            use digest::dev::blobby::Blob3Iterator;
            use digest::Mac;

            fn run_test(key: &[u8], input: &[u8], tag: &[u8]) -> Option<&'static str> {
                let mac0 = <$mac as Mac>::new_from_slice(key).unwrap();

                let mut mac = mac0.clone();
                mac.update(input);
                let result = mac.finalize().into_bytes();
                let n = tag.len();
                let result_bytes = match $trunc {
                    "left" => &result[..n],
                    "right" => &result[result.len() - n..],
                    _ => &result[..],
                };
                if result_bytes != tag {
                    return Some("whole message");
                }

                // test reading different chunk sizes
                for chunk_size in 1..min(64, input.len()) {
                    let mut mac = mac0.clone();
                    for chunk in input.chunks(chunk_size) {
                        mac.update(chunk);
                    }
                    let res = match $trunc {
                        "left" => mac.verify_truncated_left(tag),
                        "right" => mac.verify_truncated_right(tag),
                        _ => mac.verify_slice(tag),
                    };
                    if res.is_err() {
                        return Some("chunked message");
                    }
                }

                None
            }

            let data = include_bytes!(concat!("data/", $test_name, ".blb"));

            for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() {
                let [key, input, tag] = row.unwrap();
                if let Some(desc) = run_test(key, input, tag) {
                    panic!(
                        "\n\
                         Failed test №{}: {}\n\
                         key:\t{:?}\n\
                         input:\t{:?}\n\
                         tag:\t{:?}\n",
                        i, desc, key, input, tag,
                    );
                }
            }
        }
    };
}

/// Define resettable MAC test
#[macro_export]
#[cfg(feature = "mac")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "dev", feature = "mac"))))]
macro_rules! new_resettable_mac_test {
    ($name:ident, $test_name:expr, $mac:ty $(,)?) => {
        digest::new_resettable_mac_test!($name, $test_name, $mac, "");
    };
    ($name:ident, $test_name:expr, $mac:ty, trunc_left $(,)?) => {
        digest::new_resettable_mac_test!($name, $test_name, $mac, "left");
    };
    ($name:ident, $test_name:expr, $mac:ty, trunc_right $(,)?) => {
        digest::new_resettable_mac_test!($name, $test_name, $mac, "right");
    };
    ($name:ident, $test_name:expr, $mac:ty, $trunc:expr $(,)?) => {
        #[test]
        fn $name() {
            use core::cmp::min;
            use digest::dev::blobby::Blob3Iterator;
            use digest::Mac;

            fn run_test(key: &[u8], input: &[u8], tag: &[u8]) -> Option<&'static str> {
                let mac0 = <$mac as Mac>::new_from_slice(key).unwrap();

                let mut mac = mac0.clone();
                mac.update(input);
                let result = mac.finalize_reset().into_bytes();
                let n = tag.len();
                let result_bytes = match $trunc {
                    "left" => &result[..n],
                    "right" => &result[result.len() - n..],
                    _ => &result[..],
                };
                if result_bytes != tag {
                    return Some("whole message");
                }

                // test if reset worked correctly
                mac.update(input);
                let res = match $trunc {
                    "left" => mac.verify_truncated_left(tag),
                    "right" => mac.verify_truncated_right(tag),
                    _ => mac.verify_slice(tag),
                };
                if res.is_err() {
                    return Some("after reset");
                }

                // test reading different chunk sizes
                for chunk_size in 1..min(64, input.len()) {
                    let mut mac = mac0.clone();
                    for chunk in input.chunks(chunk_size) {
                        mac.update(chunk);
                    }
                    let res = match $trunc {
                        "left" => mac.verify_truncated_left(tag),
                        "right" => mac.verify_truncated_right(tag),
                        _ => mac.verify_slice(tag),
                    };
                    if res.is_err() {
                        return Some("chunked message");
                    }
                }
                None
            }

            let data = include_bytes!(concat!("data/", $test_name, ".blb"));

            for (i, row) in Blob3Iterator::new(data).unwrap().enumerate() {
                let [key, input, tag] = row.unwrap();
                if let Some(desc) = run_test(key, input, tag) {
                    panic!(
                        "\n\
                         Failed test №{}: {}\n\
                         key:\t{:?}\n\
                         input:\t{:?}\n\
                         tag:\t{:?}\n",
                        i, desc, key, input, tag,
                    );
                }
            }
        }
    };
}