Skip to contentSkip to conversation

lib/crypto: ML-DSA verification support

PATCH Superseded Eric Biggers 4 participants 9 messages Dec 3, 2025 – Dec 6, 2025

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

v3 cover lib/crypto: ML-DSA verification support

This series can also be retrieved from:

git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git mldsa-v3

This series adds support for verifying ML-DSA signatures to lib/crypto/. Patch 1 is the ML-DSA implementation itself. See that for full details. Patch 2 adds the KUnit test suite.

The initial use case for this will be kernel module signature verification. For more details, see David Howells' patchset
https://lore.kernel.org/linux-crypto/[email protected]/

Changed in v3:
- Moved SHAKE128 block buffer off the stack
- Reworked use_hint() again, simplifying it further
- Added unit test for use_hint()
- Moved some of the reduction logic into invntt_and_mul_2_32() and simplified it slightly
- Updated Zq_mult() to not rely on signed integer overflow having defined behavior, though this is unnecessary in the kernel
- Formatted the documented return values into a list
- Other minor tweaks

Changed in v2:
- Reworked the KUnit test suite
- Improved commit messages and comments
- Added missing MODULE_DESCRIPTION() and MODULE_LICENSE()
- Made the return values of mldsa_verify() differentiate between an input being malformed and the "real" signature check failing
- Refactored w1 encoding into a helper function
- Used kfree() instead of kfree_sensitive()
- Avoided unusal C syntax by accessing the hint vector via 'u8 *'
- Reworked use_hint() to be better optimized and documented

Eric Biggers (2):
lib/crypto: Add ML-DSA verification support lib/crypto: tests: Add KUnit tests for ML-DSA verification

 include/crypto/mldsa.h            |   60 +
 lib/crypto/Kconfig                |    7 +
 lib/crypto/Makefile               |    5 +
 lib/crypto/mldsa.c                |  652 ++++++++++
 lib/crypto/tests/Kconfig          |    9 +
 lib/crypto/tests/Makefile         |    1 +
 lib/crypto/tests/mldsa-testvecs.h | 1887 +++++++++++++++++++++++++++++
 lib/crypto/tests/mldsa_kunit.c    |  437 +++++++
 8 files changed, 3058 insertions(+)
 create mode 100644 include/crypto/mldsa.h
 create mode 100644 lib/crypto/mldsa.c
 create mode 100644 lib/crypto/tests/mldsa-testvecs.h
 create mode 100644 lib/crypto/tests/mldsa_kunit.c

base-commit: 3f9f0252130e7dd60d41be0802bf58f6471c691d

Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH v3 1/2 lib/crypto: Add ML-DSA verification support

Add support for verifying ML-DSA signatures.

ML-DSA (Module-Lattice-Based Digital Signature Algorithm) is specified in FIPS 204 and is the standard version of Dilithium. Unlike RSA and elliptic-curve cryptography, ML-DSA is believed to be secure even against adversaries in possession of a large-scale quantum computer.

