hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/arch/arm64/crypto/sha1-ce-glue.c
....@@ -1,17 +1,15 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
34 *
45 * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License version 2 as
8
- * published by the Free Software Foundation.
96 */
107
118 #include <asm/neon.h>
129 #include <asm/simd.h>
1310 #include <asm/unaligned.h>
1411 #include <crypto/internal/hash.h>
12
+#include <crypto/internal/simd.h>
1513 #include <crypto/sha.h>
1614 #include <crypto/sha1_base.h>
1715 #include <linux/cpufeature.h>
....@@ -28,16 +26,26 @@
2826 u32 finalize;
2927 };
3028
31
-asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
32
- int blocks);
33
-#ifdef CONFIG_CFI_CLANG
34
-static inline void __cfi_sha1_ce_transform(struct sha1_state *sst,
35
- u8 const *src, int blocks)
29
+extern const u32 sha1_ce_offsetof_count;
30
+extern const u32 sha1_ce_offsetof_finalize;
31
+
32
+asmlinkage int sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
33
+ int blocks);
34
+
35
+static void __sha1_ce_transform(struct sha1_state *sst, u8 const *src,
36
+ int blocks)
3637 {
37
- sha1_ce_transform((struct sha1_ce_state *)sst, src, blocks);
38
+ while (blocks) {
39
+ int rem;
40
+
41
+ kernel_neon_begin();
42
+ rem = sha1_ce_transform(container_of(sst, struct sha1_ce_state,
43
+ sst), src, blocks);
44
+ kernel_neon_end();
45
+ src += (blocks - rem) * SHA1_BLOCK_SIZE;
46
+ blocks = rem;
47
+ }
3848 }
39
-#define sha1_ce_transform __cfi_sha1_ce_transform
40
-#endif
4149
4250 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
4351 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
....@@ -47,14 +55,11 @@
4755 {
4856 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
4957
50
- if (!may_use_simd())
58
+ if (!crypto_simd_usable())
5159 return crypto_sha1_update(desc, data, len);
5260
5361 sctx->finalize = 0;
54
- kernel_neon_begin();
55
- sha1_base_do_update(desc, data, len,
56
- (sha1_block_fn *)sha1_ce_transform);
57
- kernel_neon_end();
62
+ sha1_base_do_update(desc, data, len, __sha1_ce_transform);
5863
5964 return 0;
6065 }
....@@ -65,7 +70,7 @@
6570 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
6671 bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE) && len;
6772
68
- if (!may_use_simd())
73
+ if (!crypto_simd_usable())
6974 return crypto_sha1_finup(desc, data, len, out);
7075
7176 /*
....@@ -74,12 +79,9 @@
7479 */
7580 sctx->finalize = finalize;
7681
77
- kernel_neon_begin();
78
- sha1_base_do_update(desc, data, len,
79
- (sha1_block_fn *)sha1_ce_transform);
82
+ sha1_base_do_update(desc, data, len, __sha1_ce_transform);
8083 if (!finalize)
81
- sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
82
- kernel_neon_end();
84
+ sha1_base_do_finalize(desc, __sha1_ce_transform);
8385 return sha1_base_finish(desc, out);
8486 }
8587
....@@ -87,14 +89,29 @@
8789 {
8890 struct sha1_ce_state *sctx = shash_desc_ctx(desc);
8991
90
- if (!may_use_simd())
92
+ if (!crypto_simd_usable())
9193 return crypto_sha1_finup(desc, NULL, 0, out);
9294
9395 sctx->finalize = 0;
94
- kernel_neon_begin();
95
- sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
96
- kernel_neon_end();
96
+ sha1_base_do_finalize(desc, __sha1_ce_transform);
9797 return sha1_base_finish(desc, out);
98
+}
99
+
100
+static int sha1_ce_export(struct shash_desc *desc, void *out)
101
+{
102
+ struct sha1_ce_state *sctx = shash_desc_ctx(desc);
103
+
104
+ memcpy(out, &sctx->sst, sizeof(struct sha1_state));
105
+ return 0;
106
+}
107
+
108
+static int sha1_ce_import(struct shash_desc *desc, const void *in)
109
+{
110
+ struct sha1_ce_state *sctx = shash_desc_ctx(desc);
111
+
112
+ memcpy(&sctx->sst, in, sizeof(struct sha1_state));
113
+ sctx->finalize = 0;
114
+ return 0;
98115 }
99116
100117 static struct shash_alg alg = {
....@@ -102,7 +119,10 @@
102119 .update = sha1_ce_update,
103120 .final = sha1_ce_final,
104121 .finup = sha1_ce_finup,
122
+ .import = sha1_ce_import,
123
+ .export = sha1_ce_export,
105124 .descsize = sizeof(struct sha1_ce_state),
125
+ .statesize = sizeof(struct sha1_state),
106126 .digestsize = SHA1_DIGEST_SIZE,
107127 .base = {
108128 .cra_name = "sha1",