Skip to content
Snippets Groups Projects
Commit f258c837 authored by DevDoggo's avatar DevDoggo
Browse files

Finished Lab4

parent a9e8f007
No related branches found
No related tags found
No related merge requests found
# Decryptor for the Cortex-M
## Requirements
STM32F401 Nucleo-64
Rust
## Description
This implementation of code is made to be run ontop of a microcontroller.
Specifically a STM32F401 Nucleo-64 microcontroller.
The program is able to, given a seed-number, determine the decoded values
of an array of integers, which are in its original state an encoded string of text.
That is, given a string encoded in a specific way, this program can decode it.
## How to use
There exist three .sh scripts ready to be used for quick and simple compilation/debugging/running.
compile.sh
- Builds using cargo onto the target thumbv7em-none-eabihf (the specific microcontroller) in --release mode
```
cargo build --target thumbv7em-none-eabihf --release
```
openocd.sh
- Runs the code on the target
```
openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg
```
gdb.sh
- Runs gdb and connects to the target's code
```
TARGET=thumbv7em-none-eabihf
arm-none-eabi-gdb target/$TARGET/release/app_a4
```
# Memory Safety Discussion
First of all, we do use unsafe Rust code. This means that the safety of our static global variables is at risk!
......@@ -8,4 +45,25 @@ Because the compiler won't check unsafe code,
this could potentially in a worst case lead to information leakage or perhaps physical damage to infrastructure or people,
all depending on what the code is able to affect.
# Cycle Count
We can see the comparisons of clock cycles required to perform various tasks in various modes,
Debug and Released.
After enabling the ```clock_cycle_count()``` in the Rust code,
we can find the DWT_CYCCNT register (clock cycle count) at memory address 0xe0001004,
which we read from to get the following values.
A breakpoint is set just as decoding is complete to prevent additional clock cycles affecting the result.
### ABC
#### DEBUG
Cycles: 4730
#### RELEASE
Cycles: 350
### CODED
#### DEBUG
Cycles: 176506
#### RELEASE
Cycles: 11614
#!/bin/bash
cargo build --target thumbv7em-none-eabihf --release
#cargo build --target thumbv7em-none-eabihf --release
cargo build --target thumbv7em-none-eabihf
#!/bin/bash
TARGET=thumbv7em-none-eabihf
arm-none-eabi-gdb target/$TARGET/release/app_a4
#arm-none-eabi-gdb target/$TARGET/release/app_a4
arm-none-eabi-gdb target/$TARGET/debug/app_a4
#![no_std]
#![feature(lang_items)]
extern crate cortex_m;
extern crate cortex_m_semihosting;
extern crate stm32f40x;
use core::fmt::Write;
use cortex_m::asm;
use cortex_m_semihosting::hio;
use stm32f40x::DWT;
//u8 plain[132];
static mut PLAIN: [u8; 132] = [0; 132];
//abc
//static ABC: [u32; 4] = [0x9fdd9158, 0x85715808, 0xac73323a, 0];
static ABC: [u32; 4] = [0x9fdd9158, 0x85715808, 0xac73323a, 0];
static CODED: [u32; 132] = [0x015e7a47, 0x2ef84ebb, 0x177a8db4, 0x1b722ff9, 0x5dc7cff0,
0x5dc9dea6, 0x1da0c15a, 0xe4c236a2, 0x3d16b0d0, 0x1f397842, 0xaae0d2ba, 0x11246674,
0x0845317f, 0xd5512dad, 0xb6184977, 0xd293a53e, 0x7d9c2716, 0xd917eae6, 0xd8852384,
......@@ -38,7 +42,6 @@ static mut SEED: u32 = 0x0e0657c1;
fn codgen(seed: &mut u32) -> u32{
let n: u32 = seed.count_zeros(); // built-in count zeroes in n
let x: u32 = seed.rotate_left(30); // rorate left by 30 bits
......@@ -72,28 +75,33 @@ fn decode(wordarr: &[u32], bytearr: &mut [u8], mut seed: &mut u32) -> u32{
}
fn main() {
let mut stdout = hio::hstdout().unwrap();
unsafe {
let a: u32 = codgen(&mut TEST_SEED);
let b: u32 = codgen(&mut TEST_SEED);
let c: u32 = codgen(&mut TEST_SEED);
//println!("\nOutput Codgen:");
//println!("{:x}", a);
//println!("{:x}", b);
//println!("{:x}", c);
//writeln!(stdout, "0x{:x}", a).unwrap();
//writeln!(stdout, "{:x}", b).unwrap();
//writeln!(stdout, "{:x}", c).unwrap();
(*DWT.get()).enable_cycle_counter();
(*DWT.get()).cyccnt.write(0);
}
let mut stdout = hio::hstdout().unwrap();
unsafe {
decode(&CODED[..], &mut PLAIN[..], &mut SEED);
//println!("\nComplete Output: {}", std::str::from_utf8(&PLAIN[..]).unwrap());
//decode(&ABC[..], &mut PLAIN[..], &mut SEED);
// We check cycles at this point
asm::bkpt();
writeln!(stdout, "\nComplete Output: {}", core::str::from_utf8(&PLAIN[..]).unwrap());
}
// How am I supposed to use this?
//let y: u32 = DWT.get().get_cycle_count();
//let x: u32 = DWT::get_cycle_count();
//writeln!(stdout, "This is it: {:?}", x);
//DWT_CYCCNT @ 0xE0001004 is the Cycle Count Register, type: RW
//writeln!(stdout, "This is it: {}", n);
loop {}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment