Skip to contentSkip to conversation

lib/crypto: Add ML-DSA signing

PATCH Superseded David Howells 1 participant 10 messages Nov 14, 2025

Subsystems (7)

v8 of 16 Superseded Latest is v16 →

lore
Share & tools
lore

David Howells

author maintainer
#0 lore raw

v8 cover lib/crypto: Add ML-DSA signing

Hi Herbert, Eric, et al.,

Building on the SHA-3 lib-crypto patches now in Eric's tree, here's a set of patches does the following:

(1) Add SHAKE-256 crypto_sig support, generating 32-byte fixed output. The XOF features aren't available through this. SHAKE-128 crypto_sig support isn't required for ML-DSA, so I haven't implemented that at this time.

(2) Add ML-DSA signature verification code, extracted from Stephan Mueller's Leancrypto project. The primary interface is intended to be through crypto_sig.

(3) Add a simplified ML-DSA API for direct lib-crypto access. Possibly this should be conditional as the main access (from PKCS#7/X.509) is going to be through crypto_sig.

(4) 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).

(5) 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.

(6) 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.

(7) Add support to the PKCS#7 and X.509 parsers for ML-DSA.

(8) Modify sign-file to handle OpenSSL not permitting CMS_NOATTR with

     ML-DSA.

(9) Allow SHA-3 algorithms, including SHAKE256, to be used for the message digest and add ML-DSA to the choice of algorithm with which to sign.

With that, ML-DSA signing appears to work.

This is based on Eric's libcrypto-next branch.

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 #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 (9):
crypto: Add support for shake256 through crypto_shash
crypto: Add ML-DSA/Dilithium verify support
mldsa: Add a simpler API
crypto: Add ML-DSA-44 pure rejection test vectors as a kunit test
crypto: Add ML-DSA-65 pure rejection test vectors as a kunit test
crypto: Add ML-DSA-87 pure rejection test vectors as a kunit test
pkcs7: Allow the signing algo to calculate the digest itself pkcs7, x509: Add ML-DSA support
modsign: Enable ML-DSA module signing

 Documentation/admin-guide/module-signing.rst  |   15 +-
 Documentation/crypto/index.rst                |    1 +
 Documentation/crypto/mldsa.rst                |  111 +
 certs/Kconfig                                 |   24 +
 certs/Makefile                                |    3 +
 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 +
 crypto/sha3.c                                 |   42 +
 include/crypto/mldsa.h                        |   34 +
 include/crypto/public_key.h                   |    1 +
 include/linux/oid_registry.h                  |    5 +
 kernel/module/Kconfig                         |    5 +
 lib/crypto/Kconfig                            |    1 +
 lib/crypto/Makefile                           |    2 +
 lib/crypto/mldsa/Kconfig                      |   28 +
 lib/crypto/mldsa/Makefile                     |   15 +
 lib/crypto/mldsa/crypto_mldsa.c               |  332 +
 lib/crypto/mldsa/dilithium.h                  |  545 ++
 lib/crypto/mldsa/dilithium_44.c               |   33 +
 lib/crypto/mldsa/dilithium_44.h               |  282 +
 lib/crypto/mldsa/dilithium_65.c               |   33 +
 lib/crypto/mldsa/dilithium_65.h               |  282 +
 lib/crypto/mldsa/dilithium_87.c               |   33 +
 lib/crypto/mldsa/dilithium_87.h               |  282 +
 lib/crypto/mldsa/dilithium_api.c              |  429 ++
 lib/crypto/mldsa/dilithium_debug.h            |   49 +
 lib/crypto/mldsa/dilithium_ntt.c              |   89 +
 lib/crypto/mldsa/dilithium_ntt.h              |   35 +
 lib/crypto/mldsa/dilithium_pack.h             |  119 +
 lib/crypto/mldsa/dilithium_poly.c             |  377 +
 lib/crypto/mldsa/dilithium_poly.h             |  181 +
 lib/crypto/mldsa/dilithium_poly_c.h           |  141 +
 lib/crypto/mldsa/dilithium_poly_common.h      |   35 +
 lib/crypto/mldsa/dilithium_polyvec.h          |  343 +
 lib/crypto/mldsa/dilithium_polyvec_c.h        |   81 +
 lib/crypto/mldsa/dilithium_reduce.h           |   85 +
 lib/crypto/mldsa/dilithium_rounding.c         |  128 +
 lib/crypto/mldsa/dilithium_service_helpers.h  |   99 +
 lib/crypto/mldsa/dilithium_signature_c.c      |  102 +
 lib/crypto/mldsa/dilithium_signature_c.h      |   37 +
 lib/crypto/mldsa/dilithium_signature_helper.c |   79 +
 lib/crypto/mldsa/dilithium_signature_impl.h   |  370 +
 lib/crypto/mldsa/dilithium_type.h             |  102 +
 lib/crypto/mldsa/dilithium_zetas.c            |   67 +
 lib/crypto/mldsa/mldsa_api.c                  |  186 +
 .../mldsa/signature_domain_separation.c       |  203 +
 .../mldsa/signature_domain_separation.h       |   30 +
 lib/crypto/tests/Kconfig                      |   10 +
 lib/crypto/tests/Makefile                     |    1 +
 lib/crypto/tests/mldsa_kunit.c                |  103 +
 .../tests/mldsa_pure_rejection_vectors_44.h   |  489 ++
 .../tests/mldsa_pure_rejection_vectors_65.h   | 4741 ++++++++++++
 .../tests/mldsa_pure_rejection_vectors_87.h   | 6456 +++++++++++++++++
 scripts/sign-file.c                           |   26 +-
 56 files changed, 17368 insertions(+), 36 deletions(-)
 create mode 100644 Documentation/crypto/mldsa.rst
 create mode 100644 include/crypto/mldsa.h
 create mode 100644 lib/crypto/mldsa/Kconfig
 create mode 100644 lib/crypto/mldsa/Makefile
 create mode 100644 lib/crypto/mldsa/crypto_mldsa.c
 create mode 100644 lib/crypto/mldsa/dilithium.h
 create mode 100644 lib/crypto/mldsa/dilithium_44.c
 create mode 100644 lib/crypto/mldsa/dilithium_44.h
 create mode 100644 lib/crypto/mldsa/dilithium_65.c
 create mode 100644 lib/crypto/mldsa/dilithium_65.h
 create mode 100644 lib/crypto/mldsa/dilithium_87.c
 create mode 100644 lib/crypto/mldsa/dilithium_87.h
 create mode 100644 lib/crypto/mldsa/dilithium_api.c
 create mode 100644 lib/crypto/mldsa/dilithium_debug.h
 create mode 100644 lib/crypto/mldsa/dilithium_ntt.c
 create mode 100644 lib/crypto/mldsa/dilithium_ntt.h
 create mode 100644 lib/crypto/mldsa/dilithium_pack.h
 create mode 100644 lib/crypto/mldsa/dilithium_poly.c
 create mode 100644 lib/crypto/mldsa/dilithium_poly.h
 create mode 100644 lib/crypto/mldsa/dilithium_poly_c.h
 create mode 100644 lib/crypto/mldsa/dilithium_poly_common.h
 create mode 100644 lib/crypto/mldsa/dilithium_polyvec.h
 create mode 100644 lib/crypto/mldsa/dilithium_polyvec_c.h
 create mode 100644 lib/crypto/mldsa/dilithium_reduce.h
 create mode 100644 lib/crypto/mldsa/dilithium_rounding.c
 create mode 100644 lib/crypto/mldsa/dilithium_service_helpers.h
 create mode 100644 lib/crypto/mldsa/dilithium_signature_c.c
 create mode 100644 lib/crypto/mldsa/dilithium_signature_c.h
 create mode 100644 lib/crypto/mldsa/dilithium_signature_helper.c
 create mode 100644 lib/crypto/mldsa/dilithium_signature_impl.h
 create mode 100644 lib/crypto/mldsa/dilithium_type.h
 create mode 100644 lib/crypto/mldsa/dilithium_zetas.c
 create mode 100644 lib/crypto/mldsa/mldsa_api.c
 create mode 100644 lib/crypto/mldsa/signature_domain_separation.c
 create mode 100644 lib/crypto/mldsa/signature_domain_separation.h
 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
