MD5 library functions
Subsystems (7)
Eric Biggers
maintainerv2 cover MD5 library functions
This series is targeting libcrypto-next and can also be retrieved from:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git md5-lib-v2
This series introduces a library API for MD5 and HMAC-MD5 and reimplements the crypto_shash "md5" and "hmac(md5)" on top of it.
The library API will also be usable directly by various in-kernel users that are stuck with MD5 due to having to implement legacy protocols.
This should again look quite boring and familiar, as it mirrors the SHA-1 and SHA-2 changes closely.
Changed in v2:
- Kept the architecture-optimized MD5 code, since unfortunately there were objections to removing it.
Eric Biggers (7):
lib/crypto: md5: Add MD5 and HMAC-MD5 library functions lib/crypto: mips/md5: Migrate optimized code into library
mips: cavium-octeon: Move octeon-crypto.c into parent dir lib/crypto: powerpc/md5: Migrate optimized code into library lib/crypto: sparc/md5: Migrate optimized code into library
crypto: md5 - Wrap library and add HMAC support lib/crypto: tests: Add KUnit tests for MD5 and HMAC-MD5
arch/mips/cavium-octeon/Makefile | 2 +-
arch/mips/cavium-octeon/crypto/Makefile | 8 -
arch/mips/cavium-octeon/crypto/octeon-md5.c | 214 -----------
.../{crypto => }/octeon-crypto.c | 0
arch/mips/configs/cavium_octeon_defconfig | 1 -
arch/mips/crypto/Kconfig | 10 -
arch/powerpc/configs/powernv_defconfig | 1 -
arch/powerpc/configs/ppc64_defconfig | 1 -
arch/powerpc/crypto/Kconfig | 8 -
arch/powerpc/crypto/Makefile | 2 -
arch/powerpc/crypto/md5-glue.c | 99 -----
arch/sparc/crypto/Kconfig | 10 -
arch/sparc/crypto/Makefile | 4 -
arch/sparc/crypto/md5_glue.c | 174 ---------
crypto/Kconfig | 2 +-
crypto/md5.c | 359 ++++++++----------
crypto/testmgr.c | 3 +
drivers/crypto/img-hash.c | 2 +-
include/crypto/md5.h | 181 ++++++++-
lib/crypto/Kconfig | 13 +
lib/crypto/Makefile | 12 +
lib/crypto/md5.c | 322 ++++++++++++++++
lib/crypto/mips/md5.h | 65 ++++
.../crypto => lib/crypto/powerpc}/md5-asm.S | 0
lib/crypto/powerpc/md5.h | 12 +
lib/crypto/sparc/md5.h | 48 +++
.../crypto => lib/crypto/sparc}/md5_asm.S | 0
lib/crypto/tests/Kconfig | 10 +
lib/crypto/tests/Makefile | 1 +
lib/crypto/tests/md5-testvecs.h | 186 +++++++++
lib/crypto/tests/md5_kunit.c | 39 ++
31 files changed, 1060 insertions(+), 729 deletions(-)
delete mode 100644 arch/mips/cavium-octeon/crypto/Makefile
delete mode 100644 arch/mips/cavium-octeon/crypto/octeon-md5.c
rename arch/mips/cavium-octeon/{crypto => }/octeon-crypto.c (100%)
delete mode 100644 arch/powerpc/crypto/md5-glue.c
delete mode 100644 arch/sparc/crypto/md5_glue.c
create mode 100644 lib/crypto/md5.c
create mode 100644 lib/crypto/mips/md5.h
rename {arch/powerpc/crypto => lib/crypto/powerpc}/md5-asm.S (100%)
create mode 100644 lib/crypto/powerpc/md5.h
create mode 100644 lib/crypto/sparc/md5.h
rename {arch/sparc/crypto => lib/crypto/sparc}/md5_asm.S (100%)
create mode 100644 lib/crypto/tests/md5-testvecs.h
create mode 100644 lib/crypto/tests/md5_kunit.cbase-commit: 186f3edfdd41f2ae87fc40a9ccba52a3bf930994
Signature · 1 line
2.50.1
Eric Biggers
maintainerPATCH v2 1/7 lib/crypto: md5: Add MD5 and HMAC-MD5 library functions
Add library functions for MD5, including HMAC support. The MD5 implementation is derived from crypto/md5.c. This closely mirrors the corresponding SHA-1 and SHA-2 changes.
Like SHA-1 and SHA-2, support for architecture-optimized MD5 implementations is included. I originally proposed dropping those, but unfortunately there is an AF_ALG user of the PowerPC MD5 code
(https://lore.kernel.org/r/[email protected]/),
and dropping that code would be viewed as a performance regression. We don't add new software algorithm implementations purely for AF_ALG, as escalating to kernel mode merely to do calculations that could be done in userspace is inefficient and is completely the wrong design. But since this one already existed, it gets grandfathered in for now. An objection was also raised to dropping the SPARC64 MD5 code because it utilizes the CPU's direct support for MD5, although it remains unclear that anyone is using that. Regardless, we'll keep these around for now.
Note that while MD5 is a legacy algorithm that is vulnerable to practical collision attacks, it still has various in-kernel users that implement legacy protocols. Switching to a simple library API, which is the way the code should have been organized originally, will greatly simplify their code. For example:
MD5:
drivers/md/dm-crypt.c (for lmk IV generation)
fs/nfsd/nfs4recover.c
fs/ecryptfs/
fs/smb/client/
net/{ipv4,ipv6}/ (for TCP-MD5 signatures)
HMAC-MD5:
fs/smb/client/
fs/smb/server/(Also net/sctp/ if it continues using HMAC-MD5 for cookie generation. However, that use case has the flexibility to upgrade to a more modern algorithm, which I'll be proposing instead.)
As usual, the "md5" and "hmac(md5)" crypto_shash algorithms will also be reimplemented on top of these library functions. For "hmac(md5)" this will provide a faster, more streamlined implementation.
--- include/crypto/md5.h | 181 +++++++++++++++++++++++- lib/crypto/Kconfig | 10 ++ lib/crypto/Makefile | 10 ++ lib/crypto/md5.c | 322 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 522 insertions(+), 1 deletion(-) create mode 100644 lib/crypto/md5.c
include/crypto/md5.h +180 -1
@@ -5,10 +5,11 @@#include <crypto/hash.h> #include <linux/types.h> #define MD5_DIGEST_SIZE 16#define MD5_HMAC_BLOCK_SIZE 64 +#define MD5_BLOCK_SIZE 64#define MD5_BLOCK_WORDS 16#define MD5_HASH_WORDS 4#define MD5_STATE_SIZE 24#define MD5_H0 0x67452301UL
@@ -25,6 +26,184 @@ struct md5_state {u32 hash[MD5_HASH_WORDS ];u64 byte_count;u32 block[MD5_BLOCK_WORDS ]; }; -#endif +/* State for the MD5 compression function */ +struct md5_block_state { +u32 h[MD5_HASH_WORDS ]; +}; + +/** +* struct md5_ctx - Context for hashing a message with MD5 +* @state: the compression function state +* @bytecount: number of bytes processed so far +* @buf: partial block buffer; bytecount % MD5_BLOCK_SIZE bytes are valid +*/ +struct md5_ctx { +struct md5_block_state state; +u64 bytecount; +u8 buf[MD5_BLOCK_SIZE ]__aligned(__alignof__(__le64)) ; +}; + +/** +* md5_init() - Initialize an MD5 context for a new message +* @ctx: the context to initialize +* +* If you don't need incremental computation, consider md5() instead. +* +* Context: Any context. +*/ +void md5_init (struct md5_ctx *ctx); + +/** +* md5_update() - Update an MD5 context with message data +* @ctx: the context to update; must have been initialized +* @data: the message data +* @len: the data length in bytes +* +* This can be called any number of times. +* +* Context: Any context. +*/ +void md5_update (struct md5_ctx *ctx,const u8 *data,size_t len); + +/** +* md5_final() - Finish computing an MD5 message digest +* @ctx: the context to finalize; must have been initialized +* @out: (output) the resulting MD5 message digest +* +* After finishing, this zeroizes @ctx. So the caller does not need to do it. +* +* Context: Any context. +*/ +void md5_final (struct md5_ctx *ctx,u8 out[MD5_DIGEST_SIZE ]); + +/** +* md5() - Compute MD5 message digest in one shot +* @data: the message data +* @len: the data length in bytes +* @out: (output) the resulting MD5 message digest +* +* Context: Any context. +*/ +void md5 (const u8 *data,size_t len,u8 out[MD5_DIGEST_SIZE ]); + +/** +* struct hmac_md5_key - Prepared key for HMAC-MD5 +* @istate: private +* @ostate: private +*/ +struct hmac_md5_key { +struct md5_block_state istate; +struct md5_block_state ostate; +}; + +/** +* struct hmac_md5_ctx - Context for computing HMAC-MD5 of a message +* @hash_ctx: private +* @ostate: private +*/ +struct hmac_md5_ctx { +struct md5_ctx hash_ctx; +struct md5_block_state ostate; +}; + +/** +* hmac_md5_preparekey() - Prepare a key for HMAC-MD5 +* @key: (output) the key structure to initialize +* @raw_key: the raw HMAC-MD5 key +* @raw_key_len: the key length in bytes. All key lengths are supported. +* +* Note: the caller is responsible for zeroizing both the struct hmac_md5_key +* and the raw key once they are no longer needed. +* +* Context: Any context. +*/ +void hmac_md5_preparekey (struct hmac_md5_key *key, +const u8 *raw_key,size_t raw_key_len); + +/** +* hmac_md5_init() - Initialize an HMAC-MD5 context for a new message +* @ctx: (output) the HMAC context to initialize +* @key: the prepared HMAC key +* +* If you don't need incremental computation, consider hmac_md5() instead. +* +* Context: Any context. +*/ +void hmac_md5_init (struct hmac_md5_ctx *ctx,const struct hmac_md5_key *key); + +/** +* hmac_md5_init_usingrawkey() - Initialize an HMAC-MD5 context for a new +* message, using a raw key +* @ctx: (output) the HMAC context to initialize +* @raw_key: the raw HMAC-MD5 key +* @raw_key_len: the key length in bytes. All key lengths are supported. +* +* If you don't need incremental computation, consider hmac_md5_usingrawkey() +* instead. +* +* Context: Any context. +*/ +void hmac_md5_init_usingrawkey (struct hmac_md5_ctx *ctx, +const u8 *raw_key,size_t raw_key_len); + +/** +* hmac_md5_update() - Update an HMAC-MD5 context with message data +* @ctx: the HMAC context to update; must have been initialized +* @data: the message data +* @data_len: the data length in bytes +* +* This can be called any number of times. +* +* Context: Any context. +*/ +static inline void hmac_md5_update (struct hmac_md5_ctx *ctx, +const u8 *data,size_t data_len) +{ +md5_update (&ctx->hash_ctx, data, data_len); +} + +/** +* hmac_md5_final() - Finish computing an HMAC-MD5 value +* @ctx: the HMAC context to finalize; must have been initialized +* @out: (output) the resulting HMAC-MD5 value +* +* After finishing, this zeroizes @ctx. So the caller does not need to do it. +* +* Context: Any context. +*/ +void hmac_md5_final (struct hmac_md5_ctx *ctx,u8 out[MD5_DIGEST_SIZE ]); + +/** +* hmac_md5() - Compute HMAC-MD5 in one shot, using a prepared key +* @key: the prepared HMAC key +* @data: the message data +* @data_len: the data length in bytes +* @out: (output) the resulting HMAC-MD5 value +* +* If you're using the key only once, consider using hmac_md5_usingrawkey(). +* +* Context: Any context. +*/ +void hmac_md5 (const struct hmac_md5_key *key,const u8 *data,size_t data_len, +u8 out[MD5_DIGEST_SIZE ]); + +/** +* hmac_md5_usingrawkey() - Compute HMAC-MD5 in one shot, using a raw key +* @raw_key: the raw HMAC-MD5 key +* @raw_key_len: the key length in bytes. All key lengths are supported. +* @data: the message data +* @data_len: the data length in bytes +* @out: (output) the resulting HMAC-MD5 value +* +* If you're using the key multiple times, prefer to use hmac_md5_preparekey() +* followed by multiple calls to hmac_md5() instead. +* +* Context: Any context. +*/ +void hmac_md5_usingrawkey (const u8 *raw_key,size_t raw_key_len, +const u8 *data,size_t data_len, +u8 out[MD5_DIGEST_SIZE ]); + +#endif /* _CRYPTO_MD5_H */
lib/crypto/Kconfig +10 -0
@@ -99,10 +99,20 @@ config CRYPTO_LIB_CURVE25519 one, if one is available and enabled. config CRYPTO_LIB_DES tristate +config CRYPTO_LIB_MD5 + tristate + help + The MD5 and HMAC-MD5 library functions. Select this if your module + uses any of the functions from <crypto/md5.h>. + +config CRYPTO_LIB_MD5_ARCH + bool + depends on CRYPTO_LIB_MD5 && !UML + config CRYPTO_LIB_POLY1305_RSIZE int default 2 if MIPS default 11 if X86_64 default 9 if ARM || ARM64
lib/crypto/Makefile +10 -0
@@ -57,10 +57,20 @@ libcurve25519-y += curve25519.o libcurve25519-$(CONFIG_CRYPTO_SELFTESTS) += curve25519-selftest.o obj-$(CONFIG_CRYPTO_LIB_DES) += libdes.o libdes-y := des.o +################################################################################ + +obj-$(CONFIG_CRYPTO_LIB_MD5) += libmd5.o +libmd5-y := md5.o +ifeq ($(CONFIG_CRYPTO_LIB_MD5_ARCH),y) +CFLAGS_md5.o += -I$(src)/$(SRCARCH) +endif # CONFIG_CRYPTO_LIB_MD5_ARCH + +################################################################################ + obj-$(CONFIG_CRYPTO_LIB_POLY1305) += libpoly1305.o libpoly1305-y += poly1305.o obj-$(CONFIG_CRYPTO_LIB_POLY1305_GENERIC) += libpoly1305-generic.o libpoly1305-generic-y := poly1305-donna32.o
lib/crypto/md5.c new file +322 -0
322 lines not on this page · raw message
Signature · 1 line
2.50.1
Eric Biggers
maintainerPATCH v2 2/7 lib/crypto: mips/md5: Migrate optimized code into library
In reply to Eric BiggersInstead of exposing the mips-optimized MD5 code via mips-specific crypto_shash algorithms, instead just implement the md5_blocks() library function. This is much simpler, it makes the MD5 library functions be mips-optimized, and it fixes the longstanding issue where the mips-optimized MD5 code was disabled by default. MD5 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-md5.c to lib/crypto/mips/md5.h, view this commit with 'git show -M10'.
--- arch/mips/cavium-octeon/crypto/Makefile | 2 - arch/mips/cavium-octeon/crypto/octeon-md5.c | 214 -------------------- arch/mips/configs/cavium_octeon_defconfig | 1 - arch/mips/crypto/Kconfig | 10 - lib/crypto/Kconfig | 1 + lib/crypto/mips/md5.h | 65 ++++++ 6 files changed, 66 insertions(+), 227 deletions(-) delete mode 100644 arch/mips/cavium-octeon/crypto/octeon-md5.c create mode 100644 lib/crypto/mips/md5.h
arch/mips/cavium-octeon/crypto/Makefile +0 -2
@@ -2,7 +2,5 @@ # # OCTEON-specific crypto modules. # obj-y += octeon-crypto.o - -obj-$(CONFIG_CRYPTO_MD5_OCTEON) += octeon-md5.o
arch/mips/cavium-octeon/crypto/octeon-md5.c deleted +0 -214
214 lines not on this page · raw message
arch/mips/configs/cavium_octeon_defconfig +0 -1
@@ -153,11 +153,10 @@ CONFIG_NLS_ISO8859_1=y CONFIG_NLS_UTF8=y CONFIG_SECURITY=y CONFIG_SECURITY_NETWORK=y CONFIG_CRYPTO_CBC=y CONFIG_CRYPTO_HMAC=y -CONFIG_CRYPTO_MD5_OCTEON=y CONFIG_CRYPTO_DES=y CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y CONFIG_DEBUG_FS=y CONFIG_MAGIC_SYSRQ=y # CONFIG_SCHED_DEBUG is not set
arch/mips/crypto/Kconfig +0 -10
@@ -1,15 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 menu "Accelerated Cryptographic Algorithms for CPU (mips)" -config CRYPTO_MD5_OCTEON - tristate "Digests: MD5 (OCTEON)" - depends on CPU_CAVIUM_OCTEON - select CRYPTO_MD5 - select CRYPTO_HASH - help - MD5 message digest algorithm (RFC1321) - - Architecture: mips OCTEON using crypto instructions, when available - endmenu
lib/crypto/Kconfig +1 -0
@@ -108,10 +108,11 @@ config CRYPTO_LIB_MD5 uses any of the functions from <crypto/md5.h>. config CRYPTO_LIB_MD5_ARCH bool depends on CRYPTO_LIB_MD5 && !UML + default y if MIPS && CPU_CAVIUM_OCTEON config CRYPTO_LIB_POLY1305_RSIZE int default 2 if MIPS default 11 if X86_64
lib/crypto/mips/md5.h new file +65 -0
65 lines not on this page · raw message
Signature · 1 line
2.50.1
Eric Biggers
maintainerPATCH v2 3/7 mips: cavium-octeon: Move octeon-crypto.c into parent dir
In reply to Eric BiggersSince octeon-crypto.c is the only remaining source file in arch/mips/cavium-octeon/crypto/, move it into its parent directory arch/mips/cavium-octeon/. Then remove the directory arch/mips/cavium-octeon/crypto/, including its Makefile.
---
arch/mips/cavium-octeon/Makefile | 2 +-
arch/mips/cavium-octeon/crypto/Makefile | 6 ------
arch/mips/cavium-octeon/{crypto => }/octeon-crypto.c | 0
3 files changed, 1 insertion(+), 7 deletions(-)
delete mode 100644 arch/mips/cavium-octeon/crypto/Makefile
rename arch/mips/cavium-octeon/{crypto => }/octeon-crypto.c (100%)arch/mips/cavium-octeon/Makefile +1 -1
@@ -9,12 +9,12 @@ # Copyright (C) 2005-2009 Cavium Networks # obj-y := cpu.o setup.o octeon-platform.o octeon-irq.o csrc-octeon.o obj-y += dma-octeon.o +obj-y += octeon-crypto.o obj-y += octeon-memcpy.o obj-y += executive/ -obj-y += crypto/ obj-$(CONFIG_MTD) += flash_setup.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_OCTEON_ILM) += oct_ilm.o
arch/mips/cavium-octeon/crypto/Makefile deleted +0 -6
@@ -1,6 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0 -# -# OCTEON-specific crypto modules. -# - -obj-y += octeon-crypto.o
arch/mips/cavium-octeon/octeon-crypto.c renamed from arch/mips/cavium-octeon/crypto/octeon-crypto.c +0 -0
Signature · 1 line
2.50.1
Eric Biggers
maintainerPATCH v2 4/7 lib/crypto: powerpc/md5: Migrate optimized code into library
In reply to Eric BiggersInstead of exposing the powerpc-optimized MD5 code via powerpc-specific crypto_shash algorithms, instead just implement the md5_blocks() library function. This is much simpler, it makes the MD5 library functions be powerpc-optimized, and it fixes the longstanding issue where the powerpc-optimized MD5 code was disabled by default. MD5 still remains available through crypto_shash, but individual architectures no longer need to handle it.
---
arch/powerpc/configs/powernv_defconfig | 1 -
arch/powerpc/configs/ppc64_defconfig | 1 -
arch/powerpc/crypto/Kconfig | 8 --
arch/powerpc/crypto/Makefile | 2 -
arch/powerpc/crypto/md5-glue.c | 99 -------------------
lib/crypto/Kconfig | 1 +
lib/crypto/Makefile | 1 +
.../crypto => lib/crypto/powerpc}/md5-asm.S | 0
lib/crypto/powerpc/md5.h | 12 +++
9 files changed, 14 insertions(+), 111 deletions(-)
delete mode 100644 arch/powerpc/crypto/md5-glue.c
rename {arch/powerpc/crypto => lib/crypto/powerpc}/md5-asm.S (100%)
create mode 100644 lib/crypto/powerpc/md5.harch/powerpc/configs/powernv_defconfig +0 -1
@@ -318,11 +318,10 @@ CONFIG_FTR_FIXUP_SELFTEST=y CONFIG_MSI_BITMAP_SELFTEST=y CONFIG_XMON=y CONFIG_CRYPTO_BENCHMARK=m CONFIG_CRYPTO_PCBC=m CONFIG_CRYPTO_HMAC=y -CONFIG_CRYPTO_MD5_PPC=m CONFIG_CRYPTO_MICHAEL_MIC=m CONFIG_CRYPTO_SHA256=y CONFIG_CRYPTO_WP512=m CONFIG_CRYPTO_ANUBIS=m CONFIG_CRYPTO_BLOWFISH=m
arch/powerpc/configs/ppc64_defconfig +0 -1
@@ -385,11 +385,10 @@ CONFIG_CRYPTO_TWOFISH=m CONFIG_CRYPTO_PCBC=m CONFIG_CRYPTO_MICHAEL_MIC=m CONFIG_CRYPTO_SHA256=y CONFIG_CRYPTO_WP512=m CONFIG_CRYPTO_LZO=m -CONFIG_CRYPTO_MD5_PPC=m CONFIG_CRYPTO_AES_GCM_P10=m CONFIG_CRYPTO_DEV_NX=y CONFIG_CRYPTO_DEV_NX_ENCRYPT=m CONFIG_CRYPTO_DEV_VMX=y CONFIG_SYSTEM_TRUSTED_KEYRING=y
arch/powerpc/crypto/Kconfig +0 -8
@@ -13,18 +13,10 @@ config CRYPTO_CURVE25519_PPC64 Curve25519 algorithm Architecture: PowerPC64 - Little-endian -config CRYPTO_MD5_PPC - tristate "Digests: MD5" - select CRYPTO_HASH - help - MD5 message digest algorithm (RFC1321) - - Architecture: powerpc - config CRYPTO_AES_PPC_SPE tristate "Ciphers: AES, modes: ECB/CBC/CTR/XTS (SPE)" depends on SPE select CRYPTO_SKCIPHER help
arch/powerpc/crypto/Makefile +0 -2
@@ -4,17 +4,15 @@ # # Arch-specific CryptoAPI modules. # obj-$(CONFIG_CRYPTO_AES_PPC_SPE) += aes-ppc-spe.o -obj-$(CONFIG_CRYPTO_MD5_PPC) += md5-ppc.o obj-$(CONFIG_CRYPTO_AES_GCM_P10) += aes-gcm-p10-crypto.o obj-$(CONFIG_CRYPTO_DEV_VMX_ENCRYPT) += vmx-crypto.o obj-$(CONFIG_CRYPTO_CURVE25519_PPC64) += curve25519-ppc64le.o aes-ppc-spe-y := aes-spe-core.o aes-spe-keys.o aes-tab-4k.o aes-spe-modes.o aes-spe-glue.o -md5-ppc-y := md5-asm.o md5-glue.o aes-gcm-p10-crypto-y := aes-gcm-p10-glue.o aes-gcm-p10.o ghashp10-ppc.o aesp10-ppc.o vmx-crypto-objs := vmx.o aesp8-ppc.o ghashp8-ppc.o aes.o aes_cbc.o aes_ctr.o aes_xts.o ghash.o curve25519-ppc64le-y := curve25519-ppc64le-core.o curve25519-ppc64le_asm.o ifeq ($(CONFIG_CPU_LITTLE_ENDIAN),y)
arch/powerpc/crypto/md5-glue.c deleted +0 -99
99 lines not on this page · raw message
lib/crypto/Kconfig +1 -0
@@ -109,10 +109,11 @@ config CRYPTO_LIB_MD5 config CRYPTO_LIB_MD5_ARCH bool depends on CRYPTO_LIB_MD5 && !UML default y if MIPS && CPU_CAVIUM_OCTEON + default y if PPC config CRYPTO_LIB_POLY1305_RSIZE int default 2 if MIPS default 11 if X86_64
lib/crypto/Makefile +1 -0
@@ -63,10 +63,11 @@ libdes-y := des.o obj-$(CONFIG_CRYPTO_LIB_MD5) += libmd5.o libmd5-y := md5.o ifeq ($(CONFIG_CRYPTO_LIB_MD5_ARCH),y) CFLAGS_md5.o += -I$(src)/$(SRCARCH) +libmd5-$(CONFIG_PPC) += powerpc/md5-asm.o endif # CONFIG_CRYPTO_LIB_MD5_ARCH ################################################################################ obj-$(CONFIG_CRYPTO_LIB_POLY1305) += libpoly1305.o
lib/crypto/powerpc/md5-asm.S renamed from arch/powerpc/crypto/md5-asm.S +0 -0
lib/crypto/powerpc/md5.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* MD5 optimized for PowerPC +*/ + +void ppc_md5_transform (u32 *state,const u8 *data,size_t nblocks); + +static void md5_blocks (struct md5_block_state *state, +const u8 *data,size_t nblocks) +{ +ppc_md5_transform (state->h, data, nblocks); +}
Signature · 1 line
2.50.1
Eric Biggers
maintainerPATCH v2 5/7 lib/crypto: sparc/md5: Migrate optimized code into library
In reply to Eric BiggersInstead of exposing the sparc-optimized MD5 code via sparc-specific crypto_shash algorithms, instead just implement the md5_blocks() library function. This is much simpler, it makes the MD5 library functions be sparc-optimized, and it fixes the longstanding issue where the sparc-optimized MD5 code was disabled by default. MD5 still remains available through crypto_shash, but individual architectures no longer need to handle it.
Note: to see the diff from arch/sparc/crypto/md5_glue.c to lib/crypto/sparc/md5.h, view this commit with 'git show -M10'.
---
arch/sparc/crypto/Kconfig | 10 -
arch/sparc/crypto/Makefile | 4 -
arch/sparc/crypto/md5_glue.c | 174 ------------------
lib/crypto/Kconfig | 1 +
lib/crypto/Makefile | 1 +
lib/crypto/sparc/md5.h | 48 +++++
.../crypto => lib/crypto/sparc}/md5_asm.S | 0
7 files changed, 50 insertions(+), 188 deletions(-)
delete mode 100644 arch/sparc/crypto/md5_glue.c
create mode 100644 lib/crypto/sparc/md5.h
rename {arch/sparc/crypto => lib/crypto/sparc}/md5_asm.S (100%)arch/sparc/crypto/Kconfig +0 -10
@@ -14,20 +14,10 @@ config CRYPTO_DES_SPARC64 Length-preserving ciphers: DES with ECB and CBC modes Length-preserving ciphers: Tripe DES EDE with ECB and CBC modes Architecture: sparc64 -config CRYPTO_MD5_SPARC64 - tristate "Digests: MD5" - depends on SPARC64 - select CRYPTO_MD5 - select CRYPTO_HASH - help - MD5 message digest algorithm (RFC1321) - - Architecture: sparc64 using crypto instructions, when available - config CRYPTO_AES_SPARC64 tristate "Ciphers: AES, modes: ECB, CBC, CTR" depends on SPARC64 select CRYPTO_SKCIPHER help
arch/sparc/crypto/Makefile +0 -4
@@ -1,16 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 # # Arch-specific CryptoAPI modules. # -obj-$(CONFIG_CRYPTO_MD5_SPARC64) += md5-sparc64.o - obj-$(CONFIG_CRYPTO_AES_SPARC64) += aes-sparc64.o obj-$(CONFIG_CRYPTO_DES_SPARC64) += des-sparc64.o obj-$(CONFIG_CRYPTO_CAMELLIA_SPARC64) += camellia-sparc64.o -md5-sparc64-y := md5_asm.o md5_glue.o - aes-sparc64-y := aes_asm.o aes_glue.o des-sparc64-y := des_asm.o des_glue.o camellia-sparc64-y := camellia_asm.o camellia_glue.o
arch/sparc/crypto/md5_glue.c deleted +0 -174
174 lines not on this page · raw message
lib/crypto/Kconfig +1 -0
@@ -110,10 +110,11 @@ config CRYPTO_LIB_MD5 config CRYPTO_LIB_MD5_ARCH bool depends on CRYPTO_LIB_MD5 && !UML default y if MIPS && CPU_CAVIUM_OCTEON default y if PPC + default y if SPARC64 config CRYPTO_LIB_POLY1305_RSIZE int default 2 if MIPS default 11 if X86_64
lib/crypto/Makefile +1 -0
@@ -64,10 +64,11 @@ libdes-y := des.o obj-$(CONFIG_CRYPTO_LIB_MD5) += libmd5.o libmd5-y := md5.o ifeq ($(CONFIG_CRYPTO_LIB_MD5_ARCH),y) CFLAGS_md5.o += -I$(src)/$(SRCARCH) libmd5-$(CONFIG_PPC) += powerpc/md5-asm.o +libmd5-$(CONFIG_SPARC) += sparc/md5_asm.o endif # CONFIG_CRYPTO_LIB_MD5_ARCH ################################################################################ obj-$(CONFIG_CRYPTO_LIB_POLY1305) += libpoly1305.o
lib/crypto/sparc/md5.h new file +48 -0
48 lines not on this page · raw message
lib/crypto/sparc/md5_asm.S renamed from arch/sparc/crypto/md5_asm.S +0 -0
Signature · 1 line
2.50.1
Eric Biggers
maintainerPATCH v2 6/7 crypto: md5 - Wrap library and add HMAC support
In reply to Eric BiggersReimplement crypto/md5.c on top of the new MD5 library functions. Also add support for HMAC-MD5, again just wrapping the library functions.
This closely mirrors crypto/sha1.c.
--- crypto/Kconfig | 2 +- crypto/md5.c | 359 ++++++++++++++++++-------------------- crypto/testmgr.c | 3 + drivers/crypto/img-hash.c | 2 +- 4 files changed, 171 insertions(+), 195 deletions(-)
crypto/Kconfig +1 -1
@@ -937,11 +937,11 @@ config CRYPTO_MD4 config CRYPTO_MD5 tristate "MD5" select CRYPTO_HASH help - MD5 message digest algorithm (RFC1321) + MD5 message digest algorithm (RFC1321), including HMAC support. config CRYPTO_MICHAEL_MIC tristate "Michael MIC" select CRYPTO_HASH help
crypto/md5.c +166 -193
390 lines not on this page · raw message
crypto/testmgr.c +3 -0
@@ -4176,10 +4176,11 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .cprng = __VECS(ansi_cprng_aes_tv_template) } }, { .alg = "authenc(hmac(md5),ecb(cipher_null))", + .generic_driver = "authenc(hmac-md5-lib,ecb-cipher_null)", .test = alg_test_aead, .suite = { .aead = __VECS(hmac_md5_ecb_cipher_null_tv_template) } }, {
@@ -5062,10 +5063,11 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .cipher = __VECS(aes_hctr2_tv_template) } }, { .alg = "hmac(md5)", + .generic_driver = "hmac-md5-lib", .test = alg_test_hash, .suite = { .hash = __VECS(hmac_md5_tv_template) } }, {
@@ -5248,10 +5250,11 @@ static const struct alg_test_desc alg_test_descs[] = { .suite = { .hash = __VECS(md4_tv_template) } }, { .alg = "md5", + .generic_driver = "md5-lib", .test = alg_test_hash, .suite = { .hash = __VECS(md5_tv_template) } }, {
drivers/crypto/img-hash.c +1 -1
@@ -698,11 +698,11 @@ static int img_hash_cra_init(struct crypto_tfm *tfm, const char *alg_name)return 0 ; }static int img_hash_cra_md5_init (struct crypto_tfm *tfm) { -return img_hash_cra_init (tfm,"md5-generic" ); +return img_hash_cra_init (tfm,"md5-lib" ); }static int img_hash_cra_sha1_init (struct crypto_tfm *tfm) {return img_hash_cra_init (tfm,"sha1-lib" );
Signature · 1 line
2.50.1
Eric Biggers
maintainerRe: [PATCH v2 6/7] crypto: md5 - Wrap library and add HMAC support
Quoted 18 lines · Eric Biggers
Reimplement
crypto/md5.con top of the new MD5 library functions. Also add support for HMAC-MD5, again just wrapping the library functions.This closely mirrors
crypto/sha1.c.Signed-off-by Eric Biggers <[email protected]>--- crypto/Kconfig | 2 +- crypto/md5.c | 359 ++++++++++++++++++-------------------- crypto/testmgr.c | 3 + drivers/crypto/img-hash.c | 2 +- 4 files changed, 171 insertions(+), 195 deletions(-)crypto/Kconfig +0
-0
crypto/Kconfig config CRYPTO_MD4 · line 942
Quoted 7 lines
crypto/Kconfig +1
-1@@ -937,11 +937,11 @@ config CRYPTO_MD4 config CRYPTO_MD5 tristate "MD5" select CRYPTO_HASH help - MD5 message digest algorithm (RFC1321) + MD5 message digest algorithm (RFC1321), including HMAC support.
Correction: there needs to be 'select CRYPTO_LIB_MD5' above.
- Eric
Eric Biggers
maintainerPATCH v2 7/7 lib/crypto: tests: Add KUnit tests for MD5 and HMAC-MD5
In reply to Eric BiggersAdd a KUnit test suite for the MD5 library functions, including the corresponding HMAC support. The core test logic is in the previously-added hash-test-template.h. This commit just adds the actual KUnit suite, and it adds the generated test vectors to the tree so that gen-hash-testvecs.py won't have to be run at build time.
--- lib/crypto/tests/Kconfig | 10 ++ lib/crypto/tests/Makefile | 1 + lib/crypto/tests/md5-testvecs.h | 186 ++++++++++++++++++++++++++++++++ lib/crypto/tests/md5_kunit.c | 39 +++++++ 4 files changed, 236 insertions(+) create mode 100644 lib/crypto/tests/md5-testvecs.h create mode 100644 lib/crypto/tests/md5_kunit.c
lib/crypto/tests/Kconfig +10 -0
@@ -1,7 +1,17 @@ # SPDX-License-Identifier: GPL-2.0-or-later +config CRYPTO_LIB_MD5_KUNIT_TEST + tristate "KUnit tests for MD5" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS + select CRYPTO_LIB_BENCHMARK_VISIBLE + select CRYPTO_LIB_MD5 + help + KUnit tests for the MD5 cryptographic hash function and its + corresponding HMAC. + config CRYPTO_LIB_POLY1305_KUNIT_TEST tristate "KUnit tests for Poly1305" if !KUNIT_ALL_TESTS depends on KUNIT default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS select CRYPTO_LIB_BENCHMARK_VISIBLE
lib/crypto/tests/Makefile +1 -0
@@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0-or-later +obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o obj-$(CONFIG_CRYPTO_LIB_SHA1_KUNIT_TEST) += sha1_kunit.o obj-$(CONFIG_CRYPTO_LIB_SHA256_KUNIT_TEST) += sha224_kunit.o sha256_kunit.o obj-$(CONFIG_CRYPTO_LIB_SHA512_KUNIT_TEST) += sha384_kunit.o sha512_kunit.o
lib/crypto/tests/md5-testvecs.h new file +186 -0
186 lines not on this page · raw message
lib/crypto/tests/md5_kunit.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright 2025 Google LLC +*/ +#include <crypto/md5.h> +#include "md5-testvecs.h" + +#define HASH md5 +#define HASH_CTX md5_ctx +#define HASH_SIZE MD5_DIGEST_SIZE +#define HASH_INIT md5_init +#define HASH_UPDATE md5_update +#define HASH_FINAL md5_final +#define HMAC_KEY hmac_md5_key +#define HMAC_CTX hmac_md5_ctx +#define HMAC_PREPAREKEY hmac_md5_preparekey +#define HMAC_INIT hmac_md5_init +#define HMAC_UPDATE hmac_md5_update +#define HMAC_FINAL hmac_md5_final +#define HMAC hmac_md5 +#define HMAC_USINGRAWKEY hmac_md5_usingrawkey +#include "hash-test-template.h" + +static struct kunit_case hash_test_cases[] = { +HASH_KUNIT_CASES , +KUNIT_CASE (benchmark_hash), + {}, +}; + +static struct kunit_suite hash_test_suite = { + .name ="md5" , + .test_cases = hash_test_cases, + .suite_init = hash_suite_init, + .suite_exit = hash_suite_exit, +}; +kunit_test_suite (hash_test_suite); + +MODULE_DESCRIPTION ("KUnit tests and benchmark for MD5 and HMAC-MD5" ); +MODULE_LICENSE ("GPL" );
Signature · 1 line
2.50.1
Quoted 17 lines · Eric Biggers
This series is targeting libcrypto-next and can also be retrieved from:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git md5-lib-v2
This series introduces a library API for MD5 and HMAC-MD5 and reimplements the
crypto_shash"md5" and "hmac(md5)" on top of it.The library API will also be usable directly by various in-kernel users that are stuck with MD5 due to having to implement legacy protocols.
This should again look quite boring and familiar, as it mirrors the SHA-1 and SHA-2 changes closely.
Changed in v2:
- Kept the architecture-optimized MD5 code, since unfortunately there were objections to removing it.
Quoted 8 lines
Eric Biggers (7):
lib/crypto: md5: Add MD5 and HMAC-MD5 library functions lib/crypto: mips/md5: Migrate optimized code into library
mips: cavium-octeon: Moveocteon-crypto.cinto parent dir lib/crypto: powerpc/md5: Migrate optimized code into library lib/crypto: sparc/md5: Migrate optimized code into library
crypto: md5 - Wrap library and add HMAC support lib/crypto: tests: Add KUnit tests for MD5 and HMAC-MD5
Applied to https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git/log/?h=libcrypto-next
Reviews and acks still greatly appreciated, of course!
- Eric