Compared to the earlier patch
(https://lore.kernel.org/r/[email protected]/)
that was based on "leancrypto", this implementation:

- Is about 700 lines of source code instead of 4800.

- Generates about 4 KB of object code instead of 28 KB.

- Uses 9-13 KB of memory to verify a signature instead of 31-84 KB.

- Is at least about the same speed, with a microbenchmark showing 3-5% improvements on one x86_64 CPU and -1% to 1% changes on another. When memory is a bottleneck, it's likely much faster.

- Correctly implements the RejNTTPoly step of the algorithm.

The API just consists of a single function mldsa_verify(), supporting pure ML-DSA with any standard parameter set (ML-DSA-44, ML-DSA-65, or ML-DSA-87) as selected by an enum. That's all that's actually needed.

The following four potential features are unneeded and aren't included. However, any that ever become needed could fairly easily be added later, as they only affect how the message representative mu is calculated:

- Nonempty context strings
- Incremental message hashing
- HashML-DSA
- External mu

Signing support would, of course, be a larger and more complex addition. However, the kernel doesn't, and shouldn't, need ML-DSA signing support.

Note that mldsa_verify() allocates memory, so it can sleep and can fail with ENOMEM. Unfortunately we don't have much choice about that, since ML-DSA needs a lot of memory. At least callers have to check for errors anyway, since the signature could be invalid.

Note that verification doesn't require constant-time code, and in fact some steps are inherently variable-time. I've used constant-time patterns in some places anyway, but technically they're not needed.

---
 include/crypto/mldsa.h |  60 ++++
 lib/crypto/Kconfig     |   7 +
 lib/crypto/Makefile    |   5 +
 lib/crypto/mldsa.c     | 652 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 724 insertions(+)
 create mode 100644 include/crypto/mldsa.h
 create mode 100644 lib/crypto/mldsa.c
include/crypto/mldsa.h new file +60 -0
new file mode 100644
index 000000000000..cf30aef29970
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Support for verifying ML-DSA signatures
+ *
+ * Copyright 2025 Google LLC
+ */
+#ifndef _CRYPTO_MLDSA_H
+#define _CRYPTO_MLDSA_H
+
+#include <linux/types.h>
+
+/* Identifier for an ML-DSA parameter set */
+enum mldsa_alg {
+	MLDSA44, /* ML-DSA-44 */
+	MLDSA65, /* ML-DSA-65 */
+	MLDSA87, /* ML-DSA-87 */
+};
+
+/* Lengths of ML-DSA public keys and signatures in bytes */
+#define MLDSA44_PUBLIC_KEY_SIZE 1312
+#define MLDSA65_PUBLIC_KEY_SIZE 1952
+#define MLDSA87_PUBLIC_KEY_SIZE 2592
+#define MLDSA44_SIGNATURE_SIZE 2420
+#define MLDSA65_SIGNATURE_SIZE 3309
+#define MLDSA87_SIGNATURE_SIZE 4627
+
+/**
+ * mldsa_verify() - Verify an ML-DSA signature
+ * @alg: The ML-DSA parameter set to use
+ * @sig: The signature
+ * @sig_len: Length of the signature in bytes.  Should match the
+ *	     MLDSA*_SIGNATURE_SIZE constant associated with @alg,
+ *	     otherwise -EBADMSG will be returned.
+ * @msg: The message
+ * @msg_len: Length of the message in bytes
+ * @pk: The public key
+ * @pk_len: Length of the public key in bytes.  Should match the
+ *	    MLDSA*_PUBLIC_KEY_SIZE constant associated with @alg,
+ *	    otherwise -EBADMSG will be returned.
+ *
+ * This verifies a signature using pure ML-DSA with the specified parameter set.
+ * The context string is assumed to be empty.
+ *
+ * Context: Might sleep
+ *
+ * Return:
+ * * 0 if the signature is valid
+ * * -EBADMSG if the signature and/or public key is malformed
+ * * -EKEYREJECTED if the signature is invalid but otherwise well-formed
+ * * -ENOMEM if out of memory so the validity of the signature is unknown
+ */
+int mldsa_verify(enum mldsa_alg alg, const u8 *sig, size_t sig_len,
+		 const u8 *msg, size_t msg_len, const u8 *pk, size_t pk_len);
+
+#if IS_ENABLED(CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST)
+/* Internal function, exposed only for unit testing */
+s32 mldsa_use_hint(u8 h, s32 r, s32 gamma2);
+#endif
+
+#endif /* _CRYPTO_MLDSA_H */
lib/crypto/Kconfig +7 -0
index a3647352bff6..6fcdbe46d590 100644
@@ -98,10 +98,17 @@ config CRYPTO_LIB_MD5_ARCH
 	depends on CRYPTO_LIB_MD5 && !UML
 	default y if MIPS && CPU_CAVIUM_OCTEON
 	default y if PPC
 	default y if SPARC64
 
+config CRYPTO_LIB_MLDSA
+	tristate
+	select CRYPTO_LIB_SHA3
+	help
+	  The ML-DSA library functions.  Select this if your module uses any of
+	  the functions from <crypto/mldsa.h>.
+
 config CRYPTO_LIB_POLY1305
 	tristate
 	help
 	  The Poly1305 library functions.  Select this if your module uses any
 	  of the functions from <crypto/poly1305.h>.
lib/crypto/Makefile +5 -0
index b5346cebbb55..9911e7b3905a 100644
@@ -125,10 +125,15 @@ libmd5-$(CONFIG_PPC) += powerpc/md5-asm.o
 libmd5-$(CONFIG_SPARC) += sparc/md5_asm.o
 endif # CONFIG_CRYPTO_LIB_MD5_ARCH
 
 ################################################################################
 
+obj-$(CONFIG_CRYPTO_LIB_MLDSA) += libmldsa.o
+libmldsa-y := mldsa.o
+
+################################################################################
+
 obj-$(CONFIG_CRYPTO_LIB_POLY1305) += libpoly1305.o
 libpoly1305-y := poly1305.o
 ifeq ($(CONFIG_ARCH_SUPPORTS_INT128),y)
 libpoly1305-$(CONFIG_CRYPTO_LIB_POLY1305_GENERIC) += poly1305-donna64.o
 else
lib/crypto/mldsa.c new file +652 -0

652 lines not on this page · raw message

Signature · 1 line
2.52.0
author maintainer
#2 lore raw

PATCH v3 2/2 lib/crypto: tests: Add KUnit tests for ML-DSA verification

In reply to Eric Biggers

Add a KUnit test suite for ML-DSA verification, including the following for each ML-DSA parameter set (ML-DSA-44, ML-DSA-65, and ML-DSA-87):

- Positive test (valid signature), using vector imported from leancrypto
- Various negative tests:
- Wrong length for signature, message, or public key
- Out-of-range coefficients in z vector
- Invalid encoded hint vector
- Any bit flipped in signature, message, or public key
- Unit test for the internal function use_hint()
- A benchmark

ML-DSA inputs and outputs are very large. To keep the size of the tests down, use just one valid test vector per parameter set, and generate the negative tests at runtime by mutating the valid test vector.

I also considered importing the test vectors from Wycheproof. I've tested that mldsa_verify() indeed passes all of Wycheproof's ML-DSA test vectors that use an empty context string. However, importing these permanently would add over 6 MB of source. That's too much to be a reasonable addition to the Linux kernel tree for one algorithm. It also wouldn't actually provide much better test coverage than this commit. Another potential issue is that Wycheproof uses the Apache license.

Similarly, this also differs from the earlier proposal to import a long list of test vectors from leancrypto. I retained only one valid signature for each algorithm, and I also added (runtime-generated) negative tests which were missing. I think this is a better tradeoff.

---
 lib/crypto/tests/Kconfig          |    9 +
 lib/crypto/tests/Makefile         |    1 +
 lib/crypto/tests/mldsa-testvecs.h | 1887 +++++++++++++++++++++++++++++
 lib/crypto/tests/mldsa_kunit.c    |  437 +++++++
 4 files changed, 2334 insertions(+)
 create mode 100644 lib/crypto/tests/mldsa-testvecs.h
 create mode 100644 lib/crypto/tests/mldsa_kunit.c
lib/crypto/tests/Kconfig +9 -0
index 61d435c450bb..c9165c14fc46 100644
@@ -36,10 +36,19 @@ config CRYPTO_LIB_MD5_KUNIT_TEST
 	select CRYPTO_LIB_MD5
 	help
 	  KUnit tests for the MD5 cryptographic hash function and its
 	  corresponding HMAC.
 
+config CRYPTO_LIB_MLDSA_KUNIT_TEST
+	tristate "KUnit tests for ML-DSA" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS || CRYPTO_SELFTESTS
+	select CRYPTO_LIB_BENCHMARK_VISIBLE
+	select CRYPTO_LIB_MLDSA
+	help
+	  KUnit tests for the ML-DSA digital signature algorithm.
+
 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
index 5109a0651925..3dde0b40b2c5 100644
@@ -2,10 +2,11 @@
 
 obj-$(CONFIG_CRYPTO_LIB_BLAKE2B_KUNIT_TEST) += blake2b_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_BLAKE2S_KUNIT_TEST) += blake2s_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_CURVE25519_KUNIT_TEST) += curve25519_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_MD5_KUNIT_TEST) += md5_kunit.o
