Merge pull request #514 from betrusted-io/curve25519

Curve25519
This commit is contained in:
bunnie 2024-03-25 07:10:34 +08:00 committed by GitHub
commit a6ff652e48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 60 additions and 74 deletions

View file

@ -15,3 +15,6 @@ rustflags = ["--cfg", "crossbeam_no_atomic_64"]
# "-C", "link-arg=-shared",
# "--cfg", "crossbeam_no_atomic_64"
# ]
[target.riscv32imac-unknown-xous-elf]
rustflags = ["--cfg", 'curve25519_dalek_backend="u32e_backend"']

34
Cargo.lock generated
View file

@ -1150,26 +1150,26 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]]
name = "curve25519-dalek"
version = "4.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89b8c6a2e4b1f45971ad09761aafb85514a84744b67a95e32c3cc1352d1f65c"
version = "4.1.2"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#0fc7183a2a4c82a5ea575f56ca975cef46cd6398"
dependencies = [
"cfg-if",
"cpufeatures",
"curve25519-dalek-derive",
"digest 0.10.7",
"fiat-crypto 0.2.6",
"platforms",
"engine25519-as",
"log",
"rustc_version 0.4.0",
"subtle",
"utralib 0.1.24",
"xous 0.9.58 (registry+https://github.com/rust-lang/crates.io-index)",
"zeroize",
]
[[package]]
name = "curve25519-dalek-derive"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#0fc7183a2a4c82a5ea575f56ca975cef46cd6398"
dependencies = [
"proc-macro2",
"quote",
@ -1186,7 +1186,7 @@ dependencies = [
"digest 0.9.0",
"engine-25519",
"engine25519-as",
"fiat-crypto 0.1.20",
"fiat-crypto",
"hex 0.4.3",
"log",
"packed_simd_2",
@ -1520,9 +1520,9 @@ checksum = "e18997d4604542d0736fae2c5ad6de987f0a50530cbcc14a7ce5a685328a252d"
[[package]]
name = "ed25519-dalek"
version = "2.1.1"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871"
checksum = "1f628eaec48bfd21b865dc2950cfa014450c01d2fa2b69a86c2fd5844ec523c0"
dependencies = [
"curve25519-dalek",
"ed25519 2.2.3",
@ -1813,12 +1813,6 @@ version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77"
[[package]]
name = "fiat-crypto"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1676f435fc1dadde4d03e43f5d62b259e1ce5f40bd4ffb21db2b42ebe59c1382"
[[package]]
name = "filetime"
version = "0.2.23"
@ -3656,12 +3650,6 @@ version = "0.3.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2900ede94e305130c13ddd391e0ab7cbaeb783945ae07a279c268cb05109c6cb"
[[package]]
name = "platforms"
version = "3.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "626dec3cac7cc0e1577a2ec3fc496277ec2baa084bebad95bb6fdbfae235f84c"
[[package]]
name = "plotters"
version = "0.3.5"
@ -4207,7 +4195,6 @@ dependencies = [
"curve25519-dalek",
"digest 0.10.7",
"ed25519-dalek",
"engine-25519",
"gam",
"graphics-server",
"hex 0.4.3",
@ -4592,7 +4579,6 @@ dependencies = [
"digest 0.10.7",
"dns",
"ed25519-dalek",
"engine-25519",
"gam",
"graphics-server",
"hex 0.4.3",

View file

@ -145,10 +145,10 @@ sha2_legacy = { git = "https://github.com/RustCrypto/hashes.git", tag = "sha2-v0
[patch.crates-io.aes]
path = "services/aes"
#[patch.crates-io.curve25519-dalek]
#git = "https://github.com/betrusted-io/curve25519-dalek.git"
#branch = "main"
#path = "../curve25519-dalek/curve25519-dalek" # when doing local dev work
[patch.crates-io.curve25519-dalek]
git = "https://github.com/betrusted-io/curve25519-dalek.git"
branch = "main"
# path = "../curve25519-dalek/curve25519-dalek" # when doing local dev work
# feature overrides are specified at the crate level
[patch."https://github.com/betrusted-io/xous-engine-25519.git"]

View file

@ -65,13 +65,13 @@ keyboard = { path = "../keyboard" }
sha2 = { version = "0.10.8" }
digest = "0.10.7"
aes = { path = "../aes" }
engine-25519 = { path = "../engine-25519" }
byteorder = "1.4.3" # used by keywrap
# engine-25519 = { path = "../engine-25519" }
byteorder = "1.4.3" # used by keywrap
# hardware acceleration adaptations are inserted into a fork of the main branch.
hex = { version = "0.4.3", default-features = false, features = [] }
[dependencies.curve25519-dalek]
version = "=4.1.1" # note this is patched to our fork in ./Cargo.toml
version = "=4.1.2" # note this is patched to our fork in ./Cargo.toml
default-features = false
[dependencies.ed25519-dalek]

View file

@ -2382,6 +2382,8 @@ fn main() -> ! {
log::error!("couldn't convert opcode");
}
}
// de-allocate the engine at the end of root-keys, so other crates can use it.
curve25519_dalek::backend::serial::u32e::free_engine();
}
// clean up our program
log::trace!("main loop exit, destroying servers");

View file

@ -28,7 +28,6 @@ sha2 = { version = "0.10.8" }
digest = "0.10.7"
aes = { path = "../aes" }
cipher = "0.4.4"
engine-25519 = { path = "../engine-25519" }
spinor = { path = "../spinor" }
root-keys = { path = "../root-keys" }
jtag = { path = "../jtag" }
@ -101,17 +100,17 @@ random-pick = { version = "1.2.16", optional = true }
hex = { version = "0.4.3", default-features = false, features = [] }
#sha2 = {version = "0.9.5", default-features = false, features = []}
[dependencies.curve25519-dalek]
version = "=4.1.1" # note this is patched to our fork in ./Cargo.toml
version = "=4.1.2" # note this is patched to our fork in ./Cargo.toml
default-features = false
[dependencies.x25519-dalek]
version = "2.0.1"
version = "=2.0.1"
# TODO: static_secrets is only needed by the engine tests. Ideally, we would put the static_secrets version in dev deps only
default-features = false
features = ["static_secrets"]
[dependencies.ed25519-dalek]
version = "2.1.0"
version = "=2.1.0"
#path = "../../../ed25519-dalek"
default-features = false
features = ["rand_core"]
@ -153,4 +152,4 @@ rand-api = []
locktests = [
] # for debugging some specific `std` tests cherry-picked out of the test suite
simple-tls = ["rustls", "webpki-roots"]
default = [] # "debugprint"
default = ["benchmarks"] # "debugprint"

View file

@ -1,5 +1,6 @@
use core::sync::atomic::{AtomicU32, Ordering};
#[cfg(feature = "engine-ll")]
use engine_25519::*;
use num_traits::*;
use xous_ipc::String;
@ -38,7 +39,13 @@ fn vector_read(word_offset: usize) -> u32 {
u32::from_le_bytes(bytes)
}
fn run_vectors(engine: &mut Engine25519) -> (usize, usize) {
fn run_vectors() -> (usize, usize) {
use curve25519_dalek::backend::serial::u32e::*;
ensure_engine();
// safety: it's safe because it's after ensure_engine()
let ucode_hw = unsafe { get_ucode() };
let rf_hw = unsafe { get_rf() };
let mut test_offset: usize = 0x0;
let mut passes: usize = 0;
let mut fails: usize = 0;
@ -58,17 +65,9 @@ fn run_vectors(engine: &mut Engine25519) -> (usize, usize) {
let num_vectors = (vector_read(test_offset) >> 0) & 0x3F_FFFF;
test_offset += 1;
let mut job = Job {
id: None,
uc_start: load_addr,
uc_len: code_len,
ucode: [0; 1024],
rf: [0; RF_SIZE_IN_U32],
window: Some(window as u8),
};
for i in load_addr as usize..(load_addr + code_len) as usize {
job.ucode[i] = vector_read(test_offset);
let mut mcode = Vec::<i32>::new();
for _i in load_addr as usize..(load_addr + code_len) as usize {
mcode.push(vector_read(test_offset) as i32);
test_offset += 1;
}
@ -79,32 +78,20 @@ fn run_vectors(engine: &mut Engine25519) -> (usize, usize) {
// a test suite can have numerous vectors against a common code base
for argcnt in 0..num_args {
for word in 0..8 {
job.rf[(/* window * 32 * 8 + */argcnt * 8 + word) as usize] = vector_read(test_offset);
rf_hw[(window * 32 * 8 + argcnt * 8 + word) as usize] = vector_read(test_offset);
test_offset += 1;
}
}
let mut passed = true;
log::trace!("spawning job");
match engine.spawn_job(job) {
Ok(rf_result) => {
for word in 0..8 {
let expect = vector_read(test_offset);
test_offset += 1;
let actual = rf_result[(/* window * 32 * 8 + */31 * 8 + word) as usize];
if expect != actual {
log::error!("e/a {:08x}/{:08x}", expect, actual);
passed = false;
}
}
}
Err(e) => {
log::error!(
"system error {:?} in running test vector: {}/0x{:x}",
e,
vector,
test_offset
);
run_job(ucode_hw, rf_hw, &mcode, window as usize);
for word in 0..8 {
let expect = vector_read(test_offset);
test_offset += 1;
let actual = rf_hw[(window * 32 * 8 + 31 * 8 + word) as usize];
if expect != actual {
log::error!("e/a {:08x}/{:08x}", expect, actual);
passed = false;
}
}
@ -121,6 +108,7 @@ fn run_vectors(engine: &mut Engine25519) -> (usize, usize) {
}
}
}
curve25519_dalek::backend::serial::u32e::free_engine();
(passes, fails)
}
/*
@ -134,8 +122,6 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let xns = xous_names::XousNames::new().unwrap();
let callback_conn = xns.request_connection_blocking(crate::SERVER_NAME_SHELLCHAT).unwrap();
let mut engine = engine_25519::Engine25519::new();
let mut trng = trng::Trng::new(&xns).unwrap();
loop {
@ -146,7 +132,7 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let mut passes = 0;
let mut fails = 0;
for _ in 0..TEST_ITERS {
let (p, f) = run_vectors(&mut engine);
let (p, f) = run_vectors();
passes += p;
fails += f;
}
@ -299,6 +285,7 @@ impl Engine {
.unwrap();
Engine {
susres: susres::Susres::new_without_hook(&xns).unwrap(),
// this is a dummy and hangs if engine-ll is not an active feature
benchmark_cid: xous::connect(sid).unwrap(),
start_time: None,
}
@ -317,16 +304,18 @@ impl<'a> ShellCmdApi<'a> for Engine {
) -> Result<Option<String<1024>>, xous::Error> {
use core::fmt::Write;
let mut ret = String::<1024>::new();
#[cfg(feature = "engine-ll")]
let helpstring = "engine [check] [bench] [benchdh] [susres] [dh] [ed] [wycheproof]";
#[cfg(not(feature = "engine-ll"))]
let helpstring = "engine [susres] [dh] [ed] [wycheproof]";
let mut tokens = args.as_str().unwrap().split(' ');
if let Some(sub_cmd) = tokens.next() {
match sub_cmd {
"check" => {
let mut engine = engine_25519::Engine25519::new();
log::debug!("running vectors");
let (passes, fails) = run_vectors(&mut engine);
let (passes, fails) = run_vectors();
write!(ret, "Engine passed {} vectors, failed {} vectors", passes, fails).unwrap();
}
@ -364,13 +353,18 @@ impl<'a> ShellCmdApi<'a> for Engine {
write!(ret, "Interrupted Engine hardware benchmark with a suspend/resume").unwrap();
}
"dh" => {
log::info!("starting DH test");
use x25519_dalek::{EphemeralSecret, PublicKey};
let alice_secret = EphemeralSecret::random_from_rng(&mut env.trng);
log::info!("1");
let alice_public = PublicKey::from(&alice_secret);
let bob_secret = EphemeralSecret::random_from_rng(&mut env.trng);
let bob_public = PublicKey::from(&bob_secret);
log::info!("2");
let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);
log::info!("3");
let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);
log::info!("4");
let mut pass = true;
for (&alice, &bob) in
alice_shared_secret.as_bytes().iter().zip(bob_shared_secret.as_bytes().iter())
@ -571,6 +565,8 @@ impl<'a> ShellCmdApi<'a> for Engine {
} else {
write!(ret, "{}", helpstring).unwrap();
}
// de-allocate the engine at the end of the test, so other processes can grab it
curve25519_dalek::backend::serial::u32e::free_engine();
Ok(Some(ret))
}

View file

@ -99,7 +99,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"root-keys",
"trng",
"sha2@0.10.8",
"engine-25519",
// "engine-25519",
"jtag",
// GUI front end
"status",