ccp-crypto-aes-xts.c 7.5 KB

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