From 2da0f0b812a43d77bcaebb704261a393c96f227b Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 28 May 2022 18:19:52 +0800 Subject: [PATCH] some progess porting ctap to Xous --- Cargo.lock | 28 ++++++++ apps/vault/Cargo.toml | 6 ++ apps/vault/libraries/cbor/Cargo.toml | 2 +- .../fuzz/fuzz_targets/fuzz_target_cbor.rs | 4 +- apps/vault/libraries/cbor/src/lib.rs | 6 -- apps/vault/libraries/cbor/src/macros.rs | 12 ++-- apps/vault/libraries/cbor/src/reader.rs | 6 +- apps/vault/libraries/cbor/src/values.rs | 6 +- apps/vault/libraries/cbor/src/writer.rs | 2 +- apps/vault/libraries/crypto/Cargo.toml | 5 +- apps/vault/libraries/crypto/src/ec/int256.rs | 4 +- apps/vault/libraries/crypto/src/ecdsa.rs | 4 +- apps/vault/libraries/crypto/src/lib.rs | 44 ------------ apps/vault/libraries/crypto/src/rng256.rs | 34 ++++----- apps/vault/libraries/crypto/tests/aesavs.rs | 2 +- apps/vault/libraries/crypto/tests/asn1.rs | 2 +- .../libraries/crypto/tests/wycheproof.rs | 4 +- .../libraries/persistent_store/src/buffer.rs | 6 +- .../libraries/persistent_store/src/format.rs | 2 +- .../libraries/persistent_store/src/lib.rs | 2 - .../libraries/persistent_store/src/store.rs | 2 +- apps/vault/src/ctap/apdu.rs | 2 +- apps/vault/src/ctap/command.rs | 6 +- apps/vault/src/ctap/ctap1.rs | 48 ++++++------- apps/vault/src/ctap/data_formats.rs | 18 ++--- apps/vault/src/ctap/hid/mod.rs | 10 +-- apps/vault/src/ctap/hid/receive.rs | 4 +- apps/vault/src/ctap/mod.rs | 68 ++++++++++-------- apps/vault/src/ctap/pin_protocol_v1.rs | 72 +++++++++---------- apps/vault/src/ctap/response.rs | 6 +- apps/vault/src/ctap/storage.rs | 18 ++--- apps/vault/src/main.rs | 11 +++ 32 files changed, 218 insertions(+), 228 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 145941b42..227a77c98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,6 +89,12 @@ version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28ae2b3dec75a406790005a200b1bd89785afc02517a00ca99ecfe093ee9e6cf" +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + [[package]] name = "atomic-polyfill" version = "0.1.8" @@ -378,6 +384,10 @@ dependencies = [ "xous-names", ] +[[package]] +name = "cbor" +version = "0.1.0" + [[package]] name = "cc" version = "1.0.66" @@ -701,6 +711,18 @@ dependencies = [ "memchr", ] +[[package]] +name = "ctap-crypto" +version = "0.1.0" +dependencies = [ + "arrayref", + "cbor", + "rand_core 0.5.1", + "subtle", + "trng", + "xous-names", +] + [[package]] name = "ctr" version = "0.7.0" @@ -3592,7 +3614,11 @@ dependencies = [ name = "vault" version = "0.1.0" dependencies = [ + "arrayref", + "byteorder", + "cbor", "content-plugin-api", + "ctap-crypto", "gam", "graphics-server", "ime-plugin-api", @@ -3602,6 +3628,8 @@ dependencies = [ "log-server", "num-derive", "num-traits", + "pddb", + "subtle", "tts-frontend", "usb-device-xous", "usbd-human-interface-device 0.1.1 (git+https://github.com/betrusted-io/usbd-human-interface-device.git?branch=main)", diff --git a/apps/vault/Cargo.toml b/apps/vault/Cargo.toml index b1147ca47..b7c13e505 100644 --- a/apps/vault/Cargo.toml +++ b/apps/vault/Cargo.toml @@ -17,10 +17,16 @@ locales = {path = "../../locales"} tts-frontend = {path="../../services/tts"} usb-device-xous = {path="../../services/usb-device-xous"} usbd-human-interface-device = {git="https://github.com/betrusted-io/usbd-human-interface-device.git", branch="main"} +pddb = {path = "../../services/pddb" } ime-plugin-api = {path = "../../services/ime-plugin-api"} ime-plugin-shell = {path = "../../services/ime-plugin-shell"} content-plugin-api = {path = "../../services/content-plugin-api"} # all content canvas providers must provide this API +ctap-crypto = {path = "libraries/crypto"} +cbor = {path = "libraries/cbor"} +byteorder = { version = "1.4.3", default-features = false } +arrayref = "0.3.6" +subtle = { version = "2.2.3", default-features = false, features = ["nightly"] } [features] tts = [] diff --git a/apps/vault/libraries/cbor/Cargo.toml b/apps/vault/libraries/cbor/Cargo.toml index bcae013d7..f8a71f33d 100644 --- a/apps/vault/libraries/cbor/Cargo.toml +++ b/apps/vault/libraries/cbor/Cargo.toml @@ -12,4 +12,4 @@ edition = "2018" [dependencies] [features] -std = [] + diff --git a/apps/vault/libraries/cbor/fuzz/fuzz_targets/fuzz_target_cbor.rs b/apps/vault/libraries/cbor/fuzz/fuzz_targets/fuzz_target_cbor.rs index 2f7e870d8..ec71768eb 100644 --- a/apps/vault/libraries/cbor/fuzz/fuzz_targets/fuzz_target_cbor.rs +++ b/apps/vault/libraries/cbor/fuzz/fuzz_targets/fuzz_target_cbor.rs @@ -1,7 +1,5 @@ #![no_main] -extern crate alloc; - -use alloc::vec::Vec; +use std::vec::Vec; use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { diff --git a/apps/vault/libraries/cbor/src/lib.rs b/apps/vault/libraries/cbor/src/lib.rs index 0a128fc3f..462a6bea1 100644 --- a/apps/vault/libraries/cbor/src/lib.rs +++ b/apps/vault/libraries/cbor/src/lib.rs @@ -12,12 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![cfg_attr(not(feature = "std"), no_std)] - -extern crate alloc; -#[cfg(feature = "std")] -extern crate core; - pub mod macros; pub mod reader; pub mod values; diff --git a/apps/vault/libraries/cbor/src/macros.rs b/apps/vault/libraries/cbor/src/macros.rs index 40669d156..368c83076 100644 --- a/apps/vault/libraries/cbor/src/macros.rs +++ b/apps/vault/libraries/cbor/src/macros.rs @@ -13,7 +13,7 @@ // limitations under the License. use crate::values::{KeyType, Value}; -use alloc::collections::btree_map; +use std::collections::btree_map; use core::cmp::Ordering; use core::iter::Peekable; @@ -41,7 +41,7 @@ use core::iter::Peekable; /// # use cbor::destructure_cbor_map; /// # /// # fn main() { -/// # let map = alloc::collections::BTreeMap::new(); +/// # let map = std::collections::BTreeMap::new(); /// destructure_cbor_map! { /// let { /// 1 => x, @@ -55,7 +55,7 @@ use core::iter::Peekable; /// # extern crate alloc; /// # /// # fn main() { -/// # let mut map = alloc::collections::BTreeMap::::new(); +/// # let mut map = std::collections::BTreeMap::::new(); /// use cbor::values::IntoCborKey; /// let x: Option = map.remove(&1.into_cbor_key()); /// let y: Option = map.remove(&"key".into_cbor_key()); @@ -157,7 +157,7 @@ macro_rules! cbor_map { // The import is unused if the list is empty. #[allow(unused_imports)] use $crate::values::{IntoCborKey, IntoCborValue}; - let mut _map = ::alloc::collections::BTreeMap::new(); + let mut _map = ::std::collections::BTreeMap::new(); $( _map.insert($key.into_cbor_key(), $value.into_cbor_value()); )* @@ -178,7 +178,7 @@ macro_rules! cbor_map_options { // The import is unused if the list is empty. #[allow(unused_imports)] use $crate::values::{IntoCborKey, IntoCborValueOption}; - let mut _map = ::alloc::collections::BTreeMap::<_, $crate::values::Value>::new(); + let mut _map = ::std::collections::BTreeMap::<_, $crate::values::Value>::new(); $( { let opt: Option<$crate::values::Value> = $value.into_cbor_value_option(); @@ -329,7 +329,7 @@ macro_rules! cbor_key_bytes { #[cfg(test)] mod test { use super::super::values::{KeyType, SimpleValue, Value}; - use alloc::collections::BTreeMap; + use std::collections::BTreeMap; #[test] fn test_cbor_simple_values() { diff --git a/apps/vault/libraries/cbor/src/reader.rs b/apps/vault/libraries/cbor/src/reader.rs index a1061a026..a955a1df0 100644 --- a/apps/vault/libraries/cbor/src/reader.rs +++ b/apps/vault/libraries/cbor/src/reader.rs @@ -14,9 +14,9 @@ use super::values::{Constants, KeyType, SimpleValue, Value}; use crate::{cbor_array_vec, cbor_bytes_lit, cbor_map_btree, cbor_text, cbor_unsigned}; -use alloc::collections::BTreeMap; -use alloc::str; -use alloc::vec::Vec; +use std::collections::BTreeMap; +use std::str; +use std::vec::Vec; #[derive(Debug, PartialEq)] pub enum DecoderError { diff --git a/apps/vault/libraries/cbor/src/values.rs b/apps/vault/libraries/cbor/src/values.rs index b20d10973..3c9a9f67a 100644 --- a/apps/vault/libraries/cbor/src/values.rs +++ b/apps/vault/libraries/cbor/src/values.rs @@ -12,9 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use alloc::collections::BTreeMap; -use alloc::string::{String, ToString}; -use alloc::vec::Vec; +use std::collections::BTreeMap; +use std::string::{String, ToString}; +use std::vec::Vec; use core::cmp::Ordering; #[derive(Clone, Debug, PartialEq)] diff --git a/apps/vault/libraries/cbor/src/writer.rs b/apps/vault/libraries/cbor/src/writer.rs index 592048d06..3cde113d7 100644 --- a/apps/vault/libraries/cbor/src/writer.rs +++ b/apps/vault/libraries/cbor/src/writer.rs @@ -13,7 +13,7 @@ // limitations under the License. use super::values::{Constants, KeyType, Value}; -use alloc::vec::Vec; +use std::vec::Vec; pub fn write(value: Value, encoded_cbor: &mut Vec) -> bool { let mut writer = Writer::new(encoded_cbor); diff --git a/apps/vault/libraries/crypto/Cargo.toml b/apps/vault/libraries/crypto/Cargo.toml index b09d15b34..fb6f163b4 100644 --- a/apps/vault/libraries/crypto/Cargo.toml +++ b/apps/vault/libraries/crypto/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "crypto" +name = "ctap-crypto" version = "0.1.0" authors = [ "Fabian Kaczmarczyck ", @@ -13,3 +13,6 @@ edition = "2018" cbor = { path = "../cbor" } arrayref = "0.3.6" subtle = { version = "2.2.3", default-features = false} +trng = {path = "../../../../services/trng"} +xous-names = {path = "../../../../services/xous-names"} +rand_core = "0.5.1" \ No newline at end of file diff --git a/apps/vault/libraries/crypto/src/ec/int256.rs b/apps/vault/libraries/crypto/src/ec/int256.rs index 9954c3725..8d89a8cb9 100644 --- a/apps/vault/libraries/crypto/src/ec/int256.rs +++ b/apps/vault/libraries/crypto/src/ec/int256.rs @@ -13,8 +13,8 @@ // limitations under the License. use super::super::rng256::Rng256; -use alloc::vec; -use alloc::vec::Vec; +use std::vec; +use std::vec::Vec; use arrayref::{array_mut_ref, array_ref}; use byteorder::{BigEndian, ByteOrder}; use core::ops::{Add, AddAssign, Sub, SubAssign}; diff --git a/apps/vault/libraries/crypto/src/ecdsa.rs b/apps/vault/libraries/crypto/src/ecdsa.rs index 52949e317..448ef8ff8 100644 --- a/apps/vault/libraries/crypto/src/ecdsa.rs +++ b/apps/vault/libraries/crypto/src/ecdsa.rs @@ -19,8 +19,8 @@ use super::ec::point::PointP256; use super::hmac::hmac_256; use super::rng256::Rng256; use super::{Hash256, HashBlockSize64Bytes}; -use alloc::vec; -use alloc::vec::Vec; +use std::vec; +use std::vec::Vec; #[cfg(feature = "std")] use arrayref::array_ref; use arrayref::{array_mut_ref, mut_array_refs}; diff --git a/apps/vault/libraries/crypto/src/lib.rs b/apps/vault/libraries/crypto/src/lib.rs index 7b35e990d..7b0fdbbba 100644 --- a/apps/vault/libraries/crypto/src/lib.rs +++ b/apps/vault/libraries/crypto/src/lib.rs @@ -12,48 +12,4 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![cfg_attr(not(feature = "std"), no_std)] -#![feature(wrapping_int_impl)] - -extern crate alloc; - -pub mod aes256; -pub mod cbc; -mod ec; -pub mod ecdh; -pub mod ecdsa; -pub mod hmac; pub mod rng256; -pub mod sha256; -pub mod util; - -// Trait for hash functions that returns a 256-bit hash. -// The type must be Sized (size known at compile time) so that we can instanciate one on the stack -// in the hash() method. -pub trait Hash256: Sized { - fn new() -> Self; - fn update(&mut self, contents: &[u8]); - fn finalize(self) -> [u8; 32]; - - fn hash(contents: &[u8]) -> [u8; 32] { - let mut h = Self::new(); - h.update(contents); - h.finalize() - } -} - -// Traits for block ciphers that operate on 16-byte blocks. -pub trait Encrypt16BytesBlock { - fn encrypt_block(&self, block: &mut [u8; 16]); -} - -pub trait Decrypt16BytesBlock { - fn decrypt_block(&self, block: &mut [u8; 16]); -} - -// Trait for hash functions that operate on 64-byte input blocks. -pub trait HashBlockSize64Bytes { - type State; - - fn hash_block(state: &mut Self::State, block: &[u8; 64]); -} diff --git a/apps/vault/libraries/crypto/src/rng256.rs b/apps/vault/libraries/crypto/src/rng256.rs index 87300b8fe..274597fc8 100644 --- a/apps/vault/libraries/crypto/src/rng256.rs +++ b/apps/vault/libraries/crypto/src/rng256.rs @@ -13,7 +13,7 @@ // limitations under the License. use arrayref::array_ref; -use libtock_drivers::rng; +use rand_core::RngCore; // Lightweight RNG trait to generate uniformly distributed 256 bits. pub trait Rng256 { @@ -37,30 +37,20 @@ fn bytes_to_u32(bytes: [u8; 32]) -> [u32; 8] { result } -// RNG backed by the TockOS rng driver. -pub struct TockRng256 {} - -impl Rng256 for TockRng256 { - fn gen_uniform_u8x32(&mut self) -> [u8; 32] { - let mut buf: [u8; 32] = [Default::default(); 32]; - rng::fill_buffer(&mut buf); - buf - } +pub struct XousRng256 { + trng: trng::Trng, } -// For tests on the desktop, we use the cryptographically secure thread rng as entropy source. -#[cfg(feature = "std")] -pub struct ThreadRng256 {} - -#[cfg(feature = "std")] -impl Rng256 for ThreadRng256 { +impl XousRng256 { + pub fn new(xns: &xous_names::XousNames) -> Self { + XousRng256 { trng: trng::Trng::new(xns).unwrap() } + } +} +impl Rng256 for XousRng256 { fn gen_uniform_u8x32(&mut self) -> [u8; 32] { - use rand::Rng; - - let mut rng = rand::thread_rng(); - let mut result = [Default::default(); 32]; - rng.fill(&mut result); - result + let mut buf: [u8; 32] = [Default::default(); 32]; + self.trng.fill_bytes(&mut buf); + buf } } diff --git a/apps/vault/libraries/crypto/tests/aesavs.rs b/apps/vault/libraries/crypto/tests/aesavs.rs index 5b6731ac9..7ffe87f3a 100644 --- a/apps/vault/libraries/crypto/tests/aesavs.rs +++ b/apps/vault/libraries/crypto/tests/aesavs.rs @@ -16,7 +16,7 @@ /// /// See also https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/aes/AESAVS.pdf use arrayref::array_ref; -use crypto::{aes256, Decrypt16BytesBlock, Encrypt16BytesBlock}; +use ctap_crypto::{aes256, Decrypt16BytesBlock, Encrypt16BytesBlock}; use regex::Regex; use std::fs::File; use std::io::{BufRead, BufReader}; diff --git a/apps/vault/libraries/crypto/tests/asn1.rs b/apps/vault/libraries/crypto/tests/asn1.rs index 6056253a6..e9382a311 100644 --- a/apps/vault/libraries/crypto/tests/asn1.rs +++ b/apps/vault/libraries/crypto/tests/asn1.rs @@ -14,7 +14,7 @@ /// A minimalist parser for ASN.1 encoded ECDSA signatures in DER form. use arrayref::mut_array_refs; -use crypto::ecdsa; +use ctap_crypto::ecdsa; use std::convert::TryFrom; use std::io::Read; diff --git a/apps/vault/libraries/crypto/tests/wycheproof.rs b/apps/vault/libraries/crypto/tests/wycheproof.rs index 910a76883..fbbc11ccd 100644 --- a/apps/vault/libraries/crypto/tests/wycheproof.rs +++ b/apps/vault/libraries/crypto/tests/wycheproof.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crypto::ecdsa; +use ctap_crypto::ecdsa; use serde::Deserialize; use std::collections::HashMap; use std::error::Error; @@ -193,7 +193,7 @@ impl TestCase { pass } Ok(signature) => { - let verified = k.verify_vartime::(&msg, &signature); + let verified = k.verify_vartime::(&msg, &signature); let pass = match self.result { TestResult::acceptable => true, TestResult::valid => verified, diff --git a/apps/vault/libraries/persistent_store/src/buffer.rs b/apps/vault/libraries/persistent_store/src/buffer.rs index ae35089e6..0d0b33300 100644 --- a/apps/vault/libraries/persistent_store/src/buffer.rs +++ b/apps/vault/libraries/persistent_store/src/buffer.rs @@ -13,9 +13,9 @@ // limitations under the License. use crate::{Storage, StorageError, StorageIndex, StorageResult}; -use alloc::borrow::Borrow; -use alloc::boxed::Box; -use alloc::vec; +use std::borrow::Borrow; +use std::boxed::Box; +use std::vec; /// Simulates a flash storage using a buffer in memory. /// diff --git a/apps/vault/libraries/persistent_store/src/format.rs b/apps/vault/libraries/persistent_store/src/format.rs index ae8d93f71..1d8912603 100644 --- a/apps/vault/libraries/persistent_store/src/format.rs +++ b/apps/vault/libraries/persistent_store/src/format.rs @@ -19,7 +19,7 @@ mod bitfield; use self::bitfield::Length; use self::bitfield::{count_zeros, num_bits, Bit, Checksum, ConstField, Field}; use crate::{usize_to_nat, Nat, Storage, StorageIndex, StoreError, StoreResult, StoreUpdate}; -use alloc::vec::Vec; +use std::vec::Vec; use core::cmp::min; use core::convert::TryFrom; diff --git a/apps/vault/libraries/persistent_store/src/lib.rs b/apps/vault/libraries/persistent_store/src/lib.rs index c8be44b46..2be9d1009 100644 --- a/apps/vault/libraries/persistent_store/src/lib.rs +++ b/apps/vault/libraries/persistent_store/src/lib.rs @@ -345,8 +345,6 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[macro_use] -extern crate alloc; #[cfg(feature = "std")] mod buffer; diff --git a/apps/vault/libraries/persistent_store/src/store.rs b/apps/vault/libraries/persistent_store/src/store.rs index 0d5c1e990..7b56aff65 100644 --- a/apps/vault/libraries/persistent_store/src/store.rs +++ b/apps/vault/libraries/persistent_store/src/store.rs @@ -23,7 +23,7 @@ use crate::{usize_to_nat, Nat, Storage, StorageError, StorageIndex}; pub use crate::{ BufferStorage, StoreDriver, StoreDriverOff, StoreDriverOn, StoreInterruption, StoreInvariant, }; -use alloc::vec::Vec; +use std::vec::Vec; use core::cmp::{max, min, Ordering}; #[cfg(feature = "std")] use std::collections::HashSet; diff --git a/apps/vault/src/ctap/apdu.rs b/apps/vault/src/ctap/apdu.rs index 46915441e..6e9bc49b6 100644 --- a/apps/vault/src/ctap/apdu.rs +++ b/apps/vault/src/ctap/apdu.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use alloc::vec::Vec; +use std::vec::Vec; use byteorder::{BigEndian, ByteOrder}; use core::convert::TryFrom; diff --git a/apps/vault/src/ctap/command.rs b/apps/vault/src/ctap/command.rs index ecfae9e63..3c71fb381 100644 --- a/apps/vault/src/ctap/command.rs +++ b/apps/vault/src/ctap/command.rs @@ -21,8 +21,8 @@ use super::data_formats::{ }; use super::key_material; use super::status_code::Ctap2StatusCode; -use alloc::string::String; -use alloc::vec::Vec; +use std::string::String; +use std::vec::Vec; use arrayref::array_ref; use cbor::destructure_cbor_map; use core::convert::TryFrom; @@ -449,7 +449,7 @@ mod test { }; use super::super::ES256_CRED_PARAM; use super::*; - use alloc::collections::BTreeMap; + use std::collections::BTreeMap; use cbor::{cbor_array, cbor_map}; #[test] diff --git a/apps/vault/src/ctap/ctap1.rs b/apps/vault/src/ctap/ctap1.rs index eb5ea134f..7963bdecf 100644 --- a/apps/vault/src/ctap/ctap1.rs +++ b/apps/vault/src/ctap/ctap1.rs @@ -16,11 +16,11 @@ use super::apdu::{Apdu, ApduStatusCode}; use super::hid::ChannelID; use super::status_code::Ctap2StatusCode; use super::CtapState; -use alloc::vec::Vec; +use std::vec::Vec; use arrayref::array_ref; use core::convert::Into; use core::convert::TryFrom; -use crypto::rng256::Rng256; +use ctap_crypto::rng256::Rng256; use libtock_drivers::timer::ClockValue; // For now, they're the same thing with apdu.rs containing the authoritative definition @@ -251,7 +251,7 @@ impl Ctap1Command { R: Rng256, CheckUserPresence: Fn(ChannelID) -> Result<(), Ctap2StatusCode>, { - let sk = crypto::ecdsa::SecKey::gensk(ctap_state.rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(ctap_state.rng); let pk = sk.genpk(); let key_handle = ctap_state .encrypt_key_handle(sk, &application) @@ -288,8 +288,8 @@ impl Ctap1Command { signature_data.extend(key_handle); signature_data.extend_from_slice(&user_pk); - let attestation_key = crypto::ecdsa::SecKey::from_bytes(&private_key).unwrap(); - let signature = attestation_key.sign_rfc6979::(&signature_data); + let attestation_key = ctap_crypto::ecdsa::SecKey::from_bytes(&private_key).unwrap(); + let signature = attestation_key.sign_rfc6979::(&signature_data); response.extend(signature.to_asn1_der()); Ok(response) @@ -336,7 +336,7 @@ impl Ctap1Command { signature_data.extend(&challenge); let signature = credential_source .private_key - .sign_rfc6979::(&signature_data); + .sign_rfc6979::(&signature_data); let mut response = signature_data[application.len()..application.len() + 5].to_vec(); response.extend(signature.to_asn1_der()); @@ -351,8 +351,8 @@ impl Ctap1Command { mod test { use super::super::{key_material, CREDENTIAL_ID_SIZE, USE_SIGNATURE_COUNTER}; use super::*; - use crypto::rng256::ThreadRng256; - use crypto::Hash256; + use ctap_crypto::rng256::ThreadRng256; + use ctap_crypto::Hash256; const CLOCK_FREQUENCY_HZ: usize = 32768; const START_CLOCK_VALUE: ClockValue = ClockValue::new(0, CLOCK_FREQUENCY_HZ); @@ -483,11 +483,11 @@ mod test { fn test_process_authenticate_check_only() { let mut rng = ThreadRng256 {}; let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = crypto::ecdsa::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); let rp_id = "example.com"; - let application = crypto::sha256::Sha256::hash(rp_id.as_bytes()); + let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); let message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); @@ -499,11 +499,11 @@ mod test { fn test_process_authenticate_check_only_wrong_rp() { let mut rng = ThreadRng256 {}; let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = crypto::ecdsa::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); let rp_id = "example.com"; - let application = crypto::sha256::Sha256::hash(rp_id.as_bytes()); + let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); let application = [0x55; 32]; let message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); @@ -516,11 +516,11 @@ mod test { fn test_process_authenticate_check_only_wrong_length() { let mut rng = ThreadRng256 {}; let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = crypto::ecdsa::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); let rp_id = "example.com"; - let application = crypto::sha256::Sha256::hash(rp_id.as_bytes()); + let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); let mut message = create_authenticate_message( &application, @@ -549,11 +549,11 @@ mod test { fn test_process_authenticate_check_only_wrong_cla() { let mut rng = ThreadRng256 {}; let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = crypto::ecdsa::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); let rp_id = "example.com"; - let application = crypto::sha256::Sha256::hash(rp_id.as_bytes()); + let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); let mut message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); @@ -567,11 +567,11 @@ mod test { fn test_process_authenticate_check_only_wrong_ins() { let mut rng = ThreadRng256 {}; let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = crypto::ecdsa::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); let rp_id = "example.com"; - let application = crypto::sha256::Sha256::hash(rp_id.as_bytes()); + let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); let mut message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); @@ -585,11 +585,11 @@ mod test { fn test_process_authenticate_check_only_wrong_flags() { let mut rng = ThreadRng256 {}; let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = crypto::ecdsa::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); let rp_id = "example.com"; - let application = crypto::sha256::Sha256::hash(rp_id.as_bytes()); + let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); let mut message = create_authenticate_message(&application, Ctap1Flags::CheckOnly, &key_handle); @@ -611,11 +611,11 @@ mod test { fn test_process_authenticate_enforce() { let mut rng = ThreadRng256 {}; let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = crypto::ecdsa::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); let rp_id = "example.com"; - let application = crypto::sha256::Sha256::hash(rp_id.as_bytes()); + let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); let message = create_authenticate_message(&application, Ctap1Flags::EnforceUpAndSign, &key_handle); @@ -638,11 +638,11 @@ mod test { fn test_process_authenticate_dont_enforce() { let mut rng = ThreadRng256 {}; let dummy_user_presence = |_| panic!("Unexpected user presence check in CTAP1"); - let sk = crypto::ecdsa::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, dummy_user_presence, START_CLOCK_VALUE); let rp_id = "example.com"; - let application = crypto::sha256::Sha256::hash(rp_id.as_bytes()); + let application = ctap_crypto::sha256::Sha256::hash(rp_id.as_bytes()); let key_handle = ctap_state.encrypt_key_handle(sk, &application).unwrap(); let message = create_authenticate_message( &application, diff --git a/apps/vault/src/ctap/data_formats.rs b/apps/vault/src/ctap/data_formats.rs index a2b490d49..62c6b60b5 100644 --- a/apps/vault/src/ctap/data_formats.rs +++ b/apps/vault/src/ctap/data_formats.rs @@ -13,13 +13,13 @@ // limitations under the License. use super::status_code::Ctap2StatusCode; -use alloc::collections::BTreeMap; -use alloc::string::String; -use alloc::vec::Vec; +use std::collections::BTreeMap; +use std::string::String; +use std::vec::Vec; use arrayref::array_ref; use cbor::{cbor_array_vec, cbor_bytes_lit, cbor_map_options, destructure_cbor_map}; use core::convert::TryFrom; -use crypto::{ecdh, ecdsa}; +use ctap_crypto::{ecdh, ecdsa}; #[cfg(test)] use enum_iterator::IntoEnumIterator; @@ -814,12 +814,12 @@ pub(super) fn ok_or_missing(value_option: Option) -> Result Result, Ctap2StatusCode> { let master_keys = self.persistent_store.master_keys()?; - let aes_enc_key = crypto::aes256::EncryptionKey::new(&master_keys.encryption); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&master_keys.encryption); let mut sk_bytes = [0; 32]; private_key.to_bytes(&mut sk_bytes); let mut iv = [0; 16]; @@ -276,8 +276,8 @@ where ) { return Ok(None); } - let aes_enc_key = crypto::aes256::EncryptionKey::new(&master_keys.encryption); - let aes_dec_key = crypto::aes256::DecryptionKey::new(&aes_enc_key); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&master_keys.encryption); + let aes_dec_key = ctap_crypto::aes256::DecryptionKey::new(&aes_enc_key); let mut iv = [0; 16]; iv.copy_from_slice(&credential_id[..16]); let mut blocks = [[0u8; 16]; 4]; @@ -296,7 +296,7 @@ where return Ok(None); } - let sk_option = crypto::ecdsa::SecKey::from_bytes(&decrypted_sk); + let sk_option = ctap_crypto::ecdsa::SecKey::from_bytes(&decrypted_sk); Ok(sk_option.map(|sk| PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, credential_id, @@ -507,7 +507,7 @@ where (self.check_user_presence)(cid)?; - let sk = crypto::ecdsa::SecKey::gensk(self.rng); + let sk = ctap_crypto::ecdsa::SecKey::gensk(self.rng); let pk = sk.genpk(); let credential_id = if options.rk { @@ -571,18 +571,18 @@ where .attestation_private_key()? .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?; let attestation_key = - crypto::ecdsa::SecKey::from_bytes(&attestation_private_key).unwrap(); + ctap_crypto::ecdsa::SecKey::from_bytes(&attestation_private_key).unwrap(); let attestation_certificate = self .persistent_store .attestation_certificate()? .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?; ( - attestation_key.sign_rfc6979::(&signature_data), + attestation_key.sign_rfc6979::(&signature_data), Some(vec![attestation_certificate]), ) } else { ( - sk.sign_rfc6979::(&signature_data), + sk.sign_rfc6979::(&signature_data), None, ) }; @@ -605,7 +605,7 @@ where // The computation is deterministic, and private_key expected to be unique. fn generate_cred_random( &mut self, - private_key: &crypto::ecdsa::SecKey, + private_key: &ctap_crypto::ecdsa::SecKey, has_uv: bool, ) -> Result<[u8; 32], Ctap2StatusCode> { let mut private_key_bytes = [0u8; 32]; @@ -647,7 +647,7 @@ where signature_data.extend(client_data_hash); let signature = credential .private_key - .sign_rfc6979::(&signature_data); + .sign_rfc6979::(&signature_data); let cred_desc = PublicKeyCredentialDescriptor { key_type: PublicKeyCredentialType::PublicKey, @@ -983,8 +983,14 @@ where #[cfg(not(feature = "with_ctap1"))] let need_certificate = USE_BATCH_ATTESTATION; + let pddb = pddb::Pddb::new(); + // Note that in the OpenSK implementation, this command both causes the + // token to enter a locked down state, and confirms it has entered that state. + // Here, we only query if the user has put the device into a locked down state. + // Perhaps in the future, this should pop up a modal dialog box requesting the + // user to approve a full device lockdown. if (need_certificate && !(response.pkey_programmed && response.cert_programmed)) - || crp::set_protection(crp::ProtectionLevel::FullyLocked).is_err() + || pddb.is_efuse_secured() { return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR); } @@ -1021,7 +1027,7 @@ mod test { }; use super::*; use cbor::cbor_array; - use crypto::rng256::ThreadRng256; + use ctap_crypto::rng256::ThreadRng256; const CLOCK_FREQUENCY_HZ: usize = 32768; const DUMMY_CLOCK_VALUE: ClockValue = ClockValue::new(0, CLOCK_FREQUENCY_HZ); @@ -1236,7 +1242,7 @@ mod test { #[test] fn test_process_make_credential_credential_excluded() { let mut rng = ThreadRng256 {}; - let excluded_private_key = crypto::ecdsa::SecKey::gensk(&mut rng); + let excluded_private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let user_immediately_present = |_| Ok(()); let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); @@ -1532,7 +1538,7 @@ mod test { #[test] fn test_process_get_assertion_hmac_secret() { let mut rng = ThreadRng256 {}; - let sk = crypto::ecdh::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdh::SecKey::gensk(&mut rng); let user_immediately_present = |_| Ok(()); let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); @@ -1599,7 +1605,7 @@ mod test { #[test] fn test_residential_process_get_assertion_hmac_secret() { let mut rng = ThreadRng256 {}; - let sk = crypto::ecdh::SecKey::gensk(&mut rng); + let sk = ctap_crypto::ecdh::SecKey::gensk(&mut rng); let user_immediately_present = |_| Ok(()); let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); @@ -1650,7 +1656,7 @@ mod test { #[test] fn test_residential_process_get_assertion_with_cred_protect() { let mut rng = ThreadRng256 {}; - let private_key = crypto::ecdsa::SecKey::gensk(&mut rng); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let credential_id = rng.gen_uniform_u8x32().to_vec(); let user_immediately_present = |_| Ok(()); let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); @@ -1767,7 +1773,7 @@ mod test { #[test] fn test_process_get_next_assertion_two_credentials_with_uv() { let mut rng = ThreadRng256 {}; - let key_agreement_key = crypto::ecdh::SecKey::gensk(&mut rng); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(&mut rng); let pin_uv_auth_token = [0x88; 32]; let pin_protocol_v1 = PinProtocolV1::new_test(key_agreement_key, pin_uv_auth_token); @@ -1992,7 +1998,7 @@ mod test { fn test_process_reset() { let mut rng = ThreadRng256 {}; let user_immediately_present = |_| Ok(()); - let private_key = crypto::ecdsa::SecKey::gensk(&mut rng); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); let credential_id = vec![0x01, 0x23, 0x45, 0x67]; @@ -2066,7 +2072,7 @@ mod test { fn test_encrypt_decrypt_credential() { let mut rng = ThreadRng256 {}; let user_immediately_present = |_| Ok(()); - let private_key = crypto::ecdsa::SecKey::gensk(&mut rng); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); // Usually, the relying party ID or its hash is provided by the client. @@ -2087,7 +2093,7 @@ mod test { fn test_encrypt_decrypt_bad_hmac() { let mut rng = ThreadRng256 {}; let user_immediately_present = |_| Ok(()); - let private_key = crypto::ecdsa::SecKey::gensk(&mut rng); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let mut ctap_state = CtapState::new(&mut rng, user_immediately_present, DUMMY_CLOCK_VALUE); // Same as above. diff --git a/apps/vault/src/ctap/pin_protocol_v1.rs b/apps/vault/src/ctap/pin_protocol_v1.rs index 410dac72e..689db8dce 100644 --- a/apps/vault/src/ctap/pin_protocol_v1.rs +++ b/apps/vault/src/ctap/pin_protocol_v1.rs @@ -18,16 +18,16 @@ use super::response::{AuthenticatorClientPinResponse, ResponseData}; use super::status_code::Ctap2StatusCode; use super::storage::PersistentStore; #[cfg(feature = "with_ctap2_1")] -use alloc::string::String; -use alloc::vec; -use alloc::vec::Vec; +use std::string::String; +use std::vec; +use std::vec::Vec; use arrayref::array_ref; use core::convert::TryInto; -use crypto::cbc::{cbc_decrypt, cbc_encrypt}; -use crypto::hmac::{hmac_256, verify_hmac_256_first_128bits}; -use crypto::rng256::Rng256; -use crypto::sha256::Sha256; -use crypto::Hash256; +use ctap_crypto::cbc::{cbc_decrypt, cbc_encrypt}; +use ctap_crypto::hmac::{hmac_256, verify_hmac_256_first_128bits}; +use ctap_crypto::rng256::Rng256; +use ctap_crypto::sha256::Sha256; +use ctap_crypto::Hash256; #[cfg(all(test, feature = "with_ctap2_1"))] use enum_iterator::IntoEnumIterator; use subtle::ConstantTimeEq; @@ -61,8 +61,8 @@ fn encrypt_hmac_secret_output( if salt_enc.len() != 32 && salt_enc.len() != 64 { return Err(Ctap2StatusCode::CTAP2_ERR_UNSUPPORTED_EXTENSION); } - let aes_enc_key = crypto::aes256::EncryptionKey::new(shared_secret); - let aes_dec_key = crypto::aes256::DecryptionKey::new(&aes_enc_key); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(shared_secret); + let aes_dec_key = ctap_crypto::aes256::DecryptionKey::new(&aes_enc_key); // The specification specifically asks for a zero IV. let iv = [0u8; 16]; @@ -102,7 +102,7 @@ fn encrypt_hmac_secret_output( /// Decrypts the new_pin_enc and outputs the found PIN. fn decrypt_pin( - aes_dec_key: &crypto::aes256::DecryptionKey, + aes_dec_key: &ctap_crypto::aes256::DecryptionKey, new_pin_enc: Vec, ) -> Option> { if new_pin_enc.len() != PIN_PADDED_LENGTH { @@ -135,7 +135,7 @@ fn decrypt_pin( /// is hashed, truncated to 16 bytes and persistently stored. fn check_and_store_new_pin( persistent_store: &mut PersistentStore, - aes_dec_key: &crypto::aes256::DecryptionKey, + aes_dec_key: &ctap_crypto::aes256::DecryptionKey, new_pin_enc: Vec, ) -> Result<(), Ctap2StatusCode> { let pin = decrypt_pin(aes_dec_key, new_pin_enc) @@ -170,7 +170,7 @@ pub enum PinPermission { } pub struct PinProtocolV1 { - key_agreement_key: crypto::ecdh::SecKey, + key_agreement_key: ctap_crypto::ecdh::SecKey, pin_uv_auth_token: [u8; PIN_TOKEN_LENGTH], consecutive_pin_mismatches: u8, #[cfg(feature = "with_ctap2_1")] @@ -181,7 +181,7 @@ pub struct PinProtocolV1 { impl PinProtocolV1 { pub fn new(rng: &mut impl Rng256) -> PinProtocolV1 { - let key_agreement_key = crypto::ecdh::SecKey::gensk(rng); + let key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(rng); let pin_uv_auth_token = rng.gen_uniform_u8x32(); PinProtocolV1 { key_agreement_key, @@ -201,7 +201,7 @@ impl PinProtocolV1 { &mut self, rng: &mut impl Rng256, persistent_store: &mut PersistentStore, - aes_dec_key: &crypto::aes256::DecryptionKey, + aes_dec_key: &ctap_crypto::aes256::DecryptionKey, pin_hash_enc: Vec, ) -> Result<(), Ctap2StatusCode> { match persistent_store.pin_hash()? { @@ -220,7 +220,7 @@ impl PinProtocolV1 { cbc_decrypt(aes_dec_key, iv, &mut blocks); if !bool::from(pin_hash.ct_eq(&blocks[0])) { - self.key_agreement_key = crypto::ecdh::SecKey::gensk(rng); + self.key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(rng); if persistent_store.pin_retries()? == 0 { return Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED); } @@ -246,16 +246,16 @@ impl PinProtocolV1 { key_agreement: CoseKey, pin_auth: &[u8], authenticated_message: &[u8], - ) -> Result { - let pk: crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; + ) -> Result { + let pk: ctap_crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; let shared_secret = self.key_agreement_key.exchange_x_sha256(&pk); if !verify_pin_auth(&shared_secret, authenticated_message, pin_auth) { return Err(Ctap2StatusCode::CTAP2_ERR_PIN_AUTH_INVALID); } - let aes_enc_key = crypto::aes256::EncryptionKey::new(&shared_secret); - let aes_dec_key = crypto::aes256::DecryptionKey::new(&aes_enc_key); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&shared_secret); + let aes_dec_key = ctap_crypto::aes256::DecryptionKey::new(&aes_enc_key); Ok(aes_dec_key) } @@ -329,11 +329,11 @@ impl PinProtocolV1 { if persistent_store.pin_retries()? == 0 { return Err(Ctap2StatusCode::CTAP2_ERR_PIN_BLOCKED); } - let pk: crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; + let pk: ctap_crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; let shared_secret = self.key_agreement_key.exchange_x_sha256(&pk); - let token_encryption_key = crypto::aes256::EncryptionKey::new(&shared_secret); - let pin_decryption_key = crypto::aes256::DecryptionKey::new(&token_encryption_key); + let token_encryption_key = ctap_crypto::aes256::EncryptionKey::new(&shared_secret); + let pin_decryption_key = ctap_crypto::aes256::DecryptionKey::new(&token_encryption_key); self.verify_pin_hash_enc(rng, persistent_store, &pin_decryption_key, pin_hash_enc)?; // Assuming PIN_TOKEN_LENGTH % block_size == 0 here. @@ -568,7 +568,7 @@ impl PinProtocolV1 { } pub fn reset(&mut self, rng: &mut impl Rng256) { - self.key_agreement_key = crypto::ecdh::SecKey::gensk(rng); + self.key_agreement_key = ctap_crypto::ecdh::SecKey::gensk(rng); self.pin_uv_auth_token = rng.gen_uniform_u8x32(); self.consecutive_pin_mismatches = 0; #[cfg(feature = "with_ctap2_1")] @@ -588,7 +588,7 @@ impl PinProtocolV1 { salt_enc, salt_auth, } = hmac_secret_input; - let pk: crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; + let pk: ctap_crypto::ecdh::PubKey = CoseKey::try_into(key_agreement)?; let shared_secret = self.key_agreement_key.exchange_x_sha256(&pk); // HMAC-secret does the same 16 byte truncated check. if !verify_pin_auth(&shared_secret, &salt_enc, &salt_auth) { @@ -622,7 +622,7 @@ impl PinProtocolV1 { #[cfg(test)] pub fn new_test( - key_agreement_key: crypto::ecdh::SecKey, + key_agreement_key: ctap_crypto::ecdh::SecKey, pin_uv_auth_token: [u8; 32], ) -> PinProtocolV1 { PinProtocolV1 { @@ -640,7 +640,7 @@ impl PinProtocolV1 { #[cfg(test)] mod test { use super::*; - use crypto::rng256::ThreadRng256; + use ctap_crypto::rng256::ThreadRng256; // Stores a PIN hash corresponding to the dummy PIN "1234". fn set_standard_pin(persistent_store: &mut PersistentStore) { @@ -659,7 +659,7 @@ mod test { for i in 0..block_len { blocks[i][..].copy_from_slice(&message[i * 16..(i + 1) * 16]); } - let aes_enc_key = crypto::aes256::EncryptionKey::new(shared_secret); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(shared_secret); let iv = [0u8; 16]; cbc_encrypt(&aes_enc_key, iv, &mut blocks); blocks.iter().flatten().cloned().collect::>() @@ -673,8 +673,8 @@ mod test { for i in 0..block_len { blocks[i][..].copy_from_slice(&message[i * 16..(i + 1) * 16]); } - let aes_enc_key = crypto::aes256::EncryptionKey::new(shared_secret); - let aes_dec_key = crypto::aes256::DecryptionKey::new(&aes_enc_key); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(shared_secret); + let aes_dec_key = ctap_crypto::aes256::DecryptionKey::new(&aes_enc_key); let iv = [0u8; 16]; cbc_decrypt(&aes_dec_key, iv, &mut blocks); blocks.iter().flatten().cloned().collect::>() @@ -712,8 +712,8 @@ mod test { ]; persistent_store.set_pin_hash(&pin_hash).unwrap(); let shared_secret = [0x88; 32]; - let aes_enc_key = crypto::aes256::EncryptionKey::new(&shared_secret); - let aes_dec_key = crypto::aes256::DecryptionKey::new(&aes_enc_key); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&shared_secret); + let aes_dec_key = ctap_crypto::aes256::DecryptionKey::new(&aes_enc_key); let mut pin_protocol_v1 = PinProtocolV1::new(&mut rng); let pin_hash_enc = vec![ @@ -1073,8 +1073,8 @@ mod test { #[test] fn test_decrypt_pin() { let shared_secret = [0x88; 32]; - let aes_enc_key = crypto::aes256::EncryptionKey::new(&shared_secret); - let aes_dec_key = crypto::aes256::DecryptionKey::new(&aes_enc_key); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&shared_secret); + let aes_dec_key = ctap_crypto::aes256::DecryptionKey::new(&aes_enc_key); // "1234" let new_pin_enc = vec![ @@ -1116,8 +1116,8 @@ mod test { let mut rng = ThreadRng256 {}; let mut persistent_store = PersistentStore::new(&mut rng); let shared_secret = [0x88; 32]; - let aes_enc_key = crypto::aes256::EncryptionKey::new(&shared_secret); - let aes_dec_key = crypto::aes256::DecryptionKey::new(&aes_enc_key); + let aes_enc_key = ctap_crypto::aes256::EncryptionKey::new(&shared_secret); + let aes_dec_key = ctap_crypto::aes256::DecryptionKey::new(&aes_enc_key); let test_cases = vec![ // Accept PIN "1234". diff --git a/apps/vault/src/ctap/response.rs b/apps/vault/src/ctap/response.rs index 642295969..d7fec6788 100644 --- a/apps/vault/src/ctap/response.rs +++ b/apps/vault/src/ctap/response.rs @@ -18,9 +18,9 @@ use super::data_formats::{ CoseKey, CredentialProtectionPolicy, PackedAttestationStatement, PublicKeyCredentialDescriptor, PublicKeyCredentialUserEntity, }; -use alloc::collections::BTreeMap; -use alloc::string::String; -use alloc::vec::Vec; +use std::collections::BTreeMap; +use std::string::String; +use std::vec::Vec; use cbor::{cbor_array_vec, cbor_bool, cbor_map_btree, cbor_map_options, cbor_text}; #[cfg_attr(test, derive(PartialEq))] diff --git a/apps/vault/src/ctap/storage.rs b/apps/vault/src/ctap/storage.rs index a70132513..72427ee27 100644 --- a/apps/vault/src/ctap/storage.rs +++ b/apps/vault/src/ctap/storage.rs @@ -23,14 +23,14 @@ use crate::ctap::status_code::Ctap2StatusCode; use crate::ctap::INITIAL_SIGNATURE_COUNTER; use crate::embedded_flash::{new_storage, Storage}; #[cfg(feature = "with_ctap2_1")] -use alloc::string::String; -use alloc::vec; -use alloc::vec::Vec; +use std::string::String; +use std::vec; +use std::vec::Vec; use arrayref::array_ref; #[cfg(feature = "with_ctap2_1")] use cbor::cbor_array_vec; use core::convert::TryInto; -use crypto::rng256::Rng256; +use ctap_crypto::rng256::Rng256; // Those constants may be modified before compilation to tune the behavior of the key. // @@ -608,14 +608,14 @@ fn _serialize_min_pin_length_rp_ids(rp_ids: Vec) -> Result, Ctap mod test { use super::*; use crate::ctap::data_formats::{PublicKeyCredentialSource, PublicKeyCredentialType}; - use crypto::rng256::{Rng256, ThreadRng256}; + use ctap_crypto::rng256::{Rng256, ThreadRng256}; fn create_credential_source( rng: &mut ThreadRng256, rp_id: &str, user_handle: Vec, ) -> PublicKeyCredentialSource { - let private_key = crypto::ecdsa::SecKey::gensk(rng); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(rng); PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, credential_id: rng.gen_uniform_u8x32().to_vec(), @@ -771,7 +771,7 @@ mod test { let mut rng = ThreadRng256 {}; let mut persistent_store = PersistentStore::new(&mut rng); assert_eq!(persistent_store.count_credentials().unwrap(), 0); - let private_key = crypto::ecdsa::SecKey::gensk(&mut rng); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let credential = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, credential_id: rng.gen_uniform_u8x32().to_vec(), @@ -837,7 +837,7 @@ mod test { let mut rng = ThreadRng256 {}; let mut persistent_store = PersistentStore::new(&mut rng); assert_eq!(persistent_store.count_credentials().unwrap(), 0); - let private_key = crypto::ecdsa::SecKey::gensk(&mut rng); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let credential = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, credential_id: rng.gen_uniform_u8x32().to_vec(), @@ -1062,7 +1062,7 @@ mod test { #[test] fn test_serialize_deserialize_credential() { let mut rng = ThreadRng256 {}; - let private_key = crypto::ecdsa::SecKey::gensk(&mut rng); + let private_key = ctap_crypto::ecdsa::SecKey::gensk(&mut rng); let credential = PublicKeyCredentialSource { key_type: PublicKeyCredentialType::PublicKey, credential_id: rng.gen_uniform_u8x32().to_vec(), diff --git a/apps/vault/src/main.rs b/apps/vault/src/main.rs index 544daeb1b..5422d85d4 100644 --- a/apps/vault/src/main.rs +++ b/apps/vault/src/main.rs @@ -8,6 +8,13 @@ use xous_ipc::Buffer; use usbd_human_interface_device::device::fido::*; use std::thread; +mod ctap; +use ctap::hid::{ChannelID, CtapHid, KeepaliveStatus, ProcessedPacket}; +use ctap::status_code::Ctap2StatusCode; +use ctap::CtapState; +mod shims; +use shims::*; + /* UI concept: @@ -87,6 +94,10 @@ fn xmain() -> ! { } }); + let rng = ctap_ctap_crypto::rng256::XousRng256::new(&xns); + let mut ctap_state = CtapState::new(&mut rng, check_user_presence, boot_time); + //let mut ctap_hid = CtapHid::new(); + let mut repl = Repl::new(&xns, sid); let mut update_repl = true; let mut was_callback = false;