ccp-crypto-sha.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
  3. *
  4. * Copyright (C) 2013,2017 Advanced Micro Devices, Inc.
  5. *
  6. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  7. * Author: Gary R Hook <gary.hook@amd.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/delay.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/crypto.h>
  18. #include <crypto/algapi.h>
  19. #include <crypto/hash.h>
  20. #include <crypto/hmac.h>
  21. #include <crypto/internal/hash.h>
  22. #include <crypto/sha.h>
  23. #include <crypto/scatterwalk.h>
  24. #include "ccp-crypto.h"
  25. static int ccp_sha_complete(struct crypto_async_request *async_req, int ret)
  26. {
  27. struct ahash_request *req = ahash_request_cast(async_req);
  28. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  29. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  30. unsigned int digest_size = crypto_ahash_digestsize(tfm);
  31. if (ret)
  32. goto e_free;
  33. if (rctx->hash_rem) {
  34. /* Save remaining data to buffer */
  35. unsigned int offset = rctx->nbytes - rctx->hash_rem;
  36. scatterwalk_map_and_copy(rctx->buf, rctx->src,
  37. offset, rctx->hash_rem, 0);
  38. rctx->buf_count = rctx->hash_rem;
  39. } else {
  40. rctx->buf_count = 0;
  41. }
  42. /* Update result area if supplied */
  43. if (req->result && rctx->final)
  44. memcpy(req->result, rctx->ctx, digest_size);
  45. e_free:
  46. sg_free_table(&rctx->data_sg);
  47. return ret;
  48. }
  49. static int ccp_do_sha_update(struct ahash_request *req, unsigned int nbytes,
  50. unsigned int final)
  51. {
  52. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  53. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  54. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  55. struct scatterlist *sg;
  56. unsigned int block_size =
  57. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  58. unsigned int sg_count;
  59. gfp_t gfp;
  60. u64 len;
  61. int ret;
  62. len = (u64)rctx->buf_count + (u64)nbytes;
  63. if (!final && (len <= block_size)) {
  64. scatterwalk_map_and_copy(rctx->buf + rctx->buf_count, req->src,
  65. 0, nbytes, 0);
  66. rctx->buf_count += nbytes;
  67. return 0;
  68. }
  69. rctx->src = req->src;
  70. rctx->nbytes = nbytes;
  71. rctx->final = final;
  72. rctx->hash_rem = final ? 0 : len & (block_size - 1);
  73. rctx->hash_cnt = len - rctx->hash_rem;
  74. if (!final && !rctx->hash_rem) {
  75. /* CCP can't do zero length final, so keep some data around */
  76. rctx->hash_cnt -= block_size;
  77. rctx->hash_rem = block_size;
  78. }
  79. /* Initialize the context scatterlist */
  80. sg_init_one(&rctx->ctx_sg, rctx->ctx, sizeof(rctx->ctx));
  81. sg = NULL;
  82. if (rctx->buf_count && nbytes) {
  83. /* Build the data scatterlist table - allocate enough entries
  84. * for both data pieces (buffer and input data)
  85. */
  86. gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
  87. GFP_KERNEL : GFP_ATOMIC;
  88. sg_count = sg_nents(req->src) + 1;
  89. ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
  90. if (ret)
  91. return ret;
  92. sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
  93. sg = ccp_crypto_sg_table_add(&rctx->data_sg, &rctx->buf_sg);
  94. if (!sg) {
  95. ret = -EINVAL;
  96. goto e_free;
  97. }
  98. sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
  99. if (!sg) {
  100. ret = -EINVAL;
  101. goto e_free;
  102. }
  103. sg_mark_end(sg);
  104. sg = rctx->data_sg.sgl;
  105. } else if (rctx->buf_count) {
  106. sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
  107. sg = &rctx->buf_sg;
  108. } else if (nbytes) {
  109. sg = req->src;
  110. }
  111. rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */
  112. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  113. INIT_LIST_HEAD(&rctx->cmd.entry);
  114. rctx->cmd.engine = CCP_ENGINE_SHA;
  115. rctx->cmd.u.sha.type = rctx->type;
  116. rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
  117. switch (rctx->type) {
  118. case CCP_SHA_TYPE_1:
  119. rctx->cmd.u.sha.ctx_len = SHA1_DIGEST_SIZE;
  120. break;
  121. case CCP_SHA_TYPE_224:
  122. rctx->cmd.u.sha.ctx_len = SHA224_DIGEST_SIZE;
  123. break;
  124. case CCP_SHA_TYPE_256:
  125. rctx->cmd.u.sha.ctx_len = SHA256_DIGEST_SIZE;
  126. break;
  127. case CCP_SHA_TYPE_384:
  128. rctx->cmd.u.sha.ctx_len = SHA384_DIGEST_SIZE;
  129. break;
  130. case CCP_SHA_TYPE_512:
  131. rctx->cmd.u.sha.ctx_len = SHA512_DIGEST_SIZE;
  132. break;
  133. default:
  134. /* Should never get here */
  135. break;
  136. }
  137. rctx->cmd.u.sha.src = sg;
  138. rctx->cmd.u.sha.src_len = rctx->hash_cnt;
  139. rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
  140. &ctx->u.sha.opad_sg : NULL;
  141. rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
  142. ctx->u.sha.opad_count : 0;
  143. rctx->cmd.u.sha.first = rctx->first;
  144. rctx->cmd.u.sha.final = rctx->final;
  145. rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
  146. rctx->first = 0;
  147. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  148. return ret;
  149. e_free:
  150. sg_free_table(&rctx->data_sg);
  151. return ret;
  152. }
  153. static int ccp_sha_init(struct ahash_request *req)
  154. {
  155. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  156. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  157. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  158. struct ccp_crypto_ahash_alg *alg =
  159. ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
  160. unsigned int block_size =
  161. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  162. memset(rctx, 0, sizeof(*rctx));
  163. rctx->type = alg->type;
  164. rctx->first = 1;
  165. if (ctx->u.sha.key_len) {
  166. /* Buffer the HMAC key for first update */
  167. memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
  168. rctx->buf_count = block_size;
  169. }
  170. return 0;
  171. }
  172. static int ccp_sha_update(struct ahash_request *req)
  173. {
  174. return ccp_do_sha_update(req, req->nbytes, 0);
  175. }
  176. static int ccp_sha_final(struct ahash_request *req)
  177. {
  178. return ccp_do_sha_update(req, 0, 1);
  179. }
  180. static int ccp_sha_finup(struct ahash_request *req)
  181. {
  182. return ccp_do_sha_update(req, req->nbytes, 1);
  183. }
  184. static int ccp_sha_digest(struct ahash_request *req)
  185. {
  186. int ret;
  187. ret = ccp_sha_init(req);
  188. if (ret)
  189. return ret;
  190. return ccp_sha_finup(req);
  191. }
  192. static int ccp_sha_export(struct ahash_request *req, void *out)
  193. {
  194. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  195. struct ccp_sha_exp_ctx state;
  196. /* Don't let anything leak to 'out' */
  197. memset(&state, 0, sizeof(state));
  198. state.type = rctx->type;
  199. state.msg_bits = rctx->msg_bits;
  200. state.first = rctx->first;
  201. memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
  202. state.buf_count = rctx->buf_count;
  203. memcpy(state.buf, rctx->buf, sizeof(state.buf));
  204. /* 'out' may not be aligned so memcpy from local variable */
  205. memcpy(out, &state, sizeof(state));
  206. return 0;
  207. }
  208. static int ccp_sha_import(struct ahash_request *req, const void *in)
  209. {
  210. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  211. struct ccp_sha_exp_ctx state;
  212. /* 'in' may not be aligned so memcpy to local variable */
  213. memcpy(&state, in, sizeof(state));
  214. memset(rctx, 0, sizeof(*rctx));
  215. rctx->type = state.type;
  216. rctx->msg_bits = state.msg_bits;
  217. rctx->first = state.first;
  218. memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
  219. rctx->buf_count = state.buf_count;
  220. memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
  221. return 0;
  222. }
  223. static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
  224. unsigned int key_len)
  225. {
  226. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  227. struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
  228. SHASH_DESC_ON_STACK(sdesc, shash);
  229. unsigned int block_size = crypto_shash_blocksize(shash);
  230. unsigned int digest_size = crypto_shash_digestsize(shash);
  231. int i, ret;
  232. /* Set to zero until complete */
  233. ctx->u.sha.key_len = 0;
  234. /* Clear key area to provide zero padding for keys smaller
  235. * than the block size
  236. */
  237. memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
  238. if (key_len > block_size) {
  239. /* Must hash the input key */
  240. sdesc->tfm = shash;
  241. sdesc->flags = crypto_ahash_get_flags(tfm) &
  242. CRYPTO_TFM_REQ_MAY_SLEEP;
  243. ret = crypto_shash_digest(sdesc, key, key_len,
  244. ctx->u.sha.key);
  245. if (ret) {
  246. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  247. return -EINVAL;
  248. }
  249. key_len = digest_size;
  250. } else {
  251. memcpy(ctx->u.sha.key, key, key_len);
  252. }
  253. for (i = 0; i < block_size; i++) {
  254. ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ HMAC_IPAD_VALUE;
  255. ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ HMAC_OPAD_VALUE;
  256. }
  257. sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
  258. ctx->u.sha.opad_count = block_size;
  259. ctx->u.sha.key_len = key_len;
  260. return 0;
  261. }
  262. static int ccp_sha_cra_init(struct crypto_tfm *tfm)
  263. {
  264. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  265. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  266. ctx->complete = ccp_sha_complete;
  267. ctx->u.sha.key_len = 0;
  268. crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
  269. return 0;
  270. }
  271. static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
  272. {
  273. }
  274. static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
  275. {
  276. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  277. struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
  278. struct crypto_shash *hmac_tfm;
  279. hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
  280. if (IS_ERR(hmac_tfm)) {
  281. pr_warn("could not load driver %s need for HMAC support\n",
  282. alg->child_alg);
  283. return PTR_ERR(hmac_tfm);
  284. }
  285. ctx->u.sha.hmac_tfm = hmac_tfm;
  286. return ccp_sha_cra_init(tfm);
  287. }
  288. static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
  289. {
  290. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  291. if (ctx->u.sha.hmac_tfm)
  292. crypto_free_shash(ctx->u.sha.hmac_tfm);
  293. ccp_sha_cra_exit(tfm);
  294. }
  295. struct ccp_sha_def {
  296. unsigned int version;
  297. const char *name;
  298. const char *drv_name;
  299. enum ccp_sha_type type;
  300. u32 digest_size;
  301. u32 block_size;
  302. };
  303. static struct ccp_sha_def sha_algs[] = {
  304. {
  305. .version = CCP_VERSION(3, 0),
  306. .name = "sha1",
  307. .drv_name = "sha1-ccp",
  308. .type = CCP_SHA_TYPE_1,
  309. .digest_size = SHA1_DIGEST_SIZE,
  310. .block_size = SHA1_BLOCK_SIZE,
  311. },
  312. {
  313. .version = CCP_VERSION(3, 0),
  314. .name = "sha224",
  315. .drv_name = "sha224-ccp",
  316. .type = CCP_SHA_TYPE_224,
  317. .digest_size = SHA224_DIGEST_SIZE,
  318. .block_size = SHA224_BLOCK_SIZE,
  319. },
  320. {
  321. .version = CCP_VERSION(3, 0),
  322. .name = "sha256",
  323. .drv_name = "sha256-ccp",
  324. .type = CCP_SHA_TYPE_256,
  325. .digest_size = SHA256_DIGEST_SIZE,
  326. .block_size = SHA256_BLOCK_SIZE,
  327. },
  328. {
  329. .version = CCP_VERSION(5, 0),
  330. .name = "sha384",
  331. .drv_name = "sha384-ccp",
  332. .type = CCP_SHA_TYPE_384,
  333. .digest_size = SHA384_DIGEST_SIZE,
  334. .block_size = SHA384_BLOCK_SIZE,
  335. },
  336. {
  337. .version = CCP_VERSION(5, 0),
  338. .name = "sha512",
  339. .drv_name = "sha512-ccp",
  340. .type = CCP_SHA_TYPE_512,
  341. .digest_size = SHA512_DIGEST_SIZE,
  342. .block_size = SHA512_BLOCK_SIZE,
  343. },
  344. };
  345. static int ccp_register_hmac_alg(struct list_head *head,
  346. const struct ccp_sha_def *def,
  347. const struct ccp_crypto_ahash_alg *base_alg)
  348. {
  349. struct ccp_crypto_ahash_alg *ccp_alg;
  350. struct ahash_alg *alg;
  351. struct hash_alg_common *halg;
  352. struct crypto_alg *base;
  353. int ret;
  354. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  355. if (!ccp_alg)
  356. return -ENOMEM;
  357. /* Copy the base algorithm and only change what's necessary */
  358. *ccp_alg = *base_alg;
  359. INIT_LIST_HEAD(&ccp_alg->entry);
  360. strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
  361. alg = &ccp_alg->alg;
  362. alg->setkey = ccp_sha_setkey;
  363. halg = &alg->halg;
  364. base = &halg->base;
  365. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
  366. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
  367. def->drv_name);
  368. base->cra_init = ccp_hmac_sha_cra_init;
  369. base->cra_exit = ccp_hmac_sha_cra_exit;
  370. ret = crypto_register_ahash(alg);
  371. if (ret) {
  372. pr_err("%s ahash algorithm registration error (%d)\n",
  373. base->cra_name, ret);
  374. kfree(ccp_alg);
  375. return ret;
  376. }
  377. list_add(&ccp_alg->entry, head);
  378. return ret;
  379. }
  380. static int ccp_register_sha_alg(struct list_head *head,
  381. const struct ccp_sha_def *def)
  382. {
  383. struct ccp_crypto_ahash_alg *ccp_alg;
  384. struct ahash_alg *alg;
  385. struct hash_alg_common *halg;
  386. struct crypto_alg *base;
  387. int ret;
  388. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  389. if (!ccp_alg)
  390. return -ENOMEM;
  391. INIT_LIST_HEAD(&ccp_alg->entry);
  392. ccp_alg->type = def->type;
  393. alg = &ccp_alg->alg;
  394. alg->init = ccp_sha_init;
  395. alg->update = ccp_sha_update;
  396. alg->final = ccp_sha_final;
  397. alg->finup = ccp_sha_finup;
  398. alg->digest = ccp_sha_digest;
  399. alg->export = ccp_sha_export;
  400. alg->import = ccp_sha_import;
  401. halg = &alg->halg;
  402. halg->digestsize = def->digest_size;
  403. halg->statesize = sizeof(struct ccp_sha_exp_ctx);
  404. base = &halg->base;
  405. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  406. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  407. def->drv_name);
  408. base->cra_flags = CRYPTO_ALG_ASYNC |
  409. CRYPTO_ALG_KERN_DRIVER_ONLY |
  410. CRYPTO_ALG_NEED_FALLBACK;
  411. base->cra_blocksize = def->block_size;
  412. base->cra_ctxsize = sizeof(struct ccp_ctx);
  413. base->cra_priority = CCP_CRA_PRIORITY;
  414. base->cra_init = ccp_sha_cra_init;
  415. base->cra_exit = ccp_sha_cra_exit;
  416. base->cra_module = THIS_MODULE;
  417. ret = crypto_register_ahash(alg);
  418. if (ret) {
  419. pr_err("%s ahash algorithm registration error (%d)\n",
  420. base->cra_name, ret);
  421. kfree(ccp_alg);
  422. return ret;
  423. }
  424. list_add(&ccp_alg->entry, head);
  425. ret = ccp_register_hmac_alg(head, def, ccp_alg);
  426. return ret;
  427. }
  428. int ccp_register_sha_algs(struct list_head *head)
  429. {
  430. int i, ret;
  431. unsigned int ccpversion = ccp_version();
  432. for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
  433. if (sha_algs[i].version > ccpversion)
  434. continue;
  435. ret = ccp_register_sha_alg(head, &sha_algs[i]);
  436. if (ret)
  437. return ret;
  438. }
  439. return 0;
  440. }