diff --git a/examples/nested.rs b/examples/nested.rs index c143dd4fe6db2b99dd1edcc209c0d01e2a8574cc..7308990925d5201b27336c67d364c29ae0ad12c6 100644 --- a/examples/nested.rs +++ b/examples/nested.rs @@ -78,7 +78,7 @@ fn write_key(aes: &stm32f413::AES, key: &Key) { 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.keyr1.write(|w| unsafe { w.bits(key[2]) }); 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]) { fn _encrypt_ecb(aes: &stm32f413::AES, key: &Key, b_in: &[Block], b_out: &mut [Block]) { // clear complete and disable AES; 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 - 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); } @@ -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()); // mode 4. ECB aes.cr.write(|w| unsafe { w.mode().bits(0b11) }); - write_key_little_endian(aes, key); - // start encrytion, mode 1, ECB by default - aes.cr.modify(|_, w| w.en().set_bit()); + write_key_msw(aes, key); + //write_key(aes, key); + // 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); } @@ -347,23 +352,34 @@ fn _init(p: init::Peripherals, _r: init::Resources) { wait(1000); // stored with MSW first for simple comparison with http://aes.online-domain-tools.com/ - // writes key with write_key_little_endian - let key = [0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]; - let key = [0; 4]; + // writes key with write_key_msw + // let key = [0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]; + 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()); //let mut b1 = [[0x0123_4567, 0x89ab_cdef, 0x0123_4567, 0x89ab_cdef]]; // b1 is initially the plain data - let mut b1 = [[0; 4]]; // works - let mut b1 = [[0x1111_1111; 4]]; // works - let mut b1 = [[0x1111_1111, 0x1111_1111, 0x1111_1111, 0]]; // works + // let mut b1 = [[0; 4]]; // works + // let mut b1 = [[0x1111_1111; 4]]; // 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 s: &[u8] = "Merry X-Mas 2017".as_bytes(); - let s: &[u8] = "rreM-X y saM7102".as_bytes(); - //let s: &[u8] = b"aaaaaaaaaaaaaaaa"; - //let s: &[u8] = &[0; 16]; + let s: &[u8] = "& Happy New 2018".as_bytes(); let q: &mut [u8; SIZE_BYTES] = unsafe { mem::transmute::<_, _>(b1.as_mut_ptr()) }; q[..s.len()].clone_from_slice(s); @@ -378,8 +394,23 @@ fn _init(p: init::Peripherals, _r: init::Resources) { _encrypt_ecb(&p.AES, &key, &b1, &mut 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); 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)]