xref: /linux/lib/crypto/arm64/sha3.h (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <asm/simd.h>
11 #include <linux/cpufeature.h>
12 
13 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_sha3);
14 
15 asmlinkage void sha3_ce_transform(struct sha3_state *state, const u8 *data,
16 				  size_t nblocks, size_t block_size);
17 
18 static void sha3_absorb_blocks(struct sha3_state *state, const u8 *data,
19 			       size_t nblocks, size_t block_size)
20 {
21 	if (static_branch_likely(&have_sha3) && likely(may_use_simd())) {
22 		scoped_ksimd()
23 			sha3_ce_transform(state, data, nblocks, block_size);
24 	} else {
25 		sha3_absorb_blocks_generic(state, data, nblocks, block_size);
26 	}
27 }
28 
29 static void sha3_keccakf(struct sha3_state *state)
30 {
31 	if (static_branch_likely(&have_sha3) && likely(may_use_simd())) {
32 		/*
33 		 * Passing zeroes into sha3_ce_transform() gives the plain
34 		 * Keccak-f permutation, which is what we want here.  Any
35 		 * supported block size may be used.  Use SHA3_512_BLOCK_SIZE
36 		 * since it's the shortest.
37 		 */
38 		static const u8 zeroes[SHA3_512_BLOCK_SIZE];
39 
40 		scoped_ksimd()
41 			sha3_ce_transform(state, zeroes, 1, sizeof(zeroes));
42 	} else {
43 		sha3_keccakf_generic(state);
44 	}
45 }
46 
47 #define sha3_mod_init_arch sha3_mod_init_arch
48 static void sha3_mod_init_arch(void)
49 {
50 	if (cpu_have_named_feature(SHA3))
51 		static_branch_enable(&have_sha3);
52 }
53