rsa.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. s = mpi_read_raw_from_sgl(req->src, req->src_len);
  181. if (!s) {
  182. ret = -ENOMEM;
  183. goto err_free_m;
  184. }
  185. ret = _rsa_verify(pkey, m, s);
  186. if (ret)
  187. goto err_free_s;
  188. ret = mpi_write_to_sgl(m, req->dst, req->dst_len, &sign);
  189. if (ret)
  190. goto err_free_s;
  191. if (sign < 0)
  192. ret = -EBADMSG;
  193. err_free_s:
  194. mpi_free(s);
  195. err_free_m:
  196. mpi_free(m);
  197. return ret;
  198. }
  199. static void rsa_free_mpi_key(struct rsa_mpi_key *key)
  200. {
  201. mpi_free(key->d);
  202. mpi_free(key->e);
  203. mpi_free(key->n);
  204. key->d = NULL;
  205. key->e = NULL;
  206. key->n = NULL;
  207. }
  208. static int rsa_check_key_length(unsigned int len)
  209. {
  210. switch (len) {
  211. case 512:
  212. case 1024:
  213. case 1536:
  214. case 2048:
  215. case 3072:
  216. case 4096:
  217. return 0;
  218. }
  219. return -EINVAL;
  220. }
  221. static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key,
  222. unsigned int keylen)
  223. {
  224. struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
  225. struct rsa_key raw_key = {0};
  226. int ret;
  227. /* Free the old MPI key if any */
  228. rsa_free_mpi_key(mpi_key);
  229. ret = rsa_parse_pub_key(&raw_key, key, keylen);
  230. if (ret)
  231. return ret;
  232. mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
  233. if (!mpi_key->e)
  234. goto err;
  235. mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
  236. if (!mpi_key->n)
  237. goto err;
  238. if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
  239. rsa_free_mpi_key(mpi_key);
  240. return -EINVAL;
  241. }
  242. return 0;
  243. err:
  244. rsa_free_mpi_key(mpi_key);
  245. return -ENOMEM;
  246. }
  247. static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
  248. unsigned int keylen)
  249. {
  250. struct rsa_mpi_key *mpi_key = akcipher_tfm_ctx(tfm);
  251. struct rsa_key raw_key = {0};
  252. int ret;
  253. /* Free the old MPI key if any */
  254. rsa_free_mpi_key(mpi_key);
  255. ret = rsa_parse_priv_key(&raw_key, key, keylen);
  256. if (ret)
  257. return ret;
  258. mpi_key->d = mpi_read_raw_data(raw_key.d, raw_key.d_sz);
  259. if (!mpi_key->d)
  260. goto err;
  261. mpi_key->e = mpi_read_raw_data(raw_key.e, raw_key.e_sz);
  262. if (!mpi_key->e)
  263. goto err;
  264. mpi_key->n = mpi_read_raw_data(raw_key.n, raw_key.n_sz);
  265. if (!mpi_key->n)
  266. goto err;
  267. if (rsa_check_key_length(mpi_get_size(mpi_key->n) << 3)) {
  268. rsa_free_mpi_key(mpi_key);
  269. return -EINVAL;
  270. }
  271. return 0;
  272. err:
  273. rsa_free_mpi_key(mpi_key);
  274. return -ENOMEM;
  275. }
  276. static unsigned int rsa_max_size(struct crypto_akcipher *tfm)
  277. {
  278. struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
  279. return mpi_get_size(pkey->n);
  280. }
  281. static void rsa_exit_tfm(struct crypto_akcipher *tfm)
  282. {
  283. struct rsa_mpi_key *pkey = akcipher_tfm_ctx(tfm);
  284. rsa_free_mpi_key(pkey);
  285. }
  286. static struct akcipher_alg rsa = {
  287. .encrypt = rsa_enc,
  288. .decrypt = rsa_dec,
  289. .sign = rsa_sign,
  290. .verify = rsa_verify,
  291. .set_priv_key = rsa_set_priv_key,
  292. .set_pub_key = rsa_set_pub_key,
  293. .max_size = rsa_max_size,
  294. .exit = rsa_exit_tfm,
  295. .base = {
  296. .cra_name = "rsa",
  297. .cra_driver_name = "rsa-generic",
  298. .cra_priority = 100,
  299. .cra_module = THIS_MODULE,
  300. .cra_ctxsize = sizeof(struct rsa_mpi_key),
  301. },
  302. };
  303. static int rsa_init(void)
  304. {
  305. int err;
  306. err = crypto_register_akcipher(&rsa);
  307. if (err)
  308. return err;
  309. err = crypto_register_template(&rsa_pkcs1pad_tmpl);
  310. if (err) {
  311. crypto_unregister_akcipher(&rsa);
  312. return err;
  313. }
  314. return 0;
  315. }
  316. static void rsa_exit(void)
  317. {
  318. crypto_unregister_template(&rsa_pkcs1pad_tmpl);
  319. crypto_unregister_akcipher(&rsa);
  320. }
  321. module_init(rsa_init);
  322. module_exit(rsa_exit);
  323. MODULE_ALIAS_CRYPTO("rsa");
  324. MODULE_LICENSE("GPL");
  325. MODULE_DESCRIPTION("RSA generic algorithm");