Skip to content

Commit

Permalink
Solve challenge 18
Browse files Browse the repository at this point in the history
  • Loading branch information
mitiko committed Jun 30, 2024
1 parent 7651310 commit e85d43b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
11 changes: 2 additions & 9 deletions src/set2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,7 @@ YnkK";

// vulnerable function
fn ecb_random(plaintext: &[u8]) -> Vec<u8> {
// only for consistency (not actually required to use a seed)
let mut rng = rand::rngs::StdRng::from_seed([57; 32]);

// random key
let key = rng.gen();
let key = rand::rngs::StdRng::from_seed([57; 32]).gen();
let suffix = base64_to_raw(SECRET);

let data = {
Expand Down Expand Up @@ -158,10 +154,7 @@ where
fn test_suffix_len_detection() {
let vuln_fn_generator = |suffix_len: usize| {
move |plaintext: &[u8]| {
let mut rng = rand::rngs::StdRng::from_seed([57; 32]);

// random key
let key = rng.gen();
let key = rand::rngs::StdRng::from_seed([57; 32]).gen();
let suffix = b"A".repeat(suffix_len);

let data = {
Expand Down
14 changes: 11 additions & 3 deletions src/set3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{cbc::*, ecb::*, utils::io::*};
use crate::{cbc::*, ctr::aes128_ctr_decrypt, ecb::*, utils::{conversions::base64_to_raw, io::*}};
use lazy_static::lazy_static;
use rand::{Rng, SeedableRng};

Expand Down Expand Up @@ -58,7 +58,7 @@ fn crack_last_block(ciphertext: &[u8]) -> Vec<u8> {
let idx = n - usize::from(guess_pad) - 16;
mutated_ciphertext[idx] ^= 0x01;
let is_padded = leak_padding_error(&mutated_ciphertext);
mutated_ciphertext[idx] ^= 0x01;
mutated_ciphertext[idx] ^= 0x01; // undo xor to restore state
!is_padded
})
.unwrap();
Expand Down Expand Up @@ -98,7 +98,7 @@ fn crack_last_block(ciphertext: &[u8]) -> Vec<u8> {
mutated_ciphertext[idx] ^= byte;
mutated_ciphertext[idx - 1] ^= 0x01;
is_padded = leak_padding_error(&mutated_ciphertext);
mutated_ciphertext[idx - 1] ^= 0x01;
mutated_ciphertext[idx - 1] ^= 0x01; // undo xor to restore state
mutated_ciphertext[idx] ^= byte;
}
is_padded
Expand Down Expand Up @@ -136,3 +136,11 @@ fn challange17() {
assert_eq!(data, &plaintext);
}
}

#[test]
fn challange18() {
let ciphertext = base64_to_raw("L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==");
let key = b"YELLOW SUBMARINE";
let plaintext = aes128_ctr_decrypt(&ciphertext, key, 0, 0);
assert!(String::from_utf8_lossy(&plaintext).contains("Ice, Ice, baby"));
}
2 changes: 1 addition & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn aes128_ctr_multi_iteration() {
#[test]
fn aes128_ctr_multi_decrypt() {
let key = b"YELLOW SUBMARINE";
let data = b"Lorem ipsum dolor sit amet";
let data = b"Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
let (nonce, counter) = (3 << 32, 33);
let ciphertext = aes128_ctr_encrypt(data, key, nonce, counter);
assert_eq!(aes128_ctr_decrypt(&ciphertext, key, nonce, counter), data);
Expand Down

0 comments on commit e85d43b

Please sign in to comment.