-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge11.cpp
More file actions
60 lines (45 loc) · 1.69 KB
/
challenge11.cpp
File metadata and controls
60 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
An ECB/CBC detection oracle
Now that you have ECB and CBC working:
Write a function to generate a random AES key; that's just 16 random bytes.
Write a function that encrypts data under an unknown key --- that is, a function that generates a random key and encrypts under it.
The function should look like:
encryption_oracle(your-input)
=> [MEANINGLESS JIBBER JABBER]
Under the hood, have the function append 5-10 bytes (count chosen randomly) before the plaintext and 5-10 bytes after the plaintext.
Now, have the function choose to encrypt under ECB 1/2 the time, and under CBC the other half (just use random IVs each time for CBC). Use rand(2) to decide which to use.
Detect the block cipher mode the function is using each time. You should end up with a piece of code that, pointed at a block box that might be encrypting ECB or CBC, tells you which one is happening.
*/
#include "challenges.h"
#include "random.h"
#include "aes.h"
#include "encoding_utils.h"
static bool isCBC = false;
std::string encryptionOracle(const std::string& input) {
std::string key = GenerateRandomBytes(16),
ciphertext,
plaintext = GenerateRandomBytes(5, 10) + input + GenerateRandomBytes(5, 10);
if (RandomInt(0, 1) == 0) {
ciphertext = aes_128_ecb_encrypt(plaintext, key);
isCBC = false;
}
else {
std::string iv = GenerateRandomBytes(16);
ciphertext = aes_128_cbc_encrypt(plaintext, key, iv, 16);
isCBC = true;
}
return ciphertext;
}
std::string challenge11() {
std::string plaintext(64, 'A');
for (int i = 0; i < 1024; i++) {
std::string ciphertext = encryptionOracle(plaintext);
if (hasRepeatingBlocks(ciphertext, 16) ^ isCBC) {
continue;
}
else {
return "FAIL";
}
}
return "PASS";
}