ccp-crypto-sha.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. struct ccp_sha_result {
  24. struct completion completion;
  25. int err;
  26. };
  27. static void ccp_sync_hash_complete(struct crypto_async_request *req, int err)
  28. {
  29. struct ccp_sha_result *result = req->data;
  30. if (err == -EINPROGRESS)
  31. return;
  32. result->err = err;
  33. complete(&result->completion);
  34. }
  35. static int ccp_sync_hash(struct crypto_ahash *tfm, u8 *buf,
  36. struct scatterlist *sg, unsigned int len)
  37. {
  38. struct ccp_sha_result result;
  39. struct ahash_request *req;
  40. int ret;
  41. init_completion(&result.completion);
  42. req = ahash_request_alloc(tfm, GFP_KERNEL);
  43. if (!req)
  44. return -ENOMEM;
  45. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  46. ccp_sync_hash_complete, &result);
  47. ahash_request_set_crypt(req, sg, buf, len);
  48. ret = crypto_ahash_digest(req);
  49. if ((ret == -EINPROGRESS) || (ret == -EBUSY)) {
  50. ret = wait_for_completion_interruptible(&result.completion);
  51. if (!ret)
  52. ret = result.err;
  53. }
  54. ahash_request_free(req);
  55. return ret;
  56. }
  57. static int ccp_sha_finish_hmac(struct crypto_async_request *async_req)
  58. {
  59. struct ahash_request *req = ahash_request_cast(async_req);
  60. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  61. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  62. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  63. struct scatterlist sg[2];
  64. unsigned int block_size =
  65. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  66. unsigned int digest_size = crypto_ahash_digestsize(tfm);
  67. sg_init_table(sg, ARRAY_SIZE(sg));
  68. sg_set_buf(&sg[0], ctx->u.sha.opad, block_size);
  69. sg_set_buf(&sg[1], rctx->ctx, digest_size);
  70. return ccp_sync_hash(ctx->u.sha.hmac_tfm, req->result, sg,
  71. block_size + digest_size);
  72. }
  73. static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
  74. {
  75. struct ahash_request *req = ahash_request_cast(async_req);
  76. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  77. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  78. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  79. unsigned int digest_size = crypto_ahash_digestsize(tfm);
  80. if (ret)
  81. goto e_free;
  82. if (rctx->hash_rem) {
  83. /* Save remaining data to buffer */
  84. unsigned int offset = rctx->nbytes - rctx->hash_rem;
  85. scatterwalk_map_and_copy(rctx->buf, rctx->src,
  86. offset, rctx->hash_rem, 0);
  87. rctx->buf_count = rctx->hash_rem;
  88. } else
  89. rctx->buf_count = 0;
  90. /* Update result area if supplied */
  91. if (req->result)
  92. memcpy(req->result, rctx->ctx, digest_size);
  93. /* If we're doing an HMAC, we need to perform that on the final op */
  94. if (rctx->final && ctx->u.sha.key_len)
  95. ret = ccp_sha_finish_hmac(async_req);
  96. e_free:
  97. sg_free_table(&rctx->data_sg);
  98. return ret;
  99. }
  100. static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
  101. unsigned int final)
  102. {
  103. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  104. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  105. struct scatterlist *sg;
  106. unsigned int block_size =
  107. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  108. unsigned int sg_count;
  109. gfp_t gfp;
  110. u64 len;
  111. int ret;
  112. len = (u64)rctx->buf_count + (u64)nbytes;
  113. if (!final && (len <= block_size)) {
  114. scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
  115. 0, nbytes, 0);
  116. rctx->buf_count += nbytes;
  117. return 0;
  118. }
  119. rctx->src = req->src;
  120. rctx->nbytes = nbytes;
  121. rctx->final = final;
  122. rctx->hash_rem = final ? 0 : len & (block_size - 1);
  123. rctx->hash_cnt = len - rctx->hash_rem;
  124. if (!final && !rctx->hash_rem) {
  125. /* CCP can't do zero length final, so keep some data around */
  126. rctx->hash_cnt -= block_size;
  127. rctx->hash_rem = block_size;
  128. }
  129. /* Initialize the context scatterlist */
  130. sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
  131. sg = NULL;
  132. if (rctx->buf_count && nbytes) {
  133. /* Build the data scatterlist table - allocate enough entries
  134. * for both data pieces (buffer and input data)
  135. */
  136. gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  137. GFP_KERNEL : GFP_ATOMIC;
  138. sg_count = sg_nents(req->src) + 1;
  139. ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
  140. if (ret)
  141. return ret;
  142. sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
  143. sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
  144. sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
  145. sg_mark_end(sg);
  146. sg = rctx->data_sg.sgl;
  147. } else if (rctx->buf_count) {
  148. sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
  149. sg = &rctx->buf_sg;
  150. } else if (nbytes) {
  151. sg = req->src;
  152. }
  153. rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */
  154. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  155. INIT_LIST_HEAD(&rctx->cmd.entry);
  156. rctx->cmd.engine = CCP_ENGINE_SHA;
  157. rctx->cmd.u.sha.type = rctx->type;
  158. rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
  159. rctx->cmd.u.sha.ctx_len = sizeof(rctx->ctx);
  160. rctx->cmd.u.sha.src = sg;
  161. rctx->cmd.u.sha.src_len = rctx->hash_cnt;
  162. rctx->cmd.u.sha.final = rctx->final;
  163. rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
  164. rctx->first = 0;
  165. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  166. return ret;
  167. }
  168. static int ccp_sha_init(struct ahash_request *req)
  169. {
  170. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  171. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  172. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  173. struct ccp_crypto_ahash_alg *alg =
  174. ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
  175. unsigned int block_size =
  176. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  177. memset(rctx, 0, sizeof(*rctx));
  178. memcpy(rctx->ctx, alg->init, sizeof(rctx->ctx));
  179. rctx->type = alg->type;
  180. rctx->first = 1;
  181. if (ctx->u.sha.key_len) {
  182. /* Buffer the HMAC key for first update */
  183. memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
  184. rctx->buf_count = block_size;
  185. }
  186. return 0;
  187. }
  188. static int ccp_sha_update(struct ahash_request *req)
  189. {
  190. return ccp_do_sha_update(req, req->nbytes, 0);
  191. }
  192. static int ccp_sha_final(struct ahash_request *req)
  193. {
  194. return ccp_do_sha_update(req, 0, 1);
  195. }
  196. static int ccp_sha_finup(struct ahash_request *req)
  197. {
  198. return ccp_do_sha_update(req, req->nbytes, 1);
  199. }
  200. static int ccp_sha_digest(struct ahash_request *req)
  201. {
  202. int ret;
  203. ret = ccp_sha_init(req);
  204. if (ret)
  205. return ret;
  206. return ccp_sha_finup(req);
  207. }
  208. static int ccp_sha_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 scatterlist sg;
  213. unsigned int block_size =
  214. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  215. unsigned int digest_size = crypto_ahash_digestsize(tfm);
  216. int i, ret;
  217. /* Set to zero until complete */
  218. ctx->u.sha.key_len = 0;
  219. /* Clear key area to provide zero padding for keys smaller
  220. * than the block size
  221. */
  222. memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
  223. if (key_len > block_size) {
  224. /* Must hash the input key */
  225. sg_init_one(&sg, key, key_len);
  226. ret = ccp_sync_hash(tfm, ctx->u.sha.key, &sg, key_len);
  227. if (ret) {
  228. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  229. return -EINVAL;
  230. }
  231. key_len = digest_size;
  232. } else
  233. memcpy(ctx->u.sha.key, key, key_len);
  234. for (i = 0; i < block_size; i++) {
  235. ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ 0x36;
  236. ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ 0x5c;
  237. }
  238. ctx->u.sha.key_len = key_len;
  239. return 0;
  240. }
  241. static int ccp_sha_cra_init(struct crypto_tfm *tfm)
  242. {
  243. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  244. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  245. ctx->complete = ccp_sha_complete;
  246. ctx->u.sha.key_len = 0;
  247. crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
  248. return 0;
  249. }
  250. static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
  251. {
  252. }
  253. static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
  254. {
  255. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  256. struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
  257. struct crypto_ahash *hmac_tfm;
  258. hmac_tfm = crypto_alloc_ahash(alg->child_alg,
  259. CRYPTO_ALG_TYPE_AHASH, 0);
  260. if (IS_ERR(hmac_tfm)) {
  261. pr_warn("could not load driver %s need for HMAC support\n",
  262. alg->child_alg);
  263. return PTR_ERR(hmac_tfm);
  264. }
  265. ctx->u.sha.hmac_tfm = hmac_tfm;
  266. return ccp_sha_cra_init(tfm);
  267. }
  268. static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
  269. {
  270. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  271. if (ctx->u.sha.hmac_tfm)
  272. crypto_free_ahash(ctx->u.sha.hmac_tfm);
  273. ccp_sha_cra_exit(tfm);
  274. }
  275. static const __be32 sha1_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
  276. cpu_to_be32(SHA1_H0), cpu_to_be32(SHA1_H1),
  277. cpu_to_be32(SHA1_H2), cpu_to_be32(SHA1_H3),
  278. cpu_to_be32(SHA1_H4), 0, 0, 0,
  279. };
  280. static const __be32 sha224_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
  281. cpu_to_be32(SHA224_H0), cpu_to_be32(SHA224_H1),
  282. cpu_to_be32(SHA224_H2), cpu_to_be32(SHA224_H3),
  283. cpu_to_be32(SHA224_H4), cpu_to_be32(SHA224_H5),
  284. cpu_to_be32(SHA224_H6), cpu_to_be32(SHA224_H7),
  285. };
  286. static const __be32 sha256_init[CCP_SHA_CTXSIZE / sizeof(__be32)] = {
  287. cpu_to_be32(SHA256_H0), cpu_to_be32(SHA256_H1),
  288. cpu_to_be32(SHA256_H2), cpu_to_be32(SHA256_H3),
  289. cpu_to_be32(SHA256_H4), cpu_to_be32(SHA256_H5),
  290. cpu_to_be32(SHA256_H6), cpu_to_be32(SHA256_H7),
  291. };
  292. struct ccp_sha_def {
  293. const char *name;
  294. const char *drv_name;
  295. const __be32 *init;
  296. enum ccp_sha_type type;
  297. u32 digest_size;
  298. u32 block_size;
  299. };
  300. static struct ccp_sha_def sha_algs[] = {
  301. {
  302. .name = "sha1",
  303. .drv_name = "sha1-ccp",
  304. .init = sha1_init,
  305. .type = CCP_SHA_TYPE_1,
  306. .digest_size = SHA1_DIGEST_SIZE,
  307. .block_size = SHA1_BLOCK_SIZE,
  308. },
  309. {
  310. .name = "sha224",
  311. .drv_name = "sha224-ccp",
  312. .init = sha224_init,
  313. .type = CCP_SHA_TYPE_224,
  314. .digest_size = SHA224_DIGEST_SIZE,
  315. .block_size = SHA224_BLOCK_SIZE,
  316. },
  317. {
  318. .name = "sha256",
  319. .drv_name = "sha256-ccp",
  320. .init = sha256_init,
  321. .type = CCP_SHA_TYPE_256,
  322. .digest_size = SHA256_DIGEST_SIZE,
  323. .block_size = SHA256_BLOCK_SIZE,
  324. },
  325. };
  326. static int ccp_register_hmac_alg(struct list_head *head,
  327. const struct ccp_sha_def *def,
  328. const struct ccp_crypto_ahash_alg *base_alg)
  329. {
  330. struct ccp_crypto_ahash_alg *ccp_alg;
  331. struct ahash_alg *alg;
  332. struct hash_alg_common *halg;
  333. struct crypto_alg *base;
  334. int ret;
  335. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  336. if (!ccp_alg)
  337. return -ENOMEM;
  338. /* Copy the base algorithm and only change what's necessary */
  339. *ccp_alg = *base_alg;
  340. INIT_LIST_HEAD(&ccp_alg->entry);
  341. strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
  342. alg = &ccp_alg->alg;
  343. alg->setkey = ccp_sha_setkey;
  344. halg = &alg->halg;
  345. base = &halg->base;
  346. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
  347. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
  348. def->drv_name);
  349. base->cra_init = ccp_hmac_sha_cra_init;
  350. base->cra_exit = ccp_hmac_sha_cra_exit;
  351. ret = crypto_register_ahash(alg);
  352. if (ret) {
  353. pr_err("%s ahash algorithm registration error (%d)\n",
  354. base->cra_name, ret);
  355. kfree(ccp_alg);
  356. return ret;
  357. }
  358. list_add(&ccp_alg->entry, head);
  359. return ret;
  360. }
  361. static int ccp_register_sha_alg(struct list_head *head,
  362. const struct ccp_sha_def *def)
  363. {
  364. struct ccp_crypto_ahash_alg *ccp_alg;
  365. struct ahash_alg *alg;
  366. struct hash_alg_common *halg;
  367. struct crypto_alg *base;
  368. int ret;
  369. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  370. if (!ccp_alg)
  371. return -ENOMEM;
  372. INIT_LIST_HEAD(&ccp_alg->entry);
  373. ccp_alg->init = def->init;
  374. ccp_alg->type = def->type;
  375. alg = &ccp_alg->alg;
  376. alg->init = ccp_sha_init;
  377. alg->update = ccp_sha_update;
  378. alg->final = ccp_sha_final;
  379. alg->finup = ccp_sha_finup;
  380. alg->digest = ccp_sha_digest;
  381. halg = &alg->halg;
  382. halg->digestsize = def->digest_size;
  383. base = &halg->base;
  384. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  385. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  386. def->drv_name);
  387. base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
  388. CRYPTO_ALG_KERN_DRIVER_ONLY |
  389. CRYPTO_ALG_NEED_FALLBACK;
  390. base->cra_blocksize = def->block_size;
  391. base->cra_ctxsize = sizeof(struct ccp_ctx);
  392. base->cra_priority = CCP_CRA_PRIORITY;
  393. base->cra_type = &crypto_ahash_type;
  394. base->cra_init = ccp_sha_cra_init;
  395. base->cra_exit = ccp_sha_cra_exit;
  396. base->cra_module = THIS_MODULE;
  397. ret = crypto_register_ahash(alg);
  398. if (ret) {
  399. pr_err("%s ahash algorithm registration error (%d)\n",
  400. base->cra_name, ret);
  401. kfree(ccp_alg);
  402. return ret;
  403. }
  404. list_add(&ccp_alg->entry, head);
  405. ret = ccp_register_hmac_alg(head, def, ccp_alg);
  406. return ret;
  407. }
  408. int ccp_register_sha_algs(struct list_head *head)
  409. {
  410. int i, ret;
  411. for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
  412. ret = ccp_register_sha_alg(head, &sha_algs[i]);
  413. if (ret)
  414. return ret;
  415. }
  416. return 0;
  417. }