Browse Source

Minor changes to test environment + ecclib.

master
Thomas Kerber 4 months ago
parent
commit
d3372ab0d4
Signed by: Thomas Kerber <tk@drwx.org> GPG Key ID: 8489B911F9ED617B
3 changed files with 27 additions and 41 deletions
  1. +4
    -21
      ecc/src/lib.rs
  2. +1
    -1
      flash.sh
  3. +22
    -19
      src/main.rs

+ 4
- 21
ecc/src/lib.rs View File

@@ -9,8 +9,6 @@ use generic_array::GenericArray;
use generic_array::typenum::{UInt, UTerm, B0, B1};

pub struct KeyPair(ec_key_pair);
pub struct PrivateKey(ec_priv_key);
pub struct PublicKey(ec_pub_key);

pub struct Curve(ec_params);
pub struct Signer(ec_sign_context);
@@ -70,39 +68,24 @@ impl Curve {
}

impl KeyPair {
pub fn join(priv_key: PrivateKey, pub_key: PublicKey) -> KeyPair {
KeyPair(ec_key_pair {
priv_key: priv_key.0,
pub_key: pub_key.0,
})
}

pub fn split(self) -> (PrivateKey, PublicKey) {
(PrivateKey(self.0.priv_key), PublicKey(self.0.pub_key))
}
}

impl PrivateKey {
pub fn export(&self) -> GenericArray<u8, KeySize> {
pub fn export_private_key(&self) -> GenericArray<u8, KeySize> {
let mut buf: [u8; KEYSIZE];
unsafe {
buf = uninitialized();
ec_priv_key_export_to_buf(
&self.0,
&self.0.priv_key,
buf[..].as_mut_ptr(),
KEYSIZE as u8);
}
GenericArray::clone_from_slice(&buf[..])
}
}

impl PublicKey {
pub fn export(&self) -> GenericArray<u8, PubKeySize> {
pub fn export_public_key(&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);
prj_pt_to_aff(&mut aff, &self.0.pub_key.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);
}

+ 1
- 1
flash.sh View File

@@ -1,5 +1,5 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p bash rustup pkg pkgsCross.arm-embedded.buildPackages.binutils dfu-util
#!nix-shell -i bash -p bash rustup pkgsCross.arm-embedded.buildPackages.gcc dfu-util
cargo build --release
arm-none-eabi-objcopy -O binary target/thumbv7em-none-eabi/release/hedgehog target/hedgehog.bin
sudo dfu-util -S 368D32853436 -a 0 -s 0x08000000 -D target/hedgehog.bin

+ 22
- 19
src/main.rs View File

@@ -76,13 +76,16 @@ fn main() -> ! {
(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[..]));
let curve = Curve::init();
//let kp = Curve::init().generate_keypair();
//let kp = Curve::init().import_keypair(
// GenericArray::from_slice(&memory::ECDSA_SECRET_KEY[..]));
//let pubkey = kp.export_public_key();
//let mut signer = Signer::new(&kp);
//signer.update(&pubkey[..]);
//let sig = signer.sign();
// Configure the syst timer to trigger an update every second
let mut timer = Timer::syst(cp.SYST, 1.hz(), clocks);
let mut timer = Timer::syst(cp.SYST, 10.hz(), clocks);
//let scl = gpiob.pb8.into_alternate_af4();
//let sda = gpiob.pb9.into_alternate_af4();
//let i2c = I2c::i2c1(
@@ -99,25 +102,21 @@ fn main() -> ! {

// Wait for the timer to trigger an update and change the state of the LED
let mut i: u32 = 0;
let key = GenericArray::from_slice(&[0u8; 32]);
let cipher = Aes256::new(&key);
loop {
block!(timer.wait()).unwrap();
i = u32::wrapping_add(i, 1);
let key = GenericArray::from_slice(&[0u8; 32]);
let mut block = GenericArray::clone_from_slice(&[0u8; 16]);
for (n, b) in i.to_ne_bytes()[..].iter().zip(block.iter_mut()) {
*b = *n;
//for b in pubkey.iter().chain(sig.iter()) {
for b in b"hello" {
block!(serial.write(*b)).ok();
}
let cipher = Aes256::new(&key);
cipher.encrypt_block(&mut block);
let mut signer = Signer::new(&kp);
signer.update(&block[..]);
let sig = signer.sign();
if (sig[0] & 1) != 0 {
block!(timer.wait()).unwrap();
let block = ctr_otp(&cipher, &mut i);
if (block[0] & 1) != 0 {
red_led.set_high();
} else {
red_led.set_low();
}
if (sig[0] & 2) != 0 {
if (block[0] & 2) != 0 {
green_led.set_high();
} else {
green_led.set_low();
@@ -129,5 +128,9 @@ use core::panic::PanicInfo;

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
let dp = unsafe { chip::Peripherals::steal() };
let gpiob = dp.GPIOB.split();
let mut red_led = gpiob.pb2.into_push_pull_output();
red_led.set_high();
loop {}
}

Loading…
Cancel
Save