From 4ed371cc378a3b579d46ed89a2677769a6d5ea24 Mon Sep 17 00:00:00 2001 From: Benedict Date: Tue, 22 Mar 2016 19:11:22 +0100 Subject: completed set 2 challenge 11 --- lib/lib.c | 50 +++++++++++++++++++++++++++++------ lib/lib.h | 4 ++- lib/lib2.c | 55 ++++++++++++++++++++++++++++++++------- lib/lib2.h | 4 ++- set1/task1.c | 3 +-- set1/task8.c | 27 ++++--------------- set2/Makefile | 2 ++ set2/task10.c | 4 ++- set2/task11.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 188 insertions(+), 44 deletions(-) create mode 100644 set2/task11.c diff --git a/lib/lib.c b/lib/lib.c index 0eb5be8..7c8beae 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -81,7 +81,6 @@ static void three_bytes_to_base64(char * encode, int bytes_to_print, char *resul // last six bit11s four = encode[2] & 0x3F; ret[3] = base64_encode[four]; - ret[4] = '\0'; if(bytes_to_print-- > 0) result[3] = ret[3]; } @@ -378,12 +377,11 @@ static int frequent_histogramm_matchs(char *string, int length) * encode the given string into base64 and stores it in result */ -void encode_to_base64(char *encode, char *result) +void encode_to_base64(char *encode, int length_encode, char *result) { - int length = strlen(encode); - int rounds = length / 3; - int bytes_last_round = length % 3; + int rounds = length_encode / 3; + int bytes_last_round = length_encode % 3; int i; for (i=0;i 0) { - three_bytes_to_base64(encode + i*3, 1+bytes_last_round, &result[i*4]); + three_bytes_to_base64(&encode[i*3], (4-bytes_last_round), &result[i*4]); - for(i=0;i<(3-bytes_last_round);i++) - result[i*4+(1+bytes_last_round)] = '='; + for(i=bytes_last_round;i>=0;i--) + result[rounds*4+(4-bytes_last_round)] = '='; + + result[rounds*4+4] = '\0'; } } @@ -522,3 +524,35 @@ int read_base64_file(const char *file, char **out) return file_pos; } + +int count_equal_blocks(char *string, int length_string, int blocksize) +{ + int number_blocks = length_string / blocksize; + printf("number_blocks: %i", number_blocks); + int hits = 0, i, j, k; + + for(i=0;i (length_string/blocksize)); +} + + diff --git a/lib/lib.h b/lib/lib.h index cca5fb0..8e384e8 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -16,12 +16,14 @@ void print_char_bit(char); void xor_string(char *str1, char *key, char *result, int length_key, int length_str1); void hex_binary_to_string(char *str1, char *result, int length); int decode_hex_string(char *encode, char* result); -void encode_to_base64(char *encode, char *result); +void encode_to_base64(char *encode, int encode_lengt, char *result); int decode_base64(char *string1, char *result); void print_base64_string(char *string); int hamming_distance_equal_length(char *string1, char *string2, int length); char brute_force_single_byte_xor(char *string, int length, struct key_and_freq *tmp); int isprintable(char *string, int length); int read_base64_file(const char *file, char **out); +int string_is_ecb_encrypted(char *string, int length_string, int blocksize); + #endif /* __CYRPTO_LIB__ */ diff --git a/lib/lib2.c b/lib/lib2.c index 49f7363..a28b7a5 100644 --- a/lib/lib2.c +++ b/lib/lib2.c @@ -59,7 +59,7 @@ int valid_pkcs7_padding(const char *in, int length_in, char *unpadded, int block * @param string_key key with which the content in in has been decrypted * @param iv initalization vector */ -int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector) +int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector, int encrypt) { char iv[16]; AES_KEY key; @@ -69,22 +69,59 @@ int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char unsigned char tmp_after_aes[128+1]; unsigned char cleartext[128+1]; // set the key and bits - AES_set_decrypt_key(string_key, 128, &key); + if(encrypt) + AES_set_encrypt_key(string_key, 128, &key); + else + AES_set_decrypt_key(string_key, 128, &key); memcpy(init_vector, iv, 16); // implement cbc mode for(i=0;i char *pkcs7_padding(char *string, int length_string, int blocksize); -int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector); +int aes_cbc(char *in, int length_in, char *out, unsigned char *string_key, char *init_vector, int encrypt); int valid_pkcs7_padding(const char *in, int length_in, char *unpadded, int blocksize); +int aes_ecb(char *in, int length_in, char *out, unsigned char *string_key, + int blocksize, int encrypt); #endif diff --git a/set1/task1.c b/set1/task1.c index 945964a..7d2a87a 100644 --- a/set1/task1.c +++ b/set1/task1.c @@ -6,12 +6,11 @@ int main(int argc, char **argv) if (argc != 2) return 0; - char *result = malloc(strlen(argv[1])); char *base64 = malloc(strlen(argv[1])); decode_hex_string(argv[1], result); - encode_to_base64(result, base64); + encode_to_base64(result, strlen(argv[1])/2, base64); printf("%s\n", base64); diff --git a/set1/task8.c b/set1/task8.c index 98b3380..d3aa8a8 100644 --- a/set1/task8.c +++ b/set1/task8.c @@ -4,9 +4,9 @@ int main(int arc, char **argv) { // detect AEC in ECB mode, do NOT break it - int number_blocks, i, j, k, read = 0; + int read = 0; size_t len = 0; - int max_hits = 0, hits = 0, line_number= 0, aes_ecb_line = 0; + int line_number= 0, aes_ecb_line = 0; char *line_hex = NULL; // read file FILE *f = fopen("8.txt", "r"); @@ -19,29 +19,12 @@ int main(int arc, char **argv) while( (read = getline(&line_hex, &len, f)) != -1) { // line is hex encoded char *line = malloc(read/2+1); - decode_hex_string(line_hex, line); - - number_blocks = read / 2 / 16; - // count 16 byte blocks which are equal - for(i=0;i max_hits) { - max_hits = hits; + printf("line %i: ", line_number); + if (string_is_ecb_encrypted(line, read/2+1, 16)) { aes_ecb_line = line_number; } line_number++; - hits = 0; free(line_hex); free(line); // set line and len to null @@ -49,7 +32,7 @@ int main(int arc, char **argv) len = 0; } - printf("found AES-128-ECB at line: %i\n with %i hits", aes_ecb_line, max_hits); + printf("found AES-128-ECB at line: %i\n", aes_ecb_line); return 0; } diff --git a/set2/Makefile b/set2/Makefile index 1f8f890..9c5f43e 100644 --- a/set2/Makefile +++ b/set2/Makefile @@ -9,6 +9,8 @@ task9: $(CC) $(CFLAGS) task9.c $(LIB) $(CLIBS) -o task9 task10: $(CC) $(CFLAGS) task10.c $(LIB) $(CLIBS) -o task10 +task11: + $(CC) $(CFLAGS) task11.c $(LIB) $(CLIBS) -o task11 task15: $(CC) $(CFLAGS) task15.c $(LIB) $(CLIBS) -o task15 clean: diff --git a/set2/task10.c b/set2/task10.c index fb8d0c1..19a7235 100644 --- a/set2/task10.c +++ b/set2/task10.c @@ -22,8 +22,10 @@ int main(int argc, char **argv) decoded_content_length = decode_base64(file_content, decoded_content); cleartext = malloc(decoded_content_length+1); // aes cbc - aes_cbc(decoded_content, decoded_content_length, cleartext, string_key, iv); + aes_cbc(decoded_content, decoded_content_length, cleartext, string_key, iv, 0); printf("%s\n", cleartext); + + free(decoded_content); free(cleartext); return 0; diff --git a/set2/task11.c b/set2/task11.c new file mode 100644 index 0000000..60483e0 --- /dev/null +++ b/set2/task11.c @@ -0,0 +1,83 @@ +#include "../lib/lib2.h" +#include "../lib/lib.h" + +int random_number_between(int min, int max) +{ + return (rand() % (max-min) + min); +} + + +int generate_random_bytes(char *buf, int length_key_bytes) +{ + int random_number; + int i; + for(i=0;i