Explorar el Código

LibWeb: Implement KeyAlgorithms for big-endian

Dennis Camera hace 1 año
padre
commit
90a2dcfde1
Se han modificado 1 ficheros con 13 adiciones y 10 borrados
  1. 13 10
      Userland/Libraries/LibWeb/Crypto/KeyAlgorithms.cpp

+ 13 - 10
Userland/Libraries/LibWeb/Crypto/KeyAlgorithms.cpp

@@ -90,8 +90,6 @@ void RsaKeyAlgorithm::visit_edges(Visitor& visitor)
 
 WebIDL::ExceptionOr<void> RsaKeyAlgorithm::set_public_exponent(::Crypto::UnsignedBigInteger exponent)
 {
-    static_assert(AK::HostIsLittleEndian, "This code assumes a little endian host");
-
     auto& realm = this->realm();
     auto& vm = this->vm();
 
@@ -99,17 +97,22 @@ WebIDL::ExceptionOr<void> RsaKeyAlgorithm::set_public_exponent(::Crypto::Unsigne
 
     bool const remove_leading_zeroes = true;
     auto data_size = exponent.export_data(bytes.span(), remove_leading_zeroes);
-    auto data_slice = bytes.bytes().slice(bytes.size() - data_size, data_size);
+    auto data_slice_be = bytes.bytes().slice(bytes.size() - data_size, data_size);
 
     // The BigInteger typedef from the WebCrypto spec requires the bytes in the Uint8Array be ordered in Big Endian
 
-    Vector<u8, 32> byte_swapped_data;
-    byte_swapped_data.ensure_capacity(data_size);
-    for (size_t i = 0; i < data_size; ++i)
-        byte_swapped_data.append(data_slice[data_size - i - 1]);
-
-    m_public_exponent = TRY(JS::Uint8Array::create(realm, byte_swapped_data.size()));
-    m_public_exponent->viewed_array_buffer()->buffer().overwrite(0, byte_swapped_data.data(), byte_swapped_data.size());
+    if constexpr (AK::HostIsLittleEndian) {
+        Vector<u8, 32> data_slice_le;
+        data_slice_le.ensure_capacity(data_size);
+        for (size_t i = 0; i < data_size; ++i) {
+            data_slice_le.append(data_slice_be[data_size - i - 1]);
+        }
+        m_public_exponent = TRY(JS::Uint8Array::create(realm, data_slice_le.size()));
+        m_public_exponent->viewed_array_buffer()->buffer().overwrite(0, data_slice_le.data(), data_slice_le.size());
+    } else {
+        m_public_exponent = TRY(JS::Uint8Array::create(realm, data_slice_be.size()));
+        m_public_exponent->viewed_array_buffer()->buffer().overwrite(0, data_slice_be.data(), data_slice_be.size());
+    }
 
     return {};
 }