script/dom/subtlecrypto/
hkdf_operation.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use hkdf::Hkdf;
6use sha1::Sha1;
7use sha2::{Sha256, Sha384, Sha512};
8
9use crate::dom::bindings::codegen::Bindings::CryptoKeyBinding::{KeyType, KeyUsage};
10use crate::dom::bindings::codegen::Bindings::SubtleCryptoBinding::KeyFormat;
11use crate::dom::bindings::error::Error;
12use crate::dom::bindings::root::DomRoot;
13use crate::dom::cryptokey::{CryptoKey, Handle};
14use crate::dom::globalscope::GlobalScope;
15use crate::dom::subtlecrypto::{
16    ALG_HKDF, ALG_SHA1, ALG_SHA256, ALG_SHA384, ALG_SHA512, KeyAlgorithmAndDerivatives,
17    SubtleHkdfParams, SubtleKeyAlgorithm,
18};
19use crate::script_runtime::CanGc;
20
21/// <https://w3c.github.io/webcrypto/#hkdf-operations-derive-bits>
22pub(crate) fn derive_bits(
23    normalized_algorithm: &SubtleHkdfParams,
24    key: &CryptoKey,
25    length: Option<u32>,
26) -> Result<Vec<u8>, Error> {
27    // Step 1. If length is null or is not a multiple of 8, then throw an OperationError.
28    let Some(length) = length else {
29        return Err(Error::Operation(Some("length is null".into())));
30    };
31    if length % 8 != 0 {
32        return Err(Error::Operation(Some(
33            "length is not a multiple of 8".into(),
34        )));
35    };
36
37    // Step 2. Let keyDerivationKey be the secret represented by the [[handle]] internal slot of key.
38    let Handle::HkdfSecret(key_derivation_key) = key.handle() else {
39        return Err(Error::Operation(Some(
40            "The [[handle]] internal slot is not from an HKDF key".into(),
41        )));
42    };
43
44    // Step 3. Let result be the result of performing the HKDF extract and then the HKDF expand
45    // step described in Section 2 of [RFC5869] using:
46    //     * the hash member of normalizedAlgorithm as Hash,
47    //     * keyDerivationKey as the input keying material, IKM,
48    //     * the salt member of normalizedAlgorithm as salt,
49    //     * the info member of normalizedAlgorithm as info,
50    //     * length divided by 8 as the value of L,
51    // Step 4. If the key derivation operation fails, then throw an OperationError.
52    let mut result = vec![0u8; length as usize / 8];
53    match normalized_algorithm.hash.name() {
54        ALG_SHA1 => Hkdf::<Sha1>::new(Some(&normalized_algorithm.salt), key_derivation_key)
55            .expand(&normalized_algorithm.info, &mut result)
56            .map_err(|error| Error::Operation(Some(error.to_string())))?,
57        ALG_SHA256 => Hkdf::<Sha256>::new(Some(&normalized_algorithm.salt), key_derivation_key)
58            .expand(&normalized_algorithm.info, &mut result)
59            .map_err(|error| Error::Operation(Some(error.to_string())))?,
60        ALG_SHA384 => Hkdf::<Sha384>::new(Some(&normalized_algorithm.salt), key_derivation_key)
61            .expand(&normalized_algorithm.info, &mut result)
62            .map_err(|error| Error::Operation(Some(error.to_string())))?,
63        ALG_SHA512 => Hkdf::<Sha512>::new(Some(&normalized_algorithm.salt), key_derivation_key)
64            .expand(&normalized_algorithm.info, &mut result)
65            .map_err(|error| Error::Operation(Some(error.to_string())))?,
66        algorithm_name => {
67            return Err(Error::Operation(Some(format!(
68                "Invalid hash algorithm: {algorithm_name}"
69            ))));
70        },
71    }
72
73    // Step 5. Return result.
74    Ok(result)
75}
76
77/// <https://w3c.github.io/webcrypto/#hkdf-operations-import-key>
78pub(crate) fn import_key(
79    global: &GlobalScope,
80    format: KeyFormat,
81    key_data: &[u8],
82    extractable: bool,
83    usages: Vec<KeyUsage>,
84    can_gc: CanGc,
85) -> Result<DomRoot<CryptoKey>, Error> {
86    // Step 1. Let keyData be the key data to be imported.
87
88    // Step 2. If format is "raw":
89    if matches!(format, KeyFormat::Raw | KeyFormat::Raw_secret) {
90        // Step 2.1. If usages contains a value that is not "deriveKey" or "deriveBits", then throw
91        // a SyntaxError.
92        if usages
93            .iter()
94            .any(|usage| !matches!(usage, KeyUsage::DeriveKey | KeyUsage::DeriveBits)) ||
95            usages.is_empty()
96        {
97            return Err(Error::Syntax(Some(
98                "Usages contains an entry which is not \"deriveKey\" or \"deriveBits\"".into(),
99            )));
100        }
101
102        // Step 2.2. If extractable is not false, then throw a SyntaxError.
103        if extractable {
104            return Err(Error::Syntax(Some("'extractable' is not false".into())));
105        }
106
107        // Step 2.3. Let key be a new CryptoKey representing the key data provided in keyData.
108        // Step 2.4. Set the [[type]] internal slot of key to "secret".
109        // Step 2.5. Let algorithm be a new KeyAlgorithm object.
110        // Step 2.6. Set the name attribute of algorithm to "HKDF".
111        // Step 2.7. Set the [[algorithm]] internal slot of key to algorithm.
112        let algorithm = SubtleKeyAlgorithm {
113            name: ALG_HKDF.to_string(),
114        };
115        let key = CryptoKey::new(
116            global,
117            KeyType::Secret,
118            extractable,
119            KeyAlgorithmAndDerivatives::KeyAlgorithm(algorithm),
120            usages,
121            Handle::HkdfSecret(key_data.to_vec()),
122            can_gc,
123        );
124
125        // Step 2.8. Return key.
126        Ok(key)
127    }
128    // Otherwise:
129    else {
130        // throw a NotSupportedError.
131        Err(Error::NotSupported(Some(
132            "Formats different than \"raw\" are unsupported".into(),
133        )))
134    }
135}
136
137/// <https://w3c.github.io/webcrypto/#hkdf-operations-get-key-length>
138pub(crate) fn get_key_length() -> Result<Option<u32>, Error> {
139    // Step 1. Return null.
140    Ok(None)
141}