author maintainer
#1 lore raw

PATCH v8 1/9 crypto: Add support for shake256 through crypto_shash

Add shake256 support to the SHA-3 crypto_sig module so that ML-DSA can use it.

cc Stephan Mueller <[email protected]>
cc Eric Biggers <[email protected]>
cc Jason A. Donenfeld <[email protected]>
cc Ard Biesheuvel <[email protected]>
cc Herbert Xu <[email protected]>
---
 crypto/sha3.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
crypto/sha3.c +42 -0
index 8f364979ec89..be1d18baee8d 100644
@@ -9,6 +9,7 @@
 #include <linux/module.h>
 
 #define SHA3_CTX(desc) ((struct sha3_ctx *)shash_desc_ctx(desc))
+#define SHAKE_CTX(desc) ((struct shake_ctx *)shash_desc_ctx(desc))
 
 static int crypto_sha3_224_init(struct shash_desc *desc)
 {
@@ -87,6 +88,36 @@ static int crypto_sha3_import_core(struct shash_desc *desc, const void *in)
 	return 0;
 }
 
+static int crypto_shake256_init(struct shash_desc *desc)
+{
+	shake256_init(SHAKE_CTX(desc));
+	return 0;
+}
+
+static int crypto_shake_update(struct shash_desc *desc, const u8 *data,
+			      unsigned int len)
+{
+	shake_update(SHAKE_CTX(desc), data, len);
+	return 0;
+}
+
+static int crypto_shake_final(struct shash_desc *desc, u8 *out)
+{
+	const struct shash_alg *alg = crypto_shash_alg(desc->tfm);
+
+	shake_squeeze(SHAKE_CTX(desc), out, alg->digestsize);
+	return 0;
+}
+
+static int crypto_shake256_digest(struct shash_desc *desc,
+				  const u8 *data, unsigned int len, u8 *out)
+{
+	const struct shash_alg *alg = crypto_shash_alg(desc->tfm);
+
+	shake256(data, len, out, alg->digestsize);
+	return 0;
+}
+
 static struct shash_alg algs[] = { {
 	.digestsize		= SHA3_224_DIGEST_SIZE,
 	.init			= crypto_sha3_224_init,
@@ -139,6 +170,17 @@ static struct shash_alg algs[] = { {
 	.base.cra_driver_name	= "sha3-512-lib",
 	.base.cra_blocksize	= SHA3_512_BLOCK_SIZE,
 	.base.cra_module	= THIS_MODULE,
+}, {
+	.digestsize		= SHAKE256_DEFAULT_SIZE,
+	.init			= crypto_shake256_init,
+	.update			= crypto_shake_update,
+	.final			= crypto_shake_final,
+	.digest			= crypto_shake256_digest,
+	.descsize		= sizeof(struct shake_ctx),
+	.base.cra_name		= "shake256",
+	.base.cra_driver_name	= "shake256-lib",
+	.base.cra_blocksize	= SHAKE256_BLOCK_SIZE,
+	.base.cra_module	= THIS_MODULE,
 } };
 
 static int __init crypto_sha3_mod_init(void)
author maintainer
#2 lore raw

PATCH v8 2/9 crypto: Add ML-DSA/Dilithium verify support

In reply to David Howells

Port the signature verification code from Stephan Mueller's Leancrypto implementation of ML-DSA/Dilithium to the kernel. The keypair generation and signature generation is not included and nor is composite signature support to reduce code footprint size.

Apologies to Stephan, but I've stripped out a bunch of macros to do return checking and suchlike and removed a bunch of "lc_" prefixes from the code.

Composite signature support is mostly removed and none of the arch-specific code from Leancrypto has been included for the moment, so this is pure C.

The interface to this code is through the crypto_sig API as the PKCS#7 code wants to use that rather than calling it directly. As such, I've placed it in crypto/ rather than lib/crypto/. Only the verification hooks are implemented; the signing hooks return an error.

cc Stephan Mueller <[email protected]>
cc Eric Biggers <[email protected]>
cc Jason A. Donenfeld <[email protected]>
cc Ard Biesheuvel <[email protected]>
cc Herbert Xu <[email protected]>
---
 lib/crypto/Kconfig                            |   1 +
 lib/crypto/Makefile                           |   2 +
 lib/crypto/mldsa/Kconfig                      |  28 +
 lib/crypto/mldsa/Makefile                     |  14 +
 lib/crypto/mldsa/crypto_mldsa.c               | 334 +++++++++++
 lib/crypto/mldsa/dilithium.h                  | 545 ++++++++++++++++++
 lib/crypto/mldsa/dilithium_44.c               |  33 ++
 lib/crypto/mldsa/dilithium_44.h               | 282 +++++++++
 lib/crypto/mldsa/dilithium_65.c               |  33 ++
 lib/crypto/mldsa/dilithium_65.h               | 282 +++++++++
 lib/crypto/mldsa/dilithium_87.c               |  33 ++
 lib/crypto/mldsa/dilithium_87.h               | 282 +++++++++
 lib/crypto/mldsa/dilithium_api.c              | 429 ++++++++++++++
 lib/crypto/mldsa/dilithium_debug.h            |  49 ++
 lib/crypto/mldsa/dilithium_ntt.c              |  89 +++
 lib/crypto/mldsa/dilithium_ntt.h              |  35 ++
 lib/crypto/mldsa/dilithium_pack.h             | 119 ++++
 lib/crypto/mldsa/dilithium_poly.c             | 377 ++++++++++++
 lib/crypto/mldsa/dilithium_poly.h             | 181 ++++++
 lib/crypto/mldsa/dilithium_poly_c.h           | 141 +++++
 lib/crypto/mldsa/dilithium_poly_common.h      |  35 ++
 lib/crypto/mldsa/dilithium_polyvec.h          | 343 +++++++++++
 lib/crypto/mldsa/dilithium_polyvec_c.h        |  81 +++
 lib/crypto/mldsa/dilithium_reduce.h           |  85 +++
 lib/crypto/mldsa/dilithium_rounding.c         | 128 ++++
 lib/crypto/mldsa/dilithium_service_helpers.h  |  99 ++++
 lib/crypto/mldsa/dilithium_signature_c.c      | 102 ++++
 lib/crypto/mldsa/dilithium_signature_c.h      |  37 ++
 lib/crypto/mldsa/dilithium_signature_helper.c |  79 +++
 lib/crypto/mldsa/dilithium_signature_impl.h   | 370 ++++++++++++
 lib/crypto/mldsa/dilithium_type.h             | 102 ++++
 lib/crypto/mldsa/dilithium_zetas.c            |  67 +++
 .../mldsa/signature_domain_separation.c       | 203 +++++++
 .../mldsa/signature_domain_separation.h       |  30 +
 34 files changed, 5050 insertions(+)
 create mode 100644 lib/crypto/mldsa/Kconfig
 create mode 100644 lib/crypto/mldsa/Makefile
 create mode 100644 lib/crypto/mldsa/crypto_mldsa.c
 create mode 100644 lib/crypto/mldsa/dilithium.h
 create mode 100644 lib/crypto/mldsa/dilithium_44.c
 create mode 100644 lib/crypto/mldsa/dilithium_44.h
 create mode 100644 lib/crypto/mldsa/dilithium_65.c
 create mode 100644 lib/crypto/mldsa/dilithium_65.h
 create mode 100644 lib/crypto/mldsa/dilithium_87.c
 create mode 100644 lib/crypto/mldsa/dilithium_87.h
 create mode 100644 lib/crypto/mldsa/dilithium_api.c
 create mode 100644 lib/crypto/mldsa/dilithium_debug.h
 create mode 100644 lib/crypto/mldsa/dilithium_ntt.c
 create mode 100644 lib/crypto/mldsa/dilithium_ntt.h
 create mode 100644 lib/crypto/mldsa/dilithium_pack.h
 create mode 100644 lib/crypto/mldsa/dilithium_poly.c
 create mode 100644 lib/crypto/mldsa/dilithium_poly.h
 create mode 100644 lib/crypto/mldsa/dilithium_poly_c.h
 create mode 100644 lib/crypto/mldsa/dilithium_poly_common.h
 create mode 100644 lib/crypto/mldsa/dilithium_polyvec.h
 create mode 100644 lib/crypto/mldsa/dilithium_polyvec_c.h
 create mode 100644 lib/crypto/mldsa/dilithium_reduce.h
 create mode 100644 lib/crypto/mldsa/dilithium_rounding.c
 create mode 100644 lib/crypto/mldsa/dilithium_service_helpers.h
 create mode 100644 lib/crypto/mldsa/dilithium_signature_c.c
 create mode 100644 lib/crypto/mldsa/dilithium_signature_c.h
 create mode 100644 lib/crypto/mldsa/dilithium_signature_helper.c
 create mode 100644 lib/crypto/mldsa/dilithium_signature_impl.h
 create mode 100644 lib/crypto/mldsa/dilithium_type.h
 create mode 100644 lib/crypto/mldsa/dilithium_zetas.c
 create mode 100644 lib/crypto/mldsa/signature_domain_separation.c
 create mode 100644 lib/crypto/mldsa/signature_domain_separation.h
lib/crypto/Kconfig +1 -0
index 9d04b3771ce2..8189ff8997be 100644
@@ -223,6 +223,7 @@ config CRYPTO_LIB_SHA3_ARCH
 config CRYPTO_LIB_SM3
 	tristate
 
+source "lib/crypto/mldsa/Kconfig"
 source "lib/crypto/tests/Kconfig"
 
 endmenu
lib/crypto/Makefile +2 -0
index 6580991f8e12..37918c7fd2f6 100644
@@ -305,6 +305,8 @@ obj-$(CONFIG_CRYPTO_SELFTESTS_FULL)		+= simd.o
 obj-$(CONFIG_CRYPTO_LIB_SM3)			+= libsm3.o
 libsm3-y					:= sm3.o
 
+obj-$(CONFIG_CRYPTO_LIB_MLDSA) += mldsa/
+
 # clean-files must be defined unconditionally
 clean-files += arm/sha256-core.S arm/sha512-core.S
 clean-files += arm64/sha256-core.S arm64/sha512-core.S
lib/crypto/mldsa/Kconfig new file +28 -0
new file mode 100644
index 000000000000..04d095da0e22
@@ -0,0 +1,28 @@
+# SPDX-License-Identifier: GPL-2.0
+menuconfig CRYPTO_LIB_MLDSA
+	tristate "ML-DSA algorithm"
+	select CRYPTO_LIB_SHA3
+	help
+	  Enable this to provide the ML-DSA (Dilithium) post-quantum crypto
+	  (PQC) algorithm.  It acts as an asymmetric cipher, but it wants to do
+	  the digestion step that usually precedes such itself, and so has
+	  digest functions in its API.  In theory, an external digestor can be
+	  used, but that is not fully supported here.
+
+	  Note that composite signature support is not currently implemented.
+
+	  Three different strengths of key are supported and each has its own
+	  separate API as most of the code gets duplicated for each strength.
+
+if CRYPTO_LIB_MLDSA
+
+config CRYPTO_LIB_MLDSA_44
+	bool "Enable ML-DSA-44 support"
+
+config CRYPTO_LIB_MLDSA_65
+	bool "Enable ML-DSA-65 support"
+
+config CRYPTO_LIB_MLDSA_87
+	bool "Enable ML-DSA-87 support"
+
+endif
lib/crypto/mldsa/Makefile new file +14 -0
new file mode 100644
index 000000000000..e24bc2b57b8d
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0
+
+mldsa-y += \
+	signature_domain_separation.o \
+	dilithium_api.o \
+	dilithium_zetas.o \
+	dilithium_signature_helper.o
+
+mldsa-$(CONFIG_CRYPTO_LIB_MLDSA_87) += dilithium_87.o
+mldsa-$(CONFIG_CRYPTO_LIB_MLDSA_65) += dilithium_65.o
+mldsa-$(CONFIG_CRYPTO_LIB_MLDSA_44) += dilithium_44.o
+mldsa-$(CONFIG_CRYPTO) += crypto_mldsa.o
+
+obj-$(CONFIG_CRYPTO_LIB_MLDSA) += mldsa.o
lib/crypto/mldsa/crypto_mldsa.c new file +334 -0

334 lines not on this page · raw message

lib/crypto/mldsa/dilithium.h new file +545 -0

545 lines not on this page · raw message

lib/crypto/mldsa/dilithium_44.c new file +33 -0
new file mode 100644
index 000000000000..1aea716016f0
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+#define DILITHIUM_TYPE_44
+#include "dilithium.h"
+
+#define DILITHIUM_TYPE			"44"
+#define DILITHIUM_MODE			2
+#define DILITHIUM_NIST_CATEGORY		DILITHIUM44_NIST_CATEGORY
+#define DILITHIUM_LAMBDA		DILITHIUM44_LAMBDA
+#define DILITHIUM_K			DILITHIUM44_K
+#define DILITHIUM_L			DILITHIUM44_L
+#define DILITHIUM_ETA			DILITHIUM44_ETA
+#define DILITHIUM_TAU			DILITHIUM44_TAU
+#define DILITHIUM_BETA			DILITHIUM44_BETA
+#define DILITHIUM_GAMMA1		DILITHIUM44_GAMMA1
+#define DILITHIUM_GAMMA2		DILITHIUM44_GAMMA2
+#define DILITHIUM_OMEGA			DILITHIUM44_OMEGA
+
+#define DILITHIUM_CTILDE_BYTES		DILITHIUM44_CTILDE_BYTES
+#define DILITHIUM_POLYT1_PACKEDBYTES	DILITHIUM44_POLYT1_PACKEDBYTES
+#define DILITHIUM_POLYT0_PACKEDBYTES	DILITHIUM44_POLYT0_PACKEDBYTES
+#define DILITHIUM_POLYVECH_PACKEDBYTES	DILITHIUM44_POLYVECH_PACKEDBYTES
+#define DILITHIUM_POLYZ_PACKEDBYTES	DILITHIUM44_POLYZ_PACKEDBYTES
+#define DILITHIUM_POLYW1_PACKEDBYTES	DILITHIUM44_POLYW1_PACKEDBYTES
+#define DILITHIUM_POLYETA_PACKEDBYTES	DILITHIUM44_POLYETA_PACKEDBYTES
+
+#define DILITHIUM_PUBLICKEYBYTES	DILITHIUM44_PUBLICKEYBYTES
+#define DILITHIUM_SECRETKEYBYTES	DILITHIUM44_SECRETKEYBYTES
+#define DILITHIUM_CRYPTO_BYTES		DILITHIUM44_CRYPTO_BYTES
+
+#include "dilithium_ntt.c"
+#include "dilithium_poly.c"
+#include "dilithium_rounding.c"
+#include "dilithium_signature_c.c"
lib/crypto/mldsa/dilithium_44.h new file +282 -0

282 lines not on this page · raw message

lib/crypto/mldsa/dilithium_65.c new file +33 -0
new file mode 100644
index 000000000000..08f3a8e71228
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+#define DILITHIUM_TYPE_65
+#include "dilithium.h"
+
+#define DILITHIUM_TYPE			"65"
+#define DILITHIUM_MODE			3
+#define DILITHIUM_NIST_CATEGORY		DILITHIUM65_NIST_CATEGORY
+#define DILITHIUM_LAMBDA		DILITHIUM65_LAMBDA
+#define DILITHIUM_K			DILITHIUM65_K
+#define DILITHIUM_L			DILITHIUM65_L
+#define DILITHIUM_ETA			DILITHIUM65_ETA
+#define DILITHIUM_TAU			DILITHIUM65_TAU
+#define DILITHIUM_BETA			DILITHIUM65_BETA
+#define DILITHIUM_GAMMA1		DILITHIUM65_GAMMA1
+#define DILITHIUM_GAMMA2		DILITHIUM65_GAMMA2
+#define DILITHIUM_OMEGA			DILITHIUM65_OMEGA
+
+#define DILITHIUM_CTILDE_BYTES		DILITHIUM65_CTILDE_BYTES
+#define DILITHIUM_POLYT1_PACKEDBYTES	DILITHIUM65_POLYT1_PACKEDBYTES
+#define DILITHIUM_POLYT0_PACKEDBYTES	DILITHIUM65_POLYT0_PACKEDBYTES
+#define DILITHIUM_POLYVECH_PACKEDBYTES	DILITHIUM65_POLYVECH_PACKEDBYTES
+#define DILITHIUM_POLYZ_PACKEDBYTES	DILITHIUM65_POLYZ_PACKEDBYTES
+#define DILITHIUM_POLYW1_PACKEDBYTES	DILITHIUM65_POLYW1_PACKEDBYTES
+#define DILITHIUM_POLYETA_PACKEDBYTES	DILITHIUM65_POLYETA_PACKEDBYTES
+
+#define DILITHIUM_PUBLICKEYBYTES	DILITHIUM65_PUBLICKEYBYTES
+#define DILITHIUM_SECRETKEYBYTES	DILITHIUM65_SECRETKEYBYTES
+#define DILITHIUM_CRYPTO_BYTES		DILITHIUM65_CRYPTO_BYTES
+
+#include "dilithium_ntt.c"
+#include "dilithium_poly.c"
+#include "dilithium_rounding.c"
+#include "dilithium_signature_c.c"
lib/crypto/mldsa/dilithium_65.h new file +282 -0

282 lines not on this page · raw message

lib/crypto/mldsa/dilithium_87.c new file +33 -0
new file mode 100644
index 000000000000..fcc3e0229ed9
@@ -0,0 +1,33 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+#define DILITHIUM_TYPE_87
+#include "dilithium.h"
+
+#define DILITHIUM_TYPE			"87"
+#define DILITHIUM_MODE			5
+#define DILITHIUM_NIST_CATEGORY		DILITHIUM87_NIST_CATEGORY
+#define DILITHIUM_LAMBDA		DILITHIUM87_LAMBDA
+#define DILITHIUM_K			DILITHIUM87_K
+#define DILITHIUM_L			DILITHIUM87_L
+#define DILITHIUM_ETA			DILITHIUM87_ETA
+#define DILITHIUM_TAU			DILITHIUM87_TAU
+#define DILITHIUM_BETA			DILITHIUM87_BETA
+#define DILITHIUM_GAMMA1		DILITHIUM87_GAMMA1
+#define DILITHIUM_GAMMA2		DILITHIUM87_GAMMA2
+#define DILITHIUM_OMEGA			DILITHIUM87_OMEGA
+
+#define DILITHIUM_CTILDE_BYTES		DILITHIUM87_CTILDE_BYTES
+#define DILITHIUM_POLYT1_PACKEDBYTES	DILITHIUM87_POLYT1_PACKEDBYTES
+#define DILITHIUM_POLYT0_PACKEDBYTES	DILITHIUM87_POLYT0_PACKEDBYTES
+#define DILITHIUM_POLYVECH_PACKEDBYTES	DILITHIUM87_POLYVECH_PACKEDBYTES
+#define DILITHIUM_POLYZ_PACKEDBYTES	DILITHIUM87_POLYZ_PACKEDBYTES
+#define DILITHIUM_POLYW1_PACKEDBYTES	DILITHIUM87_POLYW1_PACKEDBYTES
+#define DILITHIUM_POLYETA_PACKEDBYTES	DILITHIUM87_POLYETA_PACKEDBYTES
+
+#define DILITHIUM_PUBLICKEYBYTES	DILITHIUM87_PUBLICKEYBYTES
+#define DILITHIUM_SECRETKEYBYTES	DILITHIUM87_SECRETKEYBYTES
+#define DILITHIUM_CRYPTO_BYTES		DILITHIUM87_CRYPTO_BYTES
+
+#include "dilithium_ntt.c"
+#include "dilithium_poly.c"
+#include "dilithium_rounding.c"
+#include "dilithium_signature_c.c"
lib/crypto/mldsa/dilithium_87.h new file +282 -0

282 lines not on this page · raw message

lib/crypto/mldsa/dilithium_api.c new file +429 -0

429 lines not on this page · raw message

lib/crypto/mldsa/dilithium_debug.h new file +49 -0

49 lines not on this page · raw message

lib/crypto/mldsa/dilithium_ntt.c new file +89 -0

89 lines not on this page · raw message

lib/crypto/mldsa/dilithium_ntt.h new file +35 -0
new file mode 100644
index 000000000000..5dfea3a936cd
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
+/*
+ * Copyright (C) 2022 - 2025, Stephan Mueller <[email protected]>
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
+ * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
+ * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+/*
+ * This code is derived in parts from the code distribution provided with
+ * https://github.com/pq-crystals/dilithium
+ *
+ * That code is released under Public Domain
+ * (https://creativecommons.org/share-your-work/public-domain/cc0/);
+ * or Apache 2.0 License (https://www.apache.org/licenses/LICENSE-2.0.html).
+ */
+
+#ifndef DILITHIUM_NTT_H
+#define DILITHIUM_NTT_H
+
+#include "dilithium_type.h"
+
+void ntt(int32_t a[DILITHIUM_N]);
+void invntt_tomont(int32_t a[DILITHIUM_N]);
+
+#endif /* DILITHIUM_NTT_H */
lib/crypto/mldsa/dilithium_pack.h new file +119 -0

119 lines not on this page · raw message

lib/crypto/mldsa/dilithium_poly.c new file +377 -0

377 lines not on this page · raw message

lib/crypto/mldsa/dilithium_poly.h new file +181 -0

21 lines not on this page · raw message

lib/crypto/mldsa/dilithium_poly_c.h new file +141 -0

141 lines not on this page · raw message

lib/crypto/mldsa/dilithium_poly_common.h new file +35 -0
new file mode 100644
index 000000000000..7c7cef28b545

Lines of this file are not shown. View the raw message.

lib/crypto/mldsa/dilithium_polyvec.h new file +343 -0

343 lines not on this page · raw message

lib/crypto/mldsa/dilithium_polyvec_c.h new file +81 -0

81 lines not on this page · raw message

lib/crypto/mldsa/dilithium_reduce.h new file +85 -0

85 lines not on this page · raw message

lib/crypto/mldsa/dilithium_rounding.c new file +128 -0

128 lines not on this page · raw message

lib/crypto/mldsa/dilithium_service_helpers.h new file +99 -0

99 lines not on this page · raw message

lib/crypto/mldsa/dilithium_signature_c.c new file +102 -0

102 lines not on this page · raw message

lib/crypto/mldsa/dilithium_signature_c.h new file +37 -0
new file mode 100644
index 000000000000..d1e694e4c8d3

Lines of this file are not shown. View the raw message.

lib/crypto/mldsa/dilithium_signature_helper.c new file +79 -0

79 lines not on this page · raw message

lib/crypto/mldsa/dilithium_signature_impl.h new file +370 -0

370 lines not on this page · raw message

lib/crypto/mldsa/dilithium_type.h new file +102 -0

102 lines not on this page · raw message

lib/crypto/mldsa/dilithium_zetas.c new file +67 -0

67 lines not on this page · raw message

lib/crypto/mldsa/signature_domain_separation.c new file +203 -0

203 lines not on this page · raw message

lib/crypto/mldsa/signature_domain_separation.h new file +30 -0
new file mode 100644
index 000000000000..070c8b4ec371

Lines of this file are not shown. View the 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.

author maintainer
#3 lore raw

PATCH v8 3/9 mldsa: Add a simpler API

In reply to David Howells

Add a simpler ML-DSA API to hide all the details of packing/unpacking keys and signatures.

---
 Documentation/crypto/index.rst  |   1 +
 Documentation/crypto/mldsa.rst  | 111 +++++++++++++++++++
 include/crypto/mldsa.h          |  34 ++++++
 lib/crypto/mldsa/Makefile       |   3 +-
 lib/crypto/mldsa/crypto_mldsa.c |   4 +-
 lib/crypto/mldsa/mldsa_api.c    | 186 ++++++++++++++++++++++++++++++++
 6 files changed, 335 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/crypto/mldsa.rst
 create mode 100644 include/crypto/mldsa.h
 create mode 100644 lib/crypto/mldsa/mldsa_api.c
Documentation/crypto/index.rst +1 -0
index 4ee667c446f9..4498fc92bfc5 100644
@@ -28,3 +28,4 @@ for cryptographic use cases, as well as programming examples.
    device_drivers/index
    krb5
    sha3
+   mldsa
Documentation/crypto/mldsa.rst new file +111 -0

111 lines not on this page · raw message

include/crypto/mldsa.h new file +34 -0
new file mode 100644
index 000000000000..e7aac97ac6fd
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
+/* Simple API for ML-DSA.
+ *
+ * Copyright (C) 2025 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ *
+ * See also Documentation/crypto/mldsa.rst
+ */
+
+#ifndef _CRYPTO_MLDSA_H
+#define _CRYPTO_MLDSA_H
+
+struct mldsa_ctx;
+
+enum mldsa_type {
+	MLDSA_UNKNOWN,	/* Unknown key type */
+	MLDSA_87,	/* ML-DSA 87 */
+	MLDSA_65,	/* ML-DSA 65 */
+	MLDSA_44,	/* ML-DSA 44 */
+};
+
+int mldsa_verify(enum mldsa_type type,
+		 const void *pk, size_t pk_len,
+		 const uint8_t *data, size_t data_len,
+		 const void *sig, size_t sig_len);
+struct mldsa_ctx *mldsa_ctx_alloc(enum mldsa_type type);
+int mldsa_verify_update(struct mldsa_ctx *ctx, const void *data, size_t data_len);
+int mldsa_verify_final(struct mldsa_ctx *ctx,
+		       const void *pk, size_t pk_len,
+		       const void *sig, size_t sig_len);
+void mldsa_ctx_zeroize(struct mldsa_ctx *ctx);
+void mldsa_ctx_free(struct mldsa_ctx *ctx);
+
+#endif /* _CRYPTO_MLDSA_H */
lib/crypto/mldsa/Makefile +2 -1
index e24bc2b57b8d..75e260615d4a 100644
@@ -4,7 +4,8 @@ mldsa-y += \
 	signature_domain_separation.o \
 	dilithium_api.o \
 	dilithium_zetas.o \
-	dilithium_signature_helper.o
+	dilithium_signature_helper.o \
+	mldsa_api.o
 
 mldsa-$(CONFIG_CRYPTO_LIB_MLDSA_87) += dilithium_87.o
 mldsa-$(CONFIG_CRYPTO_LIB_MLDSA_65) += dilithium_65.o
lib/crypto/mldsa/crypto_mldsa.c +1 -3
index 26cafeae6f0f..91350a828d94 100644
@@ -28,9 +28,7 @@ enum dilithium_kernel_key_type {
 };
 
 struct dilithium_kernel_ctx {
-	union {
-		struct dilithium_pk pk;
-	};
+	struct dilithium_pk pk;
 	enum dilithium_kernel_key_type key_type;
 };
 
lib/crypto/mldsa/mldsa_api.c new file +186 -0

186 lines not on this page · raw message

author maintainer
#4 lore raw

PATCH v8 4/9 crypto: Add ML-DSA-44 pure rejection test vectors as a kunit test

In reply to David Howells

Add the "Pure rejection tests" from Stephan Mueller's Leancrypto implementation of ML-DSA/Dilithium 44 to the kernel as a kunit test.

cc Stephan Mueller <[email protected]>
cc Eric Biggers <[email protected]>
cc Jason A. Donenfeld <[email protected]>
cc Ard Biesheuvel <[email protected]>
cc Herbert Xu <[email protected]>
---
 lib/crypto/tests/Kconfig                      |  10 +
 lib/crypto/tests/Makefile                     |   1 +
 lib/crypto/tests/mldsa_kunit.c                |  73 +++
 .../tests/mldsa_pure_rejection_vectors_44.h   | 489 ++++++++++++++++++
 4 files changed, 573 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
index 61d435c450bb..17b66dd6037d 100644
@@ -101,6 +101,16 @@ config CRYPTO_LIB_SHA3_KUNIT_TEST
 	  including SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128 and
 	  SHAKE256.
 
+config CRYPTO_LIB_MLDSA_KUNIT_TEST
+	tristate "KUnit tests for ML-DSA"
+	depends on KUNIT
+	select CRYPTO_LIB_MLDSA
+	select CRYPTO
+	default KUNIT_ALL_TESTS
+	help
+	  This options enables the KUnit tests for ML-DSA (Dilithium)
+	  asymmetric key algorithm.
+
 config CRYPTO_LIB_BENCHMARK_VISIBLE
 	bool
 
lib/crypto/tests/Makefile +1 -0
index 5109a0651925..2d9f6bc6ba88 100644
@@ -10,3 +10,4 @@ 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
 obj-$(CONFIG_CRYPTO_LIB_SHA3_KUNIT_TEST) += sha3_kunit.o
+obj-$(CONFIG_CRYPTO_LIB_MLDSA_KUNIT_TEST) += mldsa_kunit.o
lib/crypto/tests/mldsa_kunit.c new file +73 -0

73 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

author maintainer
#5 lore raw

PATCH v8 5/9 crypto: Add ML-DSA-65 pure rejection test vectors as a kunit test

In reply to David Howells

Add the "Pure rejection tests" from Stephan Mueller's Leancrypto implementation of ML-DSA/Dilithium 65 to the kernel as a kunit test.

cc Stephan Mueller <[email protected]>
cc Eric Biggers <[email protected]>
cc Jason A. Donenfeld <[email protected]>
cc Ard Biesheuvel <[email protected]>
cc Herbert Xu <[email protected]>
---
 lib/crypto/tests/mldsa_kunit.c                |   15 +
 .../tests/mldsa_pure_rejection_vectors_65.h   | 4741 +++++++++++++++++
 2 files changed, 4756 insertions(+)
 create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_65.h
lib/crypto/tests/mldsa_kunit.c +15 -0
index 33ef92d8adbc..13b2e4f7585e 100644
@@ -35,6 +35,9 @@ struct dilithium_testvector {
 static const struct dilithium_testvector dilithium44_testvectors[] = {
 #include "mldsa_pure_rejection_vectors_44.h"
 };
+static const struct dilithium_testvector dilithium65_testvectors[] = {
+#include "mldsa_pure_rejection_vectors_65.h"
+};
 
 static void dilithium_sigver_test_one(struct kunit *test, int index,
 				      const struct dilithium_testvector *tc)
@@ -60,8 +63,20 @@ static void dilithium44_kunit_test(struct kunit *test)
 	KUNIT_SUCCEED(test);
 }
 
+static void dilithium65_kunit_test(struct kunit *test)
+{
+	const struct dilithium_testvector *tc = dilithium65_testvectors;
+	int count = ARRAY_SIZE(dilithium65_testvectors);
+
+	for (int index = 0; index < count; index++)
+		dilithium_sigver_test_one(test, index, &tc[index]);
+
+	KUNIT_SUCCEED(test);
+}
+
 static struct kunit_case __refdata mldsa_kunit_cases[] = {
 	KUNIT_CASE(dilithium44_kunit_test),
+	KUNIT_CASE(dilithium65_kunit_test),
 	{}
 };
 
lib/crypto/tests/mldsa_pure_rejection_vectors_65.h new file +4741 -0

2,971 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.

author maintainer
#6 lore raw

PATCH v8 6/9 crypto: Add ML-DSA-87 pure rejection test vectors as a kunit test

In reply to David Howells

Add the "Pure rejection tests" from Stephan Mueller's Leancrypto implementation of ML-DSA/Dilithium 87 to the kernel as a kunit test.

cc Stephan Mueller <[email protected]>
cc Eric Biggers <[email protected]>
cc Jason A. Donenfeld <[email protected]>
cc Ard Biesheuvel <[email protected]>
cc Herbert Xu <[email protected]>
---
 lib/crypto/tests/mldsa_kunit.c                |   15 +
 .../tests/mldsa_pure_rejection_vectors_87.h   | 6456 +++++++++++++++++
 2 files changed, 6471 insertions(+)
 create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_87.h
lib/crypto/tests/mldsa_kunit.c +15 -0
index 13b2e4f7585e..071b96b70bb2 100644
@@ -38,6 +38,9 @@ static const struct dilithium_testvector dilithium44_testvectors[] = {
 static const struct dilithium_testvector dilithium65_testvectors[] = {
 #include "mldsa_pure_rejection_vectors_65.h"
 };
+static const struct dilithium_testvector dilithium87_testvectors[] = {
+#include "mldsa_pure_rejection_vectors_87.h"
+};
 
 static void dilithium_sigver_test_one(struct kunit *test, int index,
 				      const struct dilithium_testvector *tc)
@@ -74,9 +77,21 @@ static void dilithium65_kunit_test(struct kunit *test)
 	KUNIT_SUCCEED(test);
 }
 
+static void dilithium87_kunit_test(struct kunit *test)
+{
+	const struct dilithium_testvector *tc = dilithium87_testvectors;
+	int count = ARRAY_SIZE(dilithium87_testvectors);
+
+	for (int index = 0; index < count; index++)
+		dilithium_sigver_test_one(test, index, &tc[index]);
+
+	KUNIT_SUCCEED(test);
+}
+
 static struct kunit_case __refdata mldsa_kunit_cases[] = {
 	KUNIT_CASE(dilithium44_kunit_test),
 	KUNIT_CASE(dilithium65_kunit_test),
+	KUNIT_CASE(dilithium87_kunit_test),
 	{}
 };
 
lib/crypto/tests/mldsa_pure_rejection_vectors_87.h new file +6456 -0

2,970 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.

author maintainer
#7 lore raw

PATCH v8 7/9 pkcs7: Allow the signing algo to calculate the digest itself

In reply to David Howells

The 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.

cc Lukas Wunner <[email protected]>
cc Ignat Korchagin <[email protected]>
cc Stephan Mueller <[email protected]>
cc Eric Biggers <[email protected]>
cc Herbert Xu <[email protected]>
---
 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
index 423d13c47545..3cdbab3b9f50 100644
@@ -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
index 81098e00c08f..e4ec8003a3a4 100644
@@ -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;
author maintainer
#8 lore raw

PATCH v8 8/9 pkcs7, x509: Add ML-DSA support

In reply to David Howells

Add support for ML-DSA keys and signatures to the PKCS#7 and X.509 implementations.

cc Lukas Wunner <[email protected]>
cc Ignat Korchagin <[email protected]>
cc Stephan Mueller <[email protected]>
cc Eric Biggers <[email protected]>
cc Herbert Xu <[email protected]>
---
 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
index 3cdbab3b9f50..d7c88b90a4fd 100644
@@ -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 = "ml-dsa44";
+		ctx->sinfo->sig->encoding = "raw";
+		ctx->sinfo->sig->algo_does_hash = true;
+		break;
+	case OID_id_ml_dsa_65:
+		ctx->sinfo->sig->pkey_algo = "ml-dsa65";
+		ctx->sinfo->sig->encoding = "raw";
+		ctx->sinfo->sig->algo_does_hash = true;
+		break;
+	case OID_id_ml_dsa_87:
+		ctx->sinfo->sig->pkey_algo = "ml-dsa87";
+		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
index e5b177c8e842..96e084668d1b 100644
@@ -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, "ml-dsa44") == 0 ||
+		   strcmp(pkey->pkey_algo, "ml-dsa65") == 0 ||
+		   strcmp(pkey->pkey_algo, "ml-dsa87") == 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
index 8df3fa60a44f..6a2a77a647a5 100644
@@ -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 = "ml-dsa44";
+		goto ml_dsa;
+	case OID_id_ml_dsa_65:
+		ctx->cert->sig->pkey_algo = "ml-dsa65";
+		goto ml_dsa;
+	case OID_id_ml_dsa_87:
+		ctx->cert->sig->pkey_algo = "ml-dsa87";
+		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 = "ml-dsa44";
+		break;
+	case OID_id_ml_dsa_65:
+		ctx->cert->pub->pkey_algo = "ml-dsa65";
+		break;
+	case OID_id_ml_dsa_87:
+		ctx->cert->pub->pkey_algo = "ml-dsa87";
+		break;
 	default:
 		return -ENOPKG;
 	}
