[openssh-commits] [openssh] 02/05: upstream: function to make a sshbuf from a hex string; useful in
git+noreply at mindrot.org
git+noreply at mindrot.org
Wed May 21 18:50:07 AEST 2025
This is an automated email from the git hooks/post-receive script.
djm pushed a commit to branch master
in repository openssh.
commit 1743589d038476f28dc4dfb1f69317649ae22ac5
Author: djm at openbsd.org <djm at openbsd.org>
AuthorDate: Wed May 21 06:43:48 2025 +0000
upstream: function to make a sshbuf from a hex string; useful in
tests
also constify some arguments
OpenBSD-Commit-ID: 00f9c25b256be0efd73f2d8268ff041bc45ffb2c
---
sshbuf-misc.c | 40 ++++++++++++++++++++++++++++++++++++++--
sshbuf.h | 6 ++++--
2 files changed, 42 insertions(+), 4 deletions(-)
diff --git a/sshbuf-misc.c b/sshbuf-misc.c
index 9c5c42bba..adbf9903b 100644
--- a/sshbuf-misc.c
+++ b/sshbuf-misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sshbuf-misc.c,v 1.18 2022/01/22 00:43:43 djm Exp $ */
+/* $OpenBSD: sshbuf-misc.c,v 1.19 2025/05/21 06:43:48 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
@@ -71,7 +71,7 @@ sshbuf_dump(const struct sshbuf *buf, FILE *f)
}
char *
-sshbuf_dtob16(struct sshbuf *buf)
+sshbuf_dtob16(const struct sshbuf *buf)
{
size_t i, j, len = sshbuf_len(buf);
const u_char *p = sshbuf_ptr(buf);
@@ -90,6 +90,42 @@ sshbuf_dtob16(struct sshbuf *buf)
return ret;
}
+static int
+b16tod(const char v)
+{
+ if (v >= '0' && v <= '9')
+ return v - '0';
+ if (v >= 'a' && v <= 'f')
+ return 10 + v - 'a';
+ if (v >= 'A' && v <= 'A')
+ return 10 + v - 'A';
+ return -1;
+}
+
+struct sshbuf *
+sshbuf_b16tod(const char *b16)
+{
+ struct sshbuf *ret;
+ size_t o;
+ int r, v1, v2;
+
+ if ((ret = sshbuf_new()) == NULL)
+ return NULL;
+ for (o = 0; b16[o] != '\0'; o += 2) {
+ if ((v1 = b16tod(b16[o])) == -1 ||
+ (v2 = b16tod(b16[o + 1])) == -1) {
+ sshbuf_free(ret);
+ return NULL;
+ }
+ if ((r = sshbuf_put_u8(ret, (u_char)((v1 << 4) | v2))) != 0) {
+ sshbuf_free(ret);
+ return NULL;
+ }
+ }
+ /* success */
+ return ret;
+}
+
int
sshbuf_dtob64(const struct sshbuf *d, struct sshbuf *b64, int wrap)
{
diff --git a/sshbuf.h b/sshbuf.h
index 49c32af8f..681abb9ee 100644
--- a/sshbuf.h
+++ b/sshbuf.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sshbuf.h,v 1.29 2024/08/15 00:51:51 djm Exp $ */
+/* $OpenBSD: sshbuf.h,v 1.30 2025/05/21 06:43:48 djm Exp $ */
/*
* Copyright (c) 2011 Damien Miller
*
@@ -235,7 +235,9 @@ void sshbuf_dump(const struct sshbuf *buf, FILE *f);
void sshbuf_dump_data(const void *s, size_t len, FILE *f);
/* Return the hexadecimal representation of the contents of the buffer */
-char *sshbuf_dtob16(struct sshbuf *buf);
+char *sshbuf_dtob16(const struct sshbuf *buf);
+/* Make a sshbuf from a hex string */
+struct sshbuf *sshbuf_b16tod(const char *b16);
/* Encode the contents of the buffer as base64 */
char *sshbuf_dtob64_string(const struct sshbuf *buf, int wrap);
--
To stop receiving notification emails like this one, please contact
djm at mindrot.org.
More information about the openssh-commits
mailing list