aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorToke Høiland-Jørgensen <toke@toke.dk>2021-04-14 21:39:43 +0200
committerOndrej Zajicek (work) <santiago@crfreenet.org>2021-06-06 16:28:18 +0200
commit35f88b305ab6a0e27b5ff1b445f63f544986e14e (patch)
tree76051c919ed62297191e1b18eab1525429b43068 /lib
parentf1a824190c22f8159ad0f9378c2dd23e521eaf61 (diff)
downloadbird-35f88b305ab6a0e27b5ff1b445f63f544986e14e.tar.gz
Nest: Allow specifying security keys as hex bytes as well as strings
Add support for specifying a password in hexadecimal format, The result is the same whether a password is specified as a quoted string or a hex-encoded byte string, this just makes it more convenient to input high-entropy byte strings as MAC keys.
Diffstat (limited to 'lib')
-rw-r--r--lib/string.h1
-rw-r--r--lib/strtoul.c27
2 files changed, 28 insertions, 0 deletions
diff --git a/lib/string.h b/lib/string.h
index 0f650178..976b1c24 100644
--- a/lib/string.h
+++ b/lib/string.h
@@ -26,6 +26,7 @@ void buffer_puts(buffer *buf, const char *str);
u64 bstrtoul10(const char *str, char **end);
u64 bstrtoul16(const char *str, char **end);
+byte bstrtobyte16(const char *str);
int patmatch(const byte *pat, const byte *str);
diff --git a/lib/strtoul.c b/lib/strtoul.c
index 44a1bb1d..a5b11f68 100644
--- a/lib/strtoul.c
+++ b/lib/strtoul.c
@@ -59,3 +59,30 @@ bstrtoul16(const char *str, char **end)
errno = ERANGE;
return UINT64_MAX;
}
+
+byte
+bstrtobyte16(const char *str)
+{
+ byte out = 0;
+ for (int i=0; i<2; i++) {
+ switch (str[i]) {
+ case '0' ... '9':
+ out *= 16;
+ out += str[i] - '0';
+ break;
+ case 'a' ... 'f':
+ out *= 16;
+ out += str[i] + 10 - 'a';
+ break;
+ case 'A' ... 'F':
+ out *= 16;
+ out += str[i] + 10 - 'A';
+ break;
+ default:
+ errno = ERANGE;
+ return -1;
+ }
+ }
+
+ return out;
+}