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 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/sched.h>
  14. #include <linux/delay.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/crypto.h>
  17. #include <crypto/algapi.h>
  18. #include <crypto/aes.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_ablkcipher_setkey(ctx->u.aes.tfm_ablkcipher, key,
  101. key_len);
  102. }
  103. static int ccp_aes_xts_crypt(struct ablkcipher_request *req,
  104. unsigned int encrypt)
  105. {
  106. struct crypto_tfm *tfm =
  107. crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
  108. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  109. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  110. unsigned int unit;
  111. u32 unit_size;
  112. int ret;
  113. if (!ctx->u.aes.key_len)
  114. return -EINVAL;
  115. if (req->nbytes & (AES_BLOCK_SIZE - 1))
  116. return -EINVAL;
  117. if (!req->info)
  118. return -EINVAL;
  119. unit_size = CCP_XTS_AES_UNIT_SIZE__LAST;
  120. if (req->nbytes <= unit_size_map[0].size) {
  121. for (unit = 0; unit < ARRAY_SIZE(unit_size_map); unit++) {
  122. if (!(req->nbytes & (unit_size_map[unit].size - 1))) {
  123. unit_size = unit_size_map[unit].value;
  124. break;
  125. }
  126. }
  127. }
  128. if ((unit_size == CCP_XTS_AES_UNIT_SIZE__LAST) ||
  129. (ctx->u.aes.key_len != AES_KEYSIZE_128)) {
  130. /* Use the fallback to process the request for any
  131. * unsupported unit sizes or key sizes
  132. */
  133. ablkcipher_request_set_tfm(req, ctx->u.aes.tfm_ablkcipher);
  134. ret = (encrypt) ? crypto_ablkcipher_encrypt(req) :
  135. crypto_ablkcipher_decrypt(req);
  136. ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(tfm));
  137. return ret;
  138. }
  139. memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
  140. sg_init_one(&rctx->iv_sg, rctx->iv, AES_BLOCK_SIZE);
  141. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  142. INIT_LIST_HEAD(&rctx->cmd.entry);
  143. rctx->cmd.engine = CCP_ENGINE_XTS_AES_128;
  144. rctx->cmd.u.xts.action = (encrypt) ? CCP_AES_ACTION_ENCRYPT
  145. : CCP_AES_ACTION_DECRYPT;
  146. rctx->cmd.u.xts.unit_size = unit_size;
  147. rctx->cmd.u.xts.key = &ctx->u.aes.key_sg;
  148. rctx->cmd.u.xts.key_len = ctx->u.aes.key_len;
  149. rctx->cmd.u.xts.iv = &rctx->iv_sg;
  150. rctx->cmd.u.xts.iv_len = AES_BLOCK_SIZE;
  151. rctx->cmd.u.xts.src = req->src;
  152. rctx->cmd.u.xts.src_len = req->nbytes;
  153. rctx->cmd.u.xts.dst = req->dst;
  154. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  155. return ret;
  156. }
  157. static int ccp_aes_xts_encrypt(struct ablkcipher_request *req)
  158. {
  159. return ccp_aes_xts_crypt(req, 1);
  160. }
  161. static int ccp_aes_xts_decrypt(struct ablkcipher_request *req)
  162. {
  163. return ccp_aes_xts_crypt(req, 0);
  164. }
  165. static int ccp_aes_xts_cra_init(struct crypto_tfm *tfm)
  166. {
  167. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  168. struct crypto_ablkcipher *fallback_tfm;
  169. ctx->complete = ccp_aes_xts_complete;
  170. ctx->u.aes.key_len = 0;
  171. fallback_tfm = crypto_alloc_ablkcipher(crypto_tfm_alg_name(tfm), 0,
  172. CRYPTO_ALG_ASYNC |
  173. CRYPTO_ALG_NEED_FALLBACK);
  174. if (IS_ERR(fallback_tfm)) {
  175. pr_warn("could not load fallback driver %s\n",
  176. crypto_tfm_alg_name(tfm));
  177. return PTR_ERR(fallback_tfm);
  178. }
  179. ctx->u.aes.tfm_ablkcipher = fallback_tfm;
  180. tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx) +
  181. fallback_tfm->base.crt_ablkcipher.reqsize;
  182. return 0;
  183. }
  184. static void ccp_aes_xts_cra_exit(struct crypto_tfm *tfm)
  185. {
  186. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  187. if (ctx->u.aes.tfm_ablkcipher)
  188. crypto_free_ablkcipher(ctx->u.aes.tfm_ablkcipher);
  189. ctx->u.aes.tfm_ablkcipher = NULL;
  190. }
  191. static int ccp_register_aes_xts_alg(struct list_head *head,
  192. const struct ccp_aes_xts_def *def)
  193. {
  194. struct ccp_crypto_ablkcipher_alg *ccp_alg;
  195. struct crypto_alg *alg;
  196. int ret;
  197. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  198. if (!ccp_alg)
  199. return -ENOMEM;
  200. INIT_LIST_HEAD(&ccp_alg->entry);
  201. alg = &ccp_alg->alg;
  202. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  203. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  204. def->drv_name);
  205. alg->cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
  206. CRYPTO_ALG_KERN_DRIVER_ONLY |
  207. CRYPTO_ALG_NEED_FALLBACK;
  208. alg->cra_blocksize = AES_BLOCK_SIZE;
  209. alg->cra_ctxsize = sizeof(struct ccp_ctx);
  210. alg->cra_priority = CCP_CRA_PRIORITY;
  211. alg->cra_type = &crypto_ablkcipher_type;
  212. alg->cra_ablkcipher.setkey = ccp_aes_xts_setkey;
  213. alg->cra_ablkcipher.encrypt = ccp_aes_xts_encrypt;
  214. alg->cra_ablkcipher.decrypt = ccp_aes_xts_decrypt;
  215. alg->cra_ablkcipher.min_keysize = AES_MIN_KEY_SIZE * 2;
  216. alg->cra_ablkcipher.max_keysize = AES_MAX_KEY_SIZE * 2;
  217. alg->cra_ablkcipher.ivsize = AES_BLOCK_SIZE;
  218. alg->cra_init = ccp_aes_xts_cra_init;
  219. alg->cra_exit = ccp_aes_xts_cra_exit;
  220. alg->cra_module = THIS_MODULE;
  221. ret = crypto_register_alg(alg);
  222. if (ret) {
  223. pr_err("%s ablkcipher algorithm registration error (%d)\n",
  224. alg->cra_name, ret);
  225. kfree(ccp_alg);
  226. return ret;
  227. }
  228. list_add(&ccp_alg->entry, head);
  229. return 0;
  230. }
  231. int ccp_register_aes_xts_algs(struct list_head *head)
  232. {
  233. int i, ret;
  234. for (i = 0; i < ARRAY_SIZE(aes_xts_algs); i++) {
  235. ret = ccp_register_aes_xts_alg(head, &aes_xts_algs[i]);
  236. if (ret)
  237. return ret;
  238. }
  239. return 0;
  240. }