1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
6 #include <crypto/internal/blake2s.h>
7 #include <crypto/internal/simd.h>
8 #include <crypto/internal/hash.h>
10 #include <linux/types.h>
11 #include <linux/jump_label.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <asm/cpufeature.h>
16 #include <asm/fpu/api.h>
17 #include <asm/processor.h>
20 asmlinkage
void blake2s_compress_ssse3(struct blake2s_state
*state
,
21 const u8
*block
, const size_t nblocks
,
23 asmlinkage
void blake2s_compress_avx512(struct blake2s_state
*state
,
24 const u8
*block
, const size_t nblocks
,
27 static __ro_after_init
DEFINE_STATIC_KEY_FALSE(blake2s_use_ssse3
);
28 static __ro_after_init
DEFINE_STATIC_KEY_FALSE(blake2s_use_avx512
);
30 void blake2s_compress_arch(struct blake2s_state
*state
,
31 const u8
*block
, size_t nblocks
,
34 /* SIMD disables preemption, so relax after processing each page. */
35 BUILD_BUG_ON(PAGE_SIZE
/ BLAKE2S_BLOCK_SIZE
< 8);
37 if (!static_branch_likely(&blake2s_use_ssse3
) || !crypto_simd_usable()) {
38 blake2s_compress_generic(state
, block
, nblocks
, inc
);
43 const size_t blocks
= min_t(size_t, nblocks
,
44 PAGE_SIZE
/ BLAKE2S_BLOCK_SIZE
);
47 if (IS_ENABLED(CONFIG_AS_AVX512
) &&
48 static_branch_likely(&blake2s_use_avx512
))
49 blake2s_compress_avx512(state
, block
, blocks
, inc
);
51 blake2s_compress_ssse3(state
, block
, blocks
, inc
);
57 block
+= blocks
* BLAKE2S_BLOCK_SIZE
;
60 EXPORT_SYMBOL(blake2s_compress_arch
);
62 static int crypto_blake2s_setkey(struct crypto_shash
*tfm
, const u8
*key
,
65 struct blake2s_tfm_ctx
*tctx
= crypto_shash_ctx(tfm
);
67 if (keylen
== 0 || keylen
> BLAKE2S_KEY_SIZE
)
70 memcpy(tctx
->key
, key
, keylen
);
71 tctx
->keylen
= keylen
;
76 static int crypto_blake2s_init(struct shash_desc
*desc
)
78 struct blake2s_tfm_ctx
*tctx
= crypto_shash_ctx(desc
->tfm
);
79 struct blake2s_state
*state
= shash_desc_ctx(desc
);
80 const int outlen
= crypto_shash_digestsize(desc
->tfm
);
83 blake2s_init_key(state
, outlen
, tctx
->key
, tctx
->keylen
);
85 blake2s_init(state
, outlen
);
90 static int crypto_blake2s_update(struct shash_desc
*desc
, const u8
*in
,
93 struct blake2s_state
*state
= shash_desc_ctx(desc
);
94 const size_t fill
= BLAKE2S_BLOCK_SIZE
- state
->buflen
;
99 memcpy(state
->buf
+ state
->buflen
, in
, fill
);
100 blake2s_compress_arch(state
, state
->buf
, 1, BLAKE2S_BLOCK_SIZE
);
105 if (inlen
> BLAKE2S_BLOCK_SIZE
) {
106 const size_t nblocks
= DIV_ROUND_UP(inlen
, BLAKE2S_BLOCK_SIZE
);
107 /* Hash one less (full) block than strictly possible */
108 blake2s_compress_arch(state
, in
, nblocks
- 1, BLAKE2S_BLOCK_SIZE
);
109 in
+= BLAKE2S_BLOCK_SIZE
* (nblocks
- 1);
110 inlen
-= BLAKE2S_BLOCK_SIZE
* (nblocks
- 1);
112 memcpy(state
->buf
+ state
->buflen
, in
, inlen
);
113 state
->buflen
+= inlen
;
118 static int crypto_blake2s_final(struct shash_desc
*desc
, u8
*out
)
120 struct blake2s_state
*state
= shash_desc_ctx(desc
);
122 blake2s_set_lastblock(state
);
123 memset(state
->buf
+ state
->buflen
, 0,
124 BLAKE2S_BLOCK_SIZE
- state
->buflen
); /* Padding */
125 blake2s_compress_arch(state
, state
->buf
, 1, state
->buflen
);
126 cpu_to_le32_array(state
->h
, ARRAY_SIZE(state
->h
));
127 memcpy(out
, state
->h
, state
->outlen
);
128 memzero_explicit(state
, sizeof(*state
));
133 static struct shash_alg blake2s_algs
[] = {{
134 .base
.cra_name
= "blake2s-128",
135 .base
.cra_driver_name
= "blake2s-128-x86",
136 .base
.cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
137 .base
.cra_ctxsize
= sizeof(struct blake2s_tfm_ctx
),
138 .base
.cra_priority
= 200,
139 .base
.cra_blocksize
= BLAKE2S_BLOCK_SIZE
,
140 .base
.cra_module
= THIS_MODULE
,
142 .digestsize
= BLAKE2S_128_HASH_SIZE
,
143 .setkey
= crypto_blake2s_setkey
,
144 .init
= crypto_blake2s_init
,
145 .update
= crypto_blake2s_update
,
146 .final
= crypto_blake2s_final
,
147 .descsize
= sizeof(struct blake2s_state
),
149 .base
.cra_name
= "blake2s-160",
150 .base
.cra_driver_name
= "blake2s-160-x86",
151 .base
.cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
152 .base
.cra_ctxsize
= sizeof(struct blake2s_tfm_ctx
),
153 .base
.cra_priority
= 200,
154 .base
.cra_blocksize
= BLAKE2S_BLOCK_SIZE
,
155 .base
.cra_module
= THIS_MODULE
,
157 .digestsize
= BLAKE2S_160_HASH_SIZE
,
158 .setkey
= crypto_blake2s_setkey
,
159 .init
= crypto_blake2s_init
,
160 .update
= crypto_blake2s_update
,
161 .final
= crypto_blake2s_final
,
162 .descsize
= sizeof(struct blake2s_state
),
164 .base
.cra_name
= "blake2s-224",
165 .base
.cra_driver_name
= "blake2s-224-x86",
166 .base
.cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
167 .base
.cra_ctxsize
= sizeof(struct blake2s_tfm_ctx
),
168 .base
.cra_priority
= 200,
169 .base
.cra_blocksize
= BLAKE2S_BLOCK_SIZE
,
170 .base
.cra_module
= THIS_MODULE
,
172 .digestsize
= BLAKE2S_224_HASH_SIZE
,
173 .setkey
= crypto_blake2s_setkey
,
174 .init
= crypto_blake2s_init
,
175 .update
= crypto_blake2s_update
,
176 .final
= crypto_blake2s_final
,
177 .descsize
= sizeof(struct blake2s_state
),
179 .base
.cra_name
= "blake2s-256",
180 .base
.cra_driver_name
= "blake2s-256-x86",
181 .base
.cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
182 .base
.cra_ctxsize
= sizeof(struct blake2s_tfm_ctx
),
183 .base
.cra_priority
= 200,
184 .base
.cra_blocksize
= BLAKE2S_BLOCK_SIZE
,
185 .base
.cra_module
= THIS_MODULE
,
187 .digestsize
= BLAKE2S_256_HASH_SIZE
,
188 .setkey
= crypto_blake2s_setkey
,
189 .init
= crypto_blake2s_init
,
190 .update
= crypto_blake2s_update
,
191 .final
= crypto_blake2s_final
,
192 .descsize
= sizeof(struct blake2s_state
),
195 static int __init
blake2s_mod_init(void)
197 if (!boot_cpu_has(X86_FEATURE_SSSE3
))
200 static_branch_enable(&blake2s_use_ssse3
);
202 if (IS_ENABLED(CONFIG_AS_AVX512
) &&
203 boot_cpu_has(X86_FEATURE_AVX
) &&
204 boot_cpu_has(X86_FEATURE_AVX2
) &&
205 boot_cpu_has(X86_FEATURE_AVX512F
) &&
206 boot_cpu_has(X86_FEATURE_AVX512VL
) &&
207 cpu_has_xfeatures(XFEATURE_MASK_SSE
| XFEATURE_MASK_YMM
|
208 XFEATURE_MASK_AVX512
, NULL
))
209 static_branch_enable(&blake2s_use_avx512
);
211 return IS_REACHABLE(CONFIG_CRYPTO_HASH
) ?
212 crypto_register_shashes(blake2s_algs
,
213 ARRAY_SIZE(blake2s_algs
)) : 0;
216 static void __exit
blake2s_mod_exit(void)
218 if (IS_REACHABLE(CONFIG_CRYPTO_HASH
) && boot_cpu_has(X86_FEATURE_SSSE3
))
219 crypto_unregister_shashes(blake2s_algs
, ARRAY_SIZE(blake2s_algs
));
222 module_init(blake2s_mod_init
);
223 module_exit(blake2s_mod_exit
);
225 MODULE_ALIAS_CRYPTO("blake2s-128");
226 MODULE_ALIAS_CRYPTO("blake2s-128-x86");
227 MODULE_ALIAS_CRYPTO("blake2s-160");
228 MODULE_ALIAS_CRYPTO("blake2s-160-x86");
229 MODULE_ALIAS_CRYPTO("blake2s-224");
230 MODULE_ALIAS_CRYPTO("blake2s-224-x86");
231 MODULE_ALIAS_CRYPTO("blake2s-256");
232 MODULE_ALIAS_CRYPTO("blake2s-256-x86");
233 MODULE_LICENSE("GPL v2");