rsa.c 7.5 KB

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