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

lib/crypto: mips/sha1: Migrate optimized code into library

Instead of exposing the mips-optimized SHA-1 code via mips-specific
crypto_shash algorithms, instead just implement the sha1_blocks()
library function. This is much simpler, it makes the SHA-1 library
functions be mips-optimized, and it fixes the longstanding issue where
the mips-optimized SHA-1 code was disabled by default. SHA-1 still
remains available through crypto_shash, but individual architectures no
longer need to handle it.

Note: to see the diff from arch/mips/cavium-octeon/crypto/octeon-sha1.c
to lib/crypto/mips/sha1.h, view this commit with 'git show -M10'.

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20250712232329.818226-10-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+82 -158
-1
arch/mips/cavium-octeon/crypto/Makefile
··· 6 6 obj-y += octeon-crypto.o 7 7 8 8 obj-$(CONFIG_CRYPTO_MD5_OCTEON) += octeon-md5.o 9 - obj-$(CONFIG_CRYPTO_SHA1_OCTEON) += octeon-sha1.o
-146
arch/mips/cavium-octeon/crypto/octeon-sha1.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-or-later 2 - /* 3 - * Cryptographic API. 4 - * 5 - * SHA1 Secure Hash Algorithm. 6 - * 7 - * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>. 8 - * 9 - * Based on crypto/sha1_generic.c, which is: 10 - * 11 - * Copyright (c) Alan Smithee. 12 - * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 13 - * Copyright (c) Jean-Francois Dive <jef@linuxbe.org> 14 - */ 15 - 16 - #include <asm/octeon/crypto.h> 17 - #include <asm/octeon/octeon.h> 18 - #include <crypto/internal/hash.h> 19 - #include <crypto/sha1.h> 20 - #include <crypto/sha1_base.h> 21 - #include <linux/errno.h> 22 - #include <linux/kernel.h> 23 - #include <linux/module.h> 24 - 25 - /* 26 - * We pass everything as 64-bit. OCTEON can handle misaligned data. 27 - */ 28 - 29 - static void octeon_sha1_store_hash(struct sha1_state *sctx) 30 - { 31 - u64 *hash = (u64 *)sctx->state; 32 - union { 33 - u32 word[2]; 34 - u64 dword; 35 - } hash_tail = { { sctx->state[4], } }; 36 - 37 - write_octeon_64bit_hash_dword(hash[0], 0); 38 - write_octeon_64bit_hash_dword(hash[1], 1); 39 - write_octeon_64bit_hash_dword(hash_tail.dword, 2); 40 - memzero_explicit(&hash_tail.word[0], sizeof(hash_tail.word[0])); 41 - } 42 - 43 - static void octeon_sha1_read_hash(struct sha1_state *sctx) 44 - { 45 - u64 *hash = (u64 *)sctx->state; 46 - union { 47 - u32 word[2]; 48 - u64 dword; 49 - } hash_tail; 50 - 51 - hash[0] = read_octeon_64bit_hash_dword(0); 52 - hash[1] = read_octeon_64bit_hash_dword(1); 53 - hash_tail.dword = read_octeon_64bit_hash_dword(2); 54 - sctx->state[4] = hash_tail.word[0]; 55 - memzero_explicit(&hash_tail.dword, sizeof(hash_tail.dword)); 56 - } 57 - 58 - static void octeon_sha1_transform(struct sha1_state *sctx, const u8 *src, 59 - int blocks) 60 - { 61 - do { 62 - const u64 *block = (const u64 *)src; 63 - 64 - write_octeon_64bit_block_dword(block[0], 0); 65 - write_octeon_64bit_block_dword(block[1], 1); 66 - write_octeon_64bit_block_dword(block[2], 2); 67 - write_octeon_64bit_block_dword(block[3], 3); 68 - write_octeon_64bit_block_dword(block[4], 4); 69 - write_octeon_64bit_block_dword(block[5], 5); 70 - write_octeon_64bit_block_dword(block[6], 6); 71 - octeon_sha1_start(block[7]); 72 - 73 - src += SHA1_BLOCK_SIZE; 74 - } while (--blocks); 75 - } 76 - 77 - static int octeon_sha1_update(struct shash_desc *desc, const u8 *data, 78 - unsigned int len) 79 - { 80 - struct sha1_state *sctx = shash_desc_ctx(desc); 81 - struct octeon_cop2_state state; 82 - unsigned long flags; 83 - int remain; 84 - 85 - flags = octeon_crypto_enable(&state); 86 - octeon_sha1_store_hash(sctx); 87 - 88 - remain = sha1_base_do_update_blocks(desc, data, len, 89 - octeon_sha1_transform); 90 - 91 - octeon_sha1_read_hash(sctx); 92 - octeon_crypto_disable(&state, flags); 93 - return remain; 94 - } 95 - 96 - static int octeon_sha1_finup(struct shash_desc *desc, const u8 *src, 97 - unsigned int len, u8 *out) 98 - { 99 - struct sha1_state *sctx = shash_desc_ctx(desc); 100 - struct octeon_cop2_state state; 101 - unsigned long flags; 102 - 103 - flags = octeon_crypto_enable(&state); 104 - octeon_sha1_store_hash(sctx); 105 - 106 - sha1_base_do_finup(desc, src, len, octeon_sha1_transform); 107 - 108 - octeon_sha1_read_hash(sctx); 109 - octeon_crypto_disable(&state, flags); 110 - return sha1_base_finish(desc, out); 111 - } 112 - 113 - static struct shash_alg octeon_sha1_alg = { 114 - .digestsize = SHA1_DIGEST_SIZE, 115 - .init = sha1_base_init, 116 - .update = octeon_sha1_update, 117 - .finup = octeon_sha1_finup, 118 - .descsize = SHA1_STATE_SIZE, 119 - .base = { 120 - .cra_name = "sha1", 121 - .cra_driver_name= "octeon-sha1", 122 - .cra_priority = OCTEON_CR_OPCODE_PRIORITY, 123 - .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY, 124 - .cra_blocksize = SHA1_BLOCK_SIZE, 125 - .cra_module = THIS_MODULE, 126 - } 127 - }; 128 - 129 - static int __init octeon_sha1_mod_init(void) 130 - { 131 - if (!octeon_has_crypto()) 132 - return -ENOTSUPP; 133 - return crypto_register_shash(&octeon_sha1_alg); 134 - } 135 - 136 - static void __exit octeon_sha1_mod_fini(void) 137 - { 138 - crypto_unregister_shash(&octeon_sha1_alg); 139 - } 140 - 141 - module_init(octeon_sha1_mod_init); 142 - module_exit(octeon_sha1_mod_fini); 143 - 144 - MODULE_LICENSE("GPL"); 145 - MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm (OCTEON)"); 146 - MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
-1
arch/mips/configs/cavium_octeon_defconfig
··· 156 156 CONFIG_CRYPTO_CBC=y 157 157 CONFIG_CRYPTO_HMAC=y 158 158 CONFIG_CRYPTO_MD5_OCTEON=y 159 - CONFIG_CRYPTO_SHA1_OCTEON=m 160 159 CONFIG_CRYPTO_DES=y 161 160 CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y 162 161 CONFIG_DEBUG_FS=y
-10
arch/mips/crypto/Kconfig
··· 12 12 13 13 Architecture: mips OCTEON using crypto instructions, when available 14 14 15 - config CRYPTO_SHA1_OCTEON 16 - tristate "Hash functions: SHA-1 (OCTEON)" 17 - depends on CPU_CAVIUM_OCTEON 18 - select CRYPTO_SHA1 19 - select CRYPTO_HASH 20 - help 21 - SHA-1 secure hash algorithm (FIPS 180) 22 - 23 - Architecture: mips OCTEON 24 - 25 15 endmenu
+1
lib/crypto/Kconfig
··· 148 148 depends on CRYPTO_LIB_SHA1 && !UML 149 149 default y if ARM 150 150 default y if ARM64 && KERNEL_MODE_NEON 151 + default y if MIPS && CPU_CAVIUM_OCTEON 151 152 152 153 config CRYPTO_LIB_SHA256 153 154 tristate
+81
lib/crypto/mips/sha1.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Cryptographic API. 4 + * 5 + * SHA1 Secure Hash Algorithm. 6 + * 7 + * Adapted for OCTEON by Aaro Koskinen <aaro.koskinen@iki.fi>. 8 + * 9 + * Based on crypto/sha1_generic.c, which is: 10 + * 11 + * Copyright (c) Alan Smithee. 12 + * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> 13 + * Copyright (c) Jean-Francois Dive <jef@linuxbe.org> 14 + */ 15 + 16 + #include <asm/octeon/crypto.h> 17 + #include <asm/octeon/octeon.h> 18 + 19 + /* 20 + * We pass everything as 64-bit. OCTEON can handle misaligned data. 21 + */ 22 + 23 + static void octeon_sha1_store_hash(struct sha1_block_state *state) 24 + { 25 + u64 *hash = (u64 *)&state->h[0]; 26 + union { 27 + u32 word[2]; 28 + u64 dword; 29 + } hash_tail = { { state->h[4], } }; 30 + 31 + write_octeon_64bit_hash_dword(hash[0], 0); 32 + write_octeon_64bit_hash_dword(hash[1], 1); 33 + write_octeon_64bit_hash_dword(hash_tail.dword, 2); 34 + memzero_explicit(&hash_tail.word[0], sizeof(hash_tail.word[0])); 35 + } 36 + 37 + static void octeon_sha1_read_hash(struct sha1_block_state *state) 38 + { 39 + u64 *hash = (u64 *)&state->h[0]; 40 + union { 41 + u32 word[2]; 42 + u64 dword; 43 + } hash_tail; 44 + 45 + hash[0] = read_octeon_64bit_hash_dword(0); 46 + hash[1] = read_octeon_64bit_hash_dword(1); 47 + hash_tail.dword = read_octeon_64bit_hash_dword(2); 48 + state->h[4] = hash_tail.word[0]; 49 + memzero_explicit(&hash_tail.dword, sizeof(hash_tail.dword)); 50 + } 51 + 52 + static void sha1_blocks(struct sha1_block_state *state, 53 + const u8 *data, size_t nblocks) 54 + { 55 + struct octeon_cop2_state cop2_state; 56 + unsigned long flags; 57 + 58 + if (!octeon_has_crypto()) 59 + return sha1_blocks_generic(state, data, nblocks); 60 + 61 + flags = octeon_crypto_enable(&cop2_state); 62 + octeon_sha1_store_hash(state); 63 + 64 + do { 65 + const u64 *block = (const u64 *)data; 66 + 67 + write_octeon_64bit_block_dword(block[0], 0); 68 + write_octeon_64bit_block_dword(block[1], 1); 69 + write_octeon_64bit_block_dword(block[2], 2); 70 + write_octeon_64bit_block_dword(block[3], 3); 71 + write_octeon_64bit_block_dword(block[4], 4); 72 + write_octeon_64bit_block_dword(block[5], 5); 73 + write_octeon_64bit_block_dword(block[6], 6); 74 + octeon_sha1_start(block[7]); 75 + 76 + data += SHA1_BLOCK_SIZE; 77 + } while (--nblocks); 78 + 79 + octeon_sha1_read_hash(state); 80 + octeon_crypto_disable(&cop2_state, flags); 81 + }