#include #include #include #include #include "src/base64.h" #include "src/hamming.h" #include "src/xor.h" #include "src/common.h" #include "src/xor_char_finder.h" void hex_to_base64_test() { const char test_buff1[] ="49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"; const char expected_buff1[] ="SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"; const char test_buff2[] ="4927"; const char expected_buff2[] ="SSH"; const char test_buff3[] ="49"; const char expected_buff3[] ="SB"; const char test_buff4[] ="33"; const char expected_buff4[] ="z"; const char test_buff5[] = "616e79"; const char expected_buff5[] = "YW55"; char output[256]; memset (output,'\0', 256); hex_to_base64(test_buff1, output, strlen(test_buff1) ); CHECK( memcmp(expected_buff1, output, strlen(expected_buff1)) == 0 ); CHECK( strlen(expected_buff1) == strlen(output)); // test case when there are 2 hex'es memset (output,'\0', 256); hex_to_base64(test_buff2, output, strlen(test_buff2) ); CHECK( memcmp(expected_buff2, output, strlen(expected_buff2)) == 0 ); CHECK( strlen(expected_buff2) == strlen(output)); // test case when there is 1 hex memset (output,'\0', 256); hex_to_base64(test_buff3, output, strlen(test_buff3) ); CHECK( memcmp(expected_buff3, output, strlen(expected_buff3)) == 0 ); CHECK( strlen(expected_buff3) == strlen(output)); // test case when there is 1 char memset (output,'\0', 256); hex_to_base64(test_buff4, output, strlen(test_buff4) ); CHECK( memcmp(expected_buff4, output, strlen(expected_buff4)) == 0 ); CHECK( strlen(expected_buff4) == strlen(output)); memset (output,'\0', 256); hex_to_base64(test_buff5, output, strlen(test_buff5) ); CHECK( memcmp(expected_buff5, output, strlen(expected_buff5)) == 0 ); CHECK( strlen(expected_buff5) == strlen(output)); } void convert_string_to_hex_test() { const char test_buff1[] = "49276d"; unsigned char out_buff[3]; convert_string_to_hex(test_buff1, strlen(test_buff1), out_buff); CHECK( out_buff[0] == 0x49 && out_buff[1] == 0x27 && out_buff[2] == 0x6d); // and back char out_buf[6]; convert_hex_to_string(out_buff, 3, out_buf); CHECK( memcmp(out_buf, test_buff1, 6) == 0 ); } void xor_strings_test() { const char i_buf_1[] = "1c0111001f010100061a024b53535009181c"; const char i_buf_2[] = "686974207468652062756c6c277320657965"; char out_buf[36]; unsigned char buf[36/2]; xor_strings(i_buf_1, i_buf_2, buf); convert_hex_to_string(buf, 36/2, out_buf); CHECK( memcmp(out_buf, "746865206b696420646f6e277420706c6179", 36) == 0); } void hamming_test() { unsigned char msg[256]; const unsigned char ch1[]="this is a test"; const unsigned char ch2[]="wokka wokka!!!"; sprintf((char*)msg, "GOT: %d", block_distance(ch1, ch2,14)); CHECK(block_distance(ch1,ch2, 14) == 37, msg); const unsigned char ch3[]="test1"; const unsigned char ch4[]="test3"; sprintf((char*)msg, "GOT: %d", block_distance(ch3, ch4,5)); CHECK(block_distance(ch3,ch4,5)==1, msg); const unsigned char ch5[]={0x01, 0x03}; const unsigned char ch6[]={0x02, 0x02}; sprintf((char*)msg, "GOT: %d", block_distance(ch5, ch6,2)); CHECK(block_distance(ch5,ch6,2)==3, msg); } void base64_to_hex_test() { const unsigned char ch2[]="TWFuTWFu"; // 0x41 0x61 0x6E 0x41 0x61 0x6E const unsigned char ch3[]="YW55IGNhcm5hbCBwbGVhc3U="; unsigned char out[6]; unsigned char out3[17]; unsigned char expected[6] = {0x4D, 0x61, 0x6E, 0x4D, 0x61, 0x6E}; unsigned char expected3[] = {0x61, 0x6e, 0x79, 0x20, 0x63, 0x61, 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x70, 0x6c, 0x65, 0x61, 0x73, 0x75}; base64_to_hex(ch2, 8, out); base64_to_hex(ch3, 17, out3); CHECK( memcmp(expected, out, 6) == 0 ); CHECK( memcmp(expected3, out3, 6) == 0 ); } void c2b_test() { CHECK( c2b('A') == 0 ); CHECK( c2b('L') == 11 ); CHECK( c2b('Z') == 25 ); CHECK( c2b('a') == 26 ); CHECK( c2b('z') == 51 ); CHECK( c2b('0') == 52 ); CHECK( c2b('5') == 57 ); CHECK( c2b('9') == 61 ); CHECK( c2b('+') == 62 ); CHECK( c2b('/') == 63 ); CHECK( c2b('=') == 64 ); CHECK( c2b('*') == 0xFF ); } void aes_block_test() { }