4 * Glue code for the SHA256 Secure Hash Algorithm assembler
5 * implementation using supplemental SSE3 / AVX / AVX2 instructions.
7 * This file is based on sha256_generic.c
9 * Copyright (C) 2013 Intel Corporation.
12 * Tim Chen <tim.c.chen@linux.intel.com>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <crypto/internal/hash.h>
33 #include <linux/init.h>
34 #include <linux/module.h>
36 #include <linux/cryptohash.h>
37 #include <linux/types.h>
38 #include <crypto/sha.h>
39 #include <crypto/sha256_base.h>
40 #include <asm/fpu/api.h>
41 #include <linux/string.h>
43 asmlinkage void sha256_transform_ssse3(u32 *digest, const char *data,
45 typedef void (sha256_transform_fn)(u32 *digest, const char *data, u64 rounds);
47 static int sha256_update(struct shash_desc *desc, const u8 *data,
48 unsigned int len, sha256_transform_fn *sha256_xform)
50 struct sha256_state *sctx = shash_desc_ctx(desc);
52 if (!irq_fpu_usable() ||
53 (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
54 return crypto_sha256_update(desc, data, len);
56 /* make sure casting to sha256_block_fn() is safe */
57 BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);
60 sha256_base_do_update(desc, data, len,
61 (sha256_block_fn *)sha256_xform);
67 static int sha256_finup(struct shash_desc *desc, const u8 *data,
68 unsigned int len, u8 *out, sha256_transform_fn *sha256_xform)
70 if (!irq_fpu_usable())
71 return crypto_sha256_finup(desc, data, len, out);
75 sha256_base_do_update(desc, data, len,
76 (sha256_block_fn *)sha256_xform);
77 sha256_base_do_finalize(desc, (sha256_block_fn *)sha256_xform);
80 return sha256_base_finish(desc, out);
83 static int sha256_ssse3_update(struct shash_desc *desc, const u8 *data,
86 return sha256_update(desc, data, len, sha256_transform_ssse3);
89 static int sha256_ssse3_finup(struct shash_desc *desc, const u8 *data,
90 unsigned int len, u8 *out)
92 return sha256_finup(desc, data, len, out, sha256_transform_ssse3);
95 /* Add padding and return the message digest. */
96 static int sha256_ssse3_final(struct shash_desc *desc, u8 *out)
98 return sha256_ssse3_finup(desc, NULL, 0, out);
101 static struct shash_alg sha256_ssse3_algs[] = { {
102 .digestsize = SHA256_DIGEST_SIZE,
103 .init = sha256_base_init,
104 .update = sha256_ssse3_update,
105 .final = sha256_ssse3_final,
106 .finup = sha256_ssse3_finup,
107 .descsize = sizeof(struct sha256_state),
109 .cra_name = "sha256",
110 .cra_driver_name = "sha256-ssse3",
112 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
113 .cra_blocksize = SHA256_BLOCK_SIZE,
114 .cra_module = THIS_MODULE,
117 .digestsize = SHA224_DIGEST_SIZE,
118 .init = sha224_base_init,
119 .update = sha256_ssse3_update,
120 .final = sha256_ssse3_final,
121 .finup = sha256_ssse3_finup,
122 .descsize = sizeof(struct sha256_state),
124 .cra_name = "sha224",
125 .cra_driver_name = "sha224-ssse3",
127 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
128 .cra_blocksize = SHA224_BLOCK_SIZE,
129 .cra_module = THIS_MODULE,
133 static int register_sha256_ssse3(void)
135 if (boot_cpu_has(X86_FEATURE_SSSE3))
136 return crypto_register_shashes(sha256_ssse3_algs,
137 ARRAY_SIZE(sha256_ssse3_algs));
141 static void unregister_sha256_ssse3(void)
143 if (boot_cpu_has(X86_FEATURE_SSSE3))
144 crypto_unregister_shashes(sha256_ssse3_algs,
145 ARRAY_SIZE(sha256_ssse3_algs));
149 asmlinkage void sha256_transform_avx(u32 *digest, const char *data,
152 static int sha256_avx_update(struct shash_desc *desc, const u8 *data,
155 return sha256_update(desc, data, len, sha256_transform_avx);
158 static int sha256_avx_finup(struct shash_desc *desc, const u8 *data,
159 unsigned int len, u8 *out)
161 return sha256_finup(desc, data, len, out, sha256_transform_avx);
164 static int sha256_avx_final(struct shash_desc *desc, u8 *out)
166 return sha256_avx_finup(desc, NULL, 0, out);
169 static struct shash_alg sha256_avx_algs[] = { {
170 .digestsize = SHA256_DIGEST_SIZE,
171 .init = sha256_base_init,
172 .update = sha256_avx_update,
173 .final = sha256_avx_final,
174 .finup = sha256_avx_finup,
175 .descsize = sizeof(struct sha256_state),
177 .cra_name = "sha256",
178 .cra_driver_name = "sha256-avx",
180 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
181 .cra_blocksize = SHA256_BLOCK_SIZE,
182 .cra_module = THIS_MODULE,
185 .digestsize = SHA224_DIGEST_SIZE,
186 .init = sha224_base_init,
187 .update = sha256_avx_update,
188 .final = sha256_avx_final,
189 .finup = sha256_avx_finup,
190 .descsize = sizeof(struct sha256_state),
192 .cra_name = "sha224",
193 .cra_driver_name = "sha224-avx",
195 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
196 .cra_blocksize = SHA224_BLOCK_SIZE,
197 .cra_module = THIS_MODULE,
201 static bool avx_usable(void)
203 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) {
204 if (boot_cpu_has(X86_FEATURE_AVX))
205 pr_info("AVX detected but unusable.\n");
212 static int register_sha256_avx(void)
215 return crypto_register_shashes(sha256_avx_algs,
216 ARRAY_SIZE(sha256_avx_algs));
220 static void unregister_sha256_avx(void)
223 crypto_unregister_shashes(sha256_avx_algs,
224 ARRAY_SIZE(sha256_avx_algs));
228 static inline int register_sha256_avx(void) { return 0; }
229 static inline void unregister_sha256_avx(void) { }
232 #if defined(CONFIG_AS_AVX2) && defined(CONFIG_AS_AVX)
233 asmlinkage void sha256_transform_rorx(u32 *digest, const char *data,
236 static int sha256_avx2_update(struct shash_desc *desc, const u8 *data,
239 return sha256_update(desc, data, len, sha256_transform_rorx);
242 static int sha256_avx2_finup(struct shash_desc *desc, const u8 *data,
243 unsigned int len, u8 *out)
245 return sha256_finup(desc, data, len, out, sha256_transform_rorx);
248 static int sha256_avx2_final(struct shash_desc *desc, u8 *out)
250 return sha256_avx2_finup(desc, NULL, 0, out);
253 static struct shash_alg sha256_avx2_algs[] = { {
254 .digestsize = SHA256_DIGEST_SIZE,
255 .init = sha256_base_init,
256 .update = sha256_avx2_update,
257 .final = sha256_avx2_final,
258 .finup = sha256_avx2_finup,
259 .descsize = sizeof(struct sha256_state),
261 .cra_name = "sha256",
262 .cra_driver_name = "sha256-avx2",
264 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
265 .cra_blocksize = SHA256_BLOCK_SIZE,
266 .cra_module = THIS_MODULE,
269 .digestsize = SHA224_DIGEST_SIZE,
270 .init = sha224_base_init,
271 .update = sha256_avx2_update,
272 .final = sha256_avx2_final,
273 .finup = sha256_avx2_finup,
274 .descsize = sizeof(struct sha256_state),
276 .cra_name = "sha224",
277 .cra_driver_name = "sha224-avx2",
279 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
280 .cra_blocksize = SHA224_BLOCK_SIZE,
281 .cra_module = THIS_MODULE,
285 static bool avx2_usable(void)
287 if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2) &&
288 boot_cpu_has(X86_FEATURE_BMI2))
294 static int register_sha256_avx2(void)
297 return crypto_register_shashes(sha256_avx2_algs,
298 ARRAY_SIZE(sha256_avx2_algs));
302 static void unregister_sha256_avx2(void)
305 crypto_unregister_shashes(sha256_avx2_algs,
306 ARRAY_SIZE(sha256_avx2_algs));
310 static inline int register_sha256_avx2(void) { return 0; }
311 static inline void unregister_sha256_avx2(void) { }
314 #ifdef CONFIG_AS_SHA256_NI
315 asmlinkage void sha256_ni_transform(u32 *digest, const char *data,
316 u64 rounds); /*unsigned int rounds);*/
318 static int sha256_ni_update(struct shash_desc *desc, const u8 *data,
321 return sha256_update(desc, data, len, sha256_ni_transform);
324 static int sha256_ni_finup(struct shash_desc *desc, const u8 *data,
325 unsigned int len, u8 *out)
327 return sha256_finup(desc, data, len, out, sha256_ni_transform);
330 static int sha256_ni_final(struct shash_desc *desc, u8 *out)
332 return sha256_ni_finup(desc, NULL, 0, out);
335 static struct shash_alg sha256_ni_algs[] = { {
336 .digestsize = SHA256_DIGEST_SIZE,
337 .init = sha256_base_init,
338 .update = sha256_ni_update,
339 .final = sha256_ni_final,
340 .finup = sha256_ni_finup,
341 .descsize = sizeof(struct sha256_state),
343 .cra_name = "sha256",
344 .cra_driver_name = "sha256-ni",
346 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
347 .cra_blocksize = SHA256_BLOCK_SIZE,
348 .cra_module = THIS_MODULE,
351 .digestsize = SHA224_DIGEST_SIZE,
352 .init = sha224_base_init,
353 .update = sha256_ni_update,
354 .final = sha256_ni_final,
355 .finup = sha256_ni_finup,
356 .descsize = sizeof(struct sha256_state),
358 .cra_name = "sha224",
359 .cra_driver_name = "sha224-ni",
361 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
362 .cra_blocksize = SHA224_BLOCK_SIZE,
363 .cra_module = THIS_MODULE,
367 static int register_sha256_ni(void)
369 if (boot_cpu_has(X86_FEATURE_SHA_NI))
370 return crypto_register_shashes(sha256_ni_algs,
371 ARRAY_SIZE(sha256_ni_algs));
375 static void unregister_sha256_ni(void)
377 if (boot_cpu_has(X86_FEATURE_SHA_NI))
378 crypto_unregister_shashes(sha256_ni_algs,
379 ARRAY_SIZE(sha256_ni_algs));
383 static inline int register_sha256_ni(void) { return 0; }
384 static inline void unregister_sha256_ni(void) { }
387 static int __init sha256_ssse3_mod_init(void)
389 if (register_sha256_ssse3())
392 if (register_sha256_avx()) {
393 unregister_sha256_ssse3();
397 if (register_sha256_avx2()) {
398 unregister_sha256_avx();
399 unregister_sha256_ssse3();
403 if (register_sha256_ni()) {
404 unregister_sha256_avx2();
405 unregister_sha256_avx();
406 unregister_sha256_ssse3();
415 static void __exit sha256_ssse3_mod_fini(void)
417 unregister_sha256_ni();
418 unregister_sha256_avx2();
419 unregister_sha256_avx();
420 unregister_sha256_ssse3();
423 module_init(sha256_ssse3_mod_init);
424 module_exit(sha256_ssse3_mod_fini);
426 MODULE_LICENSE("GPL");
427 MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm, Supplemental SSE3 accelerated");
429 MODULE_ALIAS_CRYPTO("sha256");
430 MODULE_ALIAS_CRYPTO("sha256-ssse3");
431 MODULE_ALIAS_CRYPTO("sha256-avx");
432 MODULE_ALIAS_CRYPTO("sha256-avx2");
433 MODULE_ALIAS_CRYPTO("sha224");
434 MODULE_ALIAS_CRYPTO("sha224-ssse3");
435 MODULE_ALIAS_CRYPTO("sha224-avx");
436 MODULE_ALIAS_CRYPTO("sha224-avx2");
437 #ifdef CONFIG_AS_SHA256_NI
438 MODULE_ALIAS_CRYPTO("sha256-ni");
439 MODULE_ALIAS_CRYPTO("sha224-ni");