ccp-crypto-aes-xts.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/internal/skcipher.h>
  19. #include <crypto/scatterwalk.h>
  20. #include "ccp-crypto.h"
  21. struct ccp_aes_xts_def {
  22. const char *name;
  23. const char *drv_name;
  24. };
  25. static struct ccp_aes_xts_def aes_xts_algs[] = {
  26. {
  27. .name = "xts(aes)",
  28. .drv_name = "xts-aes-ccp",
  29. },
  30. };
  31. struct ccp_unit_size_map {
  32. unsigned int size;
  33. u32 value;
  34. };
  35. static struct ccp_unit_size_map unit_size_map[] = {
  36. {
  37. .size = 4096,
  38. .value = CCP_XTS_AES_UNIT_SIZE_4096,
  39. },
  40. {
  41. .size = 2048,
  42. .value = CCP_XTS_AES_UNIT_SIZE_2048,
  43. },
  44. {
  45. .size = 1024,
  46. .value = CCP_XTS_AES_UNIT_SIZE_1024,
  47. },
  48. {
  49. .size = 512,
  50. .value = CCP_XTS_AES_UNIT_SIZE_512,
  51. },
  52. {
  53. .size = 256,
  54. .value = CCP_XTS_AES_UNIT_SIZE__LAST,
  55. },
  56. {
  57. .size = 128,
  58. .value = CCP_XTS_AES_UNIT_SIZE__LAST,
  59. },
  60. {
  61. .size = 64,
  62. .value = CCP_XTS_AES_UNIT_SIZE__LAST,
  63. },
  64. {
  65. .size = 32,
  66. .value = CCP_XTS_AES_UNIT_SIZE__LAST,
  67. },
  68. {
  69. .size = 16,
  70. .value = CCP_XTS_AES_UNIT_SIZE_16,
  71. },
  72. {
  73. .size = 1,
  74. .value = CCP_XTS_AES_UNIT_SIZE__LAST,
  75. },
  76. };
  77. static int ccp_aes_xts_complete(struct crypto_async_request *async_req, int ret)
  78. {
  79. struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
  80. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  81. if (ret)
  82. return ret;
  83. memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
  84. return 0;
  85. }
  86. static int ccp_aes_xts_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  87. unsigned int key_len)
  88. {
  89. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
  90. /* Only support 128-bit AES key with a 128-bit Tweak key,
  91. * otherwise use the fallback
  92. */
  93. switch (key_len) {
  94. case AES_KEYSIZE_128 * 2:
  95. memcpy(ctx->u.aes.key, key, key_len);
  96. break;
  97. }
  98. ctx->u.aes.key_len = key_len / 2;
  99. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  100. return crypto_skcipher_setkey(ctx->u.aes.tfm_skcipher, key, key_len);
  101. }
  102. static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
  103. unsigned int encrypt)
  104. {
  105. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  106. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  107. unsigned int unit;
  108. u32 unit_size;
  109. int ret;
  110. if (!ctx->u.aes.key_len)
  111. return -EINVAL;
  112. if (req->nbytes & (AES_BLOCK_SIZE - 1))
  113. return -EINVAL;
  114. if (!req->info)
  115. return -EINVAL;
  116. unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
  117. if (req->nbytes <= unit_size_map[0].size) {
  118. for (unit = 0; unit < ARRAY_SIZE(unit_size_map); unit++) {
  119. if (!(req->nbytes & (unit_size_map[unit].size - 1))) {
  120. unit_size = unit_size_map[unit].value;
  121. break;
  122. }
  123. }
  124. }
  125. if ((unit_size == CCP_XTS_AES_UNIT_SIZE__LAST) ||
  126. (ctx->u.aes.key_len != AES_KEYSIZE_128)) {
  127. SKCIPHER_REQUEST_ON_STACK(subreq, ctx->u.aes.tfm_skcipher);
  128. /* Use the fallback to process the request for any
  129. * unsupported unit sizes or key sizes
  130. */
  131. skcipher_request_set_tfm(subreq, ctx->u.aes.tfm_skcipher);
  132. skcipher_request_set_callback(subreq, req->base.flags,
  133. NULL, NULL);
  134. skcipher_request_set_crypt(subreq, req->src, req->dst,
  135. req->nbytes, req->info);
  136. ret = encrypt ? crypto_skcipher_encrypt(subreq) :
  137. crypto_skcipher_decrypt(subreq);
  138. skcipher_request_zero(subreq);
  139. return ret;
  140. }
  141. memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
  142. sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
  143. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  144. INIT_LIST_HEAD(&rctx->cmd.entry);
  145. rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
  146. rctx->cmd.u.xts.type = CCP_AES_TYPE_128;
  147. rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
  148. : CCP_AES_ACTION_DECRYPT;
  149. rctx->cmd.u.xts.unit_size = unit_size;
  150. rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
  151. rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
  152. rctx->cmd.u.xts.iv = &rctx->iv_sg;
  153. rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
  154. rctx->cmd.u.xts.src = req->src;
  155. rctx->cmd.u.xts.src_len = req->nbytes;
  156. rctx->cmd.u.xts.dst = req->dst;
  157. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  158. return ret;
  159. }
  160. static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
  161. {
  162. return ccp_aes_xts_crypt(req, 1);
  163. }
  164. static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
  165. {
  166. return ccp_aes_xts_crypt(req, 0);
  167. }
  168. static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
  169. {
  170. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  171. struct crypto_skcipher *fallback_tfm;
  172. ctx->complete = ccp_aes_xts_complete;
  173. ctx->u.aes.key_len = 0;
  174. fallback_tfm = crypto_alloc_skcipher("xts(aes)", 0,
  175. CRYPTO_ALG_ASYNC |
  176. CRYPTO_ALG_NEED_FALLBACK);
  177. if (IS_ERR(fallback_tfm)) {
  178. pr_warn("could not load fallback driver xts(aes)\n");
  179. return PTR_ERR(fallback_tfm);
  180. }
  181. ctx->u.aes.tfm_skcipher = fallback_tfm;
  182. tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
  183. return 0;
  184. }
  185. static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
  186. {
  187. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  188. crypto_free_skcipher(ctx->u.aes.tfm_skcipher);
  189. }
  190. static int ccp_register_aes_xts_alg(struct list_head *head,
  191. const struct ccp_aes_xts_def *def)
  192. {
  193. struct ccp_crypto_ablkcipher_alg *ccp_alg;
  194. struct crypto_alg *alg;
  195. int ret;
  196. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  197. if (!ccp_alg)
  198. return -ENOMEM;
  199. INIT_LIST_HEAD(&ccp_alg->entry);
  200. alg = &ccp_alg->alg;
  201. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  202. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  203. def->drv_name);
  204. alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
  205. CRYPTO_ALG_KERN_DRIVER_ONLY |
  206. CRYPTO_ALG_NEED_FALLBACK;
  207. alg->cra_blocksize = AES_BLOCK_SIZE;
  208. alg->cra_ctxsize = sizeof(struct ccp_ctx);
  209. alg->cra_priority = CCP_CRA_PRIORITY;
  210. alg->cra_type = &crypto_ablkcipher_type;
  211. alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
  212. alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
  213. alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
  214. alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
  215. alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
  216. alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
  217. alg->cra_init = ccp_aes_xts_cra_init;
  218. alg->cra_exit = ccp_aes_xts_cra_exit;
  219. alg->cra_module = THIS_MODULE;
  220. ret = crypto_register_alg(alg);
  221. if (ret) {
  222. pr_err("%s ablkcipher algorithm registration error (%d)\n",
  223. alg->cra_name, ret);
  224. kfree(ccp_alg);
  225. return ret;
  226. }
  227. list_add(&ccp_alg->entry, head);
  228. return 0;
  229. }
  230. int ccp_register_aes_xts_algs(struct list_head *head)
  231. {
  232. int i, ret;
  233. for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
  234. ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
  235. if (ret)
  236. return ret;
  237. }
  238. return 0;
  239. }