ccp-crypto-aes.c 8.5 KB

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