LibWeb: Implement and test SubtleCrypto interface for HKDF operations

This fixes several hundred if not thousands of WPT tests:
https://wpt.live/WebCryptoAPI/derive_bits_keys/hkdf.https.any.html?1-1000
This commit is contained in:
Ben Wiederhake 2024-10-20 06:10:36 +02:00 committed by Andrew Kaster
parent 6072ae5bae
commit f670c68ded
Notes: github-actions[bot] 2024-10-23 18:21:54 +00:00
3 changed files with 167 additions and 0 deletions

View file

@ -37,6 +37,24 @@ struct AlgorithmParams {
static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
};
// https://w3c.github.io/webcrypto/#hkdf-params
struct HKDFParams : public AlgorithmParams {
virtual ~HKDFParams() override;
HKDFParams(String name, HashAlgorithmIdentifier hash, ByteBuffer salt, ByteBuffer info)
: AlgorithmParams(move(name))
, hash(move(hash))
, salt(move(salt))
, info(move(info))
{
}
HashAlgorithmIdentifier hash;
ByteBuffer salt;
ByteBuffer info;
static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
};
// https://w3c.github.io/webcrypto/#pbkdf2-params
struct PBKDF2Params : public AlgorithmParams {
virtual ~PBKDF2Params() override;
@ -232,6 +250,21 @@ private:
}
};
class HKDF : public AlgorithmMethods {
public:
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>) override;
virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new HKDF(realm)); }
private:
explicit HKDF(JS::Realm& realm)
: AlgorithmMethods(realm)
{
}
};
class PBKDF2 : public AlgorithmMethods {
public:
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;