mirror of
https://github.com/saymrwulf/betrusted-curve25519-dalek-source.git
synced 2026-09-08 21:00:38 +00:00
initial swag at gluing things together
Note: breaks for anyone's build environment but mine!
This commit is contained in:
parent
6e2cf5ab43
commit
0b0cda3118
3 changed files with 70 additions and 95 deletions
11
Cargo.toml
11
Cargo.toml
|
|
@ -51,15 +51,14 @@ serde = { version = "1.0", default-features = false, optional = true, features =
|
||||||
packed_simd = { version = "0.3.4", package = "packed_simd_2", features = ["into_bits"], optional = true }
|
packed_simd = { version = "0.3.4", package = "packed_simd_2", features = ["into_bits"], optional = true }
|
||||||
zeroize = { version = "1", default-features = false }
|
zeroize = { version = "1", default-features = false }
|
||||||
rand = { version = "0.7", default-features = false, features = [] }
|
rand = { version = "0.7", default-features = false, features = [] }
|
||||||
volatile = "0.2.6"
|
|
||||||
#pac = { path = "../../../../sim_support/rust/pac" } # used in test simulation environment, need to figure out how to make a switch for this
|
|
||||||
betrusted-pac = { path = "../betrusted-pac" }
|
|
||||||
#pac = { package = "betrusted-pac" }
|
|
||||||
fiat-crypto = { version = "0.1.6", optional = true}
|
fiat-crypto = { version = "0.1.6", optional = true}
|
||||||
|
xous-engine-25519 = {path="../xous-engine-25519"}
|
||||||
|
engine-25519 = {path = "../xous-core/services/engine-25519"}
|
||||||
|
|
||||||
[dependencies.engine25519-as]
|
[dependencies.engine25519-as]
|
||||||
git="https://github.com/betrusted-io/engine25519-as.git"
|
#git="https://github.com/betrusted-io/engine25519-as.git"
|
||||||
rev="0bafc1ff8f58b66189d8c2f9ab7046cbbef77e6e"
|
#rev="0bafc1ff8f58b66189d8c2f9ab7046cbbef77e6e"
|
||||||
|
path="../engine25519-as"
|
||||||
default-features = false
|
default-features = false
|
||||||
features = []
|
features = []
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -67,8 +67,7 @@ pub(crate) mod macros;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate engine25519_as;
|
extern crate engine25519_as;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
extern crate volatile;
|
extern crate xous_engine_25519;
|
||||||
extern crate betrusted_pac;
|
|
||||||
|
|
||||||
//------------------------------------------------------------------------
|
//------------------------------------------------------------------------
|
||||||
// curve25519-dalek public modules
|
// curve25519-dalek public modules
|
||||||
|
|
|
||||||
|
|
@ -293,34 +293,23 @@ pub fn differential_add_and_double(
|
||||||
Q.W = t17; // W_{Q'} = U_D * 4 (W_P U_Q - U_P W_Q)^2
|
Q.W = t17; // W_{Q'} = U_D * 4 (W_P U_Q - U_P W_Q)^2
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_to_rf(bytes: [u8; 32], register: usize) {
|
fn copy_to_rf(bytes: [u8; 32], register: usize, rf: &mut [u32; xous_engine_25519::RF_SIZE_IN_U32]) {
|
||||||
use volatile::Volatile;
|
use core::convert::TryInto;
|
||||||
let rf_ptr: *mut u32 = 0xe003_0000 as *mut u32;
|
for (byte, rf_dst) in bytes.chunks_exact(4).zip(rf[register * 8..(register+1)*8].iter_mut()) {
|
||||||
let rf = rf_ptr as *mut Volatile<u32>;
|
*rf_dst = u32::from_le_bytes(byte.try_into().expect("chunks_exact failed us"));
|
||||||
for word in 0..8 {
|
|
||||||
let mut temp: [u8; 4] = [0; 4];
|
|
||||||
for i in 0..4 {
|
|
||||||
temp[i] = bytes[word*4 + i];
|
|
||||||
}
|
|
||||||
unsafe { (*( rf.add( (register * 8 + word) as usize )) ).write( u32::from_le_bytes(temp) ); }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn copy_from_rf(register: usize) -> [u8; 32] {
|
fn copy_from_rf(register: usize, rf: &[u32; xous_engine_25519::RF_SIZE_IN_U32]) -> [u8; 32] {
|
||||||
use volatile::Volatile;
|
let mut ret: [u8; 32] = [0; 32];
|
||||||
let rf_ptr: *mut u32 = 0xe003_0000 as *mut u32;
|
|
||||||
let rf = rf_ptr as *mut Volatile<u32>;
|
for (src, dst) in rf[register*8 .. (register+1)*8].iter().zip(ret.chunks_exact_mut(4).into_iter()) {
|
||||||
let mut bytes: [u8; 32] = [0; 32];
|
for (&src_byte, dst_byte) in src.to_le_bytes().iter().zip(dst.iter_mut()) {
|
||||||
for word in 0..8 {
|
*dst_byte = src_byte;
|
||||||
unsafe{
|
|
||||||
let value: u32 = (*( rf.add( (register * 8 + word) as usize ))).read();
|
|
||||||
let b = value.to_le_bytes();
|
|
||||||
for i in 0..4 {
|
|
||||||
bytes[word*4 + i] = b[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bytes
|
|
||||||
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn differential_add_and_double_hw(
|
pub fn differential_add_and_double_hw(
|
||||||
|
|
@ -328,8 +317,6 @@ pub fn differential_add_and_double_hw(
|
||||||
Q: &mut ProjectivePoint,
|
Q: &mut ProjectivePoint,
|
||||||
affine_PmQ: &FieldElement,
|
affine_PmQ: &FieldElement,
|
||||||
) {
|
) {
|
||||||
use volatile::Volatile;
|
|
||||||
let p = unsafe { betrusted_pac::Peripherals::steal() };
|
|
||||||
let mcode = assemble_engine25519!(
|
let mcode = assemble_engine25519!(
|
||||||
start:
|
start:
|
||||||
// P.U in %20
|
// P.U in %20
|
||||||
|
|
@ -411,18 +398,20 @@ pub fn differential_add_and_double_hw(
|
||||||
|
|
||||||
fin // finish execution
|
fin // finish execution
|
||||||
);
|
);
|
||||||
let microcode_ptr: *mut u32 = 0xe002_0000 as *mut u32;
|
//let mut engine = xous_engine_25519::XousEngine25519::new();
|
||||||
let microcode = microcode_ptr as *mut Volatile<u32>;
|
let mut engine = engine_25519::Engine25519::new();
|
||||||
|
|
||||||
// copy the microcode in -- later on we can optimize this so it's only done once?
|
let mut job = xous_engine_25519::Job {
|
||||||
for i in 0..mcode.len() as usize {
|
id: None,
|
||||||
unsafe { (*(microcode.add(i))).write( mcode[i] ); }
|
ucode: [0; 1024],
|
||||||
}
|
uc_len: mcode.len() as u32,
|
||||||
// setup the engine microcode parameters
|
uc_start: 0,
|
||||||
unsafe{
|
window: Some(0),
|
||||||
p.ENGINE.window.write(|w| w.bits(0));
|
rf: [0; xous_engine_25519::RF_SIZE_IN_U32],
|
||||||
p.ENGINE.mpstart.write(|w| w.bits(0));
|
};
|
||||||
p.ENGINE.mplen.write(|w| w.bits(mcode.len() as u32));
|
|
||||||
|
for (&src, dst) in mcode.iter().zip(job.ucode.iter_mut()) {
|
||||||
|
*dst = src;
|
||||||
}
|
}
|
||||||
|
|
||||||
// P.U in %20
|
// P.U in %20
|
||||||
|
|
@ -430,25 +419,19 @@ pub fn differential_add_and_double_hw(
|
||||||
// Q.U in %22
|
// Q.U in %22
|
||||||
// Q.W in %23
|
// Q.W in %23
|
||||||
// affine_PmQ in %24
|
// affine_PmQ in %24
|
||||||
copy_to_rf(P.U.to_bytes(), 20);
|
copy_to_rf(P.U.to_bytes(), 20, &mut job.rf);
|
||||||
copy_to_rf(P.W.to_bytes(), 21);
|
copy_to_rf(P.W.to_bytes(), 21, &mut job.rf);
|
||||||
copy_to_rf(Q.U.to_bytes(), 22);
|
copy_to_rf(Q.U.to_bytes(), 22, &mut job.rf);
|
||||||
copy_to_rf(Q.W.to_bytes(), 23);
|
copy_to_rf(Q.W.to_bytes(), 23, &mut job.rf);
|
||||||
copy_to_rf(affine_PmQ.to_bytes(), 24);
|
copy_to_rf(affine_PmQ.to_bytes(), 24, &mut job.rf);
|
||||||
|
|
||||||
// start the run
|
// start the run
|
||||||
p.ENGINE.control.write(|w| w.go().set_bit());
|
let result_rf = engine.spawn_job(job).expect("couldn't run engine job");
|
||||||
loop {
|
|
||||||
let status = p.ENGINE.status.read().bits();
|
|
||||||
if (status & 1) == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
P.U = FieldElement::from_bytes(©_from_rf(20));
|
P.U = FieldElement::from_bytes(©_from_rf(20, &result_rf));
|
||||||
P.W = FieldElement::from_bytes(©_from_rf(21));
|
P.W = FieldElement::from_bytes(©_from_rf(21, &result_rf));
|
||||||
Q.U = FieldElement::from_bytes(©_from_rf(22));
|
Q.U = FieldElement::from_bytes(©_from_rf(22, &result_rf));
|
||||||
Q.W = FieldElement::from_bytes(©_from_rf(23));
|
Q.W = FieldElement::from_bytes(©_from_rf(23, &result_rf));
|
||||||
}
|
}
|
||||||
|
|
||||||
define_mul_assign_variants!(LHS = MontgomeryPoint, RHS = Scalar);
|
define_mul_assign_variants!(LHS = MontgomeryPoint, RHS = Scalar);
|
||||||
|
|
@ -470,22 +453,7 @@ impl<'a, 'b> Mul<&'b Scalar> for &'a MontgomeryPoint {
|
||||||
W: FieldElement::one(),
|
W: FieldElement::one(),
|
||||||
};
|
};
|
||||||
|
|
||||||
copy_to_rf(x0.U.to_bytes(), 25);
|
|
||||||
copy_to_rf(x0.W.to_bytes(), 26);
|
|
||||||
copy_to_rf(x1.U.to_bytes(), 27);
|
|
||||||
copy_to_rf(x1.W.to_bytes(), 28);
|
|
||||||
copy_to_rf(affine_u.to_bytes(), 24);
|
|
||||||
copy_to_rf(scalar.bytes, 31);
|
|
||||||
// load the number 254 into the loop index register
|
|
||||||
copy_to_rf([
|
|
||||||
254, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
], 19);
|
|
||||||
|
|
||||||
use volatile::Volatile;
|
|
||||||
let p = unsafe { betrusted_pac::Peripherals::steal() };
|
|
||||||
let mcode = assemble_engine25519!(
|
let mcode = assemble_engine25519!(
|
||||||
start:
|
start:
|
||||||
// P.U in %20
|
// P.U in %20
|
||||||
|
|
@ -624,31 +592,40 @@ impl<'a, 'b> Mul<&'b Scalar> for &'a MontgomeryPoint {
|
||||||
|
|
||||||
fin // finish execution
|
fin // finish execution
|
||||||
);
|
);
|
||||||
let microcode_ptr: *mut u32 = 0xe002_0000 as *mut u32;
|
let mut engine = engine_25519::Engine25519::new();
|
||||||
let microcode = microcode_ptr as *mut Volatile<u32>;
|
let mut job = xous_engine_25519::Job {
|
||||||
|
id: None,
|
||||||
|
ucode: [0; 1024],
|
||||||
|
uc_len: mcode.len() as u32,
|
||||||
|
uc_start: 0,
|
||||||
|
window: Some(0),
|
||||||
|
rf: [0; xous_engine_25519::RF_SIZE_IN_U32],
|
||||||
|
};
|
||||||
|
|
||||||
// copy the microcode in -- later on we can optimize this so it's only done once?
|
for (&src, dst) in mcode.iter().zip(job.ucode.iter_mut()) {
|
||||||
for i in 0..mcode.len() as usize {
|
*dst = src as u32;
|
||||||
unsafe { (*(microcode.add(i))).write( mcode[i] as u32 ); }
|
|
||||||
}
|
|
||||||
// setup the engine microcode parameters
|
|
||||||
unsafe{
|
|
||||||
p.ENGINE.window.write(|w| w.bits(0));
|
|
||||||
p.ENGINE.mpstart.write(|w| w.bits(0));
|
|
||||||
p.ENGINE.mplen.write(|w| w.bits(mcode.len() as u32));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
copy_to_rf(x0.U.to_bytes(), 25, &mut job.rf);
|
||||||
|
copy_to_rf(x0.W.to_bytes(), 26, &mut job.rf);
|
||||||
|
copy_to_rf(x1.U.to_bytes(), 27, &mut job.rf);
|
||||||
|
copy_to_rf(x1.W.to_bytes(), 28, &mut job.rf);
|
||||||
|
copy_to_rf(affine_u.to_bytes(), 24, &mut job.rf);
|
||||||
|
copy_to_rf(scalar.bytes, 31, &mut job.rf);
|
||||||
|
// load the number 254 into the loop index register
|
||||||
|
copy_to_rf([
|
||||||
|
254, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
], 19, &mut job.rf);
|
||||||
|
|
||||||
// start the run
|
// start the run
|
||||||
p.ENGINE.control.write(|w| w.go().set_bit());
|
let result_rf = engine.spawn_job(job).expect("couldn't run engine job");
|
||||||
loop {
|
|
||||||
let status = p.ENGINE.status.read().bits();
|
|
||||||
if (status & 1) == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
x0.U = FieldElement::from_bytes(©_from_rf(25));
|
|
||||||
x0.W = FieldElement::from_bytes(©_from_rf(26));
|
x0.U = FieldElement::from_bytes(©_from_rf(25, &result_rf));
|
||||||
|
x0.W = FieldElement::from_bytes(©_from_rf(26, &result_rf));
|
||||||
|
|
||||||
// TODO: optimize this relatively innocuous looking call.
|
// TODO: optimize this relatively innocuous looking call.
|
||||||
// this consumes about 100ms runtime -- need to implement this using
|
// this consumes about 100ms runtime -- need to implement this using
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue