diff --git a/crc.c b/crc.c index 58df9c2..9f49530 100644 --- a/crc.c +++ b/crc.c @@ -1,12 +1,16 @@ #include #include #include +#include +#include #include "CRCLib.h" -int png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; -//int idat_signature[4] = { 0x, 0x, 0x, 0x} +const int png_signature[8] = { 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a }; +const int idat_signature = 1229209940; +const int iend_signature = 1229278788; +const int working = 1; -int check_header(int *addr) { +int check_file_header(int *addr) { int signature_match = 0; for( int i = 0; i < 8; i++ ) { if (addr[i] != png_signature[i]) { @@ -38,9 +42,7 @@ int first_idat(int *addr) { while(idat_found == 0) { jump_offset = check_header_length(addr, offset); header_type = check_header_length(addr, offset+4); - printf("Jump: %d\n", jump_offset); - printf("Type: %d\n", header_type); - if(header_type == 1229209940) { + if(header_type == idat_signature) { idat_found = 1; } else { offset = offset + jump_offset + 12; @@ -49,23 +51,94 @@ int first_idat(int *addr) { return offset; } -void main() { +int total_idat(int *addr) { + int iend_found = 0; + int found_idat = 0; + int offset = 8; + int jump_offset = 0; + int header_type = 0; + while(iend_found == 0) { + jump_offset = check_header_length(addr, offset); + header_type = check_header_length(addr, offset+4); + if(header_type == iend_signature) { + iend_found = 1; + } else { + if(header_type == idat_signature) { + found_idat++; + } + offset = offset + jump_offset + 12; + } + } + return found_idat; +} + +int change_idat_content(int *addr, char *message, int accuracy, int offset) { + printf("Starting IDAT Tranform"); + if(accuracy > 4) { + printf("Warning, accuracy cannot be larger than 4"); + return EXIT_FAILURE; + } + if(accuracy > 2) { + printf("Notice, this could take a long time..."); + } + if(total_idat(addr) < strlen(message)) { + printf("Warning, message exceeds IDAT amount"); + return EXIT_FAILURE; + } + + // Comment + int i = total_idat(addr); + printf("Total IDAT %d\n", i); + + int idat_length = check_header_length(addr, offset); + int prop_found = 0; + int size = 1; + int* idat_data = malloc(size * sizeof(int)); + while(prop_found == 0) { + for(i = 0; i < idat_length; i++) { + idat_data[i] = addr[i+offset+8]; + size++; + int* new_idat_data = realloc(idat_data, size * sizeof(int)); + } + int r = randombytes_uniform(5) + 1; + int j = randombytes_uniform(idat_length); + + idat_data[j] = (idat_data[j] + r) % 255; + unsigned char crc_check[size+4]; + int idat_header[] = { 0x49, 0x44, 0x41, 0x54 }; + for(i = 0; i < 4; i++) { + crc_check[i] = idat_header[i]; + } + for(i = 0; i < size; i++) { + crc_check[i] = idat_data[i+4]; + } + int crcnum = crc(crc_check, idat_length); + printf("New CRC: %08X\n", crcnum); + printf("Test: %X\n", crcnum >> (8*3)); + prop_found = 1; + } + + return 0; +} + +int main() { FILE *fp; int c; int myArray[255] = {}; int i = 0; int offset = 0; + char message[1]; + if(sodium_init() == -1) { + return EXIT_FAILURE; + } fp = fopen("./1.png", "rt"); while((c = fgetc(fp)) != EOF) { myArray[i] = c; i++; } fclose(fp); - //check_header(myArray); - //check_header_length(myArray, 54); offset = first_idat(myArray); - printf("First Chunk: %d\n", offset); - //printf("%d\n", CheckPNG(myArray)); + change_idat_content(myArray, message, 2, offset); //int crcnum = crc(myArray, 19); //printf("%08X\n", crcnum); }