From 674faa46d9eea11ace37437c7234b0902497fc2f Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 29 Jun 2021 23:46:15 +0800 Subject: [PATCH] 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)?,