[PATCH] cipher:mldsa: Fix key generation without a seed.

Florian Bauckholt florian.bauckholt at cispa.de
Thu Nov 26 10:09:28 CET 2026


* cipher/pubkey-dilithium.c (mldsa_generate): Check for
GPG_ERR_NO_OBJ to detect a missing seed.
* tests/t-mldsa.c (check_mldsa_genkey): New.
(main): Run 'check_mldsa_genkey'.
--

sexp_extract_param returns GPG_ERR_NO_OBJ for a missing parameter
and not GPG_ERR_NOT_FOUND, so "(genkey(dilithium3))" failed
instead of using a random seed.

Signed-off-by: Florian Bauckholt <florian.bauckholt at cispa.de>
---

 cipher/pubkey-dilithium.c |  2 +-
 tests/t-mldsa.c           | 43 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/cipher/pubkey-dilithium.c b/cipher/pubkey-dilithium.c
index 746aa511..d246e03e 100644
--- a/cipher/pubkey-dilithium.c
+++ b/cipher/pubkey-dilithium.c
@@ -150,7 +150,7 @@ mldsa_generate (const gcry_sexp_t genparms, gcry_sexp_t *r_skey)
    * Extract the seed (if any).
    */
   rc = sexp_extract_param (genparms, NULL, "/S", &seed_mpi, NULL);
-  if (rc == GPG_ERR_NOT_FOUND)
+  if (rc == GPG_ERR_NO_OBJ)
     {
       /* Long term key material. */
       randombytes (seed, SEEDBYTES, GCRY_VERY_STRONG_RANDOM);
diff --git a/tests/t-mldsa.c b/tests/t-mldsa.c
index 2f757636..90499b01 100644
--- a/tests/t-mldsa.c
+++ b/tests/t-mldsa.c
@@ -626,6 +626,47 @@ check_mldsa_kat (const char *fname)
 }


+/* Check the key generation without a seed.  */
+static void
+check_mldsa_genkey (void)
+{
+  gpg_error_t err;
+  gcry_sexp_t keyparm, key, l;
+  int i;
+
+  for (i = 0; mldsa_infos[i].name; i++)
+    {
+      keyparm = key = NULL;
+      err = gcry_sexp_build (&keyparm, NULL, "(genkey(%s))",
+                             mldsa_infos[i].name);
+      if (err)
+        die ("error building s-exp for genkey of %s: %s",
+             mldsa_infos[i].name, gpg_strerror (err));
+
+      err = gcry_pk_genkey (&key, keyparm);
+      if (err)
+        fail ("gcry_pk_genkey without seed failed for %s: %s",
+              mldsa_infos[i].name, gpg_strerror (err));
+      else
+        {
+          l = gcry_sexp_find_token (key, "public-key", 0);
+          if (!l)
+            fail ("gcry_pk_genkey returns no pubkey for %s",
+                  mldsa_infos[i].name);
+          gcry_sexp_release (l);
+          l = gcry_sexp_find_token (key, "private-key", 0);
+          if (!l)
+            fail ("gcry_pk_genkey returns no seckey for %s",
+                  mldsa_infos[i].name);
+          gcry_sexp_release (l);
+        }
+
+      gcry_sexp_release (key);
+      gcry_sexp_release (keyparm);
+    }
+}
+
+
 int
 main (int argc, char **argv)
 {
@@ -693,6 +734,8 @@ main (int argc, char **argv)

   start_timer ();
   check_mldsa_kat (fname);
+  if (!custom_data_file)
+    check_mldsa_genkey ();
   stop_timer ();

   xfree (fname);
--
2.56.0



More information about the Gcrypt-devel mailing list