1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use crate::prf;
6use bytes::{BufMut, BytesMut};
7use fuchsia_sync::Mutex;
89use ieee80211::MacAddr;
10use num::bigint::BigUint;
11use rand::rngs::OsRng;
12use rand::Rng as _;
13use std::sync::Arc;
1415pub type Nonce = [u8; 32];
1617/// Thread-safe nonce generator.
18/// According to IEEE Std 802.11-2016, 12.7.5 each STA should be configured with an initial,
19/// cryptographic-quality random counter at system boot up time.
20#[derive(Debug)]
21pub struct NonceReader {
22 key_counter: Mutex<BigUint>,
23}
2425impl NonceReader {
26pub fn new(sta_addr: &MacAddr) -> Result<Arc<NonceReader>, anyhow::Error> {
27// Write time and STA's address to buffer for PRF-256.
28 // It's unclear whether or not using PRF has any significant cryptographic advantage.
29 // For the time being, follow IEEE's recommendation for nonce generation.
30 // IEEE Std 802.11-2016, 12.7.5 recommends using a time in NTP format.
31 // Fuchsia has no support for NTP yet; instead use a regular timestamp.
32 // TODO(https://fxbug.dev/42124853): Use time in NTP format once Fuchsia added support.
33let mut buf = BytesMut::with_capacity(14);
34let epoch_nanos = zx::MonotonicInstant::get().into_nanos();
35 buf.put_i64_le(epoch_nanos);
36 buf.put_slice(sta_addr.as_slice());
37let k = OsRng.gen::<[u8; 32]>();
38let init = prf::prf(&k[..], "Init Counter", &buf[..], 8 * std::mem::size_of_val(&k))?;
39Ok(Arc::new(NonceReader { key_counter: Mutex::new(BigUint::from_bytes_le(&init[..])) }))
40 }
4142pub fn next(&self) -> Nonce {
43let mut counter = self.key_counter.lock();
44*counter += 1u8;
4546// Expand nonce if it's less than 32 bytes.
47let mut result = (*counter).to_bytes_le();
48 result.resize(32, 0);
49let mut nonce = Nonce::default();
50 nonce.copy_from_slice(&result[..]);
51 nonce
52 }
53}
5455#[cfg(test)]
56mod tests {
57use super::*;
5859#[test]
60fn test_next_nonce() {
61let addr = MacAddr::from([1, 2, 3, 4, 5, 6]);
62let rdr = NonceReader::new(&addr).expect("error creating NonceReader");
63let mut previous_nonce = rdr.next();
64for _ in 0..300 {
65let nonce = rdr.next();
66let nonce_int = BigUint::from_bytes_le(&nonce[..]);
67let previous_nonce_int = BigUint::from_bytes_le(&previous_nonce[..]);
68assert_eq!(nonce_int.gt(&previous_nonce_int), true);
6970 previous_nonce = nonce;
71 }
72 }
73}