1
0
mirror of https://github.com/openbsd/src.git synced 2026-05-01 09:37:02 +00:00

function to make a sshbuf from a hex string; useful in tests

also constify some arguments
This commit is contained in:
djm
2025-05-21 06:43:48 +00:00
parent f1a571bc1d
commit 79493ce72e
2 changed files with 42 additions and 4 deletions

View File

@@ -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
*
@@ -67,7 +67,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);
@@ -86,6 +86,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)
{

View File

@@ -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
*
@@ -237,7 +237,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);