-
Notifications
You must be signed in to change notification settings - Fork 219
/
Copy pathkey_manager.rs
237 lines (212 loc) · 9.01 KB
/
key_manager.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2019 The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use std::marker::PhantomData;
use derivative::Derivative;
use digest::{consts::U64, typenum::IsEqual, Digest};
use serde::{Deserialize, Serialize};
use tari_crypto::{
hashing::{DomainSeparatedHasher, LengthExtensionAttackResistant},
keys::{PublicKey, SecretKey},
tari_utilities::byte_array::ByteArrayError,
};
use zeroize::Zeroize;
use crate::{cipher_seed::CipherSeed, KeyManagerDomain, HASHER_LABEL_DERIVE_KEY};
#[derive(Clone, Derivative, Serialize, Deserialize, Zeroize)]
#[derivative(Debug)]
pub struct DerivedKey<PK>
where PK: PublicKey
{
#[derivative(Debug = "ignore")]
#[serde(skip_deserializing)]
pub key: PK::K,
pub key_index: u64,
}
#[derive(Clone, Derivative, Serialize, Deserialize, Zeroize)]
#[derivative(Debug)]
pub struct DerivedPublicKey<PK>
where PK: PublicKey
{
#[derivative(Debug = "ignore")]
#[serde(skip_deserializing)]
pub key: PK,
pub key_index: u64,
}
#[derive(Clone, Derivative, PartialEq, Serialize, Deserialize, Zeroize)]
#[derivative(Debug)]
pub struct KeyManager<PK: PublicKey, D: Digest + LengthExtensionAttackResistant> {
#[derivative(Debug = "ignore")]
seed: CipherSeed,
#[derivative(Debug = "ignore")]
pub branch_seed: String,
primary_key_index: u64,
digest_type: PhantomData<D>,
key_type: PhantomData<PK>,
}
impl<PK, D> KeyManager<PK, D>
where
PK: PublicKey,
D: Digest + LengthExtensionAttackResistant,
D::OutputSize: IsEqual<U64>,
{
/// Creates a new KeyManager with a new randomly selected entropy
pub fn new() -> KeyManager<PK, D> {
KeyManager {
seed: CipherSeed::new(),
branch_seed: "".to_string(),
primary_key_index: 0,
digest_type: PhantomData,
key_type: PhantomData,
}
}
/// Constructs a KeyManager from known parts
pub fn from(seed: CipherSeed, branch_seed: String, primary_key_index: u64) -> KeyManager<PK, D> {
KeyManager {
seed,
branch_seed,
primary_key_index,
digest_type: PhantomData,
key_type: PhantomData,
}
}
/// Derive a new private key from master key: derived_key=H(master_key||branch_seed||index), for some
/// hash function H which is Length attack resistant, such as Blake2b.
fn derive_private_key(&self, key_index: u64) -> Result<PK::K, ByteArrayError> {
// apply domain separation to generate derive key. Under the hood, the hashing api prepends the length of each
// piece of data for concatenation, reducing the risk of collisions due to redundancy of variable length
// input
let derive_key = DomainSeparatedHasher::<D, KeyManagerDomain>::new_with_label(HASHER_LABEL_DERIVE_KEY)
.chain(self.seed.entropy())
.chain(self.branch_seed.as_bytes())
.chain(key_index.to_le_bytes())
.finalize();
let derive_key = derive_key.as_ref();
let s = <PK::K>::from_uniform_bytes(derive_key)?;
Ok(s)
}
/// Derive a new private key from master key: derived_key=H(master_key||branch_seed||index), for some
/// hash function H which is Length attack resistant, such as Blake2b.
pub fn derive_key(&self, key_index: u64) -> Result<DerivedKey<PK>, ByteArrayError> {
let secret = self.derive_private_key(key_index)?;
Ok(DerivedKey { key: secret, key_index })
}
/// Derive a new public key from master key: derived_key=H(master_key||branch_seed||index), for some
/// hash function H which is Length attack resistant, such as Blake2b.
pub fn derive_public_key(&self, key_index: u64) -> Result<DerivedPublicKey<PK>, ByteArrayError> {
let secret = self.derive_private_key(key_index)?;
Ok(DerivedPublicKey {
key: PublicKey::from_secret_key(&secret),
key_index,
})
}
pub fn get_private_key(&self, key_index: u64) -> Result<PK::K, ByteArrayError> {
let secret = self.derive_private_key(key_index)?;
Ok(secret)
}
/// Generate next deterministic private key derived from master key
pub fn next_key(&mut self) -> Result<DerivedKey<PK>, ByteArrayError> {
self.primary_key_index += 1;
self.derive_key(self.primary_key_index)
}
/// Generate next deterministic private key derived from master key
pub fn increment_key_index(&mut self, increment: u64) -> u64 {
self.primary_key_index += increment;
self.primary_key_index
}
pub fn cipher_seed(&self) -> &CipherSeed {
&self.seed
}
pub fn key_index(&self) -> u64 {
self.primary_key_index
}
pub fn update_key_index(&mut self, new_index: u64) {
self.primary_key_index = new_index;
}
}
impl<K, D> Default for KeyManager<K, D>
where
K: PublicKey,
D: Digest + LengthExtensionAttackResistant,
D::OutputSize: IsEqual<U64>,
{
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod test {
use blake2::Blake2b;
use tari_crypto::ristretto::RistrettoPublicKey;
use crate::key_manager::*;
#[test]
fn test_new_keymanager() {
let km1 = KeyManager::<RistrettoPublicKey, Blake2b<U64>>::new();
let km2 = KeyManager::<RistrettoPublicKey, Blake2b<U64>>::new();
assert_ne!(km1.seed, km2.seed);
}
#[test]
fn test_derive_and_next_key() {
let mut km = KeyManager::<RistrettoPublicKey, Blake2b<U64>>::new();
let next_key1_result = km.next_key();
let next_key2_result = km.next_key();
let desired_key_index1 = 1;
let desired_key_index2 = 2;
let derived_key1_result = km.derive_key(desired_key_index1);
let derived_key2_result = km.derive_key(desired_key_index2);
let next_key1 = next_key1_result.unwrap();
let next_key2 = next_key2_result.unwrap();
let derived_key1 = derived_key1_result.unwrap();
let derived_key2 = derived_key2_result.unwrap();
assert_ne!(next_key1.key, next_key2.key);
assert_eq!(next_key1.key, derived_key1.key);
assert_eq!(next_key2.key, derived_key2.key);
assert_eq!(next_key1.key_index, desired_key_index1);
assert_eq!(next_key2.key_index, desired_key_index2);
}
#[test]
fn test_derive_and_next_key_with_branch_seed() {
let mut km = KeyManager::<RistrettoPublicKey, Blake2b<U64>>::from(CipherSeed::new(), "Test".to_string(), 0);
let next_key1_result = km.next_key();
let next_key2_result = km.next_key();
let desired_key_index1 = 1;
let desired_key_index2 = 2;
let derived_key1_result = km.derive_key(desired_key_index1);
let derived_key2_result = km.derive_key(desired_key_index2);
let next_key1 = next_key1_result.unwrap();
let next_key2 = next_key2_result.unwrap();
let derived_key1 = derived_key1_result.unwrap();
let derived_key2 = derived_key2_result.unwrap();
assert_ne!(next_key1.key, next_key2.key);
assert_eq!(next_key1.key, derived_key1.key);
assert_eq!(next_key2.key, derived_key2.key);
assert_eq!(next_key1.key_index, desired_key_index1);
assert_eq!(next_key2.key_index, desired_key_index2);
}
#[test]
fn test_use_of_branch_seed() {
let x = CipherSeed::new();
let mut km1 = KeyManager::<RistrettoPublicKey, Blake2b<U64>>::from(x.clone(), "some".to_string(), 0);
let mut km2 = KeyManager::<RistrettoPublicKey, Blake2b<U64>>::from(x, "other".to_string(), 0);
let next_key1 = km1.next_key().unwrap();
let next_key2 = km2.next_key().unwrap();
assert_ne!(next_key1.key, next_key2.key);
}
}