CryptoAlgorithms.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 NamedCurve = String;
  21. using KeyDataType = Variant<JS::Handle<WebIDL::BufferSource>, Bindings::JsonWebKey>;
  22. // https://w3c.github.io/webcrypto/#algorithm-overview
  23. struct AlgorithmParams {
  24. virtual ~AlgorithmParams();
  25. explicit AlgorithmParams(String name)
  26. : name(move(name))
  27. {
  28. }
  29. String name;
  30. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  31. };
  32. // https://w3c.github.io/webcrypto/#hkdf-params
  33. struct HKDFParams : public AlgorithmParams {
  34. virtual ~HKDFParams() override;
  35. HKDFParams(String name, HashAlgorithmIdentifier hash, ByteBuffer salt, ByteBuffer info)
  36. : AlgorithmParams(move(name))
  37. , hash(move(hash))
  38. , salt(move(salt))
  39. , info(move(info))
  40. {
  41. }
  42. HashAlgorithmIdentifier hash;
  43. ByteBuffer salt;
  44. ByteBuffer info;
  45. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  46. };
  47. // https://w3c.github.io/webcrypto/#pbkdf2-params
  48. struct PBKDF2Params : public AlgorithmParams {
  49. virtual ~PBKDF2Params() override;
  50. PBKDF2Params(String name, ByteBuffer salt, u32 iterations, HashAlgorithmIdentifier hash)
  51. : AlgorithmParams(move(name))
  52. , salt(move(salt))
  53. , iterations(iterations)
  54. , hash(move(hash))
  55. {
  56. }
  57. ByteBuffer salt;
  58. u32 iterations;
  59. HashAlgorithmIdentifier hash;
  60. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  61. };
  62. // https://w3c.github.io/webcrypto/#dfn-RsaKeyGenParams
  63. struct RsaKeyGenParams : public AlgorithmParams {
  64. virtual ~RsaKeyGenParams() override;
  65. RsaKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent)
  66. : AlgorithmParams(move(name))
  67. , modulus_length(modulus_length)
  68. , public_exponent(move(public_exponent))
  69. {
  70. }
  71. u32 modulus_length;
  72. // NOTE that the raw data is going to be in Big Endian u8[] format
  73. ::Crypto::UnsignedBigInteger public_exponent;
  74. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  75. };
  76. // https://w3c.github.io/webcrypto/#dfn-RsaHashedKeyGenParams
  77. struct RsaHashedKeyGenParams : public RsaKeyGenParams {
  78. virtual ~RsaHashedKeyGenParams() override;
  79. RsaHashedKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent, HashAlgorithmIdentifier hash)
  80. : RsaKeyGenParams(move(name), modulus_length, move(public_exponent))
  81. , hash(move(hash))
  82. {
  83. }
  84. HashAlgorithmIdentifier hash;
  85. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  86. };
  87. // https://w3c.github.io/webcrypto/#dfn-RsaHashedImportParams
  88. struct RsaHashedImportParams : public AlgorithmParams {
  89. virtual ~RsaHashedImportParams() override;
  90. RsaHashedImportParams(String name, HashAlgorithmIdentifier hash)
  91. : AlgorithmParams(move(name))
  92. , hash(move(hash))
  93. {
  94. }
  95. HashAlgorithmIdentifier hash;
  96. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  97. };
  98. // https://w3c.github.io/webcrypto/#dfn-RsaOaepParams
  99. struct RsaOaepParams : public AlgorithmParams {
  100. virtual ~RsaOaepParams() override;
  101. RsaOaepParams(String name, ByteBuffer label)
  102. : AlgorithmParams(move(name))
  103. , label(move(label))
  104. {
  105. }
  106. ByteBuffer label;
  107. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  108. };
  109. // https://w3c.github.io/webcrypto/#dfn-EcdsaParams
  110. struct EcdsaParams : public AlgorithmParams {
  111. virtual ~EcdsaParams() override;
  112. EcdsaParams(String name, HashAlgorithmIdentifier hash)
  113. : AlgorithmParams(move(name))
  114. , hash(move(hash))
  115. {
  116. }
  117. HashAlgorithmIdentifier hash;
  118. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  119. };
  120. // https://w3c.github.io/webcrypto/#dfn-EcKeyGenParams
  121. struct EcKeyGenParams : public AlgorithmParams {
  122. virtual ~EcKeyGenParams() override;
  123. EcKeyGenParams(String name, NamedCurve named_curve)
  124. : AlgorithmParams(move(name))
  125. , named_curve(move(named_curve))
  126. {
  127. }
  128. NamedCurve named_curve;
  129. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  130. };
  131. class AlgorithmMethods {
  132. public:
  133. virtual ~AlgorithmMethods();
  134. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&)
  135. {
  136. return WebIDL::NotSupportedError::create(m_realm, "encrypt is not supported"_string);
  137. }
  138. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&)
  139. {
  140. return WebIDL::NotSupportedError::create(m_realm, "decrypt is not supported"_string);
  141. }
  142. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> sign(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&)
  143. {
  144. return WebIDL::NotSupportedError::create(m_realm, "sign is not supported"_string);
  145. }
  146. virtual WebIDL::ExceptionOr<JS::Value> verify(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&, ByteBuffer const&)
  147. {
  148. return WebIDL::NotSupportedError::create(m_realm, "verify is not supported"_string);
  149. }
  150. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&)
  151. {
  152. return WebIDL::NotSupportedError::create(m_realm, "digest is not supported"_string);
  153. }
  154. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>)
  155. {
  156. return WebIDL::NotSupportedError::create(m_realm, "deriveBits is not supported"_string);
  157. }
  158. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&)
  159. {
  160. return WebIDL::NotSupportedError::create(m_realm, "importKey is not supported"_string);
  161. }
  162. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&)
  163. {
  164. return WebIDL::NotSupportedError::create(m_realm, "generateKey is not supported"_string);
  165. }
  166. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>)
  167. {
  168. return WebIDL::NotSupportedError::create(m_realm, "exportKey is not supported"_string);
  169. }
  170. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&)
  171. {
  172. return WebIDL::NotSupportedError::create(m_realm, "getKeyLength is not supported"_string);
  173. }
  174. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AlgorithmMethods(realm)); }
  175. protected:
  176. explicit AlgorithmMethods(JS::Realm& realm)
  177. : m_realm(realm)
  178. {
  179. }
  180. JS::NonnullGCPtr<JS::Realm> m_realm;
  181. };
  182. class RSAOAEP : public AlgorithmMethods {
  183. public:
  184. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  185. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  186. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  187. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  188. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
  189. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new RSAOAEP(realm)); }
  190. private:
  191. explicit RSAOAEP(JS::Realm& realm)
  192. : AlgorithmMethods(realm)
  193. {
  194. }
  195. };
  196. class HKDF : public AlgorithmMethods {
  197. public:
  198. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  199. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>) override;
  200. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
  201. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new HKDF(realm)); }
  202. private:
  203. explicit HKDF(JS::Realm& realm)
  204. : AlgorithmMethods(realm)
  205. {
  206. }
  207. };
  208. class PBKDF2 : public AlgorithmMethods {
  209. public:
  210. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  211. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>) override;
  212. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
  213. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new PBKDF2(realm)); }
  214. private:
  215. explicit PBKDF2(JS::Realm& realm)
  216. : AlgorithmMethods(realm)
  217. {
  218. }
  219. };
  220. class SHA : public AlgorithmMethods {
  221. public:
  222. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&) override;
  223. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new SHA(realm)); }
  224. private:
  225. explicit SHA(JS::Realm& realm)
  226. : AlgorithmMethods(realm)
  227. {
  228. }
  229. };
  230. class ECDSA : public AlgorithmMethods {
  231. public:
  232. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> sign(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  233. virtual WebIDL::ExceptionOr<JS::Value> verify(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&, ByteBuffer const&) override;
  234. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  235. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new ECDSA(realm)); }
  236. private:
  237. explicit ECDSA(JS::Realm& realm)
  238. : AlgorithmMethods(realm)
  239. {
  240. }
  241. };
  242. class ED25519 : public AlgorithmMethods {
  243. public:
  244. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> sign(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  245. virtual WebIDL::ExceptionOr<JS::Value> verify(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&, ByteBuffer const&) override;
  246. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  247. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new ED25519(realm)); }
  248. private:
  249. explicit ED25519(JS::Realm& realm)
  250. : AlgorithmMethods(realm)
  251. {
  252. }
  253. };
  254. ErrorOr<String> base64_url_uint_encode(::Crypto::UnsignedBigInteger);
  255. WebIDL::ExceptionOr<::Crypto::UnsignedBigInteger> base64_url_uint_decode(JS::Realm&, String const& base64_url_string);
  256. }