ccp-crypto-sha.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
  3. *
  4. * Copyright (C) 2013,2016 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/internal/hash.h>
  21. #include <crypto/sha.h>
  22. #include <crypto/scatterwalk.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)
  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. default:
  127. /* Should never get here */
  128. break;
  129. }
  130. rctx->cmd.u.sha.src = sg;
  131. rctx->cmd.u.sha.src_len = rctx->hash_cnt;
  132. rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
  133. &ctx->u.sha.opad_sg : NULL;
  134. rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
  135. ctx->u.sha.opad_count : 0;
  136. rctx->cmd.u.sha.first = rctx->first;
  137. rctx->cmd.u.sha.final = rctx->final;
  138. rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
  139. rctx->first = 0;
  140. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  141. return ret;
  142. e_free:
  143. sg_free_table(&rctx->data_sg);
  144. return ret;
  145. }
  146. static int ccp_sha_init(struct ahash_request *req)
  147. {
  148. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  149. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  150. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  151. struct ccp_crypto_ahash_alg *alg =
  152. ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
  153. unsigned int block_size =
  154. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  155. memset(rctx, 0, sizeof(*rctx));
  156. rctx->type = alg->type;
  157. rctx->first = 1;
  158. if (ctx->u.sha.key_len) {
  159. /* Buffer the HMAC key for first update */
  160. memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
  161. rctx->buf_count = block_size;
  162. }
  163. return 0;
  164. }
  165. static int ccp_sha_update(struct ahash_request *req)
  166. {
  167. return ccp_do_sha_update(req, req->nbytes, 0);
  168. }
  169. static int ccp_sha_final(struct ahash_request *req)
  170. {
  171. return ccp_do_sha_update(req, 0, 1);
  172. }
  173. static int ccp_sha_finup(struct ahash_request *req)
  174. {
  175. return ccp_do_sha_update(req, req->nbytes, 1);
  176. }
  177. static int ccp_sha_digest(struct ahash_request *req)
  178. {
  179. int ret;
  180. ret = ccp_sha_init(req);
  181. if (ret)
  182. return ret;
  183. return ccp_sha_finup(req);
  184. }
  185. static int ccp_sha_export(struct ahash_request *req, void *out)
  186. {
  187. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  188. struct ccp_sha_exp_ctx state;
  189. /* Don't let anything leak to 'out' */
  190. memset(&state, 0, sizeof(state));
  191. state.type = rctx->type;
  192. state.msg_bits = rctx->msg_bits;
  193. state.first = rctx->first;
  194. memcpy(state.ctx, rctx->ctx, sizeof(state.ctx));
  195. state.buf_count = rctx->buf_count;
  196. memcpy(state.buf, rctx->buf, sizeof(state.buf));
  197. /* 'out' may not be aligned so memcpy from local variable */
  198. memcpy(out, &state, sizeof(state));
  199. return 0;
  200. }
  201. static int ccp_sha_import(struct ahash_request *req, const void *in)
  202. {
  203. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  204. struct ccp_sha_exp_ctx state;
  205. /* 'in' may not be aligned so memcpy to local variable */
  206. memcpy(&state, in, sizeof(state));
  207. memset(rctx, 0, sizeof(*rctx));
  208. rctx->type = state.type;
  209. rctx->msg_bits = state.msg_bits;
  210. rctx->first = state.first;
  211. memcpy(rctx->ctx, state.ctx, sizeof(rctx->ctx));
  212. rctx->buf_count = state.buf_count;
  213. memcpy(rctx->buf, state.buf, sizeof(rctx->buf));
  214. return 0;
  215. }
  216. static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
  217. unsigned int key_len)
  218. {
  219. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  220. struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
  221. SHASH_DESC_ON_STACK(sdesc, shash);
  222. unsigned int block_size = crypto_shash_blocksize(shash);
  223. unsigned int digest_size = crypto_shash_digestsize(shash);
  224. int i, ret;
  225. /* Set to zero until complete */
  226. ctx->u.sha.key_len = 0;
  227. /* Clear key area to provide zero padding for keys smaller
  228. * than the block size
  229. */
  230. memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
  231. if (key_len > block_size) {
  232. /* Must hash the input key */
  233. sdesc->tfm = shash;
  234. sdesc->flags = crypto_ahash_get_flags(tfm) &
  235. CRYPTO_TFM_REQ_MAY_SLEEP;
  236. ret = crypto_shash_digest(sdesc, key, key_len,
  237. ctx->u.sha.key);
  238. if (ret) {
  239. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  240. return -EINVAL;
  241. }
  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] ^ 0x36;
  248. ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ 0x5c;
  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. static int ccp_register_hmac_alg(struct list_head *head,
  323. const struct ccp_sha_def *def,
  324. const struct ccp_crypto_ahash_alg *base_alg)
  325. {
  326. struct ccp_crypto_ahash_alg *ccp_alg;
  327. struct ahash_alg *alg;
  328. struct hash_alg_common *halg;
  329. struct crypto_alg *base;
  330. int ret;
  331. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  332. if (!ccp_alg)
  333. return -ENOMEM;
  334. /* Copy the base algorithm and only change what's necessary */
  335. *ccp_alg = *base_alg;
  336. INIT_LIST_HEAD(&ccp_alg->entry);
  337. strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
  338. alg = &ccp_alg->alg;
  339. alg->setkey = ccp_sha_setkey;
  340. halg = &alg->halg;
  341. base = &halg->base;
  342. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
  343. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
  344. def->drv_name);
  345. base->cra_init = ccp_hmac_sha_cra_init;
  346. base->cra_exit = ccp_hmac_sha_cra_exit;
  347. ret = crypto_register_ahash(alg);
  348. if (ret) {
  349. pr_err("%s ahash algorithm registration error (%d)\n",
  350. base->cra_name, ret);
  351. kfree(ccp_alg);
  352. return ret;
  353. }
  354. list_add(&ccp_alg->entry, head);
  355. return ret;
  356. }
  357. static int ccp_register_sha_alg(struct list_head *head,
  358. const struct ccp_sha_def *def)
  359. {
  360. struct ccp_crypto_ahash_alg *ccp_alg;
  361. struct ahash_alg *alg;
  362. struct hash_alg_common *halg;
  363. struct crypto_alg *base;
  364. int ret;
  365. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  366. if (!ccp_alg)
  367. return -ENOMEM;
  368. INIT_LIST_HEAD(&ccp_alg->entry);
  369. ccp_alg->type = def->type;
  370. alg = &ccp_alg->alg;
  371. alg->init = ccp_sha_init;
  372. alg->update = ccp_sha_update;
  373. alg->final = ccp_sha_final;
  374. alg->finup = ccp_sha_finup;
  375. alg->digest = ccp_sha_digest;
  376. alg->export = ccp_sha_export;
  377. alg->import = ccp_sha_import;
  378. halg = &alg->halg;
  379. halg->digestsize = def->digest_size;
  380. halg->statesize = sizeof(struct ccp_sha_exp_ctx);
  381. base = &halg->base;
  382. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  383. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  384. def->drv_name);
  385. base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
  386. CRYPTO_ALG_KERN_DRIVER_ONLY |
  387. CRYPTO_ALG_NEED_FALLBACK;
  388. base->cra_blocksize = def->block_size;
  389. base->cra_ctxsize = sizeof(struct ccp_ctx);
  390. base->cra_priority = CCP_CRA_PRIORITY;
  391. base->cra_type = &crypto_ahash_type;
  392. base->cra_init = ccp_sha_cra_init;
  393. base->cra_exit = ccp_sha_cra_exit;
  394. base->cra_module = THIS_MODULE;
  395. ret = crypto_register_ahash(alg);
  396. if (ret) {
  397. pr_err("%s ahash algorithm registration error (%d)\n",
  398. base->cra_name, ret);
  399. kfree(ccp_alg);
  400. return ret;
  401. }
  402. list_add(&ccp_alg->entry, head);
  403. ret = ccp_register_hmac_alg(head, def, ccp_alg);
  404. return ret;
  405. }
  406. int ccp_register_sha_algs(struct list_head *head)
  407. {
  408. int i, ret;
  409. unsigned int ccpversion = ccp_version();
  410. for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
  411. if (sha_algs[i].version > ccpversion)
  412. continue;
  413. ret = ccp_register_sha_alg(head, &sha_algs[i]);
  414. if (ret)
  415. return ret;
  416. }
  417. return 0;
  418. }