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::MacError;
16 use crypto_provider_default::CryptoProviderImpl;
17 use np_hkdf::*;
18 
19 #[test]
verify_hmac_correct_mac()20 fn verify_hmac_correct_mac() {
21     let data = &[1_u8; 32];
22     let hmac_key = [2; 32];
23 
24     let hmac = NpHmacSha256Key::from(hmac_key);
25 
26     let mac = hmac.calculate_hmac::<CryptoProviderImpl>(data);
27 
28     assert_eq!(Ok(()), hmac.verify_hmac::<CryptoProviderImpl>(data, mac));
29 }
30 
31 #[test]
verify_hmac_incorrect_mac()32 fn verify_hmac_incorrect_mac() {
33     let data = &[1_u8; 32];
34     let hmac_key = [2; 32];
35 
36     let hmac = NpHmacSha256Key::from(hmac_key);
37 
38     let _mac = hmac.calculate_hmac::<CryptoProviderImpl>(data);
39 
40     // wrong mac
41     assert_eq!(Err(MacError), hmac.verify_hmac::<CryptoProviderImpl>(data, [0xFF; 32]));
42 }
43