Skip to content

Commit

Permalink
Added equal check for cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmlzl committed Dec 28, 2020
1 parent 8c4f2db commit 4a0fbd5
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,17 @@ mod aes_ciy {
pub struct AES {
key: AESKey,
pub data: AESBlock,
pub cipher: Option<u128>,
}
impl AES {
pub fn new(plaintext: u128, key: u128) -> AES {
let key = AESKey::new(key);
let data = AESBlock::new(plaintext);
let cipher = None;
AES {
key,
data,
cipher,
}
}
pub fn encrypt(&mut self) {
Expand All @@ -211,6 +214,7 @@ mod aes_ciy {
self.normal_round();
}
self.last_round();
self.write_cipher();
}
fn first_round(&mut self) {
self.data.add_round_key(&self.key);
Expand All @@ -229,6 +233,13 @@ mod aes_ciy {
self.data.add_round_key(&self.key);

}
fn write_cipher(&mut self) {
let mut ciph: u128 = 0x0;
for (i, aes_b) in self.data.data.iter().enumerate() {
ciph |= (aes_b.get() as u128) << (15-i)*8;
}
self.cipher = Some(ciph);
}
}
}

Expand All @@ -242,7 +253,12 @@ fn main() {
let mut aes = AES::new(plaintext, key);
aes.encrypt();

for (i, d) in aes.data.data.iter().enumerate() {
println!("{}: 0x{:x}", i, d.get());
}
let cipher = match aes.cipher {
Some(t) => t,
None => panic!("houston, we fucked up!"),
};
println!("Cipher: {:x}", &cipher);

const CORR_CIPH: u128 = 0xBACF80FA05DF776E90CBF0E7D13335B4;
assert_eq!(cipher, CORR_CIPH, "wrong cipher computed!");
}

0 comments on commit 4a0fbd5

Please sign in to comment.