Truly independent web browser
LibWeb: Port WebCrypto ChaCha20-Poly1305 to Rust
Shannon Booth4c4fd4c30192
diff --git a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rsindex a745cf288ef..a4b677bc704 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/crypto_algorithms.rs@@ -366,7 +366,6 @@ pub(crate) enum AlgorithmId {ED448,MlDsa,MlKem,- ChaCha20Poly1305,}#[derive(Clone, Copy)]@@ -387,7 +386,6 @@ pub(crate) enum ParameterId {EcdhKeyDeriveParams,Ed448Params,ContextParams,- AeadParams,}#[derive(Clone, Trace)]@@ -6108,6 +6106,410 @@ impl AlgorithmMethods for AesOcb {}}+#[derive(Clone, Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct ChaCha20Poly1305 {+ parameters: Option<AeadParams>,+}++impl ChaCha20Poly1305 {+ pub(crate) fn from_value(vm: &Vm, object: Gc<Object>, has_aead_parameters: bool) -> WebIdlResult<Self> {+ let parameters = if has_aead_parameters {+ Some(AeadParams::from_value(vm, object)?)+ } else {+ None+ };+ Ok(Self { parameters })+ }+}++unsafe extern "C" {+ fn web_crypto_chacha20_poly1305_cipher(+ encrypt: bool,+ key: Span<'_, u8>,+ iv: Span<'_, u8>,+ input: Span<'_, u8>,+ additional_data: Span<'_, u8>,+ state: *mut c_void,+ receive: ReceiveBytes,+ ) -> usize;+}++impl AlgorithmMethods for ChaCha20Poly1305 {+ // https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-encrypt+ fn validate_encrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let normalized_algorithm = self.parameters.as_ref().expect("the operation uses AEAD parameters");++ // 1. If the iv member of normalizedAlgorithm does not have a length of 12 bytes, then throw an OperationError.+ if normalized_algorithm.iv.as_slice().len() != 12 {+ return Err(dom_error("OperationError", b"IV must have a length of 12 bytes"));+ }++ // 2. If the tagLength member of normalizedAlgorithm is present and is not 128, then throw an OperationError.+ if normalized_algorithm.tag_length.is_some_and(|length| length != 128) {+ return Err(dom_error("OperationError", b"tagLength must be 128"));+ }++ Ok(())+ }++ // https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-encrypt+ fn encrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, plaintext: &Bytes) -> WebIdlResult<Bytes> {+ let normalized_algorithm = self.parameters.as_ref().expect("the operation uses AEAD parameters");++ // NB: Steps 1 and 2 are performed by validate_encrypt_parameters().+ self.validate_encrypt_parameters(cx.realm())?;++ // 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 ciphertext be the output that results from performing the AEAD_CHACHA20_POLY1305 encryption algorithm+ // described in Section 2.8 of [RFC8439], using the key represented by [[handle]] internal slot of key as the+ // key input parameter, the iv member of normalizedAlgorithm as the nonce input parameter, plaintext as the+ // plaintext input parameter, and additionalData as the additional authenticated data (AAD) input parameter.+ let mut ciphertext: Option<Bytes> = None;+ // SAFETY: The inputs and output slot live for the call.+ let maybe_ciphertext = unsafe {+ web_crypto_chacha20_poly1305_cipher(+ true,+ Span::new(key.byte_handle_bytes()),+ Span::new(normalized_algorithm.iv.as_slice()),+ Span::new(plaintext.as_slice()),+ Span::new(additional_data),+ 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 ciphertext.+ Ok(ciphertext.expect("LibCrypto returns the result"))+ }++ // https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-decrypt+ fn validate_decrypt_parameters(&self, _: Gc<Realm>) -> WebIdlResult<()> {+ let normalized_algorithm = self.parameters.as_ref().expect("the operation uses AEAD parameters");++ // 1. If the iv member of normalizedAlgorithm does not have a length of 12 bytes, then throw an OperationError.+ if normalized_algorithm.iv.as_slice().len() != 12 {+ return Err(dom_error("OperationError", b"IV must have a length of 12 bytes"));+ }++ // 2. If the tagLength member of normalizedAlgorithm is present and is not 128, then throw an OperationError.+ if normalized_algorithm.tag_length.is_some_and(|length| length != 128) {+ return Err(dom_error("OperationError", b"tagLength must be 128"));+ }++ Ok(())+ }++ // https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-decrypt+ fn decrypt(&self, cx: &CallContext, key: Ref<CryptoKey>, ciphertext: &Bytes) -> WebIdlResult<Bytes> {+ let normalized_algorithm = self.parameters.as_ref().expect("the operation uses AEAD parameters");++ // NB: Steps 1 and 2 are performed by validate_decrypt_parameters().+ self.validate_decrypt_parameters(cx.realm())?;++ // 3. If ciphertext has a length less than 128 bits, then throw an OperationError.+ if ciphertext.as_slice().len() < 16 {+ return Err(dom_error("OperationError", b"Ciphertext must be at least 128 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 AEAD_CHACHA20_POLY1305 decryption algorithm described in Section 2.8 of [RFC8439], using the key+ // represented by [[handle]] internal slot of key as the key input parameter, the iv member of normalizedAlgorithm+ // as the nonce input parameter, ciphertext as the ciphertext input parameter, and additionalData as the additional+ // authenticated data (AAD) input parameter.+ let mut plaintext: Option<Bytes> = None;+ // SAFETY: The inputs and output slot live for the call.+ let maybe_plaintext = unsafe {+ web_crypto_chacha20_poly1305_cipher(+ false,+ Span::new(key.byte_handle_bytes()),+ Span::new(normalized_algorithm.iv.as_slice()),+ Span::new(ciphertext.as_slice()),+ Span::new(additional_data),+ core::ptr::from_mut(&mut plaintext).cast(),+ receive_bytes,+ )+ };++ // 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/#chacha20-poly1305-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)]),+ ));+ }+ }++ // 2. Generate a 256-bit key.+ let key_buffer = generate_random_key(cx.vm(), 256)?;++ // 3. 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.++ // 4. Let key be a new CryptoKey object representing the generated key.+ let key = CryptoKey::from_byte_handle(cx.vm(), &key_buffer);++ // 5. Set the [[type]] internal slot of key to "secret".+ key.set_type(KeyType::Secret);++ // 6. Let algorithm be a new KeyAlgorithm.+ // 7. Set the name attribute of algorithm to "ChaCha20-Poly1305".+ // 8. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Key {+ name: Utf16String::from_ascii(b"ChaCha20-Poly1305"),+ });++ // 9. Set the [[extractable]] internal slot of key to be extractable.+ key.set_extractable(extractable);++ // 10. Set the [[usages]] internal slot of key to be the normalized value of usages.+ key.set_usages(normalized_key_usages(key_usages));++ // 11. Return key.+ Ok(CryptoKeyOrCryptoKeyPair::CryptoKey(key))+ }++ // https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-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 256 then throw a DataError.+ if data.as_slice().len() != 32 {+ return Err(dom_error("DataError", b"Key data must be 256 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.+ // NB: 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 the alg field of jwk is present, and is not "C20P", then throw a DataError.+ if jwk+ .alg+ .as_ref()+ .is_some_and(|alg| *alg != Utf16String::from_ascii(b"C20P"))+ {+ return Err(dom_error("DataError", b"Invalid alg field"));+ }++ // 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",+ ));+ }++ // 4. Let key be a new CryptoKey object representing a 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 KeyAlgorithm.+ // 7. Set the name attribute of algorithm to "ChaCha20-Poly1305".+ // 8. Set the [[algorithm]] internal slot of key to algorithm.+ key.set_algorithm(InternalAlgorithmData::Key {+ name: Utf16String::from_ascii(b"ChaCha20-Poly1305"),+ });++ // 9. Return key.+ Ok(key)+ }++ // https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-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. Set the alg attribute of jwk to the string "C20P".+ jwk.alg = Some(Utf16String::from_ascii(b"C20P"));++ // 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/#chacha20-poly1305-operations-get-key-length+ fn get_key_length(&self, _: Gc<Realm>, _: &AlgorithmParams) -> WebIdlResult<Option<u32>> {+ // 1. Return 256.+ Ok(Some(256))+ }+}+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 3d9c723ed53..290c44bb604 100644--- a/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/crypto/subtle_crypto.rs@@ -32,9 +32,9 @@ use crate::bindings::promise::{use crate::bindings::{CallContext, Exception, MustNotThrow, WebIdlResult};use crate::crypto::crypto_algorithms::{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,+ Argon2Params, Bytes, CShakeParams, ChaCha20Poly1305, 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,@@ -107,6 +107,8 @@ enum RegisteredAlgorithm {AesGcm(AesParameterType),AesKw(AesParameterType),AesOcb(AesParameterType),+ ChaCha20Poly1305,+ ChaCha20Poly1305Aead,CShake,}@@ -136,6 +138,7 @@ enum AlgorithmImplementation {AesGcm(AesGcm),AesKw(AesKw),AesOcb(AesOcb),+ ChaCha20Poly1305(ChaCha20Poly1305),CShake(CShakeParams),}@@ -164,6 +167,7 @@ impl Deref for AlgorithmImplementation {Self::AesGcm(algorithm) => algorithm,Self::AesKw(algorithm) => algorithm,Self::AesOcb(algorithm) => algorithm,+ Self::ChaCha20Poly1305(algorithm) => algorithm,Self::CShake(algorithm) => algorithm,}}@@ -390,6 +394,12 @@ pub(crate) fn normalize_an_algorithm(RegisteredAlgorithm::AesOcb(parameter_type) => {AlgorithmImplementation::AesOcb(AesOcb::from_value(vm, *algorithm, parameter_type)?)}+ RegisteredAlgorithm::ChaCha20Poly1305 => {+ AlgorithmImplementation::ChaCha20Poly1305(ChaCha20Poly1305::from_value(vm, *algorithm, false)?)+ }+ RegisteredAlgorithm::ChaCha20Poly1305Aead => {+ AlgorithmImplementation::ChaCha20Poly1305(ChaCha20Poly1305::from_value(vm, *algorithm, true)?)+ }RegisteredAlgorithm::CShake => AlgorithmImplementation::CShake(CShakeParams::from_value(vm, *algorithm)?),};@@ -3452,37 +3462,37 @@ fn supported_algorithms() -> &'static SupportedAlgorithmsMap {&mut internal_object,"encrypt","ChaCha20-Poly1305",- RegisteredAlgorithm::Cpp(AlgorithmId::ChaCha20Poly1305, ParameterId::AeadParams),+ RegisteredAlgorithm::ChaCha20Poly1305Aead,);define_an_algorithm(&mut internal_object,"decrypt","ChaCha20-Poly1305",- RegisteredAlgorithm::Cpp(AlgorithmId::ChaCha20Poly1305, ParameterId::AeadParams),+ RegisteredAlgorithm::ChaCha20Poly1305Aead,);define_an_algorithm(&mut internal_object,"generateKey","ChaCha20-Poly1305",- RegisteredAlgorithm::Cpp(AlgorithmId::ChaCha20Poly1305, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::ChaCha20Poly1305,);define_an_algorithm(&mut internal_object,"importKey","ChaCha20-Poly1305",- RegisteredAlgorithm::Cpp(AlgorithmId::ChaCha20Poly1305, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::ChaCha20Poly1305,);define_an_algorithm(&mut internal_object,"exportKey","ChaCha20-Poly1305",- RegisteredAlgorithm::Cpp(AlgorithmId::ChaCha20Poly1305, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::ChaCha20Poly1305,);define_an_algorithm(&mut internal_object,"get key length","ChaCha20-Poly1305",- RegisteredAlgorithm::Cpp(AlgorithmId::ChaCha20Poly1305, ParameterId::AlgorithmParams),+ RegisteredAlgorithm::ChaCha20Poly1305,);// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-registrationdiff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cppindex 82786740fc8..b8fa1ca248a 100644--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp@@ -11,7 +11,6 @@#include <AK/Base64.h>#include <AK/HashTable.h>#include <AK/Memory.h>-#include <AK/Random.h>#include <LibCrypto/ASN1/ASN1.h>#include <LibCrypto/ASN1/Constants.h>#include <LibCrypto/ASN1/DER.h>@@ -374,14 +373,6 @@ static WebIDL::ExceptionOr<::Crypto::PK::RSAPublicKey> parse_jwk_rsa_public_key(return ::Crypto::PK::RSAPublicKey(move(n), move(e));}-static WebIDL::ExceptionOr<ByteBuffer> parse_jwk_symmetric_key(JS::Realm& realm, JsonWebKey const& jwk)-{- if (!jwk.k.has_value()) {- return WebIDL::DataError::create("JWK has no 'k' field"_utf16);- }- return base64_url_bytes_decode(realm, *jwk.k);-}-// https://www.rfc-editor.org/rfc/rfc7517#section-4.3static WebIDL::ExceptionOr<void> validate_jwk_key_ops(JS::Realm&, JsonWebKey const& jwk, Vector<KeyUsage> const& usages){@@ -431,26 +422,6 @@ static WebIDL::ExceptionOr<void> validate_jwk_key_ops(JS::Realm&, JsonWebKey conreturn {};}-// NB: Keeps only the bytes that hold the first length_in_bits bits of data, and zeroes the unused low-order bits of-// the last byte when the length is not a whole number of bytes.-static void keep_first_bits(ByteBuffer& data, size_t length_in_bits)-{- auto length_in_bytes = ceil_div(length_in_bits, static_cast<size_t>(8));- VERIFY(length_in_bytes <= data.size());- data.resize(length_in_bytes);-- if (auto remainder_bits = length_in_bits % 8; remainder_bits != 0)- data[length_in_bytes - 1] &= ~(0xff >> remainder_bits);-}--static WebIDL::ExceptionOr<ByteBuffer> generate_random_key(JS::VM& vm, u32 const size_in_bits)-{- auto key_buffer = TRY_OR_THROW_OOM(vm, ByteBuffer::create_uninitialized(ceil_div(size_in_bits, 8u)));- fill_with_random(key_buffer);- keep_first_bits(key_buffer, size_in_bits);- return key_buffer;-}-AlgorithmParams::~AlgorithmParams() = default;RsaKeyGenParams::~RsaKeyGenParams() = default;@@ -7826,313 +7797,17 @@ FlatPtr web_crypto_cshake_digest(::Crypto::Hash::SHAKEKind kind, ReadonlyBytes dreturn 0;}-AeadParams::~AeadParams() = default;--JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AeadParams::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 AeadParams { iv, maybe_additional_data, maybe_tag_length });-}--// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-encrypt-WebIDL::ExceptionOr<void> ChaCha20Poly1305::validate_encrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AeadParams const&>(params);-- // 1. If the iv member of normalizedAlgorithm does not have a length of 12 bytes, then throw an OperationError.- if (normalized_algorithm.iv.size() != 12)- return WebIDL::OperationError::create("IV must have a length of 12 bytes"_utf16);-- // 2. If the tagLength member of normalizedAlgorithm is present and is not 128, then throw an OperationError.- if (normalized_algorithm.tag_length.has_value() && normalized_algorithm.tag_length.value() != 128)- return WebIDL::OperationError::create("tagLength must be 128"_utf16);-- return {};-}--// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::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().- TRY(validate_encrypt_parameters(params));-- // 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 ciphertext be the output that results from performing the AEAD_CHACHA20_POLY1305 encryption algorithm- // described in Section 2.8 of [RFC8439], using the key represented by [[handle]] internal slot of key as the- // key input parameter, the iv member of normalizedAlgorithm as the nonce input parameter, plaintext as the- // plaintext input parameter, and additionalData as the additional authenticated data (AAD) input parameter.- VERIFY(key->handle().has<ByteBuffer>());- auto const maybe_ciphertext = ::Crypto::Cipher::ChaCha20Poly1305::encrypt(- key->handle().get<ByteBuffer>(),- normalized_algorithm.iv,- plaintext,- additional_data);-- if (maybe_ciphertext.is_error())- return WebIDL::OperationError::create(Utf16String::formatted("Encryption failed: {}", maybe_ciphertext.error()));-- // 5. Return ciphertext.- return maybe_ciphertext.value();-}--// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-decrypt-WebIDL::ExceptionOr<void> ChaCha20Poly1305::validate_decrypt_parameters(AlgorithmParams const& params) const-{- auto const& normalized_algorithm = static_cast<AeadParams const&>(params);-- // 1. If the iv member of normalizedAlgorithm does not have a length of 12 bytes, then throw an OperationError.- if (normalized_algorithm.iv.size() != 12)- return WebIDL::OperationError::create("IV must have a length of 12 bytes"_utf16);-- // 2. If the tagLength member of normalizedAlgorithm is present and is not 128, then throw an OperationError.- if (normalized_algorithm.tag_length.has_value() && normalized_algorithm.tag_length.value() != 128)- return WebIDL::OperationError::create("tagLength must be 128"_utf16);-- return {};-}--// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::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().- TRY(validate_decrypt_parameters(params));-- // 3. If ciphertext has a length less than 128 bits, then throw an OperationError.- if (ciphertext.size() < 16)- return WebIDL::OperationError::create("Ciphertext must be at least 128 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 AEAD_CHACHA20_POLY1305 decryption algorithm described in Section 2.8 of [RFC8439], using the key- // represented by [[handle]] internal slot of key as the key input parameter, the iv member of normalizedAlgorithm- // as the nonce input parameter, ciphertext as the ciphertext input parameter, and additionalData as the additional- // authenticated data (AAD) input parameter.- VERIFY(key->handle().has<ByteBuffer>());- auto const maybe_plaintext = ::Crypto::Cipher::ChaCha20Poly1305::decrypt(- key->handle().get<ByteBuffer>(),- normalized_algorithm.iv,- ciphertext,- additional_data);-- // 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/#chacha20-poly1305-operations-generate-key-WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ChaCha20Poly1305::generate_key(JS::Realm& realm, AlgorithmParams const&, 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)));- }- }-- // 2. Generate a 256-bit key.- auto key_buffer = TRY(generate_random_key(realm.vm(), 256));-- // 3. 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.-- // 4. Let key be a new CryptoKey object representing the generated key.- auto key = CryptoKey::create(CryptoKey::InternalKeyData { key_buffer });-- // 5. Set the [[type]] internal slot of key to "secret".- key->set_type(KeyType::Secret);-- // 6. Let algorithm be a new KeyAlgorithm.- // 7. Set the name attribute of algorithm to "ChaCha20-Poly1305".- // 8. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::KeyAlgorithmData { "ChaCha20-Poly1305"_utf16 });-- // 9. Set the [[extractable]] internal slot of key to be extractable.- key->set_extractable(extractable);-- // 10. Set the [[usages]] internal slot of key to be the normalized value of usages.- key->set_usages(normalized_key_usages(key_usages));-- // 11. Return key.- return { key };-}--// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ChaCha20Poly1305::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 256 then throw a DataError.- if (data.size() != 32)- return WebIDL::DataError::create("Key data must be 256 bits"_utf16);- }- // 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.- // NB: 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 the alg field of jwk is present, and is not "C20P", then throw a DataError.- if (jwk.alg.has_value() && jwk.alg.value() != "C20P"_utf16)- return WebIDL::DataError::create("Invalid alg field"_utf16);-- // 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);- }-- // 4. Let key be a new CryptoKey object representing a 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 KeyAlgorithm.- // 7. Set the name attribute of algorithm to "ChaCha20-Poly1305".- // 8. Set the [[algorithm]] internal slot of key to algorithm.- key->set_algorithm(CryptoKey::KeyAlgorithmData { "ChaCha20-Poly1305"_utf16 });-- // 9. Return key.- return key;-}--// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-export-key-WebIDL::ExceptionOr<ExportKeyResult> ChaCha20Poly1305::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)));+extern "C" FlatPtr web_crypto_chacha20_poly1305_cipher(bool, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, ReadonlyBytes, void*, ReceiveEncodedBytes);- // 4. Set the alg attribute of jwk to the string "C20P".- jwk.alg = "C20P"_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/#chacha20-poly1305-operations-get-key-length-WebIDL::ExceptionOr<Optional<u32>> ChaCha20Poly1305::get_key_length(JS::Realm&, AlgorithmParams const&)+FlatPtr web_crypto_chacha20_poly1305_cipher(bool encrypt, ReadonlyBytes key, ReadonlyBytes iv, ReadonlyBytes input, ReadonlyBytes additional_data, void* state, ReceiveEncodedBytes receive){- // 1. Return 256.- return 256;+ auto result = encrypt+ ? ::Crypto::Cipher::ChaCha20Poly1305::encrypt(key, iv, input, additional_data)+ : ::Crypto::Cipher::ChaCha20Poly1305::decrypt(key, iv, input, additional_data);+ if (result.is_error())+ return Utf16String::formatted("{}", result.error()).into_raw();+ receive(state, result.value());+ return 0;}extern "C" {diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h b/Libraries/LibWeb/Crypto/CryptoAlgorithms.hindex 0ac763ad482..43ccf41a44b 100644--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.h@@ -486,40 +486,6 @@ using ContextParams = Ed448Params;// https://wicg.github.io/webcrypto-modern-algos/#cshake-params// https://wicg.github.io/webcrypto-modern-algos/#dfn-AeadParams// NOTE: The AeadParams dictionary is identical to the AesGcmParams-struct AeadParams : public AlgorithmParams {- virtual ~AeadParams() override;- AeadParams(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);-};--class ChaCha20Poly1305 : 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<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> 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<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;-- static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ChaCha20Poly1305); }--private:- ChaCha20Poly1305() = 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 e2150eb0166..59722216ce7 100644--- a/Libraries/LibWeb/Crypto/SubtleCrypto.cpp+++ b/Libraries/LibWeb/Crypto/SubtleCrypto.cpp@@ -27,7 +27,6 @@ enum class AlgorithmId : u8 {ED448,MLDSA,MLKEM,- ChaCha20Poly1305,};enum class ParameterId : u8 {@@ -42,7 +41,6 @@ enum class ParameterId : u8 {EcdhKeyDeriveParams,Ed448Params,ContextParams,- AeadParams,};struct RawRsaOtherPrimesInfo {@@ -233,8 +231,6 @@ JSCompletion web_crypto_algorithm_from_value(JS::Realm* realm_pointer, Algorithmreturn MLDSA::create();case AlgorithmId::MLKEM:return MLKEM::create();- case AlgorithmId::ChaCha20Poly1305:- return ChaCha20Poly1305::create();}VERIFY_NOT_REACHED();}();@@ -263,8 +259,6 @@ JSCompletion web_crypto_algorithm_from_value(JS::Realm* realm_pointer, Algorithmreturn Ed448Params::from_value(realm, object);case ParameterId::ContextParams:return ContextParams::from_value(realm, object);- case ParameterId::AeadParams:- return AeadParams::from_value(realm, object);}VERIFY_NOT_REACHED();}();diff --git a/Meta/Linters/check_libweb_realm_mentions.py b/Meta/Linters/check_libweb_realm_mentions.pyindex dfbd4065610..b4b04312ce2 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,- 213,+ 198,"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",