From 11a9ef85f1608be039156cd66cc0562de9ef2d08 Mon Sep 17 00:00:00 2001 From: Seppia Date: Sun, 12 Jun 2016 21:28:21 +0200 Subject: fixed syntax and added some comments to onetimebluh.c --- onetimebluh.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/onetimebluh.c b/onetimebluh.c index 2aafbd6..b665fa3 100644 --- a/onetimebluh.c +++ b/onetimebluh.c @@ -6,11 +6,12 @@ #include #include -void xor(int ed, char* mess, char* keyf, char* outp); -void keyrand(int nb, char* outp); -void help(char* av[]); +void xor(int ed, char* mess, char* keyf, char* outp); /* operates the bitwise XOR between mess and keyf and puts the output to outp */ +void keyrand(int nb, char* outp); /* generates random numbers using RAND_bytes from openssl and puts them into outp */ +void help(char* av[]); /* prints the help message */ int main(int argc, char* argv[]) { + int opt = 1; int command = 0; int comm = 0; @@ -19,6 +20,9 @@ int main(int argc, char* argv[]) { char* output = NULL; int nbytes = -1; // must be resolved temporary workaround (ho sonno) + /* The following while cycle parses the argv vector to find commands, options and relative arguments + using the function getopt_long */ + while (opt) { int option_index = 0; static struct option options[] = { @@ -70,11 +74,14 @@ int main(int argc, char* argv[]) { } } + /* Next section performs some input checks */ + if (command == 0) { printf("No command called \n"); exit(EXIT_FAILURE); } else if (command > 1) { - printf("Multiple commands called (print usage)\n"); + printf("Multiple commands called \n"); + printf("%s [COMMAND] [OPTIONS] ... \n",argv[0]); exit(EXIT_FAILURE); } @@ -83,6 +90,8 @@ int main(int argc, char* argv[]) { exit(EXIT_FAILURE); } + /* Next section detects the functions to call */ + if (comm == 'e' || comm == 'd') { if (keyfile == NULL) { printf("No key specified: exit! \n"); @@ -101,12 +110,16 @@ void xor(int ed, char* mess, char* keyf, char* outp) { char* defenoutp = "critt"; char* defdeoutp = "decritt"; + /* Following if-else block check the existence of the files */ + if (access(mess, F_OK) == -1) { error(errno, errno, mess); } else if (access(keyf, F_OK) == -1) { error(errno, errno, keyf); } + /* In absence of input by users nex block sets the default values */ + if (outp == NULL) { if (ed == 'e') { printf("WARNING no output name specified using default value 'critt' \n"); @@ -116,6 +129,7 @@ void xor(int ed, char* mess, char* keyf, char* outp) { outp = defdeoutp; } } + FILE* mex = fopen(mess, "r"); FILE* keyx = fopen(keyf, "r"); FILE* critt = fopen(outp, "w"); @@ -147,6 +161,8 @@ void keyrand(int nb, char* outp) { 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"); nb = 256; @@ -161,12 +177,15 @@ void keyrand(int nb, char* outp) { printf("No byte number specified... using default value: 256 \n"); nb = 256; } + unsigned char key[nb]; RAND_bytes(key, nb); + if (outp == NULL) { outp = defoutp; printf("No output name specified... using default value: default.key \n"); } + FILE* file = fopen(outp, "w"); fwrite(key, nb, 1, file); fclose(file); -- cgit v1.2.3