Skip to content

Commit 42f55c7

Browse files
tete17gmta
authored andcommitted
LibWeb: Implement Encapsulate(Key|Bits) dictionaries
These are needed structure for the encapsulate(Key|Bits) operations.
1 parent 1d6a64b commit 42f55c7

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,32 @@ static WebIDL::ExceptionOr<ByteBuffer> generate_random_key(JS::VM& vm, u16 const
274274
return key_buffer;
275275
}
276276

277+
JS::ThrowCompletionOr<GC::Ref<JS::Object>> EncapsulatedKey::to_object(JS::Realm& realm)
278+
{
279+
auto object = JS::Object::create(realm, realm.intrinsics().object_prototype());
280+
281+
if (shared_key.has_value())
282+
TRY(object->create_data_property("shared_key"_utf16_fly_string, shared_key.value()));
283+
284+
if (ciphertext.has_value())
285+
TRY(object->create_data_property("ciphertext"_utf16_fly_string, JS::ArrayBuffer::create(realm, ciphertext.value())));
286+
287+
return object;
288+
}
289+
290+
JS::ThrowCompletionOr<GC::Ref<JS::Object>> EncapsulatedBits::to_object(JS::Realm& realm)
291+
{
292+
auto object = JS::Object::create(realm, realm.intrinsics().object_prototype());
293+
294+
if (shared_key.has_value())
295+
TRY(object->create_data_property("shared_key"_utf16_fly_string, JS::ArrayBuffer::create(realm, shared_key.value())));
296+
297+
if (ciphertext.has_value())
298+
TRY(object->create_data_property("ciphertext"_utf16_fly_string, JS::ArrayBuffer::create(realm, ciphertext.value())));
299+
300+
return object;
301+
}
302+
277303
AlgorithmParams::~AlgorithmParams() = default;
278304

279305
JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AlgorithmParams::from_value(JS::VM&, JS::Value)

Libraries/LibWeb/Crypto/CryptoAlgorithms.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ using AlgorithmIdentifier = Variant<GC::Root<JS::Object>, String>;
2626
using NamedCurve = String;
2727
using KeyDataType = Variant<GC::Root<WebIDL::BufferSource>, Bindings::JsonWebKey>;
2828

29+
// https://wicg.github.io/webcrypto-modern-algos/#encapsulation
30+
struct EncapsulatedKey {
31+
Optional<GC::Root<CryptoKey>> shared_key;
32+
Optional<ByteBuffer> ciphertext;
33+
34+
JS::ThrowCompletionOr<GC::Ref<JS::Object>> to_object(JS::Realm&);
35+
};
36+
37+
// https://wicg.github.io/webcrypto-modern-algos/#encapsulation
38+
struct EncapsulatedBits {
39+
Optional<ByteBuffer> shared_key;
40+
Optional<ByteBuffer> ciphertext;
41+
42+
JS::ThrowCompletionOr<GC::Ref<JS::Object>> to_object(JS::Realm&);
43+
};
44+
2945
struct HashAlgorithmIdentifier : public AlgorithmIdentifier {
3046
using AlgorithmIdentifier::AlgorithmIdentifier;
3147

Libraries/LibWeb/Crypto/SubtleCrypto.idl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ dictionary JsonWebKey {
4242
DOMString k;
4343
};
4444

45+
// https://wicg.github.io/webcrypto-modern-algos/#encapsulation
46+
dictionary EncapsulatedKey {
47+
CryptoKey sharedKey;
48+
ArrayBuffer ciphertext;
49+
};
50+
51+
// https://wicg.github.io/webcrypto-modern-algos/#encapsulation
52+
dictionary EncapsulatedBits {
53+
ArrayBuffer sharedKey;
54+
ArrayBuffer ciphertext;
55+
};
56+
4557
// https://w3c.github.io/webcrypto/#subtlecrypto-interface
4658
[SecureContext,Exposed=(Window,Worker)]
4759
interface SubtleCrypto {

0 commit comments

Comments
 (0)