lib/crypto: ML-DSA verification support
Subsystems (3)
Eric Biggers
maintainerv2 cover lib/crypto: ML-DSA verification support
This series is targeting libcrypto-next. It can also be retrieved from:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/linux.git mldsa-v2
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 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 | 53 + lib/crypto/Kconfig | 7 + lib/crypto/Makefile | 5 + lib/crypto/mldsa.c | 651 ++++++++++ lib/crypto/tests/Kconfig | 9 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/mldsa-testvecs.h | 1877 +++++++++++++++++++++++++++++ lib/crypto/tests/mldsa_kunit.c | 381 ++++++ 8 files changed, 2984 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: c0127f3ad65e8b21752f7b4d7dbe7e4ab5b5c62d
Signature · 1 line
2.52.0
Eric Biggers
maintainerPATCH v2 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.
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.
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 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 | 53 ++++ lib/crypto/Kconfig | 7 + lib/crypto/Makefile | 5 + lib/crypto/mldsa.c | 651 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 716 insertions(+) create mode 100644 include/crypto/mldsa.h create mode 100644 lib/crypto/mldsa.c
include/crypto/mldsa.h new file +53 -0
@@ -0,0 +1,53 @@ +/* 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 right away. +* @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 right away. +* +* 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 and public key are +* well-formed but the signature is invalid, or -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); + +#endif /* _CRYPTO_MLDSA_H */
lib/crypto/Kconfig +7 -0
@@ -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
@@ -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 +651 -0
651 lines not on this page · raw message
Signature · 1 line
2.52.0
Eric Biggers
maintainerPATCH v2 2/2 lib/crypto: tests: Add KUnit tests for ML-DSA verification
In reply to Eric BiggersAdd 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
- 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 | 1877 +++++++++++++++++++++++++++++ lib/crypto/tests/mldsa_kunit.c | 381 ++++++ 4 files changed, 2268 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
@@ -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
@@ -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 +1877 -0
1,877 lines not on this page · raw message
lib/crypto/tests/mldsa_kunit.c new file +381 -0
381 lines not on this page · raw message
Signature · 1 line
2.52.0
RE: [PATCH v2 1/2] lib/crypto: Add ML-DSA verification support
In reply to Eric BiggersQuoted 5 lines
-----Original Message-----
From: Eric Biggers <[email protected]>
Subject: [PATCH v2 1/2] lib/crypto: Add ML-DSA verification support
...
Quoted 18 lines
+++
b/lib/crypto/mldsa.clib/crypto/mldsa.c new file +16
-0+} mldsa_parameter_sets[] = { + [MLDSA44] = { + .ctilde_len = 32, + .pk_len = MLDSA44_PUBLIC_KEY_SIZE, + .sig_len = MLDSA44_SIGNATURE_SIZE, + }, + [MLDSA65] = { + .ctilde_len = 48, + .pk_len = MLDSA65_PUBLIC_KEY_SIZE, + .sig_len = MLDSA65_SIGNATURE_SIZE, + }, + [MLDSA87] = { + .ctilde_len = 64, + .pk_len = MLDSA87_PUBLIC_KEY_SIZE, + .sig_len = MLDSA87_SIGNATURE_SIZE, + },
...
Quoted 1 line
(quoted diff) +1
-0+ union {
...
Quoted 3 lines
lib/crypto/mldsa.c new file +3
-0+/* The commitment hash. Real length is params->ctilde_len */ +u8 ctildeprime[64 ]; + };
...
Quoted 3 lines
lib/crypto/mldsa.c new file +3
-0+/* Recreate the challenge c from the signer's commitment hash. */ +sample_in_ball (&ws->c, ctilde, params->ctilde_len, params->tau, + &ws->shake);
...
Quoted 2 lines
lib/crypto/mldsa.c new file +2
-0+/* Finish computing ctildeprime. */ +shake_squeeze (&ws->shake, ws->ctildeprime, params->ctilde_len);
...
lib/crypto/mldsa.c line 644
Quoted 3 lines
lib/crypto/mldsa.c new file +3
-0+/* Verify that ctilde == ctildeprime. */ +if (memcmp (ws->ctildeprime, ctilde, params->ctilde_len) !=0 ) +return -EKEYREJECTED ;
Is there any way to ensure that each ctilde_len value is <= 64 and <= the corresponding .sig_size value at compile time so there's no risk of overflowing any buffers?
Eric Biggers
maintainerRe: [PATCH v2 1/2] lib/crypto: Add ML-DSA verification support
Quoted 39 lines · Robert (Servers)
Quoted 5 lines
-----Original Message-----
From: Eric Biggers <[email protected]>
Subject: [PATCH v2 1/2] lib/crypto: Add ML-DSA verification support...
Quoted 18 lines
+++
b/lib/crypto/mldsa.clib/crypto/mldsa.c new file +16
-0+} mldsa_parameter_sets[] = { + [MLDSA44] = { + .ctilde_len = 32, + .pk_len = MLDSA44_PUBLIC_KEY_SIZE, + .sig_len = MLDSA44_SIGNATURE_SIZE, + }, + [MLDSA65] = { + .ctilde_len = 48, + .pk_len = MLDSA65_PUBLIC_KEY_SIZE, + .sig_len = MLDSA65_SIGNATURE_SIZE, + }, + [MLDSA87] = { + .ctilde_len = 64, + .pk_len = MLDSA87_PUBLIC_KEY_SIZE, + .sig_len = MLDSA87_SIGNATURE_SIZE, + },...
Quoted 1 line
(quoted diff) +1
-0+ union {...
Quoted 3 lines
lib/crypto/mldsa.c new file +3
-0+/* The commitment hash. Real length is params->ctilde_len */ +u8 ctildeprime[64 ]; + };...
Quoted 3 lines
lib/crypto/mldsa.c new file +3
-0+/* Recreate the challenge c from the signer's commitment hash. */ +sample_in_ball (&ws->c, ctilde, params->ctilde_len, params->tau, + &ws->shake);...
Quoted 2 lines
lib/crypto/mldsa.c new file +2
-0+/* Finish computing ctildeprime. */ +shake_squeeze (&ws->shake, ws->ctildeprime, params->ctilde_len);...
Quoted 7 lines
lib/crypto/mldsa.c line 644
Quoted 3 lines
lib/crypto/mldsa.c new file +3
-0+/* Verify that ctilde == ctildeprime. */ +if (memcmp (ws->ctildeprime, ctilde, params->ctilde_len) !=0 ) +return -EKEYREJECTED ;Is there any way to ensure that each
ctilde_lenvalue is <= 64 and <= the corresponding .sig_size value at compile time so there's no risk of overflowing any buffers?
Not at compile time, unless we do some fancy validation of each of the three ctilde_len values against the max3() of the signature lengths using macros. It could be checked at runtime in a module_init function. Seems pointless though, given that these parameters are fixed in the ML-DSA specification. The specification uses parameters that make sense and are consistent with each other.
- Eric