From fba801355bd8c4d7d57dadfa29362809a5a87b7f Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Wed, 18 Nov 2020 19:49:49 -0500 Subject: implement decryption adjust command-line to require no file names or exactly two, input and output remove -G and -R options specify device with -d option specify key (in hex) if necessary with -k option better warnings and errors throughout --- deonebook.c | 64 +++++++++++++++++++------------------------------------------ 1 file changed, 20 insertions(+), 44 deletions(-) (limited to 'deonebook.c') diff --git a/deonebook.c b/deonebook.c index f943396..12a67a5 100644 --- a/deonebook.c +++ b/deonebook.c @@ -7,21 +7,18 @@ int main(int argc, char *argv[]) { - enum { DECRYPT, GENERATE, READ } mode = DECRYPT; + char *keystring = NULL; + char *device = NULL; int c; - while ((c = getopt(argc, argv, "RGk:")) != -1) { + while ((c = getopt(argc, argv, "k:d:")) != -1) { switch (c) { - case 'R': - mode = READ; + case 'k': + keystring = optarg; break; - case 'G': - mode = GENERATE; - break; - - case 'k': - /* keyfile = optarg; */ + case 'd': + device = optarg; break; default: @@ -29,50 +26,29 @@ int main(int argc, char *argv[]) } } - if (mode != DECRYPT) { - unsigned char *key = NULL; - if (mode == READ) { - key = readkey("./.shm"); - } else { - char *device = "mmcblk0"; - if (optind < argc) { - device = argv[optind]; - } - - if (strstr(device, "mmcblk") != device) { - fprintf(stderr, "device must be an mmcblk device\n"); - return 1; - } + unsigned char *key = getkey(keystring, device); - char path[256]; - snprintf(path, sizeof(path), "/sys/block/%s/device", device); - DIR *d = opendir(path); - if (d == NULL) { - fprintf(stderr, "device '%s' not found\n", device); - return 1; - } - - key = genkey(dirfd(d)); - } - - if (key == NULL) { - return 1; - } + if (key == NULL) { + return 1; + } + if (optind >= argc) { for (int i = 0; i < KEY_SIZE; i++) { printf("%02hhx", key[i]); } printf("\n"); - return 0; } - if (optind >= argc) { - fprintf(stderr, "%s: missing operands\n", argv[0]); + if (optind < argc - 2) { + fprintf(stderr, "too many operands\n"); + return 1; + } + + if (optind != argc - 2) { + fprintf(stderr, "missing output file\n"); return 1; } - do { - printf("decrypting %s\n", argv[optind]); - } while (++optind < argc); + return decrypt(key, argv[optind], argv[optind + 1]); } -- cgit v1.2.1