Linux 4.2.1
[linux/fpc-iii.git] / drivers / crypto / qce / sha.c
blob5c5df1d17f90036d95b90247fe9e0c4d164968c9
1 /*
2 * Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
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.
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.
14 #include <linux/device.h>
15 #include <linux/interrupt.h>
16 #include <crypto/internal/hash.h>
18 #include "common.h"
19 #include "core.h"
20 #include "sha.h"
22 /* crypto hw padding constant for first operation */
23 #define SHA_PADDING 64
24 #define SHA_PADDING_MASK (SHA_PADDING - 1)
26 static LIST_HEAD(ahash_algs);
28 static const u32 std_iv_sha1[SHA256_DIGEST_SIZE / sizeof(u32)] = {
29 SHA1_H0, SHA1_H1, SHA1_H2, SHA1_H3, SHA1_H4, 0, 0, 0
32 static const u32 std_iv_sha256[SHA256_DIGEST_SIZE / sizeof(u32)] = {
33 SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
34 SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7
37 static void qce_ahash_done(void *data)
39 struct crypto_async_request *async_req = data;
40 struct ahash_request *req = ahash_request_cast(async_req);
41 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
42 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
43 struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
44 struct qce_device *qce = tmpl->qce;
45 struct qce_result_dump *result = qce->dma.result_buf;
46 unsigned int digestsize = crypto_ahash_digestsize(ahash);
47 int error;
48 u32 status;
50 error = qce_dma_terminate_all(&qce->dma);
51 if (error)
52 dev_dbg(qce->dev, "ahash dma termination error (%d)\n", error);
54 qce_unmapsg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE,
55 rctx->src_chained);
56 qce_unmapsg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE, 0);
58 memcpy(rctx->digest, result->auth_iv, digestsize);
59 if (req->result)
60 memcpy(req->result, result->auth_iv, digestsize);
62 rctx->byte_count[0] = cpu_to_be32(result->auth_byte_count[0]);
63 rctx->byte_count[1] = cpu_to_be32(result->auth_byte_count[1]);
65 error = qce_check_status(qce, &status);
66 if (error < 0)
67 dev_dbg(qce->dev, "ahash operation error (%x)\n", status);
69 req->src = rctx->src_orig;
70 req->nbytes = rctx->nbytes_orig;
71 rctx->last_blk = false;
72 rctx->first_blk = false;
74 qce->async_req_done(tmpl->qce, error);
77 static int qce_ahash_async_req_handle(struct crypto_async_request *async_req)
79 struct ahash_request *req = ahash_request_cast(async_req);
80 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
81 struct qce_sha_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
82 struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
83 struct qce_device *qce = tmpl->qce;
84 unsigned long flags = rctx->flags;
85 int ret;
87 if (IS_SHA_HMAC(flags)) {
88 rctx->authkey = ctx->authkey;
89 rctx->authklen = QCE_SHA_HMAC_KEY_SIZE;
90 } else if (IS_CMAC(flags)) {
91 rctx->authkey = ctx->authkey;
92 rctx->authklen = AES_KEYSIZE_128;
95 rctx->src_nents = qce_countsg(req->src, req->nbytes,
96 &rctx->src_chained);
97 ret = qce_mapsg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE,
98 rctx->src_chained);
99 if (ret < 0)
100 return ret;
102 sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ);
104 ret = qce_mapsg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE, 0);
105 if (ret < 0)
106 goto error_unmap_src;
108 ret = qce_dma_prep_sgs(&qce->dma, req->src, rctx->src_nents,
109 &rctx->result_sg, 1, qce_ahash_done, async_req);
110 if (ret)
111 goto error_unmap_dst;
113 qce_dma_issue_pending(&qce->dma);
115 ret = qce_start(async_req, tmpl->crypto_alg_type, 0, 0);
116 if (ret)
117 goto error_terminate;
119 return 0;
121 error_terminate:
122 qce_dma_terminate_all(&qce->dma);
123 error_unmap_dst:
124 qce_unmapsg(qce->dev, &rctx->result_sg, 1, DMA_FROM_DEVICE, 0);
125 error_unmap_src:
126 qce_unmapsg(qce->dev, req->src, rctx->src_nents, DMA_TO_DEVICE,
127 rctx->src_chained);
128 return ret;
131 static int qce_ahash_init(struct ahash_request *req)
133 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
134 struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
135 const u32 *std_iv = tmpl->std_iv;
137 memset(rctx, 0, sizeof(*rctx));
138 rctx->first_blk = true;
139 rctx->last_blk = false;
140 rctx->flags = tmpl->alg_flags;
141 memcpy(rctx->digest, std_iv, sizeof(rctx->digest));
143 return 0;
146 static int qce_ahash_export(struct ahash_request *req, void *out)
148 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
149 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
150 unsigned long flags = rctx->flags;
151 unsigned int digestsize = crypto_ahash_digestsize(ahash);
152 unsigned int blocksize =
153 crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
155 if (IS_SHA1(flags) || IS_SHA1_HMAC(flags)) {
156 struct sha1_state *out_state = out;
158 out_state->count = rctx->count;
159 qce_cpu_to_be32p_array((__be32 *)out_state->state,
160 rctx->digest, digestsize);
161 memcpy(out_state->buffer, rctx->buf, blocksize);
162 } else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags)) {
163 struct sha256_state *out_state = out;
165 out_state->count = rctx->count;
166 qce_cpu_to_be32p_array((__be32 *)out_state->state,
167 rctx->digest, digestsize);
168 memcpy(out_state->buf, rctx->buf, blocksize);
169 } else {
170 return -EINVAL;
173 return 0;
176 static int qce_import_common(struct ahash_request *req, u64 in_count,
177 const u32 *state, const u8 *buffer, bool hmac)
179 struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
180 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
181 unsigned int digestsize = crypto_ahash_digestsize(ahash);
182 unsigned int blocksize;
183 u64 count = in_count;
185 blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(ahash));
186 rctx->count = in_count;
187 memcpy(rctx->buf, buffer, blocksize);
189 if (in_count <= blocksize) {
190 rctx->first_blk = 1;
191 } else {
192 rctx->first_blk = 0;
194 * For HMAC, there is a hardware padding done when first block
195 * is set. Therefore the byte_count must be incremened by 64
196 * after the first block operation.
198 if (hmac)
199 count += SHA_PADDING;
202 rctx->byte_count[0] = (__force __be32)(count & ~SHA_PADDING_MASK);
203 rctx->byte_count[1] = (__force __be32)(count >> 32);
204 qce_cpu_to_be32p_array((__be32 *)rctx->digest, (const u8 *)state,
205 digestsize);
206 rctx->buflen = (unsigned int)(in_count & (blocksize - 1));
208 return 0;
211 static int qce_ahash_import(struct ahash_request *req, const void *in)
213 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
214 unsigned long flags = rctx->flags;
215 bool hmac = IS_SHA_HMAC(flags);
216 int ret = -EINVAL;
218 if (IS_SHA1(flags) || IS_SHA1_HMAC(flags)) {
219 const struct sha1_state *state = in;
221 ret = qce_import_common(req, state->count, state->state,
222 state->buffer, hmac);
223 } else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags)) {
224 const struct sha256_state *state = in;
226 ret = qce_import_common(req, state->count, state->state,
227 state->buf, hmac);
230 return ret;
233 static int qce_ahash_update(struct ahash_request *req)
235 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
236 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
237 struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
238 struct qce_device *qce = tmpl->qce;
239 struct scatterlist *sg_last, *sg;
240 unsigned int total, len;
241 unsigned int hash_later;
242 unsigned int nbytes;
243 unsigned int blocksize;
245 blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
246 rctx->count += req->nbytes;
248 /* check for buffer from previous updates and append it */
249 total = req->nbytes + rctx->buflen;
251 if (total <= blocksize) {
252 scatterwalk_map_and_copy(rctx->buf + rctx->buflen, req->src,
253 0, req->nbytes, 0);
254 rctx->buflen += req->nbytes;
255 return 0;
258 /* save the original req structure fields */
259 rctx->src_orig = req->src;
260 rctx->nbytes_orig = req->nbytes;
263 * if we have data from previous update copy them on buffer. The old
264 * data will be combined with current request bytes.
266 if (rctx->buflen)
267 memcpy(rctx->tmpbuf, rctx->buf, rctx->buflen);
269 /* calculate how many bytes will be hashed later */
270 hash_later = total % blocksize;
271 if (hash_later) {
272 unsigned int src_offset = req->nbytes - hash_later;
273 scatterwalk_map_and_copy(rctx->buf, req->src, src_offset,
274 hash_later, 0);
277 /* here nbytes is multiple of blocksize */
278 nbytes = total - hash_later;
280 len = rctx->buflen;
281 sg = sg_last = req->src;
283 while (len < nbytes && sg) {
284 if (len + sg_dma_len(sg) > nbytes)
285 break;
286 len += sg_dma_len(sg);
287 sg_last = sg;
288 sg = sg_next(sg);
291 if (!sg_last)
292 return -EINVAL;
294 sg_mark_end(sg_last);
296 if (rctx->buflen) {
297 sg_init_table(rctx->sg, 2);
298 sg_set_buf(rctx->sg, rctx->tmpbuf, rctx->buflen);
299 scatterwalk_sg_chain(rctx->sg, 2, req->src);
300 req->src = rctx->sg;
303 req->nbytes = nbytes;
304 rctx->buflen = hash_later;
306 return qce->async_req_enqueue(tmpl->qce, &req->base);
309 static int qce_ahash_final(struct ahash_request *req)
311 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
312 struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
313 struct qce_device *qce = tmpl->qce;
315 if (!rctx->buflen)
316 return 0;
318 rctx->last_blk = true;
320 rctx->src_orig = req->src;
321 rctx->nbytes_orig = req->nbytes;
323 memcpy(rctx->tmpbuf, rctx->buf, rctx->buflen);
324 sg_init_one(rctx->sg, rctx->tmpbuf, rctx->buflen);
326 req->src = rctx->sg;
327 req->nbytes = rctx->buflen;
329 return qce->async_req_enqueue(tmpl->qce, &req->base);
332 static int qce_ahash_digest(struct ahash_request *req)
334 struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
335 struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
336 struct qce_device *qce = tmpl->qce;
337 int ret;
339 ret = qce_ahash_init(req);
340 if (ret)
341 return ret;
343 rctx->src_orig = req->src;
344 rctx->nbytes_orig = req->nbytes;
345 rctx->first_blk = true;
346 rctx->last_blk = true;
348 return qce->async_req_enqueue(tmpl->qce, &req->base);
351 struct qce_ahash_result {
352 struct completion completion;
353 int error;
356 static void qce_digest_complete(struct crypto_async_request *req, int error)
358 struct qce_ahash_result *result = req->data;
360 if (error == -EINPROGRESS)
361 return;
363 result->error = error;
364 complete(&result->completion);
367 static int qce_ahash_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
368 unsigned int keylen)
370 unsigned int digestsize = crypto_ahash_digestsize(tfm);
371 struct qce_sha_ctx *ctx = crypto_tfm_ctx(&tfm->base);
372 struct qce_ahash_result result;
373 struct ahash_request *req;
374 struct scatterlist sg;
375 unsigned int blocksize;
376 struct crypto_ahash *ahash_tfm;
377 u8 *buf;
378 int ret;
379 const char *alg_name;
381 blocksize = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
382 memset(ctx->authkey, 0, sizeof(ctx->authkey));
384 if (keylen <= blocksize) {
385 memcpy(ctx->authkey, key, keylen);
386 return 0;
389 if (digestsize == SHA1_DIGEST_SIZE)
390 alg_name = "sha1-qce";
391 else if (digestsize == SHA256_DIGEST_SIZE)
392 alg_name = "sha256-qce";
393 else
394 return -EINVAL;
396 ahash_tfm = crypto_alloc_ahash(alg_name, CRYPTO_ALG_TYPE_AHASH,
397 CRYPTO_ALG_TYPE_AHASH_MASK);
398 if (IS_ERR(ahash_tfm))
399 return PTR_ERR(ahash_tfm);
401 req = ahash_request_alloc(ahash_tfm, GFP_KERNEL);
402 if (!req) {
403 ret = -ENOMEM;
404 goto err_free_ahash;
407 init_completion(&result.completion);
408 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
409 qce_digest_complete, &result);
410 crypto_ahash_clear_flags(ahash_tfm, ~0);
412 buf = kzalloc(keylen + QCE_MAX_ALIGN_SIZE, GFP_KERNEL);
413 if (!buf) {
414 ret = -ENOMEM;
415 goto err_free_req;
418 memcpy(buf, key, keylen);
419 sg_init_one(&sg, buf, keylen);
420 ahash_request_set_crypt(req, &sg, ctx->authkey, keylen);
422 ret = crypto_ahash_digest(req);
423 if (ret == -EINPROGRESS || ret == -EBUSY) {
424 ret = wait_for_completion_interruptible(&result.completion);
425 if (!ret)
426 ret = result.error;
429 if (ret)
430 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
432 kfree(buf);
433 err_free_req:
434 ahash_request_free(req);
435 err_free_ahash:
436 crypto_free_ahash(ahash_tfm);
437 return ret;
440 static int qce_ahash_cra_init(struct crypto_tfm *tfm)
442 struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
443 struct qce_sha_ctx *ctx = crypto_tfm_ctx(tfm);
445 crypto_ahash_set_reqsize(ahash, sizeof(struct qce_sha_reqctx));
446 memset(ctx, 0, sizeof(*ctx));
447 return 0;
450 struct qce_ahash_def {
451 unsigned long flags;
452 const char *name;
453 const char *drv_name;
454 unsigned int digestsize;
455 unsigned int blocksize;
456 unsigned int statesize;
457 const u32 *std_iv;
460 static const struct qce_ahash_def ahash_def[] = {
462 .flags = QCE_HASH_SHA1,
463 .name = "sha1",
464 .drv_name = "sha1-qce",
465 .digestsize = SHA1_DIGEST_SIZE,
466 .blocksize = SHA1_BLOCK_SIZE,
467 .statesize = sizeof(struct sha1_state),
468 .std_iv = std_iv_sha1,
471 .flags = QCE_HASH_SHA256,
472 .name = "sha256",
473 .drv_name = "sha256-qce",
474 .digestsize = SHA256_DIGEST_SIZE,
475 .blocksize = SHA256_BLOCK_SIZE,
476 .statesize = sizeof(struct sha256_state),
477 .std_iv = std_iv_sha256,
480 .flags = QCE_HASH_SHA1_HMAC,
481 .name = "hmac(sha1)",
482 .drv_name = "hmac-sha1-qce",
483 .digestsize = SHA1_DIGEST_SIZE,
484 .blocksize = SHA1_BLOCK_SIZE,
485 .statesize = sizeof(struct sha1_state),
486 .std_iv = std_iv_sha1,
489 .flags = QCE_HASH_SHA256_HMAC,
490 .name = "hmac(sha256)",
491 .drv_name = "hmac-sha256-qce",
492 .digestsize = SHA256_DIGEST_SIZE,
493 .blocksize = SHA256_BLOCK_SIZE,
494 .statesize = sizeof(struct sha256_state),
495 .std_iv = std_iv_sha256,
499 static int qce_ahash_register_one(const struct qce_ahash_def *def,
500 struct qce_device *qce)
502 struct qce_alg_template *tmpl;
503 struct ahash_alg *alg;
504 struct crypto_alg *base;
505 int ret;
507 tmpl = kzalloc(sizeof(*tmpl), GFP_KERNEL);
508 if (!tmpl)
509 return -ENOMEM;
511 tmpl->std_iv = def->std_iv;
513 alg = &tmpl->alg.ahash;
514 alg->init = qce_ahash_init;
515 alg->update = qce_ahash_update;
516 alg->final = qce_ahash_final;
517 alg->digest = qce_ahash_digest;
518 alg->export = qce_ahash_export;
519 alg->import = qce_ahash_import;
520 if (IS_SHA_HMAC(def->flags))
521 alg->setkey = qce_ahash_hmac_setkey;
522 alg->halg.digestsize = def->digestsize;
523 alg->halg.statesize = def->statesize;
525 base = &alg->halg.base;
526 base->cra_blocksize = def->blocksize;
527 base->cra_priority = 300;
528 base->cra_flags = CRYPTO_ALG_ASYNC;
529 base->cra_ctxsize = sizeof(struct qce_sha_ctx);
530 base->cra_alignmask = 0;
531 base->cra_module = THIS_MODULE;
532 base->cra_init = qce_ahash_cra_init;
533 INIT_LIST_HEAD(&base->cra_list);
535 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
536 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
537 def->drv_name);
539 INIT_LIST_HEAD(&tmpl->entry);
540 tmpl->crypto_alg_type = CRYPTO_ALG_TYPE_AHASH;
541 tmpl->alg_flags = def->flags;
542 tmpl->qce = qce;
544 ret = crypto_register_ahash(alg);
545 if (ret) {
546 kfree(tmpl);
547 dev_err(qce->dev, "%s registration failed\n", base->cra_name);
548 return ret;
551 list_add_tail(&tmpl->entry, &ahash_algs);
552 dev_dbg(qce->dev, "%s is registered\n", base->cra_name);
553 return 0;
556 static void qce_ahash_unregister(struct qce_device *qce)
558 struct qce_alg_template *tmpl, *n;
560 list_for_each_entry_safe(tmpl, n, &ahash_algs, entry) {
561 crypto_unregister_ahash(&tmpl->alg.ahash);
562 list_del(&tmpl->entry);
563 kfree(tmpl);
567 static int qce_ahash_register(struct qce_device *qce)
569 int ret, i;
571 for (i = 0; i < ARRAY_SIZE(ahash_def); i++) {
572 ret = qce_ahash_register_one(&ahash_def[i], qce);
573 if (ret)
574 goto err;
577 return 0;
578 err:
579 qce_ahash_unregister(qce);
580 return ret;
583 const struct qce_algo_ops ahash_ops = {
584 .type = CRYPTO_ALG_TYPE_AHASH,
585 .register_algs = qce_ahash_register,
586 .unregister_algs = qce_ahash_unregister,
587 .async_req_handle = qce_ahash_async_req_handle,