#include #include #include #include #include #include #include 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; int tear = 0; char* message = NULL; char* keyfile = NULL; 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[] = { {"decrypt", required_argument, 0, 'd'}, {"encrypt", required_argument, 0, 'e'}, {"key-file", required_argument, 0, 'k'}, {"key-gen", no_argument, 0, 'g'}, {"help", no_argument, 0, 'h'}, {"nbytes", required_argument, 0, 'b'}, {"output", required_argument, 0, 'o'}, {"tear-page", no_argument, 0, 't'}, {0, 0, 0, 0}, }; if ((opt = getopt_long(argc, argv, "b:d:e:ghk:o:t", options, &option_index)) == -1) break; switch (opt) { case 'b': nbytes = atoi(argv[optind-1]); break; case 'd': message = argv[optind-1]; command++; comm = 'u'; break; case 'e': message = argv[optind-1]; if (access(message, F_OK) == -1) { /* checks the existence of the file and eventually exits */ error(errno, errno, message); } command++; comm = 'e'; break; case 'g': command++; comm = 'g'; break; case 'h': help(argv); command++; break; case 'k': keyfile = argv[optind-1]; if (access(keyfile, F_OK) == -1) { /* look at the comment before */ error(errno, errno, keyfile); } break; case 'o': output = argv[optind-1]; break; case 't': tear = 1; break; case '?': break; default: printf("carachter code returned 0%o \n", opt); } } /* 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 \n"); printf("%s [COMMAND] [OPTIONS] ... \n",argv[0]); exit(EXIT_FAILURE); } if (optind < argc) { printf("Too many arguments \n"); exit(EXIT_FAILURE); } /* Next section detects the functions to call */ if (comm == 'e' || comm == 'u') { if (keyfile == NULL) { printf("No key specified: exit! \n"); exit(EXIT_FAILURE); } if (tear == 1) { comm++; } xor(comm, message, keyfile, output); } else if (comm == 'g') { keyrand(nbytes, output); } exit(EXIT_SUCCESS); } void xor(int ed, char* mess, char* keyf, char* outp) { char* defenoutp = "critt"; char* defdeoutp = "decritt"; /* In absence of input by users nex block sets the default values */ if (outp == NULL) { if (ed == 'e' || ed == 'f') { printf("WARNING no output name specified using default value 'critt' \n"); outp = defenoutp; } else if (ed == 'u' || ed == 'v') { printf("WARNING no output name specified usign default value 'decritt' \n"); outp = defdeoutp; } } FILE* mex = fopen(mess, "r"); FILE* keyx = fopen(keyf, "r+"); FILE* critt = fopen(outp, "w"); long mess_size; long pad_size; if (ed == 'f' || ed == 'v') { fseek(mex, 0L, SEEK_END); mess_size = ftell(mex); rewind(mex); fseek(keyx, 0L, SEEK_END); pad_size = ftell(keyx); fseek(keyx, (pad_size - mess_size), SEEK_SET); } int i = 1; char a, b; while (i != EOF) { i = fscanf(mex, "%c", &a); fscanf(keyx, "%c", &b); if(i != EOF) { fprintf(critt, "%c", a^b); } } if (ed == 'f' || ed == 'v') { ftruncate(fileno(keyx), (pad_size - mess_size)); fseek(keyx, 0L, SEEK_END); long 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); } fclose(mex); fclose(keyx); fclose(critt); if (ed == 'e' || ed == 'f') { printf("Message successfully encrypted \n"); } else if (ed == 'u' || ed == 'v') { printf("Message successfully decrypted \n"); } return; } 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; outp = defoutp; } if (nb < -1) { // orribile printf("Negative byte value inserted! \n"); printf("Exiting... \n"); exit(EXIT_FAILURE); } else if (nb != 0) { if(nb == -1) { 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, sizeof(char), nb, file); fclose(file); printf("Created key file %s of %d bytes \n", outp, nb); } else { printf("Byte number specified is 0. \n"); printf("Doing nothing! \n"); } return; } void help(char* av[]) { fprintf(stdout,"ONETIMEBLUH USAGE: \n" "%s [COMMAND] [OPTIONS] \n \n" "COMMANDS: \n" "-d, --decrypt=FILE decrypt message (input) same ad --encrypt, just for the feeling \n" "-e, --encrypt=FILE encrypt message (input) \n" "-h, --help print this help \n" "-g, --key-gen create key file \n \n" "OPTIONS \n" "-k, --key-file=KEY_FILE use key (input) \n" "-b, --nbytes=NUM number of bytes \n" "-o, --output=FILE output name \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]); return; }