Merge remote-tracking branch 'origin/master'

# Conflicts:
#	createRainbow.c
#	decrypt.c
#	readBulk.c
#	readBulk.h
master
leonnicolas 4 years ago
commit e7c0ad2e0d

@ -6,32 +6,128 @@
#include "cryptwrapper.h" #include "cryptwrapper.h"
#include "readBulk.h" #include "readBulk.h"
char bulk_buf[BULKSIZE][MAXPWSIZE]; #include <argp.h>
struct s_rainbowvalue256 bulk_buf_out[BULKSIZE];
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[]) { static struct argp_option options[] = {
if (argc != 2){ {"verbose", 'v', 0, 0, "Produce verbose output" },
printf("one argument expected ,argc =%i\n",argc); {"quiet", 'q', 0, 0, "Don't produce any output" },
return 1; {"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 <name of input file>.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; FILE *fptr;
fptr = fopen(argv[1],"r"); fptr = fopen(arguments.input_file,"r");
if(fptr == NULL) if(fptr == NULL)
{ {
printf("Could not open file %s\n", argv[1]); printf("Could not open file %s\n", arguments.input_file);
return 1; return 1;
} }
FILE *tfptr; FILE *tfptr;
char str[strlen(argv[1])+7]; if (arguments.use_output_file){
strcpy(str,argv[1]); tfptr = fopen(arguments.output_file,"wb");
strcat(str,".sha256"); if(tfptr == NULL)
tfptr = fopen(str,"w"); {
if(tfptr == NULL) printf("Could not create file %s\n", arguments.output_file);
{ return 1;
printf("Could not create file %s\n", str); }
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(); mycryptwrapper_init();
char line[256]; char line[256];
@ -45,21 +141,19 @@ int main(int argc, char const *argv[]) {
for (size_t i = 0 ; i < entries;++i){ for (size_t i = 0 ; i < entries;++i){
struct s_rainbowvalue256 r; 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 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]); strcpy(r.pw , 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; 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 else if(arguments.base64){
for (size_t i = 0 ; i < entries;++i){
free(bulk_buf_out[i].pw); }
else {
fwrite(bulk_buf_out, sizeof(struct s_rainbowvalue256),entries,tfptr);
} }
} }
fclose(tfptr); fclose(tfptr);
fclose(fptr); fclose(fptr);

@ -12,20 +12,23 @@
#include "readBulk.h" #include "readBulk.h"
int main(int argc, char const *argv[]) { int main(int argc, char const *argv[]) {
if (argc < 3) { if (argc < 3){
printf("please select rainbow table and encrypted file\n"); printf("please select rainbow table and encrypted file\n");
return 1; return 1;
} }
FILE *fptr_rainbow = fopen(argv[1], "rb"); FILE * fptr_rainbow = fopen(argv[1],"rb");
FILE *fptr_encry = fopen(argv[2], "rb"); FILE * fptr_encry = fopen(argv[2],"rb");
if (fptr_encry == NULL) { if (fptr_encry==NULL){
printf("could not open encrypted file %s\n", argv[1]); printf("could not open encrypted file %s\n",argv[1]);
return 1; return 1;
} }
if (fptr_rainbow == NULL) { if (fptr_rainbow==NULL){
printf("could not open rainbow table file %s\n", argv[2]); printf("could not open rainbow table file %s\n",argv[2]);
return 1; return 1;
} }
@ -33,25 +36,24 @@ int main(int argc, char const *argv[]) {
// to calculate hash without trailing 256 bits of decrypted file // to calculate hash without trailing 256 bits of decrypted file
size_t mens = gcry_md_get_algo_dlen(algo); size_t mens = gcry_md_get_algo_dlen(algo);
void *digest = malloc(mens); void * digest=malloc(mens);
// initialization vector for decryption and encryption // initialization vector for decryption and encryption
size_t len = 8; size_t len = 8;
size_t file_len; size_t file_len;
if (fseek(fptr_encry, 0, SEEK_END)) { if (fseek(fptr_encry,0,SEEK_END)){
perror("could not ssek end of file\n"); perror("could not ssek end of file\n"); return 1;
return 1;
} }
file_len = ftell(fptr_encry); file_len = ftell(fptr_encry);
if (file_len == 0) { if (file_len==0){
perror("file length is 0\n"); perror("file length is 0\n");
return 1; return 1;
} }
rewind(fptr_encry); // go to beginning of file rewind(fptr_encry); // go to beginning of file
uint8_t *buf = malloc(file_len); uint8_t * buf = malloc(file_len);
if (fread(buf, sizeof(uint8_t), file_len, fptr_encry) != file_len) { if (fread(buf, sizeof(uint8_t),file_len,fptr_encry)!= file_len){
perror("could not read complete file\n"); perror("could not read complete file\n");
return 1; 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; size_t num_rainbow_values;
// read a block of rainbow values
clock_t old_clock; clock_t old_clock;
// read a block of rainbow values
int success = 0; int success = 0;
while ((num_rainbow_values = fread(rs, sizeof(struct s_rainbowvalue256), BULKSIZE, fptr_rainbow)) != 0) { while ((num_rainbow_values=fread(rs, sizeof(struct s_rainbowvalue256),BULKSIZE,fptr_rainbow ))!=0){
if (success == 1) { break; } if (success==1){break;}
printf("read %d rainbow values\n", (int) num_rainbow_values); //printf("read %d rainbow values\n", (int) num_rainbow_values);
// iterate through rainbow values and decrypt // iterate through rainbow values and decrypt
old_clock=clock(); old_clock=clock();
#pragma omp parallel for #pragma omp parallel for
for (size_t i = 0; i < num_rainbow_values; i++) { for (size_t i = 0 ; i < num_rainbow_values ; i++){
uint8_t *decrypted_buf = malloc(file_len);//allocate mem for decrypted buffer if (success==1){
#pragma omp exitregion
}
uint8_t * decrypted_buf = malloc(file_len);//allocate mem for decrypted buffer
gcry_cipher_hd_t dhd; 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_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"); }; if (gcry_cipher_setkey(dhd,rs[i].hash,8)){perror("could not set key\n");};
void *iv = malloc(len); void * iv = malloc(len);
memset(iv, 0, len); memset(iv,0,len);
if (gcry_cipher_setiv(dhd, iv, len)) { perror("could not set init vector\n"); } 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_decrypt(dhd,decrypted_buf,file_len,buf,file_len)){perror("could not decrypt\n");}
//mycryptwrapper_print(decrypted_buf,file_len); //mycryptwrapper_print(decrypted_buf,file_len);
//printf("pw: %s\nfile:%s\n",rs[i].pw,decrypted_buf); //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); 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]); strcpy(enc_fname, argv[2]);
strcat(enc_fname, ".decr"); strcat(enc_fname,".decr");
FILE *encrypted_fptr = fopen(enc_fname, "wb"); FILE * encrypted_fptr = fopen(enc_fname,"wb");
if (fwrite(decrypted_buf, 1, file_len - 32, encrypted_fptr) != file_len - 32) { if (fwrite(decrypted_buf,1,file_len-32,encrypted_fptr)!=file_len-32){
perror("couln not write all data to decrypted file"); perror("couln not write all data to decrypted file");
//return 1; //return 1;
#pragma omp exitregion #pragma omp exitregion
@ -99,66 +102,19 @@ int main(int argc, char const *argv[]) {
printf("successfully saved decrypted data in %s\n", enc_fname); printf("successfully saved decrypted data in %s\n", enc_fname);
//return 0; //return 0;
success = 1; success=1;
#pragma omp exitregion #pragma omp exitregion
} }
free(iv); free(iv);
free(decrypted_buf); free(decrypted_buf);
gcry_cipher_close(dhd); gcry_cipher_close(dhd);
if (success == 1) { }// end parallel
#pragma omp exitregion
}
}// end for parallel
float sec = (float)((clock()-old_clock))/(float)CLOCKS_PER_SEC; 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); printf("\rcalc/sec: %4.0f", num_rainbow_values/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");
}
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"); printf("\nnothing found\n");
} }
//printdata(digest,mens);
return 0; return 0;
} }

@ -1,9 +1,9 @@
#include "readBulk.h" #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; size_t i;
for (i = 0; i < BULKSIZE; ++i) { for (i = 0; i < BULKSIZE; ++i) {
if (!fgets(bulk[i], MAXPWSIZE, fptr)){ if (!fgets(bulk[i], MAX_PW_LEN, fptr)){
break; break;
} }
} }
@ -12,14 +12,6 @@ size_t getBulk(FILE * fptr, char bulk[BULKSIZE][MAXPWSIZE]){
size_t getHashBulk(FILE *fptr, struct s_rainbowvalue256 rs[BULKSIZE]){ size_t getHashBulk(FILE *fptr, struct s_rainbowvalue256 rs[BULKSIZE]){
return fread(rs, sizeof(struct s_rainbowvalue256),BULKSIZE,fptr ); return fread(rs, sizeof(struct s_rainbowvalue256),BULKSIZE,fptr );
} }
size_t writeHashHumanReadable(FILE *fptr, struct s_rainbowvalue256 rs[BULKSIZE]){
size_t write_rainbow_bulk(FILE *fptr ,struct s_rainbowvalue256 rs[BULKSIZE], size_t len ){ return 0;
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;
} }

@ -1,14 +1,14 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <omp.h>
#include "rainbowvalue.h" #include "rainbowvalue.h"
#ifndef readBulk #ifndef readBulk
#define readBulk #define readBulk
#define BULKSIZE 20000 #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 * 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 * 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 * @bulk allocated memory fot bulk
* @return * @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 * reads bulk of s_rainbowvalues and stores them in rs
* @param fptr * @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]); 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 fptr
* @param rs * @param rs
* @return * @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 #endif
Loading…
Cancel
Save