-
Notifications
You must be signed in to change notification settings - Fork 110
/
lib.rs
142 lines (127 loc) · 4.55 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#![doc = include_str!("../README.md")]
//! ## Usage
//!
//! See the documentation for the [`generate_k`] function.
#![no_std]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code, clippy::unwrap_used)]
#![warn(missing_docs, rust_2018_idioms)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
html_root_url = "https://docs.rs/rfc6979/0.1.0"
)]
use crypto_bigint::{ArrayEncoding, ByteArray, Integer};
use hmac::digest::block_buffer::Eager;
use hmac::digest::core_api::{BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore};
use hmac::digest::generic_array::typenum::{IsLess, Le, NonZero, U256};
use hmac::digest::generic_array::GenericArray;
use hmac::digest::{FixedOutput, HashMarker, OutputSizeUser};
use hmac::{Hmac, Mac};
use zeroize::{Zeroize, Zeroizing};
/// Deterministically generate ephemeral scalar `k`.
///
/// Accepts the following parameters and inputs:
///
/// - `x`: secret key
/// - `n`: field modulus
/// - `h`: hash/digest of input message: must be reduced modulo `n` in advance
/// - `data`: additional associated data, e.g. CSRNG output used as added entropy
#[inline]
pub fn generate_k<D, I>(x: &I, n: &I, h: &ByteArray<I>, data: &[u8]) -> Zeroizing<I>
where
D: CoreProxy + FixedOutput<OutputSize = I::ByteSize>,
I: ArrayEncoding + Integer + Zeroize,
D::Core: BlockSizeUser
+ BufferKindUser<BufferKind = Eager>
+ Clone
+ Default
+ FixedOutputCore
+ HashMarker
+ OutputSizeUser<OutputSize = D::OutputSize>,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
let mut x = x.to_be_byte_array();
let mut hmac_drbg = HmacDrbg::<D>::new(&x, h, data);
x.zeroize();
loop {
let mut bytes = ByteArray::<I>::default();
hmac_drbg.fill_bytes(&mut bytes);
let k = I::from_be_byte_array(bytes);
if (!k.is_zero() & k.ct_lt(n)).into() {
return Zeroizing::new(k);
}
}
}
/// Internal implementation of `HMAC_DRBG` as described in NIST SP800-90A.
///
/// <https://csrc.nist.gov/publications/detail/sp/800-90a/rev-1/final>
///
/// This is a HMAC-based deterministic random bit generator used compute a
/// deterministic ephemeral scalar `k`.
pub struct HmacDrbg<D>
where
D: CoreProxy + FixedOutput,
D::Core: BlockSizeUser
+ BufferKindUser<BufferKind = Eager>
+ Clone
+ Default
+ FixedOutputCore
+ HashMarker
+ OutputSizeUser<OutputSize = D::OutputSize>,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
/// HMAC key `K` (see RFC 6979 Section 3.2.c)
k: Hmac<D>,
/// Chaining value `V` (see RFC 6979 Section 3.2.c)
v: GenericArray<u8, D::OutputSize>,
}
impl<D> HmacDrbg<D>
where
D: CoreProxy + FixedOutput,
D::Core: BlockSizeUser
+ BufferKindUser<BufferKind = Eager>
+ Clone
+ Default
+ FixedOutputCore
+ HashMarker
+ OutputSizeUser<OutputSize = D::OutputSize>,
<D::Core as BlockSizeUser>::BlockSize: IsLess<U256>,
Le<<D::Core as BlockSizeUser>::BlockSize, U256>: NonZero,
{
/// Initialize `HMAC_DRBG`
pub fn new(entropy_input: &[u8], nonce: &[u8], additional_data: &[u8]) -> Self {
let mut k = Hmac::new(&Default::default());
let mut v = GenericArray::default();
for b in &mut v {
*b = 0x01;
}
for i in 0..=1 {
k.update(&v);
k.update(&[i]);
k.update(entropy_input);
k.update(nonce);
k.update(additional_data);
k = Hmac::new_from_slice(&k.finalize().into_bytes()).expect("HMAC error");
// Steps 3.2.e,g: v = HMAC_k(v)
k.update(&v);
v = k.finalize_reset().into_bytes();
}
Self { k, v }
}
/// Write the next `HMAC_DRBG` output to the given byte slice.
pub fn fill_bytes(&mut self, out: &mut [u8]) {
for out_chunk in out.chunks_mut(self.v.len()) {
self.k.update(&self.v);
self.v = self.k.finalize_reset().into_bytes();
out_chunk.copy_from_slice(&self.v[..out_chunk.len()]);
}
self.k.update(&self.v);
self.k.update(&[0x00]);
self.k = Hmac::new_from_slice(&self.k.finalize_reset().into_bytes()).expect("HMAC error");
self.k.update(&self.v);
self.v = self.k.finalize_reset().into_bytes();
}
}