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. if (ccp_version() > CCP_VERSION(3, 0))
  53. return CCP5_RSA_MAXMOD;
  54. else
  55. return CCP_RSA_MAXMOD;
  56. }
  57. static int ccp_rsa_crypt(struct akcipher_request *req, bool encrypt)
  58. {
  59. struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
  60. struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
  61. struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req);
  62. int ret = 0;
  63. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  64. INIT_LIST_HEAD(&rctx->cmd.entry);
  65. rctx->cmd.engine = CCP_ENGINE_RSA;
  66. rctx->cmd.u.rsa.key_size = ctx->u.rsa.key_len; /* in bits */
  67. if (encrypt) {
  68. rctx->cmd.u.rsa.exp = &ctx->u.rsa.e_sg;
  69. rctx->cmd.u.rsa.exp_len = ctx->u.rsa.e_len;
  70. } else {
  71. rctx->cmd.u.rsa.exp = &ctx->u.rsa.d_sg;
  72. rctx->cmd.u.rsa.exp_len = ctx->u.rsa.d_len;
  73. }
  74. rctx->cmd.u.rsa.mod = &ctx->u.rsa.n_sg;
  75. rctx->cmd.u.rsa.mod_len = ctx->u.rsa.n_len;
  76. rctx->cmd.u.rsa.src = req->src;
  77. rctx->cmd.u.rsa.src_len = req->src_len;
  78. rctx->cmd.u.rsa.dst = req->dst;
  79. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  80. return ret;
  81. }
  82. static int ccp_rsa_encrypt(struct akcipher_request *req)
  83. {
  84. return ccp_rsa_crypt(req, true);
  85. }
  86. static int ccp_rsa_decrypt(struct akcipher_request *req)
  87. {
  88. return ccp_rsa_crypt(req, false);
  89. }
  90. static int ccp_check_key_length(unsigned int len)
  91. {
  92. /* In bits */
  93. if (len < 8 || len > 4096)
  94. return -EINVAL;
  95. return 0;
  96. }
  97. static void ccp_rsa_free_key_bufs(struct ccp_ctx *ctx)
  98. {
  99. /* Clean up old key data */
  100. kzfree(ctx->u.rsa.e_buf);
  101. ctx->u.rsa.e_buf = NULL;
  102. ctx->u.rsa.e_len = 0;
  103. kzfree(ctx->u.rsa.n_buf);
  104. ctx->u.rsa.n_buf = NULL;
  105. ctx->u.rsa.n_len = 0;
  106. kzfree(ctx->u.rsa.d_buf);
  107. ctx->u.rsa.d_buf = NULL;
  108. ctx->u.rsa.d_len = 0;
  109. }
  110. static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key,
  111. unsigned int keylen, bool private)
  112. {
  113. struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
  114. struct rsa_key raw_key;
  115. int ret;
  116. ccp_rsa_free_key_bufs(ctx);
  117. memset(&raw_key, 0, sizeof(raw_key));
  118. /* Code borrowed from crypto/rsa.c */
  119. if (private)
  120. ret = rsa_parse_priv_key(&raw_key, key, keylen);
  121. else
  122. ret = rsa_parse_pub_key(&raw_key, key, keylen);
  123. if (ret)
  124. goto n_key;
  125. ret = ccp_copy_and_save_keypart(&ctx->u.rsa.n_buf, &ctx->u.rsa.n_len,
  126. raw_key.n, raw_key.n_sz);
  127. if (ret)
  128. goto key_err;
  129. sg_init_one(&ctx->u.rsa.n_sg, ctx->u.rsa.n_buf, ctx->u.rsa.n_len);
  130. ctx->u.rsa.key_len = ctx->u.rsa.n_len << 3; /* convert to bits */
  131. if (ccp_check_key_length(ctx->u.rsa.key_len)) {
  132. ret = -EINVAL;
  133. goto key_err;
  134. }
  135. ret = ccp_copy_and_save_keypart(&ctx->u.rsa.e_buf, &ctx->u.rsa.e_len,
  136. raw_key.e, raw_key.e_sz);
  137. if (ret)
  138. goto key_err;
  139. sg_init_one(&ctx->u.rsa.e_sg, ctx->u.rsa.e_buf, ctx->u.rsa.e_len);
  140. if (private) {
  141. ret = ccp_copy_and_save_keypart(&ctx->u.rsa.d_buf,
  142. &ctx->u.rsa.d_len,
  143. raw_key.d, raw_key.d_sz);
  144. if (ret)
  145. goto key_err;
  146. sg_init_one(&ctx->u.rsa.d_sg,
  147. ctx->u.rsa.d_buf, ctx->u.rsa.d_len);
  148. }
  149. return 0;
  150. key_err:
  151. ccp_rsa_free_key_bufs(ctx);
  152. n_key:
  153. return ret;
  154. }
  155. static int ccp_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key,
  156. unsigned int keylen)
  157. {
  158. return ccp_rsa_setkey(tfm, key, keylen, true);
  159. }
  160. static int ccp_rsa_setpubkey(struct crypto_akcipher *tfm, const void *key,
  161. unsigned int keylen)
  162. {
  163. return ccp_rsa_setkey(tfm, key, keylen, false);
  164. }
  165. static int ccp_rsa_init_tfm(struct crypto_akcipher *tfm)
  166. {
  167. struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm);
  168. akcipher_set_reqsize(tfm, sizeof(struct ccp_rsa_req_ctx));
  169. ctx->complete = ccp_rsa_complete;
  170. return 0;
  171. }
  172. static void ccp_rsa_exit_tfm(struct crypto_akcipher *tfm)
  173. {
  174. struct ccp_ctx *ctx = crypto_tfm_ctx(&tfm->base);
  175. ccp_rsa_free_key_bufs(ctx);
  176. }
  177. static struct akcipher_alg ccp_rsa_defaults = {
  178. .encrypt = ccp_rsa_encrypt,
  179. .decrypt = ccp_rsa_decrypt,
  180. .sign = ccp_rsa_decrypt,
  181. .verify = ccp_rsa_encrypt,
  182. .set_pub_key = ccp_rsa_setpubkey,
  183. .set_priv_key = ccp_rsa_setprivkey,
  184. .max_size = ccp_rsa_maxsize,
  185. .init = ccp_rsa_init_tfm,
  186. .exit = ccp_rsa_exit_tfm,
  187. .base = {
  188. .cra_name = "rsa",
  189. .cra_driver_name = "rsa-ccp",
  190. .cra_priority = CCP_CRA_PRIORITY,
  191. .cra_module = THIS_MODULE,
  192. .cra_ctxsize = 2 * sizeof(struct ccp_ctx),
  193. },
  194. };
  195. struct ccp_rsa_def {
  196. unsigned int version;
  197. const char *name;
  198. const char *driver_name;
  199. unsigned int reqsize;
  200. struct akcipher_alg *alg_defaults;
  201. };
  202. static struct ccp_rsa_def rsa_algs[] = {
  203. {
  204. .version = CCP_VERSION(3, 0),
  205. .name = "rsa",
  206. .driver_name = "rsa-ccp",
  207. .reqsize = sizeof(struct ccp_rsa_req_ctx),
  208. .alg_defaults = &ccp_rsa_defaults,
  209. }
  210. };
  211. int ccp_register_rsa_alg(struct list_head *head, const struct ccp_rsa_def *def)
  212. {
  213. struct ccp_crypto_akcipher_alg *ccp_alg;
  214. struct akcipher_alg *alg;
  215. int ret;
  216. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  217. if (!ccp_alg)
  218. return -ENOMEM;
  219. INIT_LIST_HEAD(&ccp_alg->entry);
  220. alg = &ccp_alg->alg;
  221. *alg = *def->alg_defaults;
  222. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  223. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  224. def->driver_name);
  225. ret = crypto_register_akcipher(alg);
  226. if (ret) {
  227. pr_err("%s akcipher algorithm registration error (%d)\n",
  228. alg->base.cra_name, ret);
  229. kfree(ccp_alg);
  230. return ret;
  231. }
  232. list_add(&ccp_alg->entry, head);
  233. return 0;
  234. }
  235. int ccp_register_rsa_algs(struct list_head *head)
  236. {
  237. int i, ret;
  238. unsigned int ccpversion = ccp_version();
  239. /* Register the RSA algorithm in standard mode
  240. * This works for CCP v3 and later
  241. */
  242. for (i = 0; i < ARRAY_SIZE(rsa_algs); i++) {
  243. if (rsa_algs[i].version > ccpversion)
  244. continue;
  245. ret = ccp_register_rsa_alg(head, &rsa_algs[i]);
  246. if (ret)
  247. return ret;
  248. }
  249. return 0;
  250. }