include/linux/oid_registry.h +5 -0
index 6de479ebbe5d..30821a6a4f72 100644
@@ -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
 };
 
author maintainer
#9 lore raw

PATCH v8 9/9 modsign: Enable ML-DSA module signing

In reply to David Howells

Allow 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. ("ml-dsa87(sha512)").

cc Lukas Wunner <[email protected]>
cc Ignat Korchagin <[email protected]>
cc Stephan Mueller <[email protected]>
cc Eric Biggers <[email protected]>
cc Herbert Xu <[email protected]>
---
 Documentation/admin-guide/module-signing.rst | 15 +++++------
 certs/Kconfig                                | 24 ++++++++++++++++++
 certs/Makefile                               |  3 +++
 crypto/asymmetric_keys/pkcs7_verify.c        |  4 ---
 kernel/module/Kconfig                        |  5 ++++
 scripts/sign-file.c                          | 26 ++++++++++++++------
 6 files changed, 58 insertions(+), 19 deletions(-)
Documentation/admin-guide/module-signing.rst +8 -7
index a8667a777490..6daff80c277b 100644
@@ -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_ML_DSA``) to
+generate an RSA 4k, a NIST P-384 keypair or an ML-DSA keypair.
 
 It is strongly recommended that you provide your own x509.genkey file.
 
