sha.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/interrupt.h>
  15. #include <crypto/internal/hash.h>
  16. #include "common.h"
  17. #include "core.h"
  18. #include "sha.h"
  19. /* crypto hw padding constant for first operation */
  20. #define SHA_PADDING 64
  21. #define SHA_PADDING_MASK (SHA_PADDING - 1)
  22. static LIST_HEAD(ahash_algs);
  23. static const u32 std_iv_sha1[SHA256_DIGEST_SIZE / sizeof(u32)] = {
  24. SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4, 0, 0, 0
  25. };
  26. static const u32 std_iv_sha256[SHA256_DIGEST_SIZE / sizeof(u32)] = {
  27. SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
  28. SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7
  29. };
  30. static void qce_ahash_done(void *data)
  31. {
  32. struct crypto_async_request *async_req = data;
  33. struct ahash_request *req = ahash_request_cast(async_req);
  34. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  35. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  36. struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
  37. struct qce_device *qce = tmpl->qce;
  38. struct qce_result_dump *result = qce->dma.result_buf;
  39. unsigned int digestsize = crypto_ahash_digestsize(ahash);
  40. int error;
  41. u32 status;
  42. error = qce_dma_terminate_all(&qce->dma);
  43. if (error)
  44. dev_dbg(qce->dev, "ahash dma termination error (%d)\n", error);
  45. dma_unmap_sg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
  46. dma_unmap_sg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE);
  47. memcpy(rctx->digest, result->auth_iv, digestsize);
  48. if (req->result)
  49. memcpy(req->result, result->auth_iv, digestsize);
  50. rctx->byte_count[0] = cpu_to_be32(result->auth_byte_count[0]);
  51. rctx->byte_count[1] = cpu_to_be32(result->auth_byte_count[1]);
  52. error = qce_check_status(qce, &status);
  53. if (error < 0)
  54. dev_dbg(qce->dev, "ahash operation error (%x)\n", status);
  55. req->src = rctx->src_orig;
  56. req->nbytes = rctx->nbytes_orig;
  57. rctx->last_blk = false;
  58. rctx->first_blk = false;
  59. qce->async_req_done(tmpl->qce, error);
  60. }
  61. static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
  62. {
  63. struct ahash_request *req = ahash_request_cast(async_req);
  64. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  65. struct qce_sha_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
  66. struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
  67. struct qce_device *qce = tmpl->qce;
  68. unsigned long flags = rctx->flags;
  69. int ret;
  70. if (IS_SHA_HMAC(flags)) {
  71. rctx->authkey = ctx->authkey;
  72. rctx->authklen = QCE_SHA_HMAC_KEY_SIZE;
  73. } else if (IS_CMAC(flags)) {
  74. rctx->authkey = ctx->authkey;
  75. rctx->authklen = AES_KEYSIZE_128;
  76. }
  77. rctx->src_nents = sg_nents_for_len(req->src, req->nbytes);
  78. ret = dma_map_sg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
  79. if (ret < 0)
  80. return ret;
  81. sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
  82. ret = dma_map_sg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE);
  83. if (ret < 0)
  84. goto error_unmap_src;
  85. ret = qce_dma_prep_sgs(&qce->dma, req->src, rctx->src_nents,
  86. &rctx->result_sg, 1, qce_ahash_done, async_req);
  87. if (ret)
  88. goto error_unmap_dst;
  89. qce_dma_issue_pending(&qce->dma);
  90. ret = qce_start(async_req, tmpl->crypto_alg_type, 0, 0);
  91. if (ret)
  92. goto error_terminate;
  93. return 0;
  94. error_terminate:
  95. qce_dma_terminate_all(&qce->dma);
  96. error_unmap_dst:
  97. dma_unmap_sg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE);
  98. error_unmap_src:
  99. dma_unmap_sg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE);
  100. return ret;
  101. }
  102. static int qce_ahash_init(struct ahash_request *req)
  103. {
  104. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  105. struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
  106. const u32 *std_iv = tmpl->std_iv;
  107. memset(rctx, 0, sizeof(*rctx));
  108. rctx->first_blk = true;
  109. rctx->last_blk = false;
  110. rctx->flags = tmpl->alg_flags;
  111. memcpy(rctx->digest, std_iv, sizeof(rctx->digest));
  112. return 0;
  113. }
  114. static int qce_ahash_export(struct ahash_request *req, void *out)
  115. {
  116. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  117. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  118. unsigned long flags = rctx->flags;
  119. unsigned int digestsize = crypto_ahash_digestsize(ahash);
  120. unsigned int blocksize =
  121. crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
  122. if (IS_SHA1(flags) || IS_SHA1_HMAC(flags)) {
  123. struct sha1_state *out_state = out;
  124. out_state->count = rctx->count;
  125. qce_cpu_to_be32p_array((__be32 *)out_state->state,
  126. rctx->digest, digestsize);
  127. memcpy(out_state->buffer, rctx->buf, blocksize);
  128. } else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags)) {
  129. struct sha256_state *out_state = out;
  130. out_state->count = rctx->count;
  131. qce_cpu_to_be32p_array((__be32 *)out_state->state,
  132. rctx->digest, digestsize);
  133. memcpy(out_state->buf, rctx->buf, blocksize);
  134. } else {
  135. return -EINVAL;
  136. }
  137. return 0;
  138. }
  139. static int qce_import_common(struct ahash_request *req, u64 in_count,
  140. const u32 *state, const u8 *buffer, bool hmac)
  141. {
  142. struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
  143. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  144. unsigned int digestsize = crypto_ahash_digestsize(ahash);
  145. unsigned int blocksize;
  146. u64 count = in_count;
  147. blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
  148. rctx->count = in_count;
  149. memcpy(rctx->buf, buffer, blocksize);
  150. if (in_count <= blocksize) {
  151. rctx->first_blk = 1;
  152. } else {
  153. rctx->first_blk = 0;
  154. /*
  155. * For HMAC, there is a hardware padding done when first block
  156. * is set. Therefore the byte_count must be incremened by 64
  157. * after the first block operation.
  158. */
  159. if (hmac)
  160. count += SHA_PADDING;
  161. }
  162. rctx->byte_count[0] = (__force __be32)(count & ~SHA_PADDING_MASK);
  163. rctx->byte_count[1] = (__force __be32)(count >> 32);
  164. qce_cpu_to_be32p_array((__be32 *)rctx->digest, (const u8 *)state,
  165. digestsize);
  166. rctx->buflen = (unsigned int)(in_count & (blocksize - 1));
  167. return 0;
  168. }
  169. static int qce_ahash_import(struct ahash_request *req, const void *in)
  170. {
  171. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  172. unsigned long flags = rctx->flags;
  173. bool hmac = IS_SHA_HMAC(flags);
  174. int ret = -EINVAL;
  175. if (IS_SHA1(flags) || IS_SHA1_HMAC(flags)) {
  176. const struct sha1_state *state = in;
  177. ret = qce_import_common(req, state->count, state->state,
  178. state->buffer, hmac);
  179. } else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags)) {
  180. const struct sha256_state *state = in;
  181. ret = qce_import_common(req, state->count, state->state,
  182. state->buf, hmac);
  183. }
  184. return ret;
  185. }
  186. static int qce_ahash_update(struct ahash_request *req)
  187. {
  188. struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  189. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  190. struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
  191. struct qce_device *qce = tmpl->qce;
  192. struct scatterlist *sg_last, *sg;
  193. unsigned int total, len;
  194. unsigned int hash_later;
  195. unsigned int nbytes;
  196. unsigned int blocksize;
  197. blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  198. rctx->count += req->nbytes;
  199. /* check for buffer from previous updates and append it */
  200. total = req->nbytes + rctx->buflen;
  201. if (total <= blocksize) {
  202. scatterwalk_map_and_copy(rctx->buf + rctx->buflen, req->src,
  203. 0, req->nbytes, 0);
  204. rctx->buflen += req->nbytes;
  205. return 0;
  206. }
  207. /* save the original req structure fields */
  208. rctx->src_orig = req->src;
  209. rctx->nbytes_orig = req->nbytes;
  210. /*
  211. * if we have data from previous update copy them on buffer. The old
  212. * data will be combined with current request bytes.
  213. */
  214. if (rctx->buflen)
  215. memcpy(rctx->tmpbuf, rctx->buf, rctx->buflen);
  216. /* calculate how many bytes will be hashed later */
  217. hash_later = total % blocksize;
  218. if (hash_later) {
  219. unsigned int src_offset = req->nbytes - hash_later;
  220. scatterwalk_map_and_copy(rctx->buf, req->src, src_offset,
  221. hash_later, 0);
  222. }
  223. /* here nbytes is multiple of blocksize */
  224. nbytes = total - hash_later;
  225. len = rctx->buflen;
  226. sg = sg_last = req->src;
  227. while (len < nbytes && sg) {
  228. if (len + sg_dma_len(sg) > nbytes)
  229. break;
  230. len += sg_dma_len(sg);
  231. sg_last = sg;
  232. sg = sg_next(sg);
  233. }
  234. if (!sg_last)
  235. return -EINVAL;
  236. sg_mark_end(sg_last);
  237. if (rctx->buflen) {
  238. sg_init_table(rctx->sg, 2);
  239. sg_set_buf(rctx->sg, rctx->tmpbuf, rctx->buflen);
  240. sg_chain(rctx->sg, 2, req->src);
  241. req->src = rctx->sg;
  242. }
  243. req->nbytes = nbytes;
  244. rctx->buflen = hash_later;
  245. return qce->async_req_enqueue(tmpl->qce, &req->base);
  246. }
  247. static int qce_ahash_final(struct ahash_request *req)
  248. {
  249. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  250. struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
  251. struct qce_device *qce = tmpl->qce;
  252. if (!rctx->buflen)
  253. return 0;
  254. rctx->last_blk = true;
  255. rctx->src_orig = req->src;
  256. rctx->nbytes_orig = req->nbytes;
  257. memcpy(rctx->tmpbuf, rctx->buf, rctx->buflen);
  258. sg_init_one(rctx->sg, rctx->tmpbuf, rctx->buflen);
  259. req->src = rctx->sg;
  260. req->nbytes = rctx->buflen;
  261. return qce->async_req_enqueue(tmpl->qce, &req->base);
  262. }
  263. static int qce_ahash_digest(struct ahash_request *req)
  264. {
  265. struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
  266. struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
  267. struct qce_device *qce = tmpl->qce;
  268. int ret;
  269. ret = qce_ahash_init(req);
  270. if (ret)
  271. return ret;
  272. rctx->src_orig = req->src;
  273. rctx->nbytes_orig = req->nbytes;
  274. rctx->first_blk = true;
  275. rctx->last_blk = true;
  276. return qce->async_req_enqueue(tmpl->qce, &req->base);
  277. }
  278. struct qce_ahash_result {
  279. struct completion completion;
  280. int error;
  281. };
  282. static void qce_digest_complete(struct crypto_async_request *req, int error)
  283. {
  284. struct qce_ahash_result *result = req->data;
  285. if (error == -EINPROGRESS)
  286. return;
  287. result->error = error;
  288. complete(&result->completion);
  289. }
  290. static int qce_ahash_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
  291. unsigned int keylen)
  292. {
  293. unsigned int digestsize = crypto_ahash_digestsize(tfm);
  294. struct qce_sha_ctx *ctx = crypto_tfm_ctx(&tfm->base);
  295. struct qce_ahash_result result;
  296. struct ahash_request *req;
  297. struct scatterlist sg;
  298. unsigned int blocksize;
  299. struct crypto_ahash *ahash_tfm;
  300. u8 *buf;
  301. int ret;
  302. const char *alg_name;
  303. blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
  304. memset(ctx->authkey, 0, sizeof(ctx->authkey));
  305. if (keylen <= blocksize) {
  306. memcpy(ctx->authkey, key, keylen);
  307. return 0;
  308. }
  309. if (digestsize == SHA1_DIGEST_SIZE)
  310. alg_name = "sha1-qce";
  311. else if (digestsize == SHA256_DIGEST_SIZE)
  312. alg_name = "sha256-qce";
  313. else
  314. return -EINVAL;
  315. ahash_tfm = crypto_alloc_ahash(alg_name, CRYPTO_ALG_TYPE_AHASH,
  316. CRYPTO_ALG_TYPE_AHASH_MASK);
  317. if (IS_ERR(ahash_tfm))
  318. return PTR_ERR(ahash_tfm);
  319. req = ahash_request_alloc(ahash_tfm, GFP_KERNEL);
  320. if (!req) {
  321. ret = -ENOMEM;
  322. goto err_free_ahash;
  323. }
  324. init_completion(&result.completion);
  325. ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  326. qce_digest_complete, &result);
  327. crypto_ahash_clear_flags(ahash_tfm, ~0);
  328. buf = kzalloc(keylen + QCE_MAX_ALIGN_SIZE, GFP_KERNEL);
  329. if (!buf) {
  330. ret = -ENOMEM;
  331. goto err_free_req;
  332. }
  333. memcpy(buf, key, keylen);
  334. sg_init_one(&sg, buf, keylen);
  335. ahash_request_set_crypt(req, &sg, ctx->authkey, keylen);
  336. ret = crypto_ahash_digest(req);
  337. if (ret == -EINPROGRESS || ret == -EBUSY) {
  338. ret = wait_for_completion_interruptible(&result.completion);
  339. if (!ret)
  340. ret = result.error;
  341. }
  342. if (ret)
  343. crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  344. kfree(buf);
  345. err_free_req:
  346. ahash_request_free(req);
  347. err_free_ahash:
  348. crypto_free_ahash(ahash_tfm);
  349. return ret;
  350. }
  351. static int qce_ahash_cra_init(struct crypto_tfm *tfm)
  352. {
  353. struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
  354. struct qce_sha_ctx *ctx = crypto_tfm_ctx(tfm);
  355. crypto_ahash_set_reqsize(ahash, sizeof(struct qce_sha_reqctx));
  356. memset(ctx, 0, sizeof(*ctx));
  357. return 0;
  358. }
  359. struct qce_ahash_def {
  360. unsigned long flags;
  361. const char *name;
  362. const char *drv_name;
  363. unsigned int digestsize;
  364. unsigned int blocksize;
  365. unsigned int statesize;
  366. const u32 *std_iv;
  367. };
  368. static const struct qce_ahash_def ahash_def[] = {
  369. {
  370. .flags = QCE_HASH_SHA1,
  371. .name = "sha1",
  372. .drv_name = "sha1-qce",
  373. .digestsize = SHA1_DIGEST_SIZE,
  374. .blocksize = SHA1_BLOCK_SIZE,
  375. .statesize = sizeof(struct sha1_state),
  376. .std_iv = std_iv_sha1,
  377. },
  378. {
  379. .flags = QCE_HASH_SHA256,
  380. .name = "sha256",
  381. .drv_name = "sha256-qce",
  382. .digestsize = SHA256_DIGEST_SIZE,
  383. .blocksize = SHA256_BLOCK_SIZE,
  384. .statesize = sizeof(struct sha256_state),
  385. .std_iv = std_iv_sha256,
  386. },
  387. {
  388. .flags = QCE_HASH_SHA1_HMAC,
  389. .name = "hmac(sha1)",
  390. .drv_name = "hmac-sha1-qce",
  391. .digestsize = SHA1_DIGEST_SIZE,
  392. .blocksize = SHA1_BLOCK_SIZE,
  393. .statesize = sizeof(struct sha1_state),
  394. .std_iv = std_iv_sha1,
  395. },
  396. {
  397. .flags = QCE_HASH_SHA256_HMAC,
  398. .name = "hmac(sha256)",
  399. .drv_name = "hmac-sha256-qce",
  400. .digestsize = SHA256_DIGEST_SIZE,
  401. .blocksize = SHA256_BLOCK_SIZE,
  402. .statesize = sizeof(struct sha256_state),
  403. .std_iv = std_iv_sha256,
  404. },
  405. };
  406. static int qce_ahash_register_one(const struct qce_ahash_def *def,
  407. struct qce_device *qce)
  408. {
  409. struct qce_alg_template *tmpl;
  410. struct ahash_alg *alg;
  411. struct crypto_alg *base;
  412. int ret;
  413. tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
  414. if (!tmpl)
  415. return -ENOMEM;
  416. tmpl->std_iv = def->std_iv;
  417. alg = &tmpl->alg.ahash;
  418. alg->init = qce_ahash_init;
  419. alg->update = qce_ahash_update;
  420. alg->final = qce_ahash_final;
  421. alg->digest = qce_ahash_digest;
  422. alg->export = qce_ahash_export;
  423. alg->import = qce_ahash_import;
  424. if (IS_SHA_HMAC(def->flags))
  425. alg->setkey = qce_ahash_hmac_setkey;
  426. alg->halg.digestsize = def->digestsize;
  427. alg->halg.statesize = def->statesize;
  428. base = &alg->halg.base;
  429. base->cra_blocksize = def->blocksize;
  430. base->cra_priority = 300;
  431. base->cra_flags = CRYPTO_ALG_ASYNC;
  432. base->cra_ctxsize = sizeof(struct qce_sha_ctx);
  433. base->cra_alignmask = 0;
  434. base->cra_module = THIS_MODULE;
  435. base->cra_init = qce_ahash_cra_init;
  436. INIT_LIST_HEAD(&base->cra_list);
  437. snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
  438. snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
  439. def->drv_name);
  440. INIT_LIST_HEAD(&tmpl->entry);
  441. tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_AHASH;
  442. tmpl->alg_flags = def->flags;
  443. tmpl->qce = qce;
  444. ret = crypto_register_ahash(alg);
  445. if (ret) {
  446. kfree(tmpl);
  447. dev_err(qce->dev, "%s registration failed\n", base->cra_name);
  448. return ret;
  449. }
  450. list_add_tail(&tmpl->entry, &ahash_algs);
  451. dev_dbg(qce->dev, "%s is registered\n", base->cra_name);
  452. return 0;
  453. }
  454. static void qce_ahash_unregister(struct qce_device *qce)
  455. {
  456. struct qce_alg_template *tmpl, *n;
  457. list_for_each_entry_safe(tmpl, n, &ahash_algs, entry) {
  458. crypto_unregister_ahash(&tmpl->alg.ahash);
  459. list_del(&tmpl->entry);
  460. kfree(tmpl);
  461. }
  462. }
  463. static int qce_ahash_register(struct qce_device *qce)
  464. {
  465. int ret, i;
  466. for (i = 0; i < ARRAY_SIZE(ahash_def); i++) {
  467. ret = qce_ahash_register_one(&ahash_def[i], qce);
  468. if (ret)
  469. goto err;
  470. }
  471. return 0;
  472. err:
  473. qce_ahash_unregister(qce);
  474. return ret;
  475. }
  476. const struct qce_algo_ops ahash_ops = {
  477. .type = CRYPTO_ALG_TYPE_AHASH,
  478. .register_algs = qce_ahash_register,
  479. .unregister_algs = qce_ahash_unregister,
  480. .async_req_handle = qce_ahash_async_req_handle,
  481. };