blob: 773a873d2b28a31cdcaa3b1d085cb8ddf94664d6 [file] [log] [blame]
Tim Chen8275d1a2013-03-26 13:59:17 -07001/*
2 * Cryptographic API.
3 *
4 * Glue code for the SHA256 Secure Hash Algorithm assembler
5 * implementation using supplemental SSE3 / AVX / AVX2 instructions.
6 *
7 * This file is based on sha256_generic.c
8 *
9 * Copyright (C) 2013 Intel Corporation.
10 *
11 * Author:
12 * Tim Chen <tim.c.chen@linux.intel.com>
13 *
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)
17 * any later version.
18 *
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
26 * SOFTWARE.
27 */
28
29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32#include <crypto/internal/hash.h>
33#include <linux/init.h>
34#include <linux/module.h>
35#include <linux/mm.h>
36#include <linux/cryptohash.h>
37#include <linux/types.h>
38#include <crypto/sha.h>
Ard Biesheuvel16310302015-04-09 12:55:47 +020039#include <crypto/sha256_base.h>
Ingo Molnardf6b35f2015-04-24 02:46:00 +020040#include <asm/fpu/api.h>
Tim Chen8275d1a2013-03-26 13:59:17 -070041#include <linux/string.h>
42
Ard Biesheuvel16310302015-04-09 12:55:47 +020043asmlinkage void sha256_transform_ssse3(u32 *digest, const char *data,
44 u64 rounds);
tim5dda42f2015-09-16 16:35:23 -070045typedef void (sha256_transform_fn)(u32 *digest, const char *data, u64 rounds);
Tim Chen8275d1a2013-03-26 13:59:17 -070046
tim5dda42f2015-09-16 16:35:23 -070047static int sha256_update(struct shash_desc *desc, const u8 *data,
48 unsigned int len, sha256_transform_fn *sha256_xform)
Tim Chen8275d1a2013-03-26 13:59:17 -070049{
50 struct sha256_state *sctx = shash_desc_ctx(desc);
Tim Chen8275d1a2013-03-26 13:59:17 -070051
Ard Biesheuvel16310302015-04-09 12:55:47 +020052 if (!irq_fpu_usable() ||
53 (sctx->count % SHA256_BLOCK_SIZE) + len < SHA256_BLOCK_SIZE)
54 return crypto_sha256_update(desc, data, len);
Tim Chen8275d1a2013-03-26 13:59:17 -070055
Ard Biesheuvel16310302015-04-09 12:55:47 +020056 /* make sure casting to sha256_block_fn() is safe */
57 BUILD_BUG_ON(offsetof(struct sha256_state, state) != 0);
Tim Chen8275d1a2013-03-26 13:59:17 -070058
Ard Biesheuvel16310302015-04-09 12:55:47 +020059 kernel_fpu_begin();
60 sha256_base_do_update(desc, data, len,
tim5dda42f2015-09-16 16:35:23 -070061 (sha256_block_fn *)sha256_xform);
Ard Biesheuvel16310302015-04-09 12:55:47 +020062 kernel_fpu_end();
Tim Chen8275d1a2013-03-26 13:59:17 -070063
Ard Biesheuvel16310302015-04-09 12:55:47 +020064 return 0;
Tim Chen8275d1a2013-03-26 13:59:17 -070065}
66
tim5dda42f2015-09-16 16:35:23 -070067static int sha256_finup(struct shash_desc *desc, const u8 *data,
68 unsigned int len, u8 *out, sha256_transform_fn *sha256_xform)
Ard Biesheuvel16310302015-04-09 12:55:47 +020069{
70 if (!irq_fpu_usable())
71 return crypto_sha256_finup(desc, data, len, out);
72
73 kernel_fpu_begin();
74 if (len)
75 sha256_base_do_update(desc, data, len,
tim5dda42f2015-09-16 16:35:23 -070076 (sha256_block_fn *)sha256_xform);
77 sha256_base_do_finalize(desc, (sha256_block_fn *)sha256_xform);
Ard Biesheuvel16310302015-04-09 12:55:47 +020078 kernel_fpu_end();
79
80 return sha256_base_finish(desc, out);
81}
Tim Chen8275d1a2013-03-26 13:59:17 -070082
tim5dda42f2015-09-16 16:35:23 -070083static int sha256_ssse3_update(struct shash_desc *desc, const u8 *data,
84 unsigned int len)
85{
86 return sha256_update(desc, data, len, sha256_transform_ssse3);
87}
88
89static int sha256_ssse3_finup(struct shash_desc *desc, const u8 *data,
90 unsigned int len, u8 *out)
91{
92 return sha256_finup(desc, data, len, out, sha256_transform_ssse3);
93}
94
Tim Chen8275d1a2013-03-26 13:59:17 -070095/* Add padding and return the message digest. */
96static int sha256_ssse3_final(struct shash_desc *desc, u8 *out)
97{
Ard Biesheuvel16310302015-04-09 12:55:47 +020098 return sha256_ssse3_finup(desc, NULL, 0, out);
Jussi Kivilinnaa710f762013-05-21 17:10:49 +030099}
100
tim5dda42f2015-09-16 16:35:23 -0700101static struct shash_alg sha256_ssse3_algs[] = { {
Tim Chen8275d1a2013-03-26 13:59:17 -0700102 .digestsize = SHA256_DIGEST_SIZE,
Ard Biesheuvel16310302015-04-09 12:55:47 +0200103 .init = sha256_base_init,
Tim Chen8275d1a2013-03-26 13:59:17 -0700104 .update = sha256_ssse3_update,
105 .final = sha256_ssse3_final,
Ard Biesheuvel16310302015-04-09 12:55:47 +0200106 .finup = sha256_ssse3_finup,
Tim Chen8275d1a2013-03-26 13:59:17 -0700107 .descsize = sizeof(struct sha256_state),
Tim Chen8275d1a2013-03-26 13:59:17 -0700108 .base = {
109 .cra_name = "sha256",
110 .cra_driver_name = "sha256-ssse3",
111 .cra_priority = 150,
Tim Chen8275d1a2013-03-26 13:59:17 -0700112 .cra_blocksize = SHA256_BLOCK_SIZE,
113 .cra_module = THIS_MODULE,
114 }
Jussi Kivilinnaa710f762013-05-21 17:10:49 +0300115}, {
116 .digestsize = SHA224_DIGEST_SIZE,
Ard Biesheuvel16310302015-04-09 12:55:47 +0200117 .init = sha224_base_init,
Jussi Kivilinnaa710f762013-05-21 17:10:49 +0300118 .update = sha256_ssse3_update,
Ard Biesheuvel16310302015-04-09 12:55:47 +0200119 .final = sha256_ssse3_final,
120 .finup = sha256_ssse3_finup,
Jussi Kivilinnaa710f762013-05-21 17:10:49 +0300121 .descsize = sizeof(struct sha256_state),
Jussi Kivilinnaa710f762013-05-21 17:10:49 +0300122 .base = {
123 .cra_name = "sha224",
124 .cra_driver_name = "sha224-ssse3",
125 .cra_priority = 150,
Jussi Kivilinnaa710f762013-05-21 17:10:49 +0300126 .cra_blocksize = SHA224_BLOCK_SIZE,
127 .cra_module = THIS_MODULE,
128 }
129} };
Tim Chen8275d1a2013-03-26 13:59:17 -0700130
tim5dda42f2015-09-16 16:35:23 -0700131static int register_sha256_ssse3(void)
132{
133 if (boot_cpu_has(X86_FEATURE_SSSE3))
134 return crypto_register_shashes(sha256_ssse3_algs,
135 ARRAY_SIZE(sha256_ssse3_algs));
136 return 0;
137}
138
139static void unregister_sha256_ssse3(void)
140{
141 if (boot_cpu_has(X86_FEATURE_SSSE3))
142 crypto_unregister_shashes(sha256_ssse3_algs,
143 ARRAY_SIZE(sha256_ssse3_algs));
144}
145
Tim Chen8275d1a2013-03-26 13:59:17 -0700146#ifdef CONFIG_AS_AVX
tim5dda42f2015-09-16 16:35:23 -0700147asmlinkage void sha256_transform_avx(u32 *digest, const char *data,
148 u64 rounds);
149
150static int sha256_avx_update(struct shash_desc *desc, const u8 *data,
151 unsigned int len)
152{
153 return sha256_update(desc, data, len, sha256_transform_avx);
154}
155
156static int sha256_avx_finup(struct shash_desc *desc, const u8 *data,
157 unsigned int len, u8 *out)
158{
159 return sha256_finup(desc, data, len, out, sha256_transform_avx);
160}
161
162static int sha256_avx_final(struct shash_desc *desc, u8 *out)
163{
164 return sha256_avx_finup(desc, NULL, 0, out);
165}
166
167static 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),
174 .base = {
175 .cra_name = "sha256",
176 .cra_driver_name = "sha256-avx",
177 .cra_priority = 160,
tim5dda42f2015-09-16 16:35:23 -0700178 .cra_blocksize = SHA256_BLOCK_SIZE,
179 .cra_module = THIS_MODULE,
180 }
181}, {
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),
188 .base = {
189 .cra_name = "sha224",
190 .cra_driver_name = "sha224-avx",
191 .cra_priority = 160,
tim5dda42f2015-09-16 16:35:23 -0700192 .cra_blocksize = SHA224_BLOCK_SIZE,
193 .cra_module = THIS_MODULE,
194 }
195} };
196
197static bool avx_usable(void)
Tim Chen8275d1a2013-03-26 13:59:17 -0700198{
Dave Hansend91cab72015-09-02 16:31:26 -0700199 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL)) {
Borislav Petkovda154e82016-04-04 22:24:56 +0200200 if (boot_cpu_has(X86_FEATURE_AVX))
Ingo Molnar70d51eb2015-04-28 10:11:24 +0200201 pr_info("AVX detected but unusable.\n");
Tim Chen8275d1a2013-03-26 13:59:17 -0700202 return false;
203 }
204
205 return true;
206}
tim5dda42f2015-09-16 16:35:23 -0700207
208static int register_sha256_avx(void)
209{
210 if (avx_usable())
211 return crypto_register_shashes(sha256_avx_algs,
212 ARRAY_SIZE(sha256_avx_algs));
213 return 0;
214}
215
216static void unregister_sha256_avx(void)
217{
218 if (avx_usable())
219 crypto_unregister_shashes(sha256_avx_algs,
220 ARRAY_SIZE(sha256_avx_algs));
221}
222
223#else
224static inline int register_sha256_avx(void) { return 0; }
225static inline void unregister_sha256_avx(void) { }
226#endif
227
228#if defined(CONFIG_AS_AVX2) && defined(CONFIG_AS_AVX)
229asmlinkage void sha256_transform_rorx(u32 *digest, const char *data,
230 u64 rounds);
231
232static int sha256_avx2_update(struct shash_desc *desc, const u8 *data,
233 unsigned int len)
234{
235 return sha256_update(desc, data, len, sha256_transform_rorx);
236}
237
238static int sha256_avx2_finup(struct shash_desc *desc, const u8 *data,
239 unsigned int len, u8 *out)
240{
241 return sha256_finup(desc, data, len, out, sha256_transform_rorx);
242}
243
244static int sha256_avx2_final(struct shash_desc *desc, u8 *out)
245{
246 return sha256_avx2_finup(desc, NULL, 0, out);
247}
248
249static struct shash_alg sha256_avx2_algs[] = { {
250 .digestsize = SHA256_DIGEST_SIZE,
251 .init = sha256_base_init,
252 .update = sha256_avx2_update,
253 .final = sha256_avx2_final,
254 .finup = sha256_avx2_finup,
255 .descsize = sizeof(struct sha256_state),
256 .base = {
257 .cra_name = "sha256",
258 .cra_driver_name = "sha256-avx2",
259 .cra_priority = 170,
tim5dda42f2015-09-16 16:35:23 -0700260 .cra_blocksize = SHA256_BLOCK_SIZE,
261 .cra_module = THIS_MODULE,
262 }
263}, {
264 .digestsize = SHA224_DIGEST_SIZE,
265 .init = sha224_base_init,
266 .update = sha256_avx2_update,
267 .final = sha256_avx2_final,
268 .finup = sha256_avx2_finup,
269 .descsize = sizeof(struct sha256_state),
270 .base = {
271 .cra_name = "sha224",
272 .cra_driver_name = "sha224-avx2",
273 .cra_priority = 170,
tim5dda42f2015-09-16 16:35:23 -0700274 .cra_blocksize = SHA224_BLOCK_SIZE,
275 .cra_module = THIS_MODULE,
276 }
277} };
278
279static bool avx2_usable(void)
280{
281 if (avx_usable() && boot_cpu_has(X86_FEATURE_AVX2) &&
282 boot_cpu_has(X86_FEATURE_BMI2))
283 return true;
284
285 return false;
286}
287
288static int register_sha256_avx2(void)
289{
290 if (avx2_usable())
291 return crypto_register_shashes(sha256_avx2_algs,
292 ARRAY_SIZE(sha256_avx2_algs));
293 return 0;
294}
295
296static void unregister_sha256_avx2(void)
297{
298 if (avx2_usable())
299 crypto_unregister_shashes(sha256_avx2_algs,
300 ARRAY_SIZE(sha256_avx2_algs));
301}
302
303#else
304static inline int register_sha256_avx2(void) { return 0; }
305static inline void unregister_sha256_avx2(void) { }
306#endif
307
308#ifdef CONFIG_AS_SHA256_NI
309asmlinkage void sha256_ni_transform(u32 *digest, const char *data,
310 u64 rounds); /*unsigned int rounds);*/
311
312static int sha256_ni_update(struct shash_desc *desc, const u8 *data,
313 unsigned int len)
314{
315 return sha256_update(desc, data, len, sha256_ni_transform);
316}
317
318static int sha256_ni_finup(struct shash_desc *desc, const u8 *data,
319 unsigned int len, u8 *out)
320{
321 return sha256_finup(desc, data, len, out, sha256_ni_transform);
322}
323
324static int sha256_ni_final(struct shash_desc *desc, u8 *out)
325{
326 return sha256_ni_finup(desc, NULL, 0, out);
327}
328
329static struct shash_alg sha256_ni_algs[] = { {
330 .digestsize = SHA256_DIGEST_SIZE,
331 .init = sha256_base_init,
332 .update = sha256_ni_update,
333 .final = sha256_ni_final,
334 .finup = sha256_ni_finup,
335 .descsize = sizeof(struct sha256_state),
336 .base = {
337 .cra_name = "sha256",
338 .cra_driver_name = "sha256-ni",
339 .cra_priority = 250,
tim5dda42f2015-09-16 16:35:23 -0700340 .cra_blocksize = SHA256_BLOCK_SIZE,
341 .cra_module = THIS_MODULE,
342 }
343}, {
344 .digestsize = SHA224_DIGEST_SIZE,
345 .init = sha224_base_init,
346 .update = sha256_ni_update,
347 .final = sha256_ni_final,
348 .finup = sha256_ni_finup,
349 .descsize = sizeof(struct sha256_state),
350 .base = {
351 .cra_name = "sha224",
352 .cra_driver_name = "sha224-ni",
353 .cra_priority = 250,
tim5dda42f2015-09-16 16:35:23 -0700354 .cra_blocksize = SHA224_BLOCK_SIZE,
355 .cra_module = THIS_MODULE,
356 }
357} };
358
359static int register_sha256_ni(void)
360{
361 if (boot_cpu_has(X86_FEATURE_SHA_NI))
362 return crypto_register_shashes(sha256_ni_algs,
363 ARRAY_SIZE(sha256_ni_algs));
364 return 0;
365}
366
367static void unregister_sha256_ni(void)
368{
369 if (boot_cpu_has(X86_FEATURE_SHA_NI))
370 crypto_unregister_shashes(sha256_ni_algs,
371 ARRAY_SIZE(sha256_ni_algs));
372}
373
374#else
375static inline int register_sha256_ni(void) { return 0; }
376static inline void unregister_sha256_ni(void) { }
Tim Chen8275d1a2013-03-26 13:59:17 -0700377#endif
378
379static int __init sha256_ssse3_mod_init(void)
380{
tim5dda42f2015-09-16 16:35:23 -0700381 if (register_sha256_ssse3())
382 goto fail;
Tim Chen8275d1a2013-03-26 13:59:17 -0700383
tim5dda42f2015-09-16 16:35:23 -0700384 if (register_sha256_avx()) {
385 unregister_sha256_ssse3();
386 goto fail;
Tim Chen8275d1a2013-03-26 13:59:17 -0700387 }
Tim Chen8275d1a2013-03-26 13:59:17 -0700388
tim5dda42f2015-09-16 16:35:23 -0700389 if (register_sha256_avx2()) {
390 unregister_sha256_avx();
391 unregister_sha256_ssse3();
392 goto fail;
Tim Chen8275d1a2013-03-26 13:59:17 -0700393 }
Tim Chen8275d1a2013-03-26 13:59:17 -0700394
tim5dda42f2015-09-16 16:35:23 -0700395 if (register_sha256_ni()) {
396 unregister_sha256_avx2();
397 unregister_sha256_avx();
398 unregister_sha256_ssse3();
399 goto fail;
Tim Chen8275d1a2013-03-26 13:59:17 -0700400 }
Tim Chen8275d1a2013-03-26 13:59:17 -0700401
tim5dda42f2015-09-16 16:35:23 -0700402 return 0;
403fail:
Tim Chen8275d1a2013-03-26 13:59:17 -0700404 return -ENODEV;
405}
406
407static void __exit sha256_ssse3_mod_fini(void)
408{
tim5dda42f2015-09-16 16:35:23 -0700409 unregister_sha256_ni();
410 unregister_sha256_avx2();
411 unregister_sha256_avx();
412 unregister_sha256_ssse3();
Tim Chen8275d1a2013-03-26 13:59:17 -0700413}
414
415module_init(sha256_ssse3_mod_init);
416module_exit(sha256_ssse3_mod_fini);
417
418MODULE_LICENSE("GPL");
419MODULE_DESCRIPTION("SHA256 Secure Hash Algorithm, Supplemental SSE3 accelerated");
420
Kees Cook5d26a102014-11-20 17:05:53 -0800421MODULE_ALIAS_CRYPTO("sha256");
Stephan Mueller1a445e82016-05-13 14:02:00 +0200422MODULE_ALIAS_CRYPTO("sha256-ssse3");
423MODULE_ALIAS_CRYPTO("sha256-avx");
424MODULE_ALIAS_CRYPTO("sha256-avx2");
Kees Cook5d26a102014-11-20 17:05:53 -0800425MODULE_ALIAS_CRYPTO("sha224");
Stephan Mueller1a445e82016-05-13 14:02:00 +0200426MODULE_ALIAS_CRYPTO("sha224-ssse3");
427MODULE_ALIAS_CRYPTO("sha224-avx");
428MODULE_ALIAS_CRYPTO("sha224-avx2");
429#ifdef CONFIG_AS_SHA256_NI
430MODULE_ALIAS_CRYPTO("sha256-ni");
431MODULE_ALIAS_CRYPTO("sha224-ni");
432#endif