Merge pull request #518 from betrusted-io/cleanup-c25519

Cleanup c25519
This commit is contained in:
bunnie 2024-03-28 04:28:36 -04:00 committed by GitHub
commit 82fdcaadd6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 26 additions and 1406 deletions

29
Cargo.lock generated
View file

@ -1151,13 +1151,14 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]]
name = "curve25519-dalek"
version = "4.1.2"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#b6ce2f60a9c4217a926006d2feb5d3822c6641d9"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#468e304624b4bc4004863c9147176238cfc0dc10"
dependencies = [
"cfg-if",
"cpufeatures",
"curve25519-dalek-derive",
"digest 0.10.7",
"engine25519-as",
"fiat-crypto 0.2.7",
"log",
"rustc_version 0.4.0",
"subtle",
@ -1169,7 +1170,7 @@ dependencies = [
[[package]]
name = "curve25519-dalek-derive"
version = "0.1.1"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#b6ce2f60a9c4217a926006d2feb5d3822c6641d9"
source = "git+https://github.com/betrusted-io/curve25519-dalek.git?branch=main#468e304624b4bc4004863c9147176238cfc0dc10"
dependencies = [
"proc-macro2",
"quote",
@ -1186,7 +1187,7 @@ dependencies = [
"digest 0.9.0",
"engine-25519",
"engine25519-as",
"fiat-crypto",
"fiat-crypto 0.1.20",
"hex 0.4.3",
"log",
"packed_simd_2",
@ -1632,21 +1633,7 @@ dependencies = [
[[package]]
name = "engine-25519"
version = "0.1.0"
dependencies = [
"engine25519-as",
"llio",
"log",
"num-derive 0.3.3",
"num-traits",
"rkyv",
"utralib 0.1.24",
"xous 0.9.58 (registry+https://github.com/rust-lang/crates.io-index)",
"xous-api-log",
"xous-api-names",
"xous-api-susres",
"xous-api-ticktimer",
"xous-ipc 0.9.58 (registry+https://github.com/rust-lang/crates.io-index)",
]
source = "git+https://github.com/betrusted-io/xous-engine-25519.git?rev=63d3d1f30736022e791deaacf4dd62c00b42fe2e#63d3d1f30736022e791deaacf4dd62c00b42fe2e"
[[package]]
name = "engine25519-as"
@ -1813,6 +1800,12 @@ version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77"
[[package]]
name = "fiat-crypto"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c007b1ae3abe1cb6f85a16305acd418b7ca6343b953633fee2b76d8f108b830f"
[[package]]
name = "filetime"
version = "0.2.23"

View file

@ -17,8 +17,6 @@ default-members = [
"services/shellchat",
"services/llio",
"services/codec",
# "services/engine-sha512",
"services/engine-25519",
"services/aes",
"services/spinor",
"services/root-keys",
@ -60,8 +58,6 @@ members = [
"services/content-plugin-api",
"services/llio",
"services/codec",
# "services/engine-sha512",
"services/engine-25519",
"services/aes",
"services/spinor",
"services/root-keys",
@ -151,9 +147,6 @@ 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"]
engine-25519 = { path = "services/engine-25519" }
[patch.crates-io.usb-device]
git = "https://github.com/betrusted-io/usb-device.git"
branch = "main"

View file

@ -471,6 +471,7 @@ perform the Xous firmware upgrade. This requires running manual update commands,
- a number of other crates and pins were upgraded in the process due to a cargo `update` run; the `build.rs` changes were reviewed and nothing nefarious was found, so at least this process did not introduce any obvious attacks against build hosts through supply chain contamination.
- @gsora has added the `hidapi` - apps can now register a HID descriptor for custom interactions over USB. See `apps/hidv2` for democumentation.
- change kernel and loader targets to riscv-unknown-elf-none because `xous` is now a proper target (required for Rust 1.76 compatibility)
- `curve25519-dalek` API is now at 4.1.2, thanks to @kotval for pulling it together. The new API removes `engine-25519` and rolls hardware allocate/release into the forked crate, similar to how sha2 was ported. `engine-25519` crate now removed from source tree, as it is now depracted since all the functionality was pulled into `curve25519-dalek`.
## Roadmap
- Lots of testing and bug fixes

View file

@ -1,36 +0,0 @@
[package]
name = "engine-25519"
version = "0.1.0"
authors = ["bunnie <bunnie@kosagi.com>"]
edition = "2018"
description = "Curve25519 Engine"
# Dependency versions enforced by Cargo.lock.
[dependencies]
xous = "0.9.58"
log-server = { package = "xous-api-log", version = "0.1.54" }
ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.54" }
xous-names = { package = "xous-api-names", version = "0.9.56" }
log = "0.4.14"
susres = { package = "xous-api-susres", version = "0.9.54" }
llio = { path = "../llio" } # temporary for testing some power stuff
utralib = { version = "0.1.24", optional = true, default-features = false }
xous-ipc = "0.9.58"
num-derive = { version = "0.3.3", default-features = false }
num-traits = { version = "0.2.14", default-features = false }
rkyv = { version = "0.4.3", default-features = false, features = [
"const_generics",
] }
[dependencies.engine25519-as]
git = "https://github.com/betrusted-io/engine25519-as.git"
rev = "775e8406eb4aad08f05ae10619fcb4ca891ba0a6"
default-features = false
features = []
[features]
precursor = ["utralib/precursor"]
hosted = ["utralib/hosted"]
renode = ["utralib/renode"]
default = []

View file

@ -1,5 +0,0 @@
# Curve 25519 engine
Please refer to the [datasheet](https://ci.betrusted.io/betrusted-soc/doc/engine.html) for comprehensive documentation
on how the hardware engine itself works.

View file

@ -1,93 +0,0 @@
pub(crate) const SERVER_NAME_ENGINE25519: &str = "_Curve-25519 Accelerator Engine_";
// I don't understand why clippy says these are unused. But clippy is wrong. Shut up, clippy.
#[allow(dead_code)]
pub(crate) const UCODE_U8_BASE: usize = 0x0;
#[allow(dead_code)]
pub(crate) const UCODE_U32_BASE: usize = 0x0;
#[allow(dead_code)]
pub(crate) const UCODE_U32_SIZE: usize = 0x1000 / 4;
#[allow(dead_code)]
pub(crate) const UCODE_U8_SIZE: usize = 0x1000;
#[allow(dead_code)]
pub(crate) const RF_U8_BASE: usize = 0x1_0000;
#[allow(dead_code)]
pub(crate) const RF_U32_BASE: usize = 0x1_0000 / 4;
#[allow(dead_code)]
pub(crate) const RF_TOTAL_U32_SIZE: usize = 0x4000 / 4;
#[allow(dead_code)]
pub(crate) const RF_TOTAL_U8_SIZE: usize = 0x4000;
pub(crate) const NUM_REGS: usize = 32;
pub(crate) const BITWIDTH: usize = 256;
#[allow(dead_code)] // not used in hosted
pub(crate) const NUM_WINDOWS: usize = 16;
pub const RF_SIZE_IN_U32: usize = NUM_REGS * (BITWIDTH / 32); // 32 registers, 256 bits/register/32 bits per u32
#[allow(dead_code)] // not used in hosted
pub const TOTAL_RF_SIZE_IN_U32: usize = NUM_REGS * (BITWIDTH / 32) * NUM_WINDOWS; // 32 registers, 256 bits/register/32 bits per u32, times 16 windows
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone, Copy)]
pub struct Job {
/// If present the SID of the server to which we should return results asynchronously.
/// If None, then the job will run synchronously.
pub id: Option<[u32; 4]>,
/// start location for microcode load
pub uc_start: u32,
/// length of the microcode to run
pub uc_len: u32,
/// microcode program
pub ucode: [u32; 1024],
/// initial register file contents (also contains any arguments to the program)
pub rf: [u32; RF_SIZE_IN_U32],
/// which register window, if any, to use for the job
pub window: Option<u8>,
}
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Clone, Copy)]
pub struct MontgomeryJob {
pub x0_u: [u8; 32],
pub x0_w: [u8; 32],
pub x1_u: [u8; 32],
pub x1_w: [u8; 32],
pub affine_u: [u8; 32],
pub scalar: [u8; 32],
}
mod rkyv_enum;
pub use rkyv_enum::*;
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum Opcode {
/// Runs a job, if the server is not already occupied
RunJob,
/// MontgomeryJob
MontgomeryJob,
/// a function that can be polled to determine if the block has been currently acquired
IsFree,
/// IRQ handler feedback
EngineDone,
IllegalOpcode,
// note: suspend/resume handled by a separate thread and server
/// exit the server
Quit,
// note that suspend/resume is handled by a secondary thread that can concurrently
// interrupt the main thread and store the state
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum Return {
Result,
Quit,
}
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
pub(crate) enum SusResOps {
/// Suspend/resume callback
SuspendResume,
/// exit the thread
Quit,
}

View file

@ -1,26 +0,0 @@
// As of Rust 1.64.0:
//
// Rkyv-derived enums throw warnings that rkyv::Archive derived enums are never used
// and I can't figure out how to make them go away. Since they spam the build log,
// rkyv-derived enums are now isolated to their own file with a file-wide `dead_code`
// allow on top.
//
// This might be a temporary compiler regression, or it could just
// be yet another indicator that it's time to upgrade rkyv. However, we are waiting
// until rkyv hits 0.8 (the "shouldn't ever change again but still not sure enough
// for 1.0") release until we rework the entire system to chase the latest rkyv.
// As of now, the current version is 0.7.x and there isn't a timeline yet for 0.8.
#![allow(dead_code)]
#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub enum JobResult {
/// returns a copy of the entire register file as a result
Result([u32; crate::RF_SIZE_IN_U32]),
SingleResult([u8; 32]),
Started,
EngineUnavailable,
NotAsyncObject, // attempt to run an async job on an object that was setup for sync jobs
IllegalOpcodeException,
SuspendError,
}

View file

@ -1,202 +0,0 @@
#![cfg_attr(target_os = "none", no_std)]
//! Detailed docs are parked under Structs/Engine25519 down below
/*
#[cfg(any(feature="precursor", feature="renode"))]
pub use curve25519_dalek_hw::*;
#[cfg(not(target_os = "xous"))]
pub use curve25519_dalek::*;
*/
pub mod api;
pub use api::*;
use num_traits::*;
use xous::{Message, CID};
use xous_ipc::Buffer;
static mut ENGINE_CB: Option<fn(JobResult)> = None;
#[doc = include_str!("../README.md")]
pub struct Engine25519 {
conn: CID,
cb_sid: Option<[u32; 4]>,
}
impl Engine25519 {
// this is used to set up a system with async callbacks
pub fn new_async(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let conn = xns
.request_connection_blocking(api::SERVER_NAME_ENGINE25519)
.expect("Can't connect to Engine25519 server");
let sid = xous::create_server().unwrap();
let sid_tuple = sid.to_u32();
xous::create_thread_4(
engine_cb_server,
sid_tuple.0 as usize,
sid_tuple.1 as usize,
sid_tuple.2 as usize,
sid_tuple.3 as usize,
)
.unwrap();
Ok(Engine25519 { conn, cb_sid: Some(sid.to_array()) })
}
// typically, we just want synchronous callbacks. This integrates more nicely into single-threaded code.
pub fn new() -> Self {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let xns = xous_names::XousNames::new().unwrap();
let conn = xns
.request_connection_blocking(api::SERVER_NAME_ENGINE25519)
.expect("Can't connect to Engine25519 server");
Engine25519 { conn, cb_sid: None }
}
/// this is unsafe because the caller must make sure that within their process space, multiple threads
/// are not spawing concurrent jobs; or, if they do, they all share a common result_callback method:
/// we always blindly replace result_callback!
pub unsafe fn spawn_async_job(
&mut self,
job: &mut Job,
result_callback: fn(JobResult),
) -> Result<bool, xous::Error> {
if let Some(cb_sid) = self.cb_sid {
ENGINE_CB = Some(result_callback); // this is the unsafe bit!
job.id = Some(cb_sid);
let mut buf = Buffer::into_buf(*job).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.conn, Opcode::RunJob.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
match buf.to_original().unwrap() {
JobResult::Started => Ok(true),
_ => Ok(false),
}
} else {
Err(xous::Error::InvalidSyscall)
}
}
pub fn montgomery_job(&mut self, job: MontgomeryJob) -> Result<[u8; 32], xous::Error> {
let mut buf = Buffer::into_buf(job).or(Err(xous::Error::OutOfMemory))?;
match buf.lend_mut(self.conn, Opcode::MontgomeryJob.to_u32().unwrap()) {
Ok(_) => (),
Err(e) => {
if e == xous::Error::ServerNotFound {
log::error!(
"Looks like another thread called disconnect() on us while we weren't looking: {:?}",
e
);
} else {
log::error!("couldn't lend buffer: {:?}", e);
}
return Err(e);
}
}
match buf.to_original().unwrap() {
JobResult::SingleResult(r) => Ok(r),
JobResult::EngineUnavailable => {
log::debug!("spawn job: engine unavailable");
Err(xous::Error::ServerQueueFull)
}
JobResult::IllegalOpcodeException => {
log::error!("spawn job: illegal opcode");
Err(xous::Error::InvalidString)
}
_ => {
log::error!("spawn job: other error");
Err(xous::Error::UnknownError)
}
}
}
/// this is a blocking version of spawn_async_job.
/// if the engine is free, it will block until a result is returned
/// if the engine is busy, it will return an EngineUnavailable result.
pub fn spawn_job(&mut self, job: Job) -> Result<[u32; RF_SIZE_IN_U32], xous::Error> {
if job.id.is_some() {
log::error!("spawn sync job: don't set a job id if you want a synchronous job!");
return Err(xous::Error::InvalidSyscall); // the job.id should be None for a sync job. Do not set it to Some, even as a joke.
}
let mut buf = Buffer::into_buf(job).or(Err(xous::Error::OutOfMemory))?;
match buf.lend_mut(self.conn, Opcode::RunJob.to_u32().unwrap()) {
Ok(_) => (),
Err(e) => {
if e == xous::Error::ServerNotFound {
log::error!(
"Looks like another thread called disconnect() on us while we weren't looking: {:?}",
e
);
} else {
log::error!("couldn't lend buffer: {:?}", e);
}
return Err(e);
}
}
match buf.to_original().unwrap() {
JobResult::Result(rf) => Ok(rf),
JobResult::EngineUnavailable => {
log::debug!("spawn job: engine unavailable");
Err(xous::Error::ServerQueueFull)
}
JobResult::IllegalOpcodeException => {
log::error!("spawn job: illegal opcode");
Err(xous::Error::InvalidString)
}
_ => {
log::error!("spawn job: other error");
Err(xous::Error::UnknownError)
}
}
}
}
use core::sync::atomic::{AtomicU32, Ordering};
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
impl Drop for Engine25519 {
fn drop(&mut self) {
// disconnect from the main server, only if there are no more instances active
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
unsafe {
xous::disconnect(self.conn).unwrap();
}
}
// disconnect and destroy the callback server that is specific to this instance
if let Some(cb_sid) = self.cb_sid {
let cid = xous::connect(xous::SID::from_array(cb_sid)).unwrap();
xous::send_message(cid, Message::new_scalar(Return::Quit.to_usize().unwrap(), 0, 0, 0, 0))
.unwrap();
unsafe {
xous::disconnect(cid).unwrap();
}
}
}
}
/// handles callback messages from the engine server, in the library user's process space.
fn engine_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
loop {
let msg = xous::receive_message(sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(Return::Result) => {
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let job_result = buffer.to_original::<JobResult, _>().unwrap();
unsafe {
if let Some(cb) = ENGINE_CB {
cb(job_result)
}
}
}
Some(Return::Quit) => {
break; // this exits the loop and kills the thread
}
None => (),
}
}
xous::destroy_server(sid).unwrap();
}

File diff suppressed because it is too large Load diff

View file

@ -71,8 +71,9 @@ byteorder = "1.4.3" # used by keywrap
hex = { version = "0.4.3", default-features = false, features = [] }
[dependencies.curve25519-dalek]
version = "=4.1.2" # 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
features = ["auto-release", "warn-fallback"]
[dependencies.ed25519-dalek]
version = "2.1.0"

View file

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

View file

@ -100,8 +100,9 @@ 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.2" # 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
features = ["auto-release", "warn-fallback"]
[dependencies.x25519-dalek]
version = "=2.0.1"

View file

@ -41,7 +41,9 @@ fn vector_read(word_offset: usize) -> u32 {
fn run_vectors() -> (usize, usize) {
use curve25519_dalek::backend::serial::u32e::*;
ensure_engine();
while ensure_engine().is_err() {
xous::yield_slice();
}
// safety: it's safe because it's after ensure_engine()
let ucode_hw = unsafe { get_ucode() };
let rf_hw = unsafe { get_rf() };
@ -116,6 +118,10 @@ benchmark notes:
+59mA +/-1mA current draw off fully charged battery when running the benchmark
1246-1261ms/check vector iteration (10 iters total, 1450 vectors total)
With new curve25519 api:
53.6ms/check vector iteration (10 iters total, 1450 vectors total) with engine retained
56.5ms/check with auto-free
*/
pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
@ -160,6 +166,9 @@ pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
13.54ms/2xop (200 iters -hw) -- microcode pre-loaded, minimal registers transferred using MontgomeryJob call
12.2ms/2xop (200 iters -hw) -- with L2 cache on (128k)
12.29ms/2xop (200 iters -hw) -- with L2 cache on (64k)
8.37ms/2xop (200 iters - hw) - with new curve25519 lib and engine retained after every loop
33.04ms/2xop (200 iters - hw) - with new curve25519 lib and auto-free engine after every loop
*/
Some(BenchOp::StartDh) => {
let mut passes = 0;