hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/crypto/qce/sha.c
....@@ -1,17 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * 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.
124 */
135
146 #include <linux/device.h>
7
+#include <linux/dma-mapping.h>
158 #include <linux/interrupt.h>
169 #include <crypto/internal/hash.h>
1710
....@@ -211,10 +204,18 @@
211204
212205 static int qce_ahash_import(struct ahash_request *req, const void *in)
213206 {
214
- struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
215
- unsigned long flags = rctx->flags;
216
- bool hmac = IS_SHA_HMAC(flags);
217
- int ret = -EINVAL;
207
+ struct qce_sha_reqctx *rctx;
208
+ unsigned long flags;
209
+ bool hmac;
210
+ int ret;
211
+
212
+ ret = qce_ahash_init(req);
213
+ if (ret)
214
+ return ret;
215
+
216
+ rctx = ahash_request_ctx(req);
217
+ flags = rctx->flags;
218
+ hmac = IS_SHA_HMAC(flags);
218219
219220 if (IS_SHA1(flags) || IS_SHA1_HMAC(flags)) {
220221 const struct sha1_state *state = in;
....@@ -292,8 +293,6 @@
292293 if (!sg_last)
293294 return -EINVAL;
294295
295
- sg_mark_end(sg_last);
296
-
297296 if (rctx->buflen) {
298297 sg_init_table(rctx->sg, 2);
299298 sg_set_buf(rctx->sg, rctx->tmpbuf, rctx->buflen);
....@@ -313,8 +312,12 @@
313312 struct qce_alg_template *tmpl = to_ahash_tmpl(req->base.tfm);
314313 struct qce_device *qce = tmpl->qce;
315314
316
- if (!rctx->buflen)
315
+ if (!rctx->buflen) {
316
+ if (tmpl->hash_zero)
317
+ memcpy(req->result, tmpl->hash_zero,
318
+ tmpl->alg.ahash.halg.digestsize);
317319 return 0;
320
+ }
318321
319322 rctx->last_blk = true;
320323
....@@ -345,6 +348,13 @@
345348 rctx->nbytes_orig = req->nbytes;
346349 rctx->first_blk = true;
347350 rctx->last_blk = true;
351
+
352
+ if (!rctx->nbytes_orig) {
353
+ if (tmpl->hash_zero)
354
+ memcpy(req->result, tmpl->hash_zero,
355
+ tmpl->alg.ahash.halg.digestsize);
356
+ return 0;
357
+ }
348358
349359 return qce->async_req_enqueue(tmpl->qce, &req->base);
350360 }
....@@ -404,8 +414,6 @@
404414 ahash_request_set_crypt(req, &sg, ctx->authkey, keylen);
405415
406416 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
407
- if (ret)
408
- crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
409417
410418 kfree(buf);
411419 err_free_req:
....@@ -500,15 +508,19 @@
500508 alg->halg.digestsize = def->digestsize;
501509 alg->halg.statesize = def->statesize;
502510
511
+ if (IS_SHA1(def->flags))
512
+ tmpl->hash_zero = sha1_zero_message_hash;
513
+ else if (IS_SHA256(def->flags))
514
+ tmpl->hash_zero = sha256_zero_message_hash;
515
+
503516 base = &alg->halg.base;
504517 base->cra_blocksize = def->blocksize;
505518 base->cra_priority = 300;
506
- base->cra_flags = CRYPTO_ALG_ASYNC;
519
+ base->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY;
507520 base->cra_ctxsize = sizeof(struct qce_sha_ctx);
508521 base->cra_alignmask = 0;
509522 base->cra_module = THIS_MODULE;
510523 base->cra_init = qce_ahash_cra_init;
511
- INIT_LIST_HEAD(&base->cra_list);
512524
513525 snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name);
514526 snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",