[openssh-commits] [openssh] branch master updated: upstream: Factor out hex2bin into a shared helper function.

git+noreply at mindrot.org git+noreply at mindrot.org
Wed Jun 17 08:49:00 AEST 2026


This is an automated email from the git hooks/post-receive script.

dtucker pushed a commit to branch master
in repository openssh.

The following commit(s) were added to refs/heads/master by this push:
     new 0d156d385 upstream: Factor out hex2bin into a shared helper function.
0d156d385 is described below

commit 0d156d385e1e9c31bd5fb3652cf620a1f51c9b19
Author: dtucker at openbsd.org <dtucker at openbsd.org>
AuthorDate: Tue Jun 16 22:27:10 2026 +0000

    upstream: Factor out hex2bin into a shared helper function.
    
    Replace sscanf %hhx (which is C99) with plain %x for better compatibility
    in -portable. ok djm@
    
    OpenBSD-Regress-ID: 4d30bb27ffdf2154f1a9f2317df18d256717b300
---
 regress/unittests/crypto/test_ed25519.c     | 10 +---------
 regress/unittests/crypto/test_mldsa.c       | 10 +---------
 regress/unittests/crypto/test_mlkem.c       | 10 +---------
 regress/unittests/test_helper/test_helper.c | 16 +++++++++++++++-
 regress/unittests/test_helper/test_helper.h |  4 +++-
 5 files changed, 21 insertions(+), 29 deletions(-)

diff --git a/regress/unittests/crypto/test_ed25519.c b/regress/unittests/crypto/test_ed25519.c
index 3f6a8196c..14d123832 100644
--- a/regress/unittests/crypto/test_ed25519.c
+++ b/regress/unittests/crypto/test_ed25519.c
@@ -1,4 +1,4 @@
-/* 	$OpenBSD: test_ed25519.c,v 1.2 2026/06/16 09:00:47 dtucker Exp $ */
+/* 	$OpenBSD: test_ed25519.c,v 1.3 2026/06/16 22:27:10 dtucker Exp $ */
 /*
  * Regress test for Ed25519 keypair from seed
  *
@@ -63,14 +63,6 @@ static const struct ed25519_kat ed25519_kats[] = {
 
 void ed25519_tests(void);
 
-static void
-hex2bin(uint8_t *bin, const char *hex, size_t len)
-{
-	size_t i;
-	for (i = 0; i < len; i++)
-		sscanf(hex + i * 2, "%02hhx", &bin[i]);
-}
-
 void
 ed25519_tests(void)
 {
diff --git a/regress/unittests/crypto/test_mldsa.c b/regress/unittests/crypto/test_mldsa.c
index d845c49d4..4cbe8d5f8 100644
--- a/regress/unittests/crypto/test_mldsa.c
+++ b/regress/unittests/crypto/test_mldsa.c
@@ -1,4 +1,4 @@
-/* 	$OpenBSD: test_mldsa.c,v 1.1 2026/06/14 04:08:06 djm Exp $ */
+/* 	$OpenBSD: test_mldsa.c,v 1.3 2026/06/16 22:27:10 dtucker Exp $ */
 /*
  * Regress test for ML-DSA
  *
@@ -82,14 +82,6 @@ free_kats(struct kat **kats, size_t nkats)
 	free(kats);
 }
 
-static void
-hex2bin(uint8_t *bin, const char *hex, size_t len)
-{
-	size_t i;
-	for (i = 0; i < len; i++)
-		sscanf(hex + i * 2, "%02hhx", &bin[i]);
-}
-
 void
 mldsa_tests(void)
 {
diff --git a/regress/unittests/crypto/test_mlkem.c b/regress/unittests/crypto/test_mlkem.c
index 5d1b421f7..e9c41dc4f 100644
--- a/regress/unittests/crypto/test_mlkem.c
+++ b/regress/unittests/crypto/test_mlkem.c
@@ -1,4 +1,4 @@
-/* 	$OpenBSD: test_mlkem.c,v 1.1 2026/06/14 04:08:06 djm Exp $ */
+/* 	$OpenBSD: test_mlkem.c,v 1.2 2026/06/16 22:27:10 dtucker Exp $ */
 /*
  * Regress test for ML-KEM
  *
@@ -112,14 +112,6 @@ static const struct mlkem768_kat mlkem768_kats[] = {
 
 void mlkem_tests(void);
 
-static void
-hex2bin(uint8_t *bin, const char *hex, size_t len)
-{
-	size_t i;
-	for (i = 0; i < len; i++)
-		sscanf(hex + i * 2, "%02hhx", &bin[i]);
-}
-
 void
 mlkem_tests(void)
 {
diff --git a/regress/unittests/test_helper/test_helper.c b/regress/unittests/test_helper/test_helper.c
index 525a82b1f..f734b8f48 100644
--- a/regress/unittests/test_helper/test_helper.c
+++ b/regress/unittests/test_helper/test_helper.c
@@ -1,4 +1,4 @@
-/*	$OpenBSD: test_helper.c,v 1.16 2026/03/06 06:57:33 dtucker Exp $	*/
+/*	$OpenBSD: test_helper.c,v 1.17 2026/06/16 22:27:10 dtucker Exp $	*/
 /*
  * Copyright (c) 2011 Damien Miller <djm at mindrot.org>
  *
@@ -662,6 +662,20 @@ tstod(const struct timespec *ts)
 	return (double)ts->tv_sec + ((double)ts->tv_nsec / 1000000000.0);
 }
 
+void
+hex2bin(uint8_t *bin, const char *hex, size_t len)
+{
+	size_t i;
+	unsigned int v;
+
+	/* Don't use %hhx since it's C99 and older platforms don't have it. */
+	for (i = 0; i < len; i++) {
+		sscanf(hex + i * 2, "%02x", &v);
+		ASSERT_U_INT_LE(v, 256);
+		bin[i] = (uint8_t)v;
+	}
+}
+
 void
 bench_start(const char *file, int line, const char *name)
 {
diff --git a/regress/unittests/test_helper/test_helper.h b/regress/unittests/test_helper/test_helper.h
index 91fdca435..9cfc41553 100644
--- a/regress/unittests/test_helper/test_helper.h
+++ b/regress/unittests/test_helper/test_helper.h
@@ -1,4 +1,4 @@
-/*	$OpenBSD: test_helper.h,v 1.12 2026/03/06 06:57:33 dtucker Exp $	*/
+/*	$OpenBSD: test_helper.h,v 1.13 2026/06/16 22:27:10 dtucker Exp $	*/
 /*
  * Copyright (c) 2011 Damien Miller <djm at mindrot.org>
  *
@@ -39,6 +39,8 @@ typedef void (test_onerror_func_t)(void *);
 void tests(void);
 void benchmarks(void);
 
+void hex2bin(uint8_t *, const char *, size_t);
+
 const char *test_data_file(const char *name);
 void test_start(const char *n);
 void test_info(char *s, size_t len);

-- 
To stop receiving notification emails like this one, please contact
djm at mindrot.org.


More information about the openssh-commits mailing list