rsa.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. m = mpi_read_raw_data(req->src, req->src_len);
  86. if (!m) {
  87. ret = -ENOMEM;
  88. goto err_free_c;
  89. }
  90. ret = _rsa_enc(pkey, c, m);
  91. if (ret)
  92. goto err_free_m;
  93. ret = mpi_read_buffer(c, req->dst, req->dst_len, &req->dst_len, &sign);
  94. if (ret)
  95. goto err_free_m;
  96. if (sign < 0) {
  97. ret = -EBADMSG;
  98. goto err_free_m;
  99. }
  100. err_free_m:
  101. mpi_free(m);
  102. err_free_c:
  103. mpi_free(c);
  104. return ret;
  105. }
  106. static int rsa_dec(struct akcipher_request *req)
  107. {
  108. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  109. const struct rsa_key *pkey = rsa_get_key(tfm);
  110. MPI c, m = mpi_alloc(0);
  111. int ret = 0;
  112. int sign;
  113. if (!m)
  114. return -ENOMEM;
  115. if (unlikely(!pkey->n || !pkey->d)) {
  116. ret = -EINVAL;
  117. goto err_free_m;
  118. }
  119. if (req->dst_len < mpi_get_size(pkey->n)) {
  120. req->dst_len = mpi_get_size(pkey->n);
  121. ret = -EOVERFLOW;
  122. goto err_free_m;
  123. }
  124. c = mpi_read_raw_data(req->src, req->src_len);
  125. if (!c) {
  126. ret = -ENOMEM;
  127. goto err_free_m;
  128. }
  129. ret = _rsa_dec(pkey, m, c);
  130. if (ret)
  131. goto err_free_c;
  132. ret = mpi_read_buffer(m, req->dst, req->dst_len, &req->dst_len, &sign);
  133. if (ret)
  134. goto err_free_c;
  135. if (sign < 0) {
  136. ret = -EBADMSG;
  137. goto err_free_c;
  138. }
  139. err_free_c:
  140. mpi_free(c);
  141. err_free_m:
  142. mpi_free(m);
  143. return ret;
  144. }
  145. static int rsa_sign(struct akcipher_request *req)
  146. {
  147. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  148. const struct rsa_key *pkey = rsa_get_key(tfm);
  149. MPI m, s = mpi_alloc(0);
  150. int ret = 0;
  151. int sign;
  152. if (!s)
  153. return -ENOMEM;
  154. if (unlikely(!pkey->n || !pkey->d)) {
  155. ret = -EINVAL;
  156. goto err_free_s;
  157. }
  158. if (req->dst_len < mpi_get_size(pkey->n)) {
  159. req->dst_len = mpi_get_size(pkey->n);
  160. ret = -EOVERFLOW;
  161. goto err_free_s;
  162. }
  163. m = mpi_read_raw_data(req->src, req->src_len);
  164. if (!m) {
  165. ret = -ENOMEM;
  166. goto err_free_s;
  167. }
  168. ret = _rsa_sign(pkey, s, m);
  169. if (ret)
  170. goto err_free_m;
  171. ret = mpi_read_buffer(s, req->dst, req->dst_len, &req->dst_len, &sign);
  172. if (ret)
  173. goto err_free_m;
  174. if (sign < 0) {
  175. ret = -EBADMSG;
  176. goto err_free_m;
  177. }
  178. err_free_m:
  179. mpi_free(m);
  180. err_free_s:
  181. mpi_free(s);
  182. return ret;
  183. }
  184. static int rsa_verify(struct akcipher_request *req)
  185. {
  186. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  187. const struct rsa_key *pkey = rsa_get_key(tfm);
  188. MPI s, m = mpi_alloc(0);
  189. int ret = 0;
  190. int sign;
  191. if (!m)
  192. return -ENOMEM;
  193. if (unlikely(!pkey->n || !pkey->e)) {
  194. ret = -EINVAL;
  195. goto err_free_m;
  196. }
  197. if (req->dst_len < mpi_get_size(pkey->n)) {
  198. req->dst_len = mpi_get_size(pkey->n);
  199. ret = -EOVERFLOW;
  200. goto err_free_m;
  201. }
  202. s = mpi_read_raw_data(req->src, req->src_len);
  203. if (!s) {
  204. ret = -ENOMEM;
  205. goto err_free_m;
  206. }
  207. ret = _rsa_verify(pkey, m, s);
  208. if (ret)
  209. goto err_free_s;
  210. ret = mpi_read_buffer(m, req->dst, req->dst_len, &req->dst_len, &sign);
  211. if (ret)
  212. goto err_free_s;
  213. if (sign < 0) {
  214. ret = -EBADMSG;
  215. goto err_free_s;
  216. }
  217. err_free_s:
  218. mpi_free(s);
  219. err_free_m:
  220. mpi_free(m);
  221. return ret;
  222. }
  223. static int rsa_setkey(struct crypto_akcipher *tfm, const void *key,
  224. unsigned int keylen)
  225. {
  226. struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
  227. return rsa_parse_key(pkey, key, keylen);
  228. }
  229. static void rsa_exit_tfm(struct crypto_akcipher *tfm)
  230. {
  231. struct rsa_key *pkey = akcipher_tfm_ctx(tfm);
  232. rsa_free_key(pkey);
  233. }
  234. static struct akcipher_alg rsa = {
  235. .encrypt = rsa_enc,
  236. .decrypt = rsa_dec,
  237. .sign = rsa_sign,
  238. .verify = rsa_verify,
  239. .setkey = rsa_setkey,
  240. .exit = rsa_exit_tfm,
  241. .base = {
  242. .cra_name = "rsa",
  243. .cra_driver_name = "rsa-generic",
  244. .cra_priority = 100,
  245. .cra_module = THIS_MODULE,
  246. .cra_ctxsize = sizeof(struct rsa_key),
  247. },
  248. };
  249. static int rsa_init(void)
  250. {
  251. return crypto_register_akcipher(&rsa);
  252. }
  253. static void rsa_exit(void)
  254. {
  255. crypto_unregister_akcipher(&rsa);
  256. }
  257. module_init(rsa_init);
  258. module_exit(rsa_exit);
  259. MODULE_ALIAS_CRYPTO("rsa");
  260. MODULE_LICENSE("GPL");
  261. MODULE_DESCRIPTION("RSA generic algorithm");