ccp-crypto-aes-cmac.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. }
  101. if (nbytes)
  102. sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
  103. if (need_pad) {
  104. int pad_length = block_size - (len & (block_size - 1));
  105. rctx->hash_cnt += pad_length;
  106. memset(rctx->pad, 0, sizeof(rctx->pad));
  107. rctx->pad[0] = 0x80;
  108. sg_init_one(&rctx->pad_sg, rctx->pad, pad_length);
  109. sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->pad_sg);
  110. }
  111. if (sg) {
  112. sg_mark_end(sg);
  113. sg = rctx->data_sg.sgl;
  114. }
  115. /* Initialize the K1/K2 scatterlist */
  116. if (final)
  117. cmac_key_sg = (need_pad) ? &ctx->u.aes.k2_sg
  118. : &ctx->u.aes.k1_sg;
  119. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  120. INIT_LIST_HEAD(&rctx->cmd.entry);
  121. rctx->cmd.engine = CCP_ENGINE_AES;
  122. rctx->cmd.u.aes.type = ctx->u.aes.type;
  123. rctx->cmd.u.aes.mode = ctx->u.aes.mode;
  124. rctx->cmd.u.aes.action = CCP_AES_ACTION_ENCRYPT;
  125. rctx->cmd.u.aes.key = &ctx->u.aes.key_sg;
  126. rctx->cmd.u.aes.key_len = ctx->u.aes.key_len;
  127. rctx->cmd.u.aes.iv = &rctx->iv_sg;
  128. rctx->cmd.u.aes.iv_len = AES_BLOCK_SIZE;
  129. rctx->cmd.u.aes.src = sg;
  130. rctx->cmd.u.aes.src_len = rctx->hash_cnt;
  131. rctx->cmd.u.aes.dst = NULL;
  132. rctx->cmd.u.aes.cmac_key = cmac_key_sg;
  133. rctx->cmd.u.aes.cmac_key_len = ctx->u.aes.kn_len;
  134. rctx->cmd.u.aes.cmac_final = final;
  135. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  136. return ret;
  137. }
  138. static int ccp_aes_cmac_init(struct ahash_request *req)
  139. {
  140. struct ccp_aes_cmac_req_ctx *rctx = ahash_request_ctx(req);
  141. memset(rctx, 0, sizeof(*rctx));
  142. rctx->null_msg = 1;
  143. return 0;
  144. }
  145. static int ccp_aes_cmac_update(struct ahash_request *req)
  146. {
  147. return ccp_do_cmac_update(req, req->nbytes, 0);
  148. }
  149. static int ccp_aes_cmac_final(struct ahash_request *req)
  150. {
  151. return ccp_do_cmac_update(req, 0, 1);
  152. }
  153. static int ccp_aes_cmac_finup(struct ahash_request *req)
  154. {
  155. return ccp_do_cmac_update(req, req->nbytes, 1);
  156. }
  157. static int ccp_aes_cmac_digest(struct ahash_request *req)
  158. {
  159. int ret;
  160. ret = ccp_aes_cmac_init(req);
  161. if (ret)
  162. return ret;
  163. return ccp_aes_cmac_finup(req);
  164. }
  165. static int ccp_aes_cmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  166. unsigned int key_len)
  167. {
  168. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  169. struct ccp_crypto_ahash_alg *alg =
  170. ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
  171. u64 k0_hi, k0_lo, k1_hi, k1_lo, k2_hi, k2_lo;
  172. u64 rb_hi = 0x00, rb_lo = 0x87;
  173. __be64 *gk;
  174. int ret;
  175. switch (key_len) {
  176. case AES_KEYSIZE_128:
  177. ctx->u.aes.type = CCP_AES_TYPE_128;
  178. break;
  179. case AES_KEYSIZE_192:
  180. ctx->u.aes.type = CCP_AES_TYPE_192;
  181. break;
  182. case AES_KEYSIZE_256:
  183. ctx->u.aes.type = CCP_AES_TYPE_256;
  184. break;
  185. default:
  186. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  187. return -EINVAL;
  188. }
  189. ctx->u.aes.mode = alg->mode;
  190. /* Set to zero until complete */
  191. ctx->u.aes.key_len = 0;
  192. /* Set the key for the AES cipher used to generate the keys */
  193. ret = crypto_cipher_setkey(ctx->u.aes.tfm_cipher, key, key_len);
  194. if (ret)
  195. return ret;
  196. /* Encrypt a block of zeroes - use key area in context */
  197. memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key));
  198. crypto_cipher_encrypt_one(ctx->u.aes.tfm_cipher, ctx->u.aes.key,
  199. ctx->u.aes.key);
  200. /* Generate K1 and K2 */
  201. k0_hi = be64_to_cpu(*((__be64 *)ctx->u.aes.key));
  202. k0_lo = be64_to_cpu(*((__be64 *)ctx->u.aes.key + 1));
  203. k1_hi = (k0_hi << 1) | (k0_lo >> 63);
  204. k1_lo = k0_lo << 1;
  205. if (ctx->u.aes.key[0] & 0x80) {
  206. k1_hi ^= rb_hi;
  207. k1_lo ^= rb_lo;
  208. }
  209. gk = (__be64 *)ctx->u.aes.k1;
  210. *gk = cpu_to_be64(k1_hi);
  211. gk++;
  212. *gk = cpu_to_be64(k1_lo);
  213. k2_hi = (k1_hi << 1) | (k1_lo >> 63);
  214. k2_lo = k1_lo << 1;
  215. if (ctx->u.aes.k1[0] & 0x80) {
  216. k2_hi ^= rb_hi;
  217. k2_lo ^= rb_lo;
  218. }
  219. gk = (__be64 *)ctx->u.aes.k2;
  220. *gk = cpu_to_be64(k2_hi);
  221. gk++;
  222. *gk = cpu_to_be64(k2_lo);
  223. ctx->u.aes.kn_len = sizeof(ctx->u.aes.k1);
  224. sg_init_one(&ctx->u.aes.k1_sg, ctx->u.aes.k1, sizeof(ctx->u.aes.k1));
  225. sg_init_one(&ctx->u.aes.k2_sg, ctx->u.aes.k2, sizeof(ctx->u.aes.k2));
  226. /* Save the supplied key */
  227. memset(ctx->u.aes.key, 0, sizeof(ctx->u.aes.key));
  228. memcpy(ctx->u.aes.key, key, key_len);
  229. ctx->u.aes.key_len = key_len;
  230. sg_init_one(&ctx->u.aes.key_sg, ctx->u.aes.key, key_len);
  231. return ret;
  232. }
  233. static int ccp_aes_cmac_cra_init(struct crypto_tfm *tfm)
  234. {
  235. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  236. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  237. struct crypto_cipher *cipher_tfm;
  238. ctx->complete = ccp_aes_cmac_complete;
  239. ctx->u.aes.key_len = 0;
  240. crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_aes_cmac_req_ctx));
  241. cipher_tfm = crypto_alloc_cipher("aes", 0,
  242. CRYPTO_ALG_ASYNC |
  243. CRYPTO_ALG_NEED_FALLBACK);
  244. if (IS_ERR(cipher_tfm)) {
  245. pr_warn("could not load aes cipher driver\n");
  246. return PTR_ERR(cipher_tfm);
  247. }
  248. ctx->u.aes.tfm_cipher = cipher_tfm;
  249. return 0;
  250. }
  251. static void ccp_aes_cmac_cra_exit(struct crypto_tfm *tfm)
  252. {
  253. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  254. if (ctx->u.aes.tfm_cipher)
  255. crypto_free_cipher(ctx->u.aes.tfm_cipher);
  256. ctx->u.aes.tfm_cipher = NULL;
  257. }
  258. int ccp_register_aes_cmac_algs(struct list_head *head)
  259. {
  260. struct ccp_crypto_ahash_alg *ccp_alg;
  261. struct ahash_alg *alg;
  262. struct hash_alg_common *halg;
  263. struct crypto_alg *base;
  264. int ret;
  265. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  266. if (!ccp_alg)
  267. return -ENOMEM;
  268. INIT_LIST_HEAD(&ccp_alg->entry);
  269. ccp_alg->mode = CCP_AES_MODE_CMAC;
  270. alg = &ccp_alg->alg;
  271. alg->init = ccp_aes_cmac_init;
  272. alg->update = ccp_aes_cmac_update;
  273. alg->final = ccp_aes_cmac_final;
  274. alg->finup = ccp_aes_cmac_finup;
  275. alg->digest = ccp_aes_cmac_digest;
  276. alg->setkey = ccp_aes_cmac_setkey;
  277. halg = &alg->halg;
  278. halg->digestsize = AES_BLOCK_SIZE;
  279. base = &halg->base;
  280. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "cmac(aes)");
  281. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "cmac-aes-ccp");
  282. base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
  283. CRYPTO_ALG_KERN_DRIVER_ONLY |
  284. CRYPTO_ALG_NEED_FALLBACK;
  285. base->cra_blocksize = AES_BLOCK_SIZE;
  286. base->cra_ctxsize = sizeof(struct ccp_ctx);
  287. base->cra_priority = CCP_CRA_PRIORITY;
  288. base->cra_type = &crypto_ahash_type;
  289. base->cra_init = ccp_aes_cmac_cra_init;
  290. base->cra_exit = ccp_aes_cmac_cra_exit;
  291. base->cra_module = THIS_MODULE;
  292. ret = crypto_register_ahash(alg);
  293. if (ret) {
  294. pr_err("%s ahash algorithm registration error (%d)\n",
  295. base->cra_name, ret);
  296. kfree(ccp_alg);
  297. return ret;
  298. }
  299. list_add(&ccp_alg->entry, head);
  300. return 0;
  301. }