RSA.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Span.h>
  9. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  10. #include <LibCrypto/NumberTheory/ModularFunctions.h>
  11. #include <LibCrypto/PK/Code/EMSA_PSS.h>
  12. #include <LibCrypto/PK/PK.h>
  13. namespace Crypto {
  14. namespace PK {
  15. template<typename Integer = UnsignedBigInteger>
  16. class RSAPublicKey {
  17. public:
  18. RSAPublicKey(Integer n, Integer e)
  19. : m_modulus(move(n))
  20. , m_public_exponent(move(e))
  21. , m_length(m_modulus.trimmed_length() * sizeof(u32))
  22. {
  23. }
  24. RSAPublicKey()
  25. : m_modulus(0)
  26. , m_public_exponent(0)
  27. {
  28. }
  29. Integer const& modulus() const { return m_modulus; }
  30. Integer const& public_exponent() const { return m_public_exponent; }
  31. size_t length() const { return m_length; }
  32. void set_length(size_t length) { m_length = length; }
  33. void set(Integer n, Integer e)
  34. {
  35. m_modulus = move(n);
  36. m_public_exponent = move(e);
  37. m_length = (m_modulus.trimmed_length() * sizeof(u32));
  38. }
  39. private:
  40. Integer m_modulus;
  41. Integer m_public_exponent;
  42. size_t m_length { 0 };
  43. };
  44. template<typename Integer = UnsignedBigInteger>
  45. class RSAPrivateKey {
  46. public:
  47. RSAPrivateKey(Integer n, Integer d, Integer e)
  48. : m_modulus(move(n))
  49. , m_private_exponent(move(d))
  50. , m_public_exponent(move(e))
  51. , m_length(m_modulus.trimmed_length() * sizeof(u32))
  52. {
  53. }
  54. RSAPrivateKey() = default;
  55. Integer const& modulus() const { return m_modulus; }
  56. Integer const& private_exponent() const { return m_private_exponent; }
  57. Integer const& public_exponent() const { return m_public_exponent; }
  58. size_t length() const { return m_length; }
  59. void set_length(size_t length) { m_length = length; }
  60. void set(Integer n, Integer d, Integer e)
  61. {
  62. m_modulus = move(n);
  63. m_private_exponent = move(d);
  64. m_public_exponent = move(e);
  65. m_length = m_modulus.trimmed_length() * sizeof(u32);
  66. }
  67. private:
  68. Integer m_modulus;
  69. Integer m_private_exponent;
  70. Integer m_public_exponent;
  71. size_t m_length { 0 };
  72. };
  73. template<typename PubKey, typename PrivKey>
  74. struct RSAKeyPair {
  75. PubKey public_key;
  76. PrivKey private_key;
  77. };
  78. using IntegerType = UnsignedBigInteger;
  79. class RSA : public PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>> {
  80. template<typename T>
  81. friend class RSA_EMSA_PSS;
  82. public:
  83. using KeyPairType = RSAKeyPair<PublicKeyType, PrivateKeyType>;
  84. static KeyPairType parse_rsa_key(ReadonlyBytes der);
  85. static KeyPairType generate_key_pair(size_t bits = 256)
  86. {
  87. IntegerType e { 65537 }; // :P
  88. IntegerType p, q;
  89. IntegerType lambda;
  90. do {
  91. p = NumberTheory::random_big_prime(bits / 2);
  92. q = NumberTheory::random_big_prime(bits / 2);
  93. lambda = NumberTheory::LCM(p.minus(1), q.minus(1));
  94. dbgln("checking combination p={}, q={}, lambda={}", p, q, lambda.length());
  95. } while (!(NumberTheory::GCD(e, lambda) == 1));
  96. auto n = p.multiplied_by(q);
  97. auto d = NumberTheory::ModularInverse(e, lambda);
  98. dbgln("Your keys are Pub(n={}, e={}) and Priv(n={}, d={})", n, e, n, d);
  99. RSAKeyPair<PublicKeyType, PrivateKeyType> keys {
  100. { n, e },
  101. { n, d, e }
  102. };
  103. keys.public_key.set_length(bits / 2 / 8);
  104. keys.private_key.set_length(bits / 2 / 8);
  105. return keys;
  106. }
  107. RSA(IntegerType n, IntegerType d, IntegerType e)
  108. {
  109. m_public_key.set(n, e);
  110. m_private_key.set(n, d, e);
  111. }
  112. RSA(PublicKeyType& pubkey, PrivateKeyType& privkey)
  113. : PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>>(pubkey, privkey)
  114. {
  115. }
  116. RSA(ByteBuffer const& publicKeyPEM, ByteBuffer const& privateKeyPEM)
  117. {
  118. import_public_key(publicKeyPEM);
  119. import_private_key(privateKeyPEM);
  120. }
  121. RSA(StringView privKeyPEM)
  122. {
  123. import_private_key(privKeyPEM.bytes());
  124. m_public_key.set(m_private_key.modulus(), m_private_key.public_exponent());
  125. }
  126. // create our own keys
  127. RSA()
  128. {
  129. auto pair = generate_key_pair();
  130. m_public_key = pair.public_key;
  131. m_private_key = pair.private_key;
  132. }
  133. virtual void encrypt(ReadonlyBytes in, Bytes& out) override;
  134. virtual void decrypt(ReadonlyBytes in, Bytes& out) override;
  135. virtual void sign(ReadonlyBytes in, Bytes& out) override;
  136. virtual void verify(ReadonlyBytes in, Bytes& out) override;
  137. #ifndef KERNEL
  138. virtual DeprecatedString class_name() const override
  139. {
  140. return "RSA";
  141. }
  142. #endif
  143. virtual size_t output_size() const override
  144. {
  145. return m_public_key.length();
  146. }
  147. void import_public_key(ReadonlyBytes, bool pem = true);
  148. void import_private_key(ReadonlyBytes, bool pem = true);
  149. PrivateKeyType const& private_key() const { return m_private_key; }
  150. PublicKeyType const& public_key() const { return m_public_key; }
  151. };
  152. template<typename HashFunction>
  153. class RSA_EMSA_PSS {
  154. public:
  155. RSA_EMSA_PSS(RSA& rsa)
  156. : m_rsa(rsa)
  157. {
  158. }
  159. void sign(ReadonlyBytes in, Bytes& out);
  160. VerificationConsistency verify(ReadonlyBytes in);
  161. private:
  162. EMSA_PSS<HashFunction, HashFunction::DigestSize> m_emsa_pss;
  163. RSA m_rsa;
  164. };
  165. class RSA_PKCS1_EME : public RSA {
  166. public:
  167. // forward all constructions to RSA
  168. template<typename... Args>
  169. RSA_PKCS1_EME(Args... args)
  170. : RSA(args...)
  171. {
  172. }
  173. ~RSA_PKCS1_EME() = default;
  174. virtual void encrypt(ReadonlyBytes in, Bytes& out) override;
  175. virtual void decrypt(ReadonlyBytes in, Bytes& out) override;
  176. virtual void sign(ReadonlyBytes, Bytes&) override;
  177. virtual void verify(ReadonlyBytes, Bytes&) override;
  178. #ifndef KERNEL
  179. virtual DeprecatedString class_name() const override
  180. {
  181. return "RSA_PKCS1-EME";
  182. }
  183. #endif
  184. virtual size_t output_size() const override
  185. {
  186. return m_public_key.length();
  187. }
  188. };
  189. }
  190. }