Refactor code based on clippy suggestions
This commit is contained in:
109
src/set2.rs
109
src/set2.rs
@@ -32,8 +32,8 @@ pub fn challenge10() {
|
||||
pub fn challenge11() {
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum EncryptionType {
|
||||
CBC,
|
||||
ECB,
|
||||
Cbc,
|
||||
Ecb,
|
||||
}
|
||||
|
||||
fn pad_data(mut v: Vec<u8>) -> Bytes {
|
||||
@@ -56,10 +56,10 @@ pub fn challenge11() {
|
||||
// which to use.
|
||||
let zero_or_one: u32 = rand::thread_rng().gen_range(0..2);
|
||||
let (data, encryption_type) = if zero_or_one == 1 {
|
||||
(ecb::encrypt(&key, &padded_data), EncryptionType::ECB)
|
||||
(ecb::encrypt(&key, &padded_data), EncryptionType::Ecb)
|
||||
} else {
|
||||
let iv = Bytes::random(16);
|
||||
(cbc::encrypt(&key, &iv, &padded_data), EncryptionType::CBC)
|
||||
(cbc::encrypt(&key, &iv, &padded_data), EncryptionType::Cbc)
|
||||
};
|
||||
(data, encryption_type)
|
||||
}
|
||||
@@ -69,9 +69,9 @@ pub fn challenge11() {
|
||||
// with a piece of code that, pointed at a block box that might be encrypting ECB
|
||||
// or CBC, tells you which one is happening.
|
||||
if data.has_duplicated_cycle(16) {
|
||||
EncryptionType::ECB
|
||||
EncryptionType::Ecb
|
||||
} else {
|
||||
EncryptionType::CBC
|
||||
EncryptionType::Cbc
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,44 +106,38 @@ pub fn challenge12() {
|
||||
let mut data = data.to_vec();
|
||||
let mut string = utils::read_base64("data/12.txt");
|
||||
data.append(&mut string.0);
|
||||
let cipher = ecb::encrypt(&key, &Bytes(data));
|
||||
cipher
|
||||
ecb::encrypt(key, &Bytes(data))
|
||||
}
|
||||
|
||||
fn get_block_size(key: &Bytes) -> usize {
|
||||
// Detect cipher block size
|
||||
let mut v = vec![];
|
||||
let initial_cipher_len = encryption_oracle(&key, &Bytes(v.to_vec())).0.len();
|
||||
let initial_cipher_len = encryption_oracle(key, &Bytes(v.to_vec())).0.len();
|
||||
let mut new_cipher_len = initial_cipher_len;
|
||||
while initial_cipher_len == new_cipher_len {
|
||||
v.push(b'A');
|
||||
let cipher = encryption_oracle(&key, &Bytes(v.to_vec()));
|
||||
let cipher = encryption_oracle(key, &Bytes(v.to_vec()));
|
||||
new_cipher_len = cipher.0.len();
|
||||
}
|
||||
let key_length = new_cipher_len - initial_cipher_len;
|
||||
key_length
|
||||
new_cipher_len - initial_cipher_len
|
||||
}
|
||||
|
||||
fn is_encryption_ecb(key: &Bytes) -> bool {
|
||||
let data = Bytes::from_utf8("aaaabbbbccccddddaaaabbbbccccdddd");
|
||||
let cipher = encryption_oracle(&key, &data);
|
||||
if cipher.has_duplicated_cycle(16) {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
let cipher = encryption_oracle(key, &data);
|
||||
cipher.has_duplicated_cycle(16)
|
||||
}
|
||||
|
||||
fn decode(key: &Bytes) -> Bytes {
|
||||
let block_size = get_block_size(&key);
|
||||
let block_count = encryption_oracle(&key, &Bytes(vec![])).0.len() / block_size;
|
||||
let block_size = get_block_size(key);
|
||||
let block_count = encryption_oracle(key, &Bytes(vec![])).0.len() / block_size;
|
||||
let mut cleartext = vec![];
|
||||
|
||||
for block_index in 0..block_count {
|
||||
let mut cleartext_block = vec![];
|
||||
for padding_length in (0..block_size).rev() {
|
||||
let padding_text = vec![b'-'; padding_length];
|
||||
let expected = encryption_oracle(&key, &Bytes(padding_text.to_vec()));
|
||||
let expected = encryption_oracle(key, &Bytes(padding_text.to_vec()));
|
||||
let expected_block = expected.get_block(block_index, block_size);
|
||||
|
||||
let mut known_text = if block_index == 0 {
|
||||
@@ -159,7 +153,7 @@ pub fn challenge12() {
|
||||
let mut guess_text = known_text.to_vec();
|
||||
guess_text.push(i);
|
||||
let cipher_block =
|
||||
encryption_oracle(&key, &Bytes(guess_text)).get_block(0, block_size);
|
||||
encryption_oracle(key, &Bytes(guess_text)).get_block(0, block_size);
|
||||
if cipher_block.0 == expected_block.0 {
|
||||
cleartext_block.push(i);
|
||||
break;
|
||||
@@ -173,7 +167,7 @@ pub fn challenge12() {
|
||||
|
||||
let key = Bytes::random(16); // consistent but unknown key
|
||||
assert_eq!(get_block_size(&key), 16); // 1. discover block size
|
||||
assert_eq!(is_encryption_ecb(&key), true); // 2. confirm oracle uses ecb
|
||||
assert!(is_encryption_ecb(&key)); // 2. confirm oracle uses ecb
|
||||
let roundtrip_text = decode(&key); // 3.-6.
|
||||
let cleartext = utils::read_base64("data/12.txt");
|
||||
|
||||
@@ -182,10 +176,7 @@ pub fn challenge12() {
|
||||
// byte, encrypt it, and then compare it to the result of the encryption
|
||||
// oracle, but this approach is fine too.
|
||||
assert_eq!(roundtrip_text.0[..138], cleartext.0);
|
||||
println!(
|
||||
"[okay] Challenge 12: {}",
|
||||
roundtrip_text.to_utf8()[..17].to_string()
|
||||
);
|
||||
println!("[okay] Challenge 12: {}", roundtrip_text.to_sub_utf8(17));
|
||||
}
|
||||
|
||||
pub fn challenge13() {
|
||||
@@ -199,11 +190,11 @@ pub fn challenge13() {
|
||||
r.push_str("email=");
|
||||
r.push_str(input);
|
||||
r.push_str("&uid=1337&role=user");
|
||||
ecb::encrypt(&key, &Bytes(r.as_bytes().to_vec()))
|
||||
ecb::encrypt(key, &Bytes(r.as_bytes().to_vec()))
|
||||
}
|
||||
|
||||
fn decrypt(key: &Bytes, data: &Bytes) -> HashMap<String, String> {
|
||||
let c = ecb::decrypt(&key, &data);
|
||||
let c = ecb::decrypt(key, data);
|
||||
parser::parse_key_value(&c.to_utf8())
|
||||
}
|
||||
|
||||
@@ -218,19 +209,19 @@ pub fn challenge13() {
|
||||
// ________________________________
|
||||
// 0..34..78..bc..f0..34..78..bc..f0..34..78..bc..f
|
||||
// email=aaaaa@a.com&uid=1337&role=user
|
||||
let p = profile_for("aaaaa@a.com", &key);
|
||||
let p = profile_for("aaaaa@a.com", key);
|
||||
r.append(&mut p.0[0..32].to_vec());
|
||||
|
||||
// ----------------
|
||||
// 0..34..78..bc..f0..34..78..bc..f0..34..78..bc..f
|
||||
// email=aaaaaaa@a.admin&uid=1337&role=user
|
||||
let p = profile_for("aaaaaaa@a.admin", &key);
|
||||
let p = profile_for("aaaaaaa@a.admin", key);
|
||||
r.append(&mut p.0[16..32].to_vec());
|
||||
|
||||
// ----------------
|
||||
// 0..34..78..bc..f0..34..78..bc..f0..34..78..bc..f
|
||||
// email=aaaaaaaa@a.admin&uid=1337&role=user
|
||||
let p = profile_for("aaaaaaaa@a.admin", &key);
|
||||
let p = profile_for("aaaaaaaa@a.admin", key);
|
||||
r.append(&mut p.0[32..48].to_vec());
|
||||
|
||||
Bytes(r)
|
||||
@@ -259,8 +250,7 @@ pub fn challenge14() {
|
||||
plaintext.append(&mut attacker_controlled.to_vec());
|
||||
let mut target_bytes = utils::read_base64("data/12.txt").0;
|
||||
plaintext.append(&mut target_bytes);
|
||||
let cipher = ecb::encrypt(&random_key, &Bytes(plaintext));
|
||||
cipher
|
||||
ecb::encrypt(random_key, &Bytes(plaintext))
|
||||
}
|
||||
|
||||
fn get_block_size(prefix: &Bytes, key: &Bytes) -> usize {
|
||||
@@ -270,11 +260,8 @@ pub fn challenge14() {
|
||||
|
||||
for i in 1..10 {
|
||||
let block_size = i * 16;
|
||||
match get_duplicated_block_indices(&cipher, block_size) {
|
||||
Some((_, _)) => {
|
||||
return block_size;
|
||||
}
|
||||
_ => (),
|
||||
if let Some((_, _)) = get_duplicated_block_indices(&cipher, block_size) {
|
||||
return block_size;
|
||||
}
|
||||
}
|
||||
0
|
||||
@@ -293,18 +280,15 @@ pub fn challenge14() {
|
||||
}
|
||||
|
||||
fn get_prefix_size(prefix: &Bytes, key: &Bytes) -> usize {
|
||||
let block_size = get_block_size(&prefix, &key);
|
||||
let block_size = get_block_size(prefix, key);
|
||||
let duplicated_text = Bytes::from_utf8("aaaabbbbccccddddaaaabbbbccccdddd").0;
|
||||
|
||||
for i in 0..block_size {
|
||||
let mut padding = vec![b'a'; i];
|
||||
padding.append(&mut duplicated_text.to_vec());
|
||||
let cipher = encryption_oracle(prefix, key, &Bytes(padding));
|
||||
match get_duplicated_block_indices(&cipher, block_size) {
|
||||
Some((first_block, _)) => {
|
||||
return block_size * first_block - i;
|
||||
}
|
||||
_ => (),
|
||||
if let Some((first_block, _)) = get_duplicated_block_indices(&cipher, block_size) {
|
||||
return block_size * first_block - i;
|
||||
}
|
||||
}
|
||||
0
|
||||
@@ -315,7 +299,7 @@ pub fn challenge14() {
|
||||
let prefix_size = get_prefix_size(prefix, key);
|
||||
let prefix_padding_size = block_size - (prefix_size % block_size);
|
||||
let prefix_padding = Bytes(vec![b'a'; prefix_padding_size]);
|
||||
let block_count = encryption_oracle(&prefix, &key, &prefix_padding).len() / block_size;
|
||||
let block_count = encryption_oracle(prefix, key, &prefix_padding).len() / block_size;
|
||||
let first_block_index = (prefix_size + prefix_padding_size) / block_size;
|
||||
let mut cleartext = vec![];
|
||||
|
||||
@@ -371,36 +355,17 @@ pub fn challenge14() {
|
||||
let roundtrip_text = decode(&prefix, &key);
|
||||
let cleartext = utils::read_base64("data/12.txt");
|
||||
assert_eq!(roundtrip_text, cleartext);
|
||||
println!(
|
||||
"[okay] Challenge 14: {}",
|
||||
roundtrip_text.to_utf8()[..17].to_string()
|
||||
);
|
||||
println!("[okay] Challenge 14: {}", roundtrip_text.to_sub_utf8(17));
|
||||
}
|
||||
|
||||
pub fn challenge15() {
|
||||
assert_eq!(
|
||||
Bytes::from_utf8("ICE ICE BABY\u{4}\u{4}\u{4}").has_valid_pkcs7(16),
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
Bytes::from_utf8("ICE ICE BABY\u{4}\u{4}\u{4}\u{4}").has_valid_pkcs7(16),
|
||||
true
|
||||
);
|
||||
assert_eq!(
|
||||
Bytes::from_utf8("ICE ICE BABY\u{3}\u{3}\u{4}\u{4}").has_valid_pkcs7(16),
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
Bytes::from_utf8("ICE ICE BABY!!!\u{0}").has_valid_pkcs7(16),
|
||||
false
|
||||
);
|
||||
assert_eq!(
|
||||
Bytes::from_utf8("ICE ICE BABY!!!\u{1}").has_valid_pkcs7(16),
|
||||
true
|
||||
);
|
||||
assert!(!Bytes::from_utf8("ICE ICE BABY\u{4}\u{4}\u{4}").has_valid_pkcs7(16));
|
||||
assert!(!Bytes::from_utf8("ICE ICE BABY\u{3}\u{3}\u{4}\u{4}").has_valid_pkcs7(16));
|
||||
assert!(!Bytes::from_utf8("ICE ICE BABY!!!\u{0}").has_valid_pkcs7(16));
|
||||
assert!(Bytes::from_utf8("ICE ICE BABY!!!\u{1}").has_valid_pkcs7(16));
|
||||
let mut bytes = Bytes::from_utf8("ICE ICE BABY\u{3}\u{3}\u{4}\u{4}");
|
||||
bytes.pad_pkcs7(16);
|
||||
assert_eq!(bytes.has_valid_pkcs7(16), true);
|
||||
assert!(bytes.has_valid_pkcs7(16));
|
||||
println!("[okay] Challenge 15: PKCS7 works");
|
||||
}
|
||||
|
||||
@@ -417,7 +382,7 @@ pub fn challenge16() {
|
||||
r.push_str(";comment2=%20like%20a%20pound%20of%20bacon");
|
||||
let mut cleartext = Bytes(r.as_bytes().to_vec());
|
||||
cleartext.pad_pkcs7(16);
|
||||
cbc::encrypt(&key, &iv, &cleartext)
|
||||
cbc::encrypt(key, iv, &cleartext)
|
||||
}
|
||||
|
||||
let iv = Bytes::random(16);
|
||||
|
||||
Reference in New Issue
Block a user