Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
D7018E-Assignment-4
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fredrik Pettersson
D7018E-Assignment-4
Commits
f258c837
Commit
f258c837
authored
7 years ago
by
DevDoggo
Browse files
Options
Downloads
Patches
Plain Diff
Finished Lab4
parent
a9e8f007
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
README.md
+58
-0
58 additions, 0 deletions
README.md
compile.sh
+2
-1
2 additions, 1 deletion
compile.sh
gdb.sh
+2
-1
2 additions, 1 deletion
gdb.sh
src/main.rs
+26
-18
26 additions, 18 deletions
src/main.rs
with
88 additions
and
20 deletions
README.md
+
58
−
0
View file @
f258c837
# 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
This diff is collapsed.
Click to expand it.
compile.sh
+
2
−
1
View file @
f258c837
#!/bin/bash
cargo build
--target
thumbv7em-none-eabihf
--release
#cargo build --target thumbv7em-none-eabihf --release
cargo build
--target
thumbv7em-none-eabihf
This diff is collapsed.
Click to expand it.
gdb.sh
+
2
−
1
View file @
f258c837
#!/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
This diff is collapsed.
Click to expand it.
src/main.rs
+
26
−
18
View file @
f258c837
#![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
,
"
\n
Complete 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
{}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment