ccp-crypto-main.c 10 KB

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