ccp-crypto-aes.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD Cryptographic Coprocessor (CCP) AES crypto API support
  4. *
  5. * Copyright (C) 2013-2019 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/delay.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/crypto.h>
  14. #include <crypto/algapi.h>
  15. #include <crypto/aes.h>
  16. #include <crypto/ctr.h>
  17. #include <crypto/scatterwalk.h>
  18. #include "ccp-crypto.h"
  19. static int ccp_aes_complete(struct crypto_async_request *async_req, int ret)
  20. {
  21. struct skcipher_request *req = skcipher_request_cast(async_req);
  22. struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(
  23. crypto_skcipher_reqtfm(req));
  24. struct ccp_aes_req_ctx *rctx = skcipher_request_ctx_dma(req);
  25. if (ret)
  26. return ret;
  27. if (ctx->u.aes.mode != CCP_AES_MODE_ECB)
  28. memcpy(req->iv, rctx->iv, AES_BLOCK_SIZE);
  29. return 0;
  30. }
  31. static int ccp_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
  32. unsigned int key_len)
  33. {
  34. struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm);
  35. struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
  36. switch (key_len) {
  37. case AES_KEYSIZE_128:
  38. ctx->u.aes.type = CCP_AES_TYPE_128;
  39. break;
  40. case AES_KEYSIZE_192:
  41. ctx->u.aes.type = CCP_AES_TYPE_192;
  42. break;
  43. case AES_KEYSIZE_256:
  44. ctx->u.aes.type = CCP_AES_TYPE_256;
  45. break;
  46. default:
  47. return -EINVAL;
  48. }
  49. ctx->u.aes.mode = alg->mode;
  50. ctx->u.aes.key_len = key_len;
  51. memcpy(ctx->u.aes.key, key, key_len);
  52. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  53. return 0;
  54. }
  55. static int ccp_aes_crypt(struct skcipher_request *req, bool encrypt)
  56. {
  57. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  58. struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
  59. struct ccp_aes_req_ctx *rctx = skcipher_request_ctx_dma(req);
  60. struct scatterlist *iv_sg = NULL;
  61. unsigned int iv_len = 0;
  62. if (!ctx->u.aes.key_len)
  63. return -EINVAL;
  64. if (((ctx->u.aes.mode == CCP_AES_MODE_ECB) ||
  65. (ctx->u.aes.mode == CCP_AES_MODE_CBC)) &&
  66. (req->cryptlen & (AES_BLOCK_SIZE - 1)))
  67. return -EINVAL;
  68. if (ctx->u.aes.mode != CCP_AES_MODE_ECB) {
  69. if (!req->iv)
  70. return -EINVAL;
  71. memcpy(rctx->iv, req->iv, AES_BLOCK_SIZE);
  72. iv_sg = &rctx->iv_sg;
  73. iv_len = AES_BLOCK_SIZE;
  74. sg_init_one(iv_sg, rctx->iv, iv_len);
  75. }
  76. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  77. INIT_LIST_HEAD(&rctx->cmd.entry);
  78. rctx->cmd.engine = CCP_ENGINE_AES;
  79. rctx->cmd.u.aes.type = ctx->u.aes.type;
  80. rctx->cmd.u.aes.mode = ctx->u.aes.mode;
  81. rctx->cmd.u.aes.action =
  82. (encrypt) ? CCP_AES_ACTION_ENCRYPT : CCP_AES_ACTION_DECRYPT;
  83. rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
  84. rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
  85. rctx->cmd.u.aes.iv = iv_sg;
  86. rctx->cmd.u.aes.iv_len = iv_len;
  87. rctx->cmd.u.aes.src = req->src;
  88. rctx->cmd.u.aes.src_len = req->cryptlen;
  89. rctx->cmd.u.aes.dst = req->dst;
  90. return ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  91. }
  92. static int ccp_aes_encrypt(struct skcipher_request *req)
  93. {
  94. return ccp_aes_crypt(req, true);
  95. }
  96. static int ccp_aes_decrypt(struct skcipher_request *req)
  97. {
  98. return ccp_aes_crypt(req, false);
  99. }
  100. static int ccp_aes_init_tfm(struct crypto_skcipher *tfm)
  101. {
  102. struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
  103. ctx->complete = ccp_aes_complete;
  104. ctx->u.aes.key_len = 0;
  105. crypto_skcipher_set_reqsize(tfm, sizeof(struct ccp_aes_req_ctx));
  106. return 0;
  107. }
  108. static int ccp_aes_rfc3686_complete(struct crypto_async_request *async_req,
  109. int ret)
  110. {
  111. struct skcipher_request *req = skcipher_request_cast(async_req);
  112. struct ccp_aes_req_ctx *rctx = skcipher_request_ctx_dma(req);
  113. /* Restore the original pointer */
  114. req->iv = rctx->rfc3686_info;
  115. return ccp_aes_complete(async_req, ret);
  116. }
  117. static int ccp_aes_rfc3686_setkey(struct crypto_skcipher *tfm, const u8 *key,
  118. unsigned int key_len)
  119. {
  120. struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
  121. if (key_len < CTR_RFC3686_NONCE_SIZE)
  122. return -EINVAL;
  123. key_len -= CTR_RFC3686_NONCE_SIZE;
  124. memcpy(ctx->u.aes.nonce, key + key_len, CTR_RFC3686_NONCE_SIZE);
  125. return ccp_aes_setkey(tfm, key, key_len);
  126. }
  127. static int ccp_aes_rfc3686_crypt(struct skcipher_request *req, bool encrypt)
  128. {
  129. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  130. struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
  131. struct ccp_aes_req_ctx *rctx = skcipher_request_ctx_dma(req);
  132. u8 *iv;
  133. /* Initialize the CTR block */
  134. iv = rctx->rfc3686_iv;
  135. memcpy(iv, ctx->u.aes.nonce, CTR_RFC3686_NONCE_SIZE);
  136. iv += CTR_RFC3686_NONCE_SIZE;
  137. memcpy(iv, req->iv, CTR_RFC3686_IV_SIZE);
  138. iv += CTR_RFC3686_IV_SIZE;
  139. *(__be32 *)iv = cpu_to_be32(1);
  140. /* Point to the new IV */
  141. rctx->rfc3686_info = req->iv;
  142. req->iv = rctx->rfc3686_iv;
  143. return ccp_aes_crypt(req, encrypt);
  144. }
  145. static int ccp_aes_rfc3686_encrypt(struct skcipher_request *req)
  146. {
  147. return ccp_aes_rfc3686_crypt(req, true);
  148. }
  149. static int ccp_aes_rfc3686_decrypt(struct skcipher_request *req)
  150. {
  151. return ccp_aes_rfc3686_crypt(req, false);
  152. }
  153. static int ccp_aes_rfc3686_init_tfm(struct crypto_skcipher *tfm)
  154. {
  155. struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
  156. ctx->complete = ccp_aes_rfc3686_complete;
  157. ctx->u.aes.key_len = 0;
  158. crypto_skcipher_set_reqsize_dma(tfm, sizeof(struct ccp_aes_req_ctx));
  159. return 0;
  160. }
  161. static const struct skcipher_alg ccp_aes_defaults = {
  162. .setkey = ccp_aes_setkey,
  163. .encrypt = ccp_aes_encrypt,
  164. .decrypt = ccp_aes_decrypt,
  165. .min_keysize = AES_MIN_KEY_SIZE,
  166. .max_keysize = AES_MAX_KEY_SIZE,
  167. .init = ccp_aes_init_tfm,
  168. .base.cra_flags = CRYPTO_ALG_ASYNC |
  169. CRYPTO_ALG_ALLOCATES_MEMORY |
  170. CRYPTO_ALG_KERN_DRIVER_ONLY |
  171. CRYPTO_ALG_NEED_FALLBACK,
  172. .base.cra_blocksize = AES_BLOCK_SIZE,
  173. .base.cra_ctxsize = sizeof(struct ccp_ctx) + CRYPTO_DMA_PADDING,
  174. .base.cra_priority = CCP_CRA_PRIORITY,
  175. .base.cra_module = THIS_MODULE,
  176. };
  177. static const struct skcipher_alg ccp_aes_rfc3686_defaults = {
  178. .setkey = ccp_aes_rfc3686_setkey,
  179. .encrypt = ccp_aes_rfc3686_encrypt,
  180. .decrypt = ccp_aes_rfc3686_decrypt,
  181. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  182. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  183. .init = ccp_aes_rfc3686_init_tfm,
  184. .base.cra_flags = CRYPTO_ALG_ASYNC |
  185. CRYPTO_ALG_ALLOCATES_MEMORY |
  186. CRYPTO_ALG_KERN_DRIVER_ONLY |
  187. CRYPTO_ALG_NEED_FALLBACK,
  188. .base.cra_blocksize = CTR_RFC3686_BLOCK_SIZE,
  189. .base.cra_ctxsize = sizeof(struct ccp_ctx) + CRYPTO_DMA_PADDING,
  190. .base.cra_priority = CCP_CRA_PRIORITY,
  191. .base.cra_module = THIS_MODULE,
  192. };
  193. struct ccp_aes_def {
  194. enum ccp_aes_mode mode;
  195. unsigned int version;
  196. const char *name;
  197. const char *driver_name;
  198. unsigned int blocksize;
  199. unsigned int ivsize;
  200. const struct skcipher_alg *alg_defaults;
  201. };
  202. static struct ccp_aes_def aes_algs[] = {
  203. {
  204. .mode = CCP_AES_MODE_ECB,
  205. .version = CCP_VERSION(3, 0),
  206. .name = "ecb(aes)",
  207. .driver_name = "ecb-aes-ccp",
  208. .blocksize = AES_BLOCK_SIZE,
  209. .ivsize = 0,
  210. .alg_defaults = &ccp_aes_defaults,
  211. },
  212. {
  213. .mode = CCP_AES_MODE_CBC,
  214. .version = CCP_VERSION(3, 0),
  215. .name = "cbc(aes)",
  216. .driver_name = "cbc-aes-ccp",
  217. .blocksize = AES_BLOCK_SIZE,
  218. .ivsize = AES_BLOCK_SIZE,
  219. .alg_defaults = &ccp_aes_defaults,
  220. },
  221. {
  222. .mode = CCP_AES_MODE_CTR,
  223. .version = CCP_VERSION(3, 0),
  224. .name = "ctr(aes)",
  225. .driver_name = "ctr-aes-ccp",
  226. .blocksize = 1,
  227. .ivsize = AES_BLOCK_SIZE,
  228. .alg_defaults = &ccp_aes_defaults,
  229. },
  230. {
  231. .mode = CCP_AES_MODE_CTR,
  232. .version = CCP_VERSION(3, 0),
  233. .name = "rfc3686(ctr(aes))",
  234. .driver_name = "rfc3686-ctr-aes-ccp",
  235. .blocksize = 1,
  236. .ivsize = CTR_RFC3686_IV_SIZE,
  237. .alg_defaults = &ccp_aes_rfc3686_defaults,
  238. },
  239. };
  240. static int ccp_register_aes_alg(struct list_head *head,
  241. const struct ccp_aes_def *def)
  242. {
  243. struct ccp_crypto_skcipher_alg *ccp_alg;
  244. struct skcipher_alg *alg;
  245. int ret;
  246. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  247. if (!ccp_alg)
  248. return -ENOMEM;
  249. INIT_LIST_HEAD(&ccp_alg->entry);
  250. ccp_alg->mode = def->mode;
  251. /* Copy the defaults and override as necessary */
  252. alg = &ccp_alg->alg;
  253. *alg = *def->alg_defaults;
  254. snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  255. snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  256. def->driver_name);
  257. alg->base.cra_blocksize = def->blocksize;
  258. alg->ivsize = def->ivsize;
  259. ret = crypto_register_skcipher(alg);
  260. if (ret) {
  261. pr_err("%s skcipher algorithm registration error (%d)\n",
  262. alg->base.cra_name, ret);
  263. kfree(ccp_alg);
  264. return ret;
  265. }
  266. list_add(&ccp_alg->entry, head);
  267. return 0;
  268. }
  269. int ccp_register_aes_algs(struct list_head *head)
  270. {
  271. int i, ret;
  272. unsigned int ccpversion = ccp_version();
  273. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  274. if (aes_algs[i].version > ccpversion)
  275. continue;
  276. ret = ccp_register_aes_alg(head, &aes_algs[i]);
  277. if (ret)
  278. return ret;
  279. }
  280. return 0;
  281. }