1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 use crypto_provider::hmac::{InvalidLength, MacError}; 16 17 /// BoringSSL implemented Hmac Sha256 struct 18 pub struct HmacSha256(bssl_crypto::hmac::HmacSha256); 19 20 impl crypto_provider::hmac::Hmac<32> for HmacSha256 { new_from_key(key: [u8; 32]) -> Self21 fn new_from_key(key: [u8; 32]) -> Self { 22 Self(bssl_crypto::hmac::HmacSha256::new(key)) 23 } 24 new_from_slice(key: &[u8]) -> Result<Self, InvalidLength>25 fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> { 26 Ok(Self(bssl_crypto::hmac::HmacSha256::new_from_slice(key))) 27 } 28 update(&mut self, data: &[u8])29 fn update(&mut self, data: &[u8]) { 30 self.0.update(data) 31 } 32 finalize(self) -> [u8; 32]33 fn finalize(self) -> [u8; 32] { 34 self.0.finalize() 35 } 36 verify_slice(self, tag: &[u8]) -> Result<(), MacError>37 fn verify_slice(self, tag: &[u8]) -> Result<(), MacError> { 38 self.0.verify_slice(tag).map_err(|_| MacError) 39 } 40 verify(self, tag: [u8; 32]) -> Result<(), MacError>41 fn verify(self, tag: [u8; 32]) -> Result<(), MacError> { 42 self.0.verify(tag).map_err(|_| MacError) 43 } 44 verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError>45 fn verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError> { 46 self.0.verify_truncated_left(tag).map_err(|_| MacError) 47 } 48 } 49 50 /// BoringSSL implemented Hmac Sha512 struct 51 pub struct HmacSha512(bssl_crypto::hmac::HmacSha512); 52 53 impl crypto_provider::hmac::Hmac<64> for HmacSha512 { new_from_key(key: [u8; 64]) -> Self54 fn new_from_key(key: [u8; 64]) -> Self { 55 Self(bssl_crypto::hmac::HmacSha512::new(key)) 56 } 57 new_from_slice(key: &[u8]) -> Result<Self, InvalidLength>58 fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength> { 59 Ok(Self(bssl_crypto::hmac::HmacSha512::new_from_slice(key))) 60 } 61 update(&mut self, data: &[u8])62 fn update(&mut self, data: &[u8]) { 63 self.0.update(data) 64 } 65 finalize(self) -> [u8; 64]66 fn finalize(self) -> [u8; 64] { 67 self.0.finalize() 68 } 69 verify_slice(self, tag: &[u8]) -> Result<(), MacError>70 fn verify_slice(self, tag: &[u8]) -> Result<(), MacError> { 71 self.0.verify_slice(tag).map_err(|_| MacError) 72 } 73 verify(self, tag: [u8; 64]) -> Result<(), MacError>74 fn verify(self, tag: [u8; 64]) -> Result<(), MacError> { 75 self.0.verify(tag).map_err(|_| MacError) 76 } 77 verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError>78 fn verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError> { 79 self.0.verify_truncated_left(tag).map_err(|_| MacError) 80 } 81 } 82 83 #[cfg(test)] 84 mod tests { 85 use crate::Boringssl; 86 use core::marker::PhantomData; 87 use crypto_provider::hmac::testing::*; 88 89 #[apply(hmac_test_cases)] hmac_tests(testcase: CryptoProviderTestCase<Boringssl>)90 fn hmac_tests(testcase: CryptoProviderTestCase<Boringssl>) { 91 testcase(PhantomData); 92 } 93 } 94