ccp-crypto-rsa.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) RSA crypto API support
  3. *
  4. * Copyright (C) 2017 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Gary R Hook <gary.hook@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/crypto.h>
  16. #include <crypto/algapi.h>
  17. #include <crypto/internal/rsa.h>
  18. #include <crypto/internal/akcipher.h>
  19. #include <crypto/akcipher.h>
  20. #include <crypto/scatterwalk.h>
  21. #include "ccp-crypto.h"
  22. static inline struct akcipher_request *akcipher_request_cast(
  23. struct crypto_async_request *req)
  24. {
  25. return container_of(req, struct akcipher_request, base);
  26. }
  27. static inline int ccp_copy_and_save_keypart(u8 **kpbuf, unsigned int *kplen,
  28. const u8 *buf, size_t sz)
  29. {
  30. int nskip;
  31. for (nskip = 0; nskip < sz; nskip++)
  32. if (buf[nskip])
  33. break;
  34. *kplen = sz - nskip;
  35. *kpbuf = kzalloc(*kplen, GFP_KERNEL);
  36. if (!*kpbuf)
  37. return -ENOMEM;
  38. memcpy(*kpbuf, buf + nskip, *kplen);
  39. return 0;
  40. }
  41. static int ccp_rsa_complete(struct crypto_async_request *async_req, int ret)
  42. {
  43. struct akcipher_request *req = akcipher_request_cast(async_req);
  44. struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
  45. if (ret)
  46. return ret;
  47. req->dst_len = rctx->cmd.u.rsa.key_size >> 3;
  48. return 0;
  49. }
  50. static unsigned int ccp_rsa_maxsize(struct crypto_akcipher *tfm)
  51. {
  52. struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
  53. return ctx->u.rsa.n_len;
  54. }
  55. static int ccp_rsa_crypt(struct akcipher_request *req, bool encrypt)
  56. {
  57. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  58. struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
  59. struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
  60. int ret = 0;
  61. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  62. INIT_LIST_HEAD(&rctx->cmd.entry);
  63. rctx->cmd.engine = CCP_ENGINE_RSA;
  64. rctx->cmd.u.rsa.key_size = ctx->u.rsa.key_len; /* in bits */
  65. if (encrypt) {
  66. rctx->cmd.u.rsa.exp = &ctx->u.rsa.e_sg;
  67. rctx->cmd.u.rsa.exp_len = ctx->u.rsa.e_len;
  68. } else {
  69. rctx->cmd.u.rsa.exp = &ctx->u.rsa.d_sg;
  70. rctx->cmd.u.rsa.exp_len = ctx->u.rsa.d_len;
  71. }
  72. rctx->cmd.u.rsa.mod = &ctx->u.rsa.n_sg;
  73. rctx->cmd.u.rsa.mod_len = ctx->u.rsa.n_len;
  74. rctx->cmd.u.rsa.src = req->src;
  75. rctx->cmd.u.rsa.src_len = req->src_len;
  76. rctx->cmd.u.rsa.dst = req->dst;
  77. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  78. return ret;
  79. }
  80. static int ccp_rsa_encrypt(struct akcipher_request *req)
  81. {
  82. return ccp_rsa_crypt(req, true);
  83. }
  84. static int ccp_rsa_decrypt(struct akcipher_request *req)
  85. {
  86. return ccp_rsa_crypt(req, false);
  87. }
  88. static int ccp_check_key_length(unsigned int len)
  89. {
  90. /* In bits */
  91. if (len < 8 || len > 4096)
  92. return -EINVAL;
  93. return 0;
  94. }
  95. static void ccp_rsa_free_key_bufs(struct ccp_ctx *ctx)
  96. {
  97. /* Clean up old key data */
  98. kzfree(ctx->u.rsa.e_buf);
  99. ctx->u.rsa.e_buf = NULL;
  100. ctx->u.rsa.e_len = 0;
  101. kzfree(ctx->u.rsa.n_buf);
  102. ctx->u.rsa.n_buf = NULL;
  103. ctx->u.rsa.n_len = 0;
  104. kzfree(ctx->u.rsa.d_buf);
  105. ctx->u.rsa.d_buf = NULL;
  106. ctx->u.rsa.d_len = 0;
  107. }
  108. static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key,
  109. unsigned int keylen, bool private)
  110. {
  111. struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
  112. struct rsa_key raw_key;
  113. int ret;
  114. ccp_rsa_free_key_bufs(ctx);
  115. memset(&raw_key, 0, sizeof(raw_key));
  116. /* Code borrowed from crypto/rsa.c */
  117. if (private)
  118. ret = rsa_parse_priv_key(&raw_key, key, keylen);
  119. else
  120. ret = rsa_parse_pub_key(&raw_key, key, keylen);
  121. if (ret)
  122. goto n_key;
  123. ret = ccp_copy_and_save_keypart(&ctx->u.rsa.n_buf, &ctx->u.rsa.n_len,
  124. raw_key.n, raw_key.n_sz);
  125. if (ret)
  126. goto key_err;
  127. sg_init_one(&ctx->u.rsa.n_sg, ctx->u.rsa.n_buf, ctx->u.rsa.n_len);
  128. ctx->u.rsa.key_len = ctx->u.rsa.n_len << 3; /* convert to bits */
  129. if (ccp_check_key_length(ctx->u.rsa.key_len)) {
  130. ret = -EINVAL;
  131. goto key_err;
  132. }
  133. ret = ccp_copy_and_save_keypart(&ctx->u.rsa.e_buf, &ctx->u.rsa.e_len,
  134. raw_key.e, raw_key.e_sz);
  135. if (ret)
  136. goto key_err;
  137. sg_init_one(&ctx->u.rsa.e_sg, ctx->u.rsa.e_buf, ctx->u.rsa.e_len);
  138. if (private) {
  139. ret = ccp_copy_and_save_keypart(&ctx->u.rsa.d_buf,
  140. &ctx->u.rsa.d_len,
  141. raw_key.d, raw_key.d_sz);
  142. if (ret)
  143. goto key_err;
  144. sg_init_one(&ctx->u.rsa.d_sg,
  145. ctx->u.rsa.d_buf, ctx->u.rsa.d_len);
  146. }
  147. return 0;
  148. key_err:
  149. ccp_rsa_free_key_bufs(ctx);
  150. n_key:
  151. return ret;
  152. }
  153. static int ccp_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key,
  154. unsigned int keylen)
  155. {
  156. return ccp_rsa_setkey(tfm, key, keylen, true);
  157. }
  158. static int ccp_rsa_setpubkey(struct crypto_akcipher *tfm, const void *key,
  159. unsigned int keylen)
  160. {
  161. return ccp_rsa_setkey(tfm, key, keylen, false);
  162. }
  163. static int ccp_rsa_init_tfm(struct crypto_akcipher *tfm)
  164. {
  165. struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
  166. akcipher_set_reqsize(tfm, sizeof(struct ccp_rsa_req_ctx));
  167. ctx->complete = ccp_rsa_complete;
  168. return 0;
  169. }
  170. static void ccp_rsa_exit_tfm(struct crypto_akcipher *tfm)
  171. {
  172. struct ccp_ctx *ctx = crypto_tfm_ctx(&tfm->base);
  173. ccp_rsa_free_key_bufs(ctx);
  174. }
  175. static struct akcipher_alg ccp_rsa_defaults = {
  176. .encrypt = ccp_rsa_encrypt,
  177. .decrypt = ccp_rsa_decrypt,
  178. .sign = ccp_rsa_decrypt,
  179. .verify = ccp_rsa_encrypt,
  180. .set_pub_key = ccp_rsa_setpubkey,
  181. .set_priv_key = ccp_rsa_setprivkey,
  182. .max_size = ccp_rsa_maxsize,
  183. .init = ccp_rsa_init_tfm,
  184. .exit = ccp_rsa_exit_tfm,
  185. .base = {
  186. .cra_name = "rsa",
  187. .cra_driver_name = "rsa-ccp",
  188. .cra_priority = CCP_CRA_PRIORITY,
  189. .cra_module = THIS_MODULE,
  190. .cra_ctxsize = 2 * sizeof(struct ccp_ctx),
  191. },
  192. };
  193. struct ccp_rsa_def {
  194. unsigned int version;
  195. const char *name;
  196. const char *driver_name;
  197. unsigned int reqsize;
  198. struct akcipher_alg *alg_defaults;
  199. };
  200. static struct ccp_rsa_def rsa_algs[] = {
  201. {
  202. .version = CCP_VERSION(3, 0),
  203. .name = "rsa",
  204. .driver_name = "rsa-ccp",
  205. .reqsize = sizeof(struct ccp_rsa_req_ctx),
  206. .alg_defaults = &ccp_rsa_defaults,
  207. }
  208. };
  209. int ccp_register_rsa_alg(struct list_head *head, const struct ccp_rsa_def *def)
  210. {
  211. struct ccp_crypto_akcipher_alg *ccp_alg;
  212. struct akcipher_alg *alg;
  213. int ret;
  214. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  215. if (!ccp_alg)
  216. return -ENOMEM;
  217. INIT_LIST_HEAD(&ccp_alg->entry);
  218. alg = &ccp_alg->alg;
  219. *alg = *def->alg_defaults;
  220. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  221. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  222. def->driver_name);
  223. ret = crypto_register_akcipher(alg);
  224. if (ret) {
  225. pr_err("%s akcipher algorithm registration error (%d)\n",
  226. alg->base.cra_name, ret);
  227. kfree(ccp_alg);
  228. return ret;
  229. }
  230. list_add(&ccp_alg->entry, head);
  231. return 0;
  232. }
  233. int ccp_register_rsa_algs(struct list_head *head)
  234. {
  235. int i, ret;
  236. unsigned int ccpversion = ccp_version();
  237. /* Register the RSA algorithm in standard mode
  238. * This works for CCP v3 and later
  239. */
  240. for (i = 0; i < ARRAY_SIZE(rsa_algs); i++) {
  241. if (rsa_algs[i].version > ccpversion)
  242. continue;
  243. ret = ccp_register_rsa_alg(head, &rsa_algs[i]);
  244. if (ret)
  245. return ret;
  246. }
  247. return 0;
  248. }