certs/Kconfig +24 -0
index 78307dc25559..f647b944f5da 100644
@@ -39,6 +39,30 @@ 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_ML_DSA_44
+	bool "ML-DSA (Dilithium) 44"
+	select CRYPTO_ML_DSA
+	select LIB_SHA3
+	help
+	  Use an ML-DSA (Dilithium) 44 key (NIST FIPS 204) for module signing
+	  with a SHAKE256 'hash' of the message.
+
+config MODULE_SIG_KEY_TYPE_ML_DSA_65
+	bool "ML-DSA (Dilithium) 65"
+	select CRYPTO_ML_DSA
+	select LIB_SHA3
+	help
+	  Use an ML-DSA (Dilithium) 65 key (NIST FIPS 204) for module signing
+	  with a SHAKE256 'hash' of the message.
+
+config MODULE_SIG_KEY_TYPE_ML_DSA_87
+	bool "ML-DSA (Dilithium) 87"
+	select CRYPTO_ML_DSA
+	select LIB_SHA3
+	help
+	  Use an ML-DSA (Dilithium) 87 key (NIST FIPS 204) for module signing
+	  with a SHAKE256 'hash' of the message.
+
 endchoice
 
 config SYSTEM_TRUSTED_KEYRING
certs/Makefile +3 -0
index f6fa4d8d75e0..231379c91b86 100644
@@ -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_ML_DSA_44) := -newkey ml-dsa-44
+keytype-$(CONFIG_MODULE_SIG_KEY_TYPE_ML_DSA_65) := -newkey ml-dsa-65
+keytype-$(CONFIG_MODULE_SIG_KEY_TYPE_ML_DSA_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
index 0f9f515b784d..f7ea1d41771d 100644
@@ -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) {
kernel/module/Kconfig +5 -0
index 2a1beebf1d37..4b5d1601d537 100644
@@ -327,6 +327,10 @@ config MODULE_SIG_SHA3_512
 	bool "SHA3-512"
 	select CRYPTO_SHA3
 
+config MODULE_SIG_SHAKE256
+	bool "SHAKE256"
+	select CRYPTO_SHA3
+
 endchoice
 
 config MODULE_SIG_HASH
@@ -339,6 +343,7 @@ config MODULE_SIG_HASH
 	default "sha3-256" if MODULE_SIG_SHA3_256
 	default "sha3-384" if MODULE_SIG_SHA3_384
 	default "sha3-512" if MODULE_SIG_SHA3_512
+	default "shake256" if MODULE_SIG_SHAKE256
 
 config MODULE_COMPRESS
 	bool "Module compression"
scripts/sign-file.c +18 -8
index 7070245edfc1..b726581075f9 100644
@@ -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