ccp-crypto-aes.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0
  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. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/delay.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/crypto.h>
  18. #include <crypto/algapi.h>
  19. #include <crypto/aes.h>
  20. #include <crypto/ctr.h>
  21. #include <crypto/scatterwalk.h>
  22. #include "ccp-crypto.h"
  23. static int ccp_aes_complete(struct crypto_async_request *async_req, int ret)
  24. {
  25. struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
  26. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  27. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  28. if (ret)
  29. return ret;
  30. if (ctx->u.aes.mode != CCP_AES_MODE_ECB)
  31. memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
  32. return 0;
  33. }
  34. static int ccp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  35. unsigned int key_len)
  36. {
  37. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
  38. struct ccp_crypto_ablkcipher_alg *alg =
  39. ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(tfm));
  40. switch (key_len) {
  41. case AES_KEYSIZE_128:
  42. ctx->u.aes.type = CCP_AES_TYPE_128;
  43. break;
  44. case AES_KEYSIZE_192:
  45. ctx->u.aes.type = CCP_AES_TYPE_192;
  46. break;
  47. case AES_KEYSIZE_256:
  48. ctx->u.aes.type = CCP_AES_TYPE_256;
  49. break;
  50. default:
  51. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  52. return -EINVAL;
  53. }
  54. ctx->u.aes.mode = alg->mode;
  55. ctx->u.aes.key_len = key_len;
  56. memcpy(ctx->u.aes.key, key, key_len);
  57. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  58. return 0;
  59. }
  60. static int ccp_aes_crypt(struct ablkcipher_request *req, bool encrypt)
  61. {
  62. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  63. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  64. struct scatterlist *iv_sg = NULL;
  65. unsigned int iv_len = 0;
  66. int ret;
  67. if (!ctx->u.aes.key_len)
  68. return -EINVAL;
  69. if (((ctx->u.aes.mode == CCP_AES_MODE_ECB) ||
  70. (ctx->u.aes.mode == CCP_AES_MODE_CBC)) &&
  71. (req->nbytes & (AES_BLOCK_SIZE - 1)))
  72. return -EINVAL;
  73. if (ctx->u.aes.mode != CCP_AES_MODE_ECB) {
  74. if (!req->info)
  75. return -EINVAL;
  76. memcpy(rctx->iv, req->info, AES_BLOCK_SIZE);
  77. iv_sg = &rctx->iv_sg;
  78. iv_len = AES_BLOCK_SIZE;
  79. sg_init_one(iv_sg, rctx->iv, iv_len);
  80. }
  81. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  82. INIT_LIST_HEAD(&rctx->cmd.entry);
  83. rctx->cmd.engine = CCP_ENGINE_AES;
  84. rctx->cmd.u.aes.type = ctx->u.aes.type;
  85. rctx->cmd.u.aes.mode = ctx->u.aes.mode;
  86. rctx->cmd.u.aes.action =
  87. (encrypt) ? CCP_AES_ACTION_ENCRYPT : CCP_AES_ACTION_DECRYPT;
  88. rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
  89. rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
  90. rctx->cmd.u.aes.iv = iv_sg;
  91. rctx->cmd.u.aes.iv_len = iv_len;
  92. rctx->cmd.u.aes.src = req->src;
  93. rctx->cmd.u.aes.src_len = req->nbytes;
  94. rctx->cmd.u.aes.dst = req->dst;
  95. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  96. return ret;
  97. }
  98. static int ccp_aes_encrypt(struct ablkcipher_request *req)
  99. {
  100. return ccp_aes_crypt(req, true);
  101. }
  102. static int ccp_aes_decrypt(struct ablkcipher_request *req)
  103. {
  104. return ccp_aes_crypt(req, false);
  105. }
  106. static int ccp_aes_cra_init(struct crypto_tfm *tfm)
  107. {
  108. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  109. ctx->complete = ccp_aes_complete;
  110. ctx->u.aes.key_len = 0;
  111. tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
  112. return 0;
  113. }
  114. static void ccp_aes_cra_exit(struct crypto_tfm *tfm)
  115. {
  116. }
  117. static int ccp_aes_rfc3686_complete(struct crypto_async_request *async_req,
  118. int ret)
  119. {
  120. struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
  121. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  122. /* Restore the original pointer */
  123. req->info = rctx->rfc3686_info;
  124. return ccp_aes_complete(async_req, ret);
  125. }
  126. static int ccp_aes_rfc3686_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  127. unsigned int key_len)
  128. {
  129. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
  130. if (key_len < CTR_RFC3686_NONCE_SIZE)
  131. return -EINVAL;
  132. key_len -= CTR_RFC3686_NONCE_SIZE;
  133. memcpy(ctx->u.aes.nonce, key + key_len, CTR_RFC3686_NONCE_SIZE);
  134. return ccp_aes_setkey(tfm, key, key_len);
  135. }
  136. static int ccp_aes_rfc3686_crypt(struct ablkcipher_request *req, bool encrypt)
  137. {
  138. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  139. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  140. u8 *iv;
  141. /* Initialize the CTR block */
  142. iv = rctx->rfc3686_iv;
  143. memcpy(iv, ctx->u.aes.nonce, CTR_RFC3686_NONCE_SIZE);
  144. iv += CTR_RFC3686_NONCE_SIZE;
  145. memcpy(iv, req->info, CTR_RFC3686_IV_SIZE);
  146. iv += CTR_RFC3686_IV_SIZE;
  147. *(__be32 *)iv = cpu_to_be32(1);
  148. /* Point to the new IV */
  149. rctx->rfc3686_info = req->info;
  150. req->info = rctx->rfc3686_iv;
  151. return ccp_aes_crypt(req, encrypt);
  152. }
  153. static int ccp_aes_rfc3686_encrypt(struct ablkcipher_request *req)
  154. {
  155. return ccp_aes_rfc3686_crypt(req, true);
  156. }
  157. static int ccp_aes_rfc3686_decrypt(struct ablkcipher_request *req)
  158. {
  159. return ccp_aes_rfc3686_crypt(req, false);
  160. }
  161. static int ccp_aes_rfc3686_cra_init(struct crypto_tfm *tfm)
  162. {
  163. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  164. ctx->complete = ccp_aes_rfc3686_complete;
  165. ctx->u.aes.key_len = 0;
  166. tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
  167. return 0;
  168. }
  169. static void ccp_aes_rfc3686_cra_exit(struct crypto_tfm *tfm)
  170. {
  171. }
  172. static struct crypto_alg ccp_aes_defaults = {
  173. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  174. CRYPTO_ALG_ASYNC |
  175. CRYPTO_ALG_KERN_DRIVER_ONLY |
  176. CRYPTO_ALG_NEED_FALLBACK,
  177. .cra_blocksize = AES_BLOCK_SIZE,
  178. .cra_ctxsize = sizeof(struct ccp_ctx),
  179. .cra_priority = CCP_CRA_PRIORITY,
  180. .cra_type = &crypto_ablkcipher_type,
  181. .cra_init = ccp_aes_cra_init,
  182. .cra_exit = ccp_aes_cra_exit,
  183. .cra_module = THIS_MODULE,
  184. .cra_ablkcipher = {
  185. .setkey = ccp_aes_setkey,
  186. .encrypt = ccp_aes_encrypt,
  187. .decrypt = ccp_aes_decrypt,
  188. .min_keysize = AES_MIN_KEY_SIZE,
  189. .max_keysize = AES_MAX_KEY_SIZE,
  190. },
  191. };
  192. static struct crypto_alg ccp_aes_rfc3686_defaults = {
  193. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  194. CRYPTO_ALG_ASYNC |
  195. CRYPTO_ALG_KERN_DRIVER_ONLY |
  196. CRYPTO_ALG_NEED_FALLBACK,
  197. .cra_blocksize = CTR_RFC3686_BLOCK_SIZE,
  198. .cra_ctxsize = sizeof(struct ccp_ctx),
  199. .cra_priority = CCP_CRA_PRIORITY,
  200. .cra_type = &crypto_ablkcipher_type,
  201. .cra_init = ccp_aes_rfc3686_cra_init,
  202. .cra_exit = ccp_aes_rfc3686_cra_exit,
  203. .cra_module = THIS_MODULE,
  204. .cra_ablkcipher = {
  205. .setkey = ccp_aes_rfc3686_setkey,
  206. .encrypt = ccp_aes_rfc3686_encrypt,
  207. .decrypt = ccp_aes_rfc3686_decrypt,
  208. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  209. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  210. },
  211. };
  212. struct ccp_aes_def {
  213. enum ccp_aes_mode mode;
  214. unsigned int version;
  215. const char *name;
  216. const char *driver_name;
  217. unsigned int blocksize;
  218. unsigned int ivsize;
  219. struct crypto_alg *alg_defaults;
  220. };
  221. static struct ccp_aes_def aes_algs[] = {
  222. {
  223. .mode = CCP_AES_MODE_ECB,
  224. .version = CCP_VERSION(3, 0),
  225. .name = "ecb(aes)",
  226. .driver_name = "ecb-aes-ccp",
  227. .blocksize = AES_BLOCK_SIZE,
  228. .ivsize = 0,
  229. .alg_defaults = &ccp_aes_defaults,
  230. },
  231. {
  232. .mode = CCP_AES_MODE_CBC,
  233. .version = CCP_VERSION(3, 0),
  234. .name = "cbc(aes)",
  235. .driver_name = "cbc-aes-ccp",
  236. .blocksize = AES_BLOCK_SIZE,
  237. .ivsize = AES_BLOCK_SIZE,
  238. .alg_defaults = &ccp_aes_defaults,
  239. },
  240. {
  241. .mode = CCP_AES_MODE_CFB,
  242. .version = CCP_VERSION(3, 0),
  243. .name = "cfb(aes)",
  244. .driver_name = "cfb-aes-ccp",
  245. .blocksize = 1,
  246. .ivsize = AES_BLOCK_SIZE,
  247. .alg_defaults = &ccp_aes_defaults,
  248. },
  249. {
  250. .mode = CCP_AES_MODE_OFB,
  251. .version = CCP_VERSION(3, 0),
  252. .name = "ofb(aes)",
  253. .driver_name = "ofb-aes-ccp",
  254. .blocksize = 1,
  255. .ivsize = AES_BLOCK_SIZE,
  256. .alg_defaults = &ccp_aes_defaults,
  257. },
  258. {
  259. .mode = CCP_AES_MODE_CTR,
  260. .version = CCP_VERSION(3, 0),
  261. .name = "ctr(aes)",
  262. .driver_name = "ctr-aes-ccp",
  263. .blocksize = 1,
  264. .ivsize = AES_BLOCK_SIZE,
  265. .alg_defaults = &ccp_aes_defaults,
  266. },
  267. {
  268. .mode = CCP_AES_MODE_CTR,
  269. .version = CCP_VERSION(3, 0),
  270. .name = "rfc3686(ctr(aes))",
  271. .driver_name = "rfc3686-ctr-aes-ccp",
  272. .blocksize = 1,
  273. .ivsize = CTR_RFC3686_IV_SIZE,
  274. .alg_defaults = &ccp_aes_rfc3686_defaults,
  275. },
  276. };
  277. static int ccp_register_aes_alg(struct list_head *head,
  278. const struct ccp_aes_def *def)
  279. {
  280. struct ccp_crypto_ablkcipher_alg *ccp_alg;
  281. struct crypto_alg *alg;
  282. int ret;
  283. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  284. if (!ccp_alg)
  285. return -ENOMEM;
  286. INIT_LIST_HEAD(&ccp_alg->entry);
  287. ccp_alg->mode = def->mode;
  288. /* Copy the defaults and override as necessary */
  289. alg = &ccp_alg->alg;
  290. *alg = *def->alg_defaults;
  291. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  292. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  293. def->driver_name);
  294. alg->cra_blocksize = def->blocksize;
  295. alg->cra_ablkcipher.ivsize = def->ivsize;
  296. ret = crypto_register_alg(alg);
  297. if (ret) {
  298. pr_err("%s ablkcipher algorithm registration error (%d)\n",
  299. alg->cra_name, ret);
  300. kfree(ccp_alg);
  301. return ret;
  302. }
  303. list_add(&ccp_alg->entry, head);
  304. return 0;
  305. }
  306. int ccp_register_aes_algs(struct list_head *head)
  307. {
  308. int i, ret;
  309. unsigned int ccpversion = ccp_version();
  310. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  311. if (aes_algs[i].version > ccpversion)
  312. continue;
  313. ret = ccp_register_aes_alg(head, &aes_algs[i]);
  314. if (ret)
  315. return ret;
  316. }
  317. return 0;
  318. }