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 <crypto/internal/simd.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
37 #include <linux/cryptohash.h>
38 #include <linux/types.h>
39 #include <crypto/sha.h>
40 #include <crypto/sha256_base.h>
41 #include <linux/string.h>
44 asmlinkage
void sha256_transform_ssse3(struct sha256_state
*state
,
45 const u8
*data
, int blocks
);
47 static int _sha256_update(struct shash_desc
*desc
, const u8
*data
,
48 unsigned int len
, sha256_block_fn
*sha256_xform
)
50 struct sha256_state
*sctx
= shash_desc_ctx(desc
);
52 if (!crypto_simd_usable() ||
53 (sctx
->count
% SHA256_BLOCK_SIZE
) + len
< SHA256_BLOCK_SIZE
)
54 return crypto_sha256_update(desc
, data
, len
);
57 * Make sure struct sha256_state begins directly with the SHA256
58 * 256-bit internal state, as this is what the asm functions expect.
60 BUILD_BUG_ON(offsetof(struct sha256_state
, state
) != 0);
63 sha256_base_do_update(desc
, data
, len
, sha256_xform
);
69 static int sha256_finup(struct shash_desc
*desc
, const u8
*data
,
70 unsigned int len
, u8
*out
, sha256_block_fn
*sha256_xform
)
72 if (!crypto_simd_usable())
73 return crypto_sha256_finup(desc
, data
, len
, out
);
77 sha256_base_do_update(desc
, data
, len
, sha256_xform
);
78 sha256_base_do_finalize(desc
, sha256_xform
);
81 return sha256_base_finish(desc
, out
);
84 static int sha256_ssse3_update(struct shash_desc
*desc
, const u8
*data
,
87 return _sha256_update(desc
, data
, len
, sha256_transform_ssse3
);
90 static int sha256_ssse3_finup(struct shash_desc
*desc
, const u8
*data
,
91 unsigned int len
, u8
*out
)
93 return sha256_finup(desc
, data
, len
, out
, sha256_transform_ssse3
);
96 /* Add padding and return the message digest. */
97 static int sha256_ssse3_final(struct shash_desc
*desc
, u8
*out
)
99 return sha256_ssse3_finup(desc
, NULL
, 0, out
);
102 static struct shash_alg sha256_ssse3_algs
[] = { {
103 .digestsize
= SHA256_DIGEST_SIZE
,
104 .init
= sha256_base_init
,
105 .update
= sha256_ssse3_update
,
106 .final
= sha256_ssse3_final
,
107 .finup
= sha256_ssse3_finup
,
108 .descsize
= sizeof(struct sha256_state
),
110 .cra_name
= "sha256",
111 .cra_driver_name
= "sha256-ssse3",
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_blocksize
= SHA224_BLOCK_SIZE
,
128 .cra_module
= THIS_MODULE
,
132 static int register_sha256_ssse3(void)
134 if (boot_cpu_has(X86_FEATURE_SSSE3
))
135 return crypto_register_shashes(sha256_ssse3_algs
,
136 ARRAY_SIZE(sha256_ssse3_algs
));
140 static void unregister_sha256_ssse3(void)
142 if (boot_cpu_has(X86_FEATURE_SSSE3
))
143 crypto_unregister_shashes(sha256_ssse3_algs
,
144 ARRAY_SIZE(sha256_ssse3_algs
));
147 asmlinkage
void sha256_transform_avx(struct sha256_state
*state
,
148 const u8
*data
, int blocks
);
150 static int sha256_avx_update(struct shash_desc
*desc
, const u8
*data
,
153 return _sha256_update(desc
, data
, len
, sha256_transform_avx
);
156 static int sha256_avx_finup(struct shash_desc
*desc
, const u8
*data
,
157 unsigned int len
, u8
*out
)
159 return sha256_finup(desc
, data
, len
, out
, sha256_transform_avx
);
162 static int sha256_avx_final(struct shash_desc
*desc
, u8
*out
)
164 return sha256_avx_finup(desc
, NULL
, 0, out
);
167 static struct shash_alg sha256_avx_algs
[] = { {
168 .digestsize
= SHA256_DIGEST_SIZE
,
169 .init
= sha256_base_init
,
170 .update
= sha256_avx_update
,
171 .final
= sha256_avx_final
,
172 .finup
= sha256_avx_finup
,
173 .descsize
= sizeof(struct sha256_state
),
175 .cra_name
= "sha256",
176 .cra_driver_name
= "sha256-avx",
178 .cra_blocksize
= SHA256_BLOCK_SIZE
,
179 .cra_module
= THIS_MODULE
,
182 .digestsize
= SHA224_DIGEST_SIZE
,
183 .init
= sha224_base_init
,
184 .update
= sha256_avx_update
,
185 .final
= sha256_avx_final
,
186 .finup
= sha256_avx_finup
,
187 .descsize
= sizeof(struct sha256_state
),
189 .cra_name
= "sha224",
190 .cra_driver_name
= "sha224-avx",
192 .cra_blocksize
= SHA224_BLOCK_SIZE
,
193 .cra_module
= THIS_MODULE
,
197 static bool avx_usable(void)
199 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE
| XFEATURE_MASK_YMM
, NULL
)) {
200 if (boot_cpu_has(X86_FEATURE_AVX
))
201 pr_info("AVX detected but unusable.\n");
208 static int register_sha256_avx(void)
211 return crypto_register_shashes(sha256_avx_algs
,
212 ARRAY_SIZE(sha256_avx_algs
));
216 static void unregister_sha256_avx(void)
219 crypto_unregister_shashes(sha256_avx_algs
,
220 ARRAY_SIZE(sha256_avx_algs
));
223 asmlinkage
void sha256_transform_rorx(struct sha256_state
*state
,
224 const u8
*data
, int blocks
);
226 static int sha256_avx2_update(struct shash_desc
*desc
, const u8
*data
,
229 return _sha256_update(desc
, data
, len
, sha256_transform_rorx
);
232 static int sha256_avx2_finup(struct shash_desc
*desc
, const u8
*data
,
233 unsigned int len
, u8
*out
)
235 return sha256_finup(desc
, data
, len
, out
, sha256_transform_rorx
);
238 static int sha256_avx2_final(struct shash_desc
*desc
, u8
*out
)
240 return sha256_avx2_finup(desc
, NULL
, 0, out
);
243 static struct shash_alg sha256_avx2_algs
[] = { {
244 .digestsize
= SHA256_DIGEST_SIZE
,
245 .init
= sha256_base_init
,
246 .update
= sha256_avx2_update
,
247 .final
= sha256_avx2_final
,
248 .finup
= sha256_avx2_finup
,
249 .descsize
= sizeof(struct sha256_state
),
251 .cra_name
= "sha256",
252 .cra_driver_name
= "sha256-avx2",
254 .cra_blocksize
= SHA256_BLOCK_SIZE
,
255 .cra_module
= THIS_MODULE
,
258 .digestsize
= SHA224_DIGEST_SIZE
,
259 .init
= sha224_base_init
,
260 .update
= sha256_avx2_update
,
261 .final
= sha256_avx2_final
,
262 .finup
= sha256_avx2_finup
,
263 .descsize
= sizeof(struct sha256_state
),
265 .cra_name
= "sha224",
266 .cra_driver_name
= "sha224-avx2",
268 .cra_blocksize
= SHA224_BLOCK_SIZE
,
269 .cra_module
= THIS_MODULE
,
273 static bool avx2_usable(void)
275 if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2
) &&
276 boot_cpu_has(X86_FEATURE_BMI2
))
282 static int register_sha256_avx2(void)
285 return crypto_register_shashes(sha256_avx2_algs
,
286 ARRAY_SIZE(sha256_avx2_algs
));
290 static void unregister_sha256_avx2(void)
293 crypto_unregister_shashes(sha256_avx2_algs
,
294 ARRAY_SIZE(sha256_avx2_algs
));
297 #ifdef CONFIG_AS_SHA256_NI
298 asmlinkage
void sha256_ni_transform(struct sha256_state
*digest
,
299 const u8
*data
, int rounds
);
301 static int sha256_ni_update(struct shash_desc
*desc
, const u8
*data
,
304 return _sha256_update(desc
, data
, len
, sha256_ni_transform
);
307 static int sha256_ni_finup(struct shash_desc
*desc
, const u8
*data
,
308 unsigned int len
, u8
*out
)
310 return sha256_finup(desc
, data
, len
, out
, sha256_ni_transform
);
313 static int sha256_ni_final(struct shash_desc
*desc
, u8
*out
)
315 return sha256_ni_finup(desc
, NULL
, 0, out
);
318 static struct shash_alg sha256_ni_algs
[] = { {
319 .digestsize
= SHA256_DIGEST_SIZE
,
320 .init
= sha256_base_init
,
321 .update
= sha256_ni_update
,
322 .final
= sha256_ni_final
,
323 .finup
= sha256_ni_finup
,
324 .descsize
= sizeof(struct sha256_state
),
326 .cra_name
= "sha256",
327 .cra_driver_name
= "sha256-ni",
329 .cra_blocksize
= SHA256_BLOCK_SIZE
,
330 .cra_module
= THIS_MODULE
,
333 .digestsize
= SHA224_DIGEST_SIZE
,
334 .init
= sha224_base_init
,
335 .update
= sha256_ni_update
,
336 .final
= sha256_ni_final
,
337 .finup
= sha256_ni_finup
,
338 .descsize
= sizeof(struct sha256_state
),
340 .cra_name
= "sha224",
341 .cra_driver_name
= "sha224-ni",
343 .cra_blocksize
= SHA224_BLOCK_SIZE
,
344 .cra_module
= THIS_MODULE
,
348 static int register_sha256_ni(void)
350 if (boot_cpu_has(X86_FEATURE_SHA_NI
))
351 return crypto_register_shashes(sha256_ni_algs
,
352 ARRAY_SIZE(sha256_ni_algs
));
356 static void unregister_sha256_ni(void)
358 if (boot_cpu_has(X86_FEATURE_SHA_NI
))
359 crypto_unregister_shashes(sha256_ni_algs
,
360 ARRAY_SIZE(sha256_ni_algs
));
364 static inline int register_sha256_ni(void) { return 0; }
365 static inline void unregister_sha256_ni(void) { }
368 static int __init
sha256_ssse3_mod_init(void)
370 if (register_sha256_ssse3())
373 if (register_sha256_avx()) {
374 unregister_sha256_ssse3();
378 if (register_sha256_avx2()) {
379 unregister_sha256_avx();
380 unregister_sha256_ssse3();
384 if (register_sha256_ni()) {
385 unregister_sha256_avx2();
386 unregister_sha256_avx();
387 unregister_sha256_ssse3();
396 static void __exit
sha256_ssse3_mod_fini(void)
398 unregister_sha256_ni();
399 unregister_sha256_avx2();
400 unregister_sha256_avx();
401 unregister_sha256_ssse3();
404 module_init(sha256_ssse3_mod_init
);
405 module_exit(sha256_ssse3_mod_fini
);
407 MODULE_LICENSE("GPL");
408 MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm, Supplemental SSE3 accelerated");
410 MODULE_ALIAS_CRYPTO("sha256");
411 MODULE_ALIAS_CRYPTO("sha256-ssse3");
412 MODULE_ALIAS_CRYPTO("sha256-avx");
413 MODULE_ALIAS_CRYPTO("sha256-avx2");
414 MODULE_ALIAS_CRYPTO("sha224");
415 MODULE_ALIAS_CRYPTO("sha224-ssse3");
416 MODULE_ALIAS_CRYPTO("sha224-avx");
417 MODULE_ALIAS_CRYPTO("sha224-avx2");
418 #ifdef CONFIG_AS_SHA256_NI
419 MODULE_ALIAS_CRYPTO("sha256-ni");
420 MODULE_ALIAS_CRYPTO("sha224-ni");