ccp-crypto-sha.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * AMD Cryptographic Coprocessor (CCP) SHA crypto API support
  3. *
  4. * Copyright (C) 2013 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. sg = ccp_crypto_sg_table_add(&rctx->data_sg, req->src);
  93. sg_mark_end(sg);
  94. sg = rctx->data_sg.sgl;
  95. } else if (rctx->buf_count) {
  96. sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
  97. sg = &rctx->buf_sg;
  98. } else if (nbytes) {
  99. sg = req->src;
  100. }
  101. rctx->msg_bits += (rctx->hash_cnt << 3); /* Total in bits */
  102. memset(&rctx->cmd, 0, sizeof(rctx->cmd));
  103. INIT_LIST_HEAD(&rctx->cmd.entry);
  104. rctx->cmd.engine = CCP_ENGINE_SHA;
  105. rctx->cmd.u.sha.type = rctx->type;
  106. rctx->cmd.u.sha.ctx = &rctx->ctx_sg;
  107. rctx->cmd.u.sha.ctx_len = sizeof(rctx->ctx);
  108. rctx->cmd.u.sha.src = sg;
  109. rctx->cmd.u.sha.src_len = rctx->hash_cnt;
  110. rctx->cmd.u.sha.opad = ctx->u.sha.key_len ?
  111. &ctx->u.sha.opad_sg : NULL;
  112. rctx->cmd.u.sha.opad_len = ctx->u.sha.key_len ?
  113. ctx->u.sha.opad_count : 0;
  114. rctx->cmd.u.sha.first = rctx->first;
  115. rctx->cmd.u.sha.final = rctx->final;
  116. rctx->cmd.u.sha.msg_bits = rctx->msg_bits;
  117. rctx->first = 0;
  118. ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
  119. return ret;
  120. }
  121. static int ccp_sha_init(struct ahash_request *req)
  122. {
  123. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  124. struct ccp_ctx *ctx = crypto_ahash_ctx(tfm);
  125. struct ccp_sha_req_ctx *rctx = ahash_request_ctx(req);
  126. struct ccp_crypto_ahash_alg *alg =
  127. ccp_crypto_ahash_alg(crypto_ahash_tfm(tfm));
  128. unsigned int block_size =
  129. crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  130. memset(rctx, 0, sizeof(*rctx));
  131. rctx->type = alg->type;
  132. rctx->first = 1;
  133. if (ctx->u.sha.key_len) {
  134. /* Buffer the HMAC key for first update */
  135. memcpy(rctx->buf, ctx->u.sha.ipad, block_size);
  136. rctx->buf_count = block_size;
  137. }
  138. return 0;
  139. }
  140. static int ccp_sha_update(struct ahash_request *req)
  141. {
  142. return ccp_do_sha_update(req, req->nbytes, 0);
  143. }
  144. static int ccp_sha_final(struct ahash_request *req)
  145. {
  146. return ccp_do_sha_update(req, 0, 1);
  147. }
  148. static int ccp_sha_finup(struct ahash_request *req)
  149. {
  150. return ccp_do_sha_update(req, req->nbytes, 1);
  151. }
  152. static int ccp_sha_digest(struct ahash_request *req)
  153. {
  154. int ret;
  155. ret = ccp_sha_init(req);
  156. if (ret)
  157. return ret;
  158. return ccp_sha_finup(req);
  159. }
  160. static int ccp_sha_setkey(struct crypto_ahash *tfm, const u8 *key,
  161. unsigned int key_len)
  162. {
  163. struct ccp_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
  164. struct crypto_shash *shash = ctx->u.sha.hmac_tfm;
  165. SHASH_DESC_ON_STACK(sdesc, shash);
  166. unsigned int block_size = crypto_shash_blocksize(shash);
  167. unsigned int digest_size = crypto_shash_digestsize(shash);
  168. int i, ret;
  169. /* Set to zero until complete */
  170. ctx->u.sha.key_len = 0;
  171. /* Clear key area to provide zero padding for keys smaller
  172. * than the block size
  173. */
  174. memset(ctx->u.sha.key, 0, sizeof(ctx->u.sha.key));
  175. if (key_len > block_size) {
  176. /* Must hash the input key */
  177. sdesc->tfm = shash;
  178. sdesc->flags = crypto_ahash_get_flags(tfm) &
  179. CRYPTO_TFM_REQ_MAY_SLEEP;
  180. ret = crypto_shash_digest(sdesc, key, key_len,
  181. ctx->u.sha.key);
  182. if (ret) {
  183. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  184. return -EINVAL;
  185. }
  186. key_len = digest_size;
  187. } else {
  188. memcpy(ctx->u.sha.key, key, key_len);
  189. }
  190. for (i = 0; i < block_size; i++) {
  191. ctx->u.sha.ipad[i] = ctx->u.sha.key[i] ^ 0x36;
  192. ctx->u.sha.opad[i] = ctx->u.sha.key[i] ^ 0x5c;
  193. }
  194. sg_init_one(&ctx->u.sha.opad_sg, ctx->u.sha.opad, block_size);
  195. ctx->u.sha.opad_count = block_size;
  196. ctx->u.sha.key_len = key_len;
  197. return 0;
  198. }
  199. static int ccp_sha_cra_init(struct crypto_tfm *tfm)
  200. {
  201. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  202. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  203. ctx->complete = ccp_sha_complete;
  204. ctx->u.sha.key_len = 0;
  205. crypto_ahash_set_reqsize(ahash, sizeof(struct ccp_sha_req_ctx));
  206. return 0;
  207. }
  208. static void ccp_sha_cra_exit(struct crypto_tfm *tfm)
  209. {
  210. }
  211. static int ccp_hmac_sha_cra_init(struct crypto_tfm *tfm)
  212. {
  213. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  214. struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
  215. struct crypto_shash *hmac_tfm;
  216. hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
  217. if (IS_ERR(hmac_tfm)) {
  218. pr_warn("could not load driver %s need for HMAC support\n",
  219. alg->child_alg);
  220. return PTR_ERR(hmac_tfm);
  221. }
  222. ctx->u.sha.hmac_tfm = hmac_tfm;
  223. return ccp_sha_cra_init(tfm);
  224. }
  225. static void ccp_hmac_sha_cra_exit(struct crypto_tfm *tfm)
  226. {
  227. struct ccp_ctx *ctx = crypto_tfm_ctx(tfm);
  228. if (ctx->u.sha.hmac_tfm)
  229. crypto_free_shash(ctx->u.sha.hmac_tfm);
  230. ccp_sha_cra_exit(tfm);
  231. }
  232. struct ccp_sha_def {
  233. const char *name;
  234. const char *drv_name;
  235. enum ccp_sha_type type;
  236. u32 digest_size;
  237. u32 block_size;
  238. };
  239. static struct ccp_sha_def sha_algs[] = {
  240. {
  241. .name = "sha1",
  242. .drv_name = "sha1-ccp",
  243. .type = CCP_SHA_TYPE_1,
  244. .digest_size = SHA1_DIGEST_SIZE,
  245. .block_size = SHA1_BLOCK_SIZE,
  246. },
  247. {
  248. .name = "sha224",
  249. .drv_name = "sha224-ccp",
  250. .type = CCP_SHA_TYPE_224,
  251. .digest_size = SHA224_DIGEST_SIZE,
  252. .block_size = SHA224_BLOCK_SIZE,
  253. },
  254. {
  255. .name = "sha256",
  256. .drv_name = "sha256-ccp",
  257. .type = CCP_SHA_TYPE_256,
  258. .digest_size = SHA256_DIGEST_SIZE,
  259. .block_size = SHA256_BLOCK_SIZE,
  260. },
  261. };
  262. static int ccp_register_hmac_alg(struct list_head *head,
  263. const struct ccp_sha_def *def,
  264. const struct ccp_crypto_ahash_alg *base_alg)
  265. {
  266. struct ccp_crypto_ahash_alg *ccp_alg;
  267. struct ahash_alg *alg;
  268. struct hash_alg_common *halg;
  269. struct crypto_alg *base;
  270. int ret;
  271. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  272. if (!ccp_alg)
  273. return -ENOMEM;
  274. /* Copy the base algorithm and only change what's necessary */
  275. *ccp_alg = *base_alg;
  276. INIT_LIST_HEAD(&ccp_alg->entry);
  277. strncpy(ccp_alg->child_alg, def->name, CRYPTO_MAX_ALG_NAME);
  278. alg = &ccp_alg->alg;
  279. alg->setkey = ccp_sha_setkey;
  280. halg = &alg->halg;
  281. base = &halg->base;
  282. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
  283. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
  284. def->drv_name);
  285. base->cra_init = ccp_hmac_sha_cra_init;
  286. base->cra_exit = ccp_hmac_sha_cra_exit;
  287. ret = crypto_register_ahash(alg);
  288. if (ret) {
  289. pr_err("%s ahash algorithm registration error (%d)\n",
  290. base->cra_name, ret);
  291. kfree(ccp_alg);
  292. return ret;
  293. }
  294. list_add(&ccp_alg->entry, head);
  295. return ret;
  296. }
  297. static int ccp_register_sha_alg(struct list_head *head,
  298. const struct ccp_sha_def *def)
  299. {
  300. struct ccp_crypto_ahash_alg *ccp_alg;
  301. struct ahash_alg *alg;
  302. struct hash_alg_common *halg;
  303. struct crypto_alg *base;
  304. int ret;
  305. ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL);
  306. if (!ccp_alg)
  307. return -ENOMEM;
  308. INIT_LIST_HEAD(&ccp_alg->entry);
  309. ccp_alg->type = def->type;
  310. alg = &ccp_alg->alg;
  311. alg->init = ccp_sha_init;
  312. alg->update = ccp_sha_update;
  313. alg->final = ccp_sha_final;
  314. alg->finup = ccp_sha_finup;
  315. alg->digest = ccp_sha_digest;
  316. halg = &alg->halg;
  317. halg->digestsize = def->digest_size;
  318. base = &halg->base;
  319. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  320. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  321. def->drv_name);
  322. base->cra_flags = CRYPTO_ALG_TYPE_AHASH | CRYPTO_ALG_ASYNC |
  323. CRYPTO_ALG_KERN_DRIVER_ONLY |
  324. CRYPTO_ALG_NEED_FALLBACK;
  325. base->cra_blocksize = def->block_size;
  326. base->cra_ctxsize = sizeof(struct ccp_ctx);
  327. base->cra_priority = CCP_CRA_PRIORITY;
  328. base->cra_type = &crypto_ahash_type;
  329. base->cra_init = ccp_sha_cra_init;
  330. base->cra_exit = ccp_sha_cra_exit;
  331. base->cra_module = THIS_MODULE;
  332. ret = crypto_register_ahash(alg);
  333. if (ret) {
  334. pr_err("%s ahash algorithm registration error (%d)\n",
  335. base->cra_name, ret);
  336. kfree(ccp_alg);
  337. return ret;
  338. }
  339. list_add(&ccp_alg->entry, head);
  340. ret = ccp_register_hmac_alg(head, def, ccp_alg);
  341. return ret;
  342. }
  343. int ccp_register_sha_algs(struct list_head *head)
  344. {
  345. int i, ret;
  346. for (i = 0; i < ARRAY_SIZE(sha_algs); i++) {
  347. ret = ccp_register_sha_alg(head, &sha_algs[i]);
  348. if (ret)
  349. return ret;
  350. }
  351. return 0;
  352. }