| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// Copyright by contributors to this project. |
| 3 |
// SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 4 |
|
| 5 |
use alloc::vec::Vec; |
| 6 |
use mls_rs_core::crypto::CipherSuite; |
| 7 |
use nss_gk_api::hash; |
| 8 |
use nss_gk_api::hmac; |
| 9 |
|
| 10 |
#[derive(Debug)] |
| 11 |
#[cfg_attr(feature = "std", derive(thiserror::Error))] |
| 12 |
pub enum HashError { |
| 13 |
#[cfg_attr(feature = "std", error("invalid hmac length"))] |
| 14 |
InvalidHmacLength, |
| 15 |
#[cfg_attr(feature = "std", error("unsupported cipher suite"))] |
| 16 |
UnsupportedCipherSuite, |
| 17 |
#[cfg_attr(feature = "std", error("internal error"))] |
| 18 |
InternalError, |
| 19 |
} |
| 20 |
|
| 21 |
#[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 22 |
#[repr(u16)] |
| 23 |
pub enum Hash { |
| 24 |
Sha256, |
| 25 |
Sha384, |
| 26 |
Sha512, |
| 27 |
} |
| 28 |
|
| 29 |
impl Hash { |
| 30 |
pub fn new(cipher_suite: CipherSuite) -> Result<Self, HashError> { |
| 31 |
match cipher_suite { |
| 32 |
CipherSuite::CURVE25519_AES128 |
| 33 |
| CipherSuite::P256_AES128 |
| 34 |
| CipherSuite::CURVE25519_CHACHA => Ok(Hash::Sha256), |
| 35 |
CipherSuite::P384_AES256 => Ok(Hash::Sha384), |
| 36 |
CipherSuite::CURVE448_AES256 |
| 37 |
| CipherSuite::CURVE448_CHACHA |
| 38 |
| CipherSuite::P521_AES256 => Ok(Hash::Sha512), |
| 39 |
_ => Err(HashError::UnsupportedCipherSuite), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
pub fn hash(&self, data: &[u8]) -> Vec<u8> { |
| 44 |
match self { |
| 45 |
Hash::Sha256 => hash::hash(hash::HashAlgorithm::SHA2_256, data).expect("InternalError"), |
| 46 |
Hash::Sha384 => hash::hash(hash::HashAlgorithm::SHA2_384, data).expect("InternalError"), |
| 47 |
Hash::Sha512 => hash::hash(hash::HashAlgorithm::SHA2_512, data).expect("InternalError"), |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
pub fn mac(&self, key: &[u8], data: &[u8]) -> Result<Vec<u8>, HashError> { |
| 52 |
match self { |
| 53 |
Hash::Sha256 => Ok(hmac::hmac(&hmac::HmacAlgorithm::HMAC_SHA2_256, key, data) |
| 54 |
.map_err(|_| HashError::InternalError)?), |
| 55 |
Hash::Sha384 => Ok(hmac::hmac(&hmac::HmacAlgorithm::HMAC_SHA2_384, key, data) |
| 56 |
.map_err(|_| HashError::InternalError)?), |
| 57 |
Hash::Sha512 => Ok(hmac::hmac(&hmac::HmacAlgorithm::HMAC_SHA2_512, key, data) |
| 58 |
.map_err(|_| HashError::InternalError)?), |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
mod test { |
| 64 |
use crate::Hash; |
| 65 |
use alloc::vec::Vec; |
| 66 |
use serde::Deserialize; |
| 67 |
|
| 68 |
#[derive(Deserialize)] |
| 69 |
struct TestCase { |
| 70 |
#[allow(dead_code)] |
| 71 |
pub ciphersuite: u16, |
| 72 |
pub hash_function: u8, |
| 73 |
#[serde(with = "hex::serde")] |
| 74 |
pub message: Vec<u8>, |
| 75 |
#[serde(with = "hex::serde")] |
| 76 |
pub hash: Vec<u8>, |
| 77 |
} |
| 78 |
|
| 79 |
#[allow(dead_code)] |
| 80 |
fn run_test_case(t: TestCase) { |
| 81 |
let hash_fun = match t.hash_function { |
| 82 |
1 => Hash::Sha256, |
| 83 |
2 => Hash::Sha384, |
| 84 |
_default => Hash::Sha256, |
| 85 |
}; |
| 86 |
let message = t.message; |
| 87 |
let hash_result = hash_fun.hash(message.as_slice()); |
| 88 |
assert_eq!(hash_result, t.hash); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn test_algo_test_cases() { |
| 93 |
let test_case_file = include_str!("../test_data/test_hash.json"); |
| 94 |
let test_cases: Vec<TestCase> = serde_json::from_str(test_case_file).unwrap(); |
| 95 |
|
| 96 |
for case in test_cases { |
| 97 |
run_test_case(case); |
| 98 |
} |
| 99 |
} |
| 100 |
} |