CryptoAlgorithms.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/EnumBits.h>
  8. #include <AK/String.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibJS/Heap/GCPtr.h>
  11. #include <LibWeb/Bindings/SubtleCryptoPrototype.h>
  12. #include <LibWeb/Crypto/CryptoBindings.h>
  13. #include <LibWeb/Crypto/CryptoKey.h>
  14. #include <LibWeb/WebIDL/Buffers.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. namespace Web::Crypto {
  17. using KeyDataType = Variant<JS::Handle<WebIDL::BufferSource>, Bindings::JsonWebKey>;
  18. using AlgorithmIdentifier = Variant<JS::Handle<JS::Object>, String>;
  19. using HashAlgorithmIdentifier = AlgorithmIdentifier;
  20. // https://w3c.github.io/webcrypto/#algorithm-overview
  21. struct AlgorithmParams {
  22. String name;
  23. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  24. };
  25. // https://w3c.github.io/webcrypto/#pbkdf2-params
  26. struct PBKDF2Params : public AlgorithmParams {
  27. JS::Handle<WebIDL::BufferSource> salt;
  28. u32 iterations;
  29. HashAlgorithmIdentifier hash;
  30. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  31. };
  32. class AlgorithmMethods {
  33. public:
  34. virtual ~AlgorithmMethods();
  35. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&)
  36. {
  37. return WebIDL::NotSupportedError::create(m_realm, "digest is not supported"_fly_string);
  38. }
  39. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&)
  40. {
  41. return WebIDL::NotSupportedError::create(m_realm, "importKey is not supported"_fly_string);
  42. }
  43. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AlgorithmMethods(realm)); }
  44. protected:
  45. explicit AlgorithmMethods(JS::Realm& realm)
  46. : m_realm(realm)
  47. {
  48. }
  49. JS::Realm& m_realm;
  50. };
  51. class PBKDF2 : public AlgorithmMethods {
  52. public:
  53. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  54. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new PBKDF2(realm)); }
  55. private:
  56. explicit PBKDF2(JS::Realm& realm)
  57. : AlgorithmMethods(realm)
  58. {
  59. }
  60. };
  61. class SHA : public AlgorithmMethods {
  62. public:
  63. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&) override;
  64. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new SHA(realm)); }
  65. private:
  66. explicit SHA(JS::Realm& realm)
  67. : AlgorithmMethods(realm)
  68. {
  69. }
  70. };
  71. }