ccp-crypto-aes-xts.c 6.8 KB

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