Skip to content
Snippets Groups Projects
Commit 8c619421 authored by Per Lindgren's avatar Per Lindgren
Browse files

Works

parent acd5f82f
No related branches found
No related tags found
No related merge requests found
...@@ -78,7 +78,7 @@ fn write_key(aes: &stm32f413::AES, key: &Key) { ...@@ -78,7 +78,7 @@ fn write_key(aes: &stm32f413::AES, key: &Key) {
aes.keyr3.write(|w| unsafe { w.bits(key[3]) }); aes.keyr3.write(|w| unsafe { w.bits(key[3]) });
} }
fn write_key_little_endian(aes: &stm32f413::AES, key: &Key) { fn write_key_msw(aes: &stm32f413::AES, key: &Key) {
aes.keyr0.write(|w| unsafe { w.bits(key[3]) }); aes.keyr0.write(|w| unsafe { w.bits(key[3]) });
aes.keyr1.write(|w| unsafe { w.bits(key[2]) }); aes.keyr1.write(|w| unsafe { w.bits(key[2]) });
aes.keyr2.write(|w| unsafe { w.bits(key[1]) }); aes.keyr2.write(|w| unsafe { w.bits(key[1]) });
...@@ -319,9 +319,12 @@ fn _process(aes: &stm32f413::AES, b_in: &[Block], b_out: &mut [Block]) { ...@@ -319,9 +319,12 @@ fn _process(aes: &stm32f413::AES, b_in: &[Block], b_out: &mut [Block]) {
fn _encrypt_ecb(aes: &stm32f413::AES, key: &Key, b_in: &[Block], b_out: &mut [Block]) { fn _encrypt_ecb(aes: &stm32f413::AES, key: &Key, b_in: &[Block], b_out: &mut [Block]) {
// clear complete and disable AES; // clear complete and disable AES;
aes.cr.write(|w| w.ccfc().set_bit()); aes.cr.write(|w| w.ccfc().set_bit());
write_key_little_endian(aes, key); write_key_msw(aes, key);
//write_key(aes, key);
// start encrytion, mode 1, ECB by default // start encrytion, mode 1, ECB by default
aes.cr.modify(|_, w| w.en().set_bit()); //
aes.cr
.modify(|_, w| unsafe { w.datatype().bits(0b10).en().set_bit() });
_process(aes, b_in, b_out); _process(aes, b_in, b_out);
} }
...@@ -331,9 +334,11 @@ fn _decrypt_ecb(aes: &stm32f413::AES, key: &Key, b_in: &[Block], b_out: &mut [Bl ...@@ -331,9 +334,11 @@ fn _decrypt_ecb(aes: &stm32f413::AES, key: &Key, b_in: &[Block], b_out: &mut [Bl
aes.cr.write(|w| w.ccfc().set_bit()); aes.cr.write(|w| w.ccfc().set_bit());
// mode 4. ECB // mode 4. ECB
aes.cr.write(|w| unsafe { w.mode().bits(0b11) }); aes.cr.write(|w| unsafe { w.mode().bits(0b11) });
write_key_little_endian(aes, key); write_key_msw(aes, key);
// start encrytion, mode 1, ECB by default //write_key(aes, key);
aes.cr.modify(|_, w| w.en().set_bit()); // start dencryption, mode 1, ECB by default
aes.cr
.modify(|_, w| unsafe { w.datatype().bits(0b10).en().set_bit() });
_process(aes, b_in, b_out); _process(aes, b_in, b_out);
} }
...@@ -347,23 +352,34 @@ fn _init(p: init::Peripherals, _r: init::Resources) { ...@@ -347,23 +352,34 @@ fn _init(p: init::Peripherals, _r: init::Resources) {
wait(1000); wait(1000);
// stored with MSW first for simple comparison with http://aes.online-domain-tools.com/ // stored with MSW first for simple comparison with http://aes.online-domain-tools.com/
// writes key with write_key_little_endian // writes key with write_key_msw
let key = [0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]; // let key = [0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef];
let key = [0; 4]; let mut key = [0u32; 4];
{
let s: &[u8] = "Merry X-Mas 2017".as_bytes();
let q: &mut [u8; SIZE_BYTES] = unsafe { mem::transmute::<_, _>(key.as_mut_ptr()) };
q[..s.len()].clone_from_slice(s);
}
// swap the byte order (le -> be)
for mut d in key.iter_mut() {
let r = *d;
*d =
(r & 0xFF) << 24 | ((r >> 8) & 0xFF) << 16 | ((r >> 16) & 0xFF) << 8 | (r >> 24) & 0xFF;
}
p.AES.cr.modify(|_, w| w.en().set_bit()); p.AES.cr.modify(|_, w| w.en().set_bit());
//let mut b1 = [[0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]]; //let mut b1 = [[0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]];
// b1 is initially the plain data // b1 is initially the plain data
let mut b1 = [[0; 4]]; // works // let mut b1 = [[0; 4]]; // works
let mut b1 = [[0x1111_1111; 4]]; // works // let mut b1 = [[0x1111_1111; 4]]; // works
let mut b1 = [[0x1111_1111, 0x1111_1111, 0x1111_1111, 0]]; // works // let mut b1 = [[0x1111_1111, 0x1111_1111, 0x1111_1111, 0]]; // works
let mut b1 = [[0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]]; // works let mut b1 = [[0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]]; // works
{ {
let s: &[u8] = "Merry X-Mas 2017".as_bytes(); let s: &[u8] = "& Happy New 2018".as_bytes();
let s: &[u8] = "rreM-X y saM7102".as_bytes();
//let s: &[u8] = b"aaaaaaaaaaaaaaaa";
//let s: &[u8] = &[0; 16];
let q: &mut [u8; SIZE_BYTES] = unsafe { mem::transmute::<_, _>(b1.as_mut_ptr()) }; let q: &mut [u8; SIZE_BYTES] = unsafe { mem::transmute::<_, _>(b1.as_mut_ptr()) };
q[..s.len()].clone_from_slice(s); q[..s.len()].clone_from_slice(s);
...@@ -378,8 +394,23 @@ fn _init(p: init::Peripherals, _r: init::Resources) { ...@@ -378,8 +394,23 @@ fn _init(p: init::Peripherals, _r: init::Resources) {
_encrypt_ecb(&p.AES, &key, &b1, &mut b2); _encrypt_ecb(&p.AES, &key, &b1, &mut b2);
ipln!("encrypted {:?}", b2); ipln!("encrypted {:?}", b2);
let q: &[u32; SIZE_WORDS] = unsafe { mem::transmute::<_, _>(b2.as_ptr()) };
ipln!("q {:?}", q.len());
for d in q {
ipln!(
"q {:02x}{:02x}{:02x}{:02x}",
d >> 0 & 0xFF as u32,
d >> 8 & 0xFF as u32,
d >> 16 & 0xFF as u32,
d >> 24 & 0xFF as u32
);
}
_decrypt_ecb(&p.AES, &key, &b2, &mut b1); _decrypt_ecb(&p.AES, &key, &b2, &mut b1);
ipln!("decrypted {:?}", b1); ipln!("decrypted {:?}", b1);
let q: &[u8; SIZE_BYTES] = unsafe { mem::transmute::<_, _>(b1.as_ptr()) };
ipln!("q {:?}", q.len());
ipln!("q {:?}", core::str::from_utf8(&q[..]));
} }
#[inline(never)] #[inline(never)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment