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
use std::collections::{BTreeMap, HashMap};
use serde::{Deserialize, Serialize};
use super::PUBLIC_MAX_ONE_TIME_KEYS;
use crate::{
types::{Curve25519SecretKey, KeyId},
Curve25519PublicKey,
};
#[derive(Serialize, Deserialize, Clone)]
#[serde(from = "OneTimeKeysPickle")]
#[serde(into = "OneTimeKeysPickle")]
pub(super) struct OneTimeKeys {
pub key_id: u64,
pub unpublished_public_keys: BTreeMap<KeyId, Curve25519PublicKey>,
pub private_keys: BTreeMap<KeyId, Curve25519SecretKey>,
pub key_ids_by_key: HashMap<Curve25519PublicKey, KeyId>,
}
impl OneTimeKeys {
const MAX_ONE_TIME_KEYS: usize = 100 * PUBLIC_MAX_ONE_TIME_KEYS;
pub fn new() -> Self {
Self {
key_id: 0,
unpublished_public_keys: Default::default(),
private_keys: Default::default(),
key_ids_by_key: Default::default(),
}
}
pub fn mark_as_published(&mut self) {
self.unpublished_public_keys.clear();
}
pub fn get_secret_key(&self, public_key: &Curve25519PublicKey) -> Option<&Curve25519SecretKey> {
self.key_ids_by_key.get(public_key).and_then(|key_id| self.private_keys.get(key_id))
}
pub fn remove_secret_key(
&mut self,
public_key: &Curve25519PublicKey,
) -> Option<Curve25519SecretKey> {
self.key_ids_by_key.remove(public_key).and_then(|key_id| {
self.unpublished_public_keys.remove(&key_id);
self.private_keys.remove(&key_id)
})
}
pub(super) fn insert_secret_key(
&mut self,
key_id: KeyId,
key: Curve25519SecretKey,
published: bool,
) {
if self.private_keys.len() >= Self::MAX_ONE_TIME_KEYS {
if let Some(key_id) = self.private_keys.keys().next().copied() {
if let Some(private_key) = self.private_keys.remove(&key_id) {
let public_key = Curve25519PublicKey::from(&private_key);
self.key_ids_by_key.remove(&public_key);
}
self.unpublished_public_keys.remove(&key_id);
}
}
let public_key = Curve25519PublicKey::from(&key);
self.private_keys.insert(key_id, key);
self.key_ids_by_key.insert(public_key, key_id);
if !published {
self.unpublished_public_keys.insert(key_id, public_key);
}
}
pub fn generate(&mut self, count: usize) {
for _ in 0..count {
let key_id = KeyId(self.key_id);
let key = Curve25519SecretKey::new();
self.insert_secret_key(key_id, key, false);
self.key_id = self.key_id.wrapping_add(1);
}
}
}
#[derive(Serialize, Deserialize, Clone)]
pub(super) struct OneTimeKeysPickle {
key_id: u64,
public_keys: BTreeMap<KeyId, Curve25519PublicKey>,
private_keys: BTreeMap<KeyId, Curve25519SecretKey>,
}
impl From<OneTimeKeysPickle> for OneTimeKeys {
fn from(pickle: OneTimeKeysPickle) -> Self {
let mut key_ids_by_key = HashMap::new();
for (k, v) in pickle.private_keys.iter() {
key_ids_by_key.insert(v.into(), *k);
}
Self {
key_id: pickle.key_id,
unpublished_public_keys: pickle.public_keys.iter().map(|(&k, &v)| (k, v)).collect(),
private_keys: pickle.private_keys,
key_ids_by_key,
}
}
}
impl From<OneTimeKeys> for OneTimeKeysPickle {
fn from(keys: OneTimeKeys) -> Self {
OneTimeKeysPickle {
key_id: keys.key_id,
public_keys: keys.unpublished_public_keys.iter().map(|(&k, &v)| (k, v)).collect(),
private_keys: keys.private_keys,
}
}
}
#[cfg(test)]
mod test {
use super::OneTimeKeys;
use crate::types::KeyId;
#[test]
fn store_limit() {
let mut store = OneTimeKeys::new();
assert!(store.private_keys.is_empty());
store.generate(OneTimeKeys::MAX_ONE_TIME_KEYS);
assert_eq!(store.private_keys.len(), OneTimeKeys::MAX_ONE_TIME_KEYS);
assert_eq!(store.unpublished_public_keys.len(), OneTimeKeys::MAX_ONE_TIME_KEYS);
assert_eq!(store.key_ids_by_key.len(), OneTimeKeys::MAX_ONE_TIME_KEYS);
store.mark_as_published();
assert!(store.unpublished_public_keys.is_empty());
assert_eq!(store.private_keys.len(), OneTimeKeys::MAX_ONE_TIME_KEYS);
assert_eq!(store.key_ids_by_key.len(), OneTimeKeys::MAX_ONE_TIME_KEYS);
store.generate(10);
assert_eq!(store.unpublished_public_keys.len(), 10);
assert_eq!(store.private_keys.len(), OneTimeKeys::MAX_ONE_TIME_KEYS);
assert_eq!(store.key_ids_by_key.len(), OneTimeKeys::MAX_ONE_TIME_KEYS);
let oldest_key_id =
store.private_keys.keys().next().copied().expect("Coulnd't get the first key id");
assert_eq!(oldest_key_id, KeyId(10));
}
}