button up after debugging the ed25519 issue

This commit is contained in:
bunnie 2021-06-29 23:46:15 +08:00
parent d00717bc17
commit 674faa46d9
6 changed files with 68 additions and 51 deletions

25
Cargo.lock generated
View file

@ -505,28 +505,18 @@ dependencies = [
[[package]] [[package]]
name = "curve25519-dalek" name = "curve25519-dalek"
version = "3.1.0" version = "3.1.0"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#b8f968b6e45816d3f5148eb3fb9e10803fedd3f9"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"digest", "digest",
"engine-25519",
"engine25519-as",
"log", "log",
"rand_core 0.5.1", "rand_core 0.5.1",
"subtle", "subtle",
"zeroize", "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]] [[package]]
name = "digest" name = "digest"
version = "0.9.0" version = "0.9.0"
@ -554,10 +544,11 @@ dependencies = [
[[package]] [[package]]
name = "ed25519-dalek" name = "ed25519-dalek"
version = "1.0.1" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c762bae6dcaf24c4c84667b8579785430908723d5c889f469d76a41d59cc7a9d"
dependencies = [ dependencies = [
"curve25519-dalek 3.1.0", "curve25519-dalek",
"ed25519", "ed25519",
"log",
"rand 0.7.3", "rand 0.7.3",
"sha2", "sha2",
"zeroize", "zeroize",
@ -1627,7 +1618,7 @@ dependencies = [
"codec", "codec",
"com", "com",
"content-plugin-api", "content-plugin-api",
"curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "curve25519-dalek",
"digest", "digest",
"ed25519-dalek", "ed25519-dalek",
"engine-25519", "engine-25519",
@ -2112,7 +2103,7 @@ version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a0c105152107e3b96f6a00a65e86ce82d9b125230e1c4302940eca58ff71f4f" checksum = "5a0c105152107e3b96f6a00a65e86ce82d9b125230e1c4302940eca58ff71f4f"
dependencies = [ dependencies = [
"curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "curve25519-dalek",
"rand_core 0.5.1", "rand_core 0.5.1",
"zeroize", "zeroize",
] ]

View file

@ -93,14 +93,14 @@ lto = "fat"
xous = {path = "xous-rs"} xous = {path = "xous-rs"}
xous-macros = {path = "macros"} xous-macros = {path = "macros"}
# prefer hardware-accelerated versions of services # 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] [patch.crates-io.curve25519-dalek]
#git="https://github.com/betrusted-io/curve25519-dalek.git" git="https://github.com/betrusted-io/curve25519-dalek.git"
#branch="main" branch="main"
#path = "../curve25519-dalek" #path = "../curve25519-dalek" # when doing local dev work
#default-features = false default-features = false
#features = ["u32_backend", "betrusted"] features = ["u32_backend", "betrusted"]
[patch."https://github.com/betrusted-io/xous-engine-25519.git"] [patch."https://github.com/betrusted-io/xous-engine-25519.git"]
engine-25519 = {path = "services/engine-25519"} engine-25519 = {path = "services/engine-25519"}

View file

@ -1,7 +1,7 @@
REM a crappy starter script to help with builds under Windows environments. ymmv. REM a crappy starter script to help with builds under Windows environments. ymmv.
REM cargo xtask hw-image precursors/soc.svd cargo xtask hw-image precursors/soc.svd
cargo xtask minimal precursors/soc.svd REM cargo xtask minimal precursors/soc.svd
CertUtil -hashfile precursors/soc_csr.bin MD5 CertUtil -hashfile precursors/soc_csr.bin MD5
CertUtil -hashfile target/riscv32imac-unknown-none-elf/release/loader.bin MD5 CertUtil -hashfile target/riscv32imac-unknown-none-elf/release/loader.bin MD5

View file

@ -42,14 +42,9 @@ rand_core = "0.5"
hex = {version = "0.4.3", default-features = false, features = []} hex = {version = "0.4.3", default-features = false, features = []}
sha2 = {version = "0.9.5", default-features = false, features = []} sha2 = {version = "0.9.5", default-features = false, features = []}
[dependencies.curve25519-dalek] [dependencies.curve25519-dalek]
# don't forget the Cargo.toml patch.crates.io file to patch over third-party dependencies as well! version = "3.1.0" # note this is patched to our fork in ./Cargo.toml
#git="https://github.com/betrusted-io/curve25519-dalek.git"
#branch="main"
#path = "../../../curve25519-dalek"
version = "3.1.0"
default-features = false default-features = false
#features = ["u32_backend", "betrusted"] features = ["u32_backend", "betrusted"]
features = ["u32_backend"]
[dependencies.x25519-dalek] [dependencies.x25519-dalek]
version = "1.1.1" version = "1.1.1"
@ -57,8 +52,8 @@ default-features = false
features = ["u32_backend"] features = ["u32_backend"]
[dependencies.ed25519-dalek] [dependencies.ed25519-dalek]
#version = "1.0.1" version = "1.0.1"
path = "../../../ed25519-dalek" #path = "../../../ed25519-dalek"
default-features = false default-features = false
features = ["u32_backend", "rand"] features = ["u32_backend", "rand"]

View file

@ -39,47 +39,73 @@ impl ByteOrder for LittleEndian {
unsafe_read_slice!(src, dst, 8, to_le); unsafe_read_slice!(src, dst, 8, to_le);
} }
} }
#[derive(Copy, Clone, Hash)] #[derive(Copy, Clone, Hash)]
pub struct Scalar { pub struct Scalar {
pub bytes: [u8; 32], pub bytes: [u8; 32],
} }
impl Scalar { impl Scalar {
pub fn non_adjacent_form(&self, w: usize) -> [i8; 256] { pub fn non_adjacent_form(&self, w: usize) -> [i8; 256] {
let mut naf = [0i8; 256]; let mut naf = [0i8; 256];
let mut x_u64 = [0u64; 5]; #[repr(align(32))]
LittleEndian::read_u64_into(&self.bytes, &mut x_u64[0..4]); struct AlignedU64Slice<const N: usize>([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 width = 1 << w;
let window_mask = width - 1; let window_mask = width - 1;
let mut pos = 0; let mut pos = 0;
let mut carry = 0; let mut carry = 0;
while pos < 256 { while pos < 256 {
//log::info!("naf[{}]: {:?}", pos, naf); //log::info!("naf[{}]: {:?}", pos, naf);
// Construct a buffer of bits of the scalar, starting at bit `pos` // Construct a buffer of bits of the scalar, starting at bit `pos`
let u64_idx = pos / 64; let u64_idx = pos / 64;
let bit_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; let bit_buf: u64;
if bit_idx < 64 - w { if bit_idx < 64 - w {
// This window's bits are contained in a single u64 // This window's bits are contained in a single u64
/* /*
let mut lower = x_u64[u64_idx] as u32; let mut bits: [bool; 64] = [false; 64];
let upper = (x_u64[u64_idx] >> 32) as u32; for i in 0..64 {
lower >>= bit_idx; if (x_u64[u64_idx] >> i) & 1 != 0 {
lower |= upper << (32 - bit_idx); bits[i] = true;
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); 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 { } else {
// Combine the current u64's bits with the bits from the next u64 // 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 // Add the carry into the current window
let window = carry + (bit_buf & window_mask); let window = carry + (bit_buf & window_mask);
@ -95,13 +121,13 @@ impl Scalar {
if window < width/2 { if window < width/2 {
log::info!("carry 0 width {} naf[{}] = {}; c.{} bb.{:x} wm.{} idx64.{} idxbit.{} xu64[0].{:x}", width, pos, window, 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; carry = 0;
naf[pos] = window as i8; naf[pos] = window as i8;
} else { } 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), 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; carry = 1;
naf[pos] = (window as i8).wrapping_sub(width as i8); naf[pos] = (window as i8).wrapping_sub(width as i8);
@ -143,6 +169,10 @@ fn xmain() -> ! {
let scalar = Scalar { 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], 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 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]; 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); log::info!("expected_result: {:?}", expected_result);
assert!(a_naf == expected_result, "mismatch!"); assert!(a_naf == expected_result, "mismatch!");
log::info!("padding: {:?}", padding);
loop { loop {
xous::yield_slice(); xous::yield_slice();
} }

View file

@ -109,7 +109,7 @@ fn try_main() -> Result<(), DynError> {
let mut args = env::args(); let mut args = env::args();
let task = args.nth(1); let task = args.nth(1);
match task.as_deref() { 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("renode-test") => renode_image(false, &cbtest_pkgs)?,
Some("libstd-test") => renode_image_extra(false, &base_pkgs, args.collect())?, Some("libstd-test") => renode_image_extra(false, &base_pkgs, args.collect())?,
Some("renode-aes-test") => renode_image(false, &aestest_pkgs)?, Some("renode-aes-test") => renode_image(false, &aestest_pkgs)?,