lib/crypto: Add ML-DSA signing [WIP]
Subsystems (6)
David Howells
maintainerv10 cover lib/crypto: Add ML-DSA signing [WIP]
Hi Herbert, Eric, et al.,
[Note this should be considered a work in progress as it's based on Eric's new implementation of ML-DSA].
Building on the SHA-3 lib-crypto patches now in Eric's tree, here's a set of patches does the following:
(1) Add ML-DSA signature verification code, as written by Eric.
(2) Add a kunit test in three instalments (due to size) to add some testing for the three different levels of ML-DSA (44, 65 and 87).
(3) Add a crypto_sig interface.
(4) Modify PKCS#7 support to allow kernel module signatures to carry authenticatedAttributes as OpenSSL refuses to let them be opted out of for ML-DSA (CMS_NOATTR). This adds an extra digest calculation to the process.
(5) Modify PKCS#7 to pass the authenticatedAttributes directly to the ML-DSA algorithm rather than passing over a digest as is done with RSA as ML-DSA wants to do its own hashing and will add other stuff into the hash. We could use hashML-DSA or an external mu instead, but they aren't standardised for CMS yet.
(6) Add support to the PKCS#7 and X.509 parsers for ML-DSA.
(7) Modify sign-file to handle OpenSSL not permitting CMS_NOATTR with
ML-DSA.
(8) Add ML-DSA to the choice of algorithm with which to sign modules. Note that this might need some more 'select' lines in the Kconfig to select the lib stuff as well.
This is based on Eric's libcrypto-next branch with patches added from his mldsa-v1 branch. Note that I've fixed his mldsa driver to have the missing MODULE_* info.
The patches can also be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=keys-pqc
David
Changes
=======
ver #10)
- Replaced the Leancrypto ML-DSA implementation with Eric's.
- Fixed Eric's implementation to have MODULE_* info.
- Added a patch to drive Eric's ML-DSA implementation from crypto_sig.
- Removed SHAKE256 from the list of available module hash algorithms.
- Changed a some more ML_DSA to MLDSA in config symbols.
ver #9)
- ML-DSA changes:
- Separate output into four modules (1 common, 3 strength-specific).
- Solves Kconfig issue with needing to select at least one strength.
- Separate the strength-specific crypto-lib APIs.
- This is now generated by preprocessor-templating.
- Remove the multiplexor code.
- Multiplex the crypto-lib APIs by C type.
- Fix the PKCS#7/X.509 code to have the correct algo names.
ver #8)
- Moved the ML-DSA code to lib/crypto/mldsa/.
- Renamed some bits from ml-dsa to mldsa.
- Created a simplified API and placed that in include/crypto/mldsa.h.
- Made the testing code use the simplified API.
- Fixed a warning about implicitly casting between uint16_t and __le16.
ver #7)
- Rebased on Eric's tree as that now contains all the necessary SHA-3 infrastructure and drop the SHA-3 patches from here.
- Added a minimal patch to provide shake256 support for crypto_sig.
- Got rid of the memory allocation wrappers.
- Removed the ML-DSA keypair generation code and the signing code, leaving only the signature verification code.
- Removed the secret key handling code.
- Removed the secret keys from the kunit tests and the signing testing.
- Removed some unused bits from the ML-DSA code.
- Downgraded the kdoc comments to ordinary comments, but keep the markup for easier comparison to Leancrypto.
ver #6)
- Added a patch to make the jitterentropy RNG use lib/sha3.
- Added back the crypto/sha3_generic changes.
- Added ML-DSA implementation (still needs more cleanup).
- Added kunit test for ML-DSA.
- Modified PKCS#7 to accommodate ML-DSA.
- Modified PKCS#7 and X.509 to allow ML-DSA to be specified and used.
- Modified sign-file to not use CMS_NOATTR with ML-DSA.
- Allowed SHA3 and SHAKE* algorithms for module signing default.
- Allowed ML-DSA-{44,65,87} to be selected as the module signing default.
ver #5)
- Fix gen-hash-testvecs.py to correctly handle algo names that contain a dash.
- Fix gen-hash-testvecs.py to not generate HMAC for SHA3-* or SHAKE* as these don't currently have HMAC variants implemented.
- Fix algo names to be correct.
- Fix kunit module description as it now tests all SHA3 variants.
ver #4)
- Fix a couple of arm64 build problems.
- Doc fixes:
- Fix the description of the algorithm to be closer to the NIST spec's terminology.
- Don't talk of finialising the context for XOFs.
- Don't say "Return: None".
- Declare the "Context" to be "Any context" and make no mention of the fact that it might use the FPU.
- Change "initialise" to "initialize".
- Don't warn that the context is relatively large for stack use.
- Use size_t for size parameters/variables.
- Make the module_exit unconditional.
- Dropped the crypto/ dir-affecting patches for the moment.
ver #3)
- Renamed conflicting arm64 functions.
- Made a separate wrapper API for each algorithm in the family.
- Removed sha3_init(), sha3_reinit() and sha3_final().
- Removed sha3_ctx::digest_size.
- Renamed sha3_ctx::partial to sha3_ctx::absorb_offset.
- Refer to the output of SHAKE* as "output" not "digest".
- Moved the Iota transform into the one-round function.
- Made sha3_update() warn if called after sha3_squeeze().
- Simplified the module-load test to not do update after squeeze.
- Added Return: and Context: kdoc statements and expanded the kdoc headers.
- Added an API description document.
- Overhauled the kunit tests.
- Only have one kunit test.
- Only call the general hash tester on one algo.
- Add separate simple cursory checks for the other algos.
- Add resqueezing tests.
- Add some NIST example tests.
- Changed crypto/sha3_generic to use this
- Added SHAKE128/256 to crypto/sha3_generic and crypto/testmgr
- Folded struct sha3_state into struct sha3_ctx.
ver #2)
- Simplify the endianness handling.
- Rename sha3_final() to sha3_squeeze() and don't clear the context at the end as it's permitted to continue calling sha3_final() to extract continuations of the digest (needed by ML-DSA).
- Don't reapply the end marker to the hash state in continuation sha3_squeeze() unless sha3_update() gets called again (needed by ML-DSA).
- Give sha3_squeeze() the amount of digest to produce as a parameter
rather than using ctx->digest_size and don't return the amount digested.
- Reimplement sha3_final() as a wrapper around sha3_squeeze() that extracts ctx->digest_size amount of digest and then zeroes out the context. The latter is necessary to avoid upsetting hash-test-template.h.
- Provide a sha3_reinit() function to clear the state, but to leave the parameters that indicate the hash properties unaffected, allowing for reuse.
- Provide a sha3_set_digestsize() function to change the size of the digest to be extracted by sha3_final(). sha3_squeeze() takes a parameter for this instead.
- Don't pass the digest size as a parameter to shake128/256_init() but rather default to 128/256 bits as per the function name.
- Provide a sha3_clear() function to zero out the context.
David Howells (5):
lib/crypto: tests: Add KUnit tests for ML-DSA
crypto: Add ML-DSA crypto_sig support
pkcs7: Allow the signing algo to calculate the digest itself pkcs7, x509: Add ML-DSA support
modsign: Enable ML-DSA module signing
Eric Biggers (3):
lib/crypto: Add ML-DSA verification support
lib/crypto: tests: Add ML-DSA-65 test cases
lib/crypto: tests: Add ML-DSA-87 test cases
Documentation/admin-guide/module-signing.rst | 15 +- certs/Kconfig | 21 + certs/Makefile | 3 + crypto/Kconfig | 10 + crypto/Makefile | 2 + crypto/asymmetric_keys/pkcs7_parser.c | 19 +- crypto/asymmetric_keys/pkcs7_verify.c | 52 +- crypto/asymmetric_keys/public_key.c | 7 + crypto/asymmetric_keys/x509_cert_parser.c | 24 + include/crypto/mldsa.h | 51 + include/crypto/public_key.h | 1 + include/linux/oid_registry.h | 5 + lib/crypto/Kconfig | 7 + lib/crypto/Makefile | 5 + lib/crypto/mldsa.c | 570 ++ lib/crypto/tests/Kconfig | 10 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/mldsa_kunit.c | 124 + .../tests/mldsa_pure_rejection_vectors_44.h | 489 ++ .../tests/mldsa_pure_rejection_vectors_65.h | 4702 ++++++++++++ .../tests/mldsa_pure_rejection_vectors_87.h | 6390 +++++++++++++++++ scripts/sign-file.c | 26 +- 22 files changed, 12498 insertions(+), 36 deletions(-) create mode 100644 include/crypto/mldsa.h create mode 100644 lib/crypto/mldsa.c create mode 100644 lib/crypto/tests/mldsa_kunit.c create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_44.h create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_65.h create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_87.h
David Howells
maintainerPATCH v10 1/8 lib/crypto: Add ML-DSA verification support
From: Eric Biggers <[email protected]>
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 600 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 3-5% faster, depending on the ML-DSA parameter set.
The API just consists of a single function mldsa_verify(), supporting the standard parameter sets (ML-DSA-44, ML-DSA-65, and ML-DSA-87) as selected by an enum. That's all that's actually needed.
HashML-DSA, incremental message hashing, and nonempty contexts aren't supported, as they aren't needed yet. Likewise, only verification support is included, since it's all the kernel needs. It's much simpler than full keygen+sign+verify support, and it means that constant-time code isn't needed either. (I've still used constant-time patterns in some places anyway, but technically it's not needed. And some steps in ML-DSA verification are inherently variable-time anyway.)
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.
--- include/crypto/mldsa.h | 51 ++++ lib/crypto/Kconfig | 7 + lib/crypto/Makefile | 5 + lib/crypto/mldsa.c | 570 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 633 insertions(+) create mode 100644 include/crypto/mldsa.h create mode 100644 lib/crypto/mldsa.c
include/crypto/mldsa.h new file +51 -0
@@ -0,0 +1,51 @@ +/* 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 an ML-DSA signature using the specified ML-DSA parameter set. +* The context string is assumed to be empty. +* +* Context: Might sleep +* Return: 0 if the signature is valid, -EBADMSG if 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
@@ -100,6 +100,13 @@ config CRYPTO_LIB_MD5_ARCH 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
lib/crypto/Makefile +5 -0
@@ -127,6 +127,11 @@ 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)
lib/crypto/mldsa.c new file +570 -0
570 lines not on this page · raw message
David Howells
maintainerPATCH v10 2/8 lib/crypto: tests: Add KUnit tests for ML-DSA
In reply to David HowellsAdd a KUnit test suite for ML-DSA, including:
- The ML-DSA-44 "Pure rejection tests" from Stephan Mueller's Leancrypto
- An ML-DSA-44 benchmark
Later patches will add test cases for ML-DSA-65 and ML-DSA-87.
(This needs some work. For one, there are no negative tests yet.)
--- lib/crypto/tests/Kconfig | 10 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/mldsa_kunit.c | 92 ++++ .../tests/mldsa_pure_rejection_vectors_44.h | 489 ++++++++++++++++++ 4 files changed, 592 insertions(+) create mode 100644 lib/crypto/tests/mldsa_kunit.c create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_44.h
lib/crypto/tests/Kconfig +10 -0
@@ -38,6 +38,16 @@ config CRYPTO_LIB_MD5_KUNIT_TEST 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 ML-DSA (Module-Lattice-Based Digital Signature + Algorithm). + config CRYPTO_LIB_POLY1305_KUNIT_TEST tristate "KUnit tests for Poly1305" if !KUNIT_ALL_TESTS depends on KUNIT
lib/crypto/tests/Makefile +1 -0
@@ -4,6 +4,7 @@ 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
lib/crypto/tests/mldsa_kunit.c new file +92 -0
92 lines not on this page · raw message
lib/crypto/tests/mldsa_pure_rejection_vectors_44.h new file +489 -0
489 lines not on this page · raw message
David Howells
maintainerPATCH v10 3/8 lib/crypto: tests: Add ML-DSA-65 test cases
In reply to David HowellsFrom: Eric Biggers <[email protected]>
Add test vectors and test cases for ML-DSA-65.
(This needs some work. For one, there are no negative tests yet.)
--- lib/crypto/tests/mldsa_kunit.c | 16 + .../tests/mldsa_pure_rejection_vectors_65.h | 4702 +++++++++++++++++ 2 files changed, 4718 insertions(+) create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_65.h
lib/crypto/tests/mldsa_kunit.c +16 -0
@@ -25,6 +25,9 @@ struct mldsa_testvector {static const struct mldsa_testvector mldsa_44_testvectors[] = { #include "mldsa_pure_rejection_vectors_44.h" }; +static const struct mldsa_testvector mldsa_65_testvectors[] = { +#include "mldsa_pure_rejection_vectors_65.h" +};static void do_mldsa_and_assert_success (struct kunit *test,enum mldsa_alg alg,const struct mldsa_testvector *tv)
@@ -47,6 +50,12 @@ static void test_mldsa44(struct kunit *test) ARRAY_SIZE(mldsa_44_testvectors)); } +static void test_mldsa65 (struct kunit *test) +{ +test_mldsa (test,MLDSA65 , mldsa_65_testvectors, +ARRAY_SIZE (mldsa_65_testvectors)); +} +static void benchmark_mldsa (struct kunit *test,enum mldsa_alg alg,const struct mldsa_testvector *tv) {
@@ -75,9 +84,16 @@ static void benchmark_mldsa44(struct kunit *test)benchmark_mldsa (test,MLDSA44 , &mldsa_44_testvectors[0 ]); } +static void benchmark_mldsa65 (struct kunit *test) +{ +benchmark_mldsa (test,MLDSA65 , &mldsa_65_testvectors[0 ]); +} +static struct kunit_case mldsa_kunit_cases[] = {KUNIT_CASE (test_mldsa44), +KUNIT_CASE (test_mldsa65),KUNIT_CASE (benchmark_mldsa44), +KUNIT_CASE (benchmark_mldsa65), {}, };
lib/crypto/tests/mldsa_pure_rejection_vectors_65.h new file +4702 -0
2,963 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
David Howells
maintainerPATCH v10 4/8 lib/crypto: tests: Add ML-DSA-87 test cases
In reply to David HowellsFrom: Eric Biggers <[email protected]>
Add test vectors and test cases for ML-DSA-87.
(This needs some work. For one, there are no negative tests yet.)
--- lib/crypto/tests/mldsa_kunit.c | 16 + .../tests/mldsa_pure_rejection_vectors_87.h | 6390 +++++++++++++++++ 2 files changed, 6406 insertions(+) create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_87.h
lib/crypto/tests/mldsa_kunit.c +16 -0
@@ -28,6 +28,9 @@ static const struct mldsa_testvector mldsa_44_testvectors[] = {static const struct mldsa_testvector mldsa_65_testvectors[] = { #include "mldsa_pure_rejection_vectors_65.h" }; +static const struct mldsa_testvector mldsa_87_testvectors[] = { +#include "mldsa_pure_rejection_vectors_87.h" +};static void do_mldsa_and_assert_success (struct kunit *test,enum mldsa_alg alg,const struct mldsa_testvector *tv)
@@ -56,6 +59,12 @@ static void test_mldsa65(struct kunit *test) ARRAY_SIZE(mldsa_65_testvectors)); } +static void test_mldsa87 (struct kunit *test) +{ +test_mldsa (test,MLDSA87 , mldsa_87_testvectors, +ARRAY_SIZE (mldsa_87_testvectors)); +} +static void benchmark_mldsa (struct kunit *test,enum mldsa_alg alg,const struct mldsa_testvector *tv) {
@@ -89,11 +98,18 @@ static void benchmark_mldsa65(struct kunit *test)benchmark_mldsa (test,MLDSA65 , &mldsa_65_testvectors[0 ]); } +static void benchmark_mldsa87 (struct kunit *test) +{ +benchmark_mldsa (test,MLDSA87 , &mldsa_87_testvectors[0 ]); +} +static struct kunit_case mldsa_kunit_cases[] = {KUNIT_CASE (test_mldsa44),KUNIT_CASE (test_mldsa65), +KUNIT_CASE (test_mldsa87),KUNIT_CASE (benchmark_mldsa44),KUNIT_CASE (benchmark_mldsa65), +KUNIT_CASE (benchmark_mldsa87), {}, };
lib/crypto/tests/mldsa_pure_rejection_vectors_87.h new file +6390 -0
2,961 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.
David Howells
maintainerPATCH v10 5/8 crypto: Add ML-DSA crypto_sig support
In reply to David Howells--- crypto/Kconfig | 10 ++++++++++ crypto/Makefile | 2 ++ 2 files changed, 12 insertions(+)
crypto/Kconfig +10 -0
@@ -344,6 +344,16 @@ config CRYPTO_ECRDSA One of the Russian cryptographic standard algorithms (called GOST algorithms). Only signature verification is implemented. +config CRYPTO_MLDSA + tristate "ML-DSA (Module-Lattice-Based Digital Signature Algorithm)" + select CRYPTO_SIG + select CRYPTO_LIB_MLDSA + select CRYPTO_LIB_SHA3 + help + ML-DSA (Module-Lattice-Based Digital Signature Algorithm) (FIPS-204). + + Only signature verification is implemented. + endmenu menu "Block ciphers"
crypto/Makefile +2 -0
@@ -60,6 +60,8 @@ ecdsa_generic-y += ecdsa-p1363.o ecdsa_generic-y += ecdsasignature.asn1.o obj-$(CONFIG_CRYPTO_ECDSA) += ecdsa_generic.o +obj-$(CONFIG_CRYPTO_MLDSA) += mldsa.o + crypto_acompress-y := acompress.o crypto_acompress-y += scompress.o obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
David Howells
maintainerRe: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
Meh. I forgot to git add crypto/mldsa.c.
David
---
crypto/Kconfig +10 -0
@@ -344,6 +344,16 @@ config CRYPTO_ECRDSA One of the Russian cryptographic standard algorithms (called GOST algorithms). Only signature verification is implemented. +config CRYPTO_MLDSA + tristate "ML-DSA (Module-Lattice-Based Digital Signature Algorithm)" + select CRYPTO_SIG + select CRYPTO_LIB_MLDSA + select CRYPTO_LIB_SHA3 + help + ML-DSA (Module-Lattice-Based Digital Signature Algorithm) (FIPS-204). + + Only signature verification is implemented. + endmenu menu "Block ciphers"
crypto/Makefile +2 -0
@@ -60,6 +60,8 @@ ecdsa_generic-y += ecdsa-p1363.o ecdsa_generic-y += ecdsasignature.asn1.o obj-$(CONFIG_CRYPTO_ECDSA) += ecdsa_generic.o +obj-$(CONFIG_CRYPTO_MLDSA) += mldsa.o + crypto_acompress-y := acompress.o crypto_acompress-y += scompress.o obj-$(CONFIG_CRYPTO_ACOMP2) += crypto_acompress.o
crypto/mldsa.c new file +201 -0
201 lines not on this page · raw message
Eric Biggers
maintainerRe: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
crypto/mldsa.c line 4
Quoted 10 lines · David Howells
crypto/mldsa.c new file +4
-0@@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* crypto_sig wrapper around ML-DSA library. +*/
Still not really sure what the point is. There's only one user of crypto_sig, and it could just call the ML-DSA functions directly.
- Eric
David Howells
maintainerRe: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
Quoted 2 lines · Eric Biggers
Still not really sure what the point is. There's only one user of
crypto_sig, and it could just call the ML-DSA functions directly.
Is it your aim to kill off the crypto/ dir and all the (old) crypto API?
Someone (not me) thought it worthwhile removing the akcipher algorithms out of crypto/asymmetric_keys/ and interfacing to them inside crypto_akcipher and crypto_sig.
Anyway, I'll continue using crypto_sig as that provides module autoload capabilities - meaning we don't have to build all the algorithms into the base kernel.
David
Jason A. Donenfeld
maintainerRe: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
Quoted 7 lines · David Howells
Quoted 2 lines · Eric Biggers
Still not really sure what the point is. There's only one user of
crypto_sig, and it could just call the ML-DSA functions directly.Is it your aim to kill off the
crypto/dir and all the (old) crypto API?
Probably entirely killing off the old API is going to be fraught because its abstraction has leaked out to userspace. But to the extent we can minimize its use over time, I think that's a good thing. Even for crypto usages that generalize to a few different ciphers of one variety or another, I think being explicit about which ciphers and having purpose-built dispatchers is usually a better route.
Jason
David Howells
maintainerRe: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
Quoted 11 lines · Jason A. Donenfeld
Quoted 4 lines
Quoted 2 lines
Still not really sure what the point is. There's only one user of
crypto_sig, and it could just call the ML-DSA functions directly.Is it your aim to kill off the
crypto/dir and all the (old) crypto API?Probably entirely killing off the old API is going to be fraught because its abstraction has leaked out to userspace. But to the extent we can minimize its use over time, I think that's a good thing. Even for crypto usages that generalize to a few different ciphers of one variety or another, I think being explicit about which ciphers and having purpose-built dispatchers is usually a better route.
How are you proposing handling the autoloading feature of the old API?
David
Jason A. Donenfeld
maintainerRe: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
Quoted 5 lines · David Howells
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Still not really sure what the point is. There's only one user of
crypto_sig, and it could just call the ML-DSA functions directly.Is it your aim to kill off the
crypto/dir and all the (old) crypto API?
Quoted 8 lines
Quoted 6 lines
Probably entirely killing off the old API is going to be fraught because its abstraction has leaked out to userspace. But to the extent we can minimize its use over time, I think that's a good thing. Even for crypto usages that generalize to a few different ciphers of one variety or another, I think being explicit about which ciphers and having purpose-built dispatchers is usually a better route.
How are you proposing handling the autoloading feature of the old API?
I don't know. Not all features will have direct replacements. Not all usages will be replaced. Not all use cases benefit from being replaced. You asked if it was an "aim." I replied by telling you that I think killing it is going to be difficult, but that over time, usage will decline. I think that'll be a natural thing. For now, when something uses the library API, there's a pretty easy and obvious case to be made for it, as there are still such obvious low hanging use cases. I suppose in a while, we might run out of those perhaps. But that hasn't happened yet, I guess.
Jason
Eric Biggers
maintainerRe: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
Quoted 25 lines · Jason A. Donenfeld
Quoted 5 lines · David Howells
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
Still not really sure what the point is. There's only one user of
crypto_sig, and it could just call the ML-DSA functions directly.Is it your aim to kill off the
crypto/dir and all the (old) crypto API?Quoted 8 lines
Quoted 6 lines
Probably entirely killing off the old API is going to be fraught because its abstraction has leaked out to userspace. But to the extent we can minimize its use over time, I think that's a good thing. Even for crypto usages that generalize to a few different ciphers of one variety or another, I think being explicit about which ciphers and having purpose-built dispatchers is usually a better route.
How are you proposing handling the autoloading feature of the old API?
I don't know. Not all features will have direct replacements. Not all usages will be replaced. Not all use cases benefit from being replaced. You asked if it was an "aim." I replied by telling you that I think killing it is going to be difficult, but that over time, usage will decline. I think that'll be a natural thing. For now, when something uses the library API, there's a pretty easy and obvious case to be made for it, as there are still such obvious low hanging use cases. I suppose in a while, we might run out of those perhaps. But that hasn't happened yet, I guess.
Quoted 1 line
Jason
+1 to what Jason said. The traditional API generally isn't going away. However, there's no need to add new functionality to it when the library is a better fit. The crypto_sig abstraction doesn't seem very helpful, since it was added only recently and has only one user (public_key).
The traditional crypto API's dynamic loading by name also tends to be more harmful than helpful. Linux users and distros keep running into problems where algorithms aren't available in the initramfs when they should be, or where the slow generic code is used instead of the optimized code. The direct linking finally just fixes that.
You may also still be thinking of the ML-DSA code as something heavyweight. I reduced it to under 4 KB of object code.
It should also become the recommended algorithm anyway, right?
Either way, 4 KB seems awfully small to be wanting to be dynamically loaded. Last year I shrunk the x86_64 AES-GCM code (which most Linux distros build in to avoid the dynamic loading issues) by over 200 KB. No one even seemed to care that much... We can add a lot of stuff before we're even back to where we were before that.
- Eric
David Howells
maintainerRe: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
Quoted 1 line · Eric Biggers
It should also become the recommended algorithm anyway, right?
It's used for more than just module signing. IMA, for example.
David
RE: [PATCH v10 5/8] crypto: Add ML-DSA crypto_sig support
In reply to David HowellsQuoted 4 lines
-----Original Message-----
From: David Howells <[email protected]>
Sent: Monday, November 24, 2025 3:13 AM
Subject: Re: [PATCH v10 5/8] crypto: Add ML-DSAcrypto_sigsupport
...
Quoted 1 line
+++
b/crypto/mldsa.c
...
Quoted 16 lines
crypto/mldsa.c new file +16
-0+static unsigned int crypto_mldsa_key_size (struct crypto_sig *tfm) +{ +struct crypto_mldsa_ctx *ctx =crypto_sig_ctx (tfm); + +switch (ctx->strength) { +case MLDSA44 : +return MLDSA44_PUBLIC_KEY_SIZE ; +case MLDSA65 : +return MLDSA65_PUBLIC_KEY_SIZE ; +case MLDSA87 : +return MLDSA87_PUBLIC_KEY_SIZE ; +default : +WARN_ON_ONCE (1 ); +return 0 ; + } +}
...
Quoted 5 lines
crypto/mldsa.c new file +5
-0+static unsigned int crypto_mldsa_max_size (struct crypto_sig *tfm) +{ +struct crypto_mldsa_ctx *ctx =crypto_sig_ctx (tfm); + +switch (ctx->strength) {
crypto/mldsa.c line 91
Quoted 10 lines
crypto/mldsa.c new file +10
-0+case MLDSA44 : +return MLDSA44_SIGNATURE_SIZE ; +case MLDSA65 : +return MLDSA65_SIGNATURE_SIZE ; +case MLDSA87 : +return MLDSA87_SIGNATURE_SIZE ; +default : +WARN_ON_ONCE (1 ); +return 0 ; + }
In case there's any way userspace can trigger those, I'd rather not have any WARN stack dumps.
David Howells
maintainerPATCH v10 6/8 pkcs7: Allow the signing algo to calculate the digest itself
In reply to David HowellsThe ML-DSA public key algorithm really wants to calculate the message digest itself, rather than having the digest precalculated and fed to it separately as RSA does[*]. The kernel's PKCS#7 parser, however, is designed around the latter approach.
[*] ML-DSA does allow for an "external mu", but CMS doesn't yet have that standardised.
Fix this by noting in the public_key_signature struct when the signing algorithm is going to want this and then, rather than doing the digest of the authenticatedAttributes ourselves and overwriting the sig->digest with that, replace sig->digest with a copy of the contents of the authenticatedAttributes section and adjust the digest length to match.
This will then be fed to the public key algorithm as normal which can do what it wants with the data.
--- crypto/asymmetric_keys/pkcs7_parser.c | 4 +-- crypto/asymmetric_keys/pkcs7_verify.c | 48 ++++++++++++++++++--------- include/crypto/public_key.h | 1 + 3 files changed, 36 insertions(+), 17 deletions(-)
crypto/asymmetric_keys/pkcs7_parser.c +2 -2
@@ -599,8 +599,8 @@ int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen, }/* We need to switch the 'CONT 0' to a 'SET OF' when we digest */ - sinfo->authattrs = value - (hdrlen -1 ); - sinfo->authattrs_len = vlen + (hdrlen -1 ); + sinfo->authattrs = value - hdrlen; + sinfo->authattrs_len = vlen + hdrlen;return 0 ; }
crypto/asymmetric_keys/pkcs7_verify.c +33 -15
61 lines not on this page · raw message
include/crypto/public_key.h +1 -0
@@ -46,6 +46,7 @@ struct public_key_signature {u8 *digest;u32 s_size;/* Number of bytes in signature */ u32 digest_size;/* Number of bytes in digest */ +bool algo_does_hash;/* Public key algo does its own hashing */ const char *pkey_algo;const char *hash_algo;const char *encoding;
David Howells
maintainerPATCH v10 7/8 pkcs7, x509: Add ML-DSA support
In reply to David HowellsAdd support for ML-DSA keys and signatures to the PKCS#7 and X.509 implementations.
--- crypto/asymmetric_keys/pkcs7_parser.c | 15 ++++++++++++++ crypto/asymmetric_keys/public_key.c | 7 +++++++ crypto/asymmetric_keys/x509_cert_parser.c | 24 +++++++++++++++++++++++ include/linux/oid_registry.h | 5 +++++ 4 files changed, 51 insertions(+)
crypto/asymmetric_keys/pkcs7_parser.c +15 -0
@@ -297,6 +297,21 @@ int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen, ctx->sinfo->sig->pkey_algo ="ecrdsa" ; ctx->sinfo->sig->encoding ="raw" ;break ; +case OID_id_ml_dsa_44: + ctx->sinfo->sig->pkey_algo ="mldsa44" ; + ctx->sinfo->sig->encoding ="raw" ; + ctx->sinfo->sig->algo_does_hash = true; +break ; +case OID_id_ml_dsa_65: + ctx->sinfo->sig->pkey_algo ="mldsa65" ; + ctx->sinfo->sig->encoding ="raw" ; + ctx->sinfo->sig->algo_does_hash = true; +break ; +case OID_id_ml_dsa_87: + ctx->sinfo->sig->pkey_algo ="mldsa87" ; + ctx->sinfo->sig->encoding ="raw" ; + ctx->sinfo->sig->algo_does_hash = true; +break ;default :printk ("Unsupported pkey algo: %u\n" , ctx->last_oid);return -ENOPKG ;
crypto/asymmetric_keys/public_key.c +7 -0
@@ -142,6 +142,13 @@ software_key_determine_akcipher(const struct public_key *pkey,if (strcmp (hash_algo,"streebog256" ) !=0 &&strcmp (hash_algo,"streebog512" ) !=0 )return -EINVAL ; + }else if (strcmp (pkey->pkey_algo,"mldsa44" ) ==0 || +strcmp (pkey->pkey_algo,"mldsa65" ) ==0 || +strcmp (pkey->pkey_algo,"mldsa87" ) ==0 ) { +if (strcmp (encoding,"raw" ) !=0 ) +return -EINVAL ; +if (!hash_algo) +return -EINVAL ; }else {/* Unknown public key algorithm */ return -ENOPKG ;
crypto/asymmetric_keys/x509_cert_parser.c +24 -0
@@ -257,6 +257,15 @@ int x509_note_sig_algo(void *context, size_t hdrlen, unsigned char tag,case OID_gost2012Signature512: ctx->cert->sig->hash_algo ="streebog512" ; goto ecrdsa; +case OID_id_ml_dsa_44: + ctx->cert->sig->pkey_algo ="mldsa44" ; + goto ml_dsa; +case OID_id_ml_dsa_65: + ctx->cert->sig->pkey_algo ="mldsa65" ; + goto ml_dsa; +case OID_id_ml_dsa_87: + ctx->cert->sig->pkey_algo ="mldsa87" ; + goto ml_dsa; } rsa_pkcs1:
@@ -274,6 +283,12 @@ int x509_note_sig_algo(void *context, size_t hdrlen, unsigned char tag, ctx->cert->sig->encoding ="x962" ; ctx->sig_algo = ctx->last_oid;return 0 ; +ml_dsa: + ctx->cert->sig->algo_does_hash = true; + ctx->cert->sig->hash_algo = ctx->cert->sig->pkey_algo; + ctx->cert->sig->encoding ="raw" ; + ctx->sig_algo = ctx->last_oid; +return 0 ; }/*
@@ -524,6 +539,15 @@ int x509_extract_key_data(void *context, size_t hdrlen,return -ENOPKG ; }break ; +case OID_id_ml_dsa_44: + ctx->cert->pub->pkey_algo ="mldsa44" ; +break ; +case OID_id_ml_dsa_65: + ctx->cert->pub->pkey_algo ="mldsa65" ; +break ; +case OID_id_ml_dsa_87: + ctx->cert->pub->pkey_algo ="mldsa87" ; +break ;default :return -ENOPKG ; }
include/linux/oid_registry.h +5 -0
@@ -145,6 +145,11 @@ enum OID { OID_id_rsassa_pkcs1_v1_5_with_sha3_384,/* 2.16.840.1.101.3.4.3.15 */ OID_id_rsassa_pkcs1_v1_5_with_sha3_512,/* 2.16.840.1.101.3.4.3.16 */ +/* NIST FIPS-204 ML-DSA (Dilithium) */ + OID_id_ml_dsa_44,/* 2.16.840.1.101.3.4.3.17 */ + OID_id_ml_dsa_65,/* 2.16.840.1.101.3.4.3.18 */ + OID_id_ml_dsa_87,/* 2.16.840.1.101.3.4.3.19 */ + OID__NR };
David Howells
maintainerPATCH v10 8/8 modsign: Enable ML-DSA module signing
In reply to David HowellsAllow ML-DSA module signing to be enabled.
Note that openssl's CMS_*() function suite does not, as of openssl-3.5.1, support the use of CMS_NOATTR with ML-DSA, so the prohibition against using authenticatedAttributes with module signing has to be removed. The selected digest then applies only to the algorithm used to calculate the digest stored in the messageDigest attribute.
The ML-DSA algorithm uses its own internal choice of digest (SHAKE256) without regard to what's specified in the CMS message. This is, in theory, configurable, but there's currently no hook in the crypto_sig API to do that, though possibly it could be done by parameterising the name of the algorithm, e.g. ("mldsa87(sha512)").
--- Documentation/admin-guide/module-signing.rst | 15 +++++------ certs/Kconfig | 21 ++++++++++++++++ certs/Makefile | 3 +++ crypto/asymmetric_keys/pkcs7_verify.c | 4 --- scripts/sign-file.c | 26 ++++++++++++++------ 5 files changed, 50 insertions(+), 19 deletions(-)
Documentation/admin-guide/module-signing.rst +8 -7
@@ -28,10 +28,11 @@ trusted userspace bits. This facility uses X.509 ITU-T standard certificates to encode the public keys involved. The signatures are not themselves encoded in any industrial standard -type. The built-in facility currently only supports the RSA & NIST P-384 ECDSA -public key signing standard (though it is pluggable and permits others to be -used). The possible hash algorithms that can be used are SHA-2 and SHA-3 of -sizes 256, 384, and 512 (the algorithm is selected by data in the signature). +type. The built-in facility currently only supports the RSA, NIST P-384 ECDSA +and NIST FIPS-204 ML-DSA (Dilithium) public key signing standards (though it is +pluggable and permits others to be used). For RSA and ECDSA, the possible hash +algorithms that can be used are SHA-2 and SHA-3 of sizes 256, 384, and 512 (the +algorithm is selected by data in the signature); ML-DSA uses SHAKE256. ==========================
@@ -146,9 +147,9 @@ into vmlinux) using parameters in the:: file (which is also generated if it does not already exist). -One can select between RSA (``MODULE_SIG_KEY_TYPE_RSA``) and ECDSA -(``MODULE_SIG_KEY_TYPE_ECDSA``) to generate either RSA 4k or NIST -P-384 keypair. +One can select between RSA (``MODULE_SIG_KEY_TYPE_RSA``), ECDSA +(``MODULE_SIG_KEY_TYPE_ECDSA``) and ML-DSA (``MODULE_SIG_KEY_TYPE_MLDSA_*``) to +generate an RSA 4k, a NIST P-384 keypair or an ML-DSA 44, 65 or 87 keypair. It is strongly recommended that you provide your own x509.genkey file.
certs/Kconfig +21 -0
@@ -39,6 +39,27 @@ config MODULE_SIG_KEY_TYPE_ECDSA Note: Remove all ECDSA signing keys, e.g. certs/signing_key.pem, when falling back to building Linux 5.14 and older kernels. +config MODULE_SIG_KEY_TYPE_MLDSA_44 + bool "ML-DSA (Dilithium) 44" + select CRYPTO_MLDSA + help + Use an ML-DSA (Dilithium) 44 key (NIST FIPS 204) for module signing + with a SHAKE256 'hash' of the authenticatedAttributes. + +config MODULE_SIG_KEY_TYPE_MLDSA_65 + bool "ML-DSA (Dilithium) 65" + select CRYPTO_MLDSA + help + Use an ML-DSA (Dilithium) 65 key (NIST FIPS 204) for module signing + with a SHAKE256 'hash' of the authenticatedAttributes. + +config MODULE_SIG_KEY_TYPE_MLDSA_87 + bool "ML-DSA (Dilithium) 87" + select CRYPTO_MLDSA + help + Use an ML-DSA (Dilithium) 87 key (NIST FIPS 204) for module signing + with a SHAKE256 'hash' of the authenticatedAttributes. + endchoice config SYSTEM_TRUSTED_KEYRING
certs/Makefile +3 -0
@@ -43,6 +43,9 @@ targets += x509_certificate_list ifeq ($(CONFIG_MODULE_SIG_KEY),certs/signing_key.pem) keytype-$(CONFIG_MODULE_SIG_KEY_TYPE_ECDSA) := -newkey ec -pkeyopt ec_paramgen_curve:secp384r1 +keytype-$(CONFIG_MODULE_SIG_KEY_TYPE_MLDSA_44) := -newkey ml-dsa-44 +keytype-$(CONFIG_MODULE_SIG_KEY_TYPE_MLDSA_65) := -newkey ml-dsa-65 +keytype-$(CONFIG_MODULE_SIG_KEY_TYPE_MLDSA_87) := -newkey ml-dsa-87 quiet_cmd_gen_key = GENKEY $@ cmd_gen_key = openssl req -new -nodes -utf8 -$(CONFIG_MODULE_SIG_HASH) -days 36500 \
crypto/asymmetric_keys/pkcs7_verify.c +0 -4
@@ -424,10 +424,6 @@ int pkcs7_verify(struct pkcs7_message *pkcs7,pr_warn ("Invalid module sig (not pkcs7-data)\n" );return -EKEYREJECTED ; } -if (pkcs7->have_authattrs) { -pr_warn ("Invalid module sig (has authattrs)\n" ); -return -EKEYREJECTED ; - }break ;case VERIFYING_FIRMWARE_SIGNATURE :if (pkcs7->data_type != OID_data) {
scripts/sign-file.c +18 -8
@@ -315,18 +315,28 @@ int main(int argc, char **argv)ERR (!digest_algo,"EVP_get_digestbyname" );#ifndef USE_PKCS7 + +unsigned int flags = +CMS_NOCERTS | +CMS_PARTIAL | +CMS_BINARY | +CMS_DETACHED | +CMS_STREAM | +CMS_NOSMIMECAP | +CMS_NO_SIGNING_TIME | + use_keyid; +if (!EVP_PKEY_is_a (private_key,"ML-DSA-44" ) && + !EVP_PKEY_is_a (private_key,"ML-DSA-65" ) && + !EVP_PKEY_is_a (private_key,"ML-DSA-87" )) + flags |= use_signed_attrs; +/* Load the signature message from the digest buffer. */ - cms =CMS_sign (NULL ,NULL ,NULL ,NULL , -CMS_NOCERTS |CMS_PARTIAL |CMS_BINARY | -CMS_DETACHED |CMS_STREAM ); + cms =CMS_sign (NULL ,NULL ,NULL ,NULL , flags);ERR (!cms,"CMS_sign" ); -ERR (!CMS_add1_signer (cms, x509, private_key, digest_algo, -CMS_NOCERTS |CMS_BINARY | -CMS_NOSMIMECAP | use_keyid | - use_signed_attrs), +ERR (!CMS_add1_signer (cms, x509, private_key, digest_algo, flags),"CMS_add1_signer" ); -ERR (CMS_final (cms, bm,NULL ,CMS_NOCERTS |CMS_BINARY ) !=1 , +ERR (CMS_final (cms, bm,NULL , flags) !=1 ,"CMS_final" );#else