blob: 66b29a45074aec27f8b594efdb6a908c71888174 [file] [log] [blame]
Paul Crowley9da969e2022-09-16 23:42:24 +00001// Copyright (C) 2022 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{fs::File, io::Read};
16
17use anyhow::{ensure, Result};
18use log::debug;
19use tokio::io::AsyncReadExt;
20
21use crate::drbg;
22
23const SEED_FOR_CLIENT_LEN: usize = 496;
24const NUM_REQUESTS_PER_RESEED: u32 = 256;
25
26pub struct Conditioner {
27 hwrng: tokio::fs::File,
28 rg: drbg::Drbg,
29 requests_since_reseed: u32,
30}
31
32impl Conditioner {
33 pub fn new(mut hwrng: File) -> Result<Conditioner> {
34 let mut et: drbg::Entropy = [0; drbg::ENTROPY_LEN];
35 hwrng.read_exact(&mut et)?;
36 let rg = drbg::Drbg::new(&et)?;
37 Ok(Conditioner { hwrng: tokio::fs::File::from_std(hwrng), rg, requests_since_reseed: 0 })
38 }
39
40 pub async fn reseed_if_necessary(&mut self) -> Result<()> {
41 if self.requests_since_reseed >= NUM_REQUESTS_PER_RESEED {
42 debug!("Reseeding DRBG");
43 let mut et: drbg::Entropy = [0; drbg::ENTROPY_LEN];
44 self.hwrng.read_exact(&mut et).await?;
45 self.rg.reseed(&et)?;
46 self.requests_since_reseed = 0;
47 }
48 Ok(())
49 }
50
51 pub fn request(&mut self) -> Result<[u8; SEED_FOR_CLIENT_LEN]> {
52 ensure!(self.requests_since_reseed < NUM_REQUESTS_PER_RESEED, "Not enough reseeds");
53 let mut seed_for_client = [0u8; SEED_FOR_CLIENT_LEN];
54 self.rg.generate(&mut seed_for_client)?;
55 self.requests_since_reseed += 1;
56 Ok(seed_for_client)
57 }
58}