Skip to content
Snippets Groups Projects
Commit ad975eb6 authored by pln's avatar pln
Browse files

now we have encrypted data

parent 83d7b7d8
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
// The trusted code base, providing services such as Authorization, Encrytion // The trusted code base, providing services such as Authorization, Encrytion
mod trusted { mod trusted {
use std::str::from_utf8;
pub type Key = [u8; 3];
pub struct Sec { pub struct Sec {
level: u8, level: u8,
} }
...@@ -32,11 +35,62 @@ mod trusted { ...@@ -32,11 +35,62 @@ mod trusted {
} }
} }
pub fn auth(s: &str) -> Sec { pub mod aut {
unsafe { if s == "abc" { Sec::new(2) } else { Sec::new(0) } } use trusted::*;
// should be in State
static CH: [(&str, &str); 3] =
[("abc", "abc"), ("bcd", "bcd"), ("cde", "cde")];
static mut CH_NR: usize = 0;
pub fn expire() {
unsafe {
CH_NR = (CH_NR + 1).wrapping_rem(CH.len());
println!("ch {} your challenge is {:?}", CH_NR, CH[CH_NR].0);
}
}
pub fn check(key: &Key) -> bool {
let s = from_utf8(key).unwrap();
unsafe { s == CH[CH_NR].1 }
}
pub fn auth(key: &Key) -> Sec {
unsafe { if check(key) { Sec::new(2) } else { Sec::new(0) } }
}
}
// structure to pass decryptable data
struct Data<T> {
key: Key,
data: T,
}
impl<T> Data<T> {
pub const fn new(d: T) -> Self {
Data {
key: [0, 0, 0],
data: d,
} }
} }
// deref ?
pub fn get(&self) -> Option<&T> {
if aut::check(&self.key) {
Some(&self.data)
} else {
None
}
}
}
/*
impl Aut for Data {
fn encrypt(aut: Sec, R) -> S;
fn decrypt(aut: Sec, S) -> R;
}
*/
}
static mut U1: u1::State = u1::State::new(); static mut U1: u1::State = u1::State::new();
static mut U2: u2::State = u2::State::new(); static mut U2: u2::State = u2::State::new();
...@@ -44,9 +98,11 @@ static mut U2: u2::State = u2::State::new(); ...@@ -44,9 +98,11 @@ static mut U2: u2::State = u2::State::new();
// Main should be inside trusted // Main should be inside trusted
fn main() { fn main() {
println!("trusted base"); println!("trusted base");
let mut u1 = unsafe { &mut U1 }; let mut u1 = unsafe { &mut U1 };
let mut u2 = unsafe { &mut U2 }; let mut u2 = unsafe { &mut U2 };
unsafe { unsafe {
let s = Sec::new(1); let s = Sec::new(1);
// s.level = 3; // s.level = 3;
u1.user1(&s); u1.user1(&s);
...@@ -56,6 +112,14 @@ fn main() { ...@@ -56,6 +112,14 @@ fn main() {
u2.user2(&s, &mut u1); // try with key set u2.user2(&s, &mut u1); // try with key set
u2.expire(&s); u2.expire(&s);
u2.user2(&s, &mut u1); // try with key set u2.user2(&s, &mut u1); // try with key set
u2.enter(&s, "abc"); //
u2.user2(&s, &mut u1); // try with key set
trusted::aut::expire();
u2.user2(&s, &mut u1); // try with key set
u2.enter(&s, "bcd"); //
u2.user2(&s, &mut u1); // try with key set
}; };
} }
...@@ -63,7 +127,8 @@ fn main() { ...@@ -63,7 +127,8 @@ fn main() {
// user code // user code
// compiler directive, disallow unsafe code // compiler directive, disallow unsafe code
// unsafe constructor new // unsafe constructor new
use trusted::{Sec, auth}; use trusted::Sec;
use trusted::aut::auth;
// //
...@@ -108,30 +173,39 @@ mod u1 { ...@@ -108,30 +173,39 @@ mod u1 {
mod u2 { mod u2 {
use ::*; use ::*;
use trusted::*;
// use std::slice::bytes;
// use std::cmp;
pub struct State { pub struct State {
key: &'static str, key: Key,
}
fn copy_slice(dst: &mut [u8], src: &[u8]) -> () {
for (d, s) in dst.iter_mut().zip(src.iter()) {
*d = *s;
}
} }
impl State { impl State {
pub const fn new() -> Self { pub const fn new() -> Self {
State { key: "---" } State { key: [0, 0, 0] }
} }
pub fn user2(&mut self, sec: &Sec, u1: &mut u1::State) { pub fn user2(&mut self, sec: &Sec, u1: &mut u1::State) {
println!("user2, level = {}", sec.level()); println!("user2, level = {}", sec.level());
let s = auth(self.key); let s = auth(&self.key);
println!("user2, data = {:?}", u1.get_data(sec, &s)); println!("user2, data = {:?}", u1.get_data(sec, &s));
} }
pub fn enter(&mut self, sec: &Sec, k: &str) { pub fn enter(&mut self, sec: &Sec, k: &str) {
println!("enter, level = {}", sec.level()); println!("enter, level = {}", sec.level());
self.key = "abc"; copy_slice(&mut self.key, k.as_bytes());
} }
pub fn expire(&mut self, sec: &Sec) { pub fn expire(&mut self, sec: &Sec) {
println!("enter, level = {}", sec.level()); println!("enter, level = {}", sec.level());
self.key = "---"; self.key = [0, 0, 0];
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment