diff options
| -rw-r--r-- | lib/lib2.c | 46 | ||||
| -rw-r--r-- | lib/lib2.h | 4 | ||||
| -rw-r--r-- | set4/task26.c | 32 |
3 files changed, 65 insertions, 17 deletions
@@ -1,3 +1,4 @@ +#include "lib3.h" #include "lib2.h" #include "lib.h" @@ -422,7 +423,7 @@ void send_user(char *encrypted_user, int length) } -int challenge16_encrypt(char *input, char **encrypted) +int challenge16_encrypt(char *input, char **encrypted, int cbc_mode) { char *prepend = "comment1=cooking\%20MCs;userdata="; char *append = ";comment2=\%20like\%20a\%20pound\%20of\%20bacon"; @@ -465,24 +466,39 @@ int challenge16_encrypt(char *input, char **encrypted) memcpy(&res[strlen(prepend)+strlen(input)+2*quote_char], append, strlen(append)); res[strlen(prepend)+strlen(input)+strlen(append)+2*quote_char+1] = '\0'; // padding - unencrypted = pkcs7_padding(res, strlen(res), 16); - *encrypted = malloc(strlen(unencrypted)); - aes_cbc(unencrypted, strlen(unencrypted), *encrypted, key, iv , 1); - return strlen(unencrypted); + if(cbc_mode) { + unencrypted = pkcs7_padding(res, strlen(res), 16); + *encrypted = malloc(strlen(unencrypted)); + aes_cbc(unencrypted, strlen(unencrypted), *encrypted, key, iv , 1); + return strlen(unencrypted); + } + // otherwise is CTR mode + else { + *encrypted = malloc(strlen(res)); + aes_ctr(res, strlen(res), *encrypted, key, iv); + return strlen(res); + } } -void challenge16_decrypt(char *encrypted, int length) +void challenge16_decrypt(char *encrypted, int length, int cbc_mode) { char *unencrypted = malloc(length); char *unpadd= malloc(length); - - aes_cbc(encrypted, length, unencrypted, key, iv, 0); - // unpadd - int ret = valid_pkcs7_padding(unencrypted, length, unpadd, 16); - if(!ret) { - printf("no valid padding!\n"); - return; + + if(cbc_mode) { + aes_cbc(encrypted, length, unencrypted, key, iv, 0); + // unpadd + int ret = valid_pkcs7_padding(unencrypted, length, unpadd, 16); + if(!ret) { + printf("no valid padding!\n"); + return; + } + // look for string ;admin=true; + printf("unencrpyted string: %s\n", unpadd); + } + // we are in ctr mode + else { + aes_ctr(encrypted, length, unencrypted, key, iv); + printf("unencrpted string: %s\n", unencrypted); } - // look for string ;admin=true; - printf("unencrpyted string: %s\n", unpadd); } @@ -30,8 +30,8 @@ int crack_aes_ecb(char **plaintext, int blocksize, int offset); struct key_value_pair *parse_key_value(char *string, int length_string); char *profile_for(char *email); void send_user(char *encrypted_user, int length); -int challenge16_encrypt(char *input, char **encrypted); -void challenge16_decrypt(char *encrypted, int length); +int challenge16_encrypt(char *input, char **encrypted, int cbc_mode); +void challenge16_decrypt(char *encrypted, int length, int cbc_mode); int challenge12_and_14_oracle(char *attacker_data, int attacker_data_lengthn, char **ciphertext, int prepend_data); int aes_ecb_detect_prepended_data(); diff --git a/set4/task26.c b/set4/task26.c new file mode 100644 index 0000000..5c7b047 --- /dev/null +++ b/set4/task26.c @@ -0,0 +1,32 @@ +#include "../lib/lib.h" +#include "../lib/lib2.h" +#include "../lib/lib3.h" +#include "../lib/lib4.h" +#include <time.h> + + +int main() +{ + generate_random_bytes(key, 16); + generate_random_bytes(iv, 16); + + char *encrypted; + int length_enc = challenge16_encrypt(":admin<true:", &encrypted, 0); + + char *plaintext; + printf("the unchanged string:\n%s\n", plaintext); + challenge16_decrypt(encrypted, length_enc, 0); + /** + * the encrpyt function prepends two blocks + * + * we need to flip three bits, the 8th bit, 56th bit and the + * 96th bit + */ + encrypted[32] ^= 0x01; + encrypted[38] ^= 0x01; + encrypted[43] ^= 0x01; + + printf("now the changed string:\n"); + challenge16_decrypt(encrypted, length_enc, 0); + return 0; +} |