+obj-$(CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST) += mldsa_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_POLY1305_KUNIT_TEST) += poly1305_kunit.o
 obj-$(CONFIG_CRYPTO_LIB_POLYVAL_KUNIT_TEST) += polyval_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/mldsa-testvecs.h new file +1887 -0

1,887 lines not on this page · raw message

lib/crypto/tests/mldsa_kunit.c new file +437 -0

437 lines not on this page · raw message

Signature · 1 line
2.52.0
maintainer
#3 lore raw

Re: [PATCH v3 2/2] lib/crypto: tests: Add KUnit tests for ML-DSA verification

I'm seeing:

ERROR: modpost: module mldsa_kunit uses symbol mldsa_use_hint from namespace EXPORTED_FOR_KUNIT_TESTING, but does not import it.

I have this in my .config:

CONFIG_CRYPTO_LIB_MLDSA=m
CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST=m

The problem appears to be here:

	#if IS_ENABLED(CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST)
	/* Allow the __always_inline function use_hint() to be unit-tested. */
	s32 mldsa_use_hint(u8 h, s32 r, s32 gamma2)
	{
		return use_hint(h, r, gamma2);
	}
	EXPORT_SYMBOL_IF_KUNIT(mldsa_use_hint);
	#endif

It works if EXPORT_SYMBOL_IF_KUNIT() is changed to EXPORT_SYMBOL_GPL().

David

#7 lore raw
In reply to Eric Biggers
Quoted 55 lines · Eric Biggers

This series can also be retrieved from:

git fetch
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git
mldsa-v3

This series adds support for verifying ML-DSA signatures to lib/crypto/. Patch 1 is the ML-DSA implementation itself. See that for full details. Patch 2 adds the KUnit test suite.

The initial use case for this will be kernel module signature verification. For more details, see David Howells' patchset
https://lore.kernel.org/linux-crypto/[email protected]/

