Initial Commit

not_working
kirbylife 2023-03-30 12:05:33 -06:00
commit 41efcce00f
9 changed files with 2765 additions and 0 deletions

1
.gitignore vendored 100644
View File

@ -0,0 +1 @@
/target

236
Cargo.lock generated 100644
View File

@ -0,0 +1,236 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "arduino-hal"
version = "0.1.0"
source = "git+https://github.com/rahix/avr-hal#fb609ab0d14c5a0a44e2dff3e5e514cb612a529a"
dependencies = [
"atmega-hal",
"avr-device",
"avr-hal-generic",
"cfg-if 1.0.0",
"embedded-hal",
"ufmt",
"void",
]
[[package]]
name = "atmega-hal"
version = "0.1.0"
source = "git+https://github.com/rahix/avr-hal#fb609ab0d14c5a0a44e2dff3e5e514cb612a529a"
dependencies = [
"avr-device",
"avr-hal-generic",
]
[[package]]
name = "avr-device"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "deb45711b7227cb3f799c40c4e0e8beea1765acff8d06a14bb3479c1a549d8b0"
dependencies = [
"avr-device-macros",
"bare-metal",
"cfg-if 1.0.0",
"vcell",
]
[[package]]
name = "avr-device-macros"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87c428a2384981d8460668c48c766ff18e7e91d2d467f7b103151984ee952d38"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "avr-hal-generic"
version = "0.1.0"
source = "git+https://github.com/rahix/avr-hal#fb609ab0d14c5a0a44e2dff3e5e514cb612a529a"
dependencies = [
"avr-device",
"cfg-if 0.1.10",
"embedded-hal",
"embedded-storage",
"nb 0.1.3",
"paste",
"rustversion",
"ufmt",
"void",
]
[[package]]
name = "bare-metal"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603"
[[package]]
name = "binascii"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72"
[[package]]
name = "cfg-if"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "embedded-hal"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff"
dependencies = [
"nb 0.1.3",
"void",
]
[[package]]
name = "embedded-storage"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "723dce4e9f25b6e6c5f35628e144794e5b459216ed7da97b7c4b66cdb3fa82ca"
[[package]]
name = "nb"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f"
dependencies = [
"nb 1.1.0",
]
[[package]]
name = "nb"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
[[package]]
name = "panic-halt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
[[package]]
name = "paste"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79"
[[package]]
name = "proc-macro-hack"
version = "0.5.20+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
[[package]]
name = "proc-macro2"
version = "1.0.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rustversion"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06"
[[package]]
name = "rustytoken"
version = "0.1.0"
dependencies = [
"arduino-hal",
"binascii",
"panic-halt",
"sha1_smol",
]
[[package]]
name = "sha1_smol"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "ufmt"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31d3c0c63312dfc9d8e5c71114d617018a19f6058674003c0da29ee8d8036cdd"
dependencies = [
"proc-macro-hack",
"ufmt-macros",
"ufmt-write",
]
[[package]]
name = "ufmt-macros"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4ab6c92f30c996394a8bd525aef9f03ce01d0d7ac82d81902968057e37dd7d9"
dependencies = [
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "ufmt-write"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69"
[[package]]
name = "unicode-ident"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
[[package]]
name = "vcell"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002"
[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"

14
Cargo.toml 100644
View File

@ -0,0 +1,14 @@
[package]
name = "rustytoken"
version = "0.1.0"
edition = "2021"
[dependencies]
arduino-hal = { git = "https://github.com/rahix/avr-hal", features = ["arduino-uno"] }
panic-halt = "0.2.0"
sha1_smol = "1.0.0"
binascii = { version = "0.1", default-features = false, features = ["decode"] }
[profile.release]
lto = true
panic = "abort"

View File

@ -0,0 +1,30 @@
{
"arch": "avr",
"cpu": "atmega328p",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"env": "",
"executables": true,
"linker": "avr-gcc",
"linker-flavor": "gcc",
"linker-is-gnu": true,
"llvm-target": "avr-unknown-unknown",
"os": "unknown",
"position-independent-executables": false,
"exe-suffix": ".elf",
"eh-frame-header": false,
"pre-link-args": {
"gcc": [
"-mmcu=atmega328p"
]
},
"late-link-args": {
"gcc": [
"-lgcc",
"-lc"
]
},
"target-c-int-width": "16",
"target-endian": "little",
"target-pointer-width": "16",
"vendor": "unknown"
}

2
build.sh 100755
View File

@ -0,0 +1,2 @@
cargo build -Z build-std=core --target avr-atmega328p.json --release
avr-objcopy -O ihex -R .eeprom target/avr-atmega328p/release/rustytoken.elf rustytoken.hex

2339
rustytoken.hex 100644

File diff suppressed because it is too large Load Diff

40
src/crypto.rs 100644
View File

@ -0,0 +1,40 @@
use sha1_smol::Sha1;
const BLOCK_SIZE: usize = 64;
pub fn hmac_sha1(key: &[u8], message: &[u8], output: &mut [u8]) {
// Preprocess the key.
let mut key_padded = [0u8; BLOCK_SIZE];
if key.len() > BLOCK_SIZE {
let mut hash = Sha1::new();
hash.update(key);
let key_digest = hash.digest().bytes();
key_padded[..20].copy_from_slice(&key_digest);
} else {
key_padded[..key.len()].copy_from_slice(key);
}
for byte in &mut key_padded {
*byte ^= 0x36;
}
// Hash the inner padded key and the message.
let mut hash = Sha1::new();
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.
for byte in &mut key_padded {
*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);
}

102
src/main.rs 100644
View File

@ -0,0 +1,102 @@
// #![no_std]
// #![no_main]
mod crypto;
use arduino_hal::hal::usart::BaudrateArduinoExt;
use arduino_hal::prelude::*;
use arduino_hal::{delay_ms, Usart};
use binascii::b32decode;
use crypto::hmac_sha1;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
// use panic_halt as _;
const INTERVAL: u64 = 30;
fn process_secret<'a>(secret: &str, output: &mut [u8]) -> usize {
let mut result = [0u8; 32];
let mut inc = 0;
let offset = 8 - (secret.len() % 8);
for c in secret.chars() {
result[inc] = c.to_ascii_uppercase() as u8;
inc += 1;
}
for _ in 0..offset {
result[inc] = '=' as u8;
inc += 1;
}
let decoded = match binascii::b32decode(&result[..inc], output).ok() {
Some(bytes) => bytes,
None => "".as_bytes(),
};
decoded.len()
}
fn generate_otp_token(private_key: &str, actual_time: u64) -> u64 {
let mut output = [0u8; 200];
let private_key_len = process_secret(private_key, &mut output);
let interval = actual_time / INTERVAL;
let msg = interval.to_be_bytes();
println!("key:\n{private_key:?}");
println!("msg:\n{msg:?}");
let mut hmac_digest = [0u8; 20];
hmac_sha1(&output[..private_key_len], &msg, &mut hmac_digest);
println!("{:?}", hmac_digest);
let start = (hmac_digest[19] & 0xF) as usize;
println!("Start before process: {}", hmac_digest[19]);
println!("start: {start}");
let bytes: [u8; 4] = hmac_digest[start..start + 4].try_into().unwrap();
println!("bytes: {bytes:?}");
let raw_token = u32::from_be_bytes(bytes);
println!("raw token: {raw_token}");
((raw_token & 0x7F_FF_FF_FF) % 1_000_000) as u64
}
fn main() {
// let now = SystemTime::now();
// let elapsed = now.duration_since(UNIX_EPOCH).unwrap();
// let seconds = elapsed.as_secs();
// println!("seconds: {seconds}");
// let otp_token = generate_otp_token("holaaaaamundo", seconds);
// println!("{:06}", otp_token);
// std::thread::sleep(Duration::from_secs(1));
loop {
let now = SystemTime::now();
let elapsed = now.duration_since(UNIX_EPOCH).unwrap();
let seconds = elapsed.as_secs();
let otp_token = generate_otp_token("holaaaaamundo", seconds);
println!("{:06}", otp_token);
std::thread::sleep(Duration::from_secs(1));
}
}
// #[arduino_hal::entry]
// fn main() -> ! {
// let dp = arduino_hal::Peripherals::take().unwrap();
// let pins = arduino_hal::pins!(dp);
//
// let mut led = pins.d13.into_output();
// let mut serial = Usart::new(
// dp.USART0,
// pins.d0,
// pins.d1.into_output(),
// BaudrateArduinoExt::into_baudrate(9600),
// );
//
// loop {
// led.toggle();
// delay_ms(1000);
// }
// }

1
upload.sh 100755
View File

@ -0,0 +1 @@
avrdude -p atmega328p -c arduino -P /dev/ttyACM0 -U flash:w:rustytoken.hex:i