]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
6ba6c74d AB |
2 | /* |
3 | * sha2-ce-glue.c - SHA-224/SHA-256 using ARMv8 Crypto Extensions | |
4 | * | |
da179331 | 5 | * Copyright (C) 2014 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org> |
6ba6c74d AB |
6 | */ |
7 | ||
8 | #include <asm/neon.h> | |
da179331 | 9 | #include <asm/simd.h> |
6ba6c74d AB |
10 | #include <asm/unaligned.h> |
11 | #include <crypto/internal/hash.h> | |
e52b7023 | 12 | #include <crypto/internal/simd.h> |
a24d22b2 | 13 | #include <crypto/sha2.h> |
03802f6a | 14 | #include <crypto/sha256_base.h> |
6ba6c74d AB |
15 | #include <linux/cpufeature.h> |
16 | #include <linux/crypto.h> | |
17 | #include <linux/module.h> | |
18 | ||
19 | MODULE_DESCRIPTION("SHA-224/SHA-256 secure hash using ARMv8 Crypto Extensions"); | |
20 | MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>"); | |
21 | MODULE_LICENSE("GPL v2"); | |
0df07d81 AB |
22 | MODULE_ALIAS_CRYPTO("sha224"); |
23 | MODULE_ALIAS_CRYPTO("sha256"); | |
6ba6c74d | 24 | |
03802f6a AB |
25 | struct sha256_ce_state { |
26 | struct sha256_state sst; | |
27 | u32 finalize; | |
28 | }; | |
6ba6c74d | 29 | |
0f4128dc HX |
30 | extern const u32 sha256_ce_offsetof_count; |
31 | extern const u32 sha256_ce_offsetof_finalize; | |
32 | ||
b2eadbf4 AB |
33 | asmlinkage int sha2_ce_transform(struct sha256_ce_state *sst, u8 const *src, |
34 | int blocks); | |
6ba6c74d | 35 | |
6320a15e ST |
36 | static void __sha2_ce_transform(struct sha256_state *sst, u8 const *src, |
37 | int blocks) | |
38 | { | |
b2eadbf4 AB |
39 | while (blocks) { |
40 | int rem; | |
41 | ||
42 | kernel_neon_begin(); | |
43 | rem = sha2_ce_transform(container_of(sst, struct sha256_ce_state, | |
44 | sst), src, blocks); | |
45 | kernel_neon_end(); | |
46 | src += (blocks - rem) * SHA256_BLOCK_SIZE; | |
47 | blocks = rem; | |
48 | } | |
6320a15e ST |
49 | } |
50 | ||
f4857f4c AB |
51 | const u32 sha256_ce_offsetof_count = offsetof(struct sha256_ce_state, |
52 | sst.count); | |
53 | const u32 sha256_ce_offsetof_finalize = offsetof(struct sha256_ce_state, | |
54 | finalize); | |
55 | ||
da179331 AB |
56 | asmlinkage void sha256_block_data_order(u32 *digest, u8 const *src, int blocks); |
57 | ||
6320a15e ST |
58 | static void __sha256_block_data_order(struct sha256_state *sst, u8 const *src, |
59 | int blocks) | |
60 | { | |
61 | sha256_block_data_order(sst->state, src, blocks); | |
62 | } | |
63 | ||
03802f6a AB |
64 | static int sha256_ce_update(struct shash_desc *desc, const u8 *data, |
65 | unsigned int len) | |
6ba6c74d | 66 | { |
03802f6a | 67 | struct sha256_ce_state *sctx = shash_desc_ctx(desc); |
6ba6c74d | 68 | |
e52b7023 | 69 | if (!crypto_simd_usable()) |
da179331 | 70 | return sha256_base_do_update(desc, data, len, |
6320a15e | 71 | __sha256_block_data_order); |
da179331 | 72 | |
03802f6a | 73 | sctx->finalize = 0; |
6320a15e | 74 | sha256_base_do_update(desc, data, len, __sha2_ce_transform); |
6ba6c74d | 75 | |
6ba6c74d AB |
76 | return 0; |
77 | } | |
78 | ||
03802f6a AB |
79 | static int sha256_ce_finup(struct shash_desc *desc, const u8 *data, |
80 | unsigned int len, u8 *out) | |
6ba6c74d | 81 | { |
03802f6a | 82 | struct sha256_ce_state *sctx = shash_desc_ctx(desc); |
6bd934de | 83 | bool finalize = !sctx->sst.count && !(len % SHA256_BLOCK_SIZE) && len; |
6ba6c74d | 84 | |
e52b7023 | 85 | if (!crypto_simd_usable()) { |
da179331 AB |
86 | if (len) |
87 | sha256_base_do_update(desc, data, len, | |
6320a15e ST |
88 | __sha256_block_data_order); |
89 | sha256_base_do_finalize(desc, __sha256_block_data_order); | |
da179331 AB |
90 | return sha256_base_finish(desc, out); |
91 | } | |
92 | ||
6ba6c74d | 93 | /* |
03802f6a AB |
94 | * Allow the asm code to perform the finalization if there is no |
95 | * partial data and the input is a round multiple of the block size. | |
6ba6c74d | 96 | */ |
03802f6a | 97 | sctx->finalize = finalize; |
6ba6c74d | 98 | |
6320a15e | 99 | sha256_base_do_update(desc, data, len, __sha2_ce_transform); |
03802f6a | 100 | if (!finalize) |
6320a15e | 101 | sha256_base_do_finalize(desc, __sha2_ce_transform); |
03802f6a | 102 | return sha256_base_finish(desc, out); |
6ba6c74d AB |
103 | } |
104 | ||
03802f6a | 105 | static int sha256_ce_final(struct shash_desc *desc, u8 *out) |
6ba6c74d | 106 | { |
ec59a65d AB |
107 | struct sha256_ce_state *sctx = shash_desc_ctx(desc); |
108 | ||
e52b7023 | 109 | if (!crypto_simd_usable()) { |
6320a15e | 110 | sha256_base_do_finalize(desc, __sha256_block_data_order); |
da179331 AB |
111 | return sha256_base_finish(desc, out); |
112 | } | |
113 | ||
ec59a65d | 114 | sctx->finalize = 0; |
6320a15e | 115 | sha256_base_do_finalize(desc, __sha2_ce_transform); |
03802f6a | 116 | return sha256_base_finish(desc, out); |
6ba6c74d AB |
117 | } |
118 | ||
1efcbf0e EB |
119 | static int sha256_ce_digest(struct shash_desc *desc, const u8 *data, |
120 | unsigned int len, u8 *out) | |
121 | { | |
122 | sha256_base_init(desc); | |
123 | return sha256_ce_finup(desc, data, len, out); | |
124 | } | |
125 | ||
d282eeeb CL |
126 | static int sha256_ce_export(struct shash_desc *desc, void *out) |
127 | { | |
128 | struct sha256_ce_state *sctx = shash_desc_ctx(desc); | |
129 | ||
130 | memcpy(out, &sctx->sst, sizeof(struct sha256_state)); | |
131 | return 0; | |
132 | } | |
133 | ||
134 | static int sha256_ce_import(struct shash_desc *desc, const void *in) | |
135 | { | |
136 | struct sha256_ce_state *sctx = shash_desc_ctx(desc); | |
137 | ||
138 | memcpy(&sctx->sst, in, sizeof(struct sha256_state)); | |
139 | sctx->finalize = 0; | |
140 | return 0; | |
141 | } | |
142 | ||
6ba6c74d | 143 | static struct shash_alg algs[] = { { |
03802f6a AB |
144 | .init = sha224_base_init, |
145 | .update = sha256_ce_update, | |
146 | .final = sha256_ce_final, | |
147 | .finup = sha256_ce_finup, | |
d282eeeb CL |
148 | .export = sha256_ce_export, |
149 | .import = sha256_ce_import, | |
03802f6a | 150 | .descsize = sizeof(struct sha256_ce_state), |
d282eeeb | 151 | .statesize = sizeof(struct sha256_state), |
6ba6c74d | 152 | .digestsize = SHA224_DIGEST_SIZE, |
6ba6c74d AB |
153 | .base = { |
154 | .cra_name = "sha224", | |
155 | .cra_driver_name = "sha224-ce", | |
156 | .cra_priority = 200, | |
6ba6c74d AB |
157 | .cra_blocksize = SHA256_BLOCK_SIZE, |
158 | .cra_module = THIS_MODULE, | |
159 | } | |
160 | }, { | |
03802f6a AB |
161 | .init = sha256_base_init, |
162 | .update = sha256_ce_update, | |
163 | .final = sha256_ce_final, | |
164 | .finup = sha256_ce_finup, | |
1efcbf0e | 165 | .digest = sha256_ce_digest, |
d282eeeb CL |
166 | .export = sha256_ce_export, |
167 | .import = sha256_ce_import, | |
03802f6a | 168 | .descsize = sizeof(struct sha256_ce_state), |
d282eeeb | 169 | .statesize = sizeof(struct sha256_state), |
6ba6c74d | 170 | .digestsize = SHA256_DIGEST_SIZE, |
6ba6c74d AB |
171 | .base = { |
172 | .cra_name = "sha256", | |
173 | .cra_driver_name = "sha256-ce", | |
174 | .cra_priority = 200, | |
6ba6c74d AB |
175 | .cra_blocksize = SHA256_BLOCK_SIZE, |
176 | .cra_module = THIS_MODULE, | |
177 | } | |
178 | } }; | |
179 | ||
180 | static int __init sha2_ce_mod_init(void) | |
181 | { | |
182 | return crypto_register_shashes(algs, ARRAY_SIZE(algs)); | |
183 | } | |
184 | ||
185 | static void __exit sha2_ce_mod_fini(void) | |
186 | { | |
187 | crypto_unregister_shashes(algs, ARRAY_SIZE(algs)); | |
188 | } | |
189 | ||
190 | module_cpu_feature_match(SHA2, sha2_ce_mod_init); | |
191 | module_exit(sha2_ce_mod_fini); |