ccp-crypto-aes.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 ablkcipher_request *req = ablkcipher_request_cast(async_req);
  22. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  23. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  24. if (ret)
  25. return ret;
  26. if (ctx->u.aes.mode != CCP_AES_MODE_ECB)
  27. memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
  28. return 0;
  29. }
  30. static int ccp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  31. unsigned int key_len)
  32. {
  33. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
  34. struct ccp_crypto_ablkcipher_alg *alg =
  35. ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(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. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  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 ablkcipher_request *req, bool encrypt)
  57. {
  58. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  59. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  60. struct scatterlist *iv_sg = NULL;
  61. unsigned int iv_len = 0;
  62. int ret;
  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->nbytes & (AES_BLOCK_SIZE - 1)))
  68. return -EINVAL;
  69. if (ctx->u.aes.mode != CCP_AES_MODE_ECB) {
  70. if (!req->info)
  71. return -EINVAL;
  72. memcpy(rctx->iv, req->info, 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->nbytes;
  90. rctx->cmd.u.aes.dst = req->dst;
  91. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  92. return ret;
  93. }
  94. static int ccp_aes_encrypt(struct ablkcipher_request *req)
  95. {
  96. return ccp_aes_crypt(req, true);
  97. }
  98. static int ccp_aes_decrypt(struct ablkcipher_request *req)
  99. {
  100. return ccp_aes_crypt(req, false);
  101. }
  102. static int ccp_aes_cra_init(struct crypto_tfm *tfm)
  103. {
  104. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  105. ctx->complete = ccp_aes_complete;
  106. ctx->u.aes.key_len = 0;
  107. tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
  108. return 0;
  109. }
  110. static void ccp_aes_cra_exit(struct crypto_tfm *tfm)
  111. {
  112. }
  113. static int ccp_aes_rfc3686_complete(struct crypto_async_request *async_req,
  114. int ret)
  115. {
  116. struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
  117. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  118. /* Restore the original pointer */
  119. req->info = rctx->rfc3686_info;
  120. return ccp_aes_complete(async_req, ret);
  121. }
  122. static int ccp_aes_rfc3686_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  123. unsigned int key_len)
  124. {
  125. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
  126. if (key_len < CTR_RFC3686_NONCE_SIZE)
  127. return -EINVAL;
  128. key_len -= CTR_RFC3686_NONCE_SIZE;
  129. memcpy(ctx->u.aes.nonce, key + key_len, CTR_RFC3686_NONCE_SIZE);
  130. return ccp_aes_setkey(tfm, key, key_len);
  131. }
  132. static int ccp_aes_rfc3686_crypt(struct ablkcipher_request *req, bool encrypt)
  133. {
  134. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  135. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  136. u8 *iv;
  137. /* Initialize the CTR block */
  138. iv = rctx->rfc3686_iv;
  139. memcpy(iv, ctx->u.aes.nonce, CTR_RFC3686_NONCE_SIZE);
  140. iv += CTR_RFC3686_NONCE_SIZE;
  141. memcpy(iv, req->info, CTR_RFC3686_IV_SIZE);
  142. iv += CTR_RFC3686_IV_SIZE;
  143. *(__be32 *)iv = cpu_to_be32(1);
  144. /* Point to the new IV */
  145. rctx->rfc3686_info = req->info;
  146. req->info = rctx->rfc3686_iv;
  147. return ccp_aes_crypt(req, encrypt);
  148. }
  149. static int ccp_aes_rfc3686_encrypt(struct ablkcipher_request *req)
  150. {
  151. return ccp_aes_rfc3686_crypt(req, true);
  152. }
  153. static int ccp_aes_rfc3686_decrypt(struct ablkcipher_request *req)
  154. {
  155. return ccp_aes_rfc3686_crypt(req, false);
  156. }
  157. static int ccp_aes_rfc3686_cra_init(struct crypto_tfm *tfm)
  158. {
  159. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  160. ctx->complete = ccp_aes_rfc3686_complete;
  161. ctx->u.aes.key_len = 0;
  162. tfm->crt_ablkcipher.reqsize = sizeof(struct ccp_aes_req_ctx);
  163. return 0;
  164. }
  165. static void ccp_aes_rfc3686_cra_exit(struct crypto_tfm *tfm)
  166. {
  167. }
  168. static struct crypto_alg ccp_aes_defaults = {
  169. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  170. CRYPTO_ALG_ASYNC |
  171. CRYPTO_ALG_KERN_DRIVER_ONLY |
  172. CRYPTO_ALG_NEED_FALLBACK,
  173. .cra_blocksize = AES_BLOCK_SIZE,
  174. .cra_ctxsize = sizeof(struct ccp_ctx),
  175. .cra_priority = CCP_CRA_PRIORITY,
  176. .cra_type = &crypto_ablkcipher_type,
  177. .cra_init = ccp_aes_cra_init,
  178. .cra_exit = ccp_aes_cra_exit,
  179. .cra_module = THIS_MODULE,
  180. .cra_ablkcipher = {
  181. .setkey = ccp_aes_setkey,
  182. .encrypt = ccp_aes_encrypt,
  183. .decrypt = ccp_aes_decrypt,
  184. .min_keysize = AES_MIN_KEY_SIZE,
  185. .max_keysize = AES_MAX_KEY_SIZE,
  186. },
  187. };
  188. static struct crypto_alg ccp_aes_rfc3686_defaults = {
  189. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
  190. CRYPTO_ALG_ASYNC |
  191. CRYPTO_ALG_KERN_DRIVER_ONLY |
  192. CRYPTO_ALG_NEED_FALLBACK,
  193. .cra_blocksize = CTR_RFC3686_BLOCK_SIZE,
  194. .cra_ctxsize = sizeof(struct ccp_ctx),
  195. .cra_priority = CCP_CRA_PRIORITY,
  196. .cra_type = &crypto_ablkcipher_type,
  197. .cra_init = ccp_aes_rfc3686_cra_init,
  198. .cra_exit = ccp_aes_rfc3686_cra_exit,
  199. .cra_module = THIS_MODULE,
  200. .cra_ablkcipher = {
  201. .setkey = ccp_aes_rfc3686_setkey,
  202. .encrypt = ccp_aes_rfc3686_encrypt,
  203. .decrypt = ccp_aes_rfc3686_decrypt,
  204. .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  205. .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
  206. },
  207. };
  208. struct ccp_aes_def {
  209. enum ccp_aes_mode mode;
  210. unsigned int version;
  211. const char *name;
  212. const char *driver_name;
  213. unsigned int blocksize;
  214. unsigned int ivsize;
  215. struct crypto_alg *alg_defaults;
  216. };
  217. static struct ccp_aes_def aes_algs[] = {
  218. {
  219. .mode = CCP_AES_MODE_ECB,
  220. .version = CCP_VERSION(3, 0),
  221. .name = "ecb(aes)",
  222. .driver_name = "ecb-aes-ccp",
  223. .blocksize = AES_BLOCK_SIZE,
  224. .ivsize = 0,
  225. .alg_defaults = &ccp_aes_defaults,
  226. },
  227. {
  228. .mode = CCP_AES_MODE_CBC,
  229. .version = CCP_VERSION(3, 0),
  230. .name = "cbc(aes)",
  231. .driver_name = "cbc-aes-ccp",
  232. .blocksize = AES_BLOCK_SIZE,
  233. .ivsize = AES_BLOCK_SIZE,
  234. .alg_defaults = &ccp_aes_defaults,
  235. },
  236. {
  237. .mode = CCP_AES_MODE_CFB,
  238. .version = CCP_VERSION(3, 0),
  239. .name = "cfb(aes)",
  240. .driver_name = "cfb-aes-ccp",
  241. .blocksize = 1,
  242. .ivsize = AES_BLOCK_SIZE,
  243. .alg_defaults = &ccp_aes_defaults,
  244. },
  245. {
  246. .mode = CCP_AES_MODE_OFB,
  247. .version = CCP_VERSION(3, 0),
  248. .name = "ofb(aes)",
  249. .driver_name = "ofb-aes-ccp",
  250. .blocksize = 1,
  251. .ivsize = AES_BLOCK_SIZE,
  252. .alg_defaults = &ccp_aes_defaults,
  253. },
  254. {
  255. .mode = CCP_AES_MODE_CTR,
  256. .version = CCP_VERSION(3, 0),
  257. .name = "ctr(aes)",
  258. .driver_name = "ctr-aes-ccp",
  259. .blocksize = 1,
  260. .ivsize = AES_BLOCK_SIZE,
  261. .alg_defaults = &ccp_aes_defaults,
  262. },
  263. {
  264. .mode = CCP_AES_MODE_CTR,
  265. .version = CCP_VERSION(3, 0),
  266. .name = "rfc3686(ctr(aes))",
  267. .driver_name = "rfc3686-ctr-aes-ccp",
  268. .blocksize = 1,
  269. .ivsize = CTR_RFC3686_IV_SIZE,
  270. .alg_defaults = &ccp_aes_rfc3686_defaults,
  271. },
  272. };
  273. static int ccp_register_aes_alg(struct list_head *head,
  274. const struct ccp_aes_def *def)
  275. {
  276. struct ccp_crypto_ablkcipher_alg *ccp_alg;
  277. struct crypto_alg *alg;
  278. int ret;
  279. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  280. if (!ccp_alg)
  281. return -ENOMEM;
  282. INIT_LIST_HEAD(&ccp_alg->entry);
  283. ccp_alg->mode = def->mode;
  284. /* Copy the defaults and override as necessary */
  285. alg = &ccp_alg->alg;
  286. *alg = *def->alg_defaults;
  287. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  288. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  289. def->driver_name);
  290. alg->cra_blocksize = def->blocksize;
  291. alg->cra_ablkcipher.ivsize = def->ivsize;
  292. ret = crypto_register_alg(alg);
  293. if (ret) {
  294. pr_err("%s ablkcipher algorithm registration error (%d)\n",
  295. alg->cra_name, ret);
  296. kfree(ccp_alg);
  297. return ret;
  298. }
  299. list_add(&ccp_alg->entry, head);
  300. return 0;
  301. }
  302. int ccp_register_aes_algs(struct list_head *head)
  303. {
  304. int i, ret;
  305. unsigned int ccpversion = ccp_version();
  306. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  307. if (aes_algs[i].version > ccpversion)
  308. continue;
  309. ret = ccp_register_aes_alg(head, &aes_algs[i]);
  310. if (ret)
  311. return ret;
  312. }
  313. return 0;
  314. }