Reduce the bin size removing the sha1_smol library
parent
dbc75352ac
commit
4396e6a43d
|
@ -101,6 +101,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ds323x"
|
name = "ds323x"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
|
source = "git+https://github.com/kirbylife/ds323x-rs?branch=fix-set-day-format#e204b77494f921cfc276c9a66e6e9ba95dc4e142"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"embedded-hal",
|
"embedded-hal",
|
||||||
"rtcc",
|
"rtcc",
|
||||||
|
@ -122,6 +123,11 @@ version = "0.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "723dce4e9f25b6e6c5f35628e144794e5b459216ed7da97b7c4b66cdb3fa82ca"
|
checksum = "723dce4e9f25b6e6c5f35628e144794e5b459216ed7da97b7c4b66cdb3fa82ca"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hmac-sha1-compact"
|
||||||
|
version = "1.1.3"
|
||||||
|
source = "git+https://github.com/kirbylife/rust-hmac-sha1#a3c4116837efb8a850fb0d03985831ce4dc2bc12"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nb"
|
name = "nb"
|
||||||
version = "0.1.3"
|
version = "0.1.3"
|
||||||
|
@ -220,19 +226,13 @@ dependencies = [
|
||||||
"arduino-hal",
|
"arduino-hal",
|
||||||
"ds323x",
|
"ds323x",
|
||||||
"embedded-hal",
|
"embedded-hal",
|
||||||
|
"hmac-sha1-compact",
|
||||||
"nb 1.1.0",
|
"nb 1.1.0",
|
||||||
"panic-halt",
|
"panic-halt",
|
||||||
"randomize",
|
"randomize",
|
||||||
"sha1_smol",
|
|
||||||
"ufmt 0.2.0",
|
"ufmt 0.2.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "sha1_smol"
|
|
||||||
version = "1.0.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.109"
|
version = "1.0.109"
|
||||||
|
|
|
@ -5,9 +5,9 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
arduino-hal = { git = "https://github.com/rahix/avr-hal", features = ["arduino-uno"] }
|
arduino-hal = { git = "https://github.com/rahix/avr-hal", features = ["arduino-uno"] }
|
||||||
|
ds323x = { git = "https://github.com/kirbylife/ds323x-rs", branch = "fix-set-day-format" }
|
||||||
|
hmac-sha1-compact = { git = "https://github.com/kirbylife/rust-hmac-sha1" }
|
||||||
panic-halt = "0.2.0"
|
panic-halt = "0.2.0"
|
||||||
sha1_smol = "1.0.0"
|
|
||||||
ds323x = { path = "../ds323x-rs" }
|
|
||||||
embedded-hal = "0.2.7"
|
embedded-hal = "0.2.7"
|
||||||
nb = "1.1.0"
|
nb = "1.1.0"
|
||||||
randomize = "3.0.1"
|
randomize = "3.0.1"
|
||||||
|
|
|
@ -1,34 +1,16 @@
|
||||||
use sha1_smol::Sha1;
|
use hmac_sha1_compact::HMAC;
|
||||||
|
|
||||||
const BLOCK_SIZE: usize = 64;
|
pub const INTERVAL: u64 = 30;
|
||||||
|
|
||||||
pub fn hmac_sha1(key: &[u8], message: &[u8], output: &mut [u8]) {
|
pub fn generate_otp_token(private_key: &[u8], actual_time: u64) -> u32 {
|
||||||
// Preprocess the key.
|
let interval = actual_time / INTERVAL;
|
||||||
let mut key_padded = [0u8; BLOCK_SIZE];
|
let msg = interval.to_be_bytes();
|
||||||
key_padded[..key.len()].copy_from_slice(key);
|
|
||||||
|
|
||||||
for byte in &mut key_padded {
|
let hmac_digest = HMAC::mac(&msg, private_key);
|
||||||
*byte ^= 0x36;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hash the inner padded key and the message.
|
let start = (hmac_digest[19] & 0xF) as usize;
|
||||||
let mut hash = Sha1::new();
|
let bytes: [u8; 4] = hmac_digest[start..start + 4].try_into().unwrap();
|
||||||
hash.update(&key_padded[..]);
|
|
||||||
hash.update(message);
|
|
||||||
let mut inner_hash = [0u8; 20];
|
|
||||||
inner_hash.copy_from_slice(&hash.digest().bytes());
|
|
||||||
|
|
||||||
// Preprocess the key again.
|
let raw_token = u32::from_be_bytes(bytes);
|
||||||
for byte in &mut key_padded {
|
((raw_token & 0x7F_FF_FF_FF) % 1_000_000) as u32
|
||||||
*byte ^= 0x36 ^ 0x5c;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hash the outer padded key and the inner hash.
|
|
||||||
let mut hash = Sha1::new();
|
|
||||||
hash.update(&key_padded[..]);
|
|
||||||
hash.update(&inner_hash[..]);
|
|
||||||
let hmac_digest = hash.digest().bytes();
|
|
||||||
|
|
||||||
// Copy the result into the output buffer.
|
|
||||||
output.copy_from_slice(&hmac_digest);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,8 @@ pub struct Datetime {
|
||||||
|
|
||||||
impl Datetime {
|
impl Datetime {
|
||||||
pub fn unix_epoch(&self) -> u64 {
|
pub fn unix_epoch(&self) -> u64 {
|
||||||
let mut year: i64 = self.year as i64 - 1900;
|
let year: i64 = self.year as i64 - 1900;
|
||||||
let mut month: i64 = self.month as i64 - 1;
|
let month: i64 = self.month as i64 - 1;
|
||||||
|
|
||||||
let (mut t, is_leap) = year_to_secs(year);
|
let (mut t, is_leap) = year_to_secs(year);
|
||||||
t += month_to_secs(month, is_leap);
|
t += month_to_secs(month, is_leap);
|
||||||
|
|
242
src/main.rs
242
src/main.rs
|
@ -11,15 +11,14 @@ use arduino_hal::default_serial;
|
||||||
use arduino_hal::delay_ms;
|
use arduino_hal::delay_ms;
|
||||||
// use arduino_hal::delay_ms;
|
// use arduino_hal::delay_ms;
|
||||||
use arduino_hal::prelude::*;
|
use arduino_hal::prelude::*;
|
||||||
use crypto::hmac_sha1;
|
use crypto::generate_otp_token;
|
||||||
|
use crypto::INTERVAL;
|
||||||
use ds323x::Rtcc;
|
use ds323x::Rtcc;
|
||||||
use nb::block;
|
use nb::block;
|
||||||
use panic_halt as _;
|
use panic_halt as _;
|
||||||
use storage::SECRET_KEY_MAX_LEN;
|
use storage::SECRET_KEY_MAX_LEN;
|
||||||
use storage::SECRET_KEY_NAME_LEN;
|
use storage::SECRET_KEY_NAME_LEN;
|
||||||
|
|
||||||
const INTERVAL: u64 = 30;
|
|
||||||
|
|
||||||
const ENDL: u8 = 0;
|
const ENDL: u8 = 0;
|
||||||
const OK: u8 = 1;
|
const OK: u8 = 1;
|
||||||
const ERROR: u8 = 2;
|
const ERROR: u8 = 2;
|
||||||
|
@ -31,24 +30,6 @@ const GET_TOKENS: u8 = 40;
|
||||||
const WIPE_TOKENS: u8 = 50;
|
const WIPE_TOKENS: u8 = 50;
|
||||||
const EXIT: u8 = 254;
|
const EXIT: u8 = 254;
|
||||||
|
|
||||||
fn decimal_to_packed_bcd(dec: u8) -> u8 {
|
|
||||||
((dec / 10) << 4) | (dec % 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_otp_token(private_key: &[u8], actual_time: u64) -> u32 {
|
|
||||||
let interval = actual_time / INTERVAL;
|
|
||||||
let msg = interval.to_be_bytes();
|
|
||||||
|
|
||||||
let mut hmac_digest = [0u8; 20];
|
|
||||||
hmac_sha1(private_key, &msg, &mut hmac_digest);
|
|
||||||
|
|
||||||
let start = (hmac_digest[19] & 0xF) as usize;
|
|
||||||
let bytes: [u8; 4] = hmac_digest[start..start + 4].try_into().unwrap();
|
|
||||||
|
|
||||||
let raw_token = u32::from_be_bytes(bytes);
|
|
||||||
((raw_token & 0x7F_FF_FF_FF) % 1_000_000) as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
#[arduino_hal::entry]
|
#[arduino_hal::entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
let dp = arduino_hal::Peripherals::take().unwrap();
|
let dp = arduino_hal::Peripherals::take().unwrap();
|
||||||
|
@ -83,114 +64,112 @@ fn main() -> ! {
|
||||||
let up = pins.d6.into_pull_up_input();
|
let up = pins.d6.into_pull_up_input();
|
||||||
let mut button = button::Button::new(&up, true);
|
let mut button = button::Button::new(&up, true);
|
||||||
|
|
||||||
// button.update();
|
button.update();
|
||||||
// if button.update() == button::Event::Pressed || true {
|
if button.update() == button::Event::Pressed {
|
||||||
// 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...");
|
||||||
// loop {
|
loop {
|
||||||
// // Waiting from a command since the tool
|
// Waiting from a command since the tool
|
||||||
// let cmd = block!(serial.read()).unwrap_or(u8::MAX);
|
let cmd = block!(serial.read()).unwrap_or(u8::MAX);
|
||||||
// display.set_cursor(8, 1);
|
display.set_cursor(8, 1);
|
||||||
// display.write_str("---");
|
display.write_str("---");
|
||||||
// display.set_cursor(8, 1);
|
display.set_cursor(8, 1);
|
||||||
// display.write_u32(cmd as u32);
|
display.write_u32(cmd as u32);
|
||||||
|
|
||||||
// match cmd {
|
match cmd {
|
||||||
// EXIT => {}
|
EXIT => {}
|
||||||
// HANDSHAKE => serial.write(OK).unwrap(),
|
HANDSHAKE => serial.write(OK).unwrap(),
|
||||||
// SET_TIMESTAMP => {
|
SET_TIMESTAMP => {
|
||||||
// serial.write(OK).unwrap();
|
serial.write(OK).unwrap();
|
||||||
// let year = loop {
|
let year = loop {
|
||||||
// let y = block!(serial.read()).unwrap();
|
let y = block!(serial.read()).unwrap();
|
||||||
// serial.write(y).unwrap();
|
serial.write(y).unwrap();
|
||||||
// if block!(serial.read()).unwrap() == OK {
|
if block!(serial.read()).unwrap() == OK {
|
||||||
// break y;
|
break y;
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
// let month = loop {
|
let month = loop {
|
||||||
// let m = block!(serial.read()).unwrap();
|
let m = block!(serial.read()).unwrap();
|
||||||
// serial.write(m).unwrap();
|
serial.write(m).unwrap();
|
||||||
// if block!(serial.read()).unwrap() == OK {
|
if block!(serial.read()).unwrap() == OK {
|
||||||
// break m;
|
break m;
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
// let day = loop {
|
let day = loop {
|
||||||
// let d = block!(serial.read()).unwrap();
|
let d = block!(serial.read()).unwrap();
|
||||||
// serial.write(d).unwrap();
|
serial.write(d).unwrap();
|
||||||
// if block!(serial.read()).unwrap() == OK {
|
if block!(serial.read()).unwrap() == OK {
|
||||||
// break d;
|
break d;
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
// let hours = loop {
|
let hours = loop {
|
||||||
// let h = block!(serial.read()).unwrap();
|
let h = block!(serial.read()).unwrap();
|
||||||
// serial.write(h).unwrap();
|
serial.write(h).unwrap();
|
||||||
// if block!(serial.read()).unwrap() == OK {
|
if block!(serial.read()).unwrap() == OK {
|
||||||
// break h;
|
break h;
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
// let minutes = loop {
|
let minutes = loop {
|
||||||
// let m = block!(serial.read()).unwrap();
|
let m = block!(serial.read()).unwrap();
|
||||||
// serial.write(m).unwrap();
|
serial.write(m).unwrap();
|
||||||
// if block!(serial.read()).unwrap() == OK {
|
if block!(serial.read()).unwrap() == OK {
|
||||||
// break m;
|
break m;
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
// let seconds = loop {
|
let seconds = loop {
|
||||||
// let s = block!(serial.read()).unwrap();
|
let s = block!(serial.read()).unwrap();
|
||||||
// serial.write(s).unwrap();
|
serial.write(s).unwrap();
|
||||||
// if block!(serial.read()).unwrap() == OK {
|
if block!(serial.read()).unwrap() == OK {
|
||||||
// break s;
|
break s;
|
||||||
// }
|
}
|
||||||
// };
|
};
|
||||||
// rtc.set_year(year as u16 + 2000).unwrap();
|
rtc.set_year(year as u16 + 2000).unwrap();
|
||||||
// rtc.set_month(month).unwrap();
|
rtc.set_month(month).unwrap();
|
||||||
// rtc.set_day(day).unwrap();
|
rtc.set_day(day).unwrap();
|
||||||
// rtc.set_hours(ds323x::Hours::H24(hours)).unwrap();
|
rtc.set_hours(ds323x::Hours::H24(hours)).unwrap();
|
||||||
// rtc.set_minutes(minutes).unwrap();
|
rtc.set_minutes(minutes).unwrap();
|
||||||
// rtc.set_seconds(seconds).unwrap();
|
rtc.set_seconds(seconds).unwrap();
|
||||||
// serial.write(OK).unwrap();
|
serial.write(OK).unwrap();
|
||||||
// }
|
}
|
||||||
// ADD_TOKEN => match tokens.search_free() {
|
ADD_TOKEN => match tokens.search_free() {
|
||||||
// Some(index) => {
|
Some(index) => {
|
||||||
// serial.write(OK).unwrap();
|
serial.write(OK).unwrap();
|
||||||
// let mut name_buff = [0u8; SECRET_KEY_NAME_LEN as usize];
|
let mut name_buff = [0u8; SECRET_KEY_NAME_LEN as usize];
|
||||||
// for i in 0..name_buff.len() {
|
for i in 0..name_buff.len() {
|
||||||
// let ch = block!(serial.read()).unwrap();
|
let ch = block!(serial.read()).unwrap();
|
||||||
// if ch == ENDL {
|
if ch == ENDL {
|
||||||
// break;
|
break;
|
||||||
// } else {
|
} else {
|
||||||
// name_buff[i] = ch;
|
name_buff[i] = ch;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// let mut key_buff = [0u8; SECRET_KEY_MAX_LEN as usize];
|
let mut key_buff = [0u8; SECRET_KEY_MAX_LEN as usize];
|
||||||
// for i in 0..key_buff.len() {
|
for i in 0..key_buff.len() {
|
||||||
// let ch = block!(serial.read()).unwrap();
|
let ch = block!(serial.read()).unwrap();
|
||||||
// if ch == ENDL {
|
if ch == ENDL {
|
||||||
// break;
|
break;
|
||||||
// } else {
|
} else {
|
||||||
// key_buff[i] = ch;
|
key_buff[i] = ch;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// match tokens.write(index, &name_buff, &key_buff) {
|
match tokens.write(index, &name_buff, &key_buff) {
|
||||||
// Some(_) => serial.write(OK).unwrap(),
|
Some(_) => serial.write(OK).unwrap(),
|
||||||
// None => serial.write(ERROR).unwrap(),
|
None => serial.write(ERROR).unwrap(),
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// None => serial.write(ERROR).unwrap(),
|
None => serial.write(ERROR).unwrap(),
|
||||||
// },
|
},
|
||||||
// WIPE_TOKENS => {
|
WIPE_TOKENS => {
|
||||||
// serial.write(OK).unwrap();
|
serial.write(OK).unwrap();
|
||||||
// tokens.wipe_all_tokens();
|
tokens.wipe_all_tokens();
|
||||||
// serial.write(OK).unwrap();
|
serial.write(OK).unwrap();
|
||||||
// }
|
}
|
||||||
// _ => {}
|
_ => {}
|
||||||
// u8::MAX => {}
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// loop {}
|
|
||||||
|
|
||||||
match tokens.current {
|
match tokens.current {
|
||||||
Some(index) => {
|
Some(index) => {
|
||||||
|
@ -202,15 +181,6 @@ fn main() -> ! {
|
||||||
let key = &buff_key[..len_key];
|
let key = &buff_key[..len_key];
|
||||||
let dt = datetime::Datetime::from_ds3231(&mut rtc);
|
let dt = datetime::Datetime::from_ds3231(&mut rtc);
|
||||||
|
|
||||||
let datetime::Datetime {
|
|
||||||
year,
|
|
||||||
month,
|
|
||||||
day,
|
|
||||||
hours,
|
|
||||||
minutes,
|
|
||||||
seconds,
|
|
||||||
} = dt;
|
|
||||||
|
|
||||||
let timestamp = dt.unix_epoch();
|
let timestamp = dt.unix_epoch();
|
||||||
|
|
||||||
let token = generate_otp_token(key, timestamp);
|
let token = generate_otp_token(key, timestamp);
|
||||||
|
|
|
@ -70,18 +70,18 @@ impl<'a> Tokens<'a> {
|
||||||
let index_name = index * SECRET_KEY_FULL_LEN;
|
let index_name = index * SECRET_KEY_FULL_LEN;
|
||||||
let index_key = (index * SECRET_KEY_FULL_LEN) + SECRET_KEY_NAME_LEN;
|
let index_key = (index * SECRET_KEY_FULL_LEN) + SECRET_KEY_NAME_LEN;
|
||||||
match self.mem.read(index_name, name) {
|
match self.mem.read(index_name, name) {
|
||||||
Ok(n) => {}
|
Ok(_) => {}
|
||||||
Err(_) => return None,
|
Err(_) => return None,
|
||||||
}
|
}
|
||||||
match self.mem.read(index_key, key) {
|
match self.mem.read(index_key, key) {
|
||||||
Ok(n) => {}
|
Ok(_) => {}
|
||||||
Err(_) => return None,
|
Err(_) => return None,
|
||||||
}
|
}
|
||||||
let mut len_name = name
|
let len_name = name
|
||||||
.iter()
|
.iter()
|
||||||
.position(|&n| n == 0)
|
.position(|&n| n == 0)
|
||||||
.unwrap_or(SECRET_KEY_NAME_LEN as usize);
|
.unwrap_or(SECRET_KEY_NAME_LEN as usize);
|
||||||
let mut len_key = key
|
let len_key = key
|
||||||
.iter()
|
.iter()
|
||||||
.position(|&n| n == 0)
|
.position(|&n| n == 0)
|
||||||
.unwrap_or(SECRET_KEY_MAX_LEN as usize);
|
.unwrap_or(SECRET_KEY_MAX_LEN as usize);
|
||||||
|
|
Loading…
Reference in New Issue