rsa.c 6.7 KB

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