Truly independent web browser
LibWeb: Port WebCrypto AES algorithms to Rust
Shannon Booth6cc1131bb5fb
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rsindex 38cdaec1df9..f1c6e5c9d2f 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs@@ -288,6 +288,16 @@ pub(crate) fn to_octet_enforcing_range(vm: &Vm, value: Value) -> ThrowCompletionOk(to_integer_enforcing_range(vm, value, 0.0, f64::from(u8::MAX))? as u8)}+/// A JS value converted to an unsigned short with [EnforceRange], https://webidl.spec.whatwg.org/#js-unsigned-short+#[allow(+ clippy::cast_possible_truncation,+ clippy::cast_sign_loss,+ reason = "the value is an integer within the bounds of the type"+)]+pub(crate) fn to_unsigned_short_enforcing_range(vm: &Vm, value: Value) -> ThrowCompletionOr<u16> {+ Ok(to_integer_enforcing_range(vm, value, 0.0, f64::from(u16::MAX))? as u16)+}+/// A JS value converted to a long with [EnforceRange], https://webidl.spec.whatwg.org/#js-long#[allow(clippy::cast_possible_truncation,diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rsindex e6165719f84..a745cf288ef 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs@@ -19,7 +19,10 @@ use libjs_rust::api::{ErrorKind, ErrorType, Gc, Object, PropertyKey, RawNativeFunctionResult, Realm, Trace, Value, Vm,};-use crate::bindings::conversions::{buffer_source_type_of, to_octet_enforcing_range, to_unsigned_long_enforcing_range};+use crate::bindings::conversions::{+ buffer_source_type_of, to_octet_enforcing_range, to_unsigned_long_enforcing_range,+ to_unsigned_short_enforcing_range,+};use crate::bindings::interface::{Ref, cell_from_abi, cell_into_abi, realm_arguments};use crate::bindings::{CallContext, Exception, WebIdlResult};use crate::crypto::crypto::web_crypto_fill_with_random;@@ -357,10 +360,6 @@ pub(crate) enum AlgorithmId {RsaOaep,Ecdsa,Ecdh,- AesCtr,- AesCbc,- AesGcm,- AesKw,X25519,X448,ED25519,@@ -368,7 +367,6 @@ pub(crate) enum AlgorithmId {MlDsa,MlKem,ChaCha20Poly1305,- AesOcb,}#[derive(Clone, Copy)]@@ -387,11 +385,6 @@ pub(crate) enum ParameterId {EcKeyGenParams,EcKeyImportParams,EcdhKeyDeriveParams,- AesCtrParams,- AesKeyGenParams,- AesDerivedKeyParams,- AesCbcParams,- AesGcmParams,Ed448Params,ContextParams,AeadParams,@@ -651,14 +644,14 @@ pub(crate) trait AlgorithmMethods: Trace {None}- fn encrypt(&self, _realm: Gc<Realm>, _key: Ref<CryptoKey>, _data: &Bytes) -> WebIdlResult<Bytes> {+ fn encrypt(&self, _cx: &CallContext, _key: Ref<CryptoKey>, _data: &Bytes) -> WebIdlResult<Bytes> {Err(Exception::Dom("NotSupportedError",Utf16String::from_utf8("encrypt is not supported"),))}- fn decrypt(&self, _realm: Gc<Realm>, _key: Ref<CryptoKey>, _data: &Bytes) -> WebIdlResult<Bytes> {+ fn decrypt(&self, _cx: &CallContext, _key: Ref<CryptoKey>, _data: &Bytes) -> WebIdlResult<Bytes> {Err(Exception::Dom("NotSupportedError",Utf16String::from_utf8("decrypt is not supported"),@@ -836,7 +829,8 @@ impl AlgorithmMethods for CppAlgorithm {usages}- fn encrypt(&self, realm: Gc<Realm>, key: Ref<CryptoKey>, data: &Bytes) -> WebIdlResult<Bytes> {+ fn encrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, data: &Bytes) -> WebIdlResult<Bytes> {+ let realm = cx.realm();let mut out: Option<Bytes> = None;// SAFETY: The realm, algorithm and cells are live, and the other arguments are lent for the call.unsafe {@@ -853,7 +847,8 @@ impl AlgorithmMethods for CppAlgorithm {Ok(out.expect("LibWeb returns bytes"))}- fn decrypt(&self, realm: Gc<Realm>, key: Ref<CryptoKey>, data: &Bytes) -> WebIdlResult<Bytes> {+ fn decrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, data: &Bytes) -> WebIdlResult<Bytes> {+ let realm = cx.realm();let mut out: Option<Bytes> = None;// SAFETY: The realm, algorithm and cells are live, and the other arguments are lent for the call.unsafe {@@ -3447,6 +3442,2672 @@ impl AlgorithmMethods for Argon2Params {}}+#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+struct AesCbcParams {+ iv: Bytes,+}++impl AesCbcParams {+ fn from_value(vm: &Vm, object: Gc<Object>) -> WebIdlResult<Self> {+ let iv_value = object.get(vm, &PropertyKey::from_utf8("iv"))?;+ let Some(iv_source) = buffer_source_type_of(iv_value) else {+ return vm+ .throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"BufferSource"])+ .map_err(Into::into);+ };+ let iv = get_a_copy_of_the_bytes(vm, iv_source)?;++ Ok(Self { iv })+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+struct AesCtrParams {+ counter: Bytes,+ length: u8,+}++impl AesCtrParams {+ fn from_value(vm: &Vm, object: Gc<Object>) -> WebIdlResult<Self> {+ let iv_value = object.get(vm, &PropertyKey::from_utf8("counter"))?;+ let Some(iv_source) = buffer_source_type_of(iv_value) else {+ return vm+ .throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"BufferSource"])+ .map_err(Into::into);+ };+ let iv = get_a_copy_of_the_bytes(vm, iv_source)?;++ let length_value = object.get(vm, &PropertyKey::from_utf8("length"))?;+ let length = to_octet_enforcing_range(vm, length_value)?;++ Ok(Self { counter: iv, length })+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+struct AesGcmParams {+ iv: Bytes,+ additional_data: Option<Bytes>,+ tag_length: Option<u8>,+}++impl AesGcmParams {+ fn from_value(vm: &Vm, object: Gc<Object>) -> WebIdlResult<Self> {+ let iv_value = object.get(vm, &PropertyKey::from_utf8("iv"))?;+ let Some(iv_source) = buffer_source_type_of(iv_value) else {+ return vm+ .throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"BufferSource"])+ .map_err(Into::into);+ };+ let iv = get_a_copy_of_the_bytes(vm, iv_source)?;++ let mut additional_data = None;+ let additional_data_property = PropertyKey::from_utf8("additionalData");+ if object.has_property(vm, &additional_data_property)? {+ let additional_data_value = object.get(vm, &additional_data_property)?;+ let Some(additional_data_source) = buffer_source_type_of(additional_data_value) else {+ return vm+ .throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"BufferSource"])+ .map_err(Into::into);+ };+ additional_data = Some(get_a_copy_of_the_bytes(vm, additional_data_source)?);+ }++ let mut tag_length = None;+ let tag_length_property = PropertyKey::from_utf8("tagLength");+ if object.has_property(vm, &tag_length_property)? {+ let tag_length_value = object.get(vm, &tag_length_property)?;+ tag_length = Some(to_octet_enforcing_range(vm, tag_length_value)?);+ }++ Ok(Self {+ iv,+ additional_data,+ tag_length,+ })+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+struct AesKeyGenParams {+ length: u16,+}++impl AesKeyGenParams {+ fn from_value(vm: &Vm, object: Gc<Object>) -> WebIdlResult<Self> {+ let length_value = object.get(vm, &PropertyKey::from_utf8("length"))?;+ let length = to_unsigned_short_enforcing_range(vm, length_value)?;++ Ok(Self { length })+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+struct AesDerivedKeyParams {+ length: u16,+}++impl AesDerivedKeyParams {+ fn from_value(vm: &Vm, object: Gc<Object>) -> WebIdlResult<Self> {+ let length_value = object.get(vm, &PropertyKey::from_utf8("length"))?;+ let length = to_unsigned_short_enforcing_range(vm, length_value)?;++ Ok(Self { length })+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+struct AeadParams {+ iv: Bytes,+ additional_data: Option<Bytes>,+ tag_length: Option<u8>,+}++impl AeadParams {+ fn from_value(vm: &Vm, object: Gc<Object>) -> WebIdlResult<Self> {+ let iv_value = object.get(vm, &PropertyKey::from_utf8("iv"))?;+ let Some(iv_source) = buffer_source_type_of(iv_value) else {+ return vm+ .throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"BufferSource"])+ .map_err(Into::into);+ };+ let iv = get_a_copy_of_the_bytes(vm, iv_source)?;++ let mut additional_data = None;+ let additional_data_property = PropertyKey::from_utf8("additionalData");+ if object.has_property(vm, &additional_data_property)? {+ let additional_data_value = object.get(vm, &additional_data_property)?;+ let Some(additional_data_source) = buffer_source_type_of(additional_data_value) else {+ return vm+ .throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"BufferSource"])+ .map_err(Into::into);+ };+ additional_data = Some(get_a_copy_of_the_bytes(vm, additional_data_source)?);+ }++ let mut tag_length = None;+ let tag_length_property = PropertyKey::from_utf8("tagLength");+ if object.has_property(vm, &tag_length_property)? {+ let tag_length_value = object.get(vm, &tag_length_property)?;+ tag_length = Some(to_octet_enforcing_range(vm, tag_length_value)?);+ }++ Ok(Self {+ iv,+ additional_data,+ tag_length,+ })+ }+}++#[derive(Clone, Copy)]+pub(crate) enum AesParameterType {+ Base,+ Cbc,+ Ctr,+ Gcm,+ KeyGen,+ DerivedKey,+ Aead,+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+enum AesParameters {+ Base,+ Cbc(AesCbcParams),+ Ctr(AesCtrParams),+ Gcm(AesGcmParams),+ KeyGen(AesKeyGenParams),+ DerivedKey(AesDerivedKeyParams),+ Aead(AeadParams),+}++impl AesParameters {+ fn from_value(vm: &Vm, object: Gc<Object>, parameter_type: AesParameterType) -> WebIdlResult<Self> {+ Ok(match parameter_type {+ AesParameterType::Base => Self::Base,+ AesParameterType::Cbc => Self::Cbc(AesCbcParams::from_value(vm, object)?),+ AesParameterType::Ctr => Self::Ctr(AesCtrParams::from_value(vm, object)?),+ AesParameterType::Gcm => Self::Gcm(AesGcmParams::from_value(vm, object)?),+ AesParameterType::KeyGen => Self::KeyGen(AesKeyGenParams::from_value(vm, object)?),+ AesParameterType::DerivedKey => Self::DerivedKey(AesDerivedKeyParams::from_value(vm, object)?),+ AesParameterType::Aead => Self::Aead(AeadParams::from_value(vm, object)?),+ })+ }+}++unsafe extern "C" {+ fn web_crypto_aes_cbc_cipher(+ encrypt: bool,+ key: Span<'_, u8>,+ input: Span<'_, u8>,+ iv: Span<'_, u8>,+ state: *mut c_void,+ receive: ReceiveBytes,+ ) -> bool;+ fn web_crypto_aes_ctr_cipher(+ encrypt: bool,+ key: Span<'_, u8>,+ input: Span<'_, u8>,+ counter: Span<'_, u8>,+ state: *mut c_void,+ receive: ReceiveBytes,+ ) -> bool;+ fn web_crypto_aes_kw_cipher(+ wrap: bool,+ key: Span<'_, u8>,+ input: Span<'_, u8>,+ state: *mut c_void,+ receive: ReceiveBytes,+ ) -> bool;+ fn web_crypto_aes_gcm_encrypt(+ key: Span<'_, u8>,+ plaintext: Span<'_, u8>,+ iv: Span<'_, u8>,+ additional_data: Span<'_, u8>,+ tag_length: usize,+ ciphertext: *mut c_void,+ tag: *mut c_void,+ receive: ReceiveBytes,+ ) -> bool;+ fn web_crypto_aes_gcm_decrypt(+ key: Span<'_, u8>,+ ciphertext: Span<'_, u8>,+ iv: Span<'_, u8>,+ additional_data: Span<'_, u8>,+ tag: Span<'_, u8>,+ state: *mut c_void,+ receive: ReceiveBytes,+ ) -> bool;+ fn web_crypto_aes_ocb_cipher(+ encrypt: bool,+ key: Span<'_, u8>,+ input: Span<'_, u8>,+ iv: Span<'_, u8>,+ additional_data: Span<'_, u8>,+ tag_length: usize,+ state: *mut c_void,+ receive: ReceiveBytes,+ ) -> usize;+}+#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct AesCbc {+ parameters: AesParameters,+}++impl AesCbc {+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, parameter_type: AesParameterType) -> WebIdlResult<Self> {+ Ok(Self {+ parameters: AesParameters::from_value(vm, object, parameter_type)?,+ })+ }+}++impl AlgorithmMethods for AesCbc {+ // https://w3c.github.io/webcrypto/#aes-cbc-operations-encrypt+ fn validate_encrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let AesParameters::Cbc(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Cbc parameters")+ };++ // 1. If the iv member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.+ if normalized_algorithm.iv.as_slice().len() != 16 {+ return Err(dom_error("OperationError", b"IV to AES-CBC must be exactly 16 bytes"));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-cbc-operations-encrypt+ fn encrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, plaintext: &Bytes) -> WebIdlResult<Bytes> {+ let AesParameters::Cbc(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Cbc parameters")+ };++ // NB: Step 1 is performed by validate_encrypt_parameters().+ self.validate_encrypt_parameters(cx.realm())?;++ // 2. Let paddedPlaintext be the result of adding padding octets to the contents of plaintext according to the procedure defined in Section 10.3 of [RFC2315], step 2, with a value of k of 16.+ // 3. Let ciphertext be the result of performing the CBC Encryption operation described in Section 6.2 of [NIST-SP800-38A] using AES as the block cipher, the contents of the iv member of normalizedAlgorithm as the IV input parameter and paddedPlaintext as the input plaintext.+ let mut ciphertext: Option<Bytes> = None;+ // SAFETY: The input and output slots live for the call.+ let maybe_ciphertext = unsafe {+ web_crypto_aes_cbc_cipher(+ true,+ Span::new(key.byte_handle_bytes()),+ Span::new(plaintext.as_slice()),+ Span::new(normalized_algorithm.iv.as_slice()),+ core::ptr::from_mut(&mut ciphertext).cast(),+ receive_bytes,+ )+ };+ if !maybe_ciphertext {+ return Err(dom_error("OperationError", b"Failed to encrypt"));+ }++ // 4. Return the result of creating an ArrayBuffer containing ciphertext.+ Ok(ciphertext.expect("LibCrypto returns the result"))+ }++ // https://w3c.github.io/webcrypto/#aes-cbc-operations-decrypt+ fn validate_decrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let AesParameters::Cbc(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Cbc parameters")+ };++ // 1. If the iv member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.+ if normalized_algorithm.iv.as_slice().len() != 16 {+ return Err(dom_error("OperationError", b"IV to AES-CBC must be exactly 16 bytes"));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-cbc-operations-decrypt+ fn decrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, ciphertext: &Bytes) -> WebIdlResult<Bytes> {+ let AesParameters::Cbc(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Cbc parameters")+ };++ // NB: Step 1 is performed by validate_decrypt_parameters().+ self.validate_decrypt_parameters(cx.realm())?;++ // 2. If the length of ciphertext is zero or is not a multiple of 16 bytes, then throw an OperationError.+ if ciphertext.as_slice().is_empty() || !ciphertext.as_slice().len().is_multiple_of(16) {+ return Err(dom_error(+ "OperationError",+ b"Ciphertext length must be a multiple of 16 bytes",+ ));+ }++ // 3. Let paddedPlaintext be the result of performing the CBC Decryption operation described in Section 6.2 of [NIST-SP800-38A] using AES as the block cipher, the iv member of normalizedAlgorithm as the IV input parameter and ciphertext as the input ciphertext.+ // 4. Let p be the value of the last octet of paddedPlaintext.+ // 5. If p is zero or greater than 16, or if any of the last p octets of paddedPlaintext have a value which is not p, then throw an OperationError.+ // 6. Let plaintext be the result of removing p octets from the end of paddedPlaintext.+ let mut plaintext: Option<Bytes> = None;+ // SAFETY: The input and output slots live for the call.+ let maybe_plaintext = unsafe {+ web_crypto_aes_cbc_cipher(+ false,+ Span::new(key.byte_handle_bytes()),+ Span::new(ciphertext.as_slice()),+ Span::new(normalized_algorithm.iv.as_slice()),+ core::ptr::from_mut(&mut plaintext).cast(),+ receive_bytes,+ )+ };+ if !maybe_plaintext {+ return Err(dom_error("OperationError", b"Failed to decrypt"));+ }++ // 7. Return plaintext.+ Ok(plaintext.expect("LibCrypto returns the result"))+ }++ // https://w3c.github.io/webcrypto/#aes-cbc-operations-import-key+ #[allow(+ clippy::collapsible_if,+ reason = "the spec checks the JWK algorithm after matching the key length"+ )]+ fn import_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ format: KeyFormat,+ key_data: ImportKeyData,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<Ref<CryptoKey>> {+ // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::Encrypt+ && usage != KeyUsage::Decrypt+ && usage != KeyUsage::WrapKey+ && usage != KeyUsage::UnwrapKey+ {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ // 2.+ let data;+ if format == KeyFormat::Raw || format == KeyFormat::RawSecret {+ // -> If format is "raw":+ // 1. Let data be the octet string contained in keyData.+ // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.+ let ImportKeyData::Bytes(bytes) = key_data else {+ unreachable!("raw key data contains bytes")+ };+ data = bytes;+ let length_in_bits = data.as_slice().len() * 8;+ if length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256 {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted(+ "Invalid key length '{}' bits (must be either 128, 192, or 256 bits)",+ &[&length_in_bits],+ ),+ ));+ }+ } else if format == KeyFormat::Jwk {+ // -> If format is "jwk":+ // 1. -> If keyData is a JsonWebKey dictionary:+ // Let jwk equal keyData.+ // -> Otherwise:+ // Throw a DataError.+ let ImportKeyData::JsonWebKey(jwk) = key_data else {+ return Err(dom_error("DataError", b"keyData is not a JsonWebKey dictionary"));+ };++ // 2. If the kty field of jwk is not "oct", then throw a DataError.+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"oct")) {+ return Err(dom_error("DataError", b"Invalid key type"));+ }++ // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.+ // Specifically, those requirements are:+ // - ".k" is a valid bas64url encoded octet stream, which we do by just parsing it, in step 4.+ // - ".alg" is checked only in step 5.++ // 4. Let data be the octet string obtained by decoding the k field of jwk.+ data = parse_jwk_symmetric_key(cx.vm(), &jwk)?;++ // 5. -> If data has length 128 bits:+ // If the alg field of jwk is present, and is not "A128CBC", then throw a DataError.+ // -> If data has length 192 bits:+ // If the alg field of jwk is present, and is not "A192CBC", then throw a DataError.+ // -> If data has length 256 bits:+ // If the alg field of jwk is present, and is not "A256CBC", then throw a DataError.+ // -> Otherwise:+ // throw a DataError.+ let data_bits = data.as_slice().len() * 8;+ let alg = &jwk.alg;+ if data_bits == 128 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A128CBC"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm",+ ));+ }+ } else if data_bits == 192 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A192CBC"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm",+ ));+ }+ } else if data_bits == 256 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A256CBC"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm",+ ));+ }+ } else {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted("Invalid key size: {} bits", &[&data_bits]),+ ));+ }++ // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.+ if !key_usages.is_empty()+ && jwk+ .r#use+ .as_ref()+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"enc"))+ {+ return Err(dom_error("DataError", b"Invalid use field"));+ }++ // 7. If the key_ops field of jwk is present, and is invalid according to the+ // requirements of JSON Web Key [JWK] or does not contain all of the specified usages+ // values, then throw a DataError.+ validate_jwk_key_ops(&jwk, key_usages)?;++ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.+ if jwk.ext == Some(false) && extractable {+ return Err(dom_error("DataError", b"Invalid ext field"));+ }+ } else {+ // Otherwise:+ // throw a NotSupportedError+ return Err(dom_error(+ "NotSupportedError",+ b"Only raw and jwk formats are supported",+ ));+ }++ // 3. Let key be a new CryptoKey object representing an AES key with value data.+ let data_bits = data.as_slice().len() * 8;+ let key = CryptoKey::from_byte_handle(cx.vm(), data.as_slice());++ // 4. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 5. Let algorithm be a new AesKeyAlgorithm.+ // 6. Set the name attribute of algorithm to "AES-CBC".+ // 7. Set the length attribute of algorithm to the length, in bits, of data.+ // 8. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-CBC"),+ length: u16::try_from(data_bits).expect("AES key length was validated"),+ });++ // 9. Return key.+ Ok(key)+ }++ // https://w3c.github.io/webcrypto/#aes-cbc-operations-generate-key+ fn validate_generate_key_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ let AesParameters::KeyGen(length) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = length.length;+ if bits != 128 && bits != 192 && bits != 256 {+ return Err(Exception::Dom(+ "OperationError",+ libjs_rust::api::utf16_formatted("Cannot create AES-CBC key with unusual amount of {} bits", &[&bits]),+ ));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-cbc-operations-generate-key+ fn generate_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {+ // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::Encrypt+ && usage != KeyUsage::Decrypt+ && usage != KeyUsage::WrapKey+ && usage != KeyUsage::UnwrapKey+ {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ let AesParameters::KeyGen(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };++ // NB: Step 2 is performed by validate_generate_key_parameters().+ self.validate_generate_key_parameters(cx.realm())?;+ let bits = normalized_algorithm.length;++ // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.+ let key_buffer = generate_random_key(cx.vm(), u32::from(bits))?;++ // 4. If the key generation step fails, then throw an OperationError.+ // Note: Cannot happen in our implementation; and if we OOM, then allocating the Exception is probably going to crash anyway.++ // 5. Let key be a new CryptoKey object representing the generated AES key.+ let key = CryptoKey::from_byte_handle(cx.vm(), &key_buffer);++ // 9. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 6. Let algorithm be a new AesKeyAlgorithm.+ // 7. Set the name attribute of algorithm to "AES-CBC".+ // 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.+ // 10. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-CBC"),+ length: bits,+ });++ // 11. Set the [[extractable]] internal slot of key to be extractable.+ key.set_extractable(extractable);++ // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.+ key.set_usages(normalized_key_usages(key_usages));++ // 13. Return key.+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKey(key))+ }++ // https://w3c.github.io/webcrypto/#aes-cbc-operations-export-key+ #[allow(+ clippy::field_reassign_with_default,+ reason = "the algorithm sets JWK members in separate steps"+ )]+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {+ // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.+ // Note: In our impl this is always accessible++ // 2. -> If format is "raw":+ // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat+ // For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an alias of "raw".+ let result = if format == KeyFormat::Raw || format == KeyFormat::RawSecret {+ // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.+ let data = key.byte_handle_bytes();++ // 2. Let result be the result of creating an ArrayBuffer containing data.+ ExportKeyResult::Bytes(Bytes::new(data.to_vec()))+ }+ // -> If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. Let jwk be a new JsonWebKey dictionary.+ let mut jwk = JsonWebKey::default();++ // 2. Set the kty attribute of jwk to the string "oct".+ jwk.kty = Some(Utf16String::from_ascii(b"oct"));++ // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key, encoded according to Section 6.4 of JSON Web Algorithms [JWA].+ let key_bytes = key.byte_handle_bytes();+ let Ok(encoded) = base64_url_bytes_encode(key_bytes) else {+ return cx+ .vm()+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])+ .map_err(Into::into);+ };+ jwk.k = Some(encoded);++ // 4. -> If the length attribute of key is 128:+ // Set the alg attribute of jwk to the string "A128CBC".+ // -> If the length attribute of key is 192:+ // Set the alg attribute of jwk to the string "A192CBC".+ // -> If the length attribute of key is 256:+ // Set the alg attribute of jwk to the string "A256CBC".+ let key_bits = key_bytes.len() * 8;+ if key_bits == 128 {+ jwk.alg = Some(Utf16String::from_ascii(b"A128CBC"));+ } else if key_bits == 192 {+ jwk.alg = Some(Utf16String::from_ascii(b"A192CBC"));+ } else if key_bits == 256 {+ jwk.alg = Some(Utf16String::from_ascii(b"A256CBC"));+ } else {+ return Err(dom_error("OperationError", b"unclear key size"));+ }++ // 5. Set the key_ops attribute of jwk to equal the usages attribute of key.+ jwk.key_ops = Some(+ key.internal_usages()+ .iter()+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))+ .collect(),+ );++ // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.+ jwk.ext = Some(key.is_extractable());++ // 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].+ ExportKeyResult::JsonWebKey(jwk)+ }+ // -> Otherwise:+ else {+ // throw a NotSupportedError.+ return Err(dom_error("NotSupportedError", b"Cannot export to unsupported format"));+ };++ // 3. Return result.+ Ok(result)+ }++ // https://w3c.github.io/webcrypto/#aes-cbc-operations-get-key-length+ fn get_key_length(&self, _: Gc<Realm>, _: &AlgorithmParams) -> WebIdlResult<Option<u32>> {+ // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw an OperationError.+ let AesParameters::DerivedKey(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses DerivedKey parameters")+ };+ let length = normalized_algorithm.length;+ if length != 128 && length != 192 && length != 256 {+ return Err(dom_error("OperationError", b"Invalid key length"));+ }++ // 2. Return the length member of normalizedDerivedKeyAlgorithm.+ Ok(Some(u32::from(length)))+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct AesCtr {+ parameters: AesParameters,+}++impl AesCtr {+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, parameter_type: AesParameterType) -> WebIdlResult<Self> {+ Ok(Self {+ parameters: AesParameters::from_value(vm, object, parameter_type)?,+ })+ }+}++impl AlgorithmMethods for AesCtr {+ // https://w3c.github.io/webcrypto/#aes-ctr-operations-import-key+ #[allow(+ clippy::collapsible_if,+ reason = "the spec checks the JWK algorithm after matching the key length"+ )]+ fn import_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ format: KeyFormat,+ key_data: ImportKeyData,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<Ref<CryptoKey>> {+ // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::Encrypt+ && usage != KeyUsage::Decrypt+ && usage != KeyUsage::WrapKey+ && usage != KeyUsage::UnwrapKey+ {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ let data;++ // 2. If format is "raw":+ if format == KeyFormat::Raw || format == KeyFormat::RawSecret {+ // 1. Let data be the octet string contained in keyData.+ let ImportKeyData::Bytes(bytes) = key_data else {+ unreachable!("raw key data contains bytes")+ };+ data = bytes;++ // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.+ let length_in_bits = data.as_slice().len() * 8;+ if length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256 {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted(+ "Invalid key length '{}' bits (must be either 128, 192, or 256 bits)",+ &[&length_in_bits],+ ),+ ));+ }+ }+ // 2. If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. -> If keyData is a JsonWebKey dictionary:+ // Let jwk equal keyData.+ // -> Otherwise:+ // Throw a DataError.+ let ImportKeyData::JsonWebKey(jwk) = key_data else {+ return Err(dom_error("DataError", b"keyData is not a JsonWebKey dictionary"));+ };++ // 2. If the kty field of jwk is not "oct", then throw a DataError.+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"oct")) {+ return Err(dom_error("DataError", b"Invalid key type"));+ }++ // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.+ // Specifically, those requirements are:+ // * the member "k" is used to represent a symmetric key (or another key whose value is a single octet sequence).+ // * An "alg" member SHOULD also be present to identify the algorithm intended to be used with the key,+ // unless the application uses another means or convention to determine the algorithm used.+ if jwk.k.is_none() {+ return Err(dom_error("DataError", b"Missing 'k' field"));+ }++ if jwk.alg.is_none() {+ return Err(dom_error("DataError", b"Missing 'alg' field"));+ }++ // 4. Let data be the octet string obtained by decoding the k field of jwk.+ data = parse_jwk_symmetric_key(cx.vm(), &jwk)?;++ // 5. -> If data has length 128 bits:+ // If the alg field of jwk is present, and is not "A128CTR", then throw a DataError.+ // -> If data has length 192 bits:+ // If the alg field of jwk is present, and is not "A192CTR", then throw a DataError.+ // -> If data has length 256 bits:+ // If the alg field of jwk is present, and is not "A256CTR", then throw a DataError.+ // -> Otherwise:+ // throw a DataError.+ let data_bits = data.as_slice().len() * 8;+ let alg = &jwk.alg;+ if data_bits == 128 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A128CTR"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm",+ ));+ }+ } else if data_bits == 192 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A192CTR"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm",+ ));+ }+ } else if data_bits == 256 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A256CTR"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm",+ ));+ }+ } else {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted("Invalid key size: {} bits", &[&data_bits]),+ ));+ }++ // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.+ if !key_usages.is_empty()+ && jwk+ .r#use+ .as_ref()+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"enc"))+ {+ return Err(dom_error("DataError", b"Invalid use field"));+ }++ // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK]+ // or does not contain all of the specified usages values, then throw a DataError.+ validate_jwk_key_ops(&jwk, key_usages)?;++ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.+ if jwk.ext == Some(false) && extractable {+ return Err(dom_error("DataError", b"Invalid ext field"));+ }+ }+ // 2. Otherwise:+ else {+ // 1. throw a NotSupportedError.+ return Err(dom_error(+ "NotSupportedError",+ b"Only raw and jwk formats are supported",+ ));+ }++ let data_bits = data.as_slice().len() * 8;++ // 3. Let key be a new CryptoKey object representing an AES key with value data.+ let key = CryptoKey::from_byte_handle(cx.vm(), data.as_slice());++ // 4. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 5. Let algorithm be a new AesKeyAlgorithm.+ // 6. Set the name attribute of algorithm to "AES-CTR".+ // 7. Set the length attribute of algorithm to the length, in bits, of data.+ // 8. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-CTR"),+ length: u16::try_from(data_bits).expect("AES key length was validated"),+ });++ // 9. Return key.+ Ok(key)+ }++ // https://w3c.github.io/webcrypto/#aes-ctr-operations-export-key+ #[allow(+ clippy::field_reassign_with_default,+ reason = "the algorithm sets JWK members in separate steps"+ )]+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {+ // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.+ // Note: In our impl this is always accessible++ // 2. If format is "raw":+ // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat+ // For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an alias of "raw".+ let result = if format == KeyFormat::Raw || format == KeyFormat::RawSecret {+ // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.+ let data = key.byte_handle_bytes();++ // 2. Let result be the result of creating an ArrayBuffer containing data.+ ExportKeyResult::Bytes(Bytes::new(data.to_vec()))+ }+ // 2. If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. Let jwk be a new JsonWebKey dictionary.+ let mut jwk = JsonWebKey::default();++ // 2. Set the kty attribute of jwk to the string "oct".+ jwk.kty = Some(Utf16String::from_ascii(b"oct"));++ // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,+ // encoded according to Section 6.4 of JSON Web Algorithms [JWA].+ let key_bytes = key.byte_handle_bytes();+ let Ok(encoded) = base64_url_bytes_encode(key_bytes) else {+ return cx+ .vm()+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])+ .map_err(Into::into);+ };+ jwk.k = Some(encoded);++ // 4. -> If the length attribute of key is 128:+ // Set the alg attribute of jwk to the string "A128CTR".+ // -> If the length attribute of key is 192:+ // Set the alg attribute of jwk to the string "A192CTR".+ // -> If the length attribute of key is 256:+ // Set the alg attribute of jwk to the string "A256CTR".+ let key_bits = key_bytes.len() * 8;+ if key_bits == 128 {+ jwk.alg = Some(Utf16String::from_ascii(b"A128CTR"));+ } else if key_bits == 192 {+ jwk.alg = Some(Utf16String::from_ascii(b"A192CTR"));+ } else if key_bits == 256 {+ jwk.alg = Some(Utf16String::from_ascii(b"A256CTR"));+ }++ // 5. Set the key_ops attribute of jwk to the usages attribute of key.+ jwk.key_ops = Some(+ key.internal_usages()+ .iter()+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))+ .collect(),+ );++ // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.+ jwk.ext = Some(key.is_extractable());++ // 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].+ ExportKeyResult::JsonWebKey(jwk)+ }+ // 2. Otherwise:+ else {+ // 1. throw a NotSupportedError.+ return Err(dom_error("NotSupportedError", b"Cannot export to unsupported format"));+ };++ // 3. Return result.+ Ok(result)+ }++ // https://w3c.github.io/webcrypto/#aes-ctr-operations-get-key-length+ fn get_key_length(&self, _: Gc<Realm>, _: &AlgorithmParams) -> WebIdlResult<Option<u32>> {+ // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw a OperationError.+ let AesParameters::DerivedKey(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses DerivedKey parameters")+ };+ let length = normalized_algorithm.length;+ if length != 128 && length != 192 && length != 256 {+ return Err(dom_error("OperationError", b"Invalid key length"));+ }++ // 2. Return the length member of normalizedDerivedKeyAlgorithm.+ Ok(Some(u32::from(length)))+ }++ // https://w3c.github.io/webcrypto/#aes-ctr-operations-generate-key+ fn validate_generate_key_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ let AesParameters::KeyGen(length) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = length.length;+ if bits != 128 && bits != 192 && bits != 256 {+ return Err(Exception::Dom(+ "OperationError",+ libjs_rust::api::utf16_formatted("Cannot create AES-CTR key with unusual amount of {} bits", &[&bits]),+ ));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-ctr-operations-generate-key+ fn generate_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {+ // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::Encrypt+ && usage != KeyUsage::Decrypt+ && usage != KeyUsage::WrapKey+ && usage != KeyUsage::UnwrapKey+ {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ // NB: Step 2 is performed by validate_generate_key_parameters().+ self.validate_generate_key_parameters(cx.realm())?;+ let AesParameters::KeyGen(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = normalized_algorithm.length;++ // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.+ // 4. If the key generation step fails, then throw an OperationError.+ let key_buffer = generate_random_key(cx.vm(), u32::from(bits))?;++ // 5. Let key be a new CryptoKey object representing the generated AES key.+ let key = CryptoKey::from_byte_handle(cx.vm(), &key_buffer);++ // 9. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 6. Let algorithm be a new AesKeyAlgorithm.+ // 7. Set the name attribute of algorithm to "AES-CTR".+ // 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.+ // 10. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-CTR"),+ length: bits,+ });++ // 11. Set the [[extractable]] internal slot of key to be extractable.+ key.set_extractable(extractable);++ // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.+ key.set_usages(normalized_key_usages(key_usages));++ // 13. Return key.+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKey(key))+ }++ // https://w3c.github.io/webcrypto/#aes-ctr-operations-encrypt+ fn validate_encrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let AesParameters::Ctr(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Ctr parameters")+ };++ // 1. If the counter member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.+ if normalized_algorithm.counter.as_slice().len() != 16 {+ return Err(dom_error("OperationError", b"Invalid counter length"));+ }++ // 2. If the length member of normalizedAlgorithm is zero or is greater than 128, then throw an OperationError.+ if normalized_algorithm.length == 0 || normalized_algorithm.length > 128 {+ return Err(dom_error("OperationError", b"Invalid length"));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-ctr-operations-encrypt+ fn encrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, plaintext: &Bytes) -> WebIdlResult<Bytes> {+ // NB: Steps 1 and 2 are performed by validate_encrypt_parameters().+ self.validate_encrypt_parameters(cx.realm())?;+ let AesParameters::Ctr(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Ctr parameters")+ };+ let counter = &normalized_algorithm.counter;++ // 3. Let ciphertext be the result of performing the CTR Encryption operation described in Section 6.5 of [NIST-SP800-38A] using+ // AES as the block cipher,+ // the contents of the counter member of normalizedAlgorithm as the initial value of the counter block,+ // the length member of normalizedAlgorithm as the input parameter m to the standard counter block incrementing function defined in Appendix B.1 of [NIST-SP800-38A]+ // and the contents of plaintext as the input plaintext.+ let mut ciphertext: Option<Bytes> = None;+ // SAFETY: The input and output slots live for the call.+ let maybe_ciphertext = unsafe {+ web_crypto_aes_ctr_cipher(+ true,+ Span::new(key.byte_handle_bytes()),+ Span::new(plaintext.as_slice()),+ Span::new(counter.as_slice()),+ core::ptr::from_mut(&mut ciphertext).cast(),+ receive_bytes,+ )+ };+ if !maybe_ciphertext {+ return Err(dom_error("OperationError", b"Encryption failed"));+ }++ // 4. Return the result of creating an ArrayBuffer containing plaintext.+ Ok(ciphertext.expect("LibCrypto returns the result"))+ }++ // https://w3c.github.io/webcrypto/#aes-ctr-operations-decrypt+ fn validate_decrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let AesParameters::Ctr(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Ctr parameters")+ };++ // 1. If the counter member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.+ if normalized_algorithm.counter.as_slice().len() != 16 {+ return Err(dom_error("OperationError", b"Invalid counter length"));+ }++ // 2. If the length member of normalizedAlgorithm is zero or is greater than 128, then throw an OperationError.+ if normalized_algorithm.length == 0 || normalized_algorithm.length > 128 {+ return Err(dom_error("OperationError", b"Invalid length"));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-ctr-operations-decrypt+ fn decrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, ciphertext: &Bytes) -> WebIdlResult<Bytes> {+ // NB: Steps 1 and 2 are performed by validate_decrypt_parameters().+ self.validate_decrypt_parameters(cx.realm())?;+ let AesParameters::Ctr(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Ctr parameters")+ };+ let counter = &normalized_algorithm.counter;++ // 3. Let plaintext be the result of performing the CTR Decryption operation described in Section 6.5 of [NIST-SP800-38A] using+ // AES as the block cipher,+ // the contents of the counter member of normalizedAlgorithm as the initial value of the counter block,+ // the length member of normalizedAlgorithm as the input parameter m to the standard counter block incrementing function defined in Appendix B.1 of [NIST-SP800-38A]+ // and the contents of ciphertext as the input ciphertext.+ let mut plaintext: Option<Bytes> = None;+ // SAFETY: The input and output slots live for the call.+ let maybe_plaintext = unsafe {+ web_crypto_aes_ctr_cipher(+ false,+ Span::new(key.byte_handle_bytes()),+ Span::new(ciphertext.as_slice()),+ Span::new(counter.as_slice()),+ core::ptr::from_mut(&mut plaintext).cast(),+ receive_bytes,+ )+ };+ if !maybe_plaintext {+ return Err(dom_error("OperationError", b"Decryption failed"));+ }++ // 4. Return the result of creating an ArrayBuffer containing plaintext.+ Ok(plaintext.expect("LibCrypto returns the result"))+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct AesGcm {+ parameters: AesParameters,+}++impl AesGcm {+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, parameter_type: AesParameterType) -> WebIdlResult<Self> {+ Ok(Self {+ parameters: AesParameters::from_value(vm, object, parameter_type)?,+ })+ }+}++impl AlgorithmMethods for AesGcm {+ // https://w3c.github.io/webcrypto/#aes-gcm-operations-get-key-length+ fn get_key_length(&self, _: Gc<Realm>, _: &AlgorithmParams) -> WebIdlResult<Option<u32>> {+ // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw a OperationError.+ let AesParameters::DerivedKey(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses DerivedKey parameters")+ };+ let length = normalized_algorithm.length;+ if length != 128 && length != 192 && length != 256 {+ return Err(dom_error("OperationError", b"Invalid key length"));+ }++ // 2. Return the length member of normalizedDerivedKeyAlgorithm.+ Ok(Some(u32::from(length)))+ }++ // https://w3c.github.io/webcrypto/#aes-gcm-operations-import-key+ #[allow(+ clippy::collapsible_if,+ reason = "the spec checks the JWK algorithm after matching the key length"+ )]+ fn import_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ format: KeyFormat,+ key_data: ImportKeyData,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<Ref<CryptoKey>> {+ // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::Encrypt+ && usage != KeyUsage::Decrypt+ && usage != KeyUsage::WrapKey+ && usage != KeyUsage::UnwrapKey+ {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ let data;++ // 2. If format is "raw":+ if format == KeyFormat::Raw || format == KeyFormat::RawSecret {+ // 1. Let data be the octet string contained in keyData.+ let ImportKeyData::Bytes(bytes) = key_data else {+ unreachable!("raw key data contains bytes")+ };+ data = bytes;++ // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.+ let length_in_bits = data.as_slice().len() * 8;+ if length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256 {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted(+ "Invalid key length '{}' bits (must be either 128, 192, or 256 bits)",+ &[&length_in_bits],+ ),+ ));+ }+ }+ // 2. If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. -> If keyData is a JsonWebKey dictionary:+ // Let jwk equal keyData.+ // -> Otherwise:+ // Throw a DataError.+ let ImportKeyData::JsonWebKey(jwk) = key_data else {+ return Err(dom_error("DataError", b"keyData is not a JsonWebKey dictionary"));+ };++ // 2. If the kty field of jwk is not "oct", then throw a DataError.+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"oct")) {+ return Err(dom_error("DataError", b"Invalid key type"));+ }++ // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.+ // Specifically, those requirements are:+ // * the member "k" is used to represent a symmetric key (or another key whose value is a single octet sequence).+ // * An "alg" member SHOULD also be present to identify the algorithm intended to be used with the key,+ // unless the application uses another means or convention to determine the algorithm used.+ if jwk.k.is_none() {+ return Err(dom_error("DataError", b"Missing 'k' field"));+ }++ if jwk.alg.is_none() {+ return Err(dom_error("DataError", b"Missing 'alg' field"));+ }++ // 4. Let data be the octet string obtained by decoding the k field of jwk.+ data = parse_jwk_symmetric_key(cx.vm(), &jwk)?;++ // 5. -> If data has length 128 bits:+ // If the alg field of jwk is present, and is not "A128GCM", then throw a DataError.+ // -> If data has length 192 bits:+ // If the alg field of jwk is present, and is not "A192GCM", then throw a DataError.+ // -> If data has length 256 bits:+ // If the alg field of jwk is present, and is not "A256GCM", then throw a DataError.+ // -> Otherwise:+ // throw a DataError.+ let data_bits = data.as_slice().len() * 8;+ let alg = &jwk.alg;+ if data_bits == 128 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A128GCM"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm",+ ));+ }+ } else if data_bits == 192 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A192GCM"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm",+ ));+ }+ } else if data_bits == 256 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A256GCM"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm",+ ));+ }+ } else {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted("Invalid key size: {} bits", &[&data_bits]),+ ));+ }++ // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.+ if !key_usages.is_empty()+ && jwk+ .r#use+ .as_ref()+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"enc"))+ {+ return Err(dom_error("DataError", b"Invalid use field"));+ }++ // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK]+ // or does not contain all of the specified usages values, then throw a DataError.+ validate_jwk_key_ops(&jwk, key_usages)?;++ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.+ if jwk.ext == Some(false) && extractable {+ return Err(dom_error("DataError", b"Invalid ext field"));+ }+ }+ // 2. Otherwise:+ else {+ // 1. throw a NotSupportedError.+ return Err(dom_error(+ "NotSupportedError",+ b"Only raw and jwk formats are supported",+ ));+ }++ let data_bits = data.as_slice().len() * 8;++ // 3. Let key be a new CryptoKey object representing an AES key with value data.+ let key = CryptoKey::from_byte_handle(cx.vm(), data.as_slice());++ // 4. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 5. Let algorithm be a new AesKeyAlgorithm.+ // 6. Set the name attribute of algorithm to "AES-GCM".+ // 7. Set the length attribute of algorithm to the length, in bits, of data.+ // 8. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-GCM"),+ length: u16::try_from(data_bits).expect("AES key length was validated"),+ });++ // 9. Return key.+ Ok(key)+ }++ // https://w3c.github.io/webcrypto/#aes-gcm-operations-export-key+ #[allow(+ clippy::field_reassign_with_default,+ reason = "the algorithm sets JWK members in separate steps"+ )]+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {+ // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.+ // Note: In our impl this is always accessible++ // 2. If format is "raw":+ // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat+ // For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an alias of "raw".+ let result = if format == KeyFormat::Raw || format == KeyFormat::RawSecret {+ // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.+ let data = key.byte_handle_bytes();++ // 2. Let result be the result of creating an ArrayBuffer containing data.+ ExportKeyResult::Bytes(Bytes::new(data.to_vec()))+ }+ // 2. If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. Let jwk be a new JsonWebKey dictionary.+ let mut jwk = JsonWebKey::default();++ // 2. Set the kty attribute of jwk to the string "oct".+ jwk.kty = Some(Utf16String::from_ascii(b"oct"));++ // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,+ // encoded according to Section 6.4 of JSON Web Algorithms [JWA].+ let key_bytes = key.byte_handle_bytes();+ let Ok(encoded) = base64_url_bytes_encode(key_bytes) else {+ return cx+ .vm()+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])+ .map_err(Into::into);+ };+ jwk.k = Some(encoded);++ // 4. -> If the length attribute of key is 128:+ // Set the alg attribute of jwk to the string "A128GCM".+ // -> If the length attribute of key is 192:+ // Set the alg attribute of jwk to the string "A192GCM".+ // -> If the length attribute of key is 256:+ // Set the alg attribute of jwk to the string "A256GCM".+ let key_bits = key_bytes.len() * 8;+ if key_bits == 128 {+ jwk.alg = Some(Utf16String::from_ascii(b"A128GCM"));+ } else if key_bits == 192 {+ jwk.alg = Some(Utf16String::from_ascii(b"A192GCM"));+ } else if key_bits == 256 {+ jwk.alg = Some(Utf16String::from_ascii(b"A256GCM"));+ }++ // 5. Set the key_ops attribute of jwk to the usages attribute of key.+ jwk.key_ops = Some(+ key.internal_usages()+ .iter()+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))+ .collect(),+ );++ // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.+ jwk.ext = Some(key.is_extractable());++ // 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].+ ExportKeyResult::JsonWebKey(jwk)+ }+ // 2. Otherwise:+ else {+ // 1. throw a NotSupportedError.+ return Err(dom_error("NotSupportedError", b"Cannot export to unsupported format"));+ };++ // 3. Return result.+ Ok(result)+ }++ // https://w3c.github.io/webcrypto/#aes-gcm-operations-encrypt+ fn validate_encrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let AesParameters::Gcm(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Gcm parameters")+ };++ // NB: Our AES-GCM rejects an empty IV.+ if normalized_algorithm.iv.as_slice().is_empty() {+ return Err(dom_error("OperationError", b"IV must not be empty"));+ }++ // 1. If the iv member of normalizedAlgorithm has a length greater than 2^64 - 1 bytes, then throw an OperationError.+ // NOTE: This is not possible++ // 2. If the additionalData member of normalizedAlgorithm is present and has a length greater than 2^64 - 1 bytes, then throw an OperationError.+ // NOTE: This is not possible++ // 3. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.+ // If the tagLength member of normalizedAlgorithm is one of 32, 64, 96, 104, 112, 120 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm+ // Otherwise: throw an OperationError.+ let valid_tag_lengths = [32, 64, 96, 104, 112, 120, 128];+ if normalized_algorithm+ .tag_length+ .is_some_and(|length| !valid_tag_lengths.contains(&length))+ {+ return Err(dom_error("OperationError", b"Invalid tag length"));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-gcm-operations-encrypt+ fn encrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, plaintext: &Bytes) -> WebIdlResult<Bytes> {+ let AesParameters::Gcm(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Gcm parameters")+ };++ // NB: Steps 1 to 3 are performed by validate_encrypt_parameters(), which leaves tagLength as the tagLength member+ // of normalizedAlgorithm if present, or 128 otherwise.+ self.validate_encrypt_parameters(cx.realm())?;+ let tag_length = usize::from(normalized_algorithm.tag_length.unwrap_or(128));++ // 4. If plaintext has a length greater than 2^39 - 256 bytes, then throw an OperationError.+ if (plaintext.as_slice().len() as u64) > (1u64 << 39) - 256 {+ return Err(dom_error("OperationError", b"Invalid plaintext length"));+ }++ // 5. Let additionalData be the contents of the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.+ let additional_data = normalized_algorithm+ .additional_data+ .as_ref()+ .map_or(&[][..], Bytes::as_slice);++ // 6. Let C and T be the outputs that result from performing the Authenticated Encryption Function described in Section 7.1 of [NIST-SP800-38D] using+ // AES as the block cipher,+ // the contents of the iv member of normalizedAlgorithm as the IV input parameter,+ // the contents of additionalData as the A input parameter,+ // tagLength as the t pre-requisite+ // and the contents of plaintext as the input plaintext.+ let mut ciphertext: Option<Bytes> = None;+ let mut tag: Option<Bytes> = None;+ // SAFETY: The inputs and output slots live for the call.+ let maybe_encrypted = unsafe {+ web_crypto_aes_gcm_encrypt(+ Span::new(key.byte_handle_bytes()),+ Span::new(plaintext.as_slice()),+ Span::new(normalized_algorithm.iv.as_slice()),+ Span::new(additional_data),+ tag_length / 8,+ core::ptr::from_mut(&mut ciphertext).cast(),+ core::ptr::from_mut(&mut tag).cast(),+ receive_bytes,+ )+ };+ if !maybe_encrypted {+ return Err(dom_error("OperationError", b"Encryption failed"));+ }++ let mut ciphertext = Rc::unwrap_or_clone(ciphertext.expect("LibCrypto returns ciphertext").0);+ let tag = tag.expect("LibCrypto returns an authentication tag");++ // 7. Let ciphertext be equal to C | T, where '|' denotes concatenation.+ if ciphertext.try_reserve_exact(tag.as_slice().len()).is_err() {+ return cx+ .vm()+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])+ .map_err(Into::into);+ }+ ciphertext.extend_from_slice(tag.as_slice());++ // 8. Return the result of creating an ArrayBuffer containing ciphertext.+ Ok(Bytes::new(ciphertext))+ }++ // https://w3c.github.io/webcrypto/#aes-gcm-operations-decrypt+ fn validate_decrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let AesParameters::Gcm(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Gcm parameters")+ };++ // NB: Our AES-GCM rejects an empty IV.+ if normalized_algorithm.iv.as_slice().is_empty() {+ return Err(dom_error("OperationError", b"IV must not be empty"));+ }++ // 1. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.+ // If the tagLength member of normalizedAlgorithm is one of 32, 64, 96, 104, 112, 120 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm+ // Otherwise: throw an OperationError.+ let valid_tag_lengths = [32, 64, 96, 104, 112, 120, 128];+ if normalized_algorithm+ .tag_length+ .is_some_and(|length| !valid_tag_lengths.contains(&length))+ {+ return Err(dom_error("OperationError", b"Invalid tag length"));+ }++ // 2. If the iv member of normalizedAlgorithm has a length greater than 2^64 - 1 bytes, then throw an OperationError.+ // NOTE: This is not possible++ // 3. If the additionalData member of normalizedAlgorithm is present and has a length greater than 2^64 - 1 bytes, then throw an OperationError.+ // NOTE: This is not possible++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-gcm-operations-decrypt+ fn decrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, ciphertext: &Bytes) -> WebIdlResult<Bytes> {+ let AesParameters::Gcm(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Gcm parameters")+ };++ // NB: Steps 1 to 3 are performed by validate_decrypt_parameters(), which leaves tagLength as the tagLength member+ // of normalizedAlgorithm if present, or 128 otherwise.+ self.validate_decrypt_parameters(cx.realm())?;+ let tag_length = usize::from(normalized_algorithm.tag_length.unwrap_or(128));++ // 4. If ciphertext has a length in bits less than tagLength, then throw an OperationError.+ if ciphertext.as_slice().len() < tag_length / 8 {+ return Err(dom_error("OperationError", b"Invalid ciphertext length"));+ }++ // 5. Let tag be the last tagLength bits of ciphertext.+ let tag_bytes = tag_length / 8;+ let split = ciphertext.as_slice().len() - tag_bytes;+ let copy_bytes = |bytes: &[u8]| -> WebIdlResult<Vec<u8>> {+ let mut copy = Vec::new();+ if copy.try_reserve_exact(bytes.len()).is_err() {+ return cx+ .vm()+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])+ .map_err(Into::into);+ }+ copy.extend_from_slice(bytes);+ Ok(copy)+ };+ let tag = copy_bytes(&ciphertext.as_slice()[split..])?;+ // 6. Let actualCiphertext be the result of removing the last tagLength bits from ciphertext.+ let actual_ciphertext = copy_bytes(&ciphertext.as_slice()[..split])?;++ // 7. Let additionalData be the contents of the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.+ let additional_data = normalized_algorithm+ .additional_data+ .as_ref()+ .map_or(&[][..], Bytes::as_slice);++ // 8. Perform the Authenticated Decryption Function described in Section 7.2 of [NIST-SP800-38D] using+ // AES as the block cipher,+ // the contents of the iv member of normalizedAlgorithm as the IV input parameter,+ // the contents of additionalData as the A input parameter,+ // tagLength as the t pre-requisite,+ // the contents of actualCiphertext as the input ciphertext, C+ // and the contents of tag as the authentication tag, T.+ // If the result of the algorithm is the indication of inauthenticity, "FAIL": throw an OperationError+ let mut plaintext: Option<Bytes> = None;+ // SAFETY: The inputs and output slot live for the call.+ let maybe_plaintext = unsafe {+ web_crypto_aes_gcm_decrypt(+ Span::new(key.byte_handle_bytes()),+ Span::new(&actual_ciphertext),+ Span::new(normalized_algorithm.iv.as_slice()),+ Span::new(additional_data),+ Span::new(&tag),+ core::ptr::from_mut(&mut plaintext).cast(),+ receive_bytes,+ )+ };++ if !maybe_plaintext {+ return Err(dom_error("OperationError", b"Decryption failed"));+ }++ // Otherwise: Let plaintext be the output P of the Authenticated Decryption Function.+ // 9. Return the result of creating an ArrayBuffer containing plaintext.+ Ok(plaintext.expect("LibCrypto returns plaintext"))+ }++ // https://w3c.github.io/webcrypto/#aes-gcm-operations-generate-key+ fn validate_generate_key_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ let AesParameters::KeyGen(length) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = length.length;+ if bits != 128 && bits != 192 && bits != 256 {+ return Err(Exception::Dom(+ "OperationError",+ libjs_rust::api::utf16_formatted("Cannot create AES-GCM key with unusual amount of {} bits", &[&bits]),+ ));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-gcm-operations-generate-key+ fn generate_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {+ // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::Encrypt+ && usage != KeyUsage::Decrypt+ && usage != KeyUsage::WrapKey+ && usage != KeyUsage::UnwrapKey+ {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ // NB: Step 2 is performed by validate_generate_key_parameters().+ self.validate_generate_key_parameters(cx.realm())?;+ let AesParameters::KeyGen(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = normalized_algorithm.length;++ // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.+ // 4. If the key generation step fails, then throw an OperationError.+ let key_buffer = generate_random_key(cx.vm(), u32::from(bits))?;++ // 5. Let key be a new CryptoKey object representing the generated AES key.+ let key = CryptoKey::from_byte_handle(cx.vm(), &key_buffer);++ // 9. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 6. Let algorithm be a new AesKeyAlgorithm.+ // 7. Set the name attribute of algorithm to "AES-GCM".+ // 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.+ // 10. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-GCM"),+ length: bits,+ });++ // 11. Set the [[extractable]] internal slot of key to be extractable.+ key.set_extractable(extractable);++ // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.+ key.set_usages(normalized_key_usages(key_usages));++ // 13. Return key.+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKey(key))+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct AesKw {+ parameters: AesParameters,+}++impl AesKw {+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, parameter_type: AesParameterType) -> WebIdlResult<Self> {+ Ok(Self {+ parameters: AesParameters::from_value(vm, object, parameter_type)?,+ })+ }+}++impl AlgorithmMethods for AesKw {+ // https://w3c.github.io/webcrypto/#aes-kw-operations-import-key+ #[allow(+ clippy::collapsible_if,+ reason = "the spec checks the JWK algorithm after matching the key length"+ )]+ fn import_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ format: KeyFormat,+ key_data: ImportKeyData,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<Ref<CryptoKey>> {+ // 1. If usages contains an entry which is not one of "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ let data;++ // 2. If format is "raw":+ if format == KeyFormat::Raw || format == KeyFormat::RawSecret {+ // 1. Let data be the octet string contained in keyData.+ let ImportKeyData::Bytes(bytes) = key_data else {+ unreachable!("raw key data contains bytes")+ };+ data = bytes;++ // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.+ let length_in_bits = data.as_slice().len() * 8;+ if length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256 {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted(+ "Invalid key length '{}' bits (must be either 128, 192, or 256 bits)",+ &[&length_in_bits],+ ),+ ));+ }+ }+ // 2. If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. -> If keyData is a JsonWebKey dictionary:+ // Let jwk equal keyData.+ // -> Otherwise:+ // Throw a DataError.+ let ImportKeyData::JsonWebKey(jwk) = key_data else {+ return Err(dom_error("DataError", b"keyData is not a JsonWebKey dictionary"));+ };++ // 2. If the kty field of jwk is not "oct", then throw a DataError.+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"oct")) {+ return Err(dom_error("DataError", b"Invalid key type"));+ }++ // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.+ // Specifically, those requirements are:+ // * the member "k" is used to represent a symmetric key (or another key whose value is a single octet sequence).+ // * An "alg" member SHOULD also be present to identify the algorithm intended to be used with the key,+ // unless the application uses another means or convention to determine the algorithm used.+ // NOTE: "k" is already checked in step 4.+ if jwk.alg.is_none() {+ return Err(dom_error("DataError", b"Missing 'alg' field"));+ }++ // 4. Let data be the octet string obtained by decoding the k field of jwk.+ data = parse_jwk_symmetric_key(cx.vm(), &jwk)?;++ // 5. -> If data has length 128 bits:+ // If the alg field of jwk is present, and is not "A128KW", then throw a DataError.+ // -> If data has length 192 bits:+ // If the alg field of jwk is present, and is not "A192KW", then throw a DataError.+ // -> If data has length 256 bits:+ // If the alg field of jwk is present, and is not "A256KW", then throw a DataError.+ // -> Otherwise:+ // throw a DataError.+ let data_bits = data.as_slice().len() * 8;+ let alg = &jwk.alg;+ if data_bits == 128 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A128KW"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm",+ ));+ }+ } else if data_bits == 192 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A192KW"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm",+ ));+ }+ } else if data_bits == 256 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A256KW"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm",+ ));+ }+ } else {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted("Invalid key size: {} bits", &[&data_bits]),+ ));+ }++ // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.+ if !key_usages.is_empty()+ && jwk+ .r#use+ .as_ref()+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"enc"))+ {+ return Err(dom_error("DataError", b"Invalid use field"));+ }++ // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK]+ // or does not contain all of the specified usages values, then throw a DataError.+ validate_jwk_key_ops(&jwk, key_usages)?;++ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.+ if jwk.ext == Some(false) && extractable {+ return Err(dom_error("DataError", b"Invalid ext field"));+ }+ }+ // 2. Otherwise:+ else {+ // 1. throw a NotSupportedError.+ return Err(dom_error(+ "NotSupportedError",+ b"Only raw and jwk formats are supported",+ ));+ }++ let data_bits = data.as_slice().len() * 8;++ // 3. Let key be a new CryptoKey object representing an AES key with value data.+ let key = CryptoKey::from_byte_handle(cx.vm(), data.as_slice());++ // 4. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 5. Let algorithm be a new AesKeyAlgorithm.+ // 6. Set the name attribute of algorithm to "AES-KW".+ // 7. Set the length attribute of algorithm to the length, in bits, of data.+ // 8. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-KW"),+ length: u16::try_from(data_bits).expect("AES key length was validated"),+ });++ // 9. Return key.+ Ok(key)+ }++ // https://w3c.github.io/webcrypto/#aes-kw-operations-export-key+ #[allow(+ clippy::field_reassign_with_default,+ reason = "the algorithm sets JWK members in separate steps"+ )]+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {+ // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.+ // Note: In our impl this is always accessible++ // 2. If format is "raw":+ // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat+ // For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an alias of "raw".+ let result = if format == KeyFormat::Raw || format == KeyFormat::RawSecret {+ // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.+ let data = key.byte_handle_bytes();++ // 2. Let result be the result of creating an ArrayBuffer containing data.+ ExportKeyResult::Bytes(Bytes::new(data.to_vec()))+ }+ // 2. If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. Let jwk be a new JsonWebKey dictionary.+ let mut jwk = JsonWebKey::default();++ // 2. Set the kty attribute of jwk to the string "oct".+ jwk.kty = Some(Utf16String::from_ascii(b"oct"));++ // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,+ // encoded according to Section 6.4 of JSON Web Algorithms [JWA].+ let key_bytes = key.byte_handle_bytes();+ let Ok(encoded) = base64_url_bytes_encode(key_bytes) else {+ return cx+ .vm()+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])+ .map_err(Into::into);+ };+ jwk.k = Some(encoded);++ // 4. -> If the length attribute of key is 128:+ // Set the alg attribute of jwk to the string "A128KW".+ // -> If the length attribute of key is 192:+ // Set the alg attribute of jwk to the string "A192KW".+ // -> If the length attribute of key is 256:+ // Set the alg attribute of jwk to the string "A256KW".+ let key_bits = key_bytes.len() * 8;+ if key_bits == 128 {+ jwk.alg = Some(Utf16String::from_ascii(b"A128KW"));+ } else if key_bits == 192 {+ jwk.alg = Some(Utf16String::from_ascii(b"A192KW"));+ } else if key_bits == 256 {+ jwk.alg = Some(Utf16String::from_ascii(b"A256KW"));+ }++ // 5. Set the key_ops attribute of jwk to the usages attribute of key.+ jwk.key_ops = Some(+ key.internal_usages()+ .iter()+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))+ .collect(),+ );++ // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.+ jwk.ext = Some(key.is_extractable());++ // 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].+ ExportKeyResult::JsonWebKey(jwk)+ }+ // 2. Otherwise:+ else {+ // 1. throw a NotSupportedError.+ return Err(dom_error("NotSupportedError", b"Cannot export to unsupported format"));+ };++ // 3. Return result.+ Ok(result)+ }++ // https://w3c.github.io/webcrypto/#aes-kw-operations-get-key-length+ fn get_key_length(&self, _: Gc<Realm>, _: &AlgorithmParams) -> WebIdlResult<Option<u32>> {+ // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw an OperationError.+ let AesParameters::DerivedKey(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses DerivedKey parameters")+ };+ let length = normalized_algorithm.length;+ if length != 128 && length != 192 && length != 256 {+ return Err(dom_error("OperationError", b"Invalid key length"));+ }++ // 2. Return the length member of normalizedDerivedKeyAlgorithm.+ Ok(Some(u32::from(length)))+ }++ // https://w3c.github.io/webcrypto/#aes-kw-operations-generate-key+ fn validate_generate_key_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ // 2. If the length property of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ let AesParameters::KeyGen(length) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = length.length;+ if bits != 128 && bits != 192 && bits != 256 {+ return Err(Exception::Dom(+ "OperationError",+ libjs_rust::api::utf16_formatted("Cannot create AES-KW key with unusual amount of {} bits", &[&bits]),+ ));+ }++ Ok(())+ }++ // https://w3c.github.io/webcrypto/#aes-kw-operations-generate-key+ fn generate_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {+ // 1. If usages contains an entry which is not one of "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ // NB: Step 2 is performed by validate_generate_key_parameters().+ self.validate_generate_key_parameters(cx.realm())?;+ let AesParameters::KeyGen(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = normalized_algorithm.length;++ // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.+ // 4. If the key generation step fails, then throw an OperationError.+ let key_buffer = generate_random_key(cx.vm(), u32::from(bits))?;++ // 5. Let key be a new CryptoKey object representing the generated AES key.+ let key = CryptoKey::from_byte_handle(cx.vm(), &key_buffer);++ // 9. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 6. Let algorithm be a new AesKeyAlgorithm.+ // 7. Set the name attribute of algorithm to "AES-KW".+ // 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.+ // 10. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-KW"),+ length: bits,+ });++ // 11. Set the [[extractable]] internal slot of key to be extractable.+ key.set_extractable(extractable);++ // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.+ key.set_usages(normalized_key_usages(key_usages));++ // 13. Return key.+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKey(key))+ }++ // https://w3c.github.io/webcrypto/#aes-kw-operations-wrap-key+ fn wrap_key(&self, _: Gc<Realm>, key: Ref<CryptoKey>, plaintext: &Bytes) -> WebIdlResult<Bytes> {+ // 1. If plaintext is not a multiple of 64 bits in length, then throw an OperationError.+ if !plaintext.as_slice().len().is_multiple_of(8) {+ return Err(dom_error("OperationError", b"Invalid plaintext length"));+ }++ // 2. Let ciphertext be the result of performing the Key Wrap operation described in Section 2.2.1 of [RFC3394]+ // with plaintext as the plaintext to be wrapped and using the default Initial Value defined in Section 2.2.3.1 of the same document.+ let mut ciphertext: Option<Bytes> = None;+ // SAFETY: The input and output slots live for the call.+ let maybe_ciphertext = unsafe {+ web_crypto_aes_kw_cipher(+ true,+ Span::new(key.byte_handle_bytes()),+ Span::new(plaintext.as_slice()),+ core::ptr::from_mut(&mut ciphertext).cast(),+ receive_bytes,+ )+ };+ if !maybe_ciphertext {+ return Err(dom_error("OperationError", b"Key wrap failed"));+ }++ // 3. Return ciphertext.+ Ok(ciphertext.expect("LibCrypto returns the result"))+ }++ // https://w3c.github.io/webcrypto/#aes-kw-operations-unwrap-key+ fn unwrap_key(&self, _: Gc<Realm>, key: Ref<CryptoKey>, ciphertext: &Bytes) -> WebIdlResult<Bytes> {+ // NOTE: The spec does not mention this, but we need to check+ if ciphertext.as_slice().len() < 8 {+ return Err(dom_error("OperationError", b"Invalid ciphertext length"));+ }++ // 1. Let plaintext be the result of performing the Key Unwrap operation described in Section 2.2.2 of [RFC3394]+ // with ciphertext as the input ciphertext and using the default Initial Value defined in Section 2.2.3.1 of the same document+ // 2. If the Key Unwrap operation returns an error, then throw an OperationError.+ let mut plaintext: Option<Bytes> = None;+ // SAFETY: The input and output slots live for the call.+ let maybe_plaintext = unsafe {+ web_crypto_aes_kw_cipher(+ false,+ Span::new(key.byte_handle_bytes()),+ Span::new(ciphertext.as_slice()),+ core::ptr::from_mut(&mut plaintext).cast(),+ receive_bytes,+ )+ };+ if !maybe_plaintext {+ return Err(dom_error("OperationError", b"Key unwrap failed"));+ }++ // 3. Return plaintext.+ Ok(plaintext.expect("LibCrypto returns the result"))+ }+}++#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct AesOcb {+ parameters: AesParameters,+}++impl AesOcb {+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, parameter_type: AesParameterType) -> WebIdlResult<Self> {+ Ok(Self {+ parameters: AesParameters::from_value(vm, object, parameter_type)?,+ })+ }+}++impl AlgorithmMethods for AesOcb {+ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-encrypt+ fn validate_encrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let AesParameters::Aead(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Aead parameters")+ };++ // 1. If the iv member of normalizedAlgorithm has a length greater than 15 bytes, then throw an OperationError.+ if normalized_algorithm.iv.as_slice().len() > 15 {+ return Err(dom_error("OperationError", b"IV must have a length of 15 bytes"));+ }++ // NB: Our AES-OCB rejects an empty IV.+ if normalized_algorithm.iv.as_slice().is_empty() {+ return Err(dom_error("OperationError", b"IV must not be empty"));+ }++ // 2. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.+ // If the tagLength member of normalizedAlgorithm is one of 64, 96 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm+ // Otherwise: throw an OperationError.+ let valid_tag_lengths = [64, 96, 128];+ if normalized_algorithm+ .tag_length+ .is_some_and(|length| !valid_tag_lengths.contains(&length))+ {+ return Err(dom_error("OperationError", b"Invalid tag length"));+ }++ Ok(())+ }++ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-encrypt+ fn encrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, plaintext: &Bytes) -> WebIdlResult<Bytes> {+ let AesParameters::Aead(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Aead parameters")+ };++ // NB: Steps 1 and 2 are performed by validate_encrypt_parameters(), which leaves tagLength as the tagLength member+ // of normalizedAlgorithm if present, or 128 otherwise.+ self.validate_encrypt_parameters(cx.realm())?;+ let tag_length = usize::from(normalized_algorithm.tag_length.unwrap_or(128));++ // 3. Let additionalData be the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.+ let additional_data = normalized_algorithm+ .additional_data+ .as_ref()+ .map_or(&[][..], Bytes::as_slice);++ // 4. Let C be the output that results from performing the OCB-ENCRYPT function described in Section 4.2+ // of [RFC7253] using AES as the block cipher,+ // using the key represented by [[handle]] internal slot of key as the K input parameter,+ // the iv member of normalizedAlgorithm as the N input parameter,+ // additionalData as the A input parameter,+ // plaintext as the P input parameter,+ // and tagLength as the TAGLEN global parameter.+ let mut ciphertext: Option<Bytes> = None;+ // SAFETY: The inputs and output slot live for the call.+ let maybe_ciphertext = unsafe {+ web_crypto_aes_ocb_cipher(+ true,+ Span::new(key.byte_handle_bytes()),+ Span::new(plaintext.as_slice()),+ Span::new(normalized_algorithm.iv.as_slice()),+ Span::new(additional_data),+ tag_length,+ core::ptr::from_mut(&mut ciphertext).cast(),+ receive_bytes,+ )+ };++ if maybe_ciphertext != 0 {+ // SAFETY: LibCrypto transfers an ownership reference.+ let detail = unsafe { Utf16String::from_raw_owned(maybe_ciphertext) };+ return Err(Exception::Dom(+ "OperationError",+ libjs_rust::api::utf16_formatted("Encryption failed: {}", &[&detail]),+ ));+ }++ // 5. Return C+ Ok(ciphertext.expect("LibCrypto returns the result"))+ }++ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-decrypt+ fn validate_decrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let AesParameters::Aead(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Aead parameters")+ };++ // 1. If the iv member of normalizedAlgorithm has a length greater than 15 bytes, then throw an OperationError.+ if normalized_algorithm.iv.as_slice().len() > 15 {+ return Err(dom_error("OperationError", b"IV must have a length of 15 bytes"));+ }++ // NB: Our AES-OCB rejects an empty IV.+ if normalized_algorithm.iv.as_slice().is_empty() {+ return Err(dom_error("OperationError", b"IV must not be empty"));+ }++ // 2. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.+ // If the tagLength member of normalizedAlgorithm is one of 64, 96 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm+ // Otherwise: throw an OperationError.+ let valid_tag_lengths = [64, 96, 128];+ if normalized_algorithm+ .tag_length+ .is_some_and(|length| !valid_tag_lengths.contains(&length))+ {+ return Err(dom_error("OperationError", b"Invalid tag length"));+ }++ Ok(())+ }++ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-decrypt+ fn decrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, ciphertext: &Bytes) -> WebIdlResult<Bytes> {+ let AesParameters::Aead(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses Aead parameters")+ };++ // NB: Steps 1 and 2 are performed by validate_decrypt_parameters(), which leaves tagLength as the tagLength member+ // of normalizedAlgorithm if present, or 128 otherwise.+ self.validate_decrypt_parameters(cx.realm())?;+ let tag_length = usize::from(normalized_algorithm.tag_length.unwrap_or(128));++ // 3. If ciphertext has a length less than tagLength bits, then throw an OperationError.+ if ciphertext.as_slice().len() < tag_length / 8 {+ return Err(dom_error("OperationError", b"Ciphertext is less than tagLength bits"));+ }++ // 4. Let additionalData be the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.+ let additional_data = normalized_algorithm+ .additional_data+ .as_ref()+ .map_or(&[][..], Bytes::as_slice);++ // 5. Perform the OCB-DECRYPT function described in Section 4.3 of [RFC7253]+ // using AES as the block cipher,+ // using the key represented by [[handle]] internal slot of key as the K input parameter,+ // the iv member of normalizedAlgorithm as the N input parameter,+ // additionalData as the A input parameter,+ // ciphertext as the C input parameter,+ // and tagLength as the TAGLEN global parameter.+ let mut plaintext: Option<Bytes> = None;+ // SAFETY: The inputs and output slot live for the call.+ let maybe_plaintext = unsafe {+ web_crypto_aes_ocb_cipher(+ false,+ Span::new(key.byte_handle_bytes()),+ Span::new(ciphertext.as_slice()),+ Span::new(normalized_algorithm.iv.as_slice()),+ Span::new(additional_data),+ tag_length,+ core::ptr::from_mut(&mut plaintext).cast(),+ receive_bytes,+ )+ };++ // 5. If the result of the algorithm is the indication of authentication failure:+ // throw an OperationError+ // Otherwise:+ // Let plaintext be the resulting plaintext.+ if maybe_plaintext != 0 {+ // SAFETY: LibCrypto transfers an ownership reference.+ let detail = unsafe { Utf16String::from_raw_owned(maybe_plaintext) };+ return Err(Exception::Dom(+ "OperationError",+ libjs_rust::api::utf16_formatted("Decryption failed: {}", &[&detail]),+ ));+ }++ // 6. Return plaintext.+ Ok(plaintext.expect("LibCrypto returns the result"))+ }++ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-generate-key+ fn validate_generate_key_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ let AesParameters::KeyGen(length) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = length.length;+ if bits != 128 && bits != 192 && bits != 256 {+ return Err(Exception::Dom(+ "OperationError",+ libjs_rust::api::utf16_formatted("Cannot create AES-OCB key with unusual amount of {} bits", &[&bits]),+ ));+ }++ Ok(())+ }++ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-generate-key+ fn generate_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ extractable: bool,+ key_usages: &[KeyUsage],+ ) -> WebIdlResult<CryptoKeyOrCryptoKeyPair> {+ // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in key_usages {+ if usage != KeyUsage::Encrypt+ && usage != KeyUsage::Decrypt+ && usage != KeyUsage::WrapKey+ && usage != KeyUsage::UnwrapKey+ {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ // NB: Step 2 is performed by validate_generate_key_parameters().+ self.validate_generate_key_parameters(cx.realm())?;+ let AesParameters::KeyGen(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses KeyGen parameters")+ };+ let bits = normalized_algorithm.length;++ // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.+ // 4. If the key generation step fails, then throw an OperationError.+ let key_buffer = generate_random_key(cx.vm(), u32::from(bits))?;++ // 5. Let key be a new CryptoKey object representing the generated AES key.+ let key = CryptoKey::from_byte_handle(cx.vm(), &key_buffer);++ // 6. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 7. Let algorithm be a new AesKeyAlgorithm.+ // 8. Set the name attribute of algorithm to "AES-OCB".+ // 9. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.+ // 10. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-OCB"),+ length: bits,+ });++ // 11. Set the [[extractable]] internal slot of key to be extractable.+ key.set_extractable(extractable);++ // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.+ key.set_usages(normalized_key_usages(key_usages));++ // 13. Return key.+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKey(key))+ }++ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-import-key+ #[allow(+ clippy::collapsible_if,+ reason = "the spec checks the JWK algorithm after matching the key length"+ )]+ fn import_key(+ &self,+ cx: &CallContext,+ _: &AlgorithmParams,+ format: KeyFormat,+ key_data: ImportKeyData,+ extractable: bool,+ usages: &[KeyUsage],+ ) -> WebIdlResult<Ref<CryptoKey>> {+ // 1. Let keyData be the key data to be imported.++ // 2. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.+ for &usage in usages {+ if usage != KeyUsage::Encrypt+ && usage != KeyUsage::Decrypt+ && usage != KeyUsage::WrapKey+ && usage != KeyUsage::UnwrapKey+ {+ return Err(Exception::Dom(+ "SyntaxError",+ libjs_rust::api::utf16_formatted("Invalid key usage '{}'", &[&key_usage_to_string(usage)]),+ ));+ }+ }++ let data;++ // 3. If format is "raw-secret":+ if format == KeyFormat::RawSecret {+ // 1. Let data be keyData.+ let ImportKeyData::Bytes(bytes) = key_data else {+ unreachable!("raw key data contains bytes")+ };+ data = bytes;++ // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.+ let length_in_bits = data.as_slice().len() * 8;+ if length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256 {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted(+ "Invalid key length '{}' bits (must be either 128, 192, or 256 bits)",+ &[&length_in_bits],+ ),+ ));+ }+ }+ // 3. If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. If keyData is a JsonWebKey dictionary:+ // Let jwk equal keyData.+ // Otherwise:+ // Throw a DataError.+ let ImportKeyData::JsonWebKey(jwk) = key_data else {+ return Err(dom_error("DataError", b"Invalid JWK key data"));+ };++ // 2. If the kty field of jwk is not "oct", then throw a DataError.+ if jwk.kty.as_ref() != Some(&Utf16String::from_ascii(b"oct")) {+ return Err(dom_error("DataError", b"Invalid key type"));+ }++ // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.+ // NOTE: "k" is already checked in step 4.+ if jwk.alg.is_none() {+ return Err(dom_error("DataError", b"Missing 'alg' field"));+ }++ // 4. Let data be the byte sequence obtained by decoding the k field of jwk.+ data = parse_jwk_symmetric_key(cx.vm(), &jwk)?;++ // 5. -> If data has length 128 bits:+ // If the alg field of jwk is present, and is not "A128OCB", then throw a DataError.+ // -> If data has length 192 bits:+ // If the alg field of jwk is present, and is not "A192OCB", then throw a DataError.+ // -> If data has length 256 bits:+ // If the alg field of jwk is present, and is not "A256OCB", then throw a DataError.+ // -> Otherwise:+ // throw a DataError.+ let data_bits = data.as_slice().len() * 8;+ let alg = &jwk.alg;+ if data_bits == 128 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A128OCB"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm",+ ));+ }+ } else if data_bits == 192 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A192OCB"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm",+ ));+ }+ } else if data_bits == 256 {+ if alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"A256OCB"))+ {+ return Err(dom_error(+ "DataError",+ b"Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm",+ ));+ }+ } else {+ return Err(Exception::Dom(+ "DataError",+ libjs_rust::api::utf16_formatted("Invalid key size: {} bits", &[&data_bits]),+ ));+ }++ // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.+ if !usages.is_empty()+ && jwk+ .r#use+ .as_ref()+ .is_some_and(|key_use| *key_use != Utf16String::from_ascii(b"enc"))+ {+ return Err(dom_error("DataError", b"Invalid use field"));+ }++ // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK]+ // or does not contain all of the specified usages values, then throw a DataError.+ validate_jwk_key_ops(&jwk, usages)?;++ // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.+ if jwk.ext == Some(false) && extractable {+ return Err(dom_error("DataError", b"Invalid ext field"));+ }+ }+ // 2. Otherwise:+ else {+ // 1. throw a NotSupportedError.+ return Err(dom_error(+ "NotSupportedError",+ b"Only raw-secret and jwk formats are supported",+ ));+ }++ let data_bits = data.as_slice().len() * 8;++ // 4. Let key be a new CryptoKey object representing an AES key with value data.+ let key = CryptoKey::from_byte_handle(cx.vm(), data.as_slice());++ // 5. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 6. Let algorithm be a new AesKeyAlgorithm.+ // 7. Set the name attribute of algorithm to "AES-OCB".+ // 8. Set the length attribute of algorithm to the length, in bits, of data.+ // 9. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Aes {+ name: Utf16String::from_ascii(b"AES-OCB"),+ length: u16::try_from(data_bits).expect("AES key length was validated"),+ });++ // 10. Return key.+ Ok(key)+ }++ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-export-key+ #[allow(+ clippy::field_reassign_with_default,+ reason = "the algorithm sets JWK members in separate steps"+ )]+ fn export_key(&self, cx: &CallContext, format: KeyFormat, key: Ref<CryptoKey>) -> WebIdlResult<ExportKeyResult> {+ // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.+ // Note: In our impl this is always accessible++ // 2. If format is "raw-secret":+ let result = if format == KeyFormat::RawSecret {+ // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.+ let data = key.byte_handle_bytes();++ // 2. Let result be data.+ ExportKeyResult::Bytes(Bytes::new(data.to_vec()))+ }+ // 2. If format is "jwk":+ else if format == KeyFormat::Jwk {+ // 1. Let jwk be a new JsonWebKey dictionary.+ let mut jwk = JsonWebKey::default();++ // 2. Set the kty attribute of jwk to the string "oct".+ jwk.kty = Some(Utf16String::from_ascii(b"oct"));++ // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,+ // encoded according to Section 6.4 of JSON Web Algorithms [JWA].+ let key_bytes = key.byte_handle_bytes();+ let Ok(encoded) = base64_url_bytes_encode(key_bytes) else {+ return cx+ .vm()+ .throw_completion(ErrorKind::InternalError, ErrorType::OutOfMemory, &[])+ .map_err(Into::into);+ };+ jwk.k = Some(encoded);++ // 4. -> If the length attribute of key is 128:+ // Set the alg attribute of jwk to the string "A128OCB".+ // -> If the length attribute of key is 192:+ // Set the alg attribute of jwk to the string "A192OCB".+ // -> If the length attribute of key is 256:+ // Set the alg attribute of jwk to the string "A256OCB".+ let key_bits = key_bytes.len() * 8;+ if key_bits == 128 {+ jwk.alg = Some(Utf16String::from_ascii(b"A128OCB"));+ } else if key_bits == 192 {+ jwk.alg = Some(Utf16String::from_ascii(b"A192OCB"));+ } else if key_bits == 256 {+ jwk.alg = Some(Utf16String::from_ascii(b"A256OCB"));+ }++ // 5. Set the key_ops attribute of jwk to the usages attribute of key.+ jwk.key_ops = Some(+ key.internal_usages()+ .iter()+ .map(|usage| Utf16String::from_ascii(KEY_USAGES[*usage as usize]))+ .collect(),+ );++ // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.+ jwk.ext = Some(key.is_extractable());++ // 7. Let result be jwk.+ return Ok(ExportKeyResult::JsonWebKey(jwk));+ }+ // 2. Otherwise:+ else {+ // 1. throw a NotSupportedError.+ return Err(dom_error("NotSupportedError", b"Cannot export to unsupported format"));+ };++ // 3. Return result.+ Ok(result)+ }++ // https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-get-key-length+ fn get_key_length(&self, _: Gc<Realm>, _: &AlgorithmParams) -> WebIdlResult<Option<u32>> {+ let AesParameters::DerivedKey(normalized_algorithm) = &self.parameters else {+ unreachable!("the operation uses DerivedKey parameters")+ };++ // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw an OperationError.+ let length = normalized_algorithm.length;+ if length != 128 && length != 192 && length != 256 {+ return Err(Exception::Dom(+ "OperationError",+ libjs_rust::api::utf16_formatted("Invalid key length: {}", &[&length]),+ ));+ }++ // 2. Return the length member of normalizedDerivedKeyAlgorithm.+ Ok(Some(u32::from(length)))+ }+}+unsafe extern "C" fn receive_bytes(state: *mut c_void, bytes: Span<'_, u8>) {// SAFETY: The state is the caller's slot, and the service lends the bytes.unsafe { *state.cast::<Option<Bytes>>() = Some(Bytes::new(bytes.as_slice().to_vec())) };diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rsindex a65f3cd1f07..3d9c723ed53 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs@@ -31,10 +31,10 @@ use crate::bindings::promise::{};use crate::bindings::{CallContext, Exception, MustNotThrow, WebIdlResult};use crate::crypto::crypto_algorithms::{- AlgorithmId, AlgorithmMethods, AlgorithmParams, Argon2, Argon2Params, Bytes, CShakeParams, CppAlgorithm,- EncapsulatedKeyData, ExportKeyResult, Hkdf, HkdfParams, Hmac, HmacImportParams, HmacKeyGenParams, ImportKeyData,- Kmac, KmacImportParams, KmacKeyGenParams, KmacParams, ParameterId, Pbkdf2, Pbkdf2Params, Sha,- normalized_key_usages, public_key_for_private_key,+ AesCbc, AesCtr, AesGcm, AesKw, AesOcb, AesParameterType, AlgorithmId, AlgorithmMethods, AlgorithmParams, Argon2,+ Argon2Params, Bytes, CShakeParams, CppAlgorithm, EncapsulatedKeyData, ExportKeyResult, Hkdf, HkdfParams, Hmac,+ HmacImportParams, HmacKeyGenParams, ImportKeyData, Kmac, KmacImportParams, KmacKeyGenParams, KmacParams,+ ParameterId, Pbkdf2, Pbkdf2Params, Sha, normalized_key_usages, public_key_for_private_key,};#[allow(unused_imports,@@ -102,6 +102,11 @@ enum RegisteredAlgorithm {Pbkdf2Params,Argon2,Argon2Params,+ AesCbc(AesParameterType),+ AesCtr(AesParameterType),+ AesGcm(AesParameterType),+ AesKw(AesParameterType),+ AesOcb(AesParameterType),CShake,}@@ -126,6 +131,11 @@ enum AlgorithmImplementation {Pbkdf2Params(Pbkdf2Params),Argon2(Argon2),Argon2Params(Argon2Params),+ AesCbc(AesCbc),+ AesCtr(AesCtr),+ AesGcm(AesGcm),+ AesKw(AesKw),+ AesOcb(AesOcb),CShake(CShakeParams),}@@ -149,6 +159,11 @@ impl Deref for AlgorithmImplementation {Self::Pbkdf2Params(algorithm) => algorithm,Self::Argon2(algorithm) => algorithm,Self::Argon2Params(algorithm) => algorithm,+ Self::AesCbc(algorithm) => algorithm,+ Self::AesCtr(algorithm) => algorithm,+ Self::AesGcm(algorithm) => algorithm,+ Self::AesKw(algorithm) => algorithm,+ Self::AesOcb(algorithm) => algorithm,Self::CShake(algorithm) => algorithm,}}@@ -360,6 +375,21 @@ pub(crate) fn normalize_an_algorithm(RegisteredAlgorithm::Argon2Params => {AlgorithmImplementation::Argon2Params(Argon2Params::from_value(vm, *algorithm)?)}+ RegisteredAlgorithm::AesCbc(parameter_type) => {+ AlgorithmImplementation::AesCbc(AesCbc::from_value(vm, *algorithm, parameter_type)?)+ }+ RegisteredAlgorithm::AesCtr(parameter_type) => {+ AlgorithmImplementation::AesCtr(AesCtr::from_value(vm, *algorithm, parameter_type)?)+ }+ RegisteredAlgorithm::AesGcm(parameter_type) => {+ AlgorithmImplementation::AesGcm(AesGcm::from_value(vm, *algorithm, parameter_type)?)+ }+ RegisteredAlgorithm::AesKw(parameter_type) => {+ AlgorithmImplementation::AesKw(AesKw::from_value(vm, *algorithm, parameter_type)?)+ }+ RegisteredAlgorithm::AesOcb(parameter_type) => {+ AlgorithmImplementation::AesOcb(AesOcb::from_value(vm, *algorithm, parameter_type)?)+ }RegisteredAlgorithm::CShake => AlgorithmImplementation::CShake(CShakeParams::from_value(vm, *algorithm)?),};@@ -447,7 +477,9 @@ impl SubtleCrypto {}// 11. Let ciphertext be the result of performing the encrypt operation specified by normalizedAlgorithm using algorithm and key and with data as plaintext.- let cipher_text = normalized_algorithm.methods.encrypt(realm, key, &data);+ let cipher_text = normalized_algorithm+ .methods+ .encrypt(&CallContext::new(vm, realm), key, &data);if let Err(error) = cipher_text {throw_in_this_context(error);return;@@ -542,7 +574,9 @@ impl SubtleCrypto {}// 11. Let plaintext be the result of performing the decrypt operation specified by normalizedAlgorithm using key and algorithm and with data as ciphertext.- let plain_text = normalized_algorithm.methods.decrypt(realm, key, &data);+ let plain_text = normalized_algorithm+ .methods+ .decrypt(&CallContext::new(vm, realm), key, &data);if let Err(error) = plain_text {throw_in_this_context(error);return;@@ -1570,7 +1604,10 @@ impl SubtleCrypto {else if operation == "encrypt" {// Let result be the result of performing the encrypt operation specified by normalizedAlgorithm// using algorithm, wrappingKey as key and bytes as plaintext.- let result_or_error = normalized_algorithm.methods.encrypt(realm, wrapping_key, &bytes);+ let result_or_error =+ normalized_algorithm+ .methods+ .encrypt(&CallContext::new(vm, realm), wrapping_key, &bytes);if let Err(error) = result_or_error {reject_promise_with_exception(vm, promise, error);return;@@ -1729,9 +1766,11 @@ impl SubtleCrypto {else if operation == "decrypt" {// Let bytes be the result of performing the decrypt operation specified by normalizedAlgorithm// using algorithm, unwrappingKey as key and wrappedKey as ciphertext.- normalized_algorithm- .methods- .decrypt(realm, unwrapping_key, &real_wrapped_key)+ normalized_algorithm.methods.decrypt(+ &CallContext::new(vm, realm),+ unwrapping_key,+ &real_wrapped_key,+ )}// Otherwise:else {@@ -2981,37 +3020,37 @@ fn supported_algorithms() -> &'static SupportedAlgorithmsMap {&mut internal_object,"encrypt","AES-CTR",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCtr, ParameterId::AesCtrParams),+ RegisteredAlgorithm::AesCtr(AesParameterType::Ctr),);define_an_algorithm(&mut internal_object,"decrypt","AES-CTR",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCtr, ParameterId::AesCtrParams),+ RegisteredAlgorithm::AesCtr(AesParameterType::Ctr),);define_an_algorithm(&mut internal_object,"generateKey","AES-CTR",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCtr, ParameterId::AesKeyGenParams),+ RegisteredAlgorithm::AesCtr(AesParameterType::KeyGen),);define_an_algorithm(&mut internal_object,"importKey","AES-CTR",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCtr, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesCtr(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"exportKey","AES-CTR",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCtr, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesCtr(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"get key length","AES-CTR",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCtr, ParameterId::AesDerivedKeyParams),+ RegisteredAlgorithm::AesCtr(AesParameterType::DerivedKey),);// https://w3c.github.io/webcrypto/#aes-cbc-registration@@ -3019,37 +3058,37 @@ fn supported_algorithms() -> &'static SupportedAlgorithmsMap {&mut internal_object,"encrypt","AES-CBC",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCbc, ParameterId::AesCbcParams),+ RegisteredAlgorithm::AesCbc(AesParameterType::Cbc),);define_an_algorithm(&mut internal_object,"decrypt","AES-CBC",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCbc, ParameterId::AesCbcParams),+ RegisteredAlgorithm::AesCbc(AesParameterType::Cbc),);define_an_algorithm(&mut internal_object,"generateKey","AES-CBC",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCbc, ParameterId::AesKeyGenParams),+ RegisteredAlgorithm::AesCbc(AesParameterType::KeyGen),);define_an_algorithm(&mut internal_object,"importKey","AES-CBC",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCbc, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesCbc(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"exportKey","AES-CBC",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCbc, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesCbc(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"get key length","AES-CBC",- RegisteredAlgorithm::Cpp(AlgorithmId::AesCbc, ParameterId::AesDerivedKeyParams),+ RegisteredAlgorithm::AesCbc(AesParameterType::DerivedKey),);// https://w3c.github.io/webcrypto/#aes-gcm-registration@@ -3057,37 +3096,37 @@ fn supported_algorithms() -> &'static SupportedAlgorithmsMap {&mut internal_object,"encrypt","AES-GCM",- RegisteredAlgorithm::Cpp(AlgorithmId::AesGcm, ParameterId::AesGcmParams),+ RegisteredAlgorithm::AesGcm(AesParameterType::Gcm),);define_an_algorithm(&mut internal_object,"decrypt","AES-GCM",- RegisteredAlgorithm::Cpp(AlgorithmId::AesGcm, ParameterId::AesGcmParams),+ RegisteredAlgorithm::AesGcm(AesParameterType::Gcm),);define_an_algorithm(&mut internal_object,"generateKey","AES-GCM",- RegisteredAlgorithm::Cpp(AlgorithmId::AesGcm, ParameterId::AesKeyGenParams),+ RegisteredAlgorithm::AesGcm(AesParameterType::KeyGen),);define_an_algorithm(&mut internal_object,"importKey","AES-GCM",- RegisteredAlgorithm::Cpp(AlgorithmId::AesGcm, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesGcm(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"exportKey","AES-GCM",- RegisteredAlgorithm::Cpp(AlgorithmId::AesGcm, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesGcm(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"get key length","AES-GCM",- RegisteredAlgorithm::Cpp(AlgorithmId::AesGcm, ParameterId::AesDerivedKeyParams),+ RegisteredAlgorithm::AesGcm(AesParameterType::DerivedKey),);// https://w3c.github.io/webcrypto/#aes-kw-registration@@ -3095,37 +3134,37 @@ fn supported_algorithms() -> &'static SupportedAlgorithmsMap {&mut internal_object,"wrapKey","AES-KW",- RegisteredAlgorithm::Cpp(AlgorithmId::AesKw, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesKw(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"unwrapKey","AES-KW",- RegisteredAlgorithm::Cpp(AlgorithmId::AesKw, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesKw(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"generateKey","AES-KW",- RegisteredAlgorithm::Cpp(AlgorithmId::AesKw, ParameterId::AesKeyGenParams),+ RegisteredAlgorithm::AesKw(AesParameterType::KeyGen),);define_an_algorithm(&mut internal_object,"importKey","AES-KW",- RegisteredAlgorithm::Cpp(AlgorithmId::AesKw, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesKw(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"exportKey","AES-KW",- RegisteredAlgorithm::Cpp(AlgorithmId::AesKw, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesKw(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"get key length","AES-KW",- RegisteredAlgorithm::Cpp(AlgorithmId::AesKw, ParameterId::AesDerivedKeyParams),+ RegisteredAlgorithm::AesKw(AesParameterType::DerivedKey),);// https://w3c.github.io/webcrypto/#hmac-registration@@ -3451,37 +3490,37 @@ fn supported_algorithms() -> &'static SupportedAlgorithmsMap {&mut internal_object,"encrypt","AES-OCB",- RegisteredAlgorithm::Cpp(AlgorithmId::AesOcb, ParameterId::AeadParams),+ RegisteredAlgorithm::AesOcb(AesParameterType::Aead),);define_an_algorithm(&mut internal_object,"decrypt","AES-OCB",- RegisteredAlgorithm::Cpp(AlgorithmId::AesOcb, ParameterId::AeadParams),+ RegisteredAlgorithm::AesOcb(AesParameterType::Aead),);define_an_algorithm(&mut internal_object,"generateKey","AES-OCB",- RegisteredAlgorithm::Cpp(AlgorithmId::AesOcb, ParameterId::AesKeyGenParams),+ RegisteredAlgorithm::AesOcb(AesParameterType::KeyGen),);define_an_algorithm(&mut internal_object,"importKey","AES-OCB",- RegisteredAlgorithm::Cpp(AlgorithmId::AesOcb, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesOcb(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"exportKey","AES-OCB",- RegisteredAlgorithm::Cpp(AlgorithmId::AesOcb, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::AesOcb(AesParameterType::Base),);define_an_algorithm(&mut internal_object,"get key length","AES-OCB",- RegisteredAlgorithm::Cpp(AlgorithmId::AesOcb, ParameterId::AesDerivedKeyParams),+ RegisteredAlgorithm::AesOcb(AesParameterType::DerivedKey),);// https://wicg.github.io/webcrypto-modern-algos/#kmac-registrationdiff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cppindex 941fbc1ab19..82786740fc8 100644--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp@@ -453,68 +453,6 @@ static WebIDL::ExceptionOr<ByteBuffer> generate_random_key(JS::VM& vm, u32 constAlgorithmParams::~AlgorithmParams() = default;-AesCbcParams::~AesCbcParams() = default;--JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AesCbcParams::from_value(JS::Realm& realm, JS::Value value)-{- auto& vm = realm.vm();- auto& object = value.as_object();-- auto iv_value = TRY(object.get("iv"_utf16_fly_string));- if (!iv_value.is_object() || !(is<JS::TypedArrayBase>(iv_value.as_object()) || is<JS::ArrayBuffer>(iv_value.as_object()) || is<JS::DataView>(iv_value.as_object())))- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "BufferSource");- auto iv = TRY_OR_THROW_OOM(vm, WebIDL::get_buffer_source_copy(iv_value.as_object()));-- return adopt_own<AlgorithmParams>(*new AesCbcParams { iv });-}--AesCtrParams::~AesCtrParams() = default;--JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AesCtrParams::from_value(JS::Realm& realm, JS::Value value)-{- auto& vm = realm.vm();- auto& object = value.as_object();-- auto iv_value = TRY(object.get("counter"_utf16_fly_string));- if (!iv_value.is_object() || !(is<JS::TypedArrayBase>(iv_value.as_object()) || is<JS::ArrayBuffer>(iv_value.as_object()) || is<JS::DataView>(iv_value.as_object())))- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "BufferSource");- auto iv = TRY_OR_THROW_OOM(vm, WebIDL::get_buffer_source_copy(iv_value.as_object()));-- auto length_value = TRY(object.get("length"_utf16_fly_string));- auto length = TRY(WebIDL::convert_to_int<WebIDL::Octet>(vm, length_value, WebIDL::EnforceRange::Yes, WebIDL::Clamp::No));-- return adopt_own<AlgorithmParams>(*new AesCtrParams { iv, length });-}--AesGcmParams::~AesGcmParams() = default;--JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AesGcmParams::from_value(JS::Realm& realm, JS::Value value)-{- auto& vm = realm.vm();- auto& object = value.as_object();-- auto iv_value = TRY(object.get("iv"_utf16_fly_string));- if (!iv_value.is_object() || !(is<JS::TypedArrayBase>(iv_value.as_object()) || is<JS::ArrayBuffer>(iv_value.as_object()) || is<JS::DataView>(iv_value.as_object())))- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "BufferSource");- auto iv = TRY_OR_THROW_OOM(vm, WebIDL::get_buffer_source_copy(iv_value.as_object()));-- auto maybe_additional_data = Optional<ByteBuffer> {};- if (TRY(object.has_property("additionalData"_utf16_fly_string))) {- auto additional_data_value = TRY(object.get("additionalData"_utf16_fly_string));- if (!additional_data_value.is_object() || !(is<JS::TypedArrayBase>(additional_data_value.as_object()) || is<JS::ArrayBuffer>(additional_data_value.as_object()) || is<JS::DataView>(additional_data_value.as_object())))- return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "BufferSource");- maybe_additional_data = TRY_OR_THROW_OOM(vm, WebIDL::get_buffer_source_copy(additional_data_value.as_object()));- }-- auto maybe_tag_length = Optional<u8> {};- if (TRY(object.has_property("tagLength"_utf16_fly_string))) {- auto tag_length_value = TRY(object.get("tagLength"_utf16_fly_string));- maybe_tag_length = TRY(WebIDL::convert_to_int<WebIDL::Octet>(vm, tag_length_value, WebIDL::EnforceRange::Yes, WebIDL::Clamp::No));- }-- return adopt_own<AlgorithmParams>(*new AesGcmParams { iv, maybe_additional_data, maybe_tag_length });-}-RsaKeyGenParams::~RsaKeyGenParams() = default;JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> RsaKeyGenParams::from_value(JS::Realm& realm, JS::Value value)@@ -631,32 +569,6 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> EcKeyGenParams::from_valuereturn adopt_own<AlgorithmParams>(*new EcKeyGenParams { curve });}-AesKeyGenParams::~AesKeyGenParams() = default;--JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AesKeyGenParams::from_value(JS::Realm& realm, JS::Value value)-{- auto& vm = realm.vm();- auto& object = value.as_object();-- auto length_value = TRY(object.get("length"_utf16_fly_string));- auto length = TRY(WebIDL::convert_to_int<WebIDL::UnsignedShort>(vm, length_value, WebIDL::EnforceRange::Yes, WebIDL::Clamp::No));-- return adopt_own<AlgorithmParams>(*new AesKeyGenParams { length });-}--AesDerivedKeyParams::~AesDerivedKeyParams() = default;--JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AesDerivedKeyParams::from_value(JS::Realm& realm, JS::Value value)-{- auto& vm = realm.vm();- auto& object = value.as_object();-- auto length_value = TRY(object.get("length"_utf16_fly_string));- auto length = TRY(WebIDL::convert_to_int<WebIDL::UnsignedShort>(vm, length_value, WebIDL::EnforceRange::Yes, WebIDL::Clamp::No));-- return adopt_own<AlgorithmParams>(*new AesDerivedKeyParams { length });-}-EcdhKeyDeriveParams::~EcdhKeyDeriveParams() = default;JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> EcdhKeyDeriveParams::from_value(JS::Realm& realm, JS::Value value)@@ -2481,1314 +2393,81 @@ WebIDL::ExceptionOr<ExportKeyResult> RSASSAPKCS1::export_key(JS::Realm& realm, K}Optional<ReadonlySpan<KeyUsage>> RSASSAPKCS1::public_key_usages() const-{- static constexpr Array usages { KeyUsage::Verify };- return usages.span();-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-encrypt-WebIDL::ExceptionOr<void> AesCbc::validate_encrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AesCbcParams const&>(params);-- // 1. If the iv member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.- if (normalized_algorithm.iv.size() != 16)- return WebIDL::OperationError::create("IV to AES-CBC must be exactly 16 bytes"_utf16);-- return {};-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> AesCbc::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)-{- auto const& normalized_algorithm = static_cast<AesCbcParams const&>(params);-- // NB: Step 1 is performed by validate_encrypt_parameters().- TRY(validate_encrypt_parameters(params));-- // 2. Let paddedPlaintext be the result of adding padding octets to the contents of plaintext according to the procedure defined in Section 10.3 of [RFC2315], step 2, with a value of k of 16.- // 3. Let ciphertext be the result of performing the CBC Encryption operation described in Section 6.2 of [NIST-SP800-38A] using AES as the block cipher, the contents of the iv member of normalizedAlgorithm as the IV input parameter and paddedPlaintext as the input plaintext.- auto key_bytes = key->handle().get<ByteBuffer>();-- ::Crypto::Cipher::AESCBCCipher cipher(key_bytes);- auto maybe_ciphertext = cipher.encrypt(plaintext, normalized_algorithm.iv);- if (maybe_ciphertext.is_error())- return WebIDL::OperationError::create("Failed to encrypt"_utf16);-- // 4. Return the result of creating an ArrayBuffer containing ciphertext.- return maybe_ciphertext.release_value();-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-decrypt-WebIDL::ExceptionOr<void> AesCbc::validate_decrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AesCbcParams const&>(params);-- // 1. If the iv member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.- if (normalized_algorithm.iv.size() != 16)- return WebIDL::OperationError::create("IV to AES-CBC must be exactly 16 bytes"_utf16);-- return {};-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> AesCbc::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)-{- auto const& normalized_algorithm = static_cast<AesCbcParams const&>(params);-- // NB: Step 1 is performed by validate_decrypt_parameters().- TRY(validate_decrypt_parameters(params));-- // 2. If the length of ciphertext is zero or is not a multiple of 16 bytes, then throw an OperationError.- if (ciphertext.is_empty() || ciphertext.size() % 16 != 0)- return WebIDL::OperationError::create("Ciphertext length must be a multiple of 16 bytes"_utf16);-- // 3. Let paddedPlaintext be the result of performing the CBC Decryption operation described in Section 6.2 of [NIST-SP800-38A] using AES as the block cipher, the iv member of normalizedAlgorithm as the IV input parameter and ciphertext as the input ciphertext.- // 4. Let p be the value of the last octet of paddedPlaintext.- // 5. If p is zero or greater than 16, or if any of the last p octets of paddedPlaintext have a value which is not p, then throw an OperationError.- // 6. Let plaintext be the result of removing p octets from the end of paddedPlaintext.- ::Crypto::Cipher::AESCBCCipher cipher(key->handle().get<ByteBuffer>());- auto maybe_plaintext = cipher.decrypt(ciphertext, normalized_algorithm.iv);- if (maybe_plaintext.is_error())- return WebIDL::OperationError::create("Failed to decrypt"_utf16);-- // 7. Return plaintext.- return maybe_plaintext.release_value();-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> AesCbc::import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto& usage : key_usages) {- if (usage != KeyUsage::Encrypt && usage != KeyUsage::Decrypt && usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- // 2.- ByteBuffer data;- if (format == KeyFormat::Raw- || format == KeyFormat::RawSecret) {- // -> If format is "raw":- // 1. Let data be the octet string contained in keyData.- // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.- data = move(key_data.get<ByteBuffer>());- auto length_in_bits = data.size() * 8;- if (length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256) {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key length '{}' bits (must be either 128, 192, or 256 bits)", length_in_bits));- }- } else if (format == KeyFormat::Jwk) {- // -> If format is "jwk":- // 1. -> If keyData is a JsonWebKey dictionary:- // Let jwk equal keyData.- // -> Otherwise:- // Throw a DataError.- if (!key_data.has<JsonWebKey>())- return WebIDL::DataError::create("keyData is not a JsonWebKey dictionary"_utf16);- auto& jwk = key_data.get<JsonWebKey>();-- // 2. If the kty field of jwk is not "oct", then throw a DataError.- if (jwk.kty != "oct"_utf16)- return WebIDL::DataError::create("Invalid key type"_utf16);-- // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.- // Specifically, those requirements are:- // - ".k" is a valid bas64url encoded octet stream, which we do by just parsing it, in step 4.- // - ".alg" is checked only in step 5.-- // 4. Let data be the octet string obtained by decoding the k field of jwk.- data = TRY(parse_jwk_symmetric_key(realm, jwk));-- // 5. -> If data has length 128 bits:- // If the alg field of jwk is present, and is not "A128CBC", then throw a DataError.- // -> If data has length 192 bits:- // If the alg field of jwk is present, and is not "A192CBC", then throw a DataError.- // -> If data has length 256 bits:- // If the alg field of jwk is present, and is not "A256CBC", then throw a DataError.- // -> Otherwise:- // throw a DataError.- auto data_bits = data.size() * 8;- auto const& alg = jwk.alg;- if (data_bits == 128) {- if (alg.has_value() && alg != "A128CBC"_utf16) {- return WebIDL::DataError::create("Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm"_utf16);- }- } else if (data_bits == 192) {- if (alg.has_value() && alg != "A192CBC"_utf16) {- return WebIDL::DataError::create("Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm"_utf16);- }- } else if (data_bits == 256) {- if (alg.has_value() && alg != "A256CBC"_utf16) {- return WebIDL::DataError::create("Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm"_utf16);- }- } else {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key size: {} bits", data_bits));- }-- // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.- if (!key_usages.is_empty() && jwk.use.has_value() && *jwk.use != "enc"_utf16)- return WebIDL::DataError::create("Invalid use field"_utf16);-- // 7. If the key_ops field of jwk is present, and is invalid according to the- // requirements of JSON Web Key [JWK] or does not contain all of the specified usages- // values, then throw a DataError.- TRY(validate_jwk_key_ops(realm, jwk, key_usages));-- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.- if (jwk.ext.has_value() && !*jwk.ext && extractable)- return WebIDL::DataError::create("Invalid ext field"_utf16);- } else {- // Otherwise:- // throw a NotSupportedError- return WebIDL::NotSupportedError::create("Only raw and jwk formats are supported"_utf16);- }-- // 3. Let key be a new CryptoKey object representing an AES key with value data.- auto data_bits = data.size() * 8;- auto key = CryptoKey::create(move(data));-- // 4. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 5. Let algorithm be a new AesKeyAlgorithm.- // 6. Set the name attribute of algorithm to "AES-CBC".- // 7. Set the length attribute of algorithm to the length, in bits, of data.- // 8. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-CBC"_utf16, static_cast<u16>(data_bits) });-- // 9. Return key.- return key;-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-generate-key-WebIDL::ExceptionOr<void> AesCbc::validate_generate_key_parameters(AlgorithmParams const& params) const-{- // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.- auto const bits = static_cast<AesKeyGenParams const&>(params).length;- if (bits != 128 && bits != 192 && bits != 256)- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-CBC key with unusual amount of {} bits", bits));-- return {};-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-generate-key-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesCbc::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto const& usage : key_usages) {- if (usage != KeyUsage::Encrypt && usage != KeyUsage::Decrypt && usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);-- // NB: Step 2 is performed by validate_generate_key_parameters().- TRY(validate_generate_key_parameters(params));- auto const bits = normalized_algorithm.length;-- // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.- auto key_buffer = TRY(generate_random_key(realm.vm(), bits));-- // 4. If the key generation step fails, then throw an OperationError.- // Note: Cannot happen in our implementation; and if we OOM, then allocating the Exception is probably going to crash anyway.-- // 5. Let key be a new CryptoKey object representing the generated AES key.- auto key = CryptoKey::create(CryptoKey::InternalKeyData { key_buffer });-- // 9. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 6. Let algorithm be a new AesKeyAlgorithm.- // 7. Set the name attribute of algorithm to "AES-CBC".- // 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.- // 10. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-CBC"_utf16, static_cast<u16>(bits) });-- // 11. Set the [[extractable]] internal slot of key to be extractable.- key->set_extractable(extractable);-- // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.- key->set_usages(normalized_key_usages(key_usages));-- // 13. Return key.- return { key };-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-export-key-WebIDL::ExceptionOr<ExportKeyResult> AesCbc::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)-{- // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.- // Note: In our impl this is always accessible- auto const& handle = key->handle();-- Optional<ExportKeyResult> result;-- // 2. -> If format is "raw":- // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat- // For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an alias of "raw".- if (format == KeyFormat::Raw || format == KeyFormat::RawSecret) {- // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.- auto data = handle.get<ByteBuffer>();-- // 2. Let result be the result of creating an ArrayBuffer containing data.- result = ExportKeyResult { data };- }- // -> If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. Let jwk be a new JsonWebKey dictionary.- JsonWebKey jwk = {};-- // 2. Set the kty attribute of jwk to the string "oct".- jwk.kty = "oct"_utf16;-- // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key, encoded according to Section 6.4 of JSON Web Algorithms [JWA].- auto const& key_bytes = handle.get<ByteBuffer>();- jwk.k = Utf16String::from_utf8(TRY_OR_THROW_OOM(realm.vm(), encode_base64url(key_bytes, AK::OmitPadding::Yes)));-- // 4. -> If the length attribute of key is 128:- // Set the alg attribute of jwk to the string "A128CBC".- // -> If the length attribute of key is 192:- // Set the alg attribute of jwk to the string "A192CBC".- // -> If the length attribute of key is 256:- // Set the alg attribute of jwk to the string "A256CBC".- auto key_bits = key_bytes.size() * 8;- if (key_bits == 128) {- jwk.alg = "A128CBC"_utf16;- } else if (key_bits == 192) {- jwk.alg = "A192CBC"_utf16;- } else if (key_bits == 256) {- jwk.alg = "A256CBC"_utf16;- } else {- return WebIDL::OperationError::create("unclear key size"_utf16);- }-- // 5. Set the key_ops attribute of jwk to equal the usages attribute of key.- jwk.key_ops = Vector<Utf16String> {};- jwk.key_ops->ensure_capacity(key->internal_usages().size());- for (auto const& usage : key->internal_usages()) {- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));- }-- // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.- jwk.ext = key->extractable();-- // 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].- result = ExportKeyResult { move(jwk) };- }- // -> Otherwise:- else {- // throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Cannot export to unsupported format"_utf16);- }-- // 3. Return result.- return result.release_value();-}--// https://w3c.github.io/webcrypto/#aes-cbc-operations-get-key-length-WebIDL::ExceptionOr<Optional<u32>> AesCbc::get_key_length(JS::Realm&, AlgorithmParams const& params)-{- // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw an OperationError.- auto const& normalized_algorithm = static_cast<AesDerivedKeyParams const&>(params);- auto length = normalized_algorithm.length;- if (length != 128 && length != 192 && length != 256)- return WebIDL::OperationError::create("Invalid key length"_utf16);-- // 2. Return the length member of normalizedDerivedKeyAlgorithm.- return length;-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> AesCtr::import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto& usage : key_usages) {- if (usage != KeyUsage::Encrypt && usage != KeyUsage::Decrypt && usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- ByteBuffer data;-- // 2. If format is "raw":- if (format == KeyFormat::Raw- || format == KeyFormat::RawSecret) {- // 1. Let data be the octet string contained in keyData.- data = move(key_data.get<ByteBuffer>());-- // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.- auto length_in_bits = data.size() * 8;- if (length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256) {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key length '{}' bits (must be either 128, 192, or 256 bits)", length_in_bits));- }- }-- // 2. If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. -> If keyData is a JsonWebKey dictionary:- // Let jwk equal keyData.- // -> Otherwise:- // Throw a DataError.- if (!key_data.has<JsonWebKey>())- return WebIDL::DataError::create("keyData is not a JsonWebKey dictionary"_utf16);-- auto& jwk = key_data.get<JsonWebKey>();-- // 2. If the kty field of jwk is not "oct", then throw a DataError.- if (jwk.kty != "oct"_utf16)- return WebIDL::DataError::create("Invalid key type"_utf16);-- // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.- // Specifically, those requirements are:- // * the member "k" is used to represent a symmetric key (or another key whose value is a single octet sequence).- // * An "alg" member SHOULD also be present to identify the algorithm intended to be used with the key,- // unless the application uses another means or convention to determine the algorithm used.- if (!jwk.k.has_value())- return WebIDL::DataError::create("Missing 'k' field"_utf16);-- if (!jwk.alg.has_value())- return WebIDL::DataError::create("Missing 'alg' field"_utf16);-- // 4. Let data be the octet string obtained by decoding the k field of jwk.- data = TRY(parse_jwk_symmetric_key(realm, jwk));-- // 5. -> If data has length 128 bits:- // If the alg field of jwk is present, and is not "A128CTR", then throw a DataError.- // -> If data has length 192 bits:- // If the alg field of jwk is present, and is not "A192CTR", then throw a DataError.- // -> If data has length 256 bits:- // If the alg field of jwk is present, and is not "A256CTR", then throw a DataError.- // -> Otherwise:- // throw a DataError.- auto data_bits = data.size() * 8;- auto const& alg = jwk.alg;- if (data_bits == 128) {- if (alg.has_value() && alg != "A128CTR"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm"_utf16);- } else if (data_bits == 192) {- if (alg.has_value() && alg != "A192CTR"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm"_utf16);- } else if (data_bits == 256) {- if (alg.has_value() && alg != "A256CTR"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm"_utf16);- } else {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key size: {} bits", data_bits));- }-- // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.- if (!key_usages.is_empty() && jwk.use.has_value() && *jwk.use != "enc"_utf16)- return WebIDL::DataError::create("Invalid use field"_utf16);-- // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK]- // or does not contain all of the specified usages values, then throw a DataError.- TRY(validate_jwk_key_ops(realm, jwk, key_usages));-- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.- if (jwk.ext.has_value() && !*jwk.ext && extractable)- return WebIDL::DataError::create("Invalid ext field"_utf16);- }-- // 2. Otherwise:- else {- // 1. throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Only raw and jwk formats are supported"_utf16);- }-- auto data_bits = data.size() * 8;-- // 3. Let key be a new CryptoKey object representing an AES key with value data.- auto key = CryptoKey::create(move(data));-- // 4. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 5. Let algorithm be a new AesKeyAlgorithm.- // 6. Set the name attribute of algorithm to "AES-CTR".- // 7. Set the length attribute of algorithm to the length, in bits, of data.- // 8. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-CTR"_utf16, static_cast<u16>(data_bits) });-- // 9. Return key.- return key;-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-export-key-WebIDL::ExceptionOr<ExportKeyResult> AesCtr::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)-{- // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.- // Note: In our impl this is always accessible-- Optional<ExportKeyResult> result;-- // 2. If format is "raw":- // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat- // For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an alias of "raw".- if (format == KeyFormat::Raw || format == KeyFormat::RawSecret) {- // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.- auto data = key->handle().get<ByteBuffer>();-- // 2. Let result be the result of creating an ArrayBuffer containing data.- result = ExportKeyResult { data };- }-- // 2. If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. Let jwk be a new JsonWebKey dictionary.- JsonWebKey jwk = {};-- // 2. Set the kty attribute of jwk to the string "oct".- jwk.kty = "oct"_utf16;-- // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,- // encoded according to Section 6.4 of JSON Web Algorithms [JWA].- auto const& key_bytes = key->handle().get<ByteBuffer>();- jwk.k = Utf16String::from_utf8(TRY_OR_THROW_OOM(realm.vm(), encode_base64url(key_bytes, AK::OmitPadding::Yes)));-- // 4. -> If the length attribute of key is 128:- // Set the alg attribute of jwk to the string "A128CTR".- // -> If the length attribute of key is 192:- // Set the alg attribute of jwk to the string "A192CTR".- // -> If the length attribute of key is 256:- // Set the alg attribute of jwk to the string "A256CTR".- auto key_bits = key_bytes.size() * 8;- if (key_bits == 128) {- jwk.alg = "A128CTR"_utf16;- } else if (key_bits == 192) {- jwk.alg = "A192CTR"_utf16;- } else if (key_bits == 256) {- jwk.alg = "A256CTR"_utf16;- }-- // 5. Set the key_ops attribute of jwk to the usages attribute of key.- jwk.key_ops = Vector<Utf16String> {};- jwk.key_ops->ensure_capacity(key->internal_usages().size());- for (auto const& usage : key->internal_usages()) {- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));- }-- // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.- jwk.ext = key->extractable();-- // 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].- result = ExportKeyResult { move(jwk) };- }-- // 2. Otherwise:- else {- // 1. throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Cannot export to unsupported format"_utf16);- }-- // 3. Return result.- return result.release_value();-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-get-key-length-WebIDL::ExceptionOr<Optional<u32>> AesCtr::get_key_length(JS::Realm&, AlgorithmParams const& params)-{- // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw a OperationError.- auto const& normalized_algorithm = static_cast<AesDerivedKeyParams const&>(params);- auto length = normalized_algorithm.length;- if (length != 128 && length != 192 && length != 256)- return WebIDL::OperationError::create("Invalid key length"_utf16);-- // 2. Return the length member of normalizedDerivedKeyAlgorithm.- return length;-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-generate-key-WebIDL::ExceptionOr<void> AesCtr::validate_generate_key_parameters(AlgorithmParams const& params) const-{- // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.- auto const bits = static_cast<AesKeyGenParams const&>(params).length;- if (bits != 128 && bits != 192 && bits != 256)- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-CTR key with unusual amount of {} bits", bits));-- return {};-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-generate-key-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesCtr::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto const& usage : key_usages) {- if (usage != KeyUsage::Encrypt && usage != KeyUsage::Decrypt && usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- // NB: Step 2 is performed by validate_generate_key_parameters().- TRY(validate_generate_key_parameters(params));- auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);- auto const bits = normalized_algorithm.length;-- // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.- // 4. If the key generation step fails, then throw an OperationError.- auto key_buffer = TRY(generate_random_key(realm.vm(), bits));-- // 5. Let key be a new CryptoKey object representing the generated AES key.- auto key = CryptoKey::create(CryptoKey::InternalKeyData { key_buffer });-- // 9. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 6. Let algorithm be a new AesKeyAlgorithm.- // 7. Set the name attribute of algorithm to "AES-CTR".- // 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.- // 10. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-CTR"_utf16, static_cast<u16>(bits) });-- // 11. Set the [[extractable]] internal slot of key to be extractable.- key->set_extractable(extractable);-- // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.- key->set_usages(normalized_key_usages(key_usages));-- // 13. Return key.- return { key };-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-encrypt-WebIDL::ExceptionOr<void> AesCtr::validate_encrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AesCtrParams const&>(params);-- // 1. If the counter member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.- if (normalized_algorithm.counter.size() != 16)- return WebIDL::OperationError::create("Invalid counter length"_utf16);-- // 2. If the length member of normalizedAlgorithm is zero or is greater than 128, then throw an OperationError.- if (normalized_algorithm.length == 0 || normalized_algorithm.length > 128)- return WebIDL::OperationError::create("Invalid length"_utf16);-- return {};-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> AesCtr::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)-{- // NB: Steps 1 and 2 are performed by validate_encrypt_parameters().- TRY(validate_encrypt_parameters(params));- auto const& counter = static_cast<AesCtrParams const&>(params).counter;-- // 3. Let ciphertext be the result of performing the CTR Encryption operation described in Section 6.5 of [NIST-SP800-38A] using- // AES as the block cipher,- // the contents of the counter member of normalizedAlgorithm as the initial value of the counter block,- // the length member of normalizedAlgorithm as the input parameter m to the standard counter block incrementing function defined in Appendix B.1 of [NIST-SP800-38A]- // and the contents of plaintext as the input plaintext.- ::Crypto::Cipher::AESCTRCipher cipher(key->handle().get<ByteBuffer>());- auto maybe_ciphertext = cipher.encrypt(plaintext, counter);- if (maybe_ciphertext.is_error())- return WebIDL::OperationError::create("Encryption failed"_utf16);-- // 4. Return the result of creating an ArrayBuffer containing plaintext.- return maybe_ciphertext.release_value();-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-decrypt-WebIDL::ExceptionOr<void> AesCtr::validate_decrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AesCtrParams const&>(params);-- // 1. If the counter member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.- if (normalized_algorithm.counter.size() != 16)- return WebIDL::OperationError::create("Invalid counter length"_utf16);-- // 2. If the length member of normalizedAlgorithm is zero or is greater than 128, then throw an OperationError.- if (normalized_algorithm.length == 0 || normalized_algorithm.length > 128)- return WebIDL::OperationError::create("Invalid length"_utf16);-- return {};-}--// https://w3c.github.io/webcrypto/#aes-ctr-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> AesCtr::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)-{- // NB: Steps 1 and 2 are performed by validate_decrypt_parameters().- TRY(validate_decrypt_parameters(params));- auto const& counter = static_cast<AesCtrParams const&>(params).counter;-- // 3. Let plaintext be the result of performing the CTR Decryption operation described in Section 6.5 of [NIST-SP800-38A] using- // AES as the block cipher,- // the contents of the counter member of normalizedAlgorithm as the initial value of the counter block,- // the length member of normalizedAlgorithm as the input parameter m to the standard counter block incrementing function defined in Appendix B.1 of [NIST-SP800-38A]- // and the contents of ciphertext as the input ciphertext.- ::Crypto::Cipher::AESCTRCipher cipher(key->handle().get<ByteBuffer>());- auto maybe_plaintext = cipher.decrypt(ciphertext, counter);- if (maybe_plaintext.is_error())- return WebIDL::OperationError::create("Decryption failed"_utf16);-- // 4. Return the result of creating an ArrayBuffer containing plaintext.- return maybe_plaintext.release_value();-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-get-key-length-WebIDL::ExceptionOr<Optional<u32>> AesGcm::get_key_length(JS::Realm&, AlgorithmParams const& params)-{- // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw a OperationError.- auto const& normalized_algorithm = static_cast<AesDerivedKeyParams const&>(params);- auto length = normalized_algorithm.length;- if (length != 128 && length != 192 && length != 256)- return WebIDL::OperationError::create("Invalid key length"_utf16);-- // 2. Return the length member of normalizedDerivedKeyAlgorithm.- return length;-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> AesGcm::import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto& usage : key_usages) {- if (usage != KeyUsage::Encrypt && usage != KeyUsage::Decrypt && usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- ByteBuffer data;-- // 2. If format is "raw":- if (format == KeyFormat::Raw- || format == KeyFormat::RawSecret) {- // 1. Let data be the octet string contained in keyData.- data = move(key_data.get<ByteBuffer>());-- // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.- auto length_in_bits = data.size() * 8;- if (length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256) {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key length '{}' bits (must be either 128, 192, or 256 bits)", length_in_bits));- }- }-- // 2. If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. -> If keyData is a JsonWebKey dictionary:- // Let jwk equal keyData.- // -> Otherwise:- // Throw a DataError.- if (!key_data.has<JsonWebKey>())- return WebIDL::DataError::create("keyData is not a JsonWebKey dictionary"_utf16);-- auto& jwk = key_data.get<JsonWebKey>();-- // 2. If the kty field of jwk is not "oct", then throw a DataError.- if (jwk.kty != "oct"_utf16)- return WebIDL::DataError::create("Invalid key type"_utf16);-- // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.- // Specifically, those requirements are:- // * the member "k" is used to represent a symmetric key (or another key whose value is a single octet sequence).- // * An "alg" member SHOULD also be present to identify the algorithm intended to be used with the key,- // unless the application uses another means or convention to determine the algorithm used.- if (!jwk.k.has_value())- return WebIDL::DataError::create("Missing 'k' field"_utf16);-- if (!jwk.alg.has_value())- return WebIDL::DataError::create("Missing 'alg' field"_utf16);-- // 4. Let data be the octet string obtained by decoding the k field of jwk.- data = TRY(parse_jwk_symmetric_key(realm, jwk));-- // 5. -> If data has length 128 bits:- // If the alg field of jwk is present, and is not "A128GCM", then throw a DataError.- // -> If data has length 192 bits:- // If the alg field of jwk is present, and is not "A192GCM", then throw a DataError.- // -> If data has length 256 bits:- // If the alg field of jwk is present, and is not "A256GCM", then throw a DataError.- // -> Otherwise:- // throw a DataError.- auto data_bits = data.size() * 8;- auto const& alg = jwk.alg;- if (data_bits == 128) {- if (alg.has_value() && alg != "A128GCM"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm"_utf16);- } else if (data_bits == 192) {- if (alg.has_value() && alg != "A192GCM"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm"_utf16);- } else if (data_bits == 256) {- if (alg.has_value() && alg != "A256GCM"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm"_utf16);- } else {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key size: {} bits", data_bits));- }-- // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.- if (!key_usages.is_empty() && jwk.use.has_value() && *jwk.use != "enc"_utf16)- return WebIDL::DataError::create("Invalid use field"_utf16);-- // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK]- // or does not contain all of the specified usages values, then throw a DataError.- TRY(validate_jwk_key_ops(realm, jwk, key_usages));-- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.- if (jwk.ext.has_value() && !*jwk.ext && extractable)- return WebIDL::DataError::create("Invalid ext field"_utf16);- }-- // 2. Otherwise:- else {- // 1. throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Only raw and jwk formats are supported"_utf16);- }-- auto data_bits = data.size() * 8;-- // 3. Let key be a new CryptoKey object representing an AES key with value data.- auto key = CryptoKey::create(move(data));-- // 4. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 5. Let algorithm be a new AesKeyAlgorithm.- // 6. Set the name attribute of algorithm to "AES-GCM".- // 7. Set the length attribute of algorithm to the length, in bits, of data.- // 8. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-GCM"_utf16, static_cast<u16>(data_bits) });-- // 9. Return key.- return key;-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-export-key-WebIDL::ExceptionOr<ExportKeyResult> AesGcm::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)-{- // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.- // Note: In our impl this is always accessible-- Optional<ExportKeyResult> result;-- // 2. If format is "raw":- // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat- // For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an alias of "raw".- if (format == KeyFormat::Raw || format == KeyFormat::RawSecret) {- // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.- auto data = key->handle().get<ByteBuffer>();-- // 2. Let result be the result of creating an ArrayBuffer containing data.- result = ExportKeyResult { data };- }-- // 2. If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. Let jwk be a new JsonWebKey dictionary.- JsonWebKey jwk = {};-- // 2. Set the kty attribute of jwk to the string "oct".- jwk.kty = "oct"_utf16;-- // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,- // encoded according to Section 6.4 of JSON Web Algorithms [JWA].- auto const& key_bytes = key->handle().get<ByteBuffer>();- jwk.k = Utf16String::from_utf8(TRY_OR_THROW_OOM(realm.vm(), encode_base64url(key_bytes, AK::OmitPadding::Yes)));-- // 4. -> If the length attribute of key is 128:- // Set the alg attribute of jwk to the string "A128GCM".- // -> If the length attribute of key is 192:- // Set the alg attribute of jwk to the string "A192GCM".- // -> If the length attribute of key is 256:- // Set the alg attribute of jwk to the string "A256GCM".- auto key_bits = key_bytes.size() * 8;- if (key_bits == 128) {- jwk.alg = "A128GCM"_utf16;- } else if (key_bits == 192) {- jwk.alg = "A192GCM"_utf16;- } else if (key_bits == 256) {- jwk.alg = "A256GCM"_utf16;- }-- // 5. Set the key_ops attribute of jwk to the usages attribute of key.- jwk.key_ops = Vector<Utf16String> {};- jwk.key_ops->ensure_capacity(key->internal_usages().size());- for (auto const& usage : key->internal_usages()) {- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));- }-- // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.- jwk.ext = key->extractable();-- // 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].- result = ExportKeyResult { move(jwk) };- }-- // 2. Otherwise:- else {- // 1. throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Cannot export to unsupported format"_utf16);- }-- // 3. Return result.- return result.release_value();-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-encrypt-WebIDL::ExceptionOr<void> AesGcm::validate_encrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AesGcmParams const&>(params);-- // NB: Our AES-GCM rejects an empty IV.- if (normalized_algorithm.iv.is_empty())- return WebIDL::OperationError::create("IV must not be empty"_utf16);-- // 1. If the iv member of normalizedAlgorithm has a length greater than 2^64 - 1 bytes, then throw an OperationError.- // NOTE: This is not possible-- // 2. If the additionalData member of normalizedAlgorithm is present and has a length greater than 2^64 - 1 bytes, then throw an OperationError.- // NOTE: This is not possible-- // 3. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.- // If the tagLength member of normalizedAlgorithm is one of 32, 64, 96, 104, 112, 120 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm- // Otherwise: throw an OperationError.- auto constexpr valid_tag_lengths = Array { 32, 64, 96, 104, 112, 120, 128 };- if (normalized_algorithm.tag_length.has_value() && !valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))- return WebIDL::OperationError::create("Invalid tag length"_utf16);-- return {};-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> AesGcm::encrypt(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)-{- auto const& normalized_algorithm = static_cast<AesGcmParams const&>(params);-- // NB: Steps 1 to 3 are performed by validate_encrypt_parameters(), which leaves tagLength as the tagLength member- // of normalizedAlgorithm if present, or 128 otherwise.- TRY(validate_encrypt_parameters(params));- auto tag_length = normalized_algorithm.tag_length.value_or(128);-- // 4. If plaintext has a length greater than 2^39 - 256 bytes, then throw an OperationError.- if (plaintext.size() > (1ULL << 39) - 256)- return WebIDL::OperationError::create("Invalid plaintext length"_utf16);-- // 5. Let additionalData be the contents of the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.- auto additional_data = normalized_algorithm.additional_data.value_or(ByteBuffer {});-- // 6. Let C and T be the outputs that result from performing the Authenticated Encryption Function described in Section 7.1 of [NIST-SP800-38D] using- // AES as the block cipher,- // the contents of the iv member of normalizedAlgorithm as the IV input parameter,- // the contents of additionalData as the A input parameter,- // tagLength as the t pre-requisite- // and the contents of plaintext as the input plaintext.- ::Crypto::Cipher::AESGCMCipher cipher(key->handle().get<ByteBuffer>());- auto maybe_encrypted = cipher.encrypt(plaintext, normalized_algorithm.iv, additional_data, tag_length / 8);- if (maybe_encrypted.is_error()) {- return WebIDL::OperationError::create("Encryption failed"_utf16);- }-- auto [ciphertext, tag] = maybe_encrypted.release_value();-- // 7. Let ciphertext be equal to C | T, where '|' denotes concatenation.- TRY_OR_THROW_OOM(realm.vm(), ciphertext.try_append(tag));-- // 8. Return the result of creating an ArrayBuffer containing ciphertext.- return ciphertext;-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-decrypt-WebIDL::ExceptionOr<void> AesGcm::validate_decrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AesGcmParams const&>(params);-- // NB: Our AES-GCM rejects an empty IV.- if (normalized_algorithm.iv.is_empty())- return WebIDL::OperationError::create("IV must not be empty"_utf16);-- // 1. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.- // If the tagLength member of normalizedAlgorithm is one of 32, 64, 96, 104, 112, 120 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm- // Otherwise: throw an OperationError.- auto constexpr valid_tag_lengths = Array { 32, 64, 96, 104, 112, 120, 128 };- if (normalized_algorithm.tag_length.has_value() && !valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))- return WebIDL::OperationError::create("Invalid tag length"_utf16);-- // 2. If the iv member of normalizedAlgorithm has a length greater than 2^64 - 1 bytes, then throw an OperationError.- // NOTE: This is not possible-- // 3. If the additionalData member of normalizedAlgorithm is present and has a length greater than 2^64 - 1 bytes, then throw an OperationError.- // NOTE: This is not possible-- return {};-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> AesGcm::decrypt(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)-{- auto const& normalized_algorithm = static_cast<AesGcmParams const&>(params);-- // NB: Steps 1 to 3 are performed by validate_decrypt_parameters(), which leaves tagLength as the tagLength member- // of normalizedAlgorithm if present, or 128 otherwise.- TRY(validate_decrypt_parameters(params));- u32 tag_length = normalized_algorithm.tag_length.value_or(128);-- // 4. If ciphertext has a length in bits less than tagLength, then throw an OperationError.- if (ciphertext.size() < tag_length / 8)- return WebIDL::OperationError::create("Invalid ciphertext length"_utf16);-- // 5. Let tag be the last tagLength bits of ciphertext.- auto tag_bytes = tag_length / 8;- auto tag = TRY_OR_THROW_OOM(realm.vm(), ciphertext.slice(ciphertext.size() - tag_bytes, tag_bytes));-- // 6. Let actualCiphertext be the result of removing the last tagLength bits from ciphertext.- auto actual_ciphertext = TRY_OR_THROW_OOM(realm.vm(), ciphertext.slice(0, ciphertext.size() - tag_bytes));-- // 7. Let additionalData be the contents of the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.- auto additional_data = normalized_algorithm.additional_data.value_or(ByteBuffer {});-- // 8. Perform the Authenticated Decryption Function described in Section 7.2 of [NIST-SP800-38D] using- // AES as the block cipher,- // the contents of the iv member of normalizedAlgorithm as the IV input parameter,- // the contents of additionalData as the A input parameter,- // tagLength as the t pre-requisite,- // the contents of actualCiphertext as the input ciphertext, C- // and the contents of tag as the authentication tag, T.- // If the result of the algorithm is the indication of inauthenticity, "FAIL": throw an OperationError- ::Crypto::Cipher::AESGCMCipher cipher(key->handle().get<ByteBuffer>());- auto maybe_plaintext = cipher.decrypt(actual_ciphertext.bytes(), normalized_algorithm.iv, additional_data, tag);- if (maybe_plaintext.is_error()) {- dbgln("FAILED: {}", maybe_plaintext.error());- return WebIDL::OperationError::create("Decryption failed"_utf16);- }-- // Otherwise: Let plaintext be the output P of the Authenticated Decryption Function.- // 9. Return the result of creating an ArrayBuffer containing plaintext.- return maybe_plaintext.release_value();-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-generate-key-WebIDL::ExceptionOr<void> AesGcm::validate_generate_key_parameters(AlgorithmParams const& params) const-{- // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.- auto const bits = static_cast<AesKeyGenParams const&>(params).length;- if (bits != 128 && bits != 192 && bits != 256)- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-GCM key with unusual amount of {} bits", bits));-- return {};-}--// https://w3c.github.io/webcrypto/#aes-gcm-operations-generate-key-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesGcm::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto const& usage : key_usages) {- if (usage != KeyUsage::Encrypt && usage != KeyUsage::Decrypt && usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- // NB: Step 2 is performed by validate_generate_key_parameters().- TRY(validate_generate_key_parameters(params));- auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);- auto const bits = normalized_algorithm.length;-- // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.- // 4. If the key generation step fails, then throw an OperationError.- auto key_buffer = TRY(generate_random_key(realm.vm(), bits));-- // 5. Let key be a new CryptoKey object representing the generated AES key.- auto key = CryptoKey::create(CryptoKey::InternalKeyData { key_buffer });-- // 9. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 6. Let algorithm be a new AesKeyAlgorithm.- // 7. Set the name attribute of algorithm to "AES-GCM".- // 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.- // 10. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-GCM"_utf16, static_cast<u16>(bits) });-- // 11. Set the [[extractable]] internal slot of key to be extractable.- key->set_extractable(extractable);-- // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.- key->set_usages(normalized_key_usages(key_usages));-- // 13. Return key.- return { key };-}--// https://w3c.github.io/webcrypto/#aes-kw-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> AesKw::import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto& usage : key_usages) {- if (usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- ByteBuffer data;-- // 2. If format is "raw":- if (format == KeyFormat::Raw- || format == KeyFormat::RawSecret) {- // 1. Let data be the octet string contained in keyData.- data = move(key_data.get<ByteBuffer>());-- // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.- auto length_in_bits = data.size() * 8;- if (length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256) {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key length '{}' bits (must be either 128, 192, or 256 bits)", length_in_bits));- }- }-- // 2. If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. -> If keyData is a JsonWebKey dictionary:- // Let jwk equal keyData.- // -> Otherwise:- // Throw a DataError.- if (!key_data.has<JsonWebKey>())- return WebIDL::DataError::create("keyData is not a JsonWebKey dictionary"_utf16);-- auto& jwk = key_data.get<JsonWebKey>();-- // 2. If the kty field of jwk is not "oct", then throw a DataError.- if (jwk.kty != "oct"_utf16)- return WebIDL::DataError::create("Invalid key type"_utf16);-- // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.- // Specifically, those requirements are:- // * the member "k" is used to represent a symmetric key (or another key whose value is a single octet sequence).- // * An "alg" member SHOULD also be present to identify the algorithm intended to be used with the key,- // unless the application uses another means or convention to determine the algorithm used.- // NOTE: "k" is already checked in step 4.- if (!jwk.alg.has_value())- return WebIDL::DataError::create("Missing 'alg' field"_utf16);-- // 4. Let data be the octet string obtained by decoding the k field of jwk.- data = TRY(parse_jwk_symmetric_key(realm, jwk));-- // 5. -> If data has length 128 bits:- // If the alg field of jwk is present, and is not "A128KW", then throw a DataError.- // -> If data has length 192 bits:- // If the alg field of jwk is present, and is not "A192KW", then throw a DataError.- // -> If data has length 256 bits:- // If the alg field of jwk is present, and is not "A256KW", then throw a DataError.- // -> Otherwise:- // throw a DataError.- auto data_bits = data.size() * 8;- auto const& alg = jwk.alg;- if (data_bits == 128) {- if (alg.has_value() && alg != "A128KW"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm"_utf16);- } else if (data_bits == 192) {- if (alg.has_value() && alg != "A192KW"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm"_utf16);- } else if (data_bits == 256) {- if (alg.has_value() && alg != "A256KW"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm"_utf16);- } else {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key size: {} bits", data_bits));- }-- // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.- if (!key_usages.is_empty() && jwk.use.has_value() && *jwk.use != "enc"_utf16)- return WebIDL::DataError::create("Invalid use field"_utf16);-- // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK]- // or does not contain all of the specified usages values, then throw a DataError.- TRY(validate_jwk_key_ops(realm, jwk, key_usages));-- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.- if (jwk.ext.has_value() && !*jwk.ext && extractable)- return WebIDL::DataError::create("Invalid ext field"_utf16);- }-- // 2. Otherwise:- else {- // 1. throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Only raw and jwk formats are supported"_utf16);- }-- auto data_bits = data.size() * 8;-- // 3. Let key be a new CryptoKey object representing an AES key with value data.- auto key = CryptoKey::create(move(data));-- // 4. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 5. Let algorithm be a new AesKeyAlgorithm.- // 6. Set the name attribute of algorithm to "AES-KW".- // 7. Set the length attribute of algorithm to the length, in bits, of data.- // 8. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-KW"_utf16, static_cast<u16>(data_bits) });-- // 9. Return key.- return key;-}--// https://w3c.github.io/webcrypto/#aes-kw-operations-export-key-WebIDL::ExceptionOr<ExportKeyResult> AesKw::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)-{- // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.- // Note: In our impl this is always accessible-- Optional<ExportKeyResult> result;-- // 2. If format is "raw":- // https://wicg.github.io/webcrypto-modern-algos/#subtlecrypto-interface-keyformat- // For all existing symmetric algorithms in WebCrypto, "raw-secret" acts as an alias of "raw".- if (format == KeyFormat::Raw || format == KeyFormat::RawSecret) {- // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.- auto data = key->handle().get<ByteBuffer>();-- // 2. Let result be the result of creating an ArrayBuffer containing data.- result = ExportKeyResult { data };- }-- // 2. If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. Let jwk be a new JsonWebKey dictionary.- JsonWebKey jwk = {};-- // 2. Set the kty attribute of jwk to the string "oct".- jwk.kty = "oct"_utf16;-- // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,- // encoded according to Section 6.4 of JSON Web Algorithms [JWA].- auto const& key_bytes = key->handle().get<ByteBuffer>();- jwk.k = Utf16String::from_utf8(TRY_OR_THROW_OOM(realm.vm(), encode_base64url(key_bytes, AK::OmitPadding::Yes)));-- // 4. -> If the length attribute of key is 128:- // Set the alg attribute of jwk to the string "A128KW".- // -> If the length attribute of key is 192:- // Set the alg attribute of jwk to the string "A192KW".- // -> If the length attribute of key is 256:- // Set the alg attribute of jwk to the string "A256KW".- auto key_bits = key_bytes.size() * 8;- if (key_bits == 128) {- jwk.alg = "A128KW"_utf16;- } else if (key_bits == 192) {- jwk.alg = "A192KW"_utf16;- } else if (key_bits == 256) {- jwk.alg = "A256KW"_utf16;- }-- // 5. Set the key_ops attribute of jwk to the usages attribute of key.- jwk.key_ops = Vector<Utf16String> {};- jwk.key_ops->ensure_capacity(key->internal_usages().size());- for (auto const& usage : key->internal_usages()) {- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));- }-- // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.- jwk.ext = key->extractable();-- // 7. Let result be the result of converting jwk to an ECMAScript Object, as defined by [WebIDL].- result = ExportKeyResult { move(jwk) };- }-- // 2. Otherwise:- else {- // 1. throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Cannot export to unsupported format"_utf16);- }-- // 3. Return result.- return result.release_value();-}--// https://w3c.github.io/webcrypto/#aes-kw-operations-get-key-length-WebIDL::ExceptionOr<Optional<u32>> AesKw::get_key_length(JS::Realm&, AlgorithmParams const& params)-{- // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw an OperationError.- auto const& normalized_algorithm = static_cast<AesDerivedKeyParams const&>(params);- auto length = normalized_algorithm.length;- if (length != 128 && length != 192 && length != 256)- return WebIDL::OperationError::create("Invalid key length"_utf16);-- // 2. Return the length member of normalizedDerivedKeyAlgorithm.- return length;-}--// https://w3c.github.io/webcrypto/#aes-kw-operations-generate-key-WebIDL::ExceptionOr<void> AesKw::validate_generate_key_parameters(AlgorithmParams const& params) const-{- // 2. If the length property of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.- auto const bits = static_cast<AesKeyGenParams const&>(params).length;- if (bits != 128 && bits != 192 && bits != 256)- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-KW key with unusual amount of {} bits", bits));-- return {};-}--// https://w3c.github.io/webcrypto/#aes-kw-operations-generate-key-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesKw::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto const& usage : key_usages) {- if (usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- // NB: Step 2 is performed by validate_generate_key_parameters().- TRY(validate_generate_key_parameters(params));- auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);- auto const bits = normalized_algorithm.length;-- // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.- // 4. If the key generation step fails, then throw an OperationError.- auto key_buffer = TRY(generate_random_key(realm.vm(), bits));-- // 5. Let key be a new CryptoKey object representing the generated AES key.- auto key = CryptoKey::create(CryptoKey::InternalKeyData { key_buffer });-- // 9. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 6. Let algorithm be a new AesKeyAlgorithm.- // 7. Set the name attribute of algorithm to "AES-KW".- // 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.- // 10. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-KW"_utf16, static_cast<u16>(bits) });-- // 11. Set the [[extractable]] internal slot of key to be extractable.- key->set_extractable(extractable);-- // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.- key->set_usages(normalized_key_usages(key_usages));+{+ static constexpr Array usages { KeyUsage::Verify };+ return usages.span();+}- // 13. Return key.- return { key };+extern "C" {+bool web_crypto_aes_cbc_cipher(bool, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);+bool web_crypto_aes_ctr_cipher(bool, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);+bool web_crypto_aes_kw_cipher(bool, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);+bool web_crypto_aes_gcm_encrypt(ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, size_t, void*, void*, ReceiveEncodedBytes);+bool web_crypto_aes_gcm_decrypt(ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);+FlatPtr web_crypto_aes_ocb_cipher(bool, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, size_t, void*, ReceiveEncodedBytes);}-// https://w3c.github.io/webcrypto/#aes-kw-operations-wrap-key-WebIDL::ExceptionOr<ByteBuffer> AesKw::wrap_key(JS::Realm&, AlgorithmParams const&, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+bool web_crypto_aes_cbc_cipher(bool encrypt, ReadonlyBytes key, ReadonlyBytes input, ReadonlyBytes iv, void* state, ReceiveEncodedBytes receive){- // 1. If plaintext is not a multiple of 64 bits in length, then throw an OperationError.- if (plaintext.size() % 8 != 0)- return WebIDL::OperationError::create("Invalid plaintext length"_utf16);+ ::Crypto::Cipher::AESCBCCipher cipher(key);+ auto result = encrypt ? cipher.encrypt(input, iv) : cipher.decrypt(input, iv);+ if (result.is_error())+ return false;+ receive(state, result.value());+ return true;+}- // 2. Let ciphertext be the result of performing the Key Wrap operation described in Section 2.2.1 of [RFC3394]- // with plaintext as the plaintext to be wrapped and using the default Initial Value defined in Section 2.2.3.1 of the same document.- ::Crypto::Cipher::AESKWCipher cipher(key->handle().get<ByteBuffer>());- auto maybe_ciphertext = cipher.wrap(plaintext.bytes());- if (maybe_ciphertext.is_error())- return WebIDL::OperationError::create("Key wrap failed"_utf16);+bool web_crypto_aes_ctr_cipher(bool encrypt, ReadonlyBytes key, ReadonlyBytes input, ReadonlyBytes counter, void* state, ReceiveEncodedBytes receive)+{+ ::Crypto::Cipher::AESCTRCipher cipher(key);+ auto result = encrypt ? cipher.encrypt(input, counter) : cipher.decrypt(input, counter);+ if (result.is_error())+ return false;+ receive(state, result.value());+ return true;+}- // 3. Return ciphertext.- return maybe_ciphertext.release_value();+bool web_crypto_aes_kw_cipher(bool wrap, ReadonlyBytes key, ReadonlyBytes input, void* state, ReceiveEncodedBytes receive)+{+ ::Crypto::Cipher::AESKWCipher cipher(key);+ auto result = wrap ? cipher.wrap(input) : cipher.unwrap(input);+ if (result.is_error())+ return false;+ receive(state, result.value());+ return true;}-// https://w3c.github.io/webcrypto/#aes-kw-operations-unwrap-key-WebIDL::ExceptionOr<ByteBuffer> AesKw::unwrap_key(JS::Realm&, AlgorithmParams const&, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+bool web_crypto_aes_gcm_encrypt(ReadonlyBytes key, ReadonlyBytes plaintext, ReadonlyBytes iv, ReadonlyBytes additional_data, size_t tag_length, void* ciphertext, void* tag, ReceiveEncodedBytes receive){- // NOTE: The spec does not mention this, but we need to check- if (ciphertext.size() < 8)- return WebIDL::OperationError::create("Invalid ciphertext length"_utf16);+ ::Crypto::Cipher::AESGCMCipher cipher(key);+ auto result = cipher.encrypt(plaintext, iv, additional_data, tag_length);+ if (result.is_error())+ return false;+ receive(ciphertext, result.value().ciphertext);+ receive(tag, result.value().tag);+ return true;+}- // 1. Let plaintext be the result of performing the Key Unwrap operation described in Section 2.2.2 of [RFC3394]- // with ciphertext as the input ciphertext and using the default Initial Value defined in Section 2.2.3.1 of the same document- // 2. If the Key Unwrap operation returns an error, then throw an OperationError.- ::Crypto::Cipher::AESKWCipher cipher(key->handle().get<ByteBuffer>());- auto maybe_plaintext = cipher.unwrap(ciphertext.bytes());- if (maybe_plaintext.is_error())- return WebIDL::OperationError::create("Key unwrap failed"_utf16);+bool web_crypto_aes_gcm_decrypt(ReadonlyBytes key, ReadonlyBytes ciphertext, ReadonlyBytes iv, ReadonlyBytes additional_data, ReadonlyBytes tag, void* state, ReceiveEncodedBytes receive)+{+ ::Crypto::Cipher::AESGCMCipher cipher(key);+ auto result = cipher.decrypt(ciphertext, iv, additional_data, tag);+ if (result.is_error()) {+ dbgln("FAILED: {}", result.error());+ return false;+ }+ receive(state, result.value());+ return true;+}- // 3. Return plaintext.- return maybe_plaintext.release_value();+FlatPtr web_crypto_aes_ocb_cipher(bool encrypt, ReadonlyBytes key, ReadonlyBytes input, ReadonlyBytes iv, ReadonlyBytes additional_data, size_t tag_length, void* state, ReceiveEncodedBytes receive)+{+ ::Crypto::Cipher::AESOCBCipher cipher(key);+ auto result = encrypt ? cipher.encrypt(input, iv, additional_data, tag_length) : cipher.decrypt(input, iv, additional_data, tag_length);+ if (result.is_error())+ return Utf16String::formatted("{}", result.error()).into_raw();+ receive(state, result.value());+ return 0;}static_assert(to_underlying(::Crypto::Hash::HashKind::SHA1) == 4);@@ -9456,372 +8135,6 @@ WebIDL::ExceptionOr<Optional<u32>> ChaCha20Poly1305::get_key_length(JS::Realm&,return 256;}-// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-encrypt-WebIDL::ExceptionOr<void> AesOcb::validate_encrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AeadParams const&>(params);-- // 1. If the iv member of normalizedAlgorithm has a length greater than 15 bytes, then throw an OperationError.- if (normalized_algorithm.iv.size() > 15)- return WebIDL::OperationError::create("IV must have a length of 15 bytes"_utf16);-- // NB: Our AES-OCB rejects an empty IV.- if (normalized_algorithm.iv.is_empty())- return WebIDL::OperationError::create("IV must not be empty"_utf16);-- // 2. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.- // If the tagLength member of normalizedAlgorithm is one of 64, 96 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm- // Otherwise: throw an OperationError.- auto constexpr valid_tag_lengths = Array { 64, 96, 128 };- if (normalized_algorithm.tag_length.has_value() && !valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))- return WebIDL::OperationError::create("Invalid tag length"_utf16);-- return {};-}--// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> AesOcb::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)-{- auto const& normalized_algorithm = static_cast<AeadParams const&>(params);-- // NB: Steps 1 and 2 are performed by validate_encrypt_parameters(), which leaves tagLength as the tagLength member- // of normalizedAlgorithm if present, or 128 otherwise.- TRY(validate_encrypt_parameters(params));- size_t tag_length = normalized_algorithm.tag_length.value_or(128);-- // 3. Let additionalData be the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.- auto const& additional_data = normalized_algorithm.additional_data.value_or(ByteBuffer {});-- // 4. Let C be the output that results from performing the OCB-ENCRYPT function described in Section 4.2- // of [RFC7253] using AES as the block cipher,- // using the key represented by [[handle]] internal slot of key as the K input parameter,- // the iv member of normalizedAlgorithm as the N input parameter,- // additionalData as the A input parameter,- // plaintext as the P input parameter,- // and tagLength as the TAGLEN global parameter.- VERIFY(key->handle().has<ByteBuffer>());- ::Crypto::Cipher::AESOCBCipher cipher(key->handle().get<ByteBuffer>());- auto const maybe_ciphertext = cipher.encrypt(- plaintext,- normalized_algorithm.iv,- additional_data,- tag_length);-- if (maybe_ciphertext.is_error())- return WebIDL::OperationError::create(Utf16String::formatted("Encryption failed: {}", maybe_ciphertext.error()));-- // 5. Return C- return maybe_ciphertext.value();-}--// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-decrypt-WebIDL::ExceptionOr<void> AesOcb::validate_decrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AeadParams const&>(params);-- // 1. If the iv member of normalizedAlgorithm has a length greater than 15 bytes, then throw an OperationError.- if (normalized_algorithm.iv.size() > 15)- return WebIDL::OperationError::create("IV must have a length of 15 bytes"_utf16);-- // NB: Our AES-OCB rejects an empty IV.- if (normalized_algorithm.iv.is_empty())- return WebIDL::OperationError::create("IV must not be empty"_utf16);-- // 2. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.- // If the tagLength member of normalizedAlgorithm is one of 64, 96 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm- // Otherwise: throw an OperationError.- auto constexpr valid_tag_lengths = Array { 64, 96, 128 };- if (normalized_algorithm.tag_length.has_value() && !valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))- return WebIDL::OperationError::create("Invalid tag length"_utf16);-- return {};-}--// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> AesOcb::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)-{- auto const& normalized_algorithm = static_cast<AeadParams const&>(params);-- // NB: Steps 1 and 2 are performed by validate_decrypt_parameters(), which leaves tagLength as the tagLength member- // of normalizedAlgorithm if present, or 128 otherwise.- TRY(validate_decrypt_parameters(params));- size_t tag_length = normalized_algorithm.tag_length.value_or(128);-- // 3. If ciphertext has a length less than tagLength bits, then throw an OperationError.- if (ciphertext.size() < tag_length / 8)- return WebIDL::OperationError::create("Ciphertext is less than tagLength bits"_utf16);-- // 4. Let additionalData be the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.- auto const& additional_data = normalized_algorithm.additional_data.value_or(ByteBuffer {});-- // 5. Perform the OCB-DECRYPT function described in Section 4.3 of [RFC7253]- // using AES as the block cipher,- // using the key represented by [[handle]] internal slot of key as the K input parameter,- // the iv member of normalizedAlgorithm as the N input parameter,- // additionalData as the A input parameter,- // ciphertext as the C input parameter,- // and tagLength as the TAGLEN global parameter.- VERIFY(key->handle().has<ByteBuffer>());- ::Crypto::Cipher::AESOCBCipher cipher(key->handle().get<ByteBuffer>());- auto const maybe_plaintext = cipher.decrypt(- ciphertext,- normalized_algorithm.iv,- additional_data,- tag_length);-- // 5. If the result of the algorithm is the indication of authentication failure:- // throw an OperationError- // Otherwise:- // Let plaintext be the resulting plaintext.- if (maybe_plaintext.is_error())- return WebIDL::OperationError::create(Utf16String::formatted("Decryption failed: {}", maybe_plaintext.error()));-- // 6. Return plaintext.- return maybe_plaintext.value();-}--// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-generate-key-WebIDL::ExceptionOr<void> AesOcb::validate_generate_key_parameters(AlgorithmParams const& params) const-{- // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.- auto const bits = static_cast<AesKeyGenParams const&>(params).length;- if (bits != 128 && bits != 192 && bits != 256)- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-OCB key with unusual amount of {} bits", bits));-- return {};-}--// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-generate-key-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesOcb::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages)-{- // 1. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto const& usage : key_usages) {- if (usage != KeyUsage::Encrypt && usage != KeyUsage::Decrypt && usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- // NB: Step 2 is performed by validate_generate_key_parameters().- TRY(validate_generate_key_parameters(params));- auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);- auto const bits = normalized_algorithm.length;-- // 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.- // 4. If the key generation step fails, then throw an OperationError.- auto key_buffer = TRY(generate_random_key(realm.vm(), bits));-- // 5. Let key be a new CryptoKey object representing the generated AES key.- auto key = CryptoKey::create(CryptoKey::InternalKeyData { key_buffer });-- // 6. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 7. Let algorithm be a new AesKeyAlgorithm.- // 8. Set the name attribute of algorithm to "AES-OCB".- // 9. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.- // 10. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-OCB"_utf16, static_cast<u16>(bits) });-- // 11. Set the [[extractable]] internal slot of key to be extractable.- key->set_extractable(extractable);-- // 12. Set the [[usages]] internal slot of key to be the normalized value of usages.- key->set_usages(normalized_key_usages(key_usages));-- // 13. Return key.- return { key };-}--// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> AesOcb::import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages)-{- // 1. Let keyData be the key data to be imported.-- // 2. If usages contains an entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.- for (auto const& usage : usages) {- if (usage != KeyUsage::Encrypt && usage != KeyUsage::Decrypt && usage != KeyUsage::WrapKey && usage != KeyUsage::UnwrapKey) {- return WebIDL::SyntaxError::create(Utf16String::formatted("Invalid key usage '{}'", key_usage_to_string(usage)));- }- }-- ByteBuffer data;-- // 3. If format is "raw-secret":- if (format == KeyFormat::RawSecret) {- // 1. Let data be keyData.- data = move(key_data.get<ByteBuffer>());-- // 2. If the length in bits of data is not 128, 192 or 256 then throw a DataError.- auto length_in_bits = data.size() * 8;- if (length_in_bits != 128 && length_in_bits != 192 && length_in_bits != 256) {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key length '{}' bits (must be either 128, 192, or 256 bits)", length_in_bits));- }- }- // 3. If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. If keyData is a JsonWebKey dictionary:- // Let jwk equal keyData.- // Otherwise:- // Throw a DataError.- if (!key_data.has<JsonWebKey>())- return WebIDL::DataError::create("Invalid JWK key data"_utf16);-- auto const& jwk = key_data.get<JsonWebKey>();-- // 2. If the kty field of jwk is not "oct", then throw a DataError.- if (jwk.kty != "oct"_utf16)- return WebIDL::DataError::create("Invalid key type"_utf16);-- // 3. If jwk does not meet the requirements of Section 6.4 of JSON Web Algorithms [JWA], then throw a DataError.- // NOTE: "k" is already checked in step 4.- if (!jwk.alg.has_value())- return WebIDL::DataError::create("Missing 'alg' field"_utf16);-- // 4. Let data be the byte sequence obtained by decoding the k field of jwk.- data = TRY(parse_jwk_symmetric_key(realm, jwk));-- // 5. -> If data has length 128 bits:- // If the alg field of jwk is present, and is not "A128OCB", then throw a DataError.- // -> If data has length 192 bits:- // If the alg field of jwk is present, and is not "A192OCB", then throw a DataError.- // -> If data has length 256 bits:- // If the alg field of jwk is present, and is not "A256OCB", then throw a DataError.- // -> Otherwise:- // throw a DataError.- auto data_bits = data.size() * 8;- auto const& alg = jwk.alg;- if (data_bits == 128) {- if (alg.has_value() && alg != "A128OCB"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 128 bits, but alg specifies non-128-bit algorithm"_utf16);- } else if (data_bits == 192) {- if (alg.has_value() && alg != "A192OCB"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 192 bits, but alg specifies non-192-bit algorithm"_utf16);- } else if (data_bits == 256) {- if (alg.has_value() && alg != "A256OCB"_utf16)- return WebIDL::DataError::create("Contradictory key size: key has 256 bits, but alg specifies non-256-bit algorithm"_utf16);- } else {- return WebIDL::DataError::create(Utf16String::formatted("Invalid key size: {} bits", data_bits));- }-- // 6. If usages is non-empty and the use field of jwk is present and is not "enc", then throw a DataError.- if (!usages.is_empty() && jwk.use.has_value() && *jwk.use != "enc"_utf16)- return WebIDL::DataError::create("Invalid use field"_utf16);-- // 7. If the key_ops field of jwk is present, and is invalid according to the requirements of JSON Web Key [JWK]- // or does not contain all of the specified usages values, then throw a DataError.- TRY(validate_jwk_key_ops(realm, jwk, usages));-- // 8. If the ext field of jwk is present and has the value false and extractable is true, then throw a DataError.- if (jwk.ext.has_value() && !*jwk.ext && extractable)- return WebIDL::DataError::create("Invalid ext field"_utf16);- }- // 2. Otherwise:- else {- // 1. throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Only raw-secret and jwk formats are supported"_utf16);- }-- auto data_bits = data.size() * 8;-- // 4. Let key be a new CryptoKey object representing an AES key with value data.- auto key = CryptoKey::create(move(data));-- // 5. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 6. Let algorithm be a new AesKeyAlgorithm.- // 7. Set the name attribute of algorithm to "AES-OCB".- // 8. Set the length attribute of algorithm to the length, in bits, of data.- // 9. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::AesKeyAlgorithmData { "AES-OCB"_utf16, static_cast<u16>(data_bits) });-- // 10. Return key.- return key;-}--// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-export-key-WebIDL::ExceptionOr<ExportKeyResult> AesOcb::export_key(JS::Realm& realm, KeyFormat format, GC::Ref<CryptoKey> key)-{-- // 1. If the underlying cryptographic key material represented by the [[handle]] internal slot of key cannot be accessed, then throw an OperationError.- // Note: In our impl this is always accessible-- Optional<ExportKeyResult> result;-- // 2. If format is "raw-secret":- if (format == KeyFormat::RawSecret) {- // 1. Let data be the raw octets of the key represented by [[handle]] internal slot of key.- auto data = key->handle().get<ByteBuffer>();-- // 2. Let result be data.- result = ExportKeyResult { data };- }- // 2. If format is "jwk":- else if (format == KeyFormat::Jwk) {- // 1. Let jwk be a new JsonWebKey dictionary.- JsonWebKey jwk = {};-- // 2. Set the kty attribute of jwk to the string "oct".- jwk.kty = "oct"_utf16;-- // 3. Set the k attribute of jwk to be a string containing the raw octets of the key represented by [[handle]] internal slot of key,- // encoded according to Section 6.4 of JSON Web Algorithms [JWA].- auto const& key_bytes = key->handle().get<ByteBuffer>();- jwk.k = Utf16String::from_utf8(TRY_OR_THROW_OOM(realm.vm(), encode_base64url(key_bytes, AK::OmitPadding::Yes)));-- // 4. -> If the length attribute of key is 128:- // Set the alg attribute of jwk to the string "A128OCB".- // -> If the length attribute of key is 192:- // Set the alg attribute of jwk to the string "A192OCB".- // -> If the length attribute of key is 256:- // Set the alg attribute of jwk to the string "A256OCB".- auto key_bits = key_bytes.size() * 8;- if (key_bits == 128) {- jwk.alg = "A128OCB"_utf16;- } else if (key_bits == 192) {- jwk.alg = "A192OCB"_utf16;- } else if (key_bits == 256) {- jwk.alg = "A256OCB"_utf16;- }-- // 5. Set the key_ops attribute of jwk to the usages attribute of key.- jwk.key_ops = Vector<Utf16String> {};- jwk.key_ops->ensure_capacity(key->internal_usages().size());- for (auto const& usage : key->internal_usages()) {- jwk.key_ops->unchecked_append(Utf16String::from_utf8(key_usage_to_string(usage)));- }-- // 6. Set the ext attribute of jwk to equal the [[extractable]] internal slot of key.- jwk.ext = key->extractable();-- // 7. Let result be jwk.- return ExportKeyResult { move(jwk) };- }- // 2. Otherwise:- else {- // 1. throw a NotSupportedError.- return WebIDL::NotSupportedError::create("Cannot export to unsupported format"_utf16);- }-- // 3. Return result.- return result.release_value();-}--// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-get-key-length-WebIDL::ExceptionOr<Optional<u32>> AesOcb::get_key_length(JS::Realm&, AlgorithmParams const& params)-{- auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);-- // 1. If the length member of normalizedDerivedKeyAlgorithm is not 128, 192 or 256, then throw an OperationError.- auto const length = normalized_algorithm.length;- if (length != 128 && length != 192 && length != 256) {- return WebIDL::OperationError::create(Utf16String::formatted("Invalid key length: {}", length));- }-- // 2. Return the length member of normalizedDerivedKeyAlgorithm.- return length;-}-extern "C" {u32 web_crypto_kmac_default_key_length(::Crypto::Authentication::KMACKind);bool web_crypto_kmac_validate_parameters(u32, OptionalBytes);diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h b/Libraries/LibWeb/Crypto/CryptoAlgorithms.hindex 726c8f74ecc..0ac763ad482 100644--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.h@@ -50,51 +50,6 @@ struct AlgorithmParams {Utf16String name;};-// https://w3c.github.io/webcrypto/#aes-cbc-struct AesCbcParams : public AlgorithmParams {- virtual ~AesCbcParams() override;- AesCbcParams(ByteBuffer iv)- : iv(move(iv))- {- }-- ByteBuffer iv;-- static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);-};--// https://w3c.github.io/webcrypto/#dfn-AesCtrParams-struct AesCtrParams : public AlgorithmParams {- virtual ~AesCtrParams() override;- AesCtrParams(ByteBuffer counter, u8 length)- : counter(move(counter))- , length(length)- {- }-- ByteBuffer counter;- u8 length;-- static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);-};--// https://w3c.github.io/webcrypto/#dfn-AesGcmParams-struct AesGcmParams : public AlgorithmParams {- virtual ~AesGcmParams() override;- AesGcmParams(ByteBuffer iv, Optional<ByteBuffer> additional_data, Optional<u8> tag_length)- : iv(move(iv))- , additional_data(move(additional_data))- , tag_length(tag_length)- {- }-- ByteBuffer iv;- Optional<ByteBuffer> additional_data;- Optional<u8> tag_length;-- static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);-};-// https://w3c.github.io/webcrypto/#dfn-RsaKeyGenParamsstruct RsaKeyGenParams : public AlgorithmParams {virtual ~RsaKeyGenParams() override;@@ -197,34 +152,6 @@ struct EcKeyGenParams : public AlgorithmParams {static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);};-// https://w3c.github.io/webcrypto/#dfn-AesKeyGenParams-struct AesKeyGenParams : public AlgorithmParams {- virtual ~AesKeyGenParams() override;-- AesKeyGenParams(u16 length)- : length(length)- {- }-- u16 length;-- static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);-};--// https://w3c.github.io/webcrypto/#dfn-AesDerivedKeyParams-struct AesDerivedKeyParams : public AlgorithmParams {- virtual ~AesDerivedKeyParams() override;-- AesDerivedKeyParams(u16 length)- : length(length)- {- }-- u16 length;-- static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::Realm&, JS::Value);-};-class AlgorithmMethods {public:AK_ALLOC_WITH_KMALLOC;@@ -374,80 +301,6 @@ private:RSASSAPKCS1() = default;};-class AesCbc : public AlgorithmMethods {-public:- virtual WebIDL::ExceptionOr<ByteBuffer> encrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;- virtual WebIDL::ExceptionOr<ByteBuffer> decrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;- virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;-- virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;- virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;- virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;-- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesCbc); }--private:- AesCbc() = default;-};--class AesCtr : public AlgorithmMethods {-public:- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;- virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;- virtual WebIDL::ExceptionOr<ByteBuffer> encrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;- virtual WebIDL::ExceptionOr<ByteBuffer> decrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;-- virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;- virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;- virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;-- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesCtr); }--private:- AesCtr() = default;-};--class AesGcm : public AlgorithmMethods {-public:- virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;- virtual WebIDL::ExceptionOr<ByteBuffer> encrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;- virtual WebIDL::ExceptionOr<ByteBuffer> decrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;-- virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;- virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;- virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;-- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesGcm); }--private:- AesGcm() = default;-};--class AesKw : public AlgorithmMethods {-public:- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;- virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;- virtual WebIDL::ExceptionOr<ByteBuffer> wrap_key(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;- virtual WebIDL::ExceptionOr<ByteBuffer> unwrap_key(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;-- virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;-- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesKw); }--private:- AesKw() = default;-};-class ECDSA : public AlgorithmMethods {public:virtual WebIDL::ExceptionOr<ByteBuffer> sign(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;@@ -667,25 +520,6 @@ private:ChaCha20Poly1305() = default;};-class AesOcb : public AlgorithmMethods {-public:- virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;- virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;- virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;- virtual WebIDL::ExceptionOr<ByteBuffer> encrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;- virtual WebIDL::ExceptionOr<ByteBuffer> decrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;- virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;-- virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;- virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;- virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;-- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesOcb); }--private:- AesOcb() = default;-};-Vector<KeyUsage> normalized_key_usages(ReadonlySpan<KeyUsage>);ErrorOr<Utf16String> base64_url_uint_encode(::Crypto::UnsignedBigInteger);diff --git a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp b/Libraries/LibWeb/Crypto/SubtleCrypto.cppindex b9d31f2f78f..e2150eb0166 100644--- a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp@@ -21,10 +21,6 @@ enum class AlgorithmId : u8 {RSAOAEP,ECDSA,ECDH,- AesCtr,- AesCbc,- AesGcm,- AesKw,X25519,X448,ED25519,@@ -32,7 +28,6 @@ enum class AlgorithmId : u8 {MLDSA,MLKEM,ChaCha20Poly1305,- AesOcb,};enum class ParameterId : u8 {@@ -45,11 +40,6 @@ enum class ParameterId : u8 {EcKeyGenParams,EcKeyImportParams,EcdhKeyDeriveParams,- AesCtrParams,- AesKeyGenParams,- AesDerivedKeyParams,- AesCbcParams,- AesGcmParams,Ed448Params,ContextParams,AeadParams,@@ -231,14 +221,6 @@ JSCompletion web_crypto_algorithm_from_value(JS::Realm* realm_pointer, Algorithmreturn ECDSA::create();case AlgorithmId::ECDH:return ECDH::create();- case AlgorithmId::AesCtr:- return AesCtr::create();- case AlgorithmId::AesCbc:- return AesCbc::create();- case AlgorithmId::AesGcm:- return AesGcm::create();- case AlgorithmId::AesKw:- return AesKw::create();case AlgorithmId::X25519:return X25519::create();case AlgorithmId::X448:@@ -253,8 +235,6 @@ JSCompletion web_crypto_algorithm_from_value(JS::Realm* realm_pointer, Algorithmreturn MLKEM::create();case AlgorithmId::ChaCha20Poly1305:return ChaCha20Poly1305::create();- case AlgorithmId::AesOcb:- return AesOcb::create();}VERIFY_NOT_REACHED();}();@@ -279,16 +259,6 @@ JSCompletion web_crypto_algorithm_from_value(JS::Realm* realm_pointer, Algorithmreturn EcKeyImportParams::from_value(realm, object);case ParameterId::EcdhKeyDeriveParams:return EcdhKeyDeriveParams::from_value(realm, object);- case ParameterId::AesCtrParams:- return AesCtrParams::from_value(realm, object);- case ParameterId::AesKeyGenParams:- return AesKeyGenParams::from_value(realm, object);- case ParameterId::AesDerivedKeyParams:- return AesDerivedKeyParams::from_value(realm, object);- case ParameterId::AesCbcParams:- return AesCbcParams::from_value(realm, object);- case ParameterId::AesGcmParams:- return AesGcmParams::from_value(realm, object);case ParameterId::Ed448Params:return Ed448Params::from_value(realm, object);case ParameterId::ContextParams:diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.pyindex bf0076b0e17..dfbd4065610 100755--- a/Meta/Linters/check_libweb_realm_mentions.py+++ b/Meta/Linters/check_libweb_realm_mentions.py@@ -46,7 +46,7 @@ ALLOWED_REALM_MENTIONS = {"CookieStore": (2, 19, "cookie-store async callbacks and promise/value materialization still thread realms"),"Crypto": (5,- 283,+ 213,"WebCrypto algorithms still use realms for buffer/key/promise materialization and for normalizing algorithm ""dictionaries. The native CryptoKey wrapper ""uses the caller's realm and wrapper world",