Coverage Report

Created: 2026-06-28 08:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/aws-lc-rs-1.16.3/src/kdf/kbkdf.rs
Line
Count
Source
1
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: Apache-2.0 OR ISC
3
4
#![allow(clippy::module_name_repetitions)]
5
6
use crate::aws_lc::{KBKDF_ctr_hmac, EVP_MD};
7
8
use crate::digest::{match_digest_type, AlgorithmID};
9
use crate::error::Unspecified;
10
use crate::ptr::ConstPointer;
11
12
/// KBKDF in Counter Mode with HMAC-SHA224
13
#[allow(dead_code)]
14
const KBKDF_CTR_HMAC_SHA224: KbkdfCtrHmacAlgorithm = KbkdfCtrHmacAlgorithm {
15
    id: KbkdfCtrHmacAlgorithmId::Sha224,
16
};
17
18
/// KBKDF in Counter Mode with HMAC-SHA256
19
#[allow(dead_code)]
20
const KBKDF_CTR_HMAC_SHA256: KbkdfCtrHmacAlgorithm = KbkdfCtrHmacAlgorithm {
21
    id: KbkdfCtrHmacAlgorithmId::Sha256,
22
};
23
24
/// KBKDF in Counter Mode with HMAC-SHA384
25
#[allow(dead_code)]
26
const KBKDF_CTR_HMAC_SHA384: KbkdfCtrHmacAlgorithm = KbkdfCtrHmacAlgorithm {
27
    id: KbkdfCtrHmacAlgorithmId::Sha384,
28
};
29
30
/// KBKDF in Counter Mode with HMAC-SHA512
31
#[allow(dead_code)]
32
const KBKDF_CTR_HMAC_SHA512: KbkdfCtrHmacAlgorithm = KbkdfCtrHmacAlgorithm {
33
    id: KbkdfCtrHmacAlgorithmId::Sha512,
34
};
35
36
/// Retrieve [`KbkdfCtrHmacAlgorithm`] using the [`KbkdfCtrHmacAlgorithmId`] specified by `id`.
37
#[must_use]
38
0
pub const fn get_kbkdf_ctr_hmac_algorithm(
39
0
    id: KbkdfCtrHmacAlgorithmId,
40
0
) -> Option<&'static KbkdfCtrHmacAlgorithm> {
41
    {
42
0
        Some(match id {
43
0
            KbkdfCtrHmacAlgorithmId::Sha224 => &KBKDF_CTR_HMAC_SHA224,
44
0
            KbkdfCtrHmacAlgorithmId::Sha256 => &KBKDF_CTR_HMAC_SHA256,
45
0
            KbkdfCtrHmacAlgorithmId::Sha384 => &KBKDF_CTR_HMAC_SHA384,
46
0
            KbkdfCtrHmacAlgorithmId::Sha512 => &KBKDF_CTR_HMAC_SHA512,
47
        })
48
    }
49
0
}
50
51
/// KBKDF in Counter Mode with HMAC Algorithm
52
pub struct KbkdfCtrHmacAlgorithm {
53
    id: KbkdfCtrHmacAlgorithmId,
54
}
55
56
impl KbkdfCtrHmacAlgorithm {
57
    /// Returns the KBKDF Counter HMAC Algorithm Identifier
58
    #[must_use]
59
0
    pub fn id(&self) -> KbkdfCtrHmacAlgorithmId {
60
0
        self.id
61
0
    }
62
63
    #[must_use]
64
0
    fn get_evp_md(&self) -> ConstPointer<'_, EVP_MD> {
65
0
        match_digest_type(match self.id {
66
0
            KbkdfCtrHmacAlgorithmId::Sha224 => &AlgorithmID::SHA224,
67
0
            KbkdfCtrHmacAlgorithmId::Sha256 => &AlgorithmID::SHA256,
68
0
            KbkdfCtrHmacAlgorithmId::Sha384 => &AlgorithmID::SHA384,
69
0
            KbkdfCtrHmacAlgorithmId::Sha512 => &AlgorithmID::SHA512,
70
        })
71
0
    }
72
}
73
74
impl PartialEq for KbkdfCtrHmacAlgorithm {
75
0
    fn eq(&self, other: &Self) -> bool {
76
0
        self.id == other.id
77
0
    }
78
}
79
80
impl Eq for KbkdfCtrHmacAlgorithm {}
81
82
impl core::fmt::Debug for KbkdfCtrHmacAlgorithm {
83
0
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
84
0
        core::fmt::Debug::fmt(&self.id, f)
85
0
    }
86
}
87
88
/// Key-based Derivation Function Algorithm Identifier
89
#[non_exhaustive]
90
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
91
pub enum KbkdfCtrHmacAlgorithmId {
92
    /// KBKDF in Counter Mode with HMAC-SHA224
93
    Sha224,
94
95
    /// KBKDF in Counter Mode with HMAC-SHA256
96
    Sha256,
97
98
    /// KBKDF in Counter Mode with HMAC-SHA384
99
    Sha384,
100
101
    /// KBKDF in Counter Mode with HMAC-SHA512
102
    Sha512,
103
}
104
105
/// # Key-based Key Derivation Function (KBKDF) in Counter Mode with HMAC PRF
106
///
107
/// ## Input Validation and Defaults
108
/// * `output.len() > 0 and `secret.len() > 0`
109
/// * `output.len() <= usize::MAX - DIGEST_LENGTH`
110
/// * The requested `output.len()` would result in overflowing the counter.
111
///
112
/// ## Implementation Notes
113
///
114
/// This implementation adheres to the algorithm specified in Section 4.1 of the
115
/// NIST Special Publication 800-108 Revision 1 Update 1 published on August
116
/// 2022. Using HMAC as the PRF function. In this implementation:
117
/// * The counter is 32-bits and is represented in big-endian format
118
/// * The counter is placed before the fixed info string
119
///
120
/// Specification available at <https://doi.org/10.6028/NIST.SP.800-108r1-upd1>
121
///
122
/// # Errors
123
/// `Unspecified` is returned if input validation fails or an unexpected error occurs.
124
0
pub fn kbkdf_ctr_hmac(
125
0
    algorithm: &'static KbkdfCtrHmacAlgorithm,
126
0
    secret: &[u8],
127
0
    info: &[u8],
128
0
    output: &mut [u8],
129
0
) -> Result<(), Unspecified> {
130
0
    let evp_md = algorithm.get_evp_md();
131
0
    let out_len = output.len();
132
0
    if 1 != unsafe {
133
0
        KBKDF_ctr_hmac(
134
0
            output.as_mut_ptr(),
135
0
            out_len,
136
0
            evp_md.as_const_ptr(),
137
0
            secret.as_ptr(),
138
0
            secret.len(),
139
0
            info.as_ptr(),
140
0
            info.len(),
141
0
        )
142
0
    } {
143
0
        return Err(Unspecified);
144
0
    }
145
0
    Ok(())
146
0
}