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
use arrayvec::ArrayVec;
use serde::{Deserialize, Serialize};
use super::{
chain_key::RemoteChainKey, message_key::RemoteMessageKey, ratchet::RemoteRatchetKey,
DecryptionError,
};
use crate::olm::messages::Message;
const MAX_MESSAGE_GAP: u64 = 2000;
const MAX_MESSAGE_KEYS: usize = 40;
#[derive(Serialize, Deserialize, Clone)]
struct MessageKeyStore {
inner: ArrayVec<RemoteMessageKey, MAX_MESSAGE_KEYS>,
}
impl MessageKeyStore {
fn new() -> Self {
Self { inner: ArrayVec::new() }
}
fn push(&mut self, message_key: RemoteMessageKey) {
if self.inner.is_full() {
self.inner.pop_at(0);
}
self.inner.push(message_key)
}
fn merge(&mut self, mut store: MessageKeyStore) {
for key in store.inner.drain(..) {
self.push(key);
}
}
fn get_message_key(&self, chain_index: u64) -> Option<&RemoteMessageKey> {
self.inner.iter().find(|k| k.chain_index() == chain_index)
}
fn remove_message_key(&mut self, chain_index: u64) {
self.inner.retain(|k| k.chain_index() != chain_index);
}
}
impl Default for MessageKeyStore {
fn default() -> Self {
Self::new()
}
}
#[derive(Serialize, Deserialize, Clone)]
pub(super) struct ReceiverChain {
ratchet_key: RemoteRatchetKey,
hkdf_ratchet: RemoteChainKey,
skipped_message_keys: MessageKeyStore,
}
impl ReceiverChain {
pub fn new(ratchet_key: RemoteRatchetKey, chain_key: RemoteChainKey) -> Self {
ReceiverChain {
ratchet_key,
hkdf_ratchet: chain_key,
skipped_message_keys: Default::default(),
}
}
pub fn decrypt(&mut self, message: &Message) -> Result<Vec<u8>, DecryptionError> {
let message_gap = message.chain_index.saturating_sub(self.hkdf_ratchet.chain_index());
if message_gap > MAX_MESSAGE_GAP {
Err(DecryptionError::TooBigMessageGap(message_gap, MAX_MESSAGE_GAP))
} else if self.hkdf_ratchet.chain_index() > message.chain_index {
if let Some(message_key) =
self.skipped_message_keys.get_message_key(message.chain_index)
{
let plaintext = message_key.decrypt(message)?;
self.skipped_message_keys.remove_message_key(message.chain_index);
Ok(plaintext)
} else {
Err(DecryptionError::MissingMessageKey(message.chain_index))
}
} else {
let mut ratchet = self.hkdf_ratchet.clone();
let mut skipped_keys = MessageKeyStore::new();
while ratchet.chain_index() < message.chain_index {
if message.chain_index - ratchet.chain_index() > MAX_MESSAGE_KEYS as u64 {
ratchet.advance();
} else {
let key = ratchet.create_message_key();
skipped_keys.push(key);
}
}
let message_key = ratchet.create_message_key();
let plaintext = message_key.decrypt(message)?;
self.hkdf_ratchet = ratchet;
self.skipped_message_keys.merge(skipped_keys);
Ok(plaintext)
}
}
#[cfg(feature = "libolm-compat")]
pub fn ratchet_key(&self) -> RemoteRatchetKey {
self.ratchet_key
}
#[cfg(feature = "libolm-compat")]
pub fn insert_message_key(&mut self, message_key: RemoteMessageKey) {
self.skipped_message_keys.push(message_key)
}
pub fn belongs_to(&self, ratchet_key: &RemoteRatchetKey) -> bool {
&self.ratchet_key == ratchet_key
}
}