• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

namib-project / dcaf-rs / 11935120896

20 Nov 2024 02:11PM UTC coverage: 86.555% (+1.3%) from 85.242%
11935120896

Pull #27

github

web-flow
Merge d2b3d706b into 383248641
Pull Request #27: ci: update grcov to latest stable version

6116 of 7066 relevant lines covered (86.56%)

167.28 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.29
/src/token/cose/crypto_impl/rustcrypto/mac/hmac.rs
1
/*
2
 * Copyright (c) 2024 The NAMIB Project Developers.
3
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6
 * option. This file may not be copied, modified, or distributed
7
 * except according to those terms.
8
 *
9
 * SPDX-License-Identifier: MIT OR Apache-2.0
10
 */
11
use coset::{iana, Algorithm};
12
use digest::{FixedOutput, Mac, MacMarker, Update};
13
use hmac::Hmac;
14
use rand::{CryptoRng, RngCore};
15
use sha2::{Sha256, Sha384, Sha512};
16

17
use crate::error::CoseCipherError;
18
use crate::token::cose::crypto_impl::rustcrypto::RustCryptoContext;
19
use crate::token::cose::{CoseSymmetricKey, CryptoBackend};
20
use alloc::vec::Vec;
21

22
impl<RNG: RngCore + CryptoRng> RustCryptoContext<RNG> {
23
    /// Compute the HMAC of `payload` using the given `key` with the HMAC function
24
    /// `MAC`.
25
    fn compute_hmac_using_mac<MAC: hmac::digest::KeyInit + Update + FixedOutput + MacMarker>(
27✔
26
        key: &CoseSymmetricKey<'_, <Self as CryptoBackend>::Error>,
27✔
27
        payload: &[u8],
27✔
28
    ) -> Vec<u8> {
27✔
29
        let key_size = key.k.len();
27✔
30
        let mut hmac_key = hmac::digest::Key::<MAC>::default();
27✔
31
        hmac_key.as_mut_slice()[..key_size].copy_from_slice(key.k);
27✔
32
        let mut hmac = MAC::new(&hmac_key);
27✔
33
        hmac.update(payload);
27✔
34
        hmac.finalize().into_bytes().to_vec()
27✔
35
    }
27✔
36

37
    /// Verify the HMAC of `payload` using the given `key` with the HMAC function `MAC`.
38
    fn verify_hmac_using_mac<MAC: hmac::digest::KeyInit + Update + FixedOutput + MacMarker>(
41✔
39
        key: &CoseSymmetricKey<'_, <Self as CryptoBackend>::Error>,
41✔
40
        payload: &[u8],
41✔
41
        tag: &[u8],
41✔
42
    ) -> Result<(), CoseCipherError<<Self as CryptoBackend>::Error>> {
41✔
43
        let key_size = key.k.len();
41✔
44
        let mut hmac_key = hmac::digest::Key::<MAC>::default();
41✔
45
        hmac_key.as_mut_slice()[..key_size].copy_from_slice(key.k);
41✔
46
        let mut hmac = MAC::new(&hmac_key);
41✔
47
        hmac.update(payload);
41✔
48
        hmac.verify_slice(tag).map_err(CoseCipherError::from)
41✔
49
    }
41✔
50

51
    /// Compute the HMAC of `payload` using the given `key` with the HMAC function
52
    /// specified in the `algorithm`.
53
    pub(super) fn compute_hmac(
27✔
54
        algorithm: iana::Algorithm,
27✔
55
        key: &CoseSymmetricKey<'_, <Self as CryptoBackend>::Error>,
27✔
56
        payload: &[u8],
27✔
57
    ) -> Result<Vec<u8>, CoseCipherError<<Self as CryptoBackend>::Error>> {
27✔
58
        match algorithm {
27✔
59
            iana::Algorithm::HMAC_256_256 => Ok(
23✔
60
                Self::compute_hmac_using_mac::<Hmac<sha2::Sha256>>(key, payload),
23✔
61
            ),
23✔
62
            iana::Algorithm::HMAC_384_384 => Ok(
2✔
63
                Self::compute_hmac_using_mac::<Hmac<sha2::Sha384>>(key, payload),
2✔
64
            ),
2✔
65
            iana::Algorithm::HMAC_512_512 => Ok(
2✔
66
                Self::compute_hmac_using_mac::<Hmac<sha2::Sha512>>(key, payload),
2✔
67
            ),
2✔
68
            a => Err(CoseCipherError::UnsupportedAlgorithm(Algorithm::Assigned(
×
69
                a,
×
70
            ))),
×
71
        }
72
    }
27✔
73

74
    /// Verify the HMAC `tag` of `payload` using the given `key` with the HMAC
75
    /// function specified in the `algorithm`.
76
    pub(super) fn verify_hmac(
41✔
77
        algorithm: iana::Algorithm,
41✔
78
        key: &CoseSymmetricKey<'_, <Self as CryptoBackend>::Error>,
41✔
79
        tag: &[u8],
41✔
80
        payload: &[u8],
41✔
81
    ) -> Result<(), CoseCipherError<<Self as CryptoBackend>::Error>> {
41✔
82
        match algorithm {
41✔
83
            iana::Algorithm::HMAC_256_256 => {
84
                Self::verify_hmac_using_mac::<Hmac<Sha256>>(key, payload, tag)
33✔
85
            }
86
            iana::Algorithm::HMAC_384_384 => {
87
                Self::verify_hmac_using_mac::<Hmac<Sha384>>(key, payload, tag)
4✔
88
            }
89
            iana::Algorithm::HMAC_512_512 => {
90
                Self::verify_hmac_using_mac::<Hmac<Sha512>>(key, payload, tag)
4✔
91
            }
92
            a => Err(CoseCipherError::UnsupportedAlgorithm(Algorithm::Assigned(
×
93
                a,
×
94
            ))),
×
95
        }
96
    }
41✔
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc