From e33838a9a2746f12f68fd31c02f6af9c6e70dbb8 Mon Sep 17 00:00:00 2001 From: Seppia Date: Tue, 3 Jul 2018 20:58:33 +0200 Subject: Quiet option addition This commit adds a 'quiet' option which suppresses all info-only outputs, usefult for scripting in prevision of future sdtin/stdout interaction support. All error messages will ignore the 'quite' option, and so the help function will. --- src/onetimebluh.c | 84 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 59 insertions(+), 25 deletions(-) diff --git a/src/onetimebluh.c b/src/onetimebluh.c index 4ceee04..50eaeae 100644 --- a/src/onetimebluh.c +++ b/src/onetimebluh.c @@ -7,9 +7,9 @@ #include #include -void xor (int64_t ed, char* mess, char* keyf, char* outp); /* operates the bitwise XOR between mess and keyf and puts the output to outp */ -void keyrand (int64_t nb, char* outp); /* generates random numbers using RAND_bytes from openssl and puts them into outp */ -void bluh (int64_t c, char* mess, char* outp, char* ch); /* performs the binary dump of the input file and prints that to outp */ +void xor (int64_t ed, char* mess, char* keyf, char* outp, int64_t quiet); /* operates the bitwise XOR between mess and keyf and puts the output to outp */ +void keyrand (int64_t nb, char* outp, int64_t quiet); /* generates random numbers using RAND_bytes from openssl and puts them into outp */ +void bluh (int64_t c, char* mess, char* outp, char* ch, int64_t quiet); /* performs the binary dump of the input file and prints that to outp */ void help (char* av[]); /* prints the help message */ int main (int argc, char* argv[]) { @@ -18,6 +18,7 @@ int main (int argc, char* argv[]) { int64_t command = 0; int64_t comm = 0; int64_t tear = 0; + int64_t quiet = 0; char* chars = "01"; char* message = NULL; char* keyfile = NULL; @@ -40,12 +41,13 @@ int main (int argc, char* argv[]) { {"help", no_argument, 0, 'h'}, {"nbytes", required_argument, 0, 'n'}, {"output", required_argument, 0, 'o'}, + {"quiet", no_argument, 0, 'q'}, {"tear-page", no_argument, 0, 't'}, {"uhbluh", required_argument, 0, 'u'}, {0, 0, 0, 0}, }; - if ((opt = getopt_long (argc, argv, "b:c:d:e:ghk:n:o:tu:", options, &option_index)) == -1) + if ((opt = getopt_long (argc, argv, "b:c:d:e:ghk:n:o:qtu:", options, &option_index)) == -1) break; switch (opt) { @@ -109,6 +111,9 @@ int main (int argc, char* argv[]) { case 'o': output = argv[optind-1]; break; + case 'q': + quiet = 1; + break; case 't': tear = 1; break; @@ -153,17 +158,17 @@ int main (int argc, char* argv[]) { if (tear == 1) { comm++; } - xor (comm, message, keyfile, output); + xor (comm, message, keyfile, output, quiet); } else if (comm == 'g') { - keyrand (nbytes, output); + keyrand (nbytes, output, quiet); } else if (comm == 'b' || comm == 'u') { - bluh (comm, message, output, chars); + bluh (comm, message, output, chars, quiet); } exit (EXIT_SUCCESS); } -void xor (int64_t ed, char* mess, char* keyf, char* outp) { +void xor (int64_t ed, char* mess, char* keyf, char* outp, int64_t quiet) { char* defenoutp = "critt"; char* defdeoutp = "decritt"; @@ -172,10 +177,14 @@ void xor (int64_t ed, char* mess, char* keyf, char* outp) { if (outp == NULL) { if (ed == 'e' || ed == 'f') { - printf ("WARNING no output name specified using default value 'critt' \n"); + if (!quiet) { + printf ("WARNING no output name specified using default value 'critt' \n"); + } outp = defenoutp; } else if (ed == 'r' || ed == 's') { - printf ("WARNING no output name specified usign default value 'decritt' \n"); + if (!quiet) { + printf ("WARNING no output name specified usign default value 'decritt' \n"); + } outp = defdeoutp; } } @@ -211,8 +220,10 @@ void xor (int64_t ed, char* mess, char* keyf, char* outp) { ftruncate (fileno (keyx), (pad_size - mess_size)); fseek (keyx, 0L, SEEK_END); int64_t new_pad_size = ftell (keyx); - printf ("Your pad is now %li bytes shorter \n", mess_size); - printf ("You now have %li bytes left \n", new_pad_size); + if (!quiet) { + printf ("Your pad is now %li bytes shorter \n", mess_size); + printf ("You now have %li bytes left \n", new_pad_size); + } } free (bytes); @@ -222,22 +233,28 @@ void xor (int64_t ed, char* mess, char* keyf, char* outp) { fclose (critt); if (ed == 'e' || ed == 'f') { - printf ("Message successfully encrypted \n"); + if (!quiet) { + printf ("Message successfully encrypted \n"); + } } else if (ed == 'r' || ed == 's') { - printf ("Message successfully decrypted \n"); + if (!quiet) { + printf ("Message successfully decrypted \n"); + } } return; } -void keyrand (int64_t nb, char* outp) { +void keyrand (int64_t nb, char* outp, int64_t quiet) { char* defoutp = "default.key"; /* Next block controls the inputs and eventually sets the default values */ if((nb == -1) && (outp == NULL)) { - printf ("WARNING no option specified usign default values... \n"); + if (!quiet) { + printf ("WARNING no option specified usign default values... \n"); + } nb = 1048576; outp = defoutp; } @@ -247,16 +264,22 @@ void keyrand (int64_t nb, char* outp) { exit (EXIT_FAILURE); } else if (nb != 0) { if(nb == -1) { - printf ("No byte number specified... using default value: 1MB \n"); + if (!quiet) { + printf ("No byte number specified... using default value: 1MB \n"); + } nb = 1048576; } if (outp == NULL) { outp = defoutp; - printf ("No output name specified... using default value: default.key \n"); + if (!quiet) { + printf ("No output name specified... using default value: default.key \n"); + } } - printf ("Generating pad...\n"); + if (!quiet) { + printf ("Generating pad...\n"); + } unsigned char* key = malloc (nb); RAND_bytes (key, nb); @@ -264,7 +287,9 @@ void keyrand (int64_t nb, char* outp) { fwrite (key, sizeof(char), nb, file); fclose (file); free (key); - printf ("Created key file %s of %ld bytes \n", outp, nb); + if (!quiet) { + printf ("Created key file %s of %ld bytes \n", outp, nb); + } } else { printf ("Byte number specified is 0. \n"); printf ("Doing nothing! \n"); @@ -273,7 +298,7 @@ void keyrand (int64_t nb, char* outp) { return; } -void bluh (int64_t c, char* mess, char* outp, char* ch) { +void bluh (int64_t c, char* mess, char* outp, char* ch, int64_t quiet) { char* defbluh = "bluhed"; char* defunbluh = "unbluhed"; @@ -284,10 +309,14 @@ void bluh (int64_t c, char* mess, char* outp, char* ch) { if (outp == NULL) { if (c == 'b') { - printf ("WARNING no output name specified using default value 'bluhed' \n"); + if (!quiet) { + printf ("WARNING no output name specified using default value 'bluhed' \n"); + } outp = defbluh; } else if (c == 'u') { - printf ("WARNING no output name specified usign default value 'unbluhed' \n"); + if (!quiet) { + printf ("WARNING no output name specified usign default value 'unbluhed' \n"); + } outp = defunbluh; } } @@ -319,7 +348,9 @@ void bluh (int64_t c, char* mess, char* outp, char* ch) { free (bytes); free (bits); - printf ("Message successfully bluhed!\n"); + if (!quiet) { + printf ("Message successfully bluhed!\n"); + } } else if (c == 'u') { char* comp = malloc ((len / 8) + 1); @@ -342,7 +373,9 @@ void bluh (int64_t c, char* mess, char* outp, char* ch) { free (comp); free (bits); - printf ("Message successfully unbluhed!\n"); + if (!quiet) { + printf ("Message successfully unbluhed!\n"); + } } fclose (mex); @@ -366,6 +399,7 @@ void help (char* av[]) { " -k, --key-file=KEY_FILE use key (input)\n" " -n, --nbytes=NUM{K,M,G,T} number of bytes, you can specify K for KiB, M for MiB, etc... \n" " -o, --output=FILE output name\n" + " -q, --quiet suppresses all messages, except from error releated ones\n" " -t, --tear-page deletes from the pad file the bytes used to encrypt/decrypt\n\n" "Onetimebluh project repository at https://git.eigenlab.org/seppia/onetimebluh\n", av[0]); -- cgit v1.2.3