From 5a7cc1ddc0454c057121448587a8c76f647546d9 Mon Sep 17 00:00:00 2001 From: None Date: Wed, 20 May 2015 09:00:51 +0200 Subject: [PATCH] S1T6 not finished --- Makefile | 3 +- MatsanoCrypto.sublime-workspace | 319 ++++++++++++-------------------- etc/set1_t6.txt | 64 +++++++ utils/hamming.c | 63 +++++++ utils/hamming.h | 2 + utils/hex_to_base64.cpp | 77 +++++++- utils/hex_to_base64.h | 7 + utils/utils_tester.cpp | 59 +++++- 8 files changed, 379 insertions(+), 215 deletions(-) create mode 100644 etc/set1_t6.txt create mode 100644 utils/hamming.c create mode 100644 utils/hamming.h diff --git a/Makefile b/Makefile index 828e778..fa47997 100644 --- a/Makefile +++ b/Makefile @@ -4,5 +4,6 @@ all: g++ -g -I${INC} -c utils/utils_tester.cpp -o obj/utils_tester.o g++ -g -I${INC} -c utils/xor.c -o obj/xor.o g++ -g -I${INC} -c set1/xor_char_finder.cpp -o obj/xor_char_finder.o - g++ -g -lpthread -o utils_tester obj/hex_to_base64.o obj/utils_tester.o obj/xor_char_finder.o obj/xor.o + g++ -g -I${INC} -c utils/hamming.c -o obj/hamming.o + g++ -g -lpthread -o utils_tester obj/hex_to_base64.o obj/utils_tester.o obj/xor_char_finder.o obj/xor.o obj/hamming.o diff --git a/MatsanoCrypto.sublime-workspace b/MatsanoCrypto.sublime-workspace index 2f8b43c..5cb6dbe 100644 --- a/MatsanoCrypto.sublime-workspace +++ b/MatsanoCrypto.sublime-workspace @@ -3,6 +3,26 @@ { "selected_items": [ + [ + "min", + "min_dist" + ], + [ + "tmp_di", + "tmp_dist1" + ], + [ + "tmp_", + "tmp_dist2" + ], + [ + "block", + "block_size" + ], + [ + "calc", + "calculate_nb_of_bits_set" + ], [ "stri", "string_hex" @@ -494,113 +514,83 @@ [ "getCcn", "getCcnConnectionSubscribers" - ], - [ - "Ou", - "OutputMap_t" - ], - [ - "aVisi", - "aVisitorRange" - ], - [ - "aVis", - "aVisitor._map" - ], - [ - "subscr", - "subscribe" - ], - [ - "sub", - "subscribers" ] ] }, "buffers": [ { - "file": "utils/hex_to_base64.h", + "file": "utils/hamming.c", "settings": { - "buffer_size": 414, + "buffer_size": 1448, "line_ending": "Unix" } }, { - "file": "utils/hex_to_base64.cpp", - "settings": - { - "buffer_size": 5988, - "line_ending": "Unix" - } - }, - { - "file": "set1/xor_char_finder.cpp", - "settings": - { - "buffer_size": 4610, - "line_ending": "Unix" - } - }, - { - "file": "set1/xor_char_finder.h", + "contents": "#include \"hex_to_base64.h\"\n#include \"set1/xor_char_finder.h\"\n#include \"hamming.h\"\n#include \n#include \n#include \n#include \n#include \"xor.h\"\n\nvoid hex_to_base64_text() {\n const char test_buff1[] =\"49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d\"; \n const char expected_buff1[] =\"SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t\";\n\n const char test_buff2[] =\"4927\"; \n const char expected_buff2[] =\"SSH\";\n\n const char test_buff3[] =\"49\"; \n const char expected_buff3[] =\"SB\";\n\n const char test_buff4[] =\"33\"; \n const char expected_buff4[] =\"z\";\n\n char output[256];\n char* po=output;\n\n memset (output,'\\0', 256);\n hex_to_base64(test_buff1, po, strlen(test_buff1) );\n assert( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 );\n assert( strlen(expected_buff1) == strlen(output));\n \n // test case when there are 2 hex'es\n memset (output,'\\0', 256);\n hex_to_base64(test_buff2, po, strlen(test_buff2) );\n assert( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 );\n assert( strlen(expected_buff2) == strlen(output));\n\n // test case when there is 1 hex\n memset (output,'\\0', 256);\n hex_to_base64(test_buff3, po, strlen(test_buff3) );\n assert( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 );\n assert( strlen(expected_buff3) == strlen(output));\n\n // test case when there is 1 char\n memset (output,'\\0', 256);\n hex_to_base64(test_buff4, po, strlen(test_buff4) );\n assert( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 );\n assert( strlen(expected_buff4) == strlen(output));\n}\n\nvoid convert_string_to_hex_test()\n{\n const char test_buff1[] = \"49276d\";\n unsigned char out_buff[3]; \n unsigned char* p = out_buff;\n convert_string_to_hex(test_buff1, strlen(test_buff1), p);\n assert( p[0] == 0x49\n && p[1] == 0x27\n && p[2] == 0x6d);\n\n // and back\n char out_buf[6];\n char* op = out_buf;\n convert_hex_to_string(p, 3, op);\n assert( memcmp(op, test_buff1, 6) == 0 );\n\n}\n\nvoid xor_strings_test()\n{ \n const char i_buf_1[] = \"1c0111001f010100061a024b53535009181c\";\n const char* p_ibuf1=i_buf_1;\n\n const char i_buf_2[] = \"686974207468652062756c6c277320657965\";\n const char* p_ibuf2=i_buf_2;\n\n char out_buf[36];\n char* p_out_buf=out_buf;\n\n unsigned char buf[36/2];\n unsigned char* p_buf = buf;\n\n xor_strings(p_ibuf1, p_ibuf2, p_buf);\n\n convert_hex_to_string(buf, 36/2, p_out_buf);\n assert( memcmp(out_buf, \"746865206b696420646f6e277420706c6179\", 36) == 0);\n}\n\nvoid set1_challenge3_test()\n{\n static const char ciphertext[] = \"1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736\"; \n struct frequency_t max_score;\n xor_char_finder(ciphertext, max_score);\n assert(max_score.letter == 'X');\n}\n\nvoid set2_challange4_test()\n{\n FILE* fp;\n char* line=NULL;\n size_t len=0;\n int line_nb = 0;\n unsigned char hexdump[30];\n unsigned char* p_hexdump = hexdump;\n\n fp=fopen(\"etc/set1_t4_input.txt\", \"r\");\n if(fp==NULL) \n {\n fprintf(stderr, \"etc/set1_t4_input.txt not found\");\n return;\n }\n \n while( getline(&line, &len, fp) != -1 )\n {\n ++line_nb;\n // printf(\"%s\",line);\n struct frequency_t max_score;\n max_score.letter=0x0;\n max_score.score = 0;\n xor_char_finder(line, max_score);\n if(max_score.letter != 0x00)\n {\n // printf(\"%d -> %c\\n\", line_nb, max_score.letter);\n\n convert_string_to_hex(line, 60, p_hexdump);\n for(int i=0; i<30;++i)\n { \n // printf(\"%c\", hexdump[i]^max_score.letter);\n }\n // printf(\"\\n\");\n }\n free(line); line=0; len=0;\n\n }\n free(line);\n}\n\nvoid set2_challenge_5_test()\n{\n static const char ch1[] = \"Burning 'em, if you ain't quick and nimble\\nI go crazy when I hear a cymbal\";\n static const char xor_val[] = \"ICE\";\n static const unsigned char expected[] = {\n 0x0b,0x36,0x37,0x27,0x2a,0x2b,0x2e,0x63,0x62,0x2c,0x2e,0x69,0x69,0x2a,0x23,0x69,\n 0x3a,0x2a,0x3c,0x63,0x24,0x20,0x2d,0x62,0x3d,0x63,0x34,0x3c,0x2a,0x26,0x22,0x63,\n 0x24,0x27,0x27,0x65,0x27,0x2a,0x28,0x2b,0x2f,0x20,0x43,0x0a,0x65,0x2e,0x2c,0x65,\n 0x2a,0x31,0x24,0x33,0x3a,0x65,0x3e,0x2b,0x20,0x27,0x63,0x0c,0x69,0x2b,0x20,0x28,\n 0x31,0x65,0x28,0x63,0x26,0x30,0x2e,0x27,0x28,0x2f};\n\n unsigned char o_ch1[256]; \n memset(o_ch1, '\\0', 256);\n unsigned char* po_ch1 = o_ch1;\n \n xor_repeatedly(xor_val, ch1, po_ch1);\n\n for(unsigned i=0; i +#include +#include "utils/hex_to_base64.h" + +static unsigned calculate_nb_of_bits_set(unsigned char i_char) +{ + static const unsigned len = 8; + unsigned char tmp_char = i_char; + unsigned idx = 0; + unsigned ret = 0; + for(;idx> 1; + } + return ret; +} + +// calculate distance between s1 and s2 +// -1 : failure (different sizes of the strings) +int block_distance(const char* const s1, const char* const s2, const unsigned i_size) +{ + int ret = 0; + int counter = 0; + + for(counter=0; counter max dist is 40 + double tmp_dist1, tmp_dist2; + int min_block_size=2; + + tmp_dist1 = tmp_dist2 = 0; + + for(; block_size<=40; block_size++) + { + // take 4 blocks, calc distance, normalize and get min + + tmp_dist1 = block_distance(i_string, i_string+block_size, block_size); + tmp_dist1 = tmp_dist1/block_size; + tmp_dist2 = block_distance(i_string+(block_size*2), i_string+(block_size*3), block_size); + tmp_dist2 = tmp_dist2/block_size; + + if(tmp_dist2 #include #include +#include "utils/hex_to_base64.h" #define MIN(a,b) a96) // a... + return T-71; //// 26=97-71 + else if(T>64) // A... + return T-65; //// 0=65-65 + else if(T>47) // 0... + return T+5; //// 52=47+5 + else if(T==47) // / + return T+16; //// 63=47+16 + else if(T==43) // + + return T+21; //// 64=43+21 +} + +static const unsigned char int_to_base64[65] = { 'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N','O','P','Q','R','S','T', 'U','V','W','X','Y','Z','a','b','c','d', @@ -46,10 +61,6 @@ unsigned char int_to_base64[65] = { '8','9','+','/','=' }; -// data stores 24 bits -struct int24 { - unsigned int data:24; -}; void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char*& oHexArray) { @@ -71,7 +82,7 @@ void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexAr } } -void read_bytes(const char* hex_buff, int24& o_data, unsigned len=3) +void read_bytes(const char* hex_buff, struct int24& o_data, unsigned len=3) { unsigned char* tmp = (unsigned char*)malloc(len); convert_string_to_hex(hex_buff, len*2, tmp); @@ -153,7 +164,6 @@ unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_bu return j; } - /* ----------------------------------------------------------------------------- * @brief xor_strings * @@ -187,4 +197,53 @@ void xor_strings(const char* const iLeftString, const char* const iRightString, free(xor_array1); free(xor_array2); -} \ No newline at end of file +} + +struct int24 base64_3char_to_hex(const char (&T)[4]) +{ + unsigned char a = ( c2b(T[0]) << 2 ) | (c2b(T[1]) >> 4 ); + unsigned char b = ((c2b(T[1]) & 0xF ) << 4) | (c2b(T[2]) >> 2); + unsigned char c = ((c2b(T[2]) & 3 ) << 6 ) | (c2b(T[3]) & 0x3F); + struct int24 t; + t.data = (a<<16) | (b<<8) | c; + return t; +} + +/* ----------------------------------------------------------------------------- + * @brief Converts string encoded in base64 to HEX + * + * @param i_string: Base64 encoded string. + * @param i_string_len: % 4 must == 0 + * @param o_hex_array: array to be populated with HEX's. Caller must ensure + * that array has required size. Method doesn't do any checks + * + * @returns -1 on failure, 0 on success + * +-------------------------------------------------------------------------------- */ +int base64_to_hex(const char* const i_string, int i_string_len, unsigned char*& o_hex_array ) +{ + int pointer = 0; + int hex_pointer = 0; + char array[4]; + + struct int24 tmp; + while(pointer> 16; + o_hex_array[hex_pointer++] = tmp.data >> 8; + o_hex_array[hex_pointer++] = tmp.data; + } + + return 0; +} + diff --git a/utils/hex_to_base64.h b/utils/hex_to_base64.h index e1cb1b9..ed16fa3 100644 --- a/utils/hex_to_base64.h +++ b/utils/hex_to_base64.h @@ -1,4 +1,11 @@ +// data stores 24 bits +struct int24 { + unsigned int data:24; +}; + unsigned hex_to_base64(const char* hex_buff, char*& base64_buff, unsigned hex_buff_len ); +int base64_to_hex(const char* const i_string, int i_string_len, unsigned char*& o_hex_array ); void convert_string_to_hex(const char* const iArray, const unsigned iArrayLen, unsigned char*& oHexArray); void convert_hex_to_string(const unsigned char* const iHexArray, unsigned iHexArrayLen, char*& oStringBuf ); void xor_strings(const char* const iLeftString, const char* const iRightString, unsigned char*& oXorArray); +int c2b(char T); diff --git a/utils/utils_tester.cpp b/utils/utils_tester.cpp index f033325..9113586 100644 --- a/utils/utils_tester.cpp +++ b/utils/utils_tester.cpp @@ -1,5 +1,6 @@ #include "hex_to_base64.h" #include "set1/xor_char_finder.h" +#include "hamming.h" #include #include #include @@ -46,6 +47,33 @@ void hex_to_base64_text() { assert( strlen(expected_buff4) == strlen(output)); } +unsigned long read_file_to_buffer(const char* const filepath, char*& obuff, int remove_new_line_chr = 1) +{ + FILE* fh=0; + size_t len=0; + char* line=NULL; + unsigned long total_len=0; + + if( (fh=fopen(filepath, "rb")) == 0 ) + { + fprintf(stderr, "File %s doesn't exist. Exiting...\n", filepath); + exit(1); + } + + while( getline(&line, &len, fh) != -1 ) + { + len = strlen(line); + if(remove_new_line_chr) + len--; + + strncpy(obuff+total_len, line, len); + total_len+=len; + free(line); + line=NULL; + } + return total_len; +} + void convert_string_to_hex_test() { const char test_buff1[] = "49276d"; @@ -118,14 +146,14 @@ void set2_challange4_test() xor_char_finder(line, max_score); if(max_score.letter != 0x00) { - printf("%d -> %c\n", line_nb, max_score.letter); + // printf("%d -> %c\n", line_nb, max_score.letter); convert_string_to_hex(line, 60, p_hexdump); for(int i=0; i<30;++i) { - printf("%c", hexdump[i]^max_score.letter); + // printf("%c", hexdump[i]^max_score.letter); } - printf("\n"); + // printf("\n"); } free(line); line=0; len=0; @@ -154,6 +182,23 @@ void set2_challenge_5_test() assert( expected[i] == po_ch1[i] ); } +void hamming_test() +{ + static const char ch1[]="this is a test"; + static const char ch2[]="wokka wokka!!!"; + assert(block_distance(ch1,ch2,strlen(ch1)) == 37); +} + +void base64_to_hex_test() +{ + static const char ch2[]="TWFuTWFu"; // 0x41 0x61 0x6E 0x41 0x61 0x6E + unsigned char out[6]; + unsigned char *p=out; + unsigned char expected[6] = {0x4D, 0x61, 0x6E, 0x4D, 0x61, 0x6E}; + base64_to_hex(ch2, 8, p); + assert( memcmp(expected, out, 6) == 0 ); +} + int main() { hex_to_base64_text(); convert_string_to_hex_test(); @@ -161,5 +206,13 @@ int main() { //set1_challenge3_test(); set2_challange4_test(); set2_challenge_5_test(); + hamming_test(); + base64_to_hex_test(); + + char buf[1024*1024]; // 1 MB + char* pbuf=buf; + unsigned long l=read_file_to_buffer("etc/set1_t6.txt", pbuf); + buf[l]='\0'; + printf("%s\n", buf); }