rsa.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* RSA asymmetric public-key algorithm [RFC3447]
  2. *
  3. * Copyright (c) 2015, Intel Corporation
  4. * Authors: Tadeusz Struk <tadeusz.struk@intel.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <crypto/internal/rsa.h>
  13. #include <crypto/internal/akcipher.h>
  14. #include <crypto/akcipher.h>
  15. #include <crypto/algapi.h>
  16. /*
  17. * RSAEP function [RFC3447 sec 5.1.1]
  18. * c = m^e mod n;
  19. */
  20. static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m)
  21. {
  22. /* (1) Validate 0 <= m < n */
  23. if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
  24. return -EINVAL;
  25. /* (2) c = m^e mod n */
  26. return mpi_powm(c, m, key->e, key->n);
  27. }
  28. /*
  29. * RSADP function [RFC3447 sec 5.1.2]
  30. * m = c^d mod n;
  31. */
  32. static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c)
  33. {
  34. /* (1) Validate 0 <= c < n */
  35. if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0)
  36. return -EINVAL;
  37. /* (2) m = c^d mod n */
  38. return mpi_powm(m, c, key->d, key->n);
  39. }
  40. /*
  41. * RSASP1 function [RFC3447 sec 5.2.1]
  42. * s = m^d mod n
  43. */
  44. static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m)
  45. {
  46. /* (1) Validate 0 <= m < n */
  47. if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0)
  48. return -EINVAL;
  49. /* (2) s = m^d mod n */
  50. return mpi_powm(s, m, key->d, key->n);
  51. }
  52. /*
  53. * RSAVP1 function [RFC3447 sec 5.2.2]
  54. * m = s^e mod n;
  55. */
  56. static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s)
  57. {
  58. /* (1) Validate 0 <= s < n */
  59. if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0)
  60. return -EINVAL;
  61. /* (2) m = s^e mod n */
  62. return mpi_powm(m, s, key->e, key->n);
  63. }
  64. static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm)
  65. {
  66. return akcipher_tfm_ctx(tfm);
  67. }
  68. static int rsa_enc(struct akcipher_request *req)
  69. {
  70. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  71. const struct rsa_key *pkey = rsa_get_key(tfm);
  72. MPI m, c = mpi_alloc(0);
  73. int ret = 0;
  74. int sign;
  75. if (!c)
  76. return -ENOMEM;
  77. if (unlikely(!pkey->n || !pkey->e)) {
  78. ret = -EINVAL;
  79. goto err_free_c;
  80. }
  81. ret = -ENOMEM;
  82. m = mpi_read_raw_from_sgl(req->src, req->src_len);
  83. if (!m)
  84. goto err_free_c;
  85. ret = _rsa_enc(pkey, c, m);
  86. if (ret)
  87. goto err_free_m;
  88. ret = mpi_write_to_sgl(c, req->dst, &req->dst_len, &sign);
  89. if (ret)
  90. goto err_free_m;
  91. if (sign < 0)
  92. ret = -EBADMSG;
  93. err_free_m:
  94. mpi_free(m);
  95. err_free_c:
  96. mpi_free(c);
  97. return ret;
  98. }
  99. static int rsa_dec(struct akcipher_request *req)
  100. {
  101. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  102. const struct rsa_key *pkey = rsa_get_key(tfm);
  103. MPI c, m = mpi_alloc(0);
  104. int ret = 0;
  105. int sign;
  106. if (!m)
  107. return -ENOMEM;
  108. if (unlikely(!pkey->n || !pkey->d)) {
  109. ret = -EINVAL;
  110. goto err_free_m;
  111. }
  112. ret = -ENOMEM;
  113. c = mpi_read_raw_from_sgl(req->src, req->src_len);
  114. if (!c)
  115. goto err_free_m;
  116. ret = _rsa_dec(pkey, m, c);
  117. if (ret)
  118. goto err_free_c;
  119. ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign);
  120. if (ret)
  121. goto err_free_c;
  122. if (sign < 0)
  123. ret = -EBADMSG;
  124. err_free_c:
  125. mpi_free(c);
  126. err_free_m:
  127. mpi_free(m);
  128. return ret;
  129. }
  130. static int rsa_sign(struct akcipher_request *req)
  131. {
  132. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  133. const struct rsa_key *pkey = rsa_get_key(tfm);
  134. MPI m, s = mpi_alloc(0);
  135. int ret = 0;
  136. int sign;
  137. if (!s)
  138. return -ENOMEM;
  139. if (unlikely(!pkey->n || !pkey->d)) {
  140. ret = -EINVAL;
  141. goto err_free_s;
  142. }
  143. ret = -ENOMEM;
  144. m = mpi_read_raw_from_sgl(req->src, req->src_len);
  145. if (!m)
  146. goto err_free_s;
  147. ret = _rsa_sign(pkey, s, m);
  148. if (ret)
  149. goto err_free_m;
  150. ret = mpi_write_to_sgl(s, req->dst, &req->dst_len, &sign);
  151. if (ret)
  152. goto err_free_m;
  153. if (sign < 0)
  154. ret = -EBADMSG;
  155. err_free_m:
  156. mpi_free(m);
  157. err_free_s:
  158. mpi_free(s);
  159. return ret;
  160. }
  161. static int rsa_verify(struct akcipher_request *req)
  162. {
  163. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  164. const struct rsa_key *pkey = rsa_get_key(tfm);
  165. MPI s, m = mpi_alloc(0);
  166. int ret = 0;
  167. int sign;
  168. if (!m)
  169. return -ENOMEM;
  170. if (unlikely(!pkey->n || !pkey->e)) {
  171. ret = -EINVAL;
  172. goto err_free_m;
  173. }
  174. ret = -ENOMEM;
  175. s = mpi_read_raw_from_sgl(req->src, req->src_len);
  176. if (!s) {
  177. ret = -ENOMEM;
  178. goto err_free_m;
  179. }
  180. ret = _rsa_verify(pkey, m, s);
  181. if (ret)
  182. goto err_free_s;
  183. ret = mpi_write_to_sgl(m, req->dst, &req->dst_len, &sign);
  184. if (ret)
  185. goto err_free_s;
  186. if (sign < 0)
  187. ret = -EBADMSG;
  188. err_free_s:
  189. mpi_free(s);
  190. err_free_m:
  191. mpi_free(m);
  192. return ret;
  193. }
  194. static int rsa_check_key_length(unsigned int len)
  195. {
  196. switch (len) {
  197. case 512:
  198. case 1024:
  199. case 1536:
  200. case 2048:
  201. case 3072:
  202. case 4096:
  203. return 0;
  204. }
  205. return -EINVAL;
  206. }
  207. static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
  208. unsigned int keylen)
  209. {
  210. struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
  211. int ret;
  212. ret = rsa_parse_pub_key(pkey, key, keylen);
  213. if (ret)
  214. return ret;
  215. if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) {
  216. rsa_free_key(pkey);
  217. ret = -EINVAL;
  218. }
  219. return ret;
  220. }
  221. static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
  222. unsigned int keylen)
  223. {
  224. struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
  225. int ret;
  226. ret = rsa_parse_priv_key(pkey, key, keylen);
  227. if (ret)
  228. return ret;
  229. if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) {
  230. rsa_free_key(pkey);
  231. ret = -EINVAL;
  232. }
  233. return ret;
  234. }
  235. static int rsa_max_size(struct crypto_akcipher *tfm)
  236. {
  237. struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
  238. return pkey->n ? mpi_get_size(pkey->n) : -EINVAL;
  239. }
  240. static void rsa_exit_tfm(struct crypto_akcipher *tfm)
  241. {
  242. struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
  243. rsa_free_key(pkey);
  244. }
  245. static struct akcipher_alg rsa = {
  246. .encrypt = rsa_enc,
  247. .decrypt = rsa_dec,
  248. .sign = rsa_sign,
  249. .verify = rsa_verify,
  250. .set_priv_key = rsa_set_priv_key,
  251. .set_pub_key = rsa_set_pub_key,
  252. .max_size = rsa_max_size,
  253. .exit = rsa_exit_tfm,
  254. .base = {
  255. .cra_name = "rsa",
  256. .cra_driver_name = "rsa-generic",
  257. .cra_priority = 100,
  258. .cra_module = THIS_MODULE,
  259. .cra_ctxsize = sizeof(struct rsa_key),
  260. },
  261. };
  262. static int rsa_init(void)
  263. {
  264. int err;
  265. err = crypto_register_akcipher(&rsa);
  266. if (err)
  267. return err;
  268. err = crypto_register_template(&rsa_pkcs1pad_tmpl);
  269. if (err) {
  270. crypto_unregister_akcipher(&rsa);
  271. return err;
  272. }
  273. return 0;
  274. }
  275. static void rsa_exit(void)
  276. {
  277. crypto_unregister_template(&rsa_pkcs1pad_tmpl);
  278. crypto_unregister_akcipher(&rsa);
  279. }
  280. module_init(rsa_init);
  281. module_exit(rsa_exit);
  282. MODULE_ALIAS_CRYPTO("rsa");
  283. MODULE_LICENSE("GPL");
  284. MODULE_DESCRIPTION("RSA generic algorithm");