diff --git a/Makefile b/Makefile index 9d3cb0d..d048279 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ OUTPUT_DIR = ./bin OUTPUT = -o ${OUTPUT_DIR}/PROG build: output_dir - gcc -Wall ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} + gcc -Wall -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} debug: output_dir - gcc -Wall -g ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} + gcc -Wall -g -pthread ${LIBRARIES} ${SOURCES} ${OUTPUT:PROG=crc} output_dir: mkdir -p ${OUTPUT_DIR} diff --git a/cmd/crc.c b/cmd/crc.c index 0708625..147f183 100644 --- a/cmd/crc.c +++ b/cmd/crc.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "crc_util.h" #include "CRCLib.h" #include "crc.h" @@ -65,11 +66,118 @@ int update_file_crc(unsigned char *addr, unsigned long offset , unsigned int crc return 0; } +static void random_window_bit_change(unsigned char *data, int width, int rounds, int color_range, size_t length) { + size_t random_num = 0; + for (int i = 0; i < rounds; i++) { + random_num = randombytes_uniform(length); + do { + random_num = randombytes_uniform(length); + } while((random_num % ((width * color_range) + 1)) == 0); + + if(data[random_num] == 255) { + data[random_num]--; + } else { + data[random_num]++; + } + } + return; +} + +static int verify_crc_chunks(unsigned char *data, size_t data_length, int crc_depth, unsigned int max_data_length) { + unsigned char* testing_chunk; + unsigned int crc_check = 0; + unsigned int test_data_length = 0; + + for(int i = 0; i < crc_depth; i++) { + if((max_data_length * (i+1)) > data_length) { + test_data_length = (data_length - (max_data_length * i)); + testing_chunk = calloc(test_data_length+4, sizeof(unsigned char)); + } else { + test_data_length = max_data_length; + testing_chunk = calloc(test_data_length+4, sizeof(unsigned char)); + } + testing_chunk[0] = 0x49; + testing_chunk[1] = 0x44; + testing_chunk[2] = 0x41; + testing_chunk[3] = 0x54; + for(int j = 0; j < test_data_length; j++) { + testing_chunk[j+4] = data[(max_data_length*i)+j]; + } + crc_check = crc(testing_chunk, test_data_length); + + if(i == 2) { + printf("THIRD: %08X\n", crc_check); + } else if(i == 1) { + printf("SECOND: %08X\n", crc_check); + } + if ((crc_check >> (8*3)) != 10 ) { + free(testing_chunk); + return 1; + } + } + free(testing_chunk); + printf("FOUND: %08X\n", crc_check); + return 0; +} + +static int crc_embed_data(unsigned char *data, unsigned int data_length, int bit_width, int color_range, unsigned int sliding_window) { + unsigned char *check_data; + size_t check_data_length = 0; + + random_window_bit_change(data, bit_width, 3, color_range, sliding_window); + + // Compressing data for test + zlib_compress_data(data, data_length, &check_data, &check_data_length); + + int match_crc = verify_crc_chunks(check_data, check_data_length, 3, 8192); + + if(match_crc == 0) { + free(check_data); + return 0; + } + free(check_data); + return 1; +} + +void *random_data_change_thread_call(void *w) { + struct EMBED_THREAD_STRUCT *data = w; + unsigned char* temp_color_data = calloc(data->uncom_data_len, sizeof(unsigned char)); + int searching = 1; + do { + // Creating temporary data set + memcpy(temp_color_data, data->data, data->uncom_data_len); + + if(data->uncom_data_len > 800000) { + data->data_len = 150000; + } else { + data->data_len = data->uncom_data_len; + } + if(data->uncom_data_len > 16000) { + data->win_size = 9000; + } else { + data->win_size = data->uncom_data_len; + } + + searching = crc_embed_data(temp_color_data, data->data_len, data->width, data->color_range, data->win_size); + + if (searching == 0) { + pthread_mutex_lock(data->mutex_lock); + //pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + *data->searching = searching; + memcpy(data->data, temp_color_data, data->uncom_data_len); + //pthread_cancel(data->thread_id); + pthread_mutex_unlock(data->mutex_lock); + } + + } while(*data->searching == 1); + free(temp_color_data); + return NULL; +} + void random_data_change(unsigned char *color_data, unsigned char *width, size_t length) { int searching = 1; - size_t rounds = 0; - unsigned char* full_data; - int data_array_size = 0; + unsigned int sliding_window = 0; + unsigned int compress_data_length = 0; // Needs to be turned into a variable int color_range = 3; // Union for width type cast @@ -77,60 +185,27 @@ void random_data_change(unsigned char *color_data, unsigned char *width, size_t uint32_t width_int; unsigned char width_array[4]; }w; - // Temp data array for crc testing - unsigned char* temp_color_data = calloc(length, sizeof(unsigned char)); - memcpy(w.width_array, width, 4); - do { - rounds++; - // Creating temporary data set - memcpy(temp_color_data, color_data, length); - // Generating random byte to change - size_t random_num = randombytes_uniform(100000); - //size_t random_num = randombytes_uniform(length); - // Checking for index break - if(random_num % ((be32toh(w.width_int) * color_range) + 1)) { - if(color_data[random_num] == 255) { - temp_color_data[random_num]--; - } else { - temp_color_data[random_num]++; - } - // Compressing data for test - unsigned char *check_data_buff = NULL; - size_t check_data_length = 0; - zlib_compress_data(temp_color_data, 50000, &check_data_buff, &check_data_length); - //zlib_compress_data(temp_color_data, length, &check_data_buff, &check_data_length); + // Temp data array for crc testing + //unsigned char* temp_color_data = calloc(length, sizeof(unsigned char)); - if(check_data_length > 8192) { - data_array_size = 8192; - full_data = calloc(8196, sizeof(unsigned char)); - } else { - data_array_size = check_data_length; - full_data = calloc(check_data_length+4, sizeof(unsigned char)); - } + //int num_threads = 1; + struct EMBED_THREAD_STRUCT t_data; + t_data.searching = &searching; + t_data.data = color_data; + t_data.data_len = compress_data_length; + t_data.uncom_data_len = length; + t_data.width = be32toh(w.width_int); + t_data.color_range = color_range; + t_data.win_size = sliding_window; + pthread_t tid; + t_data.thread_id = tid; + for(int i = 0; i < 28; i++) { + pthread_create(&tid, NULL, random_data_change_thread_call, &t_data); + } + pthread_join(tid, NULL); - full_data[0] = 0x49; - full_data[1] = 0x44; - full_data[2] = 0x41; - full_data[3] = 0x54; - for(int i = 0; i < data_array_size; i++) { - full_data[i+4] = check_data_buff[i]; - } - unsigned int temp_crc = crc(full_data, data_array_size); - printf("%08X\n", temp_crc); - if ((temp_crc >> (8*3)) == 10 ) { - printf("RAND Key: %zu\n", random_num); - printf("Found in %zu rounds!\n", rounds); - memcpy(color_data, temp_color_data, length); - searching = 0; - } - free(check_data_buff); - } - - } while(searching == 1); - - free(temp_color_data); return; } @@ -200,7 +275,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c exit(EXIT_FAILURE); } - size_t size = 1; + size_t size = 8; size_t idat_byte_length = 0; unsigned char* idat_data = calloc(size, sizeof(unsigned char)); for(size_t i = 0; i < total_idat(addr); i++) { @@ -229,7 +304,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c // Start data testing random_data_change(uncom_data_buff, png_file->png_start_data.file_width, uncom_data_size); - return 0; + //return 0; // Compress Data unsigned char *com_data_buff; size_t com_data_size = 0; @@ -244,7 +319,7 @@ int change_idat_content(unsigned char *addr, struct PNG_FILE_STRUCT *png_file, c } // Build PNG File - //build_png_file(png_file, out_file_name); + build_png_file(png_file, out_file_name); // Freeing used memory free(uncom_data_buff); diff --git a/include/crc_util.h b/include/crc_util.h index 695146b..49189f0 100644 --- a/include/crc_util.h +++ b/include/crc_util.h @@ -33,6 +33,18 @@ struct PNG_FILE_STRUCT { struct PNG_IDAT_FILE_STRUCT png_idat_data; }; +struct EMBED_THREAD_STRUCT { + pthread_mutex_t *mutex_lock; + pthread_t thread_id; + int *searching; + unsigned char *data; + unsigned int data_len; + size_t uncom_data_len; + int width; + int color_range; + unsigned int win_size; +}; + extern const long png_signature[8]; unsigned long check_header_length(unsigned char *addr, long offset);