From de888fd88d3c10880be0c46aabd8ca97ccc330cf Mon Sep 17 00:00:00 2001 From: leonnicolas Date: Fri, 6 Mar 2020 19:06:18 +0100 Subject: [PATCH] rainbow values are saved in csv, but its very slow to convert sha to HEX --- Makefile | 6 ++-- createRainbow.c | 36 +++++++++---------- decrypt.c | 92 +++++++++++++++++++++++++++---------------------- rainbowvalue.h | 6 ++-- readBulk.c | 15 ++++++-- readBulk.h | 14 ++++++-- 6 files changed, 97 insertions(+), 72 deletions(-) diff --git a/Makefile b/Makefile index 38d2660..d281ad6 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ all: brutef rainbow encrypt brutef: - gcc -fopenmp -std=c11 -o brutefd decrypt.c crypwrapper.c readBulk.c `libgcrypt-config --cflags --libs` + gcc -fopenmp -O -std=c11 -o brutefd decrypt.c crypwrapper.c readBulk.c `libgcrypt-config --cflags --libs` rainbow: - gcc -fopenmp -std=c11 -o rainb createRainbow.c crypwrapper.c readBulk.c `libgcrypt-config --cflags --libs` + gcc -fopenmp -std=c11 -o rainb createRainbow.c crypwrapper.c readBulk.c `libgcrypt-config --cflags --libs` encrypt: - gcc -std=c11 -o encryptf encrypt.c crypwrapper.c `libgcrypt-config --cflags --libs` \ No newline at end of file + gcc -std=c11 -O -o encryptf encrypt.c crypwrapper.c `libgcrypt-config --cflags --libs` \ No newline at end of file diff --git a/createRainbow.c b/createRainbow.c index 747cbc0..5592bfd 100644 --- a/createRainbow.c +++ b/createRainbow.c @@ -6,8 +6,10 @@ #include "cryptwrapper.h" #include "readBulk.h" -char bulk_buf[BULKSIZE][MAXMEM]; +char bulk_buf[BULKSIZE][MAXPWSIZE]; struct s_rainbowvalue256 bulk_buf_out[BULKSIZE]; + + int main(int argc, char const *argv[]) { if (argc != 2){ printf("one argument expected ,argc =%i\n",argc); @@ -24,7 +26,7 @@ int main(int argc, char const *argv[]) { char str[strlen(argv[1])+7]; strcpy(str,argv[1]); strcat(str,".sha256"); - tfptr = fopen(str,"wb"); + tfptr = fopen(str,"w"); if(tfptr == NULL) { printf("Could not create file %s\n", str); @@ -37,34 +39,28 @@ int main(int argc, char const *argv[]) { size_t mens = gcry_md_get_algo_dlen(algo); void * digest=malloc(mens); - //printdata(buf,5); - //printf("\n"); size_t entries; while ((entries = getBulk(fptr,bulk_buf))){ #pragma omp parallel for for (size_t i = 0 ; i < entries;++i){ - //printf("thread: %i\n",omp_get_thread_num()); 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 - //mycryptwrapper_print(digest, strlen(digest)); - //printf("%s", line); - strcpy(r.pw , bulk_buf[i]); - //strcpy(r.hash,digest); + 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'; bulk_buf_out[i]= r; } - fwrite(bulk_buf_out, sizeof(struct s_rainbowvalue256),entries,tfptr); - } + //fwrite(bulk_buf_out, sizeof(struct s_rainbowvalue256),entries,tfptr); - /*while (fgets(line, sizeof(line), fptr)) { - - - fwrite(&r, sizeof(struct s_rainbowvalue256),1,tfptr); + 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); + } } -*/ - - - - fclose(tfptr); fclose(fptr); return 0; diff --git a/decrypt.c b/decrypt.c index ebdd583..7467fdc 100644 --- a/decrypt.c +++ b/decrypt.c @@ -5,30 +5,27 @@ #include #include #include - +#include #include #include "cryptwrapper.h" #include "rainbowvalue.h" #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; } @@ -36,24 +33,25 @@ 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; } @@ -62,35 +60,38 @@ 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; int success = 0; - while ((num_rainbow_values=fread(rs, sizeof(struct s_rainbowvalue256),BULKSIZE,fptr_rainbow ))!=0){ - if (success==1){break;} + 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 - #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 + 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 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 @@ -98,17 +99,26 @@ int main(int argc, char const *argv[]) { printf("successfully saved decrypted data in %s\n", enc_fname); //return 0; - success=1; + success = 1; #pragma omp exitregion - } + } free(iv); free(decrypted_buf); gcry_cipher_close(dhd); + if (success == 1) { + #pragma omp exitregion + } + }// end for 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; } - if (success==1){ -#pragma omp exitregion - } - } + + + }// 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); @@ -141,7 +151,7 @@ int main(int argc, char const *argv[]) { gcry_cipher_close(dhd);//close cipher } */ - if(success==0){ + if (success == 0) { printf("\nnothing found\n"); } diff --git a/rainbowvalue.h b/rainbowvalue.h index 496f53f..088057e 100644 --- a/rainbowvalue.h +++ b/rainbowvalue.h @@ -1,10 +1,10 @@ - +#include #ifndef rainbowvalue #define rainbowvalue #define MAX_PW_LEN 64 struct s_rainbowvalue256 { - char pw[MAX_PW_LEN]; - char hash[32]; // 32 * 8bit = 256 bit + char * pw; + uint8_t hash[32]; // 32 * 8bit = 256 bit }; #endif \ No newline at end of file diff --git a/readBulk.c b/readBulk.c index 5e5b679..6758abe 100644 --- a/readBulk.c +++ b/readBulk.c @@ -1,9 +1,9 @@ #include "readBulk.h" -size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXMEM]){ +size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXPWSIZE]){ size_t i; for (i = 0; i < BULKSIZE; ++i) { - if (!fgets(bulk[i], MAXMEM, fptr)){ + if (!fgets(bulk[i], MAXPWSIZE, fptr)){ break; } } @@ -11,4 +11,15 @@ size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXMEM]){ } 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