rainbow values are saved in csv, but its very slow to convert sha to HEX

master
leonnicolas 4 years ago
parent b3e917b369
commit de888fd88d

@ -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`
gcc -std=c11 -O -o encryptf encrypt.c crypwrapper.c `libgcrypt-config --cflags --libs`

@ -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;

@ -5,30 +5,27 @@
#include <stdint.h>
#include <strings.h>
#include <omp.h>
#include <time.h>
#include <inttypes.h>
#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");
}

@ -1,10 +1,10 @@
#include <inttypes.h>
#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

@ -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<len ; i++){
fprintf(fptr,"%s,",rs[i].pw);
for (int j = 0; j<32 ; j++){
fprintf(fptr,"%02x",rs[i].hash[j]);
}
fprintf(fptr,"\n");
}
return len;
}

@ -7,8 +7,8 @@
#ifndef readBulk
#define readBulk
#define BULKSIZE 2000
#define MAXMEM 256
#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][MAXMEM]);
size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXPWSIZE]);
/**
* reads bulk of s_rainbowvalues and stores them in rs
* @param fptr
@ -24,4 +24,12 @@ size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXMEM]);
* @return values read
*/
size_t getHashBulk(FILE *fptr, struct s_rainbowvalue256 rs[BULKSIZE]);
/**
*
* @param fptr
* @param rs
* @return
*/
size_t write_rainbow_bulk(FILE *fptr ,struct s_rainbowvalue256 rs[BULKSIZE] , size_t len);
#endif
Loading…
Cancel
Save