ccp-crypto-aes-xts.c 6.7 KB

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