ccp-crypto-aes-xts.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Cryptographic Coprocessor (CCP) AES XTS crypto API support
  4. *
  5. * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Gary R Hook <gary.hook@amd.com>
  8. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/delay.h>
  13. #include <linux/scatterlist.h>
  14. #include <crypto/aes.h>
  15. #include <crypto/xts.h>
  16. #include <crypto/internal/skcipher.h>
  17. #include <crypto/scatterwalk.h>
  18. #include "ccp-crypto.h"
  19. struct ccp_aes_xts_def {
  20. const char *name;
  21. const char *drv_name;
  22. };
  23. static struct ccp_aes_xts_def aes_xts_algs[] = {
  24. {
  25. .name = "xts(aes)",
  26. .drv_name = "xts-aes-ccp",
  27. },
  28. };
  29. struct ccp_unit_size_map {
  30. unsigned int size;
  31. u32 value;
  32. };
  33. static struct ccp_unit_size_map xts_unit_sizes[] = {
  34. {
  35. .size = 16,
  36. .value = CCP_XTS_AES_UNIT_SIZE_16,
  37. },
  38. {
  39. .size = 512,
  40. .value = CCP_XTS_AES_UNIT_SIZE_512,
  41. },
  42. {
  43. .size = 1024,
  44. .value = CCP_XTS_AES_UNIT_SIZE_1024,
  45. },
  46. {
  47. .size = 2048,
  48. .value = CCP_XTS_AES_UNIT_SIZE_2048,
  49. },
  50. {
  51. .size = 4096,
  52. .value = CCP_XTS_AES_UNIT_SIZE_4096,
  53. },
  54. };
  55. static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
  56. {
  57. struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
  58. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  59. if (ret)
  60. return ret;
  61. memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
  62. return 0;
  63. }
  64. static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  65. unsigned int key_len)
  66. {
  67. struct crypto_tfm *xfm = crypto_ablkcipher_tfm(tfm);
  68. struct ccp_ctx *ctx = crypto_tfm_ctx(xfm);
  69. unsigned int ccpversion = ccp_version();
  70. int ret;
  71. ret = xts_check_key(xfm, key, key_len);
  72. if (ret)
  73. return ret;
  74. /* Version 3 devices support 128-bit keys; version 5 devices can
  75. * accommodate 128- and 256-bit keys.
  76. */
  77. switch (key_len) {
  78. case AES_KEYSIZE_128 * 2:
  79. memcpy(ctx->u.aes.key, key, key_len);
  80. break;
  81. case AES_KEYSIZE_256 * 2:
  82. if (ccpversion > CCP_VERSION(3, 0))
  83. memcpy(ctx->u.aes.key, key, key_len);
  84. break;
  85. }
  86. ctx->u.aes.key_len = key_len / 2;
  87. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  88. return crypto_sync_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len);
  89. }
  90. static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
  91. unsigned int encrypt)
  92. {
  93. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  94. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  95. unsigned int ccpversion = ccp_version();
  96. unsigned int fallback = 0;
  97. unsigned int unit;
  98. u32 unit_size;
  99. int ret;
  100. if (!ctx->u.aes.key_len)
  101. return -EINVAL;
  102. if (!req->info)
  103. return -EINVAL;
  104. /* Check conditions under which the CCP can fulfill a request. The
  105. * device can handle input plaintext of a length that is a multiple
  106. * of the unit_size, bug the crypto implementation only supports
  107. * the unit_size being equal to the input length. This limits the
  108. * number of scenarios we can handle.
  109. */
  110. unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
  111. for (unit = 0; unit < ARRAY_SIZE(xts_unit_sizes); unit++) {
  112. if (req->nbytes == xts_unit_sizes[unit].size) {
  113. unit_size = unit;
  114. break;
  115. }
  116. }
  117. /* The CCP has restrictions on block sizes. Also, a version 3 device
  118. * only supports AES-128 operations; version 5 CCPs support both
  119. * AES-128 and -256 operations.
  120. */
  121. if (unit_size == CCP_XTS_AES_UNIT_SIZE__LAST)
  122. fallback = 1;
  123. if ((ccpversion < CCP_VERSION(5, 0)) &&
  124. (ctx->u.aes.key_len != AES_KEYSIZE_128))
  125. fallback = 1;
  126. if ((ctx->u.aes.key_len != AES_KEYSIZE_128) &&
  127. (ctx->u.aes.key_len != AES_KEYSIZE_256))
  128. fallback = 1;
  129. if (fallback) {
  130. SYNC_SKCIPHER_REQUEST_ON_STACK(subreq,
  131. ctx->u.aes.tfm_skcipher);
  132. /* Use the fallback to process the request for any
  133. * unsupported unit sizes or key sizes
  134. */
  135. skcipher_request_set_sync_tfm(subreq, ctx->u.aes.tfm_skcipher);
  136. skcipher_request_set_callback(subreq, req->base.flags,
  137. NULL, NULL);
  138. skcipher_request_set_crypt(subreq, req->src, req->dst,
  139. req->nbytes, req->info);
  140. ret = encrypt ? crypto_skcipher_encrypt(subreq) :
  141. crypto_skcipher_decrypt(subreq);
  142. skcipher_request_zero(subreq);
  143. return ret;
  144. }
  145. memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
  146. sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
  147. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  148. INIT_LIST_HEAD(&rctx->cmd.entry);
  149. rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
  150. rctx->cmd.u.xts.type = CCP_AES_TYPE_128;
  151. rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
  152. : CCP_AES_ACTION_DECRYPT;
  153. rctx->cmd.u.xts.unit_size = unit_size;
  154. rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
  155. rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
  156. rctx->cmd.u.xts.iv = &rctx->iv_sg;
  157. rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
  158. rctx->cmd.u.xts.src = req->src;
  159. rctx->cmd.u.xts.src_len = req->nbytes;
  160. rctx->cmd.u.xts.dst = req->dst;
  161. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  162. return ret;
  163. }
  164. static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
  165. {
  166. return ccp_aes_xts_crypt(req, 1);
  167. }
  168. static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
  169. {
  170. return ccp_aes_xts_crypt(req, 0);
  171. }
  172. static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
  173. {
  174. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  175. struct crypto_sync_skcipher *fallback_tfm;
  176. ctx->complete = ccp_aes_xts_complete;
  177. ctx->u.aes.key_len = 0;
  178. fallback_tfm = crypto_alloc_sync_skcipher("xts(aes)", 0,
  179. CRYPTO_ALG_ASYNC |
  180. CRYPTO_ALG_NEED_FALLBACK);
  181. if (IS_ERR(fallback_tfm)) {
  182. pr_warn("could not load fallback driver xts(aes)\n");
  183. return PTR_ERR(fallback_tfm);
  184. }
  185. ctx->u.aes.tfm_skcipher = fallback_tfm;
  186. tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
  187. return 0;
  188. }
  189. static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
  190. {
  191. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  192. crypto_free_sync_skcipher(ctx->u.aes.tfm_skcipher);
  193. }
  194. static int ccp_register_aes_xts_alg(struct list_head *head,
  195. const struct ccp_aes_xts_def *def)
  196. {
  197. struct ccp_crypto_ablkcipher_alg *ccp_alg;
  198. struct crypto_alg *alg;
  199. int ret;
  200. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  201. if (!ccp_alg)
  202. return -ENOMEM;
  203. INIT_LIST_HEAD(&ccp_alg->entry);
  204. alg = &ccp_alg->alg;
  205. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  206. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  207. def->drv_name);
  208. alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
  209. CRYPTO_ALG_KERN_DRIVER_ONLY |
  210. CRYPTO_ALG_NEED_FALLBACK;
  211. alg->cra_blocksize = AES_BLOCK_SIZE;
  212. alg->cra_ctxsize = sizeof(struct ccp_ctx);
  213. alg->cra_priority = CCP_CRA_PRIORITY;
  214. alg->cra_type = &crypto_ablkcipher_type;
  215. alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
  216. alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
  217. alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
  218. alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
  219. alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
  220. alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
  221. alg->cra_init = ccp_aes_xts_cra_init;
  222. alg->cra_exit = ccp_aes_xts_cra_exit;
  223. alg->cra_module = THIS_MODULE;
  224. ret = crypto_register_alg(alg);
  225. if (ret) {
  226. pr_err("%s ablkcipher algorithm registration error (%d)\n",
  227. alg->cra_name, ret);
  228. kfree(ccp_alg);
  229. return ret;
  230. }
  231. list_add(&ccp_alg->entry, head);
  232. return 0;
  233. }
  234. int ccp_register_aes_xts_algs(struct list_head *head)
  235. {
  236. int i, ret;
  237. for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
  238. ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
  239. if (ret)
  240. return ret;
  241. }
  242. return 0;
  243. }