ccp-crypto-aes-cmac.c 9.2 KB

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