Improve the soft wipe method

main
kirbylife 2023-04-27 23:28:12 -06:00
parent c8f14b7b19
commit 298117ee90
5 changed files with 27 additions and 34 deletions

7
Cargo.lock generated
View File

@ -198,12 +198,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "randomize"
version = "3.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88c38c99b51f33c9fcc655252bf02ac8048eb70f35244e4697b0de9c473e940a"
[[package]] [[package]]
name = "rtcc" name = "rtcc"
version = "0.3.0" version = "0.3.0"
@ -229,7 +223,6 @@ dependencies = [
"hmac-sha1-compact", "hmac-sha1-compact",
"nb 1.1.0", "nb 1.1.0",
"panic-halt", "panic-halt",
"randomize",
"ufmt 0.2.0", "ufmt 0.2.0",
] ]

View File

@ -10,7 +10,6 @@ hmac-sha1-compact = { git = "https://github.com/kirbylife/rust-hmac-sha1" }
panic-halt = "0.2.0" panic-halt = "0.2.0"
embedded-hal = "0.2.7" embedded-hal = "0.2.7"
nb = "1.1.0" nb = "1.1.0"
randomize = "3.0.1"
ufmt = "0.2.0" ufmt = "0.2.0"
[profile.release] [profile.release]

View File

@ -57,10 +57,8 @@ fn main() -> ! {
let mut display = screen::StnScreen::new(&mut rs, &mut en, &mut d4, &mut d5, &mut d6, &mut d7); let mut display = screen::StnScreen::new(&mut rs, &mut en, &mut d4, &mut d5, &mut d6, &mut d7);
display.clear(); display.clear();
let datetime = datetime::Datetime::from_ds3231(&mut rtc);
let mut eeprom = arduino_hal::Eeprom::new(dp.EEPROM); let mut eeprom = arduino_hal::Eeprom::new(dp.EEPROM);
let mut tokens = storage::Tokens::new(&mut eeprom, datetime.unix_epoch()); let mut tokens = storage::Tokens::new(&mut eeprom);
let up = pins.d6.into_pull_up_input(); let up = pins.d6.into_pull_up_input();
let mut up_button = button::Button::new(&up, true); let mut up_button = button::Button::new(&up, true);
@ -68,7 +66,7 @@ fn main() -> ! {
let mut down_button = button::Button::new(&down, true); let mut down_button = button::Button::new(&down, true);
up_button.update(); up_button.update();
if up_button.update() == button::Event::Pressed { if up_button.update() == button::Event::Pressed || tokens.current.is_none() {
display.write_str("Connected to"); display.write_str("Connected to");
display.set_cursor(0, 1); display.set_cursor(0, 1);
display.write_str("USB..."); display.write_str("USB...");
@ -126,7 +124,8 @@ fn main() -> ! {
}, },
SOFT_WIPE_TOKENS => { SOFT_WIPE_TOKENS => {
serial.write(OK).unwrap(); serial.write(OK).unwrap();
tokens.soft_wipe_all_tokens(); let deleted_tokens = tokens.soft_wipe_all_tokens();
serial.write(deleted_tokens).unwrap();
serial.write(OK).unwrap(); serial.write(OK).unwrap();
} }
HARD_WIPE_TOKENS => { HARD_WIPE_TOKENS => {
@ -140,9 +139,8 @@ fn main() -> ! {
} }
} }
let mut changed = false;
let mut last_index = 100;
let mut last_time = 0; let mut last_time = 0;
let mut changed = false;
loop { loop {
let timestamp = datetime::Datetime::from_ds3231(&mut rtc).unix_epoch(); let timestamp = datetime::Datetime::from_ds3231(&mut rtc).unix_epoch();

View File

@ -1,25 +1,23 @@
use arduino_hal::eeprom::Eeprom; use arduino_hal::eeprom::Eeprom;
use randomize::PCG32;
pub const SECRET_KEY_MAX_LEN: u16 = 32; pub const SECRET_KEY_MAX_LEN: u16 = 32;
pub const SECRET_KEY_NAME_LEN: u16 = 16; pub const SECRET_KEY_NAME_LEN: u16 = 16;
pub const SECRET_KEY_FULL_LEN: u16 = SECRET_KEY_MAX_LEN + SECRET_KEY_NAME_LEN; pub const SECRET_KEY_FULL_LEN: u16 = SECRET_KEY_MAX_LEN + SECRET_KEY_NAME_LEN;
pub const ENDL: u8 = 0;
pub struct Tokens<'a> { pub struct Tokens<'a> {
mem: &'a mut Eeprom, mem: &'a mut Eeprom,
pub current: Option<u16>, pub current: Option<u16>,
capacity: u16, capacity: u16,
rand: PCG32,
} }
impl<'a> Tokens<'a> { impl<'a> Tokens<'a> {
pub fn new(mem: &'a mut Eeprom, rand_seed: u64) -> Self { pub fn new(mem: &'a mut Eeprom) -> Self {
let capacity = mem.capacity() / SECRET_KEY_FULL_LEN; let capacity = mem.capacity() / SECRET_KEY_FULL_LEN;
let mut tokens = Tokens { let mut tokens = Tokens {
mem, mem,
capacity, capacity,
rand: PCG32::seed(rand_seed, 1),
current: None, current: None,
}; };
tokens.current = tokens.first(); tokens.current = tokens.first();
@ -30,7 +28,7 @@ impl<'a> Tokens<'a> {
pub fn search_free(&self) -> Option<u16> { pub fn search_free(&self) -> Option<u16> {
for n in 0..self.capacity { for n in 0..self.capacity {
let index = SECRET_KEY_FULL_LEN * n; let index = SECRET_KEY_FULL_LEN * n;
if self.mem.read_byte(index) == 255 { if self.mem.read_byte(index) == ENDL {
return Some(n); return Some(n);
} }
} }
@ -41,7 +39,7 @@ impl<'a> Tokens<'a> {
fn first(&self) -> Option<u16> { fn first(&self) -> Option<u16> {
for n in 0..self.capacity { for n in 0..self.capacity {
let index = SECRET_KEY_FULL_LEN * n; let index = SECRET_KEY_FULL_LEN * n;
if self.mem.read_byte(index) != 255 { if self.mem.read_byte(index) != ENDL {
return Some(n); return Some(n);
} }
} }
@ -52,7 +50,7 @@ impl<'a> Tokens<'a> {
let mut index = self.current.unwrap(); let mut index = self.current.unwrap();
for _ in 0..self.capacity { for _ in 0..self.capacity {
index = (index + 1) % self.capacity; index = (index + 1) % self.capacity;
if self.mem.read_byte(index * SECRET_KEY_FULL_LEN) != 255 { if self.mem.read_byte(index * SECRET_KEY_FULL_LEN) != ENDL {
self.current = Some(index); self.current = Some(index);
return Some(index); return Some(index);
} }
@ -97,14 +95,14 @@ impl<'a> Tokens<'a> {
pub fn delete(&mut self, index: u16) -> Option<u16> { pub fn delete(&mut self, index: u16) -> Option<u16> {
// The Arduino's EEPROM memory has a maximum number of write cycles. // The Arduino's EEPROM memory has a maximum number of write cycles.
// To keep writes to a minimum, only the first byte of the token name is set to 0 // To keep writes to a minimum, only the first byte of the token name is set to 0
// and a byte of the key is randomly chosen to be overwritten with // and wipe all the secret key
// another random value, so that it's unrecoverable. let index_token = index * SECRET_KEY_FULL_LEN;
let index_name = index * SECRET_KEY_FULL_LEN; let index_key_start = index_token + SECRET_KEY_NAME_LEN;
let index_key = (index * SECRET_KEY_FULL_LEN) + SECRET_KEY_NAME_LEN; let index_key_end = index_key_start + SECRET_KEY_MAX_LEN - 1;
let index_key = index_key + (self.rand.next_u32() % SECRET_KEY_MAX_LEN as u32) as u16; self.mem.write_byte(index_token, ENDL);
let rand_byte = (self.rand.next_u32() % 255) as u8; for index in index_key_start..index_key_end {
self.mem.write_byte(index_name, 255); self.mem.write_byte(index, ENDL);
self.mem.write_byte(index_key, rand_byte); }
Some(index) Some(index)
} }
@ -113,7 +111,7 @@ impl<'a> Tokens<'a> {
for n in 0..self.capacity { for n in 0..self.capacity {
let index = SECRET_KEY_FULL_LEN * n; let index = SECRET_KEY_FULL_LEN * n;
if self.mem.read_byte(index) != 255 { if self.mem.read_byte(index) != ENDL {
self.delete(index); self.delete(index);
inc += 1; inc += 1;
} }
@ -122,6 +120,8 @@ impl<'a> Tokens<'a> {
} }
pub fn hard_wipe_all_tokens(&mut self) { pub fn hard_wipe_all_tokens(&mut self) {
self.mem.erase(0, self.mem.capacity()).unwrap(); for index in 0..self.mem.capacity() {
self.mem.write_byte(index, ENDL);
}
} }
} }

View File

@ -125,21 +125,24 @@ def main(argv: list[str]):
print("Error trying to add the token, try again") print("Error trying to add the token, try again")
else: else:
print("Token added successfully!") print("Token added successfully!")
# Wipe tokens # Soft wipe tokens
elif opt == "4": elif opt == "4":
conn.write(SOFT_WIPE_TOKENS) conn.write(SOFT_WIPE_TOKENS)
sleep(0.1) sleep(0.1)
_ = conn.read() _ = conn.read()
deleted_tokens = conn.read()
resp = conn.read() resp = conn.read()
if resp == OK: if resp == OK:
print("All the tokens wipped successfully!") print(f"{deleted_tokens} tokens wipped successfully!")
else: else:
print("Error!!") print("Error!!")
# Hard wipe tokens
elif opt == "5": elif opt == "5":
resp = loop_input("This will erase all the EEPROM, do you want to continue? [Y/N]", ["y", "Y", "n", "N"]).upper() resp = loop_input("This will erase all the EEPROM, do you want to continue? [Y/N]", ["y", "Y", "n", "N"]).upper()
if resp == "Y": if resp == "Y":
conn.write(HARD_WIPE_TOKENS) conn.write(HARD_WIPE_TOKENS)
sleep(0.1) sleep(0.1)
_ = conn.read()
resp = conn.read() resp = conn.read()
if resp == OK: if resp == OK:
print("All the Eeprom erased successfully!") print("All the Eeprom erased successfully!")