ccp-crypto-sha.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) SHA 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/hash.h>
  19. #include <crypto/internal/hash.h>
  20. #include <crypto/sha.h>
  21. #include <crypto/scatterwalk.h>
  22. #include "ccp-crypto.h"
  23. static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
  24. {
  25. struct ahash_request *req = ahash_request_cast(async_req);
  26. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  27. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  28. unsigned int digest_size = crypto_ahash_digestsize(tfm);
  29. if (ret)
  30. goto e_free;
  31. if (rctx->hash_rem) {
  32. /* Save remaining data to buffer */
  33. unsigned int offset = rctx->nbytes - rctx->hash_rem;
  34. scatterwalk_map_and_copy(rctx->buf, rctx->src,
  35. offset, rctx->hash_rem, 0);
  36. rctx->buf_count = rctx->hash_rem;
  37. } else
  38. rctx->buf_count = 0;
  39. /* Update result area if supplied */
  40. if (req->result)
  41. memcpy(req->result, rctx->ctx, digest_size);
  42. e_free:
  43. sg_free_table(&rctx->data_sg);
  44. return ret;
  45. }
  46. static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
  47. unsigned int final)
  48. {
  49. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  50. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  51. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  52. struct scatterlist *sg;
  53. unsigned int block_size =
  54. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  55. unsigned int sg_count;
  56. gfp_t gfp;
  57. u64 len;
  58. int ret;
  59. len = (u64)rctx->buf_count + (u64)nbytes;
  60. if (!final && (len <= block_size)) {
  61. scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
  62. 0, nbytes, 0);
  63. rctx->buf_count += nbytes;
  64. return 0;
  65. }
  66. rctx->src = req->src;
  67. rctx->nbytes = nbytes;
  68. rctx->final = final;
  69. rctx->hash_rem = final ? 0 : len & (block_size - 1);
  70. rctx->hash_cnt = len - rctx->hash_rem;
  71. if (!final && !rctx->hash_rem) {
  72. /* CCP can't do zero length final, so keep some data around */
  73. rctx->hash_cnt -= block_size;
  74. rctx->hash_rem = block_size;
  75. }
  76. /* Initialize the context scatterlist */
  77. sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
  78. sg = NULL;
  79. if (rctx->buf_count && nbytes) {
  80. /* Build the data scatterlist table - allocate enough entries
  81. * for both data pieces (buffer and input data)
  82. */
  83. gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  84. GFP_KERNEL : GFP_ATOMIC;
  85. sg_count = sg_nents(req->src) + 1;
  86. ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
  87. if (ret)
  88. return ret;
  89. sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
  90. sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
  91. sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
  92. sg_mark_end(sg);
  93. sg = rctx->data_sg.sgl;
  94. } else if (rctx->buf_count) {
  95. sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
  96. sg = &rctx->buf_sg;
  97. } else if (nbytes) {
  98. sg = req->src;
  99. }
  100. rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */
  101. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  102. INIT_LIST_HEAD(&rctx->cmd.entry);
  103. rctx->cmd.engine = CCP_ENGINE_SHA;
  104. rctx->cmd.u.sha.type = rctx->type;
  105. rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
  106. rctx->cmd.u.sha.ctx_len = sizeof(rctx->ctx);
  107. rctx->cmd.u.sha.src = sg;
  108. rctx->cmd.u.sha.src_len = rctx->hash_cnt;
  109. rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
  110. &ctx->u.sha.opad_sg : NULL;
  111. rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
  112. ctx->u.sha.opad_count : 0;
  113. rctx->cmd.u.sha.first = rctx->first;
  114. rctx->cmd.u.sha.final = rctx->final;
  115. rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
  116. rctx->first = 0;
  117. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  118. return ret;
  119. }
  120. static int ccp_sha_init(struct ahash_request *req)
  121. {
  122. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  123. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  124. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  125. struct ccp_crypto_ahash_alg *alg =
  126. ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
  127. unsigned int block_size =
  128. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  129. memset(rctx, 0, sizeof(*rctx));
  130. rctx->type = alg->type;
  131. rctx->first = 1;
  132. if (ctx->u.sha.key_len) {
  133. /* Buffer the HMAC key for first update */
  134. memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
  135. rctx->buf_count = block_size;
  136. }
  137. return 0;
  138. }
  139. static int ccp_sha_update(struct ahash_request *req)
  140. {
  141. return ccp_do_sha_update(req, req->nbytes, 0);
  142. }
  143. static int ccp_sha_final(struct ahash_request *req)
  144. {
  145. return ccp_do_sha_update(req, 0, 1);
  146. }
  147. static int ccp_sha_finup(struct ahash_request *req)
  148. {
  149. return ccp_do_sha_update(req, req->nbytes, 1);
  150. }
  151. static int ccp_sha_digest(struct ahash_request *req)
  152. {
  153. int ret;
  154. ret = ccp_sha_init(req);
  155. if (ret)
  156. return ret;
  157. return ccp_sha_finup(req);
  158. }
  159. static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
  160. unsigned int key_len)
  161. {
  162. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  163. struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
  164. struct {
  165. struct shash_desc sdesc;
  166. char ctx[crypto_shash_descsize(shash)];
  167. } desc;
  168. unsigned int block_size = crypto_shash_blocksize(shash);
  169. unsigned int digest_size = crypto_shash_digestsize(shash);
  170. int i, ret;
  171. /* Set to zero until complete */
  172. ctx->u.sha.key_len = 0;
  173. /* Clear key area to provide zero padding for keys smaller
  174. * than the block size
  175. */
  176. memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
  177. if (key_len > block_size) {
  178. /* Must hash the input key */
  179. desc.sdesc.tfm = shash;
  180. desc.sdesc.flags = crypto_ahash_get_flags(tfm) &
  181. CRYPTO_TFM_REQ_MAY_SLEEP;
  182. ret = crypto_shash_digest(&desc.sdesc, key, key_len,
  183. ctx->u.sha.key);
  184. if (ret) {
  185. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  186. return -EINVAL;
  187. }
  188. key_len = digest_size;
  189. } else
  190. memcpy(ctx->u.sha.key, key, key_len);
  191. for (i = 0; i < block_size; i++) {
  192. ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ 0x36;
  193. ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ 0x5c;
  194. }
  195. sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
  196. ctx->u.sha.opad_count = block_size;
  197. ctx->u.sha.key_len = key_len;
  198. return 0;
  199. }
  200. static int ccp_sha_cra_init(struct crypto_tfm *tfm)
  201. {
  202. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  203. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  204. ctx->complete = ccp_sha_complete;
  205. ctx->u.sha.key_len = 0;
  206. crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
  207. return 0;
  208. }
  209. static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
  210. {
  211. }
  212. static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
  213. {
  214. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  215. struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
  216. struct crypto_shash *hmac_tfm;
  217. hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
  218. if (IS_ERR(hmac_tfm)) {
  219. pr_warn("could not load driver %s need for HMAC support\n",
  220. alg->child_alg);
  221. return PTR_ERR(hmac_tfm);
  222. }
  223. ctx->u.sha.hmac_tfm = hmac_tfm;
  224. return ccp_sha_cra_init(tfm);
  225. }
  226. static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
  227. {
  228. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  229. if (ctx->u.sha.hmac_tfm)
  230. crypto_free_shash(ctx->u.sha.hmac_tfm);
  231. ccp_sha_cra_exit(tfm);
  232. }
  233. struct ccp_sha_def {
  234. const char *name;
  235. const char *drv_name;
  236. enum ccp_sha_type type;
  237. u32 digest_size;
  238. u32 block_size;
  239. };
  240. static struct ccp_sha_def sha_algs[] = {
  241. {
  242. .name = "sha1",
  243. .drv_name = "sha1-ccp",
  244. .type = CCP_SHA_TYPE_1,
  245. .digest_size = SHA1_DIGEST_SIZE,
  246. .block_size = SHA1_BLOCK_SIZE,
  247. },
  248. {
  249. .name = "sha224",
  250. .drv_name = "sha224-ccp",
  251. .type = CCP_SHA_TYPE_224,
  252. .digest_size = SHA224_DIGEST_SIZE,
  253. .block_size = SHA224_BLOCK_SIZE,
  254. },
  255. {
  256. .name = "sha256",
  257. .drv_name = "sha256-ccp",
  258. .type = CCP_SHA_TYPE_256,
  259. .digest_size = SHA256_DIGEST_SIZE,
  260. .block_size = SHA256_BLOCK_SIZE,
  261. },
  262. };
  263. static int ccp_register_hmac_alg(struct list_head *head,
  264. const struct ccp_sha_def *def,
  265. const struct ccp_crypto_ahash_alg *base_alg)
  266. {
  267. struct ccp_crypto_ahash_alg *ccp_alg;
  268. struct ahash_alg *alg;
  269. struct hash_alg_common *halg;
  270. struct crypto_alg *base;
  271. int ret;
  272. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  273. if (!ccp_alg)
  274. return -ENOMEM;
  275. /* Copy the base algorithm and only change what's necessary */
  276. *ccp_alg = *base_alg;
  277. INIT_LIST_HEAD(&ccp_alg->entry);
  278. strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
  279. alg = &ccp_alg->alg;
  280. alg->setkey = ccp_sha_setkey;
  281. halg = &alg->halg;
  282. base = &halg->base;
  283. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
  284. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
  285. def->drv_name);
  286. base->cra_init = ccp_hmac_sha_cra_init;
  287. base->cra_exit = ccp_hmac_sha_cra_exit;
  288. ret = crypto_register_ahash(alg);
  289. if (ret) {
  290. pr_err("%s ahash algorithm registration error (%d)\n",
  291. base->cra_name, ret);
  292. kfree(ccp_alg);
  293. return ret;
  294. }
  295. list_add(&ccp_alg->entry, head);
  296. return ret;
  297. }
  298. static int ccp_register_sha_alg(struct list_head *head,
  299. const struct ccp_sha_def *def)
  300. {
  301. struct ccp_crypto_ahash_alg *ccp_alg;
  302. struct ahash_alg *alg;
  303. struct hash_alg_common *halg;
  304. struct crypto_alg *base;
  305. int ret;
  306. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  307. if (!ccp_alg)
  308. return -ENOMEM;
  309. INIT_LIST_HEAD(&ccp_alg->entry);
  310. ccp_alg->type = def->type;
  311. alg = &ccp_alg->alg;
  312. alg->init = ccp_sha_init;
  313. alg->update = ccp_sha_update;
  314. alg->final = ccp_sha_final;
  315. alg->finup = ccp_sha_finup;
  316. alg->digest = ccp_sha_digest;
  317. halg = &alg->halg;
  318. halg->digestsize = def->digest_size;
  319. base = &halg->base;
  320. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  321. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  322. def->drv_name);
  323. base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
  324. CRYPTO_ALG_KERN_DRIVER_ONLY |
  325. CRYPTO_ALG_NEED_FALLBACK;
  326. base->cra_blocksize = def->block_size;
  327. base->cra_ctxsize = sizeof(struct ccp_ctx);
  328. base->cra_priority = CCP_CRA_PRIORITY;
  329. base->cra_type = &crypto_ahash_type;
  330. base->cra_init = ccp_sha_cra_init;
  331. base->cra_exit = ccp_sha_cra_exit;
  332. base->cra_module = THIS_MODULE;
  333. ret = crypto_register_ahash(alg);
  334. if (ret) {
  335. pr_err("%s ahash algorithm registration error (%d)\n",
  336. base->cra_name, ret);
  337. kfree(ccp_alg);
  338. return ret;
  339. }
  340. list_add(&ccp_alg->entry, head);
  341. ret = ccp_register_hmac_alg(head, def, ccp_alg);
  342. return ret;
  343. }
  344. int ccp_register_sha_algs(struct list_head *head)
  345. {
  346. int i, ret;
  347. for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
  348. ret = ccp_register_sha_alg(head, &sha_algs[i]);
  349. if (ret)
  350. return ret;
  351. }
  352. return 0;
  353. }