ccp-crypto-main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Cryptographic Coprocessor (CCP) crypto API support
  4. *
  5. * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/kernel.h>
  12. #include <linux/list.h>
  13. #include <linux/ccp.h>
  14. #include <linux/scatterlist.h>
  15. #include <crypto/internal/hash.h>
  16. #include <crypto/internal/akcipher.h>
  17. #include "ccp-crypto.h"
  18. MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
  19. MODULE_LICENSE("GPL");
  20. MODULE_VERSION("1.0.0");
  21. MODULE_DESCRIPTION("AMD Cryptographic Coprocessor crypto API support");
  22. static unsigned int aes_disable;
  23. module_param(aes_disable, uint, 0444);
  24. MODULE_PARM_DESC(aes_disable, "Disable use of AES - any non-zero value");
  25. static unsigned int sha_disable;
  26. module_param(sha_disable, uint, 0444);
  27. MODULE_PARM_DESC(sha_disable, "Disable use of SHA - any non-zero value");
  28. static unsigned int des3_disable;
  29. module_param(des3_disable, uint, 0444);
  30. MODULE_PARM_DESC(des3_disable, "Disable use of 3DES - any non-zero value");
  31. static unsigned int rsa_disable;
  32. module_param(rsa_disable, uint, 0444);
  33. MODULE_PARM_DESC(rsa_disable, "Disable use of RSA - any non-zero value");
  34. /* List heads for the supported algorithms */
  35. static LIST_HEAD(hash_algs);
  36. static LIST_HEAD(cipher_algs);
  37. static LIST_HEAD(aead_algs);
  38. static LIST_HEAD(akcipher_algs);
  39. /* For any tfm, requests for that tfm must be returned on the order
  40. * received. With multiple queues available, the CCP can process more
  41. * than one cmd at a time. Therefore we must maintain a cmd list to insure
  42. * the proper ordering of requests on a given tfm.
  43. */
  44. struct ccp_crypto_queue {
  45. struct list_head cmds;
  46. struct list_head *backlog;
  47. unsigned int cmd_count;
  48. };
  49. #define CCP_CRYPTO_MAX_QLEN 100
  50. static struct ccp_crypto_queue req_queue;
  51. static spinlock_t req_queue_lock;
  52. struct ccp_crypto_cmd {
  53. struct list_head entry;
  54. struct ccp_cmd *cmd;
  55. /* Save the crypto_tfm and crypto_async_request addresses
  56. * separately to avoid any reference to a possibly invalid
  57. * crypto_async_request structure after invoking the request
  58. * callback
  59. */
  60. struct crypto_async_request *req;
  61. struct crypto_tfm *tfm;
  62. /* Used for held command processing to determine state */
  63. int ret;
  64. };
  65. struct ccp_crypto_cpu {
  66. struct work_struct work;
  67. struct completion completion;
  68. struct ccp_crypto_cmd *crypto_cmd;
  69. int err;
  70. };
  71. static inline bool ccp_crypto_success(int err)
  72. {
  73. if (err && (err != -EINPROGRESS) && (err != -EBUSY))
  74. return false;
  75. return true;
  76. }
  77. static struct ccp_crypto_cmd *ccp_crypto_cmd_complete(
  78. struct ccp_crypto_cmd *crypto_cmd, struct ccp_crypto_cmd **backlog)
  79. {
  80. struct ccp_crypto_cmd *held = NULL, *tmp;
  81. unsigned long flags;
  82. *backlog = NULL;
  83. spin_lock_irqsave(&req_queue_lock, flags);
  84. /* Held cmds will be after the current cmd in the queue so start
  85. * searching for a cmd with a matching tfm for submission.
  86. */
  87. tmp = crypto_cmd;
  88. list_for_each_entry_continue(tmp, &req_queue.cmds, entry) {
  89. if (crypto_cmd->tfm != tmp->tfm)
  90. continue;
  91. held = tmp;
  92. break;
  93. }
  94. /* Process the backlog:
  95. * Because cmds can be executed from any point in the cmd list
  96. * special precautions have to be taken when handling the backlog.
  97. */
  98. if (req_queue.backlog != &req_queue.cmds) {
  99. /* Skip over this cmd if it is the next backlog cmd */
  100. if (req_queue.backlog == &crypto_cmd->entry)
  101. req_queue.backlog = crypto_cmd->entry.next;
  102. *backlog = container_of(req_queue.backlog,
  103. struct ccp_crypto_cmd, entry);
  104. req_queue.backlog = req_queue.backlog->next;
  105. /* Skip over this cmd if it is now the next backlog cmd */
  106. if (req_queue.backlog == &crypto_cmd->entry)
  107. req_queue.backlog = crypto_cmd->entry.next;
  108. }
  109. /* Remove the cmd entry from the list of cmds */
  110. req_queue.cmd_count--;
  111. list_del(&crypto_cmd->entry);
  112. spin_unlock_irqrestore(&req_queue_lock, flags);
  113. return held;
  114. }
  115. static void ccp_crypto_complete(void *data, int err)
  116. {
  117. struct ccp_crypto_cmd *crypto_cmd = data;
  118. struct ccp_crypto_cmd *held, *next, *backlog;
  119. struct crypto_async_request *req = crypto_cmd->req;
  120. struct ccp_ctx *ctx = crypto_tfm_ctx(req->tfm);
  121. int ret;
  122. if (err == -EINPROGRESS) {
  123. /* Only propagate the -EINPROGRESS if necessary */
  124. if (crypto_cmd->ret == -EBUSY) {
  125. crypto_cmd->ret = -EINPROGRESS;
  126. req->complete(req, -EINPROGRESS);
  127. }
  128. return;
  129. }
  130. /* Operation has completed - update the queue before invoking
  131. * the completion callbacks and retrieve the next cmd (cmd with
  132. * a matching tfm) that can be submitted to the CCP.
  133. */
  134. held = ccp_crypto_cmd_complete(crypto_cmd, &backlog);
  135. if (backlog) {
  136. backlog->ret = -EINPROGRESS;
  137. backlog->req->complete(backlog->req, -EINPROGRESS);
  138. }
  139. /* Transition the state from -EBUSY to -EINPROGRESS first */
  140. if (crypto_cmd->ret == -EBUSY)
  141. req->complete(req, -EINPROGRESS);
  142. /* Completion callbacks */
  143. ret = err;
  144. if (ctx->complete)
  145. ret = ctx->complete(req, ret);
  146. req->complete(req, ret);
  147. /* Submit the next cmd */
  148. while (held) {
  149. /* Since we have already queued the cmd, we must indicate that
  150. * we can backlog so as not to "lose" this request.
  151. */
  152. held->cmd->flags |= CCP_CMD_MAY_BACKLOG;
  153. ret = ccp_enqueue_cmd(held->cmd);
  154. if (ccp_crypto_success(ret))
  155. break;
  156. /* Error occurred, report it and get the next entry */
  157. ctx = crypto_tfm_ctx(held->req->tfm);
  158. if (ctx->complete)
  159. ret = ctx->complete(held->req, ret);
  160. held->req->complete(held->req, ret);
  161. next = ccp_crypto_cmd_complete(held, &backlog);
  162. if (backlog) {
  163. backlog->ret = -EINPROGRESS;
  164. backlog->req->complete(backlog->req, -EINPROGRESS);
  165. }
  166. kfree(held);
  167. held = next;
  168. }
  169. kfree(crypto_cmd);
  170. }
  171. static int ccp_crypto_enqueue_cmd(struct ccp_crypto_cmd *crypto_cmd)
  172. {
  173. struct ccp_crypto_cmd *active = NULL, *tmp;
  174. unsigned long flags;
  175. bool free_cmd = true;
  176. int ret;
  177. spin_lock_irqsave(&req_queue_lock, flags);
  178. /* Check if the cmd can/should be queued */
  179. if (req_queue.cmd_count >= CCP_CRYPTO_MAX_QLEN) {
  180. if (!(crypto_cmd->cmd->flags & CCP_CMD_MAY_BACKLOG)) {
  181. ret = -ENOSPC;
  182. goto e_lock;
  183. }
  184. }
  185. /* Look for an entry with the same tfm. If there is a cmd
  186. * with the same tfm in the list then the current cmd cannot
  187. * be submitted to the CCP yet.
  188. */
  189. list_for_each_entry(tmp, &req_queue.cmds, entry) {
  190. if (crypto_cmd->tfm != tmp->tfm)
  191. continue;
  192. active = tmp;
  193. break;
  194. }
  195. ret = -EINPROGRESS;
  196. if (!active) {
  197. ret = ccp_enqueue_cmd(crypto_cmd->cmd);
  198. if (!ccp_crypto_success(ret))
  199. goto e_lock; /* Error, don't queue it */
  200. }
  201. if (req_queue.cmd_count >= CCP_CRYPTO_MAX_QLEN) {
  202. ret = -EBUSY;
  203. if (req_queue.backlog == &req_queue.cmds)
  204. req_queue.backlog = &crypto_cmd->entry;
  205. }
  206. crypto_cmd->ret = ret;
  207. req_queue.cmd_count++;
  208. list_add_tail(&crypto_cmd->entry, &req_queue.cmds);
  209. free_cmd = false;
  210. e_lock:
  211. spin_unlock_irqrestore(&req_queue_lock, flags);
  212. if (free_cmd)
  213. kfree(crypto_cmd);
  214. return ret;
  215. }
  216. /**
  217. * ccp_crypto_enqueue_request - queue an crypto async request for processing
  218. * by the CCP
  219. *
  220. * @req: crypto_async_request struct to be processed
  221. * @cmd: ccp_cmd struct to be sent to the CCP
  222. */
  223. int ccp_crypto_enqueue_request(struct crypto_async_request *req,
  224. struct ccp_cmd *cmd)
  225. {
  226. struct ccp_crypto_cmd *crypto_cmd;
  227. gfp_t gfp;
  228. gfp = req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
  229. crypto_cmd = kzalloc(sizeof(*crypto_cmd), gfp);
  230. if (!crypto_cmd)
  231. return -ENOMEM;
  232. /* The tfm pointer must be saved and not referenced from the
  233. * crypto_async_request (req) pointer because it is used after
  234. * completion callback for the request and the req pointer
  235. * might not be valid anymore.
  236. */
  237. crypto_cmd->cmd = cmd;
  238. crypto_cmd->req = req;
  239. crypto_cmd->tfm = req->tfm;
  240. cmd->callback = ccp_crypto_complete;
  241. cmd->data = crypto_cmd;
  242. if (req->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)
  243. cmd->flags |= CCP_CMD_MAY_BACKLOG;
  244. else
  245. cmd->flags &= ~CCP_CMD_MAY_BACKLOG;
  246. return ccp_crypto_enqueue_cmd(crypto_cmd);
  247. }
  248. struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table,
  249. struct scatterlist *sg_add)
  250. {
  251. struct scatterlist *sg, *sg_last = NULL;
  252. for (sg = table->sgl; sg; sg = sg_next(sg))
  253. if (!sg_page(sg))
  254. break;
  255. if (WARN_ON(!sg))
  256. return NULL;
  257. for (; sg && sg_add; sg = sg_next(sg), sg_add = sg_next(sg_add)) {
  258. sg_set_page(sg, sg_page(sg_add), sg_add->length,
  259. sg_add->offset);
  260. sg_last = sg;
  261. }
  262. if (WARN_ON(sg_add))
  263. return NULL;
  264. return sg_last;
  265. }
  266. static int ccp_register_algs(void)
  267. {
  268. int ret;
  269. if (!aes_disable) {
  270. ret = ccp_register_aes_algs(&cipher_algs);
  271. if (ret)
  272. return ret;
  273. ret = ccp_register_aes_cmac_algs(&hash_algs);
  274. if (ret)
  275. return ret;
  276. ret = ccp_register_aes_xts_algs(&cipher_algs);
  277. if (ret)
  278. return ret;
  279. ret = ccp_register_aes_aeads(&aead_algs);
  280. if (ret)
  281. return ret;
  282. }
  283. if (!des3_disable) {
  284. ret = ccp_register_des3_algs(&cipher_algs);
  285. if (ret)
  286. return ret;
  287. }
  288. if (!sha_disable) {
  289. ret = ccp_register_sha_algs(&hash_algs);
  290. if (ret)
  291. return ret;
  292. }
  293. if (!rsa_disable) {
  294. ret = ccp_register_rsa_algs(&akcipher_algs);
  295. if (ret)
  296. return ret;
  297. }
  298. return 0;
  299. }
  300. static void ccp_unregister_algs(void)
  301. {
  302. struct ccp_crypto_ahash_alg *ahash_alg, *ahash_tmp;
  303. struct ccp_crypto_ablkcipher_alg *ablk_alg, *ablk_tmp;
  304. struct ccp_crypto_aead *aead_alg, *aead_tmp;
  305. struct ccp_crypto_akcipher_alg *akc_alg, *akc_tmp;
  306. list_for_each_entry_safe(ahash_alg, ahash_tmp, &hash_algs, entry) {
  307. crypto_unregister_ahash(&ahash_alg->alg);
  308. list_del(&ahash_alg->entry);
  309. kfree(ahash_alg);
  310. }
  311. list_for_each_entry_safe(ablk_alg, ablk_tmp, &cipher_algs, entry) {
  312. crypto_unregister_alg(&ablk_alg->alg);
  313. list_del(&ablk_alg->entry);
  314. kfree(ablk_alg);
  315. }
  316. list_for_each_entry_safe(aead_alg, aead_tmp, &aead_algs, entry) {
  317. crypto_unregister_aead(&aead_alg->alg);
  318. list_del(&aead_alg->entry);
  319. kfree(aead_alg);
  320. }
  321. list_for_each_entry_safe(akc_alg, akc_tmp, &akcipher_algs, entry) {
  322. crypto_unregister_akcipher(&akc_alg->alg);
  323. list_del(&akc_alg->entry);
  324. kfree(akc_alg);
  325. }
  326. }
  327. static int ccp_crypto_init(void)
  328. {
  329. int ret;
  330. ret = ccp_present();
  331. if (ret) {
  332. pr_err("Cannot load: there are no available CCPs\n");
  333. return ret;
  334. }
  335. spin_lock_init(&req_queue_lock);
  336. INIT_LIST_HEAD(&req_queue.cmds);
  337. req_queue.backlog = &req_queue.cmds;
  338. req_queue.cmd_count = 0;
  339. ret = ccp_register_algs();
  340. if (ret)
  341. ccp_unregister_algs();
  342. return ret;
  343. }
  344. static void ccp_crypto_exit(void)
  345. {
  346. ccp_unregister_algs();
  347. }
  348. module_init(ccp_crypto_init);
  349. module_exit(ccp_crypto_exit);