diff options
| author | Benedict <benedict@0xb8000.de> | 2016-03-22 19:11:22 +0100 |
|---|---|---|
| committer | Benedict <benedict@0xb8000.de> | 2017-02-21 13:00:25 +0100 |
| commit | 4ed371cc378a3b579d46ed89a2677769a6d5ea24 (patch) | |
| tree | 9e9bb09f2798954540bc62d452258a74923c5e8d /lib/lib2.c | |
| parent | b586255b0dc1d6940c4a552a7125c9875e8adba6 (diff) | |
completed set 2 challenge 11
Diffstat (limited to 'lib/lib2.c')
| -rw-r--r-- | lib/lib2.c | 55 |
1 files changed, 46 insertions, 9 deletions
@@ -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); + } + } +} |
