ccp-crypto-sha.c 12 KB

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