diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/lib.c | 50 | ||||
| -rw-r--r-- | lib/lib.h | 4 | ||||
| -rw-r--r-- | lib/lib2.c | 55 | ||||
| -rw-r--r-- | lib/lib2.h | 4 |
4 files changed, 94 insertions, 19 deletions
@@ -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<rounds;i++) { @@ -391,11 +389,15 @@ void encode_to_base64(char *encode, char *result) } // in der letzen runde nicht mehr alle ausgeben // nur noch 3-leftover + result[rounds*4] = '\0'; + if (bytes_last_round > 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=bytes_last_round;i>=0;i--) + result[rounds*4+(4-bytes_last_round)] = '='; - for(i=0;i<(3-bytes_last_round);i++) - result[i*4+(1+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<number_blocks;i++) { + for(j=1;j<number_blocks;j++) { + for(k=0;k<blocksize;k++) { + if(string[i*blocksize+k] != string[j*blocksize+k]) + break; + if ( k == (blocksize-1)) + hits++; + } + } + } + printf("hits:%i\n", hits); + return hits; + +} + +int string_is_ecb_encrypted(char *string, int length_string, int blocksize) +{ + /** + * equal cleartext blocks results in equal ciphertext blocks + * in ECB mode, so basically we are just counting equal blocks... + */ + return (count_equal_blocks(string, length_string, blocksize) > (length_string/blocksize)); +} + + @@ -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__ */ @@ -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<number_blocks;i++) { - //do aes decryption - AES_decrypt(&in[i*16], tmp_after_aes, &key); - // xor - xor_string(iv, tmp_after_aes, &out[i*16], 16, 16); - // this ciphertext block is the next iv - for(j=0;j<16;j++) { - iv[j] = in[i*16+j]; + if (!encrypt) { + //do aes decryption + AES_decrypt(&in[i*16], tmp_after_aes, &key); + // xor + xor_string(iv, tmp_after_aes, &out[i*16], 16, 16); + // this ciphertext block is the next iv + for(j=0;j<16;j++) { + iv[j] = in[i*16+j]; + } + } + else { + // first xor + xor_string(iv, &in[i*16], tmp_after_aes, 16, 16); + // aes encrypt + AES_encrypt(tmp_after_aes, &out[i*16], &key); + // ciphertext is next iv + for(j=0;j<16;j++) { + iv[j] = out[i*16+j]; + } } } return 0; } + +int aes_ecb(char *in, int length_in, char *out, unsigned char *string_key, + int blocksize, int encrypt) +{ + AES_KEY key; + int number_blocks = length_in / blocksize; + int i; + + if(encrypt) + AES_set_encrypt_key(string_key, blocksize, &key); + else + AES_set_decrypt_key(string_key, blocksize, &key); + + for(i=0;i<number_blocks;i++) { + if(encrypt) { + AES_encrypt(&in[i*blocksize], &out[i*blocksize],&key); + } + else { + AES_decrypt(&in[i*blocksize], &out[i*blocksize],&key); + } + } +} @@ -7,7 +7,9 @@ #include <openssl/aes.h> 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 |
