Skip to content

Commit b6ac1da

Browse files
author
Eric Biggers
committed
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>
1 parent 00d549b commit b6ac1da

6 files changed

Lines changed: 82 additions & 158 deletions

File tree

arch/mips/cavium-octeon/crypto/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@
66
obj-y += octeon-crypto.o
77

88
obj-$(CONFIG_CRYPTO_MD5_OCTEON) += octeon-md5.o
9-
obj-$(CONFIG_CRYPTO_SHA1_OCTEON) += octeon-sha1.o

arch/mips/cavium-octeon/crypto/octeon-sha1.c

Lines changed: 0 additions & 146 deletions
This file was deleted.

arch/mips/configs/cavium_octeon_defconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ CONFIG_SECURITY_NETWORK=y
156156
CONFIG_CRYPTO_CBC=y
157157
CONFIG_CRYPTO_HMAC=y
158158
CONFIG_CRYPTO_MD5_OCTEON=y
159-
CONFIG_CRYPTO_SHA1_OCTEON=m
160159
CONFIG_CRYPTO_DES=y
161160
CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
162161
CONFIG_DEBUG_FS=y

arch/mips/crypto/Kconfig

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,4 @@ config CRYPTO_MD5_OCTEON
1212

1313
Architecture: mips OCTEON using crypto instructions, when available
1414

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-
2515
endmenu

lib/crypto/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ config CRYPTO_LIB_SHA1_ARCH
148148
depends on CRYPTO_LIB_SHA1 && !UML
149149
default y if ARM
150150
default y if ARM64 && KERNEL_MODE_NEON
151+
default y if MIPS && CPU_CAVIUM_OCTEON
151152

152153
config CRYPTO_LIB_SHA256
153154
tristate

lib/crypto/mips/sha1.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}

0 commit comments

Comments
 (0)