Skip to content

Commit

Permalink
Merge pull request #27 from sigpwny/harden-timer
Browse files Browse the repository at this point in the history
Harden timer behavior
  • Loading branch information
WhiteHoodHacker authored Mar 12, 2023
2 parents a502fbc + 5f3a0c9 commit 9ecac1c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions docker_env/src/bin/car.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use embedded_hal::digital::v2::OutputPin;

use tiva::{
driverlib::*,
log, setup_board, Board, words_to_bytes, Signer, Verifier, get_combined_entropy
log, setup_board, Board, words_to_bytes, Signer, Verifier, get_combined_entropy, get_timer_entropy
};

use p256_cortex_m4::{SecretKey, Signature, PublicKey};
Expand Down Expand Up @@ -88,6 +88,7 @@ fn main() -> ! {

// Seed RNG with entropy sources
let entropy: [u8; 32] = get_combined_entropy();
let mut timer_entropy: u64 = 0;
let mut rng = rand_chacha::ChaChaRng::from_seed(entropy);

loop {
Expand All @@ -97,7 +98,7 @@ fn main() -> ! {
MAGIC_UNLOCK_REQ => {
// log!("Car: Received UNLOCK_REQ");
board.led_blue.set_high().unwrap();
unlock_start(&mut rng, &mut board);
unlock_start(&mut rng, &mut board, &mut timer_entropy);
board.led_blue.set_low().unwrap();
}
_ => {
Expand All @@ -109,12 +110,16 @@ fn main() -> ! {
}

/// Handle UNLOCK_REQ
fn unlock_start(rng: &mut (impl CryptoRng + RngCore), board: &mut Board) {
fn unlock_start(rng: &mut (impl CryptoRng + RngCore), board: &mut Board, timer_entropy: &mut u64) {
// Start timeout timer for 500ms, need time to rx from fob
start_delay_timer_us(500_000);

// Update timer entropy
let new_timer_entropy = get_timer_entropy();
*timer_entropy ^= u64::from_ne_bytes(new_timer_entropy[0..8].try_into().unwrap());

// Initialize car nonce with random value :) it's very random
let mut car_nonce: u64 = rng.next_u64() ^ get_tick_timer();
let mut car_nonce: u64 = rng.next_u64() ^ *timer_entropy;
let car_nonce_b: [u8; 8] = car_nonce.to_be_bytes();

// Get car secret key
Expand Down

0 comments on commit 9ecac1c

Please sign in to comment.