Skip to content

Commit ecd3e91

Browse files
author
Jakob Gerstmayer
committed
modified encrypt cbc to take more than one block
1 parent be0ac18 commit ecd3e91

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

hal/src/aes.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,8 @@ impl Aes {
803803
&mut self,
804804
key: &[u32],
805805
_iv: &[u32; 4],
806-
plaintext: &[u32; 4],
807-
ciphertext: &mut [u32; 4],
806+
plaintext: &[u32],
807+
ciphertext: &mut [u32],
808808
) -> Result<(), Error> {
809809
const ALGO: Algorithm = Algorithm::Cbc;
810810
const CHMOD2: bool = ALGO.chmod2();
@@ -830,9 +830,36 @@ impl Aes {
830830
w.npblb().bits(0) // no padding
831831
});
832832

833-
self.set_din(plaintext);
834-
self.poll_completion()?;
835-
self.dout(ciphertext);
833+
if plaintext.len() != ciphertext.len() {
834+
panic!("Plaintext and Ciphertext fields need to have the same length!")
835+
}
836+
837+
//Would be nice to have automatic padding here
838+
if plaintext.len() % 4 != 0 {
839+
panic!("Plaintext has to be a multiple of 128 bits!")
840+
}
841+
842+
let mut i = 0;
843+
while i < plaintext.len() {
844+
let mut part: [u32; 4] = [0; 4];
845+
part[0] = plaintext[i];
846+
part[1] = plaintext[i + 1];
847+
part[2] = plaintext[i + 2];
848+
part[3] = plaintext[i + 3];
849+
850+
self.set_din(&part);
851+
self.poll_completion()?;
852+
853+
let mut cipher_out: [u32; 4] = [0; 4];
854+
self.dout(&mut cipher_out);
855+
ciphertext[i] = cipher_out[0];
856+
ciphertext[i + 1] = cipher_out[1];
857+
ciphertext[i + 2] = cipher_out[2];
858+
ciphertext[i + 3] = cipher_out[3];
859+
860+
i = i + 4;
861+
}
862+
836863
Ok(())
837864
}
838865

0 commit comments

Comments
 (0)