diff --git a/Cargo.lock b/Cargo.lock index 8af2b666e..0cc7c79b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -505,7 +505,7 @@ dependencies = [ [[package]] name = "curve25519-dalek" version = "3.1.0" -source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#1a79b76e90d49366b47eb98398a6104678686c15" +source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#b8f968b6e45816d3f5148eb3fb9e10803fedd3f9" dependencies = [ "byteorder", "digest", @@ -1638,6 +1638,7 @@ dependencies = [ "rand_core 0.5.1", "rkyv 0.4.3", "rtc", + "sha2", "susres", "ticktimer-server", "trng", @@ -1787,6 +1788,21 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "test-stub" +version = "0.1.0" +dependencies = [ + "log", + "log-server", + "num-derive", + "num-traits", + "susres", + "ticktimer-server", + "utralib", + "xous", + "xous-names", +] + [[package]] name = "textwrap" version = "0.11.0" diff --git a/Cargo.toml b/Cargo.toml index b407b53e3..550cc607d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ default-members = [ "services/engine-sha512", "services/engine-25519", "services/aes", + "services/test-stub", ] members = [ "xous-ipc", @@ -67,6 +68,7 @@ members = [ "services/engine-sha512", "services/engine-25519", "services/aes", + "services/test-stub", ] resolver = "2" @@ -92,10 +94,11 @@ xous = {path = "xous-rs"} xous-macros = {path = "macros"} # prefer hardware-accelerated versions of services sha2 = {path = "services/engine-sha512", package = "engine-sha512"} + [patch.crates-io.curve25519-dalek] git="https://github.com/betrusted-io/curve25519-dalek.git" branch="main" -#path = "../curve25519-dalek" +#path = "../curve25519-dalek" # when doing local dev work default-features = false features = ["u32_backend", "betrusted"] diff --git a/buildpush.bat b/buildpush.bat index 06a38480a..7248e42d7 100644 --- a/buildpush.bat +++ b/buildpush.bat @@ -1,6 +1,7 @@ REM a crappy starter script to help with builds under Windows environments. ymmv. cargo xtask hw-image precursors/soc.svd +REM cargo xtask minimal precursors/soc.svd CertUtil -hashfile precursors/soc_csr.bin MD5 CertUtil -hashfile target/riscv32imac-unknown-none-elf/release/loader.bin MD5 diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 1ba297851..3d6b22433 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -40,11 +40,9 @@ rkyv = {version = "0.4.3", default-features = false, features = ["const_generics # hardware acceleration adaptations are inserted into a fork of the main branch. rand_core = "0.5" hex = {version = "0.4.3", default-features = false, features = []} +sha2 = {version = "0.9.5", default-features = false, features = []} [dependencies.curve25519-dalek] -# don't forget the Cargo.toml patch.crates.io file to patch over third-party dependencies as well! -git="https://github.com/betrusted-io/curve25519-dalek.git" -branch="main" -#path = "../../../curve25519-dalek" +version = "3.1.0" # note this is patched to our fork in ./Cargo.toml default-features = false features = ["u32_backend", "betrusted"] diff --git a/services/shellchat/src/cmds/engine.rs b/services/shellchat/src/cmds/engine.rs index bae8449c5..dbae78d45 100644 --- a/services/shellchat/src/cmds/engine.rs +++ b/services/shellchat/src/cmds/engine.rs @@ -293,6 +293,83 @@ impl<'a> ShellCmdApi<'a> for Engine { } else { write!(ret, "x25519 key exchange fail").unwrap(); } + + /////////////////////// fixed vectors from x25519-dalek tests + use curve25519_dalek::scalar::Scalar; + use curve25519_dalek::montgomery::MontgomeryPoint; + /// "Decode" a scalar from a 32-byte array. + /// + /// By "decode" here, what is really meant is applying key clamping by twiddling + /// some bits. + /// + /// # Returns + /// + /// A `Scalar`. + fn clamp_scalar(mut scalar: [u8; 32]) -> Scalar { + scalar[0] &= 248; + scalar[31] &= 127; + scalar[31] |= 64; + + Scalar::from_bits(scalar) + } + + /// The bare, byte-oriented x25519 function, exactly as specified in RFC7748. + /// + /// This can be used with [`X25519_BASEPOINT_BYTES`] for people who + /// cannot use the better, safer, and faster DH API. + fn x25519(k: [u8; 32], u: [u8; 32]) -> [u8; 32] { + (clamp_scalar(k) * MontgomeryPoint(u)).to_bytes() + } + { + let input_scalar: [u8; 32] = [ + 0xa5, 0x46, 0xe3, 0x6b, 0xf0, 0x52, 0x7c, 0x9d, 0x3b, 0x16, 0x15, 0x4b, 0x82, 0x46, + 0x5e, 0xdd, 0x62, 0x14, 0x4c, 0x0a, 0xc1, 0xfc, 0x5a, 0x18, 0x50, 0x6a, 0x22, 0x44, + 0xba, 0x44, 0x9a, 0xc4, + ]; + let input_point: [u8; 32] = [ + 0xe6, 0xdb, 0x68, 0x67, 0x58, 0x30, 0x30, 0xdb, 0x35, 0x94, 0xc1, 0xa4, 0x24, 0xb1, + 0x5f, 0x7c, 0x72, 0x66, 0x24, 0xec, 0x26, 0xb3, 0x35, 0x3b, 0x10, 0xa9, 0x03, 0xa6, + 0xd0, 0xab, 0x1c, 0x4c, + ]; + let expected: [u8; 32] = [ + 0xc3, 0xda, 0x55, 0x37, 0x9d, 0xe9, 0xc6, 0x90, 0x8e, 0x94, 0xea, 0x4d, 0xf2, 0x8d, + 0x08, 0x4f, 0x32, 0xec, 0xcf, 0x03, 0x49, 0x1c, 0x71, 0xf7, 0x54, 0xb4, 0x07, 0x55, + 0x77, 0xa2, 0x85, 0x52, + ]; + let result = x25519(input_scalar, input_point); + if result != expected { + write!(ret, "x25519 vector 1 failed!\n").unwrap(); + log::error!("result: {:x?}", result); + log::error!("expected: {:x?}", expected); + } else { + write!(ret, "x25519 vector 1 passed\n").unwrap(); + } + } + { + let input_scalar: [u8; 32] = [ + 0x4b, 0x66, 0xe9, 0xd4, 0xd1, 0xb4, 0x67, 0x3c, 0x5a, 0xd2, 0x26, 0x91, 0x95, 0x7d, + 0x6a, 0xf5, 0xc1, 0x1b, 0x64, 0x21, 0xe0, 0xea, 0x01, 0xd4, 0x2c, 0xa4, 0x16, 0x9e, + 0x79, 0x18, 0xba, 0x0d, + ]; + let input_point: [u8; 32] = [ + 0xe5, 0x21, 0x0f, 0x12, 0x78, 0x68, 0x11, 0xd3, 0xf4, 0xb7, 0x95, 0x9d, 0x05, 0x38, + 0xae, 0x2c, 0x31, 0xdb, 0xe7, 0x10, 0x6f, 0xc0, 0x3c, 0x3e, 0xfc, 0x4c, 0xd5, 0x49, + 0xc7, 0x15, 0xa4, 0x93, + ]; + let expected: [u8; 32] = [ + 0x95, 0xcb, 0xde, 0x94, 0x76, 0xe8, 0x90, 0x7d, 0x7a, 0xad, 0xe4, 0x5c, 0xb4, 0xb8, + 0x73, 0xf8, 0x8b, 0x59, 0x5a, 0x68, 0x79, 0x9f, 0xa1, 0x52, 0xe6, 0xf8, 0xf7, 0x64, + 0x7a, 0xac, 0x79, 0x57, + ]; + let result = x25519(input_scalar, input_point); + if result != expected { + write!(ret, "x25519 vector 2 failed!\n").unwrap(); + log::error!("result: {:x?}", result); + log::error!("expected: {:x?}", expected); + } else { + write!(ret, "x25519 vector 2 passed\n").unwrap(); + } + } } "ed2" => { use ed25519_dalek::{Keypair, Signature, Signer}; @@ -342,8 +419,10 @@ impl<'a> ShellCmdApi<'a> for Engine { let keypair: Keypair = Keypair{ secret: secret, public: public }; let sig1: Signature = Signature::from_bytes(&sig_bytes[..]).unwrap(); - let mut prehash_for_signing = engine_sha512::Sha512::default(); // this defaults to Hw then Sw strategy - let mut prehash_for_verifying = engine_sha512::Sha512::default(); + //let mut prehash_for_signing = engine_sha512::Sha512::default(); // this defaults to Hw then Sw strategy + //let mut prehash_for_verifying = engine_sha512::Sha512::default(); + let mut prehash_for_signing = sha2::Sha512::default(); // this defaults to Hw then Sw strategy + let mut prehash_for_verifying = sha2::Sha512::default(); prehash_for_signing.update(&msg_bytes[..]); prehash_for_verifying.update(&msg_bytes[..]); diff --git a/services/test-stub/Cargo.toml b/services/test-stub/Cargo.toml new file mode 100644 index 000000000..407d7f4d8 --- /dev/null +++ b/services/test-stub/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "test-stub" +version = "0.1.0" +authors = ["bunnie "] +edition = "2018" +description = "Test Stub Program" + +[dependencies] +xous = { path = "../../xous-rs" } +log-server = { path = "../log-server" } +ticktimer-server = { path = "../ticktimer-server" } +xous-names = { path = "../xous-names" } +log = "0.4" +num-derive = {version = "0.3", default-features = false} +num-traits = {version = "0.2", default-features = false} +susres = {path = "../susres"} + +[target.'cfg(not(any(windows,unix)))'.dependencies] +utralib = { path = "../../utralib"} + +[features] +default = [] diff --git a/services/test-stub/README.md b/services/test-stub/README.md new file mode 100644 index 000000000..cb839c6db --- /dev/null +++ b/services/test-stub/README.md @@ -0,0 +1,2 @@ +A test stub. Various small routines and tests get thrown here over time +for isolating parts of hairy bugs. diff --git a/services/test-stub/src/main.rs b/services/test-stub/src/main.rs new file mode 100644 index 000000000..9d5b5b468 --- /dev/null +++ b/services/test-stub/src/main.rs @@ -0,0 +1,192 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +use core::{ + fmt::Debug, hash::Hash, ptr::copy_nonoverlapping, +}; + +macro_rules! unsafe_read_slice { + ($src:expr, $dst:expr, $size:expr, $which:ident) => {{ + assert_eq!($src.len(), $size * $dst.len()); + + unsafe { + copy_nonoverlapping( + $src.as_ptr(), + $dst.as_mut_ptr() as *mut u8, + $src.len(), + ); + } + for v in $dst.iter_mut() { + *v = v.$which(); + } + }}; +} +pub trait ByteOrder: +{ + fn read_u64_into(src: &[u8], dst: &mut [u64]); +} +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub enum LittleEndian {} + +impl Default for LittleEndian { + fn default() -> LittleEndian { + panic!("LittleEndian default") + } +} +impl ByteOrder for LittleEndian { + #[inline] + fn read_u64_into(src: &[u8], dst: &mut [u64]) { + unsafe_read_slice!(src, dst, 8, to_le); + } +} +#[derive(Copy, Clone, Hash)] +pub struct Scalar { + pub bytes: [u8; 32], +} +impl Scalar { + pub fn non_adjacent_form(&self, w: usize) -> [i8; 256] { + let mut naf = [0i8; 256]; + + #[repr(align(32))] + struct AlignedU64Slice([u64; N]); + + let mut x_u64 = AlignedU64Slice([0u64; 5]); + LittleEndian::read_u64_into(&self.bytes, &mut x_u64.0[0..4]); + + log::info!("x_u64: {:x?}", x_u64.0); + + let width = 1 << w; + let window_mask = width - 1; + + let mut pos = 0; + let mut carry = 0; + + while pos < 256 { + //log::info!("naf[{}]: {:?}", pos, naf); + + // Construct a buffer of bits of the scalar, starting at bit `pos` + let u64_idx = pos / 64; + let bit_idx = pos % 64; + + let mut x_u64_idx: u64 = x_u64.0[u64_idx]; + let mut x_u64_idx_plus_1: u64 = x_u64.0[u64_idx+1]; + + let bit_buf: u64; + if bit_idx < 64 - w { + // This window's bits are contained in a single u64 + /* + let mut bits: [bool; 64] = [false; 64]; + for i in 0..64 { + if (x_u64[u64_idx] >> i) & 1 != 0 { + bits[i] = true; + } + } + log::info!("bits: {:?}", bits); + let mut buf: u64 = 0; + for i in u64_idx..64 { + if bits[i] { + buf |= 1 << i; + } + } + bit_buf = buf; + */ + /* + let mut modulus = 1; + for _ in 0..bit_idx { + modulus *= 2; + } + bit_buf = x_u64[u64_idx] / modulus;*/ + bit_buf = x_u64_idx >> bit_idx; /////////////////////////////// <----------- culprit + x_u64_idx += 1; + log::info!("bit_buf1: {:x}/{:x}/{}", x_u64_idx, bit_buf, u64_idx); + } else { + // Combine the current u64's bits with the bits from the next u64 + bit_buf = (x_u64_idx >> bit_idx) | (x_u64_idx_plus_1 << (64 - bit_idx)); + x_u64_idx_plus_1 += 1; + log::info!("bit_buf2: {:x}", bit_buf); + } + log::info!("dummy: {:x}", x_u64_idx + x_u64_idx_plus_1); + + // Add the carry into the current window + let window = carry + (bit_buf & window_mask); + + if window & 1 == 0 { + // If the window value is even, preserve the carry and continue. + // Why is the carry preserved? + // If carry == 0 and window & 1 == 0, then the next carry should be 0 + // If carry == 1 and window & 1 == 0, then bit_buf & 1 == 1 so the next carry should be 1 + pos += 1; + continue; + } + + if window < width/2 { + log::info!("carry 0 width {} naf[{}] = {}; c.{} bb.{:x} wm.{} idx64.{} idxbit.{} xu64[0].{:x}", width, pos, window, + carry, bit_buf, window_mask, u64_idx, bit_idx, x_u64.0[0], + ); + carry = 0; + naf[pos] = window as i8; + } else { + log::info!("carry 1 width {} naf[{}] = {}/{}; c.{} bb.{:x} wm.{} idx64.{} idxbit.{} xu64[0].{:x}", width, pos, window, (window as i8).wrapping_sub(width as i8), + carry, bit_buf, window_mask, u64_idx, bit_idx, x_u64.0[0] + ); + carry = 1; + naf[pos] = (window as i8).wrapping_sub(width as i8); + } + + pos += w; + } + + naf + } +} + +// see https://godbolt.org/z/K8MK1v6f9 for the compiler explorer version of this +#[xous::xous_main] +fn xmain() -> ! { + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); + + // let test_x: [u64; 5] = [3670528585167682493, 5751548057121892975, 14440402437776689254, 997049197504491580, 0]; + let mut test_x: [u64; 5] = [0u64; 5]; + LittleEndian::read_u64_into(&[189, 59, 214, 8, 77, 86, 240, 50, 111, 170, 86, 37, 124, 154, 209, 79, 102, 72, 93, 53, 130, 157, 102, 200, 60, 240, 215, 104, 246, 58, 214, 13], &mut test_x[0..4]); + + let mut dummy: u64 = 0; + let mut bit_idx = 0; + for i in 0..4 { + let bit_buf: u64; + if i < 2 { + bit_buf = test_x[i] >> bit_idx; + } else { + bit_buf = (test_x[i] >> bit_idx) | (test_x[i+1] << (64 - bit_idx)); + } + dummy += bit_buf; + bit_idx += 8; + log::info!("bit_buf: {:x}", bit_buf); + } + log::info!("dummy: {:x}", dummy); + + let scalar = Scalar { + bytes: [189, 59, 214, 8, 77, 86, 240, 50, 111, 170, 86, 37, 124, 154, 209, 79, 102, 72, 93, 53, 130, 157, 102, 200, 60, 240, 215, 104, 246, 58, 214, 13], + }; + let mut padding: [u8; 33] = [0; 33]; + for (&src, dst) in scalar.bytes.iter().zip(padding.iter_mut()) { + *dst = src; + } + let a_naf = scalar.non_adjacent_form(5); + + let expected_result = [-3, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, -7, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, -7, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, -11, 0, 0, 0, 0, -5, 0, 0, 0, 0, 11, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, -15, 0, 0, 0, 0, -11, 0, 0, 0, 0, 15, 0, 0, 0, 0, -11, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, -7, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 9, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, -7, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0]; + + log::info!("a_naf: {:?}", a_naf); + log::info!("expected_result: {:?}", expected_result); + assert!(a_naf == expected_result, "mismatch!"); + + log::info!("padding: {:?}", padding); + loop { + xous::yield_slice(); + } + /* + log::trace!("quitting"); + xous::terminate_process(0) + */ +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index fdd4bed82..930f15dcf 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -74,8 +74,9 @@ fn try_main() -> Result<(), DynError> { "xous-names", "trng", "llio", - "rkyv-test-client", - "rkyv-test-server", +// "rkyv-test-client", +// "rkyv-test-server", + "test-stub", "susres", "com", ];