Changed in v3:
- Moved SHAKE128 block buffer off the stack
- Reworked use_hint() again, simplifying it further
- Added unit test for use_hint()
- Moved some of the reduction logic into invntt_and_mul_2_32() and simplified it slightly
- Updated Zq_mult() to not rely on signed integer overflow having defined behavior, though this is unnecessary in the kernel
- Formatted the documented return values into a list
- Other minor tweaks

Changed in v2:
- Reworked the KUnit test suite
- Improved commit messages and comments
- Added missing MODULE_DESCRIPTION() and MODULE_LICENSE()
- Made the return values of mldsa_verify() differentiate between an input being malformed and the "real" signature check failing
- Refactored w1 encoding into a helper function
- Used kfree() instead of kfree_sensitive()
- Avoided unusal C syntax by accessing the hint vector via 'u8 *'
- Reworked use_hint() to be better optimized and documented

Eric Biggers (2):
lib/crypto: Add ML-DSA verification support lib/crypto: tests: Add KUnit tests for ML-DSA verification

 include/crypto/mldsa.h            |   60 +
 lib/crypto/Kconfig                |    7 +
 lib/crypto/Makefile               |    5 +
 lib/crypto/mldsa.c                |  652 ++++++++++
 lib/crypto/tests/Kconfig          |    9 +
 lib/crypto/tests/Makefile         |    1 +
 lib/crypto/tests/mldsa-testvecs.h | 1887 +++++++++++++++++++++++++++++
 lib/crypto/tests/mldsa_kunit.c    |  437 +++++++
 8 files changed, 3058 insertions(+)
 create mode 100644 include/crypto/mldsa.h
 create mode 100644 lib/crypto/mldsa.c
 create mode 100644 lib/crypto/tests/mldsa-testvecs.h
 create mode 100644 lib/crypto/tests/mldsa_kunit.c
Quoted 1 line

base-commit: 3f9f0252130e7dd60d41be0802bf58f6471c691d

Hi Eric, I pulled your branch but it does not build (on s390):

[root@b83lp71 ebiggers-linux]# make
CALL scripts/checksyscalls.sh
MODPOST Module.symvers
ERROR: modpost: module mldsa_kunit uses symbol mldsa_use_hint from namespace EXPORTED_FOR_KUNIT_TESTING, but does not import it. make[2]: *** [scripts/Makefile.modpost:147: Module.symvers] Error 1
make[1]: *** [/ext/ebiggers-linux/Makefile:1963: modpost] Error 2
make: *** [Makefile:248: __sub-make] Error 2
[root@b83lp71 ebiggers-linux]#

any ideas?

Harald Freudenberger

maintainer
#8 lore raw
Quoted 2 lines · Harald Freudenberger

ERROR: modpost: module mldsa_kunit uses symbol mldsa_use_hint from namespace EXPORTED_FOR_KUNIT_TESTING, but does not import it.

Change EXPORT_SYMBOL_FOR_KUNIT() in patch 1 to EXPORT_SYMBOL_GPL().

David

author maintainer
#4 lore raw

Re: [PATCH v3 2/2] lib/crypto: tests: Add KUnit tests for ML-DSA verification

In reply to David Howells
Quoted 4 lines · David Howells

I'm seeing:

ERROR: modpost: module mldsa_kunit uses symbol mldsa_use_hint from namespace EXPORTED_FOR_KUNIT_TESTING, but does not import it.

Yep, sorry for the trouble. mldsa_kunit.c needs:

    MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");

Would be nice if the kunit_test_suite() macro did that so that test authors don't have to remember to add the namespace import separately. But maybe there's a reason why they need to be separate.

- Eric

#5 lore raw

Re: [PATCH v3 2/2] lib/crypto: tests: Add KUnit tests for ML-DSA verification

In reply to Eric Biggers
kernel test robot noticed the following build errors:

Hi Eric,

kernel test robot noticed the following build errors:

[auto build test ERROR on 3f9f0252130e7dd60d41be0802bf58f6471c691d]

url:    https://github.com/intel-lab-lkp/linux/commits/Eric-Biggers/lib-crypto-Add-ML-DSA-verification-support/20251203-153410
base:   3f9f0252130e7dd60d41be0802bf58f6471c691d
patch link:    https://lore.kernel.org/r/20251203072844.484893-3-ebiggers%40kernel.org
patch subject: [PATCH v3 2/2] lib/crypto: tests: Add KUnit tests for ML-DSA verification
config: i386-randconfig-004-20251206 (https://download.01.org/0day-ci/archive/20251206/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251206/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: module mldsa_kunit uses symbol mldsa_use_hint from namespace EXPORTED_FOR_KUNIT_TESTING, but does not import it.
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki