From 7765f3dec32346d12f024a1eebad83d47ad68ae2 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 24 Jun 2022 01:46:09 +0800 Subject: [PATCH] add other hash support; notch back the hmac dependencies our engine is a version older in API rev, so notch back the dependencies so we're all at the same rev --- Cargo.lock | 13 ++++++++----- RELEASE-v0.9.md | 2 +- apps/vault/Cargo.toml | 7 ++++--- apps/vault/src/totp.rs | 24 ++++++++++++++++++------ apps/vault/src/ux/framework.rs | 4 +++- 5 files changed, 34 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3fa31eb18..df1f5df84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3103,13 +3103,15 @@ dependencies = [ [[package]] name = "sha-1" -version = "0.10.0" +version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ + "block-buffer 0.9.0", "cfg-if 1.0.0", "cpufeatures", - "digest 0.10.3", + "digest 0.9.0", + "opaque-debug", ] [[package]] @@ -3912,14 +3914,14 @@ dependencies = [ "cbor", "chrono", "content-plugin-api", - "crypto-common", + "crypto-mac", "ctap-crypto", "digest 0.9.0", "enum-iterator", "gam", "graphics-server", "hex 0.4.3", - "hmac 0.12.1", + "hmac 0.11.0", "ime-plugin-api", "locales", "log", @@ -3933,6 +3935,7 @@ dependencies = [ "random-pick", "rkyv", "sha-1", + "sha2", "subtle", "ticktimer-server", "trng", diff --git a/RELEASE-v0.9.md b/RELEASE-v0.9.md index 354e110fd..386e3476e 100644 --- a/RELEASE-v0.9.md +++ b/RELEASE-v0.9.md @@ -171,7 +171,7 @@ perform the Xous firmware upgrade. This requires running manual update commands, - Autobacklight feature: selectable feature to automatically raise the backlight when keys are pressed. Thanks to gsora for the feature! - More importantly, this adds `crossbeam` into the kernel. This is a somewhat heavyweight item and is not recommended for use in core services, but by including it to drive the auto-backlight feature, we get regular code coverage of the condvar pathway. - +- TOTP integration thanks to blakesmith's PoC TOTP code! ## Roadmap to 1.0 diff --git a/apps/vault/Cargo.toml b/apps/vault/Cargo.toml index 67458527c..c876db5bd 100644 --- a/apps/vault/Cargo.toml +++ b/apps/vault/Cargo.toml @@ -40,11 +40,12 @@ chrono = {version = "0.4.19", default-features = false, features = ["std"]} passwords = "3.1.9" # totp -crypto-common = "0.1.3" -sha-1 = "0.10.0" -hmac = "0.12.1" +crypto-mac = "0.11.1" +sha-1 = "0.9.8" +hmac = "0.11.0" digest = "0.9.0" base32 = "0.4.0" +sha2 = {path="../../services/engine-sha512"} # testing random-pick = {version = "1.2.15", optional = true} diff --git a/apps/vault/src/totp.rs b/apps/vault/src/totp.rs index ba4185861..61f85a627 100644 --- a/apps/vault/src/totp.rs +++ b/apps/vault/src/totp.rs @@ -1,6 +1,6 @@ -use crypto_common::InvalidLength; +use crypto_mac::InvalidKeyLength; use sha1::Sha1; -use hmac::{Hmac, Mac}; +use hmac::{Hmac, Mac, NewMac}; use std::{ convert::TryFrom, time::{SystemTime, SystemTimeError}, @@ -55,7 +55,7 @@ pub(crate) struct TotpEntry { #[derive(Debug)] pub(crate) enum Error { Io(std::io::Error), - DigestLength(InvalidLength), + DigestLength(InvalidKeyLength), } impl From for Error { @@ -64,8 +64,8 @@ impl From for Error { } } -impl From for Error { - fn from(err: InvalidLength) -> Self { +impl From for Error { + fn from(err: InvalidKeyLength) -> Self { Error::DigestLength(err) } } @@ -94,7 +94,19 @@ fn generate_hmac_bytes(unix_timestamp: u64, totp_entry: &TotpEntry) -> Result todo!(), + // note: sha256/sha512 implementations not yet tested, as we have yet to find a site that uses this to test against. + TotpAlgorithm::HmacSha256 => { + let mut mac: Hmac = Hmac::new_from_slice(&totp_entry.shared_secret)?; + mac.update(&unpack_u64(unix_timestamp / totp_entry.step_seconds as u64)); + let hash: &[u8] = &mac.finalize().into_bytes(); + computed_hmac.extend_from_slice(hash); + } + TotpAlgorithm::HmacSha512 => { + let mut mac: Hmac = Hmac::new_from_slice(&totp_entry.shared_secret)?; + mac.update(&unpack_u64(unix_timestamp / totp_entry.step_seconds as u64)); + let hash: &[u8] = &mac.finalize().into_bytes(); + computed_hmac.extend_from_slice(hash); + } } Ok(computed_hmac) diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index 574ea53c9..8c02a80d8 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -556,7 +556,9 @@ impl VaultUx { totp::get_current_unix_time().unwrap_or(0), &totp ).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string()); - write!(box_text, "{}\n{}", item.name, code).ok(); + // why code on top? because the item.name can be very long, and it can wrap which would cause + // the code to become hidden. + write!(box_text, "{}\n{}", code, item.name).ok(); } else { write!(box_text, "{}", t!("vault.error.record_error", xous::LANG)).ok(); }