CryptoAlgorithms.h 22 KB

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