diff --git a/createRainbow.c b/createRainbow.c index 5592bfd..fc5745b 100644 --- a/createRainbow.c +++ b/createRainbow.c @@ -6,32 +6,128 @@ #include "cryptwrapper.h" #include "readBulk.h" -char bulk_buf[BULKSIZE][MAXPWSIZE]; -struct s_rainbowvalue256 bulk_buf_out[BULKSIZE]; +#include +const char *argp_program_version = + "create Rainbow 0.1"; +/* Program documentation. */ +static char doc[] = + "I wonder for what this is"; -int main(int argc, char const *argv[]) { - if (argc != 2){ - printf("one argument expected ,argc =%i\n",argc); - return 1; +static struct argp_option options[] = { + {"verbose", 'v', 0, 0, "Produce verbose output" }, + {"quiet", 'q', 0, 0, "Don't produce any output" }, + {"base64", 'b', 0, 0, "Saves data in base 64" }, + {"humanreadable", 'H', 0, 0, "Output is human readable (slower)" }, + {"output", 'o', "FILE", 0, "Output to FILE instead of .sha256" }, + { 0 } +}; +struct arguments +{ + char *input_file; /* arg1 & arg2 */ + int silent, verbose, humanreadable, base64; + int use_output_file, use_input_file; + char *output_file; +}; +/* Parse a single option. */ +static error_t +parse_opt (int key, char *arg, struct argp_state *state) +{ + /* Get the input argument from argp_parse, which we + know is a pointer to our arguments structure. */ + struct arguments *arguments = state->input; + + switch (key) + { + case 'q': case 's': + arguments->silent = 1; + break; + case 'v': + arguments->verbose = 1; + break; + case 'H': + arguments->humanreadable = 1; + break; + case 'b': + arguments->base64 = 1; + break; + case 'o': + arguments->output_file = arg; + arguments->use_output_file = 1; + break; + case ARGP_KEY_ARG: + if (state->arg_num >= 1) + /* Too many arguments. */ + argp_usage (state); + + arguments->input_file = arg; + + break; + + case ARGP_KEY_END: + if (state->arg_num < 1) + /* Not enough arguments. */ + argp_usage (state); + break; + + + default: + return ARGP_ERR_UNKNOWN; } + return 0; +} + +/* Our argp parser. */ +static struct argp argp = { options, parse_opt, 0 , doc }; +//parsing args set up end--------------------------------------------------------------- + +char bulk_buf[BULKSIZE][MAX_PW_LEN]; +struct s_rainbowvalue256 bulk_buf_out[BULKSIZE]; + +int main(int argc, char **argv) { + //parsing args---------------------------------------------------------------- + struct arguments arguments; + + /* Default values. */ + arguments.silent = 0; + arguments.verbose = 0; + arguments.humanreadable = 0; + arguments.base64 = 0; + arguments.output_file = "-"; + arguments.input_file = "-"; + + argp_parse (&argp, argc, argv, 0, 0, &arguments); + //parsing args end--------------------------------------------------------------- + FILE *fptr; - fptr = fopen(argv[1],"r"); + fptr = fopen(arguments.input_file,"r"); if(fptr == NULL) { - printf("Could not open file %s\n", argv[1]); + printf("Could not open file %s\n", arguments.input_file); return 1; } FILE *tfptr; - char str[strlen(argv[1])+7]; - strcpy(str,argv[1]); - strcat(str,".sha256"); - tfptr = fopen(str,"w"); - if(tfptr == NULL) - { - printf("Could not create file %s\n", str); - return 1; + if (arguments.use_output_file){ + tfptr = fopen(arguments.output_file,"wb"); + if(tfptr == NULL) + { + printf("Could not create file %s\n", arguments.output_file); + return 1; + } + } + else { + char str[strlen(argv[1])+7]; + strcpy(str,argv[1]); + strcat(str,".sha256"); + tfptr = fopen(str,"wb"); + if(tfptr == NULL) + { + printf("Could not create file %s\n", str); + return 1; + } } + + mycryptwrapper_init(); char line[256]; @@ -45,21 +141,19 @@ int main(int argc, char const *argv[]) { for (size_t i = 0 ; i < entries;++i){ struct s_rainbowvalue256 r; gcry_md_hash_buffer(algo,r.hash,bulk_buf[i],strlen(bulk_buf[i])-1);// -1 so trailing \n is not used for calculation - int pw_len= strlen(bulk_buf[i]); - r.pw = malloc(pw_len); - strncpy(r.pw ,bulk_buf[i],pw_len-1 ); - r.pw[pw_len-1]='\0'; + strcpy(r.pw , bulk_buf[i]); bulk_buf_out[i]= r; } - //fwrite(bulk_buf_out, sizeof(struct s_rainbowvalue256),entries,tfptr); + if (arguments.humanreadable){ - if (entries!=write_rainbow_bulk(tfptr,bulk_buf_out,entries)){ - perror("could not write all data\n"); } - //free memory - for (size_t i = 0 ; i < entries;++i){ - free(bulk_buf_out[i].pw); + else if(arguments.base64){ + + } + else { + fwrite(bulk_buf_out, sizeof(struct s_rainbowvalue256),entries,tfptr); } + } fclose(tfptr); fclose(fptr); diff --git a/decrypt.c b/decrypt.c index 7467fdc..562be2e 100644 --- a/decrypt.c +++ b/decrypt.c @@ -12,20 +12,23 @@ #include "readBulk.h" + + + int main(int argc, char const *argv[]) { - if (argc < 3) { + if (argc < 3){ printf("please select rainbow table and encrypted file\n"); return 1; } - FILE *fptr_rainbow = fopen(argv[1], "rb"); - FILE *fptr_encry = fopen(argv[2], "rb"); + FILE * fptr_rainbow = fopen(argv[1],"rb"); + FILE * fptr_encry = fopen(argv[2],"rb"); - if (fptr_encry == NULL) { - printf("could not open encrypted file %s\n", argv[1]); + if (fptr_encry==NULL){ + printf("could not open encrypted file %s\n",argv[1]); return 1; } - if (fptr_rainbow == NULL) { - printf("could not open rainbow table file %s\n", argv[2]); + if (fptr_rainbow==NULL){ + printf("could not open rainbow table file %s\n",argv[2]); return 1; } @@ -33,25 +36,24 @@ int main(int argc, char const *argv[]) { // to calculate hash without trailing 256 bits of decrypted file size_t mens = gcry_md_get_algo_dlen(algo); - void *digest = malloc(mens); + void * digest=malloc(mens); // initialization vector for decryption and encryption size_t len = 8; size_t file_len; - if (fseek(fptr_encry, 0, SEEK_END)) { - perror("could not ssek end of file\n"); - return 1; + if (fseek(fptr_encry,0,SEEK_END)){ + perror("could not ssek end of file\n"); return 1; } file_len = ftell(fptr_encry); - if (file_len == 0) { + if (file_len==0){ perror("file length is 0\n"); return 1; } rewind(fptr_encry); // go to beginning of file - uint8_t *buf = malloc(file_len); - if (fread(buf, sizeof(uint8_t), file_len, fptr_encry) != file_len) { + uint8_t * buf = malloc(file_len); + if (fread(buf, sizeof(uint8_t),file_len,fptr_encry)!= file_len){ perror("could not read complete file\n"); return 1; } @@ -60,38 +62,39 @@ int main(int argc, char const *argv[]) { - struct s_rainbowvalue256 *rs = malloc(sizeof(struct s_rainbowvalue256) * BULKSIZE); + struct s_rainbowvalue256 * rs = malloc(sizeof(struct s_rainbowvalue256)*BULKSIZE); size_t num_rainbow_values; - // read a block of rainbow values - clock_t old_clock; + // read a block of rainbow values int success = 0; - while ((num_rainbow_values = fread(rs, sizeof(struct s_rainbowvalue256), BULKSIZE, fptr_rainbow)) != 0) { - if (success == 1) { break; } - printf("read %d rainbow values\n", (int) num_rainbow_values); + while ((num_rainbow_values=fread(rs, sizeof(struct s_rainbowvalue256),BULKSIZE,fptr_rainbow ))!=0){ + if (success==1){break;} + //printf("read %d rainbow values\n", (int) num_rainbow_values); // iterate through rainbow values and decrypt - old_clock=clock(); - #pragma omp parallel for - for (size_t i = 0; i < num_rainbow_values; i++) { - uint8_t *decrypted_buf = malloc(file_len);//allocate mem for decrypted buffer + #pragma omp parallel for + for (size_t i = 0 ; i < num_rainbow_values ; i++){ + if (success==1){ + #pragma omp exitregion + } + uint8_t * decrypted_buf = malloc(file_len);//allocate mem for decrypted buffer gcry_cipher_hd_t dhd; - if (gcry_cipher_open(&dhd, cipher, GCRY_CIPHER_MODE_CFB, 0)) { perror("could not open cypher\n"); } - if (gcry_cipher_setkey(dhd, rs[i].hash, 8)) { perror("could not set key\n"); }; - void *iv = malloc(len); - memset(iv, 0, len); - if (gcry_cipher_setiv(dhd, iv, len)) { perror("could not set init vector\n"); } - if (gcry_cipher_decrypt(dhd, decrypted_buf, file_len, buf, file_len)) { perror("could not decrypt\n"); } + if (gcry_cipher_open(&dhd,cipher,GCRY_CIPHER_MODE_CFB,0)){perror("could not open cypher\n");} + if (gcry_cipher_setkey(dhd,rs[i].hash,8)){perror("could not set key\n");}; + void * iv = malloc(len); + memset(iv,0,len); + if (gcry_cipher_setiv(dhd, iv , len)){perror("could not set init vector\n");} + if (gcry_cipher_decrypt(dhd,decrypted_buf,file_len,buf,file_len)){perror("could not decrypt\n");} //mycryptwrapper_print(decrypted_buf,file_len); //printf("pw: %s\nfile:%s\n",rs[i].pw,decrypted_buf); - if (check_sha256_tag(decrypted_buf, file_len)) { + if (check_sha256_tag(decrypted_buf,file_len)){ printf("pw: %s\n", rs[i].pw); - char *enc_fname = malloc(strlen(argv[2]) + 5); + char * enc_fname = malloc(strlen(argv[2])+5); strcpy(enc_fname, argv[2]); - strcat(enc_fname, ".decr"); - FILE *encrypted_fptr = fopen(enc_fname, "wb"); - if (fwrite(decrypted_buf, 1, file_len - 32, encrypted_fptr) != file_len - 32) { + strcat(enc_fname,".decr"); + FILE * encrypted_fptr = fopen(enc_fname,"wb"); + if (fwrite(decrypted_buf,1,file_len-32,encrypted_fptr)!=file_len-32){ perror("couln not write all data to decrypted file"); //return 1; #pragma omp exitregion @@ -99,66 +102,19 @@ int main(int argc, char const *argv[]) { printf("successfully saved decrypted data in %s\n", enc_fname); //return 0; - success = 1; - #pragma omp exitregion - } + success=1; + #pragma omp exitregion + } free(iv); free(decrypted_buf); gcry_cipher_close(dhd); - if (success == 1) { - #pragma omp exitregion - } - }// end for parallel - + }// end parallel float sec = (float)((clock()-old_clock))/(float)CLOCKS_PER_SEC; - printf("Clocks per sec: %ld\ncalc/sec: %f\n",CLOCKS_PER_SEC, (float)BULKSIZE/sec); - if (success == 1) { - return 0; - } - - - }// end while - - /*while (fread(&r, sizeof(struct s_rainbowvalue256), 1,fptr)) {//reading hash values from rainbowtable - gcry_cipher_hd_t dhd; - gcry_error_t err= gcry_cipher_open(&dhd,cipher,GCRY_CIPHER_MODE_CFB,0); - if (err){ - printf("could not open handle\n"); - } - err = gcry_cipher_setkey(dhd,r.hash,8); - if (err) { - printf("could not set key \n"); - } - memset(iv,0,len); - err = gcry_cipher_setiv(dhd, iv , len); - if (err){ - printf("could not init init vector"); - } - - - memset(out,0,256); - err = gcry_cipher_decrypt(dhd,out,256,encrypted_secret,strlen(encrypted_secret)); - if (err){ - printf("could not decrypt\n"); - } + printf("\rcalc/sec: %4.0f", num_rainbow_values/sec); - if (strcmp(out,dummydata)==0){ - printf("pw: %sfor data: %s\npwhash: ", r.pw, (char*)out); - mycryptwrapper_print(r.hash, strlen(r.hash)); - gcry_cipher_close(dhd);//close cipher - return 0; - } - gcry_cipher_close(dhd);//close cipher } -*/ - if (success == 0) { + if(success==0){ printf("\nnothing found\n"); - } - - //printdata(digest,mens); - - - return 0; } \ No newline at end of file diff --git a/readBulk.c b/readBulk.c index 6758abe..76d94fe 100644 --- a/readBulk.c +++ b/readBulk.c @@ -1,9 +1,9 @@ #include "readBulk.h" -size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXPWSIZE]){ +size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAX_PW_LEN]){ size_t i; for (i = 0; i < BULKSIZE; ++i) { - if (!fgets(bulk[i], MAXPWSIZE, fptr)){ + if (!fgets(bulk[i], MAX_PW_LEN, fptr)){ break; } } @@ -12,14 +12,6 @@ size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXPWSIZE]){ size_t getHashBulk(FILE *fptr, struct s_rainbowvalue256 rs[BULKSIZE]){ return fread(rs, sizeof(struct s_rainbowvalue256),BULKSIZE,fptr ); } - -size_t write_rainbow_bulk(FILE *fptr ,struct s_rainbowvalue256 rs[BULKSIZE], size_t len ){ - for (size_t i = 0 ; i #include #include - +#include #include "rainbowvalue.h" #ifndef readBulk #define readBulk #define BULKSIZE 20000 -#define MAXPWSIZE 256 // Max PW size + /** * reads bulk from file, file pointer is used as handle so if you read several bulk, dont use file pointer between * if BULKSIZE or EOF is reached, 2000 or number of lines read is returned. Memory must be allocated by caller @@ -16,7 +16,7 @@ * @bulk allocated memory fot bulk * @return */ -size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXPWSIZE]); +size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAX_PW_LEN]); /** * reads bulk of s_rainbowvalues and stores them in rs * @param fptr @@ -25,11 +25,18 @@ size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXPWSIZE]); */ size_t getHashBulk(FILE *fptr, struct s_rainbowvalue256 rs[BULKSIZE]); /** - * + * writes to tsv file + * @param fptr file pointer to which values are written + * @param rs buffer of values to be written + * @return values written + */ +size_t writeHashHumanReadable(FILE *fptr, struct s_rainbowvalue256 rs[BULKSIZE]); +/** + * writes hash to tsv in base 64 * @param fptr * @param rs * @return */ -size_t write_rainbow_bulk(FILE *fptr ,struct s_rainbowvalue256 rs[BULKSIZE] , size_t len); +size_t writeHashBase64(FILE *fptr, struct s_rainbowvalue256 rs[BULKSIZE]); #endif \ No newline at end of file