ccp-crypto-aes-cmac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) AES CMAC 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/hash.h>
  20. #include <crypto/internal/hash.h>
  21. #include <crypto/scatterwalk.h>
  22. #include "ccp-crypto.h"
  23. static int ccp_aes_cmac_complete(struct crypto_async_request *async_req,
  24. int ret)
  25. {
  26. struct ahash_request *req = ahash_request_cast(async_req);
  27. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  28. struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
  29. unsigned int digest_size = crypto_ahash_digestsize(tfm);
  30. if (ret)
  31. goto e_free;
  32. if (rctx->hash_rem) {
  33. /* Save remaining data to buffer */
  34. unsigned int offset = rctx->nbytes - rctx->hash_rem;
  35. scatterwalk_map_and_copy(rctx->buf, rctx->src,
  36. offset, rctx->hash_rem, 0);
  37. rctx->buf_count = rctx->hash_rem;
  38. } else {
  39. rctx->buf_count = 0;
  40. }
  41. /* Update result area if supplied */
  42. if (req->result)
  43. memcpy(req->result, rctx->iv, digest_size);
  44. e_free:
  45. sg_free_table(&rctx->data_sg);
  46. return ret;
  47. }
  48. static int ccp_do_cmac_update(struct ahash_request *req, unsigned int nbytes,
  49. unsigned int final)
  50. {
  51. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  52. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  53. struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
  54. struct scatterlist *sg, *cmac_key_sg = NULL;
  55. unsigned int block_size =
  56. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  57. unsigned int need_pad, sg_count;
  58. gfp_t gfp;
  59. u64 len;
  60. int ret;
  61. if (!ctx->u.aes.key_len)
  62. return -EINVAL;
  63. if (nbytes)
  64. rctx->null_msg = 0;
  65. len = (u64)rctx->buf_count + (u64)nbytes;
  66. if (!final && (len <= block_size)) {
  67. scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
  68. 0, nbytes, 0);
  69. rctx->buf_count += nbytes;
  70. return 0;
  71. }
  72. rctx->src = req->src;
  73. rctx->nbytes = nbytes;
  74. rctx->final = final;
  75. rctx->hash_rem = final ? 0 : len & (block_size - 1);
  76. rctx->hash_cnt = len - rctx->hash_rem;
  77. if (!final && !rctx->hash_rem) {
  78. /* CCP can't do zero length final, so keep some data around */
  79. rctx->hash_cnt -= block_size;
  80. rctx->hash_rem = block_size;
  81. }
  82. if (final && (rctx->null_msg || (len & (block_size - 1))))
  83. need_pad = 1;
  84. else
  85. need_pad = 0;
  86. sg_init_one(&rctx->iv_sg, rctx->iv, sizeof(rctx->iv));
  87. /* Build the data scatterlist table - allocate enough entries for all
  88. * possible data pieces (buffer, input data, padding)
  89. */
  90. sg_count = (nbytes) ? sg_nents(req->src) + 2 : 2;
  91. gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  92. GFP_KERNEL : GFP_ATOMIC;
  93. ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
  94. if (ret)
  95. return ret;
  96. sg = NULL;
  97. if (rctx->buf_count) {
  98. sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
  99. sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
  100. if (!sg) {
  101. ret = -EINVAL;
  102. goto e_free;
  103. }
  104. }
  105. if (nbytes) {
  106. sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
  107. if (!sg) {
  108. ret = -EINVAL;
  109. goto e_free;
  110. }
  111. }
  112. if (need_pad) {
  113. int pad_length = block_size - (len & (block_size - 1));
  114. rctx->hash_cnt += pad_length;
  115. memset(rctx->pad, 0, sizeof(rctx->pad));
  116. rctx->pad[0] = 0x80;
  117. sg_init_one(&rctx->pad_sg, rctx->pad, pad_length);
  118. sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->pad_sg);
  119. if (!sg) {
  120. ret = -EINVAL;
  121. goto e_free;
  122. }
  123. }
  124. if (sg) {
  125. sg_mark_end(sg);
  126. sg = rctx->data_sg.sgl;
  127. }
  128. /* Initialize the K1/K2 scatterlist */
  129. if (final)
  130. cmac_key_sg = (need_pad) ? &ctx->u.aes.k2_sg
  131. : &ctx->u.aes.k1_sg;
  132. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  133. INIT_LIST_HEAD(&rctx->cmd.entry);
  134. rctx->cmd.engine = CCP_ENGINE_AES;
  135. rctx->cmd.u.aes.type = ctx->u.aes.type;
  136. rctx->cmd.u.aes.mode = ctx->u.aes.mode;
  137. rctx->cmd.u.aes.action = CCP_AES_ACTION_ENCRYPT;
  138. rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
  139. rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
  140. rctx->cmd.u.aes.iv = &rctx->iv_sg;
  141. rctx->cmd.u.aes.iv_len = AES_BLOCK_SIZE;
  142. rctx->cmd.u.aes.src = sg;
  143. rctx->cmd.u.aes.src_len = rctx->hash_cnt;
  144. rctx->cmd.u.aes.dst = NULL;
  145. rctx->cmd.u.aes.cmac_key = cmac_key_sg;
  146. rctx->cmd.u.aes.cmac_key_len = ctx->u.aes.kn_len;
  147. rctx->cmd.u.aes.cmac_final = final;
  148. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  149. return ret;
  150. e_free:
  151. sg_free_table(&rctx->data_sg);
  152. return ret;
  153. }
  154. static int ccp_aes_cmac_init(struct ahash_request *req)
  155. {
  156. struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
  157. memset(rctx, 0, sizeof(*rctx));
  158. rctx->null_msg = 1;
  159. return 0;
  160. }
  161. static int ccp_aes_cmac_update(struct ahash_request *req)
  162. {
  163. return ccp_do_cmac_update(req, req->nbytes, 0);
  164. }
  165. static int ccp_aes_cmac_final(struct ahash_request *req)
  166. {
  167. return ccp_do_cmac_update(req, 0, 1);
  168. }
  169. static int ccp_aes_cmac_finup(struct ahash_request *req)
  170. {
  171. return ccp_do_cmac_update(req, req->nbytes, 1);
  172. }
  173. static int ccp_aes_cmac_digest(struct ahash_request *req)
  174. {
  175. int ret;
  176. ret = ccp_aes_cmac_init(req);
  177. if (ret)
  178. return ret;
  179. return ccp_aes_cmac_finup(req);
  180. }
  181. static int ccp_aes_cmac_export(struct ahash_request *req, void *out)
  182. {
  183. struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
  184. struct ccp_aes_cmac_exp_ctx state;
  185. /* Don't let anything leak to 'out' */
  186. memset(&state, 0, sizeof(state));
  187. state.null_msg = rctx->null_msg;
  188. memcpy(state.iv, rctx->iv, sizeof(state.iv));
  189. state.buf_count = rctx->buf_count;
  190. memcpy(state.buf, rctx->buf, sizeof(state.buf));
  191. /* 'out' may not be aligned so memcpy from local variable */
  192. memcpy(out, &state, sizeof(state));
  193. return 0;
  194. }
  195. static int ccp_aes_cmac_import(struct ahash_request *req, const void *in)
  196. {
  197. struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
  198. struct ccp_aes_cmac_exp_ctx state;
  199. /* 'in' may not be aligned so memcpy to local variable */
  200. memcpy(&state, in, sizeof(state));
  201. memset(rctx, 0, sizeof(*rctx));
  202. rctx->null_msg = state.null_msg;
  203. memcpy(rctx->iv, state.iv, sizeof(rctx->iv));
  204. rctx->buf_count = state.buf_count;
  205. memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
  206. return 0;
  207. }
  208. static int ccp_aes_cmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  209. unsigned int key_len)
  210. {
  211. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  212. struct ccp_crypto_ahash_alg *alg =
  213. ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
  214. u64 k0_hi, k0_lo, k1_hi, k1_lo, k2_hi, k2_lo;
  215. u64 rb_hi = 0x00, rb_lo = 0x87;
  216. __be64 *gk;
  217. int ret;
  218. switch (key_len) {
  219. case AES_KEYSIZE_128:
  220. ctx->u.aes.type = CCP_AES_TYPE_128;
  221. break;
  222. case AES_KEYSIZE_192:
  223. ctx->u.aes.type = CCP_AES_TYPE_192;
  224. break;
  225. case AES_KEYSIZE_256:
  226. ctx->u.aes.type = CCP_AES_TYPE_256;
  227. break;
  228. default:
  229. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  230. return -EINVAL;
  231. }
  232. ctx->u.aes.mode = alg->mode;
  233. /* Set to zero until complete */
  234. ctx->u.aes.key_len = 0;
  235. /* Set the key for the AES cipher used to generate the keys */
  236. ret = crypto_cipher_setkey(ctx->u.aes.tfm_cipher, key, key_len);
  237. if (ret)
  238. return ret;
  239. /* Encrypt a block of zeroes - use key area in context */
  240. memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key));
  241. crypto_cipher_encrypt_one(ctx->u.aes.tfm_cipher, ctx->u.aes.key,
  242. ctx->u.aes.key);
  243. /* Generate K1 and K2 */
  244. k0_hi = be64_to_cpu(*((__be64 *)ctx->u.aes.key));
  245. k0_lo = be64_to_cpu(*((__be64 *)ctx->u.aes.key + 1));
  246. k1_hi = (k0_hi << 1) | (k0_lo >> 63);
  247. k1_lo = k0_lo << 1;
  248. if (ctx->u.aes.key[0] & 0x80) {
  249. k1_hi ^= rb_hi;
  250. k1_lo ^= rb_lo;
  251. }
  252. gk = (__be64 *)ctx->u.aes.k1;
  253. *gk = cpu_to_be64(k1_hi);
  254. gk++;
  255. *gk = cpu_to_be64(k1_lo);
  256. k2_hi = (k1_hi << 1) | (k1_lo >> 63);
  257. k2_lo = k1_lo << 1;
  258. if (ctx->u.aes.k1[0] & 0x80) {
  259. k2_hi ^= rb_hi;
  260. k2_lo ^= rb_lo;
  261. }
  262. gk = (__be64 *)ctx->u.aes.k2;
  263. *gk = cpu_to_be64(k2_hi);
  264. gk++;
  265. *gk = cpu_to_be64(k2_lo);
  266. ctx->u.aes.kn_len = sizeof(ctx->u.aes.k1);
  267. sg_init_one(&ctx->u.aes.k1_sg, ctx->u.aes.k1, sizeof(ctx->u.aes.k1));
  268. sg_init_one(&ctx->u.aes.k2_sg, ctx->u.aes.k2, sizeof(ctx->u.aes.k2));
  269. /* Save the supplied key */
  270. memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key));
  271. memcpy(ctx->u.aes.key, key, key_len);
  272. ctx->u.aes.key_len = key_len;
  273. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  274. return ret;
  275. }
  276. static int ccp_aes_cmac_cra_init(struct crypto_tfm *tfm)
  277. {
  278. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  279. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  280. struct crypto_cipher *cipher_tfm;
  281. ctx->complete = ccp_aes_cmac_complete;
  282. ctx->u.aes.key_len = 0;
  283. crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_aes_cmac_req_ctx));
  284. cipher_tfm = crypto_alloc_cipher("aes", 0,
  285. CRYPTO_ALG_ASYNC |
  286. CRYPTO_ALG_NEED_FALLBACK);
  287. if (IS_ERR(cipher_tfm)) {
  288. pr_warn("could not load aes cipher driver\n");
  289. return PTR_ERR(cipher_tfm);
  290. }
  291. ctx->u.aes.tfm_cipher = cipher_tfm;
  292. return 0;
  293. }
  294. static void ccp_aes_cmac_cra_exit(struct crypto_tfm *tfm)
  295. {
  296. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  297. if (ctx->u.aes.tfm_cipher)
  298. crypto_free_cipher(ctx->u.aes.tfm_cipher);
  299. ctx->u.aes.tfm_cipher = NULL;
  300. }
  301. int ccp_register_aes_cmac_algs(struct list_head *head)
  302. {
  303. struct ccp_crypto_ahash_alg *ccp_alg;
  304. struct ahash_alg *alg;
  305. struct hash_alg_common *halg;
  306. struct crypto_alg *base;
  307. int ret;
  308. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  309. if (!ccp_alg)
  310. return -ENOMEM;
  311. INIT_LIST_HEAD(&ccp_alg->entry);
  312. ccp_alg->mode = CCP_AES_MODE_CMAC;
  313. alg = &ccp_alg->alg;
  314. alg->init = ccp_aes_cmac_init;
  315. alg->update = ccp_aes_cmac_update;
  316. alg->final = ccp_aes_cmac_final;
  317. alg->finup = ccp_aes_cmac_finup;
  318. alg->digest = ccp_aes_cmac_digest;
  319. alg->export = ccp_aes_cmac_export;
  320. alg->import = ccp_aes_cmac_import;
  321. alg->setkey = ccp_aes_cmac_setkey;
  322. halg = &alg->halg;
  323. halg->digestsize = AES_BLOCK_SIZE;
  324. halg->statesize = sizeof(struct ccp_aes_cmac_exp_ctx);
  325. base = &halg->base;
  326. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "cmac(aes)");
  327. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "cmac-aes-ccp");
  328. base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
  329. CRYPTO_ALG_KERN_DRIVER_ONLY |
  330. CRYPTO_ALG_NEED_FALLBACK;
  331. base->cra_blocksize = AES_BLOCK_SIZE;
  332. base->cra_ctxsize = sizeof(struct ccp_ctx);
  333. base->cra_priority = CCP_CRA_PRIORITY;
  334. base->cra_type = &crypto_ahash_type;
  335. base->cra_init = ccp_aes_cmac_cra_init;
  336. base->cra_exit = ccp_aes_cmac_cra_exit;
  337. base->cra_module = THIS_MODULE;
  338. ret = crypto_register_ahash(alg);
  339. if (ret) {
  340. pr_err("%s ahash algorithm registration error (%d)\n",
  341. base->cra_name, ret);
  342. kfree(ccp_alg);
  343. return ret;
  344. }
  345. list_add(&ccp_alg->entry, head);
  346. return 0;
  347. }