diff options
author | Mattias Andrée <maandree@kth.se> | 2023-06-23 19:15:49 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2023-06-23 19:15:49 +0200 |
commit | 7281ae7db4d9bc9eb2b59134e1dea1231013f1af (patch) | |
tree | 14d15e56dea896c71b0b4576a9633ea759080d58 /key2root-crypt.c | |
parent | Fix exit status (diff) | |
download | key2root-7281ae7db4d9bc9eb2b59134e1dea1231013f1af.tar.gz key2root-7281ae7db4d9bc9eb2b59134e1dea1231013f1af.tar.bz2 key2root-7281ae7db4d9bc9eb2b59134e1dea1231013f1af.tar.xz |
Use libar2simplified instead of crypt as crypt has a verily limited imput size, and add -h to key2root-addkey, and add key2root-crypt
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'key2root-crypt.c')
-rw-r--r-- | key2root-crypt.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/key2root-crypt.c b/key2root-crypt.c new file mode 100644 index 0000000..961e00c --- /dev/null +++ b/key2root-crypt.c @@ -0,0 +1,78 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "arg.h" +#include "crypt.h" + + +char *argv0; + + +static void +usage(void) +{ + fprintf(stderr, "usage: %s [crypt-parameters]\n", argv0); + exit(1); +} + + +int +main(int argc, char *argv[]) +{ + const char *parameters; + char *key = NULL, *new; + size_t key_len = 0; + size_t key_size = 0; + char *hash; + ssize_t r; + + ARGBEGIN { + default: + usage(); + } ARGEND; + + if (argc > 1) + usage(); + + parameters = argv[0]; + + for (;;) { + if (key_len == key_size) { + new = malloc(1 + (key_size += 1024)); + if (!new) { + explicit_bzero(key, key_len); + fprintf(stderr, "%s: read <stdin>: %s\n", argv0, strerror(errno)); + exit(1); + } + memcpy(new, key, key_len); + explicit_bzero(key, key_len); + free(key); + key = new; + } + r = read(STDIN_FILENO, &key[key_len], key_size - key_len); + if (r <= 0) { + if (!r) + break; + explicit_bzero(key, key_len); + fprintf(stderr, "%s: read <stdin>: %s\n", argv0, strerror(errno)); + exit(1); + } + key_len += (size_t)r; + } + hash = key2root_crypt(key, key_len, parameters, 1); + if (!hash) + exit(1); + free(key); + printf("%s\n", hash); + free(hash); + + if (fflush(stdout) || ferror(stdout) || fclose(stdout)) { + fprintf(stderr, "%s: printf: %s\n", argv0, strerror(errno)); + exit(1); + } + return 0; +} |