#include "../lib/lib.h" #include "../lib/lib2.h" #include /** * So what are we doing here? * We do not know the key. But we can ask Alice to encrypt with here key * an arbritrary plaintext we give here. From the ciphertext she gives us * we can infer the original plaintext. A is attacker controlled plaintext. * P stands for plaintext we don't now. * K is plaintext we alredy know. * * with block size 16 we do: * AAAAAAAAAAAAAAAP * in the next round we know P, * AAAAAAAAAAAAAAKP * and next round: * AAAAAAAAAAAAAKKP * and so one until we know the complete block * * crack the second block: you now already the first block: * AAAAAAAAAAAAAAAK KKKKKKKKKKKKKKKP * you are not interested in the first block now, but in the P of * the last block. Since you now all the other K's in the second block * already you can crak P now. And so on. * * It is sufficient to make BLOCKSIZE encryption request to Alice to break * a plaintext of arbitrary length. */ int main(int argc, char **argv) { int i; srand(time(NULL)); char *base64_task_string = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK"; // generate random key once generate_random_bytes(key, 16); char *task_string = malloc(strlen(base64_task_string)); char *plaintext; // unbases it int length_cleartext = decode_base64(base64_task_string, task_string); // cleartext + maybe an additional block char *ciphertext = malloc(length_cleartext+17); // encrypt aes_ecb(task_string, length_cleartext, ciphertext, key, 16, 1); // discover the block size of the cipher int blocksize = detect_blocksize_ecb(task_string, length_cleartext, key); printf("Detected blocksize: %i\n", blocksize); // detect if it uses ECB printf("REAL PLAINTEXT:\n%s\n", task_string); char *test_string = "Benedict ist ein wirklicher, echter Mensch mit Wurzeln im Boden"; crack_aes_ecb(&plaintext, blocksize, 0); printf("Recovered plaintext:\n%s\n", plaintext); // make dictionary of every possible last byte by feedind different // string to the oracle function, e.g. AAAAAAAA, AAAAAAAB, AAAAAAAC //Match the output of the one-byte-short input to one of the // entries in your dictionary. return 0; }