ccp-crypto-sha.c 13 KB

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