diff --git a/Cargo.lock b/Cargo.lock index f2edc9e..3a5dc40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,7 +12,7 @@ dependencies = [ "avr-hal-generic", "cfg-if 1.0.0", "embedded-hal", - "ufmt", + "ufmt 0.1.2", "void", ] @@ -25,6 +25,12 @@ dependencies = [ "avr-hal-generic", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "avr-device" version = "0.5.0" @@ -60,7 +66,7 @@ dependencies = [ "nb 0.1.3", "paste", "rustversion", - "ufmt", + "ufmt 0.1.2", "void", ] @@ -88,6 +94,26 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "ds323x" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "057e63d03a3beb83d3c9b7a19b799c60e203b0c53f1bbec6a715804907f73a2b" +dependencies = [ + "embedded-hal", + "rtcc", +] + [[package]] name = "embedded-hal" version = "0.2.7" @@ -119,6 +145,25 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +dependencies = [ + "autocfg", +] + [[package]] name = "panic-halt" version = "0.2.0" @@ -155,6 +200,15 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rtcc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3623619ce77c09a7d87cf7c61c5c887b9c7dee8805f66af6c4aa5824be4d9930" +dependencies = [ + "chrono", +] + [[package]] name = "rustversion" version = "1.0.12" @@ -167,8 +221,11 @@ version = "0.1.0" dependencies = [ "arduino-hal", "binascii", + "chrono", + "ds323x", "panic-halt", "sha1_smol", + "ufmt 0.2.0", ] [[package]] @@ -195,7 +252,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "31d3c0c63312dfc9d8e5c71114d617018a19f6058674003c0da29ee8d8036cdd" dependencies = [ "proc-macro-hack", - "ufmt-macros", + "ufmt-macros 0.2.0", + "ufmt-write", +] + +[[package]] +name = "ufmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a64846ec02b57e9108d6469d98d1648782ad6bb150a95a9baac26900bbeab9d" +dependencies = [ + "ufmt-macros 0.3.0", "ufmt-write", ] @@ -211,6 +278,17 @@ dependencies = [ "syn", ] +[[package]] +name = "ufmt-macros" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d337d3be617449165cb4633c8dece429afd83f84051024079f97ad32a9663716" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "ufmt-write" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 5c1744c..d657aeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,13 @@ arduino-hal = { git = "https://github.com/rahix/avr-hal", features = ["arduino- panic-halt = "0.2.0" sha1_smol = "1.0.0" binascii = { version = "0.1", default-features = false, features = ["decode"] } +chrono = { version = "0.4.24", default-features = false } +ds323x = "0.5.0" +ufmt = "0.2.0" [profile.release] lto = true panic = "abort" strip = true opt-level = "z" -codegen-units = 1 \ No newline at end of file +codegen-units = 1 diff --git a/README.md b/README.md new file mode 100644 index 0000000..6b3b935 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# RustyToken + +## Converts any Arduino into a TOTP keys + +You can build a TOTP Key with an Arduino diff --git a/build.sh b/build.sh index 03eca32..5347662 100755 --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -cargo build -Z build-std=core --target avr-atmega328p.json --release +cargo build -Z build-std-features=panic_immediate_abort -Z build-std=core --target avr-atmega328p.json --release diff --git a/src/main.rs b/src/main.rs index e41ab92..3c119d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,15 +3,15 @@ mod crypto; -use arduino_hal::hal::usart::BaudrateArduinoExt; -use arduino_hal::prelude::*; -use arduino_hal::{delay_ms, Usart}; +use arduino_hal::default_serial; +use arduino_hal::delay_ms; use crypto::hmac_sha1; +use ds323x::DateTimeAccess; use panic_halt as _; -const INTERVAL: u64 = 30; -const SECRET_KEY_MAX_LEN: u8 = 32; +const INTERVAL: i64 = 30; +const SECRET_KEY_MAX_LEN: usize = 32; fn process_secret<'a>(secret: &str, output: &mut [u8]) -> usize { // RFC 6238 Doesn't specify a max length for the secret keys, So I chose 32 @@ -39,7 +39,7 @@ fn process_secret<'a>(secret: &str, output: &mut [u8]) -> usize { decoded.len() } -fn generate_otp_token(private_key: &str, actual_time: u64) -> u32 { +fn generate_otp_token(private_key: &str, actual_time: i64) -> u32 { let mut output = [0u8; 50]; let private_key_len = process_secret(private_key, &mut output); @@ -72,22 +72,24 @@ fn main() -> ! { 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), + let mut serial = default_serial!(dp, pins, 9600); + let i2c = arduino_hal::I2c::new( + dp.TWI, + pins.a4.into_pull_up_input(), + pins.a5.into_pull_up_input(), + 50000, ); + let mut rtc = ds323x::Ds323x::new_ds3231(i2c); - // let token = generate_otp_token("holaaaaamundo", 1680155315); - let token = generate_otp_token("FS7J22EHLLSOGKUVJKV2XE7FTIX24JAJ", 1680155315); - let mut output = [0u8; 6]; - token_to_bytes(token, &mut output); + let timestamp = rtc.datetime().unwrap().timestamp(); + let timestamp = 0; - for b in output { - serial.write_byte(b); - } - serial.write_byte('\n' as u8); + ufmt::uwriteln!(&mut serial, "timestamp: {}", timestamp).unwrap(); + + // let token = generate_otp_token("holaaaaamundo", timestamp.try_into().unwrap()); + let token = generate_otp_token("aaaaaaaa", timestamp); + + ufmt::uwriteln!(&mut serial, "token: {}", token).unwrap(); loop { led.toggle();