Browse Source

Add basic pubkey export.

master
Thomas Kerber 3 months ago
parent
commit
fc4caedbbc
Signed by: Thomas Kerber <t.kerber@ed.ac.uk> GPG Key ID: 8489B911F9ED617B
3 changed files with 41 additions and 5 deletions
  1. 16
    0
      ecc/src/lib.rs
  2. 15
    5
      libecc-sys/src/lib.rs
  3. 10
    0
      src/main.rs

+ 16
- 0
ecc/src/lib.rs View File

@@ -17,6 +17,8 @@ pub struct Signer(ec_sign_context);

type KeySize = UInt<UInt<UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B0>, B0>, B0>, B0>;
const KEYSIZE: usize = 48;
type PubKeySize = UInt<KeySize, B0>;
const PUBKEYSIZE: usize = 96;
type SigSize = UInt<KeySize, B0>;
const SIGSIZE: usize = 96;

@@ -94,6 +96,20 @@ impl PrivateKey {
}
}

impl PublicKey {
pub fn export(&self) -> GenericArray<u8, PubKeySize> {
let mut buf: [u8; PUBKEYSIZE];
unsafe {
buf = uninitialized();
let mut aff: aff_pt = uninitialized();
prj_pt_to_aff(&mut aff, &self.0.y);
nn_export_to_buf(buf.as_mut_ptr(), KEYSIZE as u16, &aff.x.fp_val);
nn_export_to_buf(buf[KEYSIZE..].as_mut_ptr(), KEYSIZE as u16, &aff.y.fp_val);
}
GenericArray::clone_from_slice(&buf[..])
}
}

impl Signer {
pub fn new(kp: &KeyPair) -> Self {
let mut ctx: ec_sign_context;

+ 15
- 5
libecc-sys/src/lib.rs View File

@@ -62,21 +62,29 @@ pub struct ec_params {
pub struct ec_str_params { _private: [u8; 0] }

#[repr(C)]
struct nn {
pub struct nn {
val: [usize; FIELD_SIZE],
magic : usize,
wlen: u8,
}

#[repr(C)]
struct fp {
fp_val: nn,
pub struct fp {
pub fp_val: nn,
ctx: *const fp_ctx,
magic: usize,
}

#[repr(C)]
struct prj_pt {
pub struct aff_pt {
pub x: fp,
pub y: fp,
crv: *const ec_shortw_crv,
magic: usize,
}

#[repr(C)]
pub struct prj_pt {
X: fp,
Y: fp,
Z: fp,
@@ -96,7 +104,7 @@ pub struct ec_priv_key {
pub struct ec_pub_key {
key_type: ec_sig_alg_type,
params: *const ec_params,
y: prj_pt,
pub y: prj_pt,
magic: usize,
}

@@ -196,4 +204,6 @@ extern "C" {
) -> c_void;
pub fn ec_get_curve_params_by_type(ec_type: ec_curve_type)
-> *const ec_str_params;
pub fn prj_pt_to_aff(out: *mut aff_pt, inp: *const prj_pt) -> c_void;
pub fn nn_export_to_buf(buf: *mut u8, buflen: u16, in_nn: *const nn) -> c_void;
}

+ 10
- 0
src/main.rs View File

@@ -17,6 +17,7 @@ use stm32f4::stm32f401 as chip;
use hal::prelude::*;
use hal::timer::Timer;
use hal::nb::block;
use hal::serial::{self, Serial};
//use hal::i2c::I2c;
//use ssd1306::prelude::*;
//use ssd1306::Builder;
@@ -64,11 +65,20 @@ fn main() -> ! {
let clocks = rcc.cfgr.freeze();

// Acquire the GPIOb peripheral
let gpioa = dp.GPIOA.split();
let gpiob = dp.GPIOB.split();

// Configure gpio C pin 13 as a push-pull output.
let mut red_led = gpiob.pb2.into_push_pull_output();
let mut green_led = gpiob.pb12.into_push_pull_output();
let mut serial = Serial::usart6(
dp.USART6,
(gpioa.pa11.into_alternate_af8(), gpioa.pa12.into_alternate_af8()),
serial::config::Config::default(),
clocks).unwrap();
for b in b"hello, world" {
serial.write(*b).unwrap();
}
let kp = Curve::init().import_keypair(
GenericArray::from_slice(&memory::ECDSA_SECRET_KEY[..]));
// Configure the syst timer to trigger an update every second

Loading…
Cancel
Save