commit bf808eff120238a6a3ef7cc243469143a7f8c40b
parent b7ba4766399045f736c536b1a9b17c26e181cfa8
Author: sin <sin@2f30.org>
Date: Tue, 21 May 2019 13:55:55 +0300
Store key in hex format
Diffstat:
M | key.c | | | 18 | ++++++++++++++++-- |
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/key.c b/key.c
@@ -25,12 +25,17 @@ keygen(unsigned char *key, size_t n)
int
writekey(int fd, unsigned char *key, size_t n)
{
+ unsigned char keystr[KEYSIZE * 2 + 1];
+
assert(KEYSIZE == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
if (n != KEYSIZE) {
seterr("invalid key size");
return -1;
}
- if (xwrite(fd, key, n) != n) {
+
+ sodium_bin2hex(keystr, sizeof(keystr), key, n);
+
+ if (xwrite(fd, keystr, sizeof(keystr) - 1) != sizeof(keystr) - 1) {
seterr("failed to write key");
return -1;
}
@@ -40,14 +45,23 @@ writekey(int fd, unsigned char *key, size_t n)
int
readkey(int fd, unsigned char *key, size_t n)
{
+ unsigned char keystr[KEYSIZE * 2 + 1];
+ size_t binlen;
+
assert(KEYSIZE == crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
if (n != KEYSIZE) {
seterr("invalid key size");
return -1;
}
- if (xread(fd, key, n) != n) {
+
+ if (xread(fd, keystr, sizeof(keystr) - 1) != sizeof(keystr) - 1) {
seterr("failed to read key");
return -1;
}
+ keystr[sizeof(keystr) - 1] = '\0';
+
+ sodium_hex2bin(key, n, keystr, sizeof(keystr), NULL, &binlen, NULL);
+ if (binlen != KEYSIZE)
+ return -1;
return 0;
}