Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

crypto: mips/sha256 - implement library instead of shash

Instead of providing crypto_shash algorithms for the arch-optimized
SHA-256 code, instead implement the SHA-256 library. This is much
simpler, it makes the SHA-256 library functions be arch-optimized, and
it fixes the longstanding issue where the arch-optimized SHA-256 was
disabled by default. SHA-256 still remains available through
crypto_shash, but individual architectures no longer need to handle it.

Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Eric Biggers and committed by
Herbert Xu
b67b6f9a 6e36be51

+35 -121
+6
arch/mips/cavium-octeon/Kconfig
··· 23 23 legally range is from zero to 54 cache blocks (i.e. CVMSEG LM is 24 24 between zero and 6192 bytes). 25 25 26 + config CRYPTO_SHA256_OCTEON 27 + tristate 28 + default CRYPTO_LIB_SHA256 29 + select CRYPTO_ARCH_HAVE_LIB_SHA256 30 + select CRYPTO_LIB_SHA256_GENERIC 31 + 26 32 endif # CPU_CAVIUM_OCTEON 27 33 28 34 if CAVIUM_OCTEON_SOC
+29 -110
arch/mips/cavium-octeon/crypto/octeon-sha256.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-or-later 2 2 /* 3 - * Cryptographic API. 4 - * 5 - * SHA-224 and SHA-256 Secure Hash Algorithm. 3 + * SHA-256 Secure Hash Algorithm. 6 4 * 7 5 * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>. 8 6 * ··· 13 15 */ 14 16 15 17 #include <asm/octeon/octeon.h> 16 - #include <crypto/internal/hash.h> 17 - #include <crypto/sha2.h> 18 - #include <crypto/sha256_base.h> 18 + #include <crypto/internal/sha2.h> 19 19 #include <linux/kernel.h> 20 20 #include <linux/module.h> 21 21 ··· 23 27 * We pass everything as 64-bit. OCTEON can handle misaligned data. 24 28 */ 25 29 26 - static void octeon_sha256_store_hash(struct crypto_sha256_state *sctx) 30 + void sha256_blocks_arch(u32 state[SHA256_STATE_WORDS], 31 + const u8 *data, size_t nblocks) 27 32 { 28 - u64 *hash = (u64 *)sctx->state; 33 + struct octeon_cop2_state cop2_state; 34 + u64 *state64 = (u64 *)state; 35 + unsigned long flags; 29 36 30 - write_octeon_64bit_hash_dword(hash[0], 0); 31 - write_octeon_64bit_hash_dword(hash[1], 1); 32 - write_octeon_64bit_hash_dword(hash[2], 2); 33 - write_octeon_64bit_hash_dword(hash[3], 3); 34 - } 37 + if (!octeon_has_crypto()) 38 + return sha256_blocks_generic(state, data, nblocks); 35 39 36 - static void octeon_sha256_read_hash(struct crypto_sha256_state *sctx) 37 - { 38 - u64 *hash = (u64 *)sctx->state; 40 + flags = octeon_crypto_enable(&cop2_state); 41 + write_octeon_64bit_hash_dword(state64[0], 0); 42 + write_octeon_64bit_hash_dword(state64[1], 1); 43 + write_octeon_64bit_hash_dword(state64[2], 2); 44 + write_octeon_64bit_hash_dword(state64[3], 3); 39 45 40 - hash[0] = read_octeon_64bit_hash_dword(0); 41 - hash[1] = read_octeon_64bit_hash_dword(1); 42 - hash[2] = read_octeon_64bit_hash_dword(2); 43 - hash[3] = read_octeon_64bit_hash_dword(3); 44 - } 45 - 46 - static void octeon_sha256_transform(struct crypto_sha256_state *sctx, 47 - const u8 *src, int blocks) 48 - { 49 46 do { 50 - const u64 *block = (const u64 *)src; 47 + const u64 *block = (const u64 *)data; 51 48 52 49 write_octeon_64bit_block_dword(block[0], 0); 53 50 write_octeon_64bit_block_dword(block[1], 1); ··· 51 62 write_octeon_64bit_block_dword(block[6], 6); 52 63 octeon_sha256_start(block[7]); 53 64 54 - src += SHA256_BLOCK_SIZE; 55 - } while (--blocks); 56 - } 65 + data += SHA256_BLOCK_SIZE; 66 + } while (--nblocks); 57 67 58 - static int octeon_sha256_update(struct shash_desc *desc, const u8 *data, 59 - unsigned int len) 68 + state64[0] = read_octeon_64bit_hash_dword(0); 69 + state64[1] = read_octeon_64bit_hash_dword(1); 70 + state64[2] = read_octeon_64bit_hash_dword(2); 71 + state64[3] = read_octeon_64bit_hash_dword(3); 72 + octeon_crypto_disable(&cop2_state, flags); 73 + } 74 + EXPORT_SYMBOL(sha256_blocks_arch); 75 + 76 + bool sha256_is_arch_optimized(void) 60 77 { 61 - struct crypto_sha256_state *sctx = shash_desc_ctx(desc); 62 - struct octeon_cop2_state state; 63 - unsigned long flags; 64 - int remain; 65 - 66 - flags = octeon_crypto_enable(&state); 67 - octeon_sha256_store_hash(sctx); 68 - 69 - remain = sha256_base_do_update_blocks(desc, data, len, 70 - octeon_sha256_transform); 71 - 72 - octeon_sha256_read_hash(sctx); 73 - octeon_crypto_disable(&state, flags); 74 - return remain; 78 + return octeon_has_crypto(); 75 79 } 76 - 77 - static int octeon_sha256_finup(struct shash_desc *desc, const u8 *src, 78 - unsigned int len, u8 *out) 79 - { 80 - struct crypto_sha256_state *sctx = shash_desc_ctx(desc); 81 - struct octeon_cop2_state state; 82 - unsigned long flags; 83 - 84 - flags = octeon_crypto_enable(&state); 85 - octeon_sha256_store_hash(sctx); 86 - 87 - sha256_base_do_finup(desc, src, len, octeon_sha256_transform); 88 - 89 - octeon_sha256_read_hash(sctx); 90 - octeon_crypto_disable(&state, flags); 91 - return sha256_base_finish(desc, out); 92 - } 93 - 94 - static struct shash_alg octeon_sha256_algs[2] = { { 95 - .digestsize = SHA256_DIGEST_SIZE, 96 - .init = sha256_base_init, 97 - .update = octeon_sha256_update, 98 - .finup = octeon_sha256_finup, 99 - .descsize = sizeof(struct crypto_sha256_state), 100 - .base = { 101 - .cra_name = "sha256", 102 - .cra_driver_name= "octeon-sha256", 103 - .cra_priority = OCTEON_CR_OPCODE_PRIORITY, 104 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 105 - .cra_blocksize = SHA256_BLOCK_SIZE, 106 - .cra_module = THIS_MODULE, 107 - } 108 - }, { 109 - .digestsize = SHA224_DIGEST_SIZE, 110 - .init = sha224_base_init, 111 - .update = octeon_sha256_update, 112 - .finup = octeon_sha256_finup, 113 - .descsize = sizeof(struct crypto_sha256_state), 114 - .base = { 115 - .cra_name = "sha224", 116 - .cra_driver_name= "octeon-sha224", 117 - .cra_priority = OCTEON_CR_OPCODE_PRIORITY, 118 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 119 - .cra_blocksize = SHA224_BLOCK_SIZE, 120 - .cra_module = THIS_MODULE, 121 - } 122 - } }; 123 - 124 - static int __init octeon_sha256_mod_init(void) 125 - { 126 - if (!octeon_has_crypto()) 127 - return -ENOTSUPP; 128 - return crypto_register_shashes(octeon_sha256_algs, 129 - ARRAY_SIZE(octeon_sha256_algs)); 130 - } 131 - 132 - static void __exit octeon_sha256_mod_fini(void) 133 - { 134 - crypto_unregister_shashes(octeon_sha256_algs, 135 - ARRAY_SIZE(octeon_sha256_algs)); 136 - } 137 - 138 - module_init(octeon_sha256_mod_init); 139 - module_exit(octeon_sha256_mod_fini); 80 + EXPORT_SYMBOL(sha256_is_arch_optimized); 140 81 141 82 MODULE_LICENSE("GPL"); 142 - MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm (OCTEON)"); 83 + MODULE_DESCRIPTION("SHA-256 Secure Hash Algorithm (OCTEON)"); 143 84 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
-1
arch/mips/configs/cavium_octeon_defconfig
··· 157 157 CONFIG_CRYPTO_HMAC=y 158 158 CONFIG_CRYPTO_MD5_OCTEON=y 159 159 CONFIG_CRYPTO_SHA1_OCTEON=m 160 - CONFIG_CRYPTO_SHA256_OCTEON=m 161 160 CONFIG_CRYPTO_SHA512_OCTEON=m 162 161 CONFIG_CRYPTO_DES=y 163 162 CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
-10
arch/mips/crypto/Kconfig
··· 22 22 23 23 Architecture: mips OCTEON 24 24 25 - config CRYPTO_SHA256_OCTEON 26 - tristate "Hash functions: SHA-224 and SHA-256 (OCTEON)" 27 - depends on CPU_CAVIUM_OCTEON 28 - select CRYPTO_SHA256 29 - select CRYPTO_HASH 30 - help 31 - SHA-224 and SHA-256 secure hash algorithms (FIPS 180) 32 - 33 - Architecture: mips OCTEON using crypto instructions, when available 34 - 35 25 config CRYPTO_SHA512_OCTEON 36 26 tristate "Hash functions: SHA-384 and SHA-512 (OCTEON)" 37 27 depends on CPU_CAVIUM_OCTEON