4 * Glue code for the SHA512 Secure Hash Algorithm assembler
5 * implementation using supplemental SSE3 / AVX / AVX2 instructions.
7 * This file is based on sha512_generic.c
9 * Copyright (C) 2013 Intel Corporation
10 * Author: Tim Chen <tim.c.chen@linux.intel.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <crypto/internal/hash.h>
31 #include <crypto/internal/simd.h>
32 #include <linux/init.h>
33 #include <linux/module.h>
35 #include <linux/string.h>
36 #include <linux/types.h>
37 #include <crypto/sha2.h>
38 #include <crypto/sha512_base.h>
41 asmlinkage
void sha512_transform_ssse3(struct sha512_state
*state
,
42 const u8
*data
, int blocks
);
44 static int sha512_update(struct shash_desc
*desc
, const u8
*data
,
45 unsigned int len
, sha512_block_fn
*sha512_xform
)
47 struct sha512_state
*sctx
= shash_desc_ctx(desc
);
49 if (!crypto_simd_usable() ||
50 (sctx
->count
[0] % SHA512_BLOCK_SIZE
) + len
< SHA512_BLOCK_SIZE
)
51 return crypto_sha512_update(desc
, data
, len
);
54 * Make sure struct sha512_state begins directly with the SHA512
55 * 512-bit internal state, as this is what the asm functions expect.
57 BUILD_BUG_ON(offsetof(struct sha512_state
, state
) != 0);
60 sha512_base_do_update(desc
, data
, len
, sha512_xform
);
66 static int sha512_finup(struct shash_desc
*desc
, const u8
*data
,
67 unsigned int len
, u8
*out
, sha512_block_fn
*sha512_xform
)
69 if (!crypto_simd_usable())
70 return crypto_sha512_finup(desc
, data
, len
, out
);
74 sha512_base_do_update(desc
, data
, len
, sha512_xform
);
75 sha512_base_do_finalize(desc
, sha512_xform
);
78 return sha512_base_finish(desc
, out
);
81 static int sha512_ssse3_update(struct shash_desc
*desc
, const u8
*data
,
84 return sha512_update(desc
, data
, len
, sha512_transform_ssse3
);
87 static int sha512_ssse3_finup(struct shash_desc
*desc
, const u8
*data
,
88 unsigned int len
, u8
*out
)
90 return sha512_finup(desc
, data
, len
, out
, sha512_transform_ssse3
);
93 /* Add padding and return the message digest. */
94 static int sha512_ssse3_final(struct shash_desc
*desc
, u8
*out
)
96 return sha512_ssse3_finup(desc
, NULL
, 0, out
);
99 static struct shash_alg sha512_ssse3_algs
[] = { {
100 .digestsize
= SHA512_DIGEST_SIZE
,
101 .init
= sha512_base_init
,
102 .update
= sha512_ssse3_update
,
103 .final
= sha512_ssse3_final
,
104 .finup
= sha512_ssse3_finup
,
105 .descsize
= sizeof(struct sha512_state
),
107 .cra_name
= "sha512",
108 .cra_driver_name
= "sha512-ssse3",
110 .cra_blocksize
= SHA512_BLOCK_SIZE
,
111 .cra_module
= THIS_MODULE
,
114 .digestsize
= SHA384_DIGEST_SIZE
,
115 .init
= sha384_base_init
,
116 .update
= sha512_ssse3_update
,
117 .final
= sha512_ssse3_final
,
118 .finup
= sha512_ssse3_finup
,
119 .descsize
= sizeof(struct sha512_state
),
121 .cra_name
= "sha384",
122 .cra_driver_name
= "sha384-ssse3",
124 .cra_blocksize
= SHA384_BLOCK_SIZE
,
125 .cra_module
= THIS_MODULE
,
129 static int register_sha512_ssse3(void)
131 if (boot_cpu_has(X86_FEATURE_SSSE3
))
132 return crypto_register_shashes(sha512_ssse3_algs
,
133 ARRAY_SIZE(sha512_ssse3_algs
));
137 static void unregister_sha512_ssse3(void)
139 if (boot_cpu_has(X86_FEATURE_SSSE3
))
140 crypto_unregister_shashes(sha512_ssse3_algs
,
141 ARRAY_SIZE(sha512_ssse3_algs
));
144 asmlinkage
void sha512_transform_avx(struct sha512_state
*state
,
145 const u8
*data
, int blocks
);
146 static bool avx_usable(void)
148 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE
| XFEATURE_MASK_YMM
, NULL
)) {
149 if (boot_cpu_has(X86_FEATURE_AVX
))
150 pr_info("AVX detected but unusable.\n");
157 static int sha512_avx_update(struct shash_desc
*desc
, const u8
*data
,
160 return sha512_update(desc
, data
, len
, sha512_transform_avx
);
163 static int sha512_avx_finup(struct shash_desc
*desc
, const u8
*data
,
164 unsigned int len
, u8
*out
)
166 return sha512_finup(desc
, data
, len
, out
, sha512_transform_avx
);
169 /* Add padding and return the message digest. */
170 static int sha512_avx_final(struct shash_desc
*desc
, u8
*out
)
172 return sha512_avx_finup(desc
, NULL
, 0, out
);
175 static struct shash_alg sha512_avx_algs
[] = { {
176 .digestsize
= SHA512_DIGEST_SIZE
,
177 .init
= sha512_base_init
,
178 .update
= sha512_avx_update
,
179 .final
= sha512_avx_final
,
180 .finup
= sha512_avx_finup
,
181 .descsize
= sizeof(struct sha512_state
),
183 .cra_name
= "sha512",
184 .cra_driver_name
= "sha512-avx",
186 .cra_blocksize
= SHA512_BLOCK_SIZE
,
187 .cra_module
= THIS_MODULE
,
190 .digestsize
= SHA384_DIGEST_SIZE
,
191 .init
= sha384_base_init
,
192 .update
= sha512_avx_update
,
193 .final
= sha512_avx_final
,
194 .finup
= sha512_avx_finup
,
195 .descsize
= sizeof(struct sha512_state
),
197 .cra_name
= "sha384",
198 .cra_driver_name
= "sha384-avx",
200 .cra_blocksize
= SHA384_BLOCK_SIZE
,
201 .cra_module
= THIS_MODULE
,
205 static int register_sha512_avx(void)
208 return crypto_register_shashes(sha512_avx_algs
,
209 ARRAY_SIZE(sha512_avx_algs
));
213 static void unregister_sha512_avx(void)
216 crypto_unregister_shashes(sha512_avx_algs
,
217 ARRAY_SIZE(sha512_avx_algs
));
220 asmlinkage
void sha512_transform_rorx(struct sha512_state
*state
,
221 const u8
*data
, int blocks
);
223 static int sha512_avx2_update(struct shash_desc
*desc
, const u8
*data
,
226 return sha512_update(desc
, data
, len
, sha512_transform_rorx
);
229 static int sha512_avx2_finup(struct shash_desc
*desc
, const u8
*data
,
230 unsigned int len
, u8
*out
)
232 return sha512_finup(desc
, data
, len
, out
, sha512_transform_rorx
);
235 /* Add padding and return the message digest. */
236 static int sha512_avx2_final(struct shash_desc
*desc
, u8
*out
)
238 return sha512_avx2_finup(desc
, NULL
, 0, out
);
241 static struct shash_alg sha512_avx2_algs
[] = { {
242 .digestsize
= SHA512_DIGEST_SIZE
,
243 .init
= sha512_base_init
,
244 .update
= sha512_avx2_update
,
245 .final
= sha512_avx2_final
,
246 .finup
= sha512_avx2_finup
,
247 .descsize
= sizeof(struct sha512_state
),
249 .cra_name
= "sha512",
250 .cra_driver_name
= "sha512-avx2",
252 .cra_blocksize
= SHA512_BLOCK_SIZE
,
253 .cra_module
= THIS_MODULE
,
256 .digestsize
= SHA384_DIGEST_SIZE
,
257 .init
= sha384_base_init
,
258 .update
= sha512_avx2_update
,
259 .final
= sha512_avx2_final
,
260 .finup
= sha512_avx2_finup
,
261 .descsize
= sizeof(struct sha512_state
),
263 .cra_name
= "sha384",
264 .cra_driver_name
= "sha384-avx2",
266 .cra_blocksize
= SHA384_BLOCK_SIZE
,
267 .cra_module
= THIS_MODULE
,
271 static bool avx2_usable(void)
273 if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2
) &&
274 boot_cpu_has(X86_FEATURE_BMI2
))
280 static int register_sha512_avx2(void)
283 return crypto_register_shashes(sha512_avx2_algs
,
284 ARRAY_SIZE(sha512_avx2_algs
));
288 static void unregister_sha512_avx2(void)
291 crypto_unregister_shashes(sha512_avx2_algs
,
292 ARRAY_SIZE(sha512_avx2_algs
));
295 static int __init
sha512_ssse3_mod_init(void)
298 if (register_sha512_ssse3())
301 if (register_sha512_avx()) {
302 unregister_sha512_ssse3();
306 if (register_sha512_avx2()) {
307 unregister_sha512_avx();
308 unregister_sha512_ssse3();
317 static void __exit
sha512_ssse3_mod_fini(void)
319 unregister_sha512_avx2();
320 unregister_sha512_avx();
321 unregister_sha512_ssse3();
324 module_init(sha512_ssse3_mod_init
);
325 module_exit(sha512_ssse3_mod_fini
);
327 MODULE_LICENSE("GPL");
328 MODULE_DESCRIPTION("SHA512 Secure Hash Algorithm, Supplemental SSE3 accelerated");
330 MODULE_ALIAS_CRYPTO("sha512");
331 MODULE_ALIAS_CRYPTO("sha512-ssse3");
332 MODULE_ALIAS_CRYPTO("sha512-avx");
333 MODULE_ALIAS_CRYPTO("sha512-avx2");
334 MODULE_ALIAS_CRYPTO("sha384");
335 MODULE_ALIAS_CRYPTO("sha384-ssse3");
336 MODULE_ALIAS_CRYPTO("sha384-avx");
337 MODULE_ALIAS_CRYPTO("sha384-avx2");