Skip to content

Commit b53e23d

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

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

hal/src/aes.rs

Lines changed: 34 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,38 @@ 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+
if plaintext.len() % 4 != 0 {
838+
//TODO padding
839+
todo!("Padding is currently missing, make sure to have multiples of 128 bits!")
840+
}
841+
let mut i = 0;
842+
while i < plaintext.len() {
843+
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+
861+
i = i + 4;
862+
}
863+
864+
836865
Ok(())
837866
}
838867

0 commit comments

Comments
 (0)