]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Move read_hex_file from slh-dsa-test.c to testutils.c master master-updates
authorDaiki Ueno <dueno@redhat.com>
Thu, 30 Oct 2025 03:35:15 +0000 (12:35 +0900)
committerDaiki Ueno <dueno@redhat.com>
Wed, 15 Apr 2026 14:31:03 +0000 (23:31 +0900)
The function is also useful for ML-KEM and ML-DSA tests, whose test
vectors are large.

Signed-off-by: Daiki Ueno <dueno@redhat.com>
testsuite/slh-dsa-test.c
testsuite/testutils.c
testsuite/testutils.h

index 7653687b73eaa1e954182e35e7709be9d18b7a86..1be9b24fa6abbc4f4b797f3b86f364daee404f20 100644 (file)
 
 #include "testutils.h"
 
-#include "base16.h"
 #include "sha3.h"
 #include "slh-dsa.h"
 #include "slh-dsa-internal.h"
 #include "bswap-internal.h"
 
-#include <errno.h>
-
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-
-static const struct tstring *
-read_hex_file (const char *name, size_t max_size)
-{
-  char input_buf[1000];
-  FILE *input = open_srcdir_file (name);
-  size_t done;
-  struct tstring *s;
-  struct base16_decode_ctx ctx;
-  base16_decode_init (&ctx);
-
-  /* This function expects to read up to EOF, and fails if the amount
-     of data exceeds the max size. */
-  s = tstring_alloc (max_size);
-
-  for (done = 0;;)
-    {
-      size_t left = s->length - done;
-      size_t got = fread (input_buf, 1, MIN (left, sizeof (input_buf)), input);
-      size_t res;
-
-      if (!got)
-       {
-         if (ferror (input))
-           {
-             fprintf (stderr, "reading %s failed: %s\n", name, strerror (errno));
-             FAIL ();
-           }
-         fclose (input);
-         ASSERT (base16_decode_final (&ctx));
-         s->length = done;
-         return s;
-       }
-
-      res = s->length - done;
-      if (!base16_decode_update (&ctx, &res, s->data + done, got, input_buf))
-       {
-         fprintf (stderr, "hex decoding %s failed (possibly exceeding max size)\n", name);
-         FAIL ();
-       }
-      done += res;
-    }
-}
-
 static void
 test_wots_gen (const struct tstring *public_seed, const struct tstring *secret_seed,
               unsigned layer, uint64_t tree_idx, uint32_t keypair,
index 7b0c9020a428673489c86c06ae195f10908d1c69..74798c49bec1e0be9efc01fe40f7b0778f95cac2 100644 (file)
@@ -20,6 +20,8 @@
 # include <valgrind/valgrind.h>
 #endif
 
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
 void
 die(const char *format, ...)
 {
@@ -2548,3 +2550,46 @@ test_ecc_get_ga (unsigned curve, mp_limb_t *rp)
 #include "mini-gmp.c"
 #endif /* !WITH_HOGWEED */
 
+const struct tstring *
+read_hex_file (const char *name, size_t max_size)
+{
+  char input_buf[1000];
+  FILE *input = open_srcdir_file (name);
+  size_t done;
+  struct tstring *s;
+  struct base16_decode_ctx ctx;
+  base16_decode_init (&ctx);
+
+  /* This function expects to read up to EOF, and fails if the amount
+     of data exceeds the max size. */
+  s = tstring_alloc (max_size);
+
+  for (done = 0;;)
+    {
+      size_t left = s->length - done;
+      size_t got = fread (input_buf, 1, MIN (left, sizeof (input_buf)), input);
+      size_t res;
+
+      if (!got)
+       {
+         if (ferror (input))
+           {
+             fprintf (stderr, "reading %s failed: %s\n", name, strerror (errno));
+             FAIL ();
+           }
+         fclose (input);
+         ASSERT (base16_decode_final (&ctx));
+         s->length = done;
+         return s;
+       }
+
+      res = s->length - done;
+      if (!base16_decode_update (&ctx, &res, s->data + done, got, input_buf))
+       {
+         fprintf (stderr, "hex decoding %s failed (possibly exceeding max size)\n", name);
+         FAIL ();
+       }
+      done += res;
+    }
+}
+
index 43c9b4116c7173b9aac5998a431cc870a5fe0468..b30dedac2ee510c454ff9e57c5c0ba90c83aa19c 100644 (file)
@@ -71,6 +71,9 @@ print_hex(size_t length, const uint8_t *data);
 FILE *
 open_srcdir_file (const char *name);
 
+const struct tstring *
+read_hex_file (const char *name, size_t max_size);
+
 /* If side-channel tests are requested, attach valgrind annotations on
    given memory area. */
 void