CryptoAlgorithms.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <LibCrypto/BigInt/UnsignedBigInteger.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Heap/GCPtr.h>
  12. #include <LibWeb/Bindings/SubtleCryptoPrototype.h>
  13. #include <LibWeb/Crypto/CryptoBindings.h>
  14. #include <LibWeb/Crypto/CryptoKey.h>
  15. #include <LibWeb/WebIDL/Buffers.h>
  16. #include <LibWeb/WebIDL/ExceptionOr.h>
  17. namespace Web::Crypto {
  18. using AlgorithmIdentifier = Variant<JS::Handle<JS::Object>, String>;
  19. using HashAlgorithmIdentifier = AlgorithmIdentifier;
  20. using KeyDataType = Variant<JS::Handle<WebIDL::BufferSource>, Bindings::JsonWebKey>;
  21. // https://w3c.github.io/webcrypto/#algorithm-overview
  22. struct AlgorithmParams {
  23. String name;
  24. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  25. };
  26. // https://w3c.github.io/webcrypto/#pbkdf2-params
  27. struct PBKDF2Params : public AlgorithmParams {
  28. JS::Handle<WebIDL::BufferSource> salt;
  29. u32 iterations;
  30. HashAlgorithmIdentifier hash;
  31. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  32. };
  33. // https://w3c.github.io/webcrypto/#dfn-RsaKeyGenParams
  34. struct RsaKeyGenParams : public AlgorithmParams {
  35. u32 modulus_length;
  36. // NOTE that the raw data is going to be in Big Endian u8[] format
  37. ::Crypto::UnsignedBigInteger public_exponent;
  38. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  39. };
  40. // https://w3c.github.io/webcrypto/#dfn-RsaHashedKeyGenParams
  41. struct RsaHashedKeyGenParams : public RsaKeyGenParams {
  42. HashAlgorithmIdentifier hash;
  43. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  44. };
  45. class AlgorithmMethods {
  46. public:
  47. virtual ~AlgorithmMethods();
  48. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&)
  49. {
  50. return WebIDL::NotSupportedError::create(m_realm, "digest is not supported"_fly_string);
  51. }
  52. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&)
  53. {
  54. return WebIDL::NotSupportedError::create(m_realm, "importKey is not supported"_fly_string);
  55. }
  56. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&)
  57. {
  58. return WebIDL::NotSupportedError::create(m_realm, "generateKey is not supported"_fly_string);
  59. }
  60. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AlgorithmMethods(realm)); }
  61. protected:
  62. explicit AlgorithmMethods(JS::Realm& realm)
  63. : m_realm(realm)
  64. {
  65. }
  66. JS::Realm& m_realm;
  67. };
  68. class RSAOAEP : public AlgorithmMethods {
  69. public:
  70. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  71. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new RSAOAEP(realm)); }
  72. private:
  73. explicit RSAOAEP(JS::Realm& realm)
  74. : AlgorithmMethods(realm)
  75. {
  76. }
  77. };
  78. class PBKDF2 : public AlgorithmMethods {
  79. public:
  80. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  81. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new PBKDF2(realm)); }
  82. private:
  83. explicit PBKDF2(JS::Realm& realm)
  84. : AlgorithmMethods(realm)
  85. {
  86. }
  87. };
  88. class SHA : public AlgorithmMethods {
  89. public:
  90. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&) override;
  91. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new SHA(realm)); }
  92. private:
  93. explicit SHA(JS::Realm& realm)
  94. : AlgorithmMethods(realm)
  95. {
  96. }
  97. };
  98. }