CryptoAlgorithms.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. * Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/EnumBits.h>
  9. #include <AK/String.h>
  10. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/Heap/GCPtr.h>
  13. #include <LibWeb/Bindings/SubtleCryptoPrototype.h>
  14. #include <LibWeb/Crypto/CryptoBindings.h>
  15. #include <LibWeb/Crypto/CryptoKey.h>
  16. #include <LibWeb/WebIDL/Buffers.h>
  17. #include <LibWeb/WebIDL/ExceptionOr.h>
  18. namespace Web::Crypto {
  19. using AlgorithmIdentifier = Variant<JS::Handle<JS::Object>, String>;
  20. using NamedCurve = String;
  21. using KeyDataType = Variant<JS::Handle<WebIDL::BufferSource>, Bindings::JsonWebKey>;
  22. struct HashAlgorithmIdentifier : public AlgorithmIdentifier {
  23. using AlgorithmIdentifier::AlgorithmIdentifier;
  24. JS::ThrowCompletionOr<String> name(JS::VM& vm) const
  25. {
  26. auto value = visit(
  27. [](String const& name) -> JS::ThrowCompletionOr<String> { return name; },
  28. [&](JS::Handle<JS::Object> const& obj) -> JS::ThrowCompletionOr<String> {
  29. auto name_property = TRY(obj->get("name"));
  30. return name_property.to_string(vm);
  31. });
  32. return value;
  33. }
  34. };
  35. // https://w3c.github.io/webcrypto/#algorithm-overview
  36. struct AlgorithmParams {
  37. virtual ~AlgorithmParams();
  38. explicit AlgorithmParams(String name)
  39. : name(move(name))
  40. {
  41. }
  42. String name;
  43. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  44. };
  45. // https://w3c.github.io/webcrypto/#aes-cbc
  46. struct AesCbcParams : public AlgorithmParams {
  47. virtual ~AesCbcParams() override;
  48. AesCbcParams(String name, ByteBuffer iv)
  49. : AlgorithmParams(move(name))
  50. , iv(move(iv))
  51. {
  52. }
  53. ByteBuffer iv;
  54. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  55. };
  56. // https://w3c.github.io/webcrypto/#dfn-AesCtrParams
  57. struct AesCtrParams : public AlgorithmParams {
  58. virtual ~AesCtrParams() override;
  59. AesCtrParams(String name, ByteBuffer counter, u8 length)
  60. : AlgorithmParams(move(name))
  61. , counter(move(counter))
  62. , length(length)
  63. {
  64. }
  65. ByteBuffer counter;
  66. u8 length;
  67. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  68. };
  69. // https://w3c.github.io/webcrypto/#dfn-AesGcmParams
  70. struct AesGcmParams : public AlgorithmParams {
  71. virtual ~AesGcmParams() override;
  72. AesGcmParams(String name, ByteBuffer iv, Optional<ByteBuffer> additional_data, Optional<u8> tag_length)
  73. : AlgorithmParams(move(name))
  74. , iv(move(iv))
  75. , additional_data(move(additional_data))
  76. , tag_length(tag_length)
  77. {
  78. }
  79. ByteBuffer iv;
  80. Optional<ByteBuffer> additional_data;
  81. Optional<u8> tag_length;
  82. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  83. };
  84. // https://w3c.github.io/webcrypto/#hkdf-params
  85. struct HKDFParams : public AlgorithmParams {
  86. virtual ~HKDFParams() override;
  87. HKDFParams(String name, HashAlgorithmIdentifier hash, ByteBuffer salt, ByteBuffer info)
  88. : AlgorithmParams(move(name))
  89. , hash(move(hash))
  90. , salt(move(salt))
  91. , info(move(info))
  92. {
  93. }
  94. HashAlgorithmIdentifier hash;
  95. ByteBuffer salt;
  96. ByteBuffer info;
  97. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  98. };
  99. // https://w3c.github.io/webcrypto/#pbkdf2-params
  100. struct PBKDF2Params : public AlgorithmParams {
  101. virtual ~PBKDF2Params() override;
  102. PBKDF2Params(String name, ByteBuffer salt, u32 iterations, HashAlgorithmIdentifier hash)
  103. : AlgorithmParams(move(name))
  104. , salt(move(salt))
  105. , iterations(iterations)
  106. , hash(move(hash))
  107. {
  108. }
  109. ByteBuffer salt;
  110. u32 iterations;
  111. HashAlgorithmIdentifier hash;
  112. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  113. };
  114. // https://w3c.github.io/webcrypto/#dfn-RsaKeyGenParams
  115. struct RsaKeyGenParams : public AlgorithmParams {
  116. virtual ~RsaKeyGenParams() override;
  117. RsaKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent)
  118. : AlgorithmParams(move(name))
  119. , modulus_length(modulus_length)
  120. , public_exponent(move(public_exponent))
  121. {
  122. }
  123. u32 modulus_length;
  124. // NOTE that the raw data is going to be in Big Endian u8[] format
  125. ::Crypto::UnsignedBigInteger public_exponent;
  126. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  127. };
  128. // https://w3c.github.io/webcrypto/#dfn-RsaHashedKeyGenParams
  129. struct RsaHashedKeyGenParams : public RsaKeyGenParams {
  130. virtual ~RsaHashedKeyGenParams() override;
  131. RsaHashedKeyGenParams(String name, u32 modulus_length, ::Crypto::UnsignedBigInteger public_exponent, HashAlgorithmIdentifier hash)
  132. : RsaKeyGenParams(move(name), modulus_length, move(public_exponent))
  133. , hash(move(hash))
  134. {
  135. }
  136. HashAlgorithmIdentifier hash;
  137. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  138. };
  139. // https://w3c.github.io/webcrypto/#dfn-RsaHashedImportParams
  140. struct RsaHashedImportParams : public AlgorithmParams {
  141. virtual ~RsaHashedImportParams() override;
  142. RsaHashedImportParams(String name, HashAlgorithmIdentifier hash)
  143. : AlgorithmParams(move(name))
  144. , hash(move(hash))
  145. {
  146. }
  147. HashAlgorithmIdentifier hash;
  148. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  149. };
  150. // https://w3c.github.io/webcrypto/#dfn-RsaOaepParams
  151. struct RsaOaepParams : public AlgorithmParams {
  152. virtual ~RsaOaepParams() override;
  153. RsaOaepParams(String name, ByteBuffer label)
  154. : AlgorithmParams(move(name))
  155. , label(move(label))
  156. {
  157. }
  158. ByteBuffer label;
  159. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  160. };
  161. // https://w3c.github.io/webcrypto/#dfn-EcdsaParams
  162. struct EcdsaParams : public AlgorithmParams {
  163. virtual ~EcdsaParams() override;
  164. EcdsaParams(String name, HashAlgorithmIdentifier hash)
  165. : AlgorithmParams(move(name))
  166. , hash(move(hash))
  167. {
  168. }
  169. HashAlgorithmIdentifier hash;
  170. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  171. };
  172. // https://w3c.github.io/webcrypto/#dfn-EcKeyGenParams
  173. struct EcKeyGenParams : public AlgorithmParams {
  174. virtual ~EcKeyGenParams() override;
  175. EcKeyGenParams(String name, NamedCurve named_curve)
  176. : AlgorithmParams(move(name))
  177. , named_curve(move(named_curve))
  178. {
  179. }
  180. NamedCurve named_curve;
  181. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  182. };
  183. // https://w3c.github.io/webcrypto/#dfn-AesKeyGenParams
  184. struct AesKeyGenParams : public AlgorithmParams {
  185. virtual ~AesKeyGenParams() override;
  186. AesKeyGenParams(String name, u16 length)
  187. : AlgorithmParams(move(name))
  188. , length(length)
  189. {
  190. }
  191. u16 length;
  192. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  193. };
  194. // https://w3c.github.io/webcrypto/#dfn-AesDerivedKeyParams
  195. struct AesDerivedKeyParams : public AlgorithmParams {
  196. virtual ~AesDerivedKeyParams() override;
  197. AesDerivedKeyParams(String name, u16 length)
  198. : AlgorithmParams(move(name))
  199. , length(length)
  200. {
  201. }
  202. u16 length;
  203. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  204. };
  205. class AlgorithmMethods {
  206. public:
  207. virtual ~AlgorithmMethods();
  208. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&)
  209. {
  210. return WebIDL::NotSupportedError::create(m_realm, "encrypt is not supported"_string);
  211. }
  212. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&)
  213. {
  214. return WebIDL::NotSupportedError::create(m_realm, "decrypt is not supported"_string);
  215. }
  216. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> sign(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&)
  217. {
  218. return WebIDL::NotSupportedError::create(m_realm, "sign is not supported"_string);
  219. }
  220. virtual WebIDL::ExceptionOr<JS::Value> verify(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&, ByteBuffer const&)
  221. {
  222. return WebIDL::NotSupportedError::create(m_realm, "verify is not supported"_string);
  223. }
  224. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&)
  225. {
  226. return WebIDL::NotSupportedError::create(m_realm, "digest is not supported"_string);
  227. }
  228. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>)
  229. {
  230. return WebIDL::NotSupportedError::create(m_realm, "deriveBits is not supported"_string);
  231. }
  232. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&)
  233. {
  234. return WebIDL::NotSupportedError::create(m_realm, "importKey is not supported"_string);
  235. }
  236. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&)
  237. {
  238. return WebIDL::NotSupportedError::create(m_realm, "generateKey is not supported"_string);
  239. }
  240. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>)
  241. {
  242. return WebIDL::NotSupportedError::create(m_realm, "exportKey is not supported"_string);
  243. }
  244. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&)
  245. {
  246. return WebIDL::NotSupportedError::create(m_realm, "getKeyLength is not supported"_string);
  247. }
  248. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AlgorithmMethods(realm)); }
  249. protected:
  250. explicit AlgorithmMethods(JS::Realm& realm)
  251. : m_realm(realm)
  252. {
  253. }
  254. JS::NonnullGCPtr<JS::Realm> m_realm;
  255. };
  256. class RSAOAEP : public AlgorithmMethods {
  257. public:
  258. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  259. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  260. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  261. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  262. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
  263. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new RSAOAEP(realm)); }
  264. private:
  265. explicit RSAOAEP(JS::Realm& realm)
  266. : AlgorithmMethods(realm)
  267. {
  268. }
  269. };
  270. class AesCbc : public AlgorithmMethods {
  271. public:
  272. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  273. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  274. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  275. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  276. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
  277. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
  278. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AesCbc(realm)); }
  279. private:
  280. explicit AesCbc(JS::Realm& realm)
  281. : AlgorithmMethods(realm)
  282. {
  283. }
  284. };
  285. class AesCtr : public AlgorithmMethods {
  286. public:
  287. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  288. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
  289. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
  290. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  291. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  292. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  293. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AesCtr(realm)); }
  294. private:
  295. explicit AesCtr(JS::Realm& realm)
  296. : AlgorithmMethods(realm)
  297. {
  298. }
  299. };
  300. class AesGcm : public AlgorithmMethods {
  301. public:
  302. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
  303. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  304. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
  305. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  306. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AesGcm(realm)); }
  307. private:
  308. explicit AesGcm(JS::Realm& realm)
  309. : AlgorithmMethods(realm)
  310. {
  311. }
  312. };
  313. class HKDF : public AlgorithmMethods {
  314. public:
  315. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  316. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>) override;
  317. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
  318. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new HKDF(realm)); }
  319. private:
  320. explicit HKDF(JS::Realm& realm)
  321. : AlgorithmMethods(realm)
  322. {
  323. }
  324. };
  325. class PBKDF2 : public AlgorithmMethods {
  326. public:
  327. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  328. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>) override;
  329. virtual WebIDL::ExceptionOr<JS::Value> get_key_length(AlgorithmParams const&) override;
  330. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new PBKDF2(realm)); }
  331. private:
  332. explicit PBKDF2(JS::Realm& realm)
  333. : AlgorithmMethods(realm)
  334. {
  335. }
  336. };
  337. class SHA : public AlgorithmMethods {
  338. public:
  339. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> digest(AlgorithmParams const&, ByteBuffer const&) override;
  340. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new SHA(realm)); }
  341. private:
  342. explicit SHA(JS::Realm& realm)
  343. : AlgorithmMethods(realm)
  344. {
  345. }
  346. };
  347. class ECDSA : public AlgorithmMethods {
  348. public:
  349. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> sign(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  350. virtual WebIDL::ExceptionOr<JS::Value> verify(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&, ByteBuffer const&) override;
  351. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  352. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new ECDSA(realm)); }
  353. private:
  354. explicit ECDSA(JS::Realm& realm)
  355. : AlgorithmMethods(realm)
  356. {
  357. }
  358. };
  359. class ED25519 : public AlgorithmMethods {
  360. public:
  361. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> sign(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
  362. virtual WebIDL::ExceptionOr<JS::Value> verify(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&, ByteBuffer const&) override;
  363. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  364. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new ED25519(realm)); }
  365. private:
  366. explicit ED25519(JS::Realm& realm)
  367. : AlgorithmMethods(realm)
  368. {
  369. }
  370. };
  371. class X25519 : public AlgorithmMethods {
  372. public:
  373. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>) override;
  374. virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
  375. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> import_key(AlgorithmParams const&, Bindings::KeyFormat, CryptoKey::InternalKeyData, bool, Vector<Bindings::KeyUsage> const&) override;
  376. virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
  377. static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new X25519(realm)); }
  378. private:
  379. explicit X25519(JS::Realm& realm)
  380. : AlgorithmMethods(realm)
  381. {
  382. }
  383. };
  384. struct EcdhKeyDerivePrams : public AlgorithmParams {
  385. virtual ~EcdhKeyDerivePrams() override;
  386. EcdhKeyDerivePrams(String name, CryptoKey& public_key)
  387. : AlgorithmParams(move(name))
  388. , public_key(public_key)
  389. {
  390. }
  391. JS::NonnullGCPtr<CryptoKey> public_key;
  392. static JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> from_value(JS::VM&, JS::Value);
  393. };
  394. ErrorOr<String> base64_url_uint_encode(::Crypto::UnsignedBigInteger);
  395. WebIDL::ExceptionOr<ByteBuffer> base64_url_bytes_decode(JS::Realm&, String const& base64_url_string);
  396. WebIDL::ExceptionOr<::Crypto::UnsignedBigInteger> base64_url_uint_decode(JS::Realm&, String const& base64_url_string);
  397. }