Skip to main content

script_webcrypto/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 js::context::JSContext;
7use script_bindings::codegen::GenericBindings::CryptoKeyBinding::{KeyType, KeyUsage};
8use script_bindings::codegen::GenericBindings::SubtleCryptoBinding::KeyFormat;
9use script_bindings::error::Error;
10use script_bindings::root::DomRoot;
11use sha1::Sha1;
12use sha2::{Sha256, Sha384, Sha512};
13
14use crate::cryptokey::{CryptoKey, Handle, KeyUsageSliceHelper};
15use crate::subtlecrypto::{
16    CryptoAlgorithm, HkdfParams, KeyAlgorithm, KeyAlgorithmAndDerivatives, NormalizedAlgorithm,
17};
18use crate::traits::Equivalence;
19
20/// <https://w3c.github.io/webcrypto/#hkdf-operations-derive-bits>
21pub(crate) fn derive_bits<D: Equivalence>(
22    normalized_algorithm: &HkdfParams,
23    key: &CryptoKey<D>,
24    length: Option<u32>,
25) -> Result<Vec<u8>, Error> {
26    // Step 1. If length is null or is not a multiple of 8, then throw an OperationError.
27    let Some(length) = length else {
28        return Err(Error::Operation(Some("length is null".into())));
29    };
30    if length % 8 != 0 {
31        return Err(Error::Operation(Some(
32            "length is not a multiple of 8".into(),
33        )));
34    };
35
36    // Step 2. Let hashLength be the length in bits of the output of the hash function identified by
37    // the hash member of normalizedAlgorithm.
38    let hash_length = match normalized_algorithm.hash.name() {
39        CryptoAlgorithm::Sha1 => 160,
40        CryptoAlgorithm::Sha256 => 256,
41        CryptoAlgorithm::Sha384 => 384,
42        CryptoAlgorithm::Sha512 => 512,
43        algorithm_name => {
44            return Err(Error::Operation(Some(format!(
45                "Invalid hash algorithm: {}",
46                algorithm_name.as_str()
47            ))));
48        },
49    };
50
51    // Step 3. If length is greater than 255 * hashLength, then throw an OperationError.
52    if length > 255 * hash_length {
53        return Err(Error::Operation(Some(
54            "length is greater than 255 * hashLength".into(),
55        )));
56    }
57
58    // Step 4. Let keyDerivationKey be the secret represented by the [[handle]] internal slot of key.
59    let Handle::HkdfSecret(key_derivation_key) = key.handle() else {
60        return Err(Error::Operation(Some(
61            "The [[handle]] internal slot is not from an HKDF key".into(),
62        )));
63    };
64
65    // Step 5. Let result be the result of performing the HKDF extract and then the HKDF expand
66    // step described in Section 2 of [RFC5869] using:
67    //     * the hash member of normalizedAlgorithm as Hash,
68    //     * keyDerivationKey as the input keying material, IKM,
69    //     * the salt member of normalizedAlgorithm as salt,
70    //     * the info member of normalizedAlgorithm as info,
71    //     * length divided by 8 as the value of L,
72    // Step 6. If the key derivation operation fails, then throw an OperationError.
73    let mut result = vec![0u8; length as usize / 8];
74    match normalized_algorithm.hash.name() {
75        CryptoAlgorithm::Sha1 => {
76            Hkdf::<Sha1>::new(Some(&normalized_algorithm.salt), key_derivation_key)
77                .expand(&normalized_algorithm.info, &mut result)
78                .map_err(|error| Error::Operation(Some(error.to_string())))?
79        },
80        CryptoAlgorithm::Sha256 => {
81            Hkdf::<Sha256>::new(Some(&normalized_algorithm.salt), key_derivation_key)
82                .expand(&normalized_algorithm.info, &mut result)
83                .map_err(|error| Error::Operation(Some(error.to_string())))?
84        },
85        CryptoAlgorithm::Sha384 => {
86            Hkdf::<Sha384>::new(Some(&normalized_algorithm.salt), key_derivation_key)
87                .expand(&normalized_algorithm.info, &mut result)
88                .map_err(|error| Error::Operation(Some(error.to_string())))?
89        },
90        CryptoAlgorithm::Sha512 => {
91            Hkdf::<Sha512>::new(Some(&normalized_algorithm.salt), key_derivation_key)
92                .expand(&normalized_algorithm.info, &mut result)
93                .map_err(|error| Error::Operation(Some(error.to_string())))?
94        },
95        algorithm_name => {
96            return Err(Error::Operation(Some(format!(
97                "Invalid hash algorithm: {}",
98                algorithm_name.as_str()
99            ))));
100        },
101    }
102
103    // Step 7. Return result.
104    Ok(result)
105}
106
107/// <https://w3c.github.io/webcrypto/#hkdf-operations-import-key>
108pub(crate) fn import_key<D: Equivalence>(
109    cx: &mut JSContext,
110    global: &D::GlobalScope,
111    format: KeyFormat,
112    key_data: &[u8],
113    extractable: bool,
114    usages: Vec<KeyUsage>,
115) -> Result<DomRoot<CryptoKey<D>>, Error> {
116    // Step 1. Let keyData be the key data to be imported.
117
118    // Step 2. If format is "raw":
119    if matches!(format, KeyFormat::Raw | KeyFormat::Raw_secret) {
120        // Step 2.1. If usages contains a value that is not "deriveKey" or "deriveBits", then throw
121        // a SyntaxError.
122        usages.ensure_only_contain_entries_from(&[KeyUsage::DeriveKey, KeyUsage::DeriveBits])?;
123
124        // Step 2.2. If extractable is not false, then throw a SyntaxError.
125        if extractable {
126            return Err(Error::Syntax(Some("'extractable' is not false".into())));
127        }
128
129        // Step 2.3. Let key be a new CryptoKey representing the key data provided in keyData.
130        // Step 2.4. Set the [[type]] internal slot of key to "secret".
131        // Step 2.5. Let algorithm be a new KeyAlgorithm object.
132        // Step 2.6. Set the name attribute of algorithm to "HKDF".
133        // Step 2.7. Set the [[algorithm]] internal slot of key to algorithm.
134        let algorithm = KeyAlgorithm {
135            name: CryptoAlgorithm::Hkdf,
136        };
137        let key = CryptoKey::new(
138            cx,
139            global,
140            KeyType::Secret,
141            extractable,
142            KeyAlgorithmAndDerivatives::KeyAlgorithm(algorithm),
143            usages.normalized_value(),
144            Handle::HkdfSecret(key_data.to_vec().into()),
145        );
146
147        // Step 2.8. Return key.
148        Ok(key)
149    }
150    // Otherwise:
151    else {
152        // throw a NotSupportedError.
153        Err(Error::NotSupported(Some(
154            "Formats different than \"raw\" are unsupported".into(),
155        )))
156    }
157}
158
159/// <https://w3c.github.io/webcrypto/#hkdf-operations-get-key-length>
160pub(crate) fn get_key_length() -> Result<Option<u32>, Error> {
161    // Step 1. Return null.
162    Ok(None)
163}