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 /// Trait which defines hmac operations 16 pub trait Hmac<const N: usize>: Sized { 17 /// Create a new hmac from a fixed size key new_from_key(key: [u8; N]) -> Self18 fn new_from_key(key: [u8; N]) -> Self; 19 20 /// Create new hmac value from variable size key. new_from_slice(key: &[u8]) -> Result<Self, InvalidLength>21 fn new_from_slice(key: &[u8]) -> Result<Self, InvalidLength>; 22 23 /// Update state using the provided data update(&mut self, data: &[u8])24 fn update(&mut self, data: &[u8]); 25 26 /// Obtain the hmac computation consuming the hmac instance finalize(self) -> [u8; N]27 fn finalize(self) -> [u8; N]; 28 29 /// Check that the tag value is correct for the processed input verify_slice(self, tag: &[u8]) -> Result<(), MacError>30 fn verify_slice(self, tag: &[u8]) -> Result<(), MacError>; 31 32 /// Check that the tag value is correct for the processed input verify(self, tag: [u8; N]) -> Result<(), MacError>33 fn verify(self, tag: [u8; N]) -> Result<(), MacError>; 34 35 /// Check truncated tag correctness using left side bytes of the calculated tag verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError>36 fn verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError>; 37 } 38 39 /// Error type for when the output of the hmac operation 40 /// is not equal to the expected value. 41 #[derive(Debug, PartialEq, Eq)] 42 pub struct MacError; 43 44 /// Error output when the provided key material length is invalid 45 #[derive(Debug)] 46 pub struct InvalidLength; 47