wlan_rsn/integrity/
mod.rs

1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5pub mod cmac_aes128;
6pub mod hmac_md5;
7pub mod hmac_sha1;
8pub mod hmac_sha256;
9
10use crate::Error;
11use crate::integrity::cmac_aes128::CmacAes128;
12use crate::integrity::hmac_md5::HmacMd5;
13use crate::integrity::hmac_sha1::HmacSha1;
14use crate::integrity::hmac_sha256::HmacSha256;
15use mundane::bytes;
16use wlan_common::ie::rsn::akm;
17
18pub trait Algorithm {
19    // NOTE: The default implementation truncates the output if it is larger than the given
20    //       expected bytes.
21    fn verify(&self, key: &[u8], data: &[u8], expected: &[u8]) -> bool {
22        self.compute(key, data)
23            .map(|mut output| {
24                output.resize(expected.len(), 0);
25                bytes::constant_time_eq(&output, expected)
26            })
27            .unwrap_or(false)
28    }
29
30    #[allow(clippy::result_large_err, reason = "mass allow for https://fxbug.dev/381896734")]
31    fn compute(&self, key: &[u8], data: &[u8]) -> Result<Vec<u8>, Error>;
32}
33
34/// IEEE Std 802.11-2024, 12.7.2 b.1)
35pub fn integrity_algorithm(
36    key_descriptor_version: u16,
37    akm: &akm::Akm,
38) -> Option<Box<dyn Algorithm>> {
39    match key_descriptor_version {
40        1 => Some(Box::new(HmacMd5::new())),
41        2 => Some(Box::new(HmacSha1::new())),
42        // IEEE Std 802.11 does not specify a key descriptor version for SAE. In practice, 0 is used.
43        3 | 0 if akm.suite_type == akm::SAE => Some(Box::new(CmacAes128::new())),
44        // For OWE, we assume group 19 is used and return HMAC-SHA-256 here.
45        // See IEEE 802.11-2024, 12.7.3, Table 12-11.
46        // TODO(https://fxbug.dev/479562399): Return different integrity OWE algorithms for
47        // OWE groups other than 19.
48        0 if akm.suite_type == akm::OWE => Some(Box::new(HmacSha256::new())),
49        _ => None,
50    }
51}