4 * Glue code for the SHA1 Secure Hash Algorithm assembler implementation using
5 * Supplemental SSE3 instructions.
7 * This file is based on sha1_generic.c
9 * Copyright (c) Alan Smithee.
10 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
11 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
12 * Copyright (c) Mathias Krause <minipli@googlemail.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)
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <crypto/internal/hash.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
27 #include <linux/cryptohash.h>
28 #include <linux/types.h>
29 #include <crypto/sha.h>
30 #include <asm/byteorder.h>
33 #include <asm/xsave.h>
36 asmlinkage
void sha1_transform_ssse3(u32
*digest
, const char *data
,
38 #ifdef SHA1_ENABLE_AVX_SUPPORT
39 asmlinkage
void sha1_transform_avx(u32
*digest
, const char *data
,
43 static asmlinkage
void (*sha1_transform_asm
)(u32
*, const char *, unsigned int);
46 static int sha1_ssse3_init(struct shash_desc
*desc
)
48 struct sha1_state
*sctx
= shash_desc_ctx(desc
);
50 *sctx
= (struct sha1_state
){
51 .state
= { SHA1_H0
, SHA1_H1
, SHA1_H2
, SHA1_H3
, SHA1_H4
},
57 static int __sha1_ssse3_update(struct shash_desc
*desc
, const u8
*data
,
58 unsigned int len
, unsigned int partial
)
60 struct sha1_state
*sctx
= shash_desc_ctx(desc
);
61 unsigned int done
= 0;
66 done
= SHA1_BLOCK_SIZE
- partial
;
67 memcpy(sctx
->buffer
+ partial
, data
, done
);
68 sha1_transform_asm(sctx
->state
, sctx
->buffer
, 1);
71 if (len
- done
>= SHA1_BLOCK_SIZE
) {
72 const unsigned int rounds
= (len
- done
) / SHA1_BLOCK_SIZE
;
74 sha1_transform_asm(sctx
->state
, data
+ done
, rounds
);
75 done
+= rounds
* SHA1_BLOCK_SIZE
;
78 memcpy(sctx
->buffer
, data
+ done
, len
- done
);
83 static int sha1_ssse3_update(struct shash_desc
*desc
, const u8
*data
,
86 struct sha1_state
*sctx
= shash_desc_ctx(desc
);
87 unsigned int partial
= sctx
->count
% SHA1_BLOCK_SIZE
;
90 /* Handle the fast case right here */
91 if (partial
+ len
< SHA1_BLOCK_SIZE
) {
93 memcpy(sctx
->buffer
+ partial
, data
, len
);
98 if (!irq_fpu_usable()) {
99 res
= crypto_sha1_update(desc
, data
, len
);
102 res
= __sha1_ssse3_update(desc
, data
, len
, partial
);
110 /* Add padding and return the message digest. */
111 static int sha1_ssse3_final(struct shash_desc
*desc
, u8
*out
)
113 struct sha1_state
*sctx
= shash_desc_ctx(desc
);
114 unsigned int i
, index
, padlen
;
115 __be32
*dst
= (__be32
*)out
;
117 static const u8 padding
[SHA1_BLOCK_SIZE
] = { 0x80, };
119 bits
= cpu_to_be64(sctx
->count
<< 3);
121 /* Pad out to 56 mod 64 and append length */
122 index
= sctx
->count
% SHA1_BLOCK_SIZE
;
123 padlen
= (index
< 56) ? (56 - index
) : ((SHA1_BLOCK_SIZE
+56) - index
);
124 if (!irq_fpu_usable()) {
125 crypto_sha1_update(desc
, padding
, padlen
);
126 crypto_sha1_update(desc
, (const u8
*)&bits
, sizeof(bits
));
129 /* We need to fill a whole block for __sha1_ssse3_update() */
131 sctx
->count
+= padlen
;
132 memcpy(sctx
->buffer
+ index
, padding
, padlen
);
134 __sha1_ssse3_update(desc
, padding
, padlen
, index
);
136 __sha1_ssse3_update(desc
, (const u8
*)&bits
, sizeof(bits
), 56);
140 /* Store state in digest */
141 for (i
= 0; i
< 5; i
++)
142 dst
[i
] = cpu_to_be32(sctx
->state
[i
]);
145 memset(sctx
, 0, sizeof(*sctx
));
150 static int sha1_ssse3_export(struct shash_desc
*desc
, void *out
)
152 struct sha1_state
*sctx
= shash_desc_ctx(desc
);
154 memcpy(out
, sctx
, sizeof(*sctx
));
159 static int sha1_ssse3_import(struct shash_desc
*desc
, const void *in
)
161 struct sha1_state
*sctx
= shash_desc_ctx(desc
);
163 memcpy(sctx
, in
, sizeof(*sctx
));
168 static struct shash_alg alg
= {
169 .digestsize
= SHA1_DIGEST_SIZE
,
170 .init
= sha1_ssse3_init
,
171 .update
= sha1_ssse3_update
,
172 .final
= sha1_ssse3_final
,
173 .export
= sha1_ssse3_export
,
174 .import
= sha1_ssse3_import
,
175 .descsize
= sizeof(struct sha1_state
),
176 .statesize
= sizeof(struct sha1_state
),
179 .cra_driver_name
= "sha1-ssse3",
181 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
182 .cra_blocksize
= SHA1_BLOCK_SIZE
,
183 .cra_module
= THIS_MODULE
,
187 #ifdef SHA1_ENABLE_AVX_SUPPORT
188 static bool __init
avx_usable(void)
192 if (!cpu_has_avx
|| !cpu_has_osxsave
)
195 xcr0
= xgetbv(XCR_XFEATURE_ENABLED_MASK
);
196 if ((xcr0
& (XSTATE_SSE
| XSTATE_YMM
)) != (XSTATE_SSE
| XSTATE_YMM
)) {
197 pr_info("AVX detected but unusable.\n");
206 static int __init
sha1_ssse3_mod_init(void)
208 /* test for SSSE3 first */
210 sha1_transform_asm
= sha1_transform_ssse3
;
212 #ifdef SHA1_ENABLE_AVX_SUPPORT
213 /* allow AVX to override SSSE3, it's a little faster */
215 sha1_transform_asm
= sha1_transform_avx
;
218 if (sha1_transform_asm
) {
219 pr_info("Using %s optimized SHA-1 implementation\n",
220 sha1_transform_asm
== sha1_transform_ssse3
? "SSSE3"
222 return crypto_register_shash(&alg
);
224 pr_info("Neither AVX nor SSSE3 is available/usable.\n");
229 static void __exit
sha1_ssse3_mod_fini(void)
231 crypto_unregister_shash(&alg
);
234 module_init(sha1_ssse3_mod_init
);
235 module_exit(sha1_ssse3_mod_fini
);
237 MODULE_LICENSE("GPL");
238 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, Supplemental SSE3 accelerated");
240 MODULE_ALIAS("sha1");