ccp-crypto-sha.c 13 KB

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