rsa.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* RSA asymmetric public-key algorithm [RFC3447]
  3. *
  4. * Copyright (c) 2015, Intel Corporation
  5. * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/mpi.h>
  9. #include <crypto/internal/rsa.h>
  10. #include <crypto/internal/akcipher.h>
  11. #include <crypto/akcipher.h>
  12. #include <crypto/algapi.h>
  13. struct rsa_mpi_key {
  14. MPI n;
  15. MPI e;
  16. MPI d;
  17. };
  18. /*
  19. * RSAEP function [RFC3447 sec 5.1.1]
  20. * c = m^e mod n;
  21. */
  22. static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m)
  23. {
  24. /* (1) Validate 0 <= m < n */
  25. if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
  26. return -EINVAL;
  27. /* (2) c = m^e mod n */
  28. return mpi_powm(c, m, key->e, key->n);
  29. }
  30. /*
  31. * RSADP function [RFC3447 sec 5.1.2]
  32. * m = c^d mod n;
  33. */
  34. static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c)
  35. {
  36. /* (1) Validate 0 <= c < n */
  37. if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
  38. return -EINVAL;
  39. /* (2) m = c^d mod n */
  40. return mpi_powm(m, c, key->d, key->n);
  41. }
  42. static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm)
  43. {
  44. return akcipher_tfm_ctx(tfm);
  45. }
  46. static int rsa_enc(struct akcipher_request *req)
  47. {
  48. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  49. const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
  50. MPI m, c = mpi_alloc(0);
  51. int ret = 0;
  52. int sign;
  53. if (!c)
  54. return -ENOMEM;
  55. if (unlikely(!pkey->n || !pkey->e)) {
  56. ret = -EINVAL;
  57. goto err_free_c;
  58. }
  59. ret = -ENOMEM;
  60. m = mpi_read_raw_from_sgl(req->src, req->src_len);
  61. if (!m)
  62. goto err_free_c;
  63. ret = _rsa_enc(pkey, c, m);
  64. if (ret)
  65. goto err_free_m;
  66. ret = mpi_write_to_sgl(c, req->dst, req->dst_len, &sign);
  67. if (ret)
  68. goto err_free_m;
  69. if (sign < 0)
  70. ret = -EBADMSG;
  71. err_free_m:
  72. mpi_free(m);
  73. err_free_c:
  74. mpi_free(c);
  75. return ret;
  76. }
  77. static int rsa_dec(struct akcipher_request *req)
  78. {
  79. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  80. const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
  81. MPI c, m = mpi_alloc(0);
  82. int ret = 0;
  83. int sign;
  84. if (!m)
  85. return -ENOMEM;
  86. if (unlikely(!pkey->n || !pkey->d)) {
  87. ret = -EINVAL;
  88. goto err_free_m;
  89. }
  90. ret = -ENOMEM;
  91. c = mpi_read_raw_from_sgl(req->src, req->src_len);
  92. if (!c)
  93. goto err_free_m;
  94. ret = _rsa_dec(pkey, m, c);
  95. if (ret)
  96. goto err_free_c;
  97. ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
  98. if (ret)
  99. goto err_free_c;
  100. if (sign < 0)
  101. ret = -EBADMSG;
  102. err_free_c:
  103. mpi_free(c);
  104. err_free_m:
  105. mpi_free(m);
  106. return ret;
  107. }
  108. static void rsa_free_mpi_key(struct rsa_mpi_key *key)
  109. {
  110. mpi_free(key->d);
  111. mpi_free(key->e);
  112. mpi_free(key->n);
  113. key->d = NULL;
  114. key->e = NULL;
  115. key->n = NULL;
  116. }
  117. static int rsa_check_key_length(unsigned int len)
  118. {
  119. switch (len) {
  120. case 512:
  121. case 1024:
  122. case 1536:
  123. case 2048:
  124. case 3072:
  125. case 4096:
  126. return 0;
  127. }
  128. return -EINVAL;
  129. }
  130. static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
  131. unsigned int keylen)
  132. {
  133. struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
  134. struct rsa_key raw_key = {0};
  135. int ret;
  136. /* Free the old MPI key if any */
  137. rsa_free_mpi_key(mpi_key);
  138. ret = rsa_parse_pub_key(&raw_key, key, keylen);
  139. if (ret)
  140. return ret;
  141. mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
  142. if (!mpi_key->e)
  143. goto err;
  144. mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
  145. if (!mpi_key->n)
  146. goto err;
  147. if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
  148. rsa_free_mpi_key(mpi_key);
  149. return -EINVAL;
  150. }
  151. return 0;
  152. err:
  153. rsa_free_mpi_key(mpi_key);
  154. return -ENOMEM;
  155. }
  156. static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
  157. unsigned int keylen)
  158. {
  159. struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
  160. struct rsa_key raw_key = {0};
  161. int ret;
  162. /* Free the old MPI key if any */
  163. rsa_free_mpi_key(mpi_key);
  164. ret = rsa_parse_priv_key(&raw_key, key, keylen);
  165. if (ret)
  166. return ret;
  167. mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz);
  168. if (!mpi_key->d)
  169. goto err;
  170. mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
  171. if (!mpi_key->e)
  172. goto err;
  173. mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
  174. if (!mpi_key->n)
  175. goto err;
  176. if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
  177. rsa_free_mpi_key(mpi_key);
  178. return -EINVAL;
  179. }
  180. return 0;
  181. err:
  182. rsa_free_mpi_key(mpi_key);
  183. return -ENOMEM;
  184. }
  185. static unsigned int rsa_max_size(struct crypto_akcipher *tfm)
  186. {
  187. struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
  188. return mpi_get_size(pkey->n);
  189. }
  190. static void rsa_exit_tfm(struct crypto_akcipher *tfm)
  191. {
  192. struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
  193. rsa_free_mpi_key(pkey);
  194. }
  195. static struct akcipher_alg rsa = {
  196. .encrypt = rsa_enc,
  197. .decrypt = rsa_dec,
  198. .set_priv_key = rsa_set_priv_key,
  199. .set_pub_key = rsa_set_pub_key,
  200. .max_size = rsa_max_size,
  201. .exit = rsa_exit_tfm,
  202. .base = {
  203. .cra_name = "rsa",
  204. .cra_driver_name = "rsa-generic",
  205. .cra_priority = 100,
  206. .cra_module = THIS_MODULE,
  207. .cra_ctxsize = sizeof(struct rsa_mpi_key),
  208. },
  209. };
  210. static int rsa_init(void)
  211. {
  212. int err;
  213. err = crypto_register_akcipher(&rsa);
  214. if (err)
  215. return err;
  216. err = crypto_register_template(&rsa_pkcs1pad_tmpl);
  217. if (err) {
  218. crypto_unregister_akcipher(&rsa);
  219. return err;
  220. }
  221. return 0;
  222. }
  223. static void rsa_exit(void)
  224. {
  225. crypto_unregister_template(&rsa_pkcs1pad_tmpl);
  226. crypto_unregister_akcipher(&rsa);
  227. }
  228. subsys_initcall(rsa_init);
  229. module_exit(rsa_exit);
  230. MODULE_ALIAS_CRYPTO("rsa");
  231. MODULE_LICENSE("GPL");
  232. MODULE_DESCRIPTION("RSA generic algorithm");