From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 2/3] lib/crypto: mldsa: Explicitly check that 'alg' is a valid value
Date: Wed, 28 Oct 2026 22:45:33 -0700 [thread overview]
Message-ID: <20261029054534.68481-3-ebiggers@kernel.org> (raw)
In-Reply-To: <20261029054534.68481-1-ebiggers@kernel.org>
C doesn't guarantee at the language level that an enum actually has one
of the declared values.
For robustness, make mldsa_verify() handle invalid values of 'alg'.
No effect on current callers, who always pass a valid value for 'alg'.
(Except for the KUnit test case that this commit adds.)
Fixes: 64edccea594c ("lib/crypto: Add ML-DSA verification support")
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
include/crypto/mldsa.h | 1 +
lib/crypto/mldsa.c | 10 ++++++++--
lib/crypto/tests/mldsa_kunit.c | 9 +++++++++
3 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/include/crypto/mldsa.h b/include/crypto/mldsa.h
index 3ef2676787c9e..e0754879e7452 100644
--- a/include/crypto/mldsa.h
+++ b/include/crypto/mldsa.h
@@ -48,6 +48,7 @@ enum mldsa_alg {
* Return:
* * 0 if the signature is valid
* * -EBADMSG if the signature and/or public key is malformed
+ * * -EINVAL if @alg is not a valid value
* * -EKEYREJECTED if the signature is invalid but otherwise well-formed
* * -ENOMEM if out of memory so the validity of the signature is unknown
*/
diff --git a/lib/crypto/mldsa.c b/lib/crypto/mldsa.c
index 68a1b432315b1..80e0e63b418a7 100644
--- a/lib/crypto/mldsa.c
+++ b/lib/crypto/mldsa.c
@@ -528,8 +528,8 @@ static size_t encode_w1(u8 out[MAX_W1_ENCODED_LEN],
int mldsa_verify(enum mldsa_alg alg, const u8 *sig, size_t sig_len,
const u8 *msg, size_t msg_len, const u8 *pk, size_t pk_len)
{
- const struct mldsa_parameter_set *params = &mldsa_parameter_sets[alg];
- const int k = params->k, l = params->l;
+ const struct mldsa_parameter_set *params;
+ int k, l;
/* For now this just does pure ML-DSA with an empty context string. */
static const u8 msg_prefix[2] = { /* dom_sep= */ 0, /* ctx_len= */ 0 };
const u8 *ctilde; /* The signer's commitment hash */
@@ -537,6 +537,12 @@ int mldsa_verify(enum mldsa_alg alg, const u8 *sig, size_t sig_len,
u8 *h; /* The signer's hint vector, length k * N */
size_t w1_enc_len;
+ if (alg != MLDSA44 && alg != MLDSA65 && alg != MLDSA87)
+ return -EINVAL;
+ params = &mldsa_parameter_sets[alg];
+ k = params->k;
+ l = params->l;
+
/* Validate the public key and signature lengths. */
if (pk_len != params->pk_len || sig_len != params->sig_len)
return -EBADMSG;
diff --git a/lib/crypto/tests/mldsa_kunit.c b/lib/crypto/tests/mldsa_kunit.c
index ec64e10c97f25..d0ee704a36944 100644
--- a/lib/crypto/tests/mldsa_kunit.c
+++ b/lib/crypto/tests/mldsa_kunit.c
@@ -372,6 +372,14 @@ static void test_mldsa_use_hint(struct kunit *test)
}
}
+static void test_mldsa_invalid_alg(struct kunit *test)
+{
+ KUNIT_ASSERT_EQ(test, -EINVAL,
+ mldsa_verify(MLDSA44 - 1, NULL, 0, NULL, 0, NULL, 0));
+ KUNIT_ASSERT_EQ(test, -EINVAL,
+ mldsa_verify(MLDSA87 + 1, NULL, 0, NULL, 0, NULL, 0));
+}
+
static void benchmark_mldsa(struct kunit *test,
const struct mldsa_testvector *tv)
{
@@ -414,6 +422,7 @@ static struct kunit_case mldsa_kunit_cases[] = {
KUNIT_CASE(test_mldsa65),
KUNIT_CASE(test_mldsa87),
KUNIT_CASE(test_mldsa_use_hint),
+ KUNIT_CASE(test_mldsa_invalid_alg),
KUNIT_CASE(benchmark_mldsa44),
KUNIT_CASE(benchmark_mldsa65),
KUNIT_CASE(benchmark_mldsa87),
--
2.56.0
next prev parent reply other threads:[~2026-10-29 5:47 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 5:45 [PATCH 0/3] lib/crypto: mldsa: Address some minor nits Eric Biggers
2026-10-29 5:45 ` [PATCH 1/3] lib/crypto: mldsa: Check pk_len before doing pointer arithmetic Eric Biggers
2026-10-29 5:45 ` Eric Biggers [this message]
2026-10-29 5:45 ` [PATCH 3/3] lib/crypto: mldsa: Add __must_check to mldsa_verify() Eric Biggers
2026-10-29 9:44 ` [PATCH 0/3] lib/crypto: mldsa: Address some minor nits Ard Biesheuvel
2026-10-29 17:14 ` Eric Biggers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261029054534.68481-3-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=Jason@zx2c4.com \
--cc=ardb@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.