lib/crypto: Add ML-DSA signing
Subsystems (8)
David Howells
maintainerv9 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 #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 (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 | 143 + certs/Kconfig | 27 + 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 | 50 + 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 | 29 + lib/crypto/mldsa/Makefile | 20 + lib/crypto/mldsa/crypto_mldsa_44.c | 166 + lib/crypto/mldsa/crypto_mldsa_65.c | 166 + lib/crypto/mldsa/crypto_mldsa_87.c | 166 + lib/crypto/mldsa/dilithium.h | 304 + lib/crypto/mldsa/dilithium_44.c | 34 + lib/crypto/mldsa/dilithium_44.h | 291 + lib/crypto/mldsa/dilithium_65.c | 34 + lib/crypto/mldsa/dilithium_65.h | 291 + lib/crypto/mldsa/dilithium_87.c | 34 + lib/crypto/mldsa/dilithium_87.h | 291 + lib/crypto/mldsa/dilithium_common.c | 117 + 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 | 279 + lib/crypto/mldsa/dilithium_signature_c.h | 37 + lib/crypto/mldsa/dilithium_signature_impl.h | 370 + lib/crypto/mldsa/dilithium_type.h | 121 + lib/crypto/mldsa/dilithium_zetas.c | 68 + lib/crypto/mldsa/mldsa_api.c | 168 + .../mldsa/signature_domain_separation.c | 204 + .../mldsa/signature_domain_separation.h | 30 + lib/crypto/tests/Kconfig | 10 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/mldsa_kunit.c | 105 + .../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 +- 57 files changed, 17171 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_44.c create mode 100644 lib/crypto/mldsa/crypto_mldsa_65.c create mode 100644 lib/crypto/mldsa/crypto_mldsa_87.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_common.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_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
David Howells
maintainerPATCH v9 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.
--- crypto/sha3.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
crypto/sha3.c +42 -0
@@ -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 )
David Howells
maintainerPATCH v9 2/9 crypto: Add ML-DSA/Dilithium verify support
In reply to David HowellsPort 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.
The output is built into four parts that can be built as separate modules:
one common module and one module for each strength option. This allows one of the strengths to be built into the base kernel and the other two to be loaded on demand if such is desired.
--- lib/crypto/Kconfig | 1 + lib/crypto/Makefile | 2 + lib/crypto/mldsa/Kconfig | 29 ++ lib/crypto/mldsa/Makefile | 20 + lib/crypto/mldsa/crypto_mldsa_44.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_65.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_87.c | 166 ++++++++ lib/crypto/mldsa/dilithium.h | 304 ++++++++++++++ lib/crypto/mldsa/dilithium_44.c | 33 ++ lib/crypto/mldsa/dilithium_44.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_65.c | 33 ++ lib/crypto/mldsa/dilithium_65.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_87.c | 33 ++ lib/crypto/mldsa/dilithium_87.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_common.c | 117 ++++++ 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 | 279 +++++++++++++ lib/crypto/mldsa/dilithium_signature_c.h | 37 ++ lib/crypto/mldsa/dilithium_signature_impl.h | 370 +++++++++++++++++ lib/crypto/mldsa/dilithium_type.h | 108 +++++ lib/crypto/mldsa/dilithium_zetas.c | 68 ++++ .../mldsa/signature_domain_separation.c | 204 ++++++++++ .../mldsa/signature_domain_separation.h | 30 ++ 35 files changed, 4801 insertions(+) create mode 100644 lib/crypto/mldsa/Kconfig create mode 100644 lib/crypto/mldsa/Makefile create mode 100644 lib/crypto/mldsa/crypto_mldsa_44.c create mode 100644 lib/crypto/mldsa/crypto_mldsa_65.c create mode 100644 lib/crypto/mldsa/crypto_mldsa_87.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_common.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_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
@@ -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
@@ -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 +29 -0
@@ -0,0 +1,29 @@ +# 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 and is built into a separate module as most of the code + gets duplicated for each strength. + +if CRYPTO_LIB_MLDSA + +config CRYPTO_LIB_MLDSA_44 + tristate "Enable ML-DSA-44 support" + +config CRYPTO_LIB_MLDSA_65 + tristate "Enable ML-DSA-65 support" + +config CRYPTO_LIB_MLDSA_87 + tristate "Enable ML-DSA-87 support" + +endif
lib/crypto/mldsa/Makefile new file +20 -0
@@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0 + +mldsa_common-y += \ + dilithium_common.o \ + dilithium_zetas.o \ + signature_domain_separation.o + +mldsa_44-y += dilithium_44.o +mldsa_44-$(CONFIG_CRYPTO) += crypto_mldsa_44.o + +mldsa_65-y += dilithium_65.o +mldsa_65-$(CONFIG_CRYPTO) += crypto_mldsa_65.o + +mldsa_87-y += dilithium_87.o +mldsa_87-$(CONFIG_CRYPTO) += crypto_mldsa_87.o + +obj-$(CONFIG_CRYPTO_LIB_MLDSA) += mldsa_common.o +obj-$(CONFIG_CRYPTO_LIB_MLDSA_44) += mldsa_44.o +obj-$(CONFIG_CRYPTO_LIB_MLDSA_65) += mldsa_65.o +obj-$(CONFIG_CRYPTO_LIB_MLDSA_87) += mldsa_87.o
lib/crypto/mldsa/crypto_mldsa_44.c new file +166 -0
166 lines not on this page · raw message
lib/crypto/mldsa/crypto_mldsa_65.c new file +166 -0
166 lines not on this page · raw message
lib/crypto/mldsa/crypto_mldsa_87.c new file +166 -0
166 lines not on this page · raw message
lib/crypto/mldsa/dilithium.h new file +304 -0
304 lines not on this page · raw message
lib/crypto/mldsa/dilithium_44.c new file +33 -0
@@ -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 +291 -0
291 lines not on this page · raw message
lib/crypto/mldsa/dilithium_65.c new file +33 -0
@@ -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 +291 -0
291 lines not on this page · raw message
lib/crypto/mldsa/dilithium_87.c new file +33 -0
@@ -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 +291 -0
291 lines not on this page · raw message
lib/crypto/mldsa/dilithium_common.c new file +117 -0
117 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
@@ -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
181 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
@@ -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_POLY_COMMON_H +#define DILITHIUM_POLY_COMMON_H + +#include "dilithium_type.h" + +void poly_uniform_gamma1 (poly *a,const uint8_t seed[DILITHIUM_CRHBYTES ], +uint16_t nonce,void *ws_buf); + +#endif /* DILITHIUM_POLY_COMMON_H */
lib/crypto/mldsa/dilithium_polyvec.h new file +343 -0
19 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 +279 -0
279 lines not on this page · raw message
lib/crypto/mldsa/dilithium_signature_c.h new file +37 -0
Lines of this file are not shown. View the 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 +108 -0
108 lines not on this page · raw message
lib/crypto/mldsa/dilithium_zetas.c new file +68 -0
68 lines not on this page · raw message
lib/crypto/mldsa/signature_domain_separation.c new file +204 -0
204 lines not on this page · raw message
lib/crypto/mldsa/signature_domain_separation.h new file +30 -0
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.
David Howells
maintainerPATCH v9 3/9 mldsa: Add a simpler API
In reply to David HowellsAdd 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 | 143 +++++++++++++++++++++++++ include/crypto/mldsa.h | 50 +++++++++ lib/crypto/mldsa/dilithium_44.c | 1 + lib/crypto/mldsa/dilithium_65.c | 1 + lib/crypto/mldsa/dilithium_87.c | 1 + lib/crypto/mldsa/dilithium_type.h | 13 +++ lib/crypto/mldsa/mldsa_api.c | 168 ++++++++++++++++++++++++++++++ 8 files changed, 378 insertions(+) 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
@@ -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 +143 -0
143 lines not on this page · raw message
include/crypto/mldsa.h new file +50 -0
50 lines not on this page · raw message
lib/crypto/mldsa/dilithium_44.c +1 -0
@@ -31,3 +31,4 @@#include "dilithium_poly.c" #include "dilithium_rounding.c" #include "dilithium_signature_c.c" +#include "mldsa_api.c"
lib/crypto/mldsa/dilithium_65.c +1 -0
@@ -31,3 +31,4 @@#include "dilithium_poly.c" #include "dilithium_rounding.c" #include "dilithium_signature_c.c" +#include "mldsa_api.c"
lib/crypto/mldsa/dilithium_87.c +1 -0
@@ -31,3 +31,4 @@#include "dilithium_poly.c" #include "dilithium_rounding.c" #include "dilithium_signature_c.c" +#include "mldsa_api.c"
lib/crypto/mldsa/dilithium_type.h +13 -0
@@ -34,23 +34,29 @@* compilation different sizes would not be possible. */ #ifdef DILITHIUM_TYPE_65 +#define MLDSA_F (name) mldsa_65_##name#define DILITHIUM_F (name) dilithium_65_##name#define dilithium_pk dilithium_65_pk#define dilithium_sig dilithium_65_sig +#define mldsa_ctx mldsa_65_ctx#include "dilithium_65.h" #elif definedDILITHIUM_TYPE_44 +#define MLDSA_F (name) mldsa_44_##name#define DILITHIUM_F (name) dilithium_44_##name#define dilithium_pk dilithium_44_pk#define dilithium_sig dilithium_44_sig +#define mldsa_ctx mldsa_44_ctx#include "dilithium_44.h" #else +#define MLDSA_F (name) mldsa_87_##name#define DILITHIUM_F (name) dilithium_87_##name#define dilithium_pk dilithium_87_pk#define dilithium_sig dilithium_87_sig +#define mldsa_ctx mldsa_87_ctx#include "dilithium_87.h"
@@ -60,6 +66,13 @@* The following defines simply allow duplicate compilation of the * respective functions. */ +#define mldsa_verify MLDSA_F(verify) +#define mldsa_verify_update MLDSA_F(verify_update) +#define mldsa_verify_final MLDSA_F(verify_final) +#define mldsa_ctx_alloc MLDSA_F(ctx_alloc) +#define mldsa_ctx_zeroize MLDSA_F(ctx_zeroize) +#define mldsa_ctx_free MLDSA_F(ctx_free) +#define dilithium_pk_load DILITHIUM_F(pk_load)#define dilithium_sig_load DILITHIUM_F(sig_load)#define dilithium_pk_ptr DILITHIUM_F(pk_ptr)
lib/crypto/mldsa/mldsa_api.c new file +168 -0
168 lines not on this page · raw message
David Howells
maintainerPATCH v9 4/9 crypto: Add ML-DSA-44 pure rejection test vectors as a kunit test
In reply to David HowellsAdd the "Pure rejection tests" from Stephan Mueller's Leancrypto implementation of ML-DSA/Dilithium 44 to the kernel as a kunit test.
--- lib/crypto/tests/Kconfig | 10 + lib/crypto/tests/Makefile | 1 + lib/crypto/tests/mldsa_kunit.c | 65 +++ .../tests/mldsa_pure_rejection_vectors_44.h | 489 ++++++++++++++++++ 4 files changed, 565 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
@@ -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
@@ -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 +65 -0
65 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 v9 5/9 crypto: Add ML-DSA-65 pure rejection test vectors as a kunit test
In reply to David HowellsAdd the "Pure rejection tests" from Stephan Mueller's Leancrypto implementation of ML-DSA/Dilithium 65 to the kernel as a kunit test.
--- lib/crypto/tests/mldsa_kunit.c | 20 + .../tests/mldsa_pure_rejection_vectors_65.h | 4741 +++++++++++++++++ 2 files changed, 4761 insertions(+) create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_65.h
lib/crypto/tests/mldsa_kunit.c +20 -0
@@ -35,6 +35,9 @@ struct dilithium_testvector {static const struct dilithium_testvector dilithium_44_testvectors[] = { #include "mldsa_pure_rejection_vectors_44.h" }; +static const struct dilithium_testvector dilithium_65_testvectors[] = { +#include "mldsa_pure_rejection_vectors_65.h" +};static void dilithium_44_kunit_test (struct kunit *test) {
@@ -52,8 +55,25 @@ static void dilithium_44_kunit_test(struct kunit *test)KUNIT_SUCCEED (test); } +static void dilithium_65_kunit_test (struct kunit *test) +{ +const struct dilithium_testvector *tc = dilithium_65_testvectors; +int count =ARRAY_SIZE (dilithium_65_testvectors); +int ret; + +for (int index =0 ; index < count; index++) { + ret =mldsa_65_verify (tc->pk, tc->pk_len, + tc->msg, tc->msg_len, + tc->sig, tc->sig_len); +KUNIT_ASSERT_EQ_MSG (test, ret,0 ,"Verify failed" ); + } + +KUNIT_SUCCEED (test); +} +static struct kunit_case __refdata mldsa_kunit_cases[] = {KUNIT_CASE (dilithium_44_kunit_test), +KUNIT_CASE (dilithium_65_kunit_test), {} };
lib/crypto/tests/mldsa_pure_rejection_vectors_65.h new file +4741 -0
2,966 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 v9 6/9 crypto: Add ML-DSA-87 pure rejection test vectors as a kunit test
In reply to David HowellsAdd the "Pure rejection tests" from Stephan Mueller's Leancrypto implementation of ML-DSA/Dilithium 87 to the kernel as a kunit test.
--- lib/crypto/tests/mldsa_kunit.c | 20 + .../tests/mldsa_pure_rejection_vectors_87.h | 6456 +++++++++++++++++ 2 files changed, 6476 insertions(+) create mode 100644 lib/crypto/tests/mldsa_pure_rejection_vectors_87.h
lib/crypto/tests/mldsa_kunit.c +20 -0
@@ -38,6 +38,9 @@ static const struct dilithium_testvector dilithium_44_testvectors[] = {static const struct dilithium_testvector dilithium_65_testvectors[] = { #include "mldsa_pure_rejection_vectors_65.h" }; +static const struct dilithium_testvector dilithium_87_testvectors[] = { +#include "mldsa_pure_rejection_vectors_87.h" +};static void dilithium_44_kunit_test (struct kunit *test) {
@@ -71,9 +74,26 @@ static void dilithium_65_kunit_test(struct kunit *test)KUNIT_SUCCEED (test); } +static void dilithium_87_kunit_test (struct kunit *test) +{ +const struct dilithium_testvector *tc = dilithium_87_testvectors; +int count =ARRAY_SIZE (dilithium_87_testvectors); +int ret; + +for (int index =0 ; index < count; index++) { + ret =mldsa_87_verify (tc->pk, tc->pk_len, + tc->msg, tc->msg_len, + tc->sig, tc->sig_len); +KUNIT_ASSERT_EQ_MSG (test, ret,0 ,"Verify failed" ); + } + +KUNIT_SUCCEED (test); +} +static struct kunit_case __refdata mldsa_kunit_cases[] = {KUNIT_CASE (dilithium_44_kunit_test),KUNIT_CASE (dilithium_65_kunit_test), +KUNIT_CASE (dilithium_87_kunit_test), {} };
lib/crypto/tests/mldsa_pure_rejection_vectors_87.h new file +6456 -0
2,965 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 v9 7/9 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 v9 8/9 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 v9 9/9 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. ("ml-dsa87(sha512)").
--- Documentation/admin-guide/module-signing.rst | 15 ++++++----- certs/Kconfig | 27 ++++++++++++++++++++ certs/Makefile | 3 +++ crypto/asymmetric_keys/pkcs7_verify.c | 4 --- kernel/module/Kconfig | 5 ++++ scripts/sign-file.c | 26 +++++++++++++------ 6 files changed, 61 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_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 +27 -0
@@ -39,6 +39,33 @@ 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_LIB_MLDSA + select CRYPTO_LIB_MLDSA_44 + select LIB_SHA3 + 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_ML_DSA_65 + bool "ML-DSA (Dilithium) 65" + select CRYPTO_LIB_MLDSA + select CRYPTO_LIB_MLDSA_65 + select LIB_SHA3 + 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_ML_DSA_87 + bool "ML-DSA (Dilithium) 87" + select CRYPTO_LIB_MLDSA + select CRYPTO_LIB_MLDSA_87 + select LIB_SHA3 + 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_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
@@ -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
@@ -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
@@ -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
David Howells
maintainerPick up keys-pqc branch for linux-next?
In reply to David HowellsHi Stephen,
Can you pick up my keys-pqc branch for linux-next please? It can be found at:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/
keys-pqc
Note that it's based on Eric Bigger's libcrypto/lbcrypto-next branch which I believe you already have in order to get SHA-3/SHAKE support.
Thanks,
David
David Howells
maintainerWhere to add FIPS tests
In reply to David HowellsHi Herbert,
I'm wondering from where I should invoke the FIPS tests for ML-DSA.
Currently, the asymmetric key type has some FIPS selftests for RSA and ECDSA built into it, but I wonder if that's the best way. The problem is that it does the selftest during module init - but that can only test whatever algorithms are built into the base kernel image and initialised at the time late_initcall() happens.
It might be better to put the tests into the algorithm modules themselves - but that then has a potential circular dependency issue. However, that might not matter as the asymmetric key type won't be built as a module and will be built into the kernel (though some of the components such as X.509 and PKCS#7 can be built as modules).
If I don't involve X.509/PKCS#7 in the selftest, then doing it from the ML-DSA modules during module init would be fine.
Do you (or anyone else) have any thoughts?
David
Eric Biggers
maintainerRe: [PATCH v9 1/9] crypto: Add support for shake256 through crypto_shash
In reply to David HowellsQuoted 2 lines · David Howells
Add shake256 support to the SHA-3
crypto_sigmodule so that ML-DSA can use it.
Why? Not only does this patch not expose SHAKE256 support correctly, the ML-DSA code just accesses SHAKE256 via the library API, not crypto_shash. So this seems unnecessary.
- Eric
Eric Biggers
maintainerRe: Where to add FIPS tests
In reply to David HowellsQuoted 16 lines · David Howells
Hi Herbert,
I'm wondering from where I should invoke the FIPS tests for ML-DSA.
Currently, the asymmetric key type has some FIPS selftests for RSA and ECDSA built into it, but I wonder if that's the best way. The problem is that it does the selftest during module init - but that can only test whatever algorithms are built into the base kernel image and initialised at the time
late_initcall()happens.It might be better to put the tests into the algorithm modules themselves - but that then has a potential circular dependency issue. However, that might not matter as the asymmetric key type won't be built as a module and will be built into the kernel (though some of the components such as X.509 and PKCS#7 can be built as modules).
Quoted 4 lines
If I don't involve X.509/PKCS#7 in the selftest, then doing it from the ML-DSA modules during module init would be fine.
Do you (or anyone else) have any thoughts?
The FIPS self-test should just go in the algorithm module itself and test ML-DSA directly. See the other lib/crypto/ FIPS self-tests. Please check the FIPS Implementation Guidance documentation to see what is needed; it isn't actually very much.
- Eric
Eric Biggers
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
In reply to David HowellsQuoted 28 lines · David Howells
lib/crypto/Kconfig | 1 + lib/crypto/Makefile | 2 + lib/crypto/mldsa/Kconfig | 29 ++ lib/crypto/mldsa/Makefile | 20 + lib/crypto/mldsa/crypto_mldsa_44.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_65.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_87.c | 166 ++++++++ lib/crypto/mldsa/dilithium.h | 304 ++++++++++++++ lib/crypto/mldsa/dilithium_44.c | 33 ++ lib/crypto/mldsa/dilithium_44.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_65.c | 33 ++ lib/crypto/mldsa/dilithium_65.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_87.c | 33 ++ lib/crypto/mldsa/dilithium_87.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_common.c | 117 ++++++ 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 +++++
Quoted 8 lines
lib/crypto/mldsa/dilithium_signature_c.c | 279 +++++++++++++ lib/crypto/mldsa/dilithium_signature_c.h | 37 ++ lib/crypto/mldsa/dilithium_signature_impl.h | 370 +++++++++++++++++ lib/crypto/mldsa/dilithium_type.h | 108 +++++ lib/crypto/mldsa/dilithium_zetas.c | 68 ++++ .../mldsa/signature_domain_separation.c | 204 ++++++++++ .../mldsa/signature_domain_separation.h | 30 ++ 35 files changed, 4801 insertions(+)
Over the past week I've been starting to review this massive addition.
I don't think this is on the right track. This implementation is really messy, with lots of unused functionality and unnecessary abstractions, and code that doesn't follow kernel conventions.
In comparison, BoringSSL has an entire implementation of ML-DSA, *including key generation and signing*, in a bit over 3000 lines in one file. But about half of that code is specific to key generation or signing, which the kernel doesn't need, so in principle verification-only shouldn't be much more than a thousand. I find it to be much easier to understand than leancrypto as well.
Historically we've had a lot of problems with people integrating code from external sources into the kernel, like mpi, with properly "owning" it because they feel like it's not their code and someone else is responsible. I feel like that's going to be a big problem here.
I think we can do better here and put together a smaller implementation for the kernel that we'll actually be able to maintain.
- Eric
Eric Biggers
maintainerRe: Pick up keys-pqc branch for linux-next?
In reply to David HowellsQuoted 10 lines · David Howells
Hi Stephen,
Can you pick up my keys-pqc branch for linux-next please? It can be found at:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/
keys-pqcNote that it's based on Eric Bigger's libcrypto/lbcrypto-next branch which I believe you already have in order to get SHA-3/SHAKE support.
I don't really see the point yet, since this isn't going to be ready for the next merge window anyway.
- Eric
David Howells
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
In reply to Eric BiggersQuoted 1 line · Eric Biggers
In comparison, BoringSSL has an entire implementation of ML-DSA, ...
... which cannot be used in the kernel due to the licence.
David
David Howells
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
Quoted 5 lines
Quoted 1 line · Eric Biggers
In comparison, BoringSSL has an entire implementation of ML-DSA, ...
... which cannot be used in the kernel due to the licence.
... and it's written in C++. Now, whilst I'm in favour generally of moving the kernel to C++, it could be a hard sell.
David
Eric Biggers
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
In reply to David HowellsQuoted 7 lines · David Howells
Quoted 1 line · Eric Biggers
In comparison, BoringSSL has an entire implementation of ML-DSA, ...
... which cannot be used in the kernel due to the licence.
David
First, BoringSSL's license changed to Apache only recently. An older version of the code is available under a BSD style license.
Second, even with the new license I can get permission to relicense it if needed.
Third, regardless of license BoringSSL's code can't be reused directly in the kernel anyway, for various reasons. My point is that a smaller, cleaner, and more maintainable implementation of ML-DSA is possible, and your submission misses the mark.
- Eric
Re: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
In reply to David HowellsQuoted 6 lines · David Howells
Quoted 2 lines · Eric Biggers
In comparison, BoringSSL has an entire implementation of ML-DSA,
...... which cannot be used in the kernel due to the licence.
The licence problem is not insurmountable. There are now real lawyers (not just me) prepared to stand up and say Apache-2 is GPL-2 compatible:
https://digitalcommons.law.scu.edu/chtlj/vol40/iss3/2/
But even if you don't accept that, Google keeps effective joint ownership of the code through their CLA and so could grant a dual licence to the kernel anyway without needing to refer to any contributors.
Regards,
James
David Howells
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
Quoted 4 lines · James Bottomley
But even if you don't accept that, Google keeps effective joint ownership of the code through their CLA and so could grant a dual licence to the kernel anyway without needing to refer to any contributors.
Actually, the fact that BoringSSL's ML-DSA implementation uses C++ with heavy use of integer-parametered templating is more insurmountable for borrowing their code. Yes, it does allow them to reduce their LoC to ~3000 and is much more readable, but I can't do that in C. Now, if we want to work on persuading Linus to allow C++ into the kernel...
David
Re: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
Quoted 7 lines · David Howells
Quoted 4 lines · James Bottomley
But even if you don't accept that, Google keeps effective joint ownership of the code through their CLA and so could grant a dual licence to the kernel anyway without needing to refer to any contributors.
Quoted 5 lines
Actually, the fact that BoringSSL's ML-DSA implementation uses C++ with heavy use of integer-parametered templating is more insurmountable for borrowing their code. Yes, it does allow them to reduce their LoC to ~3000 and is much more readable, but I can't do that in C.
I was only commenting on the legality of copying not the technical feasibility.
Quoted 2 lines
Now, if we want to work on persuading Linus to allow C++ into the kernel...
Having worked on it, C++ is a bit of a messy language in that there are many more idioms for the same construct than in C, so I can see why C is preferred for cleaner coding. On the other hand if the safety profiles work actually produces something useful (unlike safe C++ which just flamed out) I could see that being a reason to revisit.
Regards,
James
Eric Biggers
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
In reply to Eric BiggersQuoted 2 lines · Eric Biggers
My point is that a smaller, cleaner, and more maintainable implementation of ML-DSA is possible,
I've written an implementation of ML-DSA verification in about 600 lines. It may grow slightly as I clean it up and test it, but we definitely don't need 4800 lines. I'll send it out once it's ready.
By the way, your proposed KUnit test suite has no negative test cases :( We can do better than that.
- Eric
David Howells
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
Quoted 3 lines · Eric Biggers
I've written an implementation of ML-DSA verification in about 600 lines. It may grow slightly as I clean it up and test it, but we definitely don't need 4800 lines. I'll send it out once it's ready.
Could you post what you have now so that I can have a look at it, please? It doesn't matter if it's fully tested, but I can at least compare the algorithm to that of Leancrypto.
David
Re: Pick up keys-pqc branch for linux-next?
In reply to Eric BiggersHi David,
Quoted 5 lines · On Mon, 17 Nov 2025 09:11:26 -0800 Eric Biggers <[email protected]> wrote:
Quoted 3 lines · David Howells
Can you pick up my keys-pqc branch for linux-next please? It can be found at:
Quoted 8 lines
Quoted 5 lines
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/
keys-pqcNote that it's based on Eric Bigger's libcrypto/lbcrypto-next branch which I believe you already have in order to get SHA-3/SHAKE support.
I don't really see the point yet, since this isn't going to be ready for the next merge window anyway.
Yeah, if it is not going into the next merge window, then I don't want it until after that merge window closes, OK?
Signature · 2 lines
Cheers, Stephen Rothwell
Eric Biggers
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
In reply to David HowellsQuoted 27 lines · David Howells
lib/crypto/mldsa/dilithium_poly.c new file +27
-0+/* +* @brief poly_uniform - Sample polynomial with uniformly random coefficients +* in [0,Q-1] by performing rejection sampling on the +* output stream of SHAKE128(seed|nonce). +* +* @param [out] a pointer to output polynomial +* @param [in] seed byte array with seed of length DILITHIUM_SEEDBYTES +* @param [in] nonce 2-byte nonce +*/ +void poly_uniform (poly *a,const uint8_t seed[DILITHIUM_SEEDBYTES ], +__le16 nonce,void *ws_buf) +{ +struct shake_ctx hash_ctx; +unsigned int i, ctr, off; +unsigned int buflen =POLY_UNIFORM_NBLOCKS *SHAKE128_BLOCK_SIZE ; +uint8_t *buf = ws_buf; + +shake128_init (&hash_ctx); +shake_update (&hash_ctx, seed,DILITHIUM_SEEDBYTES ); +shake_update (&hash_ctx, (uint8_t *)&nonce,sizeof (nonce)); +shake_squeeze (&hash_ctx, buf, buflen); + + ctr =rej_uniform (a->coeffs,DILITHIUM_N , buf, buflen); + +while (ctr <DILITHIUM_N ) { + off = buflen %3 ; +for (i =0 ; i < off; ++i)
lib/crypto/mldsa/dilithium_poly.c line 101
Quoted 10 lines
lib/crypto/mldsa/dilithium_poly.c new file +10
-0+ buf[i] = buf[buflen - off + i]; + +shake_squeeze (&hash_ctx, buf + off,SHAKE128_BLOCK_SIZE ); + buflen =DILITHIUM_SEEDBYTES + off; + ctr +=rej_uniform (a->coeffs + ctr,DILITHIUM_N - ctr, buf, + buflen); + } + +shake_zeroize_ctx (&hash_ctx); +}
By the way, the above has a bug. In the second and later squeezes, it squeezes SHAKE128_BLOCK_SIZE (168) bytes, but then it uses only the first DILITHIUM_SEEDBYTES (32) bytes.
Now, that 32 is on top of the 840-byte first squeeze, so there are 872 correct bytes which is enough for 290 samples. So an incorrect matrix would be generated only if more than 290 samples happen to be required to get the 256 coefficients. q / 2^23 = ~99.9% of coefficients are accepted, so that number of rejections would be pretty unlikely.
Still, it's a bug. Anyway, we're not going to use this code (we'll use my code that does this correctly and in a simpler way), but I thought I'd point it out so that Stephan can fix it. This seems to be a "leancrypto" specific bug.
Note: this feedback should not be taken as implying that I've reviewed the entire 4800 lines of code. I just happened to notice this.
- Eric
Eric Biggers
maintainerRe: Pick up keys-pqc branch for linux-next?
In reply to Stephen RothwellQuoted 11 lines · Stephen Rothwell
Hi David,
Quoted 8 lines · On Mon, 17 Nov 2025 09:11:26 -0800 Eric Biggers <[email protected]> wrote:
Quoted 6 lines · David Howells
Can you pick up my keys-pqc branch for linux-next please? It can be found at:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/
keys-pqc
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Note that it's based on Eric Bigger's libcrypto/lbcrypto-next branch which I believe you already have in order to get SHA-3/SHAKE support.
I don't really see the point yet, since this isn't going to be ready for the next merge window anyway.
Yeah, if it is not going into the next merge window, then I don't want it until after that merge window closes, OK?
Makes sense to me. David, with
https://lore.kernel.org/r/[email protected]
we're getting closer, but it's still too late for 6.19. We need proper tests to be added at the same time as the ML-DSA implementation itself. The higher-level parts such as crypto/asymmetric_keys/ need review too. Also, lib/crypto/ changes should go through the libcrypto tree.
wycheproof has negative test vectors for ML-DSA edge cases. We maybe could borrow those. Note that ML-DSA keys and signatures are very large, which results in huge files for the test vectors. I'd also like to keep the size down, but we might not have much choice there.
I'm also working to get another crypto-oriented developer, who doesn't normally work on the Linux kernel, to review my ML-DSA code. (I don't think I was going to have any chance at getting anyone to look at the original 4800-line submission, but with the 600-line one it's feasible.)
It's also worth noting that this is ML-DSA, not ML-KEM. The cryptographic community generally views upgrading to quantum-resistant key encapsulation (e.g. ML-KEM) as more urgent than upgrading to quantum-resistant signatures (e.g. ML-DSA). I assume you have a reason why you want the signatures. That's fine, and I'm okay with ML-DSA support being added. But we shouldn't rush it in.
- Eric
Eric Biggers
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
In reply to Eric BiggersQuoted 49 lines · Eric Biggers
Quoted 27 lines · David Howells
lib/crypto/mldsa/dilithium_poly.c new file +27
-0+/* +* @brief poly_uniform - Sample polynomial with uniformly random coefficients +* in [0,Q-1] by performing rejection sampling on the +* output stream of SHAKE128(seed|nonce). +* +* @param [out] a pointer to output polynomial +* @param [in] seed byte array with seed of length DILITHIUM_SEEDBYTES +* @param [in] nonce 2-byte nonce +*/ +void poly_uniform (poly *a,const uint8_t seed[DILITHIUM_SEEDBYTES ], +__le16 nonce,void *ws_buf) +{ +struct shake_ctx hash_ctx; +unsigned int i, ctr, off; +unsigned int buflen =POLY_UNIFORM_NBLOCKS *SHAKE128_BLOCK_SIZE ; +uint8_t *buf = ws_buf; + +shake128_init (&hash_ctx); +shake_update (&hash_ctx, seed,DILITHIUM_SEEDBYTES ); +shake_update (&hash_ctx, (uint8_t *)&nonce,sizeof (nonce)); +shake_squeeze (&hash_ctx, buf, buflen); + + ctr =rej_uniform (a->coeffs,DILITHIUM_N , buf, buflen); + +while (ctr <DILITHIUM_N ) { + off = buflen %3 ; +for (i =0 ; i < off; ++i)lib/crypto/mldsa/dilithium_poly.c line 101
Quoted 10 lines
lib/crypto/mldsa/dilithium_poly.c new file +10
-0+ buf[i] = buf[buflen - off + i]; + +shake_squeeze (&hash_ctx, buf + off,SHAKE128_BLOCK_SIZE ); + buflen =DILITHIUM_SEEDBYTES + off; + ctr +=rej_uniform (a->coeffs + ctr,DILITHIUM_N - ctr, buf, + buflen); + } + +shake_zeroize_ctx (&hash_ctx); +}By the way, the above has a bug. In the second and later squeezes, it squeezes
SHAKE128_BLOCK_SIZE(168) bytes, but then it uses only the firstDILITHIUM_SEEDBYTES(32) bytes.Now, that 32 is on top of the 840-byte first squeeze, so there are 872 correct bytes which is enough for 290 samples. So an incorrect matrix would be generated only if more than 290 samples happen to be required to get the 256 coefficients. q / 2^23 = ~99.9% of coefficients are accepted, so that number of rejections would be pretty unlikely.
Quoted 7 lines
Still, it's a bug. Anyway, we're not going to use this code (we'll use my code that does this correctly and in a simpler way), but I thought I'd point it out so that Stephan can fix it. This seems to be a "leancrypto" specific bug.
Note: this feedback should not be taken as implying that I've reviewed the entire 4800 lines of code. I just happened to notice this.
No reply from Stephan yet, so to make sure this doesn't get missed I also opened an issue at
https://github.com/smuellerDD/leancrypto/issues/42
- Eric
Re: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
Am Dienstag, 25. November 2025, 05:10:50 Mitteleuropäische Normalzeit schrieb Eric Biggers:
Hi Eric,
Quoted 3 lines
No reply from Stephan yet, so to make sure this doesn't get missed I also opened an issue at
https://github.com/smuellerDD/leancrypto/issues/42
Thanks for the report, fixed.
Ciao
Stephan
Re: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
In reply to Eric BiggersHi all,
Quoted 52 lines · Eric Biggers
Quoted 28 lines · David Howells
lib/crypto/Kconfig | 1 + lib/crypto/Makefile | 2 + lib/crypto/mldsa/Kconfig | 29 ++ lib/crypto/mldsa/Makefile | 20 + lib/crypto/mldsa/crypto_mldsa_44.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_65.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_87.c | 166 ++++++++ lib/crypto/mldsa/dilithium.h | 304 ++++++++++++++ lib/crypto/mldsa/dilithium_44.c | 33 ++ lib/crypto/mldsa/dilithium_44.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_65.c | 33 ++ lib/crypto/mldsa/dilithium_65.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_87.c | 33 ++ lib/crypto/mldsa/dilithium_87.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_common.c | 117 ++++++ 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 +++++Quoted 8 lines
lib/crypto/mldsa/dilithium_signature_c.c | 279 +++++++++++++ lib/crypto/mldsa/dilithium_signature_c.h | 37 ++ lib/crypto/mldsa/dilithium_signature_impl.h | 370 +++++++++++++++++ lib/crypto/mldsa/dilithium_type.h | 108 +++++ lib/crypto/mldsa/dilithium_zetas.c | 68 ++++ .../mldsa/signature_domain_separation.c | 204 ++++++++++ .../mldsa/signature_domain_separation.h | 30 ++ 35 files changed, 4801 insertions(+)Over the past week I've been starting to review this massive addition.
I don't think this is on the right track. This implementation is really messy, with lots of unused functionality and unnecessary abstractions, and code that doesn't follow kernel conventions.
In comparison, BoringSSL has an entire implementation of ML-DSA, *including key generation and signing*, in a bit over 3000 lines in one file. But about half of that code is specific to key generation or signing, which the kernel doesn't need, so in principle verification-only shouldn't be much more than a thousand. I find it to be much easier to understand than leancrypto as well.
Quoted 7 lines
Historically we've had a lot of problems with people integrating code from external sources into the kernel, like mpi, with properly "owning" it because they feel like it's not their code and someone else is responsible. I feel like that's going to be a big problem here.
I think we can do better here and put together a smaller implementation for the kernel that we'll actually be able to maintain.
I was thinking about this lately for some time - even put forward a small discussion proposal for upcoming Plumbers (which wasn't accepted unfortunately). Should we consider somehow safely "outsourcing" at least some (asymmetric - potentially slow anyway) crypto implementations to userspace? Something similar to request_module/request_firmware/request_key usermode helpers or an io_uring interface or a crypto socket? This way we can bring any well-maintained crypto library (and even multiple ones) to the kernel:
* it can have any software license
* can be written in any programming language
* can use architecture vector instructions more easily
* can have any certifications out of the box (looking at you - FIPS)
* distros would have the ability to plug their own
* maybe it can even do private key crypto (which I personally would
really like more support of and it would be acceptable to Herbert)
Given the past experience of RSA and mpi - it is not that difficult to port something to the kernel, but quite hard to maintain it over time in a secure manner. With a userspace approach the kernel can piggy-back on proven vendors like any other piece of open source software out there.
I understand that there probably still be a need for some in-kernel crypto, so the proposal here is not to fully replace things, but rather complement the current offerings with an interface, which could enable faster adoption of newer (and more secure versions of existing) crypto algorithms.
Quoted 1 line
- Eric
Ignat
Eric Biggers
maintainerRe: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
Quoted 85 lines · Ignat Korchagin
Hi all,
Quoted 52 lines · Eric Biggers
Quoted 28 lines · David Howells
lib/crypto/Kconfig | 1 + lib/crypto/Makefile | 2 + lib/crypto/mldsa/Kconfig | 29 ++ lib/crypto/mldsa/Makefile | 20 + lib/crypto/mldsa/crypto_mldsa_44.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_65.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_87.c | 166 ++++++++ lib/crypto/mldsa/dilithium.h | 304 ++++++++++++++ lib/crypto/mldsa/dilithium_44.c | 33 ++ lib/crypto/mldsa/dilithium_44.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_65.c | 33 ++ lib/crypto/mldsa/dilithium_65.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_87.c | 33 ++ lib/crypto/mldsa/dilithium_87.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_common.c | 117 ++++++ 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 +++++Quoted 8 lines
lib/crypto/mldsa/dilithium_signature_c.c | 279 +++++++++++++ lib/crypto/mldsa/dilithium_signature_c.h | 37 ++ lib/crypto/mldsa/dilithium_signature_impl.h | 370 +++++++++++++++++ lib/crypto/mldsa/dilithium_type.h | 108 +++++ lib/crypto/mldsa/dilithium_zetas.c | 68 ++++ .../mldsa/signature_domain_separation.c | 204 ++++++++++ .../mldsa/signature_domain_separation.h | 30 ++ 35 files changed, 4801 insertions(+)Over the past week I've been starting to review this massive addition.
I don't think this is on the right track. This implementation is really messy, with lots of unused functionality and unnecessary abstractions, and code that doesn't follow kernel conventions.
In comparison, BoringSSL has an entire implementation of ML-DSA, *including key generation and signing*, in a bit over 3000 lines in one file. But about half of that code is specific to key generation or signing, which the kernel doesn't need, so in principle verification-only shouldn't be much more than a thousand. I find it to be much easier to understand than leancrypto as well.
Quoted 7 lines
Historically we've had a lot of problems with people integrating code from external sources into the kernel, like mpi, with properly "owning" it because they feel like it's not their code and someone else is responsible. I feel like that's going to be a big problem here.
I think we can do better here and put together a smaller implementation for the kernel that we'll actually be able to maintain.
I was thinking about this lately for some time - even put forward a small discussion proposal for upcoming Plumbers (which wasn't accepted unfortunately). Should we consider somehow safely "outsourcing" at least some (asymmetric - potentially slow anyway) crypto implementations to userspace? Something similar to request_module/request_firmware/request_key usermode helpers or an
io_uringinterface or a crypto socket? This way we can bring any well-maintained crypto library (and even multiple ones) to the kernel:
* it can have any software license
* can be written in any programming language
* can use architecture vector instructions more easily
* can have any certifications out of the box (looking at you - FIPS)
* distros would have the ability to plug their own
* maybe it can even do private key crypto (which I personally would
really like more support of and it would be acceptable to Herbert)Given the past experience of RSA and mpi - it is not that difficult to port something to the kernel, but quite hard to maintain it over time in a secure manner. With a userspace approach the kernel can piggy-back on proven vendors like any other piece of open source software out there.
Quoted 5 lines
I understand that there probably still be a need for some in-kernel crypto, so the proposal here is not to fully replace things, but rather complement the current offerings with an interface, which could enable faster adoption of newer (and more secure versions of existing) crypto algorithms.
The performance cost of that would be very high, so it would only have any chance at possibly being reasonable for some of the asymmetric algorithms. It would also introduce reliability issues.
I'll also note that the main reason that people seem to want private key operations in the kernel is for the keyctl() UAPIs for userspace, which is already a bad idea. So I guess we end up with userspace calling into the kernel, which calls back into userspace to use some userspace crypto library which the original userspace program refused to use in the first place for some reason? It makes no sense to me, sorry.
There is the opportunity to share more code with userspace projects at the source code level. Just it doesn't always work out due to different languages, licences, requirements, conventions, and implementation qualities. For ML-DSA verification I didn't see a good alternative to just writing it myself. But in other cases a different conclusion could be reached. The kernel uses a lot of the assembly files from CRYPTOGAMS, for example, and some of the Curve25519 code (including formally verified code) is imported from other sources.
- Eric
Re: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
Quoted 93 lines · Eric Biggers
Quoted 85 lines · Ignat Korchagin
Hi all,
Quoted 52 lines · Eric Biggers
Quoted 28 lines · David Howells
lib/crypto/Kconfig | 1 + lib/crypto/Makefile | 2 + lib/crypto/mldsa/Kconfig | 29 ++ lib/crypto/mldsa/Makefile | 20 + lib/crypto/mldsa/crypto_mldsa_44.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_65.c | 166 ++++++++ lib/crypto/mldsa/crypto_mldsa_87.c | 166 ++++++++ lib/crypto/mldsa/dilithium.h | 304 ++++++++++++++ lib/crypto/mldsa/dilithium_44.c | 33 ++ lib/crypto/mldsa/dilithium_44.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_65.c | 33 ++ lib/crypto/mldsa/dilithium_65.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_87.c | 33 ++ lib/crypto/mldsa/dilithium_87.h | 291 ++++++++++++++ lib/crypto/mldsa/dilithium_common.c | 117 ++++++ 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 +++++Quoted 8 lines
lib/crypto/mldsa/dilithium_signature_c.c | 279 +++++++++++++ lib/crypto/mldsa/dilithium_signature_c.h | 37 ++ lib/crypto/mldsa/dilithium_signature_impl.h | 370 +++++++++++++++++ lib/crypto/mldsa/dilithium_type.h | 108 +++++ lib/crypto/mldsa/dilithium_zetas.c | 68 ++++ .../mldsa/signature_domain_separation.c | 204 ++++++++++ .../mldsa/signature_domain_separation.h | 30 ++ 35 files changed, 4801 insertions(+)Over the past week I've been starting to review this massive addition.
I don't think this is on the right track. This implementation is really messy, with lots of unused functionality and unnecessary abstractions, and code that doesn't follow kernel conventions.
In comparison, BoringSSL has an entire implementation of ML-DSA, *including key generation and signing*, in a bit over 3000 lines in one file. But about half of that code is specific to key generation or signing, which the kernel doesn't need, so in principle verification-only shouldn't be much more than a thousand. I find it to be much easier to understand than leancrypto as well.
Quoted 7 lines
Historically we've had a lot of problems with people integrating code from external sources into the kernel, like mpi, with properly "owning" it because they feel like it's not their code and someone else is responsible. I feel like that's going to be a big problem here.
I think we can do better here and put together a smaller implementation for the kernel that we'll actually be able to maintain.
I was thinking about this lately for some time - even put forward a small discussion proposal for upcoming Plumbers (which wasn't accepted unfortunately). Should we consider somehow safely "outsourcing" at least some (asymmetric - potentially slow anyway) crypto implementations to userspace? Something similar to request_module/request_firmware/request_key usermode helpers or an
io_uringinterface or a crypto socket? This way we can bring any well-maintained crypto library (and even multiple ones) to the kernel:
* it can have any software license
* can be written in any programming language
* can use architecture vector instructions more easily
* can have any certifications out of the box (looking at you - FIPS)
* distros would have the ability to plug their own
* maybe it can even do private key crypto (which I personally would
really like more support of and it would be acceptable to Herbert)Given the past experience of RSA and mpi - it is not that difficult to port something to the kernel, but quite hard to maintain it over time in a secure manner. With a userspace approach the kernel can piggy-back on proven vendors like any other piece of open source software out there.
Quoted 5 lines
I understand that there probably still be a need for some in-kernel crypto, so the proposal here is not to fully replace things, but rather complement the current offerings with an interface, which could enable faster adoption of newer (and more secure versions of existing) crypto algorithms.
Quoted 3 lines
The performance cost of that would be very high, so it would only have any chance at possibly being reasonable for some of the asymmetric algorithms. It would also introduce reliability issues.
Yes, definitely. For the userspace approach I would think only asymmetric algorithms. For "reliability issues" I don't know if it is a problem of the kernel itself, or the user/admin of the system. The kernel supports network-based and userspace-based filesystems and userspace-based block devices even and users successfully use them as root filesystems. Surely a small crypto agent running locally would not be worse than this.
Quoted 3 lines
I'll also note that the main reason that people seem to want private key operations in the kernel is for the
keyctl()UAPIs for userspace, which is already a bad idea. So I guess we end up with userspace calling into
Can you elaborate on this? (I want to understand for myself). I think keyctl UAPIs for private keys are a great idea and allows building good (and relatively simple) secure architectures. So I want to enable more of this, not less.
Quoted 3 lines
the kernel, which calls back into userspace to use some userspace crypto library which the original userspace program refused to use in the first place for some reason? It makes no sense to me, sorry.
It does make sense for userspace programs (apart from potential keyctl users which don't want to have private keys in their address space), but what about in-kernel services? How is this fundamentally different from request_module, for example? If the kernel needs a module, it tells userspace and there is a userspace helper, which provides it. It is an "external service" to the kernel. So crypto can be another service. After all, microkernels have been doing this forever.
Quoted 6 lines
There is the opportunity to share more code with userspace projects at the source code level. Just it doesn't always work out due to different languages, licences, requirements, conventions, and implementation qualities. For ML-DSA verification I didn't see a good alternative to just writing it myself. But in other cases a different conclusion could be reached. The kernel uses a lot of the assembly files from
What about sharing code at the binary level? At Cloudflare we have an internal kernel crypto driver, which just imports its implementation directly from compiled BoringSSL into the kernel space. So in the end it is fully in-kernel and fast, but the advantage is that the code itself comes from BoringSSL (and we regularly update it). There are rough edges of course (like using vector instructions, different stack alignment etc), but it kinda works and I think with some investment we can make such an approach upstream. We even considered open sourcing it, but not sure about licensing as we effectively have a bespoke "module loader" for BoringSSL, which is Apache (I hope James is right that Apache may be considered GPL-2 compatible in the future).
While I think such an approach is much more technically challenging, it provides "best of both worlds" by having fast in-kernel crypto directly from established libraries.
Quoted 4 lines
CRYPTOGAMS, for example, and some of the Curve25519 code (including formally verified code) is imported from other sources.
- Eric
Ignat