|
1 | 1 | use core::fmt::{self, Debug}; |
2 | 2 | use hmac::{ |
3 | | - KeyInit, SimpleHmacReset, |
4 | | - digest::{Digest, FixedOutputReset, Mac, array::Array, block_api::BlockSizeUser}, |
| 3 | + KeyInit, SimpleHmac, |
| 4 | + digest::{Digest, Mac, OutputSizeUser, array::Array, block_api::BlockSizeUser}, |
5 | 5 | }; |
6 | 6 |
|
7 | | -/// Implementation of `HMAC_DRBG` as described in NIST SP800-90A. |
| 7 | +/// Implementation of [NIST SP800-90A]'s `HMAC_DRBG` HMAC-based deterministic random bit generator. |
8 | 8 | /// |
9 | | -/// <https://csrc.nist.gov/publications/detail/sp/800-90a/rev-1/final> |
10 | | -/// |
11 | | -/// This is a HMAC-based deterministic random bit generator. |
12 | | -// TODO(tarcieri): extract this into its own crate |
| 9 | +/// [NIST SP800-90A]: https://csrc.nist.gov/publications/detail/sp/800-90a/rev-1/final |
| 10 | +// TODO(tarcieri): extract this into the `hmacdrbg` crate: RustCrypto/CSRNGs#7 |
13 | 11 | pub(crate) struct HmacDrbg<D> |
14 | 12 | where |
15 | | - D: Digest + BlockSizeUser + FixedOutputReset, |
| 13 | + D: OutputSizeUser, |
16 | 14 | { |
17 | 15 | /// HMAC key `K` (see RFC 6979 Section 3.2.c) |
18 | | - k: SimpleHmacReset<D>, |
| 16 | + k: Array<u8, D::OutputSize>, |
19 | 17 |
|
20 | 18 | /// Chaining value `V` (see RFC 6979 Section 3.2.c) |
21 | 19 | v: Array<u8, D::OutputSize>, |
22 | 20 | } |
23 | 21 |
|
24 | 22 | impl<D> HmacDrbg<D> |
25 | 23 | where |
26 | | - D: Digest + BlockSizeUser + FixedOutputReset, |
| 24 | + D: BlockSizeUser + Digest, |
27 | 25 | { |
28 | 26 | /// Initialize `HMAC_DRBG`. |
29 | 27 | #[must_use] |
30 | | - #[allow(clippy::missing_panics_doc, reason = "should not panic")] |
31 | 28 | pub(crate) fn new(entropy_input: &[u8], nonce: &[u8], personalization_string: &[u8]) -> Self { |
32 | | - let mut k = SimpleHmacReset::new(&Default::default()); |
33 | | - let mut v = Array::default(); |
34 | | - |
35 | | - v.fill(0x01); |
36 | | - |
37 | | - for i in 0..=1 { |
38 | | - k.update(&v); |
39 | | - k.update(&[i]); |
40 | | - k.update(entropy_input); |
41 | | - k.update(nonce); |
42 | | - k.update(personalization_string); |
43 | | - k = SimpleHmacReset::new_from_slice(&k.finalize().into_bytes()).expect("should work"); |
44 | | - |
45 | | - // Steps 3.2.e,g: v = HMAC_k(v) |
46 | | - k.update(&v); |
47 | | - v = k.finalize_reset().into_bytes(); |
48 | | - } |
49 | | - |
50 | | - Self { k, v } |
| 29 | + let mut drbg = Self { |
| 30 | + k: Array::default(), |
| 31 | + v: Array::default(), |
| 32 | + }; |
| 33 | + drbg.v.fill(0x01); |
| 34 | + drbg.update(&[entropy_input, nonce, personalization_string]); |
| 35 | + drbg |
51 | 36 | } |
52 | 37 |
|
53 | 38 | /// Write the next `HMAC_DRBG` output to the given byte slice. |
54 | | - #[allow(clippy::missing_panics_doc, reason = "should not panic")] |
55 | 39 | pub(crate) fn fill_bytes(&mut self, out: &mut [u8]) { |
56 | | - let mut out_chunks = out.chunks_exact_mut(self.v.len()); |
57 | | - |
58 | | - for out_chunk in &mut out_chunks { |
59 | | - self.k.update(&self.v); |
60 | | - self.v = self.k.finalize_reset().into_bytes(); |
| 40 | + for out_chunk in out.chunks_mut(self.v.len()) { |
| 41 | + self.update_v(); |
61 | 42 | out_chunk.copy_from_slice(&self.v[..out_chunk.len()]); |
62 | 43 | } |
63 | 44 |
|
64 | | - let out_remainder = out_chunks.into_remainder(); |
65 | | - if !out_remainder.is_empty() { |
66 | | - self.k.update(&self.v); |
67 | | - self.v = self.k.finalize_reset().into_bytes(); |
68 | | - out_remainder.copy_from_slice(&self.v[..out_remainder.len()]); |
| 45 | + self.update(&[]); |
| 46 | + } |
| 47 | + |
| 48 | + /// Update `K` and `V` using the provided seed material. |
| 49 | + fn update(&mut self, inputs: &[&[u8]]) { |
| 50 | + let mut hmac = self.hmac_k(); |
| 51 | + hmac.update(&self.v); |
| 52 | + hmac.update(&[0x00]); |
| 53 | + inputs.iter().for_each(|&input| hmac.update(input)); |
| 54 | + |
| 55 | + self.k = hmac.finalize().into_bytes(); |
| 56 | + self.update_v(); |
| 57 | + |
| 58 | + if !inputs.is_empty() { |
| 59 | + let mut hmac = self.hmac_k(); |
| 60 | + hmac.update(&self.v); |
| 61 | + hmac.update(&[0x01]); |
| 62 | + inputs.iter().for_each(|&input| hmac.update(input)); |
| 63 | + |
| 64 | + self.k = hmac.finalize().into_bytes(); |
| 65 | + self.update_v(); |
69 | 66 | } |
| 67 | + } |
| 68 | + |
| 69 | + /// Update `V = HMAC(K, V)`. |
| 70 | + fn update_v(&mut self) { |
| 71 | + let mut hmac = self.hmac_k(); |
| 72 | + hmac.update(&self.v); |
| 73 | + self.v = hmac.finalize().into_bytes(); |
| 74 | + } |
70 | 75 |
|
71 | | - self.k.update(&self.v); |
72 | | - self.k.update(&[0x00]); |
73 | | - self.k = SimpleHmacReset::new_from_slice(&self.k.finalize_reset().into_bytes()) |
74 | | - .expect("should work"); |
75 | | - self.k.update(&self.v); |
76 | | - self.v = self.k.finalize_reset().into_bytes(); |
| 76 | + /// Initialize HMAC using the current value of `K`. |
| 77 | + fn hmac_k(&self) -> SimpleHmac<D> { |
| 78 | + SimpleHmac::<D>::new_from_slice(&self.k).expect("HMAC key should be valid") |
77 | 79 | } |
78 | 80 | } |
79 | 81 |
|
80 | 82 | impl<D> Debug for HmacDrbg<D> |
81 | 83 | where |
82 | | - D: Digest + BlockSizeUser + FixedOutputReset, |
| 84 | + D: OutputSizeUser, |
83 | 85 | { |
84 | 86 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
85 | 87 | f.debug_struct("HmacDrbg").finish_non_exhaustive() |
|
0 commit comments