#include #include #include #include #include #include #include #include typedef struct opts { int64_t comm; int64_t nbytes; int64_t quiet; int64_t tear; char* chars; char* keyfile; char* input; char* output; } Opts; void xor (Opts* opzioni); /* operates the bitwise XOR between mess and keyf and puts the output to outp */ void keyrand (Opts* opzioni); /* generates random numbers using RAND_bytes from openssl and puts them into outp */ void bluh (Opts* opzioni); /* 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[]) { uint64_t opt = 1; int64_t command = 0; char* sip = NULL; Opts opzioni; opzioni.comm = 0; opzioni.nbytes = -1; // must be resolved temporary workaround (ho sonno) opzioni.quiet = 0; opzioni.tear = 0; opzioni.chars = "01"; opzioni.keyfile = NULL; opzioni.input = NULL; opzioni.output = NULL; /* 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[] = { {"bluh", required_argument, 0, 'b'}, {"char", required_argument, 0, 'c'}, {"decrypt", required_argument, 0, 'd'}, {"encrypt", required_argument, 0, 'e'}, {"key-file", required_argument, 0, 'k'}, {"gen-key", no_argument, 0, 'g'}, {"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:qtu:", options, &option_index)) == -1) break; switch (opt) { case 'b': opzioni.input = argv[optind-1]; if (access (opzioni.input, F_OK) == -1) { /* look at next comment */ error (errno, errno, opzioni.input); } command ++; opzioni.comm = 'b'; break; case 'c': if (strlen (argv[optind - 1]) != 2){ printf ("Too much or too many characters inserted!"); exit (EXIT_FAILURE); } opzioni.chars = argv[optind - 1]; break; case 'd': opzioni.input = argv[optind-1]; command ++; opzioni.comm = 'd'; break; case 'e': opzioni.input = argv[optind-1]; if (access (opzioni.input, F_OK) == -1) { /* checks the existence of the file and eventually exits */ error (errno, errno, opzioni.input); } command ++; opzioni.comm = 'e'; break; case 'g': command++; opzioni.comm = 'g'; break; case 'h': help (argv); command ++; break; case 'k': opzioni.keyfile = argv[optind-1]; if (access (opzioni.keyfile, F_OK) == -1) { /* look at the previous comment */ error (errno, errno, opzioni.keyfile); } break; case 'n': opzioni.nbytes = strtol (argv[optind-1], &sip, 10); if (sip[0] == 'K') { opzioni.nbytes = opzioni.nbytes*1024; } else if (sip[0] == 'M') { opzioni.nbytes = opzioni.nbytes*1024*1024; } else if (sip[0] == 'G') { opzioni.nbytes = opzioni.nbytes*1024*1024*1024; } else if (sip[0] == 'T') { opzioni.nbytes = opzioni.nbytes*1024*1024*1024*1024; } else if (sip[0] != '\0') { printf ("You must specify the size in KiB, MiB, GiB or TiB, respectively using K, M, G or T, or nothing for single bytes."); exit (EXIT_FAILURE); } break; case 'o': opzioni.output = argv[optind-1]; break; case 'q': opzioni.quiet = 1; break; case 't': opzioni.tear = 1; break; case 'u': opzioni.input = argv[optind-1]; if (access (opzioni.input, F_OK) == -1) { /* just guess */ error (errno, errno, opzioni.input); } command ++; opzioni.comm = 'u'; break; case '?': break; default: printf ("carachter code returned 0%lo \n", opt); } } /* Next section performs some input checks */ if (!command) { printf ("%s: No command called\n", argv[0]); 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 ("%s: Too many arguments\n", argv[0]); exit (EXIT_FAILURE); } /* Next section detects the functions to call */ if (opzioni.comm == 'e' || opzioni.comm == 'd') { if (opzioni.keyfile == NULL) { printf ("%s: No key specified: exit!\n", argv[0]); exit (EXIT_FAILURE); } xor (&opzioni); } else if (opzioni.comm == 'g') { keyrand (&opzioni); } else if (opzioni.comm == 'b' || opzioni.comm == 'u') { bluh (&opzioni); } exit (EXIT_SUCCESS); } void xor (Opts* opzioni) { char* defenout = "critt"; char* defdeout = "decritt"; /* In absence of input by users next block sets the default values */ if (opzioni->output == NULL) { if (opzioni->comm == 'e') { if (!opzioni->quiet) { printf ("WARNING no output name specified using default value 'critt' \n"); } opzioni->output = defenout; } else if (opzioni->comm == 'd') { if (!opzioni->quiet) { printf ("WARNING no output name specified usign default value 'decritt' \n"); } opzioni->output = defdeout; } } FILE* mex = fopen (opzioni->input, "r"); FILE* keyx = fopen (opzioni->keyfile, "r+"); FILE* critt = fopen (opzioni->output, "w"); int64_t mess_size; int64_t pad_size; 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); char* bytes = malloc (mess_size); int64_t i; char a, b; for (i = 0; i < mess_size; i ++) { fscanf (mex, "%c", &a); fscanf (keyx, "%c", &b); bytes[i] = a ^ b; } fwrite (bytes, sizeof(char), mess_size, critt); if (opzioni->tear) { ftruncate (fileno (keyx), (pad_size - mess_size)); fseek (keyx, 0L, SEEK_END); int64_t new_pad_size = ftell (keyx); if (!opzioni->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); fclose (mex); fclose (keyx); fclose (critt); if (opzioni->comm == 'e') { if (!opzioni->quiet) { printf ("Message successfully encrypted \n"); } } else if (opzioni->comm == 'd') { if (!opzioni->quiet) { printf ("Message successfully decrypted \n"); } } return; } void keyrand (Opts* opzioni) { char* defout = "default.key"; /* Next block controls the inputs and eventually sets the default values */ if((opzioni->nbytes == -1) && (opzioni->output == NULL)) { if (!opzioni->quiet) { printf ("WARNING no option specified usign default values... \n"); } opzioni->nbytes = 1048576; opzioni->output = defout; } if (opzioni->nbytes < -1) { // orribile printf ("Negative byte value inserted! \n"); printf ("Exiting... \n"); exit (EXIT_FAILURE); } else if (opzioni->nbytes) { if(opzioni->nbytes == -1) { if (!opzioni->quiet) { printf ("No byte number specified... using default value: 1MB \n"); } opzioni->nbytes = 1048576; } if (opzioni->output == NULL) { opzioni->output = defout; if (!opzioni->quiet) { printf ("No output name specified... using default value: default.key \n"); } } if (!opzioni->quiet) { printf ("Generating pad...\n"); } unsigned char* key = malloc (opzioni->nbytes); RAND_bytes (key, opzioni->nbytes); FILE* file = fopen (opzioni->output, "w"); fwrite (key, sizeof(char), opzioni->nbytes, file); fclose (file); free (key); if (!opzioni->quiet) { printf ("Created key file %s of %ld bytes \n", opzioni->output, opzioni->nbytes); } } else { printf ("Byte number specified is 0. \n"); printf ("Doing nothing! \n"); } return; } void bluh (Opts* opzioni) { char* defbluh = "bluhed"; char* defunbluh = "unbluhed"; char a = opzioni->chars[0]; char b = opzioni->chars[1]; /* In absence of input by users next block sets the default values */ if (opzioni->output == NULL) { if (opzioni->comm == 'b') { if (!opzioni->quiet) { printf ("WARNING no output name specified using default value 'bluhed' \n"); } opzioni->output = defbluh; } else if (opzioni->comm == 'u') { if (!opzioni->quiet) { printf ("WARNING no output name specified usign default value 'unbluhed' \n"); } opzioni->output = defunbluh; } } FILE* mex = fopen (opzioni->input, "r"); FILE* bluh = fopen (opzioni->output, "w"); int64_t len; fseek (mex, 0L, SEEK_END); len = ftell (mex); rewind (mex); if (opzioni->comm == 'b') { char* bytes = malloc (len); char* bits = malloc (8 * len); fread (bytes, sizeof(char), len, mex); int64_t i,j; for (i = 0; i < len; i ++) { for (j = 0; j < 8; j ++) { bits[8 * i + j] = (bytes[i] & (1 << (7 -j))) ? b : a; } } fwrite (bits, sizeof(char), (8 * len), bluh); free (bytes); free (bits); if (!opzioni->quiet) { printf ("Message successfully bluhed!\n"); } } else if (opzioni->comm == 'u') { char* comp = malloc ((len / 8) + 1); char* bits = malloc (len); fread (bits, sizeof(char), len, mex); memset (comp, 0, ((len / 8) + 1)); int64_t i, j; for (i = 0; i < (len / 8); i ++) { for (j = 0; j < 8; j ++) { if (bits[8 * i + j] == b) { comp[i] |= 1 << (7 - j); } } } fwrite (comp, sizeof(char), (len / 8), bluh); free (comp); free (bits); if (!opzioni->quiet) { printf ("Message successfully unbluhed!\n"); } } fclose (mex); fclose (bluh); return; } void help (char* av[]) { fprintf (stdout,"Onetimebluh usage:\n" "%s [COMMAND] [OPTIONS]\n\n" "Commands:\n\n" " -b, --bluh=FILE bluhes your message or in other words performs a binary dump\n" " -d, --decrypt=FILE decrypt message (input) same ad --encrypt, just for the feeling\n" " -e, --encrypt=FILE encrypt message (input)\n" " -g, --gen-key create key file\n" " -h, --help print this help\n" " -u, --unbluh=FILE unbluhes your message (gets the orginal message from the binary dump)\n\n" "Options:\n\n" " -c, --char=CAHRS reads two cahracters as argument and uses them to bluh your message\n" " -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]); return; }