ccp-crypto-aes.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) AES crypto API support
  3. *
  4. * Copyright (C) 2013 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/delay.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/crypto.h>
  17. #include <crypto/algapi.h>
  18. #include <crypto/aes.h>
  19. #include <crypto/ctr.h>
  20. #include <crypto/scatterwalk.h>
  21. #include "ccp-crypto.h"
  22. static int ccp_aes_complete(struct crypto_async_request *async_req, int ret)
  23. {
  24. struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
  25. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  26. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  27. if (ret)
  28. return ret;
  29. if (ctx->u.aes.mode != CCP_AES_MODE_ECB)
  30. memcpy(req->info, rctx->iv, AES_BLOCK_SIZE);
  31. return 0;
  32. }
  33. static int ccp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  34. unsigned int key_len)
  35. {
  36. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ablkcipher_tfm(tfm));
  37. struct ccp_crypto_ablkcipher_alg *alg =
  38. ccp_crypto_ablkcipher_alg(crypto_ablkcipher_tfm(tfm));
  39. switch (key_len) {
  40. case AES_KEYSIZE_128:
  41. ctx->u.aes.type = CCP_AES_TYPE_128;
  42. break;
  43. case AES_KEYSIZE_192:
  44. ctx->u.aes.type = CCP_AES_TYPE_192;
  45. break;
  46. case AES_KEYSIZE_256:
  47. ctx->u.aes.type = CCP_AES_TYPE_256;
  48. break;
  49. default:
  50. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  51. return -EINVAL;
  52. }
  53. ctx->u.aes.mode = alg->mode;
  54. ctx->u.aes.key_len = key_len;
  55. memcpy(ctx->u.aes.key, key, key_len);
  56. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  57. return 0;
  58. }
  59. static int ccp_aes_crypt(struct ablkcipher_request *req, bool encrypt)
  60. {
  61. struct ccp_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
  62. struct ccp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
  63. struct scatterlist *iv_sg = NULL;
  64. unsigned int iv_len = 0;
  65. int ret;
  66. if (!ctx->u.aes.key_len)
  67. return -EINVAL;
  68. if (((ctx->u.aes.mode == CCP_AES_MODE_ECB) ||
  69. (ctx->u.aes.mode == CCP_AES_MODE_CBC) ||
  70. (ctx->u.aes.mode == CCP_AES_MODE_CFB)) &&
  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. const char *name;
  215. const char *driver_name;
  216. unsigned int blocksize;
  217. unsigned int ivsize;
  218. struct crypto_alg *alg_defaults;
  219. };
  220. static struct ccp_aes_def aes_algs[] = {
  221. {
  222. .mode = CCP_AES_MODE_ECB,
  223. .name = "ecb(aes)",
  224. .driver_name = "ecb-aes-ccp",
  225. .blocksize = AES_BLOCK_SIZE,
  226. .ivsize = 0,
  227. .alg_defaults = &ccp_aes_defaults,
  228. },
  229. {
  230. .mode = CCP_AES_MODE_CBC,
  231. .name = "cbc(aes)",
  232. .driver_name = "cbc-aes-ccp",
  233. .blocksize = AES_BLOCK_SIZE,
  234. .ivsize = AES_BLOCK_SIZE,
  235. .alg_defaults = &ccp_aes_defaults,
  236. },
  237. {
  238. .mode = CCP_AES_MODE_CFB,
  239. .name = "cfb(aes)",
  240. .driver_name = "cfb-aes-ccp",
  241. .blocksize = AES_BLOCK_SIZE,
  242. .ivsize = AES_BLOCK_SIZE,
  243. .alg_defaults = &ccp_aes_defaults,
  244. },
  245. {
  246. .mode = CCP_AES_MODE_OFB,
  247. .name = "ofb(aes)",
  248. .driver_name = "ofb-aes-ccp",
  249. .blocksize = 1,
  250. .ivsize = AES_BLOCK_SIZE,
  251. .alg_defaults = &ccp_aes_defaults,
  252. },
  253. {
  254. .mode = CCP_AES_MODE_CTR,
  255. .name = "ctr(aes)",
  256. .driver_name = "ctr-aes-ccp",
  257. .blocksize = 1,
  258. .ivsize = AES_BLOCK_SIZE,
  259. .alg_defaults = &ccp_aes_defaults,
  260. },
  261. {
  262. .mode = CCP_AES_MODE_CTR,
  263. .name = "rfc3686(ctr(aes))",
  264. .driver_name = "rfc3686-ctr-aes-ccp",
  265. .blocksize = 1,
  266. .ivsize = CTR_RFC3686_IV_SIZE,
  267. .alg_defaults = &ccp_aes_rfc3686_defaults,
  268. },
  269. };
  270. static int ccp_register_aes_alg(struct list_head *head,
  271. const struct ccp_aes_def *def)
  272. {
  273. struct ccp_crypto_ablkcipher_alg *ccp_alg;
  274. struct crypto_alg *alg;
  275. int ret;
  276. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  277. if (!ccp_alg)
  278. return -ENOMEM;
  279. INIT_LIST_HEAD(&ccp_alg->entry);
  280. ccp_alg->mode = def->mode;
  281. /* Copy the defaults and override as necessary */
  282. alg = &ccp_alg->alg;
  283. *alg = *def->alg_defaults;
  284. snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  285. snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  286. def->driver_name);
  287. alg->cra_blocksize = def->blocksize;
  288. alg->cra_ablkcipher.ivsize = def->ivsize;
  289. ret = crypto_register_alg(alg);
  290. if (ret) {
  291. pr_err("%s ablkcipher algorithm registration error (%d)\n",
  292. alg->cra_name, ret);
  293. kfree(ccp_alg);
  294. return ret;
  295. }
  296. list_add(&ccp_alg->entry, head);
  297. return 0;
  298. }
  299. int ccp_register_aes_algs(struct list_head *head)
  300. {
  301. int i, ret;
  302. for (i = 0; i < ARRAY_SIZE(aes_algs); i++) {
  303. ret = ccp_register_aes_alg(head, &aes_algs[i]);
  304. if (ret)
  305. return ret;
  306. }
  307. return 0;
  308. }