From 8da62390ad384dda69f1b2b274850929a10cb037 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 28 Jun 2021 19:01:22 +0800 Subject: [PATCH 1/5] add RFC7748 test vectors for diffie hellman --- Cargo.lock | 4 +- Cargo.toml | 6 +-- services/shellchat/Cargo.toml | 10 ++-- services/shellchat/src/cmds/engine.rs | 77 +++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8af2b666e..4aa93808a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -505,7 +505,6 @@ dependencies = [ [[package]] name = "curve25519-dalek" version = "3.1.0" -source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#1a79b76e90d49366b47eb98398a6104678686c15" dependencies = [ "byteorder", "digest", @@ -544,11 +543,10 @@ dependencies = [ [[package]] name = "ed25519-dalek" version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" dependencies = [ "curve25519-dalek", "ed25519", + "log", "rand 0.7.3", "sha2", "zeroize", diff --git a/Cargo.toml b/Cargo.toml index b407b53e3..e0d042b10 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,9 +93,9 @@ 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" +#git="https://github.com/betrusted-io/curve25519-dalek.git" +#branch="main" +path = "../curve25519-dalek" default-features = false features = ["u32_backend", "betrusted"] diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 1ba297851..45acc44d3 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -42,9 +42,9 @@ rand_core = "0.5" hex = {version = "0.4.3", 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" +#git="https://github.com/betrusted-io/curve25519-dalek.git" +#branch="main" +path = "../../../curve25519-dalek" default-features = false features = ["u32_backend", "betrusted"] @@ -54,8 +54,8 @@ default-features = false features = ["u32_backend"] [dependencies.ed25519-dalek] -version = "1.0.1" -#path = "../../../ed25519-dalek" +#version = "1.0.1" +path = "../../../ed25519-dalek" default-features = false features = ["u32_backend", "rand"] diff --git a/services/shellchat/src/cmds/engine.rs b/services/shellchat/src/cmds/engine.rs index bae8449c5..fa806e9f3 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}; From 0bad1ad7550fcd7aeb6868763c8b5dbcf787f8b5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 29 Jun 2021 00:10:46 +0800 Subject: [PATCH 2/5] isolate a potential compiler bug --- Cargo.lock | 37 +++++++- Cargo.toml | 13 ++- buildpush.bat | 3 +- services/shellchat/Cargo.toml | 7 +- services/shellchat/src/cmds/engine.rs | 6 +- services/test-stub/Cargo.toml | 22 +++++ services/test-stub/README.md | 2 + services/test-stub/src/main.rs | 126 ++++++++++++++++++++++++++ xtask/src/main.rs | 5 +- 9 files changed, 204 insertions(+), 17 deletions(-) create mode 100644 services/test-stub/Cargo.toml create mode 100644 services/test-stub/README.md create mode 100644 services/test-stub/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 4aa93808a..1f0fc654c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,14 +508,25 @@ version = "3.1.0" dependencies = [ "byteorder", "digest", - "engine-25519", - "engine25519-as", "log", "rand_core 0.5.1", "subtle", "zeroize", ] +[[package]] +name = "curve25519-dalek" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "639891fde0dbea823fc3d798a0fdf9d2f9440a42d64a78ab3488b0ca025117b3" +dependencies = [ + "byteorder", + "digest", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + [[package]] name = "digest" version = "0.9.0" @@ -544,7 +555,7 @@ dependencies = [ name = "ed25519-dalek" version = "1.0.1" dependencies = [ - "curve25519-dalek", + "curve25519-dalek 3.1.0", "ed25519", "log", "rand 0.7.3", @@ -1616,7 +1627,7 @@ dependencies = [ "codec", "com", "content-plugin-api", - "curve25519-dalek", + "curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "digest", "ed25519-dalek", "engine-25519", @@ -1636,6 +1647,7 @@ dependencies = [ "rand_core 0.5.1", "rkyv 0.4.3", "rtc", + "sha2", "susres", "ticktimer-server", "trng", @@ -1785,6 +1797,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" @@ -2085,7 +2112,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a0c105152107e3b96f6a00a65e86ce82d9b125230e1c4302940eca58ff71f4f" dependencies = [ - "curve25519-dalek", + "curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.5.1", "zeroize", ] diff --git a/Cargo.toml b/Cargo.toml index e0d042b10..eeb0c4a5c 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" @@ -91,13 +93,14 @@ lto = "fat" 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] +#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" -default-features = false -features = ["u32_backend", "betrusted"] +#path = "../curve25519-dalek" +#default-features = false +#features = ["u32_backend", "betrusted"] [patch."https://github.com/betrusted-io/xous-engine-25519.git"] engine-25519 = {path = "services/engine-25519"} \ No newline at end of file diff --git a/buildpush.bat b/buildpush.bat index 06a38480a..de568eacd 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 hw-image precursors/soc.svd +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 45acc44d3..b1ae8681d 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -40,13 +40,16 @@ 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" +#path = "../../../curve25519-dalek" +version = "3.1.0" default-features = false -features = ["u32_backend", "betrusted"] +#features = ["u32_backend", "betrusted"] +features = ["u32_backend"] [dependencies.x25519-dalek] version = "1.1.1" diff --git a/services/shellchat/src/cmds/engine.rs b/services/shellchat/src/cmds/engine.rs index fa806e9f3..dbae78d45 100644 --- a/services/shellchat/src/cmds/engine.rs +++ b/services/shellchat/src/cmds/engine.rs @@ -419,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..e9f145c7b --- /dev/null +++ b/services/test-stub/src/main.rs @@ -0,0 +1,126 @@ +#![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]; + + let mut x_u64 = [0u64; 5]; + LittleEndian::read_u64_into(&self.bytes, &mut x_u64[0..4]); + + 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 bit_buf: u64; + if bit_idx < 64 - w { + // This window's bits are contained in a single u64 + bit_buf = x_u64[u64_idx] >> bit_idx; + } else { + // Combine the current u64's bits with the bits from the next u64 + bit_buf = (x_u64[u64_idx] >> bit_idx) | (x_u64[1+u64_idx] << (64 - bit_idx)); + } + + // 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 { + carry = 0; + naf[pos] = window as i8; + } else { + carry = 1; + naf[pos] = (window as i8).wrapping_sub(width as i8); + } + + pos += w; + } + + naf + } +} + +#[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 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 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!"); + + 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", ]; From c880eda822bf15bbb4bd7aa58a58fa8c12ce7a64 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 29 Jun 2021 00:51:52 +0800 Subject: [PATCH 3/5] commit renode changes --- services/test-stub/src/main.rs | 18 +++++++++++++++++- xtask/src/main.rs | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/services/test-stub/src/main.rs b/services/test-stub/src/main.rs index e9f145c7b..4cd433f35 100644 --- a/services/test-stub/src/main.rs +++ b/services/test-stub/src/main.rs @@ -52,6 +52,8 @@ impl Scalar { let mut x_u64 = [0u64; 5]; LittleEndian::read_u64_into(&self.bytes, &mut x_u64[0..4]); + log::info!("x_u64: {:x?}", x_u64); + let width = 1 << w; let window_mask = width - 1; @@ -66,7 +68,14 @@ impl Scalar { let bit_buf: u64; if bit_idx < 64 - w { // This window's bits are contained in a single u64 - bit_buf = x_u64[u64_idx] >> bit_idx; + /* + let mut lower = x_u64[u64_idx] as u32; + let upper = (x_u64[u64_idx] >> 32) as u32; + lower >>= bit_idx; + lower |= upper << (32 - bit_idx); + bit_buf = ((upper >> bit_idx) as u64) << 32 | lower as u64;*/ + bit_buf = x_u64[u64_idx] >> bit_idx; /////////////////////////////// <----------- culprit + log::info!("bit_buf: {:x}", bit_buf); } else { // Combine the current u64's bits with the bits from the next u64 bit_buf = (x_u64[u64_idx] >> bit_idx) | (x_u64[1+u64_idx] << (64 - bit_idx)); @@ -85,9 +94,15 @@ impl Scalar { } 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], + ); 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] + ); carry = 1; naf[pos] = (window as i8).wrapping_sub(width as i8); } @@ -99,6 +114,7 @@ impl Scalar { } } +// see https://godbolt.org/z/K8MK1v6f9 for the compiler explorer version of this #[xous::xous_main] fn xmain() -> ! { log_server::init_wait().unwrap(); diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 930f15dcf..6b4503cfe 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -109,7 +109,7 @@ fn try_main() -> Result<(), DynError> { let mut args = env::args(); let task = args.nth(1); match task.as_deref() { - Some("renode-image") => renode_image(false, &hw_pkgs)?, + Some("renode-image") => renode_image(false, &minimal_pkgs)?, Some("renode-test") => renode_image(false, &cbtest_pkgs)?, Some("libstd-test") => renode_image_extra(false, &base_pkgs, args.collect())?, Some("renode-aes-test") => renode_image(false, &aestest_pkgs)?, From d00717bc17efbc6a9ef886e8477a6e2df2f263fe Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 29 Jun 2021 01:35:03 +0800 Subject: [PATCH 4/5] try to make a simpler example --- services/test-stub/src/main.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/services/test-stub/src/main.rs b/services/test-stub/src/main.rs index 4cd433f35..04909a5ba 100644 --- a/services/test-stub/src/main.rs +++ b/services/test-stub/src/main.rs @@ -121,6 +121,25 @@ fn xmain() -> ! { 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], }; From 674faa46d9eea11ace37437c7234b0902497fc2f Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 29 Jun 2021 23:46:15 +0800 Subject: [PATCH 5/5] button up after debugging the ed25519 issue --- Cargo.lock | 25 +++++--------- Cargo.toml | 14 ++++---- buildpush.bat | 4 +-- services/shellchat/Cargo.toml | 13 +++----- services/test-stub/src/main.rs | 61 +++++++++++++++++++++++++--------- xtask/src/main.rs | 2 +- 6 files changed, 68 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1f0fc654c..0cc7c79b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -505,28 +505,18 @@ dependencies = [ [[package]] name = "curve25519-dalek" version = "3.1.0" +source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#b8f968b6e45816d3f5148eb3fb9e10803fedd3f9" dependencies = [ "byteorder", "digest", + "engine-25519", + "engine25519-as", "log", "rand_core 0.5.1", "subtle", "zeroize", ] -[[package]] -name = "curve25519-dalek" -version = "3.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "639891fde0dbea823fc3d798a0fdf9d2f9440a42d64a78ab3488b0ca025117b3" -dependencies = [ - "byteorder", - "digest", - "rand_core 0.5.1", - "subtle", - "zeroize", -] - [[package]] name = "digest" version = "0.9.0" @@ -554,10 +544,11 @@ dependencies = [ [[package]] name = "ed25519-dalek" version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d" dependencies = [ - "curve25519-dalek 3.1.0", + "curve25519-dalek", "ed25519", - "log", "rand 0.7.3", "sha2", "zeroize", @@ -1627,7 +1618,7 @@ dependencies = [ "codec", "com", "content-plugin-api", - "curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "curve25519-dalek", "digest", "ed25519-dalek", "engine-25519", @@ -2112,7 +2103,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a0c105152107e3b96f6a00a65e86ce82d9b125230e1c4302940eca58ff71f4f" dependencies = [ - "curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "curve25519-dalek", "rand_core 0.5.1", "zeroize", ] diff --git a/Cargo.toml b/Cargo.toml index eeb0c4a5c..550cc607d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -93,14 +93,14 @@ lto = "fat" xous = {path = "xous-rs"} xous-macros = {path = "macros"} # prefer hardware-accelerated versions of services -#sha2 = {path = "services/engine-sha512", package = "engine-sha512"} +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" -#default-features = false -#features = ["u32_backend", "betrusted"] +[patch.crates-io.curve25519-dalek] +git="https://github.com/betrusted-io/curve25519-dalek.git" +branch="main" +#path = "../curve25519-dalek" # when doing local dev work +default-features = false +features = ["u32_backend", "betrusted"] [patch."https://github.com/betrusted-io/xous-engine-25519.git"] engine-25519 = {path = "services/engine-25519"} \ No newline at end of file diff --git a/buildpush.bat b/buildpush.bat index de568eacd..7248e42d7 100644 --- a/buildpush.bat +++ b/buildpush.bat @@ -1,7 +1,7 @@ REM a crappy starter script to help with builds under Windows environments. ymmv. -REM cargo xtask hw-image precursors/soc.svd -cargo xtask minimal precursors/soc.svd +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 b1ae8681d..3d6b22433 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -42,14 +42,9 @@ 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" +version = "3.1.0" # note this is patched to our fork in ./Cargo.toml default-features = false -#features = ["u32_backend", "betrusted"] -features = ["u32_backend"] +features = ["u32_backend", "betrusted"] [dependencies.x25519-dalek] version = "1.1.1" @@ -57,8 +52,8 @@ default-features = false features = ["u32_backend"] [dependencies.ed25519-dalek] -#version = "1.0.1" -path = "../../../ed25519-dalek" +version = "1.0.1" +#path = "../../../ed25519-dalek" default-features = false features = ["u32_backend", "rand"] diff --git a/services/test-stub/src/main.rs b/services/test-stub/src/main.rs index 04909a5ba..9d5b5b468 100644 --- a/services/test-stub/src/main.rs +++ b/services/test-stub/src/main.rs @@ -39,47 +39,73 @@ impl ByteOrder for LittleEndian { 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]; - let mut x_u64 = [0u64; 5]; - LittleEndian::read_u64_into(&self.bytes, &mut x_u64[0..4]); + #[repr(align(32))] + struct AlignedU64Slice([u64; N]); - log::info!("x_u64: {:x?}", x_u64); + 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 lower = x_u64[u64_idx] as u32; - let upper = (x_u64[u64_idx] >> 32) as u32; - lower >>= bit_idx; - lower |= upper << (32 - bit_idx); - bit_buf = ((upper >> bit_idx) as u64) << 32 | lower as u64;*/ - bit_buf = x_u64[u64_idx] >> bit_idx; /////////////////////////////// <----------- culprit - log::info!("bit_buf: {:x}", bit_buf); + 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[u64_idx] >> bit_idx) | (x_u64[1+u64_idx] << (64 - bit_idx)); + 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); @@ -95,13 +121,13 @@ impl Scalar { 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], + 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] + 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); @@ -143,6 +169,10 @@ fn xmain() -> ! { 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]; @@ -151,6 +181,7 @@ fn xmain() -> ! { log::info!("expected_result: {:?}", expected_result); assert!(a_naf == expected_result, "mismatch!"); + log::info!("padding: {:?}", padding); loop { xous::yield_slice(); } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 6b4503cfe..930f15dcf 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -109,7 +109,7 @@ fn try_main() -> Result<(), DynError> { let mut args = env::args(); let task = args.nth(1); match task.as_deref() { - Some("renode-image") => renode_image(false, &minimal_pkgs)?, + Some("renode-image") => renode_image(false, &hw_pkgs)?, Some("renode-test") => renode_image(false, &cbtest_pkgs)?, Some("libstd-test") => renode_image_extra(false, &base_pkgs, args.collect())?, Some("renode-aes-test") => renode_image(false, &aestest_pkgs)?,