Skip to content

Commit 9ecac1c

Browse files
Merge pull request #27 from sigpwny/harden-timer
Harden timer behavior
2 parents a502fbc + 5f3a0c9 commit 9ecac1c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

docker_env/src/bin/car.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use embedded_hal::digital::v2::OutputPin;
66

77
use tiva::{
88
driverlib::*,
9-
log, setup_board, Board, words_to_bytes, Signer, Verifier, get_combined_entropy
9+
log, setup_board, Board, words_to_bytes, Signer, Verifier, get_combined_entropy, get_timer_entropy
1010
};
1111

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

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

9394
loop {
@@ -97,7 +98,7 @@ fn main() -> ! {
9798
MAGIC_UNLOCK_REQ => {
9899
// log!("Car: Received UNLOCK_REQ");
99100
board.led_blue.set_high().unwrap();
100-
unlock_start(&mut rng, &mut board);
101+
unlock_start(&mut rng, &mut board, &mut timer_entropy);
101102
board.led_blue.set_low().unwrap();
102103
}
103104
_ => {
@@ -109,12 +110,16 @@ fn main() -> ! {
109110
}
110111

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

117+
// Update timer entropy
118+
let new_timer_entropy = get_timer_entropy();
119+
*timer_entropy ^= u64::from_ne_bytes(new_timer_entropy[0..8].try_into().unwrap());
120+
116121
// Initialize car nonce with random value :) it's very random
117-
let mut car_nonce: u64 = rng.next_u64() ^ get_tick_timer();
122+
let mut car_nonce: u64 = rng.next_u64() ^ *timer_entropy;
118123
let car_nonce_b: [u8; 8] = car_nonce.to_be_bytes();
119124

120125
// Get car secret key

0 commit comments

Comments
 (0)