sha.c 15 KB

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