ccp-crypto-aes.c 8.8 KB

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