From: Daiki Ueno Date: Thu, 30 Oct 2025 03:35:15 +0000 (+0900) Subject: Move read_hex_file from slh-dsa-test.c to testutils.c X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=HEAD;p=thirdparty%2Fnettle.git Move read_hex_file from slh-dsa-test.c to testutils.c The function is also useful for ML-KEM and ML-DSA tests, whose test vectors are large. Signed-off-by: Daiki Ueno --- diff --git a/testsuite/slh-dsa-test.c b/testsuite/slh-dsa-test.c index 7653687b..1be9b24f 100644 --- a/testsuite/slh-dsa-test.c +++ b/testsuite/slh-dsa-test.c @@ -31,59 +31,11 @@ #include "testutils.h" -#include "base16.h" #include "sha3.h" #include "slh-dsa.h" #include "slh-dsa-internal.h" #include "bswap-internal.h" -#include - -#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, diff --git a/testsuite/testutils.c b/testsuite/testutils.c index 7b0c9020..74798c49 100644 --- a/testsuite/testutils.c +++ b/testsuite/testutils.c @@ -20,6 +20,8 @@ # include #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; + } +} + diff --git a/testsuite/testutils.h b/testsuite/testutils.h index 43c9b411..b30dedac 100644 --- a/testsuite/testutils.h +++ b/testsuite/testutils.h @@ -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