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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
mod fallback_keys;
mod one_time_keys;
use std::collections::HashMap;
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use x25519_dalek::ReusableSecret;
use zeroize::Zeroize;
use self::{
fallback_keys::FallbackKeys,
one_time_keys::{OneTimeKeys, OneTimeKeysPickle},
};
use super::{
messages::PreKeyMessage,
session::{DecryptionError, Session},
session_keys::SessionKeys,
shared_secret::{RemoteShared3DHSecret, Shared3DHSecret},
};
use crate::{
types::{
Curve25519Keypair, Curve25519KeypairPickle, Curve25519PublicKey, Curve25519SecretKey,
Ed25519Keypair, Ed25519KeypairPickle, Ed25519PublicKey, KeyId,
},
utilities::{pickle, unpickle, DecodeSecret},
Ed25519Signature, PickleError,
};
const PUBLIC_MAX_ONE_TIME_KEYS: usize = 50;
#[derive(Error, Debug)]
pub enum SessionCreationError {
#[error("The pre-key message contained an unknown one-time key")]
MissingOneTimeKey,
#[error("The given identity key doesn't match the one in the pre-key message")]
MismatchedIdentityKey,
#[error("The message that was used to establish the Session couldn't be decrypted")]
Decryption(#[from] DecryptionError),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct IdentityKeys {
pub ed25519: Ed25519PublicKey,
pub curve25519: Curve25519PublicKey,
}
#[derive(Debug)]
pub struct InboundCreationResult {
pub session: Session,
pub plaintext: String,
}
pub struct Account {
signing_key: Ed25519Keypair,
diffie_hellman_key: Curve25519Keypair,
one_time_keys: OneTimeKeys,
fallback_keys: FallbackKeys,
}
impl Account {
pub fn new() -> Self {
Self {
signing_key: Ed25519Keypair::new(),
diffie_hellman_key: Curve25519Keypair::new(),
one_time_keys: OneTimeKeys::new(),
fallback_keys: FallbackKeys::new(),
}
}
pub fn identity_keys(&self) -> IdentityKeys {
IdentityKeys { ed25519: self.ed25519_key(), curve25519: self.curve25519_key() }
}
pub fn ed25519_key(&self) -> Ed25519PublicKey {
self.signing_key.public_key()
}
pub fn curve25519_key(&self) -> Curve25519PublicKey {
self.diffie_hellman_key.public_key()
}
pub fn sign(&self, message: &str) -> Ed25519Signature {
self.signing_key.sign(message.as_bytes())
}
pub fn max_number_of_one_time_keys(&self) -> usize {
PUBLIC_MAX_ONE_TIME_KEYS
}
pub fn create_outbound_session(
&self,
identity_key: Curve25519PublicKey,
one_time_key: Curve25519PublicKey,
) -> Session {
let rng = thread_rng();
let base_key = ReusableSecret::new(rng);
let public_base_key = Curve25519PublicKey::from(&base_key);
let shared_secret = Shared3DHSecret::new(
self.diffie_hellman_key.secret_key(),
&base_key,
&identity_key,
&one_time_key,
);
let session_keys = SessionKeys {
identity_key: self.curve25519_key(),
base_key: public_base_key,
one_time_key,
};
Session::new(shared_secret, session_keys)
}
fn find_one_time_key(&self, public_key: &Curve25519PublicKey) -> Option<&Curve25519SecretKey> {
self.one_time_keys
.get_secret_key(public_key)
.or_else(|| self.fallback_keys.get_secret_key(public_key))
}
pub fn remove_one_time_key(
&mut self,
public_key: &Curve25519PublicKey,
) -> Option<Curve25519SecretKey> {
self.one_time_keys.remove_secret_key(public_key)
}
pub fn create_inbound_session(
&mut self,
their_identity_key: Curve25519PublicKey,
pre_key_message: &PreKeyMessage,
) -> Result<InboundCreationResult, SessionCreationError> {
if their_identity_key != pre_key_message.identity_key() {
Err(SessionCreationError::MismatchedIdentityKey)
} else {
let one_time_key = self
.find_one_time_key(&pre_key_message.one_time_key())
.ok_or(SessionCreationError::MissingOneTimeKey)?;
let shared_secret = RemoteShared3DHSecret::new(
self.diffie_hellman_key.secret_key(),
one_time_key,
&pre_key_message.identity_key(),
&pre_key_message.base_key(),
);
let session_keys = SessionKeys {
identity_key: pre_key_message.identity_key(),
base_key: pre_key_message.base_key(),
one_time_key: pre_key_message.one_time_key(),
};
let mut session = Session::new_remote(
shared_secret,
pre_key_message.message.ratchet_key,
session_keys,
);
let plaintext = session.decrypt_decoded(&pre_key_message.message)?;
let plaintext = String::from_utf8_lossy(&plaintext).to_string();
self.remove_one_time_key(&pre_key_message.one_time_key());
Ok(InboundCreationResult { session, plaintext })
}
}
pub fn generate_one_time_keys(&mut self, count: usize) {
self.one_time_keys.generate(count);
}
pub fn one_time_keys(&self) -> HashMap<KeyId, Curve25519PublicKey> {
self.one_time_keys
.unpublished_public_keys
.iter()
.map(|(key_id, key)| (*key_id, *key))
.collect()
}
pub fn generate_fallback_key(&mut self) {
self.fallback_keys.generate_fallback_key()
}
pub fn fallback_key(&self) -> HashMap<KeyId, Curve25519PublicKey> {
let fallback_key = self.fallback_keys.unpublished_fallback_key();
if let Some(fallback_key) = fallback_key {
HashMap::from([(fallback_key.key_id(), fallback_key.public_key())])
} else {
HashMap::new()
}
}
pub fn forget_fallback_key(&mut self) -> bool {
self.fallback_keys.forget_previous_fallback_key().is_some()
}
pub fn mark_keys_as_published(&mut self) {
self.one_time_keys.mark_as_published();
self.fallback_keys.mark_as_published();
}
pub fn pickle(&self) -> AccountPickle {
AccountPickle {
signing_key: self.signing_key.clone().into(),
diffie_hellman_key: self.diffie_hellman_key.clone().into(),
one_time_keys: self.one_time_keys.clone().into(),
fallback_keys: self.fallback_keys.clone(),
}
}
pub fn from_pickle(pickle: AccountPickle) -> Self {
pickle.into()
}
#[cfg(feature = "libolm-compat")]
pub fn from_libolm_pickle(
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, crate::LibolmPickleError> {
use self::fallback_keys::FallbackKey;
use crate::utilities::{unpickle_libolm, Decode};
#[derive(Debug, Zeroize)]
#[zeroize(drop)]
struct OneTimeKey {
key_id: u32,
published: bool,
public_key: [u8; 32],
private_key: Box<[u8; 32]>,
}
impl Decode for OneTimeKey {
fn decode(
reader: &mut impl std::io::Read,
) -> Result<Self, crate::utilities::LibolmDecodeError> {
let key_id = u32::decode(reader)?;
let published = bool::decode(reader)?;
let public_key = <[u8; 32]>::decode(reader)?;
let private_key = <[u8; 32]>::decode_secret(reader)?;
Ok(Self { key_id, published, public_key, private_key })
}
}
impl From<&OneTimeKey> for FallbackKey {
fn from(key: &OneTimeKey) -> Self {
FallbackKey {
key_id: KeyId(key.key_id.into()),
key: Curve25519SecretKey::from_slice(&key.private_key),
published: key.published,
}
}
}
#[derive(Debug, Zeroize)]
#[zeroize(drop)]
struct FallbackKeysArray {
fallback_key: Option<OneTimeKey>,
previous_fallback_key: Option<OneTimeKey>,
}
impl Decode for FallbackKeysArray {
fn decode(
reader: &mut impl std::io::Read,
) -> Result<Self, crate::utilities::LibolmDecodeError> {
let count = u8::decode(reader)?;
let (fallback_key, previous_fallback_key) = if count >= 1 {
let fallback_key = OneTimeKey::decode(reader)?;
let previous_fallback_key =
if count >= 2 { Some(OneTimeKey::decode(reader)?) } else { None };
(Some(fallback_key), previous_fallback_key)
} else {
(None, None)
};
Ok(Self { fallback_key, previous_fallback_key })
}
}
#[derive(Debug, Zeroize)]
#[zeroize(drop)]
struct Pickle {
version: u32,
public_ed25519_key: [u8; 32],
private_ed25519_key: Box<[u8; 64]>,
public_curve25519_key: [u8; 32],
private_curve25519_key: Box<[u8; 32]>,
one_time_keys: Vec<OneTimeKey>,
fallback_keys: FallbackKeysArray,
}
impl Decode for Pickle {
fn decode(
reader: &mut impl std::io::Read,
) -> Result<Self, crate::utilities::LibolmDecodeError> {
let version = u32::decode(reader)?;
let public_ed25519_key = <[u8; 32]>::decode(reader)?;
let private_ed25519_key = <[u8; 64]>::decode_secret(reader)?;
let public_curve25519_key = <[u8; 32]>::decode(reader)?;
let private_curve25519_key = <[u8; 32]>::decode_secret(reader)?;
let one_time_keys = Vec::decode(reader)?;
let fallback_keys = FallbackKeysArray::decode(reader)?;
Ok(Self {
version,
public_ed25519_key,
private_ed25519_key,
public_curve25519_key,
private_curve25519_key,
one_time_keys,
fallback_keys,
})
}
}
impl TryFrom<Pickle> for Account {
type Error = crate::LibolmPickleError;
fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
let mut one_time_keys = OneTimeKeys::new();
let mut max_key_id = 0;
for key in &pickle.one_time_keys {
let secret_key = Curve25519SecretKey::from_slice(&key.private_key);
let key_id = KeyId(key.key_id.into());
one_time_keys.insert_secret_key(key_id, secret_key, key.published);
if key_id.0 > max_key_id {
max_key_id = key_id.0;
}
}
one_time_keys.key_id =
if pickle.one_time_keys.is_empty() { 0 } else { max_key_id + 1 };
let fallback_keys = FallbackKeys {
key_id: pickle
.fallback_keys
.fallback_key
.as_ref()
.map(|k| k.key_id + 1)
.unwrap_or(0) as u64,
fallback_key: pickle.fallback_keys.fallback_key.as_ref().map(|k| k.into()),
previous_fallback_key: pickle
.fallback_keys
.previous_fallback_key
.as_ref()
.map(|k| k.into()),
};
Ok(Self {
signing_key: Ed25519Keypair::from_expanded_key(&pickle.private_ed25519_key)?,
diffie_hellman_key: Curve25519Keypair::from_secret_key(
&pickle.private_curve25519_key,
),
one_time_keys,
fallback_keys,
})
}
}
const PICKLE_VERSION: u32 = 4;
unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
}
}
impl Default for Account {
fn default() -> Self {
Self::new()
}
}
#[derive(Serialize, Deserialize)]
pub struct AccountPickle {
signing_key: Ed25519KeypairPickle,
diffie_hellman_key: Curve25519KeypairPickle,
one_time_keys: OneTimeKeysPickle,
fallback_keys: FallbackKeys,
}
impl AccountPickle {
pub fn encrypt(self, pickle_key: &[u8; 32]) -> String {
pickle(&self, pickle_key)
}
pub fn from_encrypted(ciphertext: &str, pickle_key: &[u8; 32]) -> Result<Self, PickleError> {
unpickle(ciphertext, pickle_key)
}
}
impl From<AccountPickle> for Account {
fn from(pickle: AccountPickle) -> Self {
Self {
signing_key: pickle.signing_key.into(),
diffie_hellman_key: pickle.diffie_hellman_key.into(),
one_time_keys: pickle.one_time_keys.into(),
fallback_keys: pickle.fallback_keys,
}
}
}
#[cfg(test)]
mod test {
use anyhow::{bail, Context, Result};
use olm_rs::{account::OlmAccount, session::OlmMessage as LibolmOlmMessage};
use super::{Account, InboundCreationResult, SessionCreationError};
use crate::{
cipher::Mac,
olm::{
messages::{OlmMessage, PreKeyMessage},
AccountPickle,
},
Curve25519PublicKey as PublicKey,
};
const PICKLE_KEY: [u8; 32] = [0u8; 32];
#[test]
fn vodozemac_libolm_communication() -> Result<()> {
let alice = Account::new();
let bob = OlmAccount::new();
bob.generate_one_time_keys(1);
let one_time_key = bob
.parsed_one_time_keys()
.curve25519()
.values()
.next()
.cloned()
.expect("Didn't find a valid one-time key");
bob.mark_keys_as_published();
let identity_keys = bob.parsed_identity_keys();
let curve25519_key = PublicKey::from_base64(identity_keys.curve25519())?;
let one_time_key = PublicKey::from_base64(&one_time_key)?;
let mut alice_session = alice.create_outbound_session(curve25519_key, one_time_key);
let message = "It's a secret to everybody";
let olm_message: LibolmOlmMessage = alice_session.encrypt(message).into();
if let LibolmOlmMessage::PreKey(m) = olm_message.clone() {
let libolm_session =
bob.create_inbound_session_from(&alice.curve25519_key().to_base64(), m)?;
assert_eq!(alice_session.session_id(), libolm_session.session_id());
let plaintext = libolm_session.decrypt(olm_message)?;
assert_eq!(message, plaintext);
let second_text = "Here's another secret to everybody";
let olm_message = alice_session.encrypt(second_text).into();
let plaintext = libolm_session.decrypt(olm_message)?;
assert_eq!(second_text, plaintext);
let reply_plain = "Yes, take this, it's dangerous out there";
let reply = libolm_session.encrypt(reply_plain).into();
let plaintext = alice_session.decrypt(&reply)?;
assert_eq!(&plaintext, reply_plain);
let another_reply = "Last one";
let reply = libolm_session.encrypt(another_reply).into();
let plaintext = alice_session.decrypt(&reply)?;
assert_eq!(&plaintext, another_reply);
let last_text = "Nope, I'll have the last word";
let olm_message = alice_session.encrypt(last_text).into();
let plaintext = libolm_session.decrypt(olm_message)?;
assert_eq!(last_text, plaintext);
} else {
bail!("Received a invalid message type {:?}", olm_message);
}
Ok(())
}
#[test]
fn vodozemac_vodozemac_communication() -> Result<()> {
let alice = Account::new();
let mut bob = Account::new();
bob.generate_one_time_keys(1);
let mut alice_session = alice.create_outbound_session(
bob.curve25519_key(),
*bob.one_time_keys()
.iter()
.next()
.context("Failed getting bob's OTK, which should never happen here.")?
.1,
);
bob.mark_keys_as_published();
let message = "It's a secret to everybody";
let olm_message = alice_session.encrypt(message);
if let OlmMessage::PreKey(m) = olm_message {
assert_eq!(m.session_keys(), alice_session.session_keys());
let InboundCreationResult { session: mut bob_session, plaintext } =
bob.create_inbound_session(alice.curve25519_key(), &m)?;
assert_eq!(alice_session.session_id(), bob_session.session_id());
assert_eq!(m.session_keys(), bob_session.session_keys());
assert_eq!(message, plaintext);
let second_text = "Here's another secret to everybody";
let olm_message = alice_session.encrypt(second_text);
let plaintext = bob_session.decrypt(&olm_message)?;
assert_eq!(second_text, plaintext);
let reply_plain = "Yes, take this, it's dangerous out there";
let reply = bob_session.encrypt(reply_plain);
let plaintext = alice_session.decrypt(&reply)?;
assert_eq!(&plaintext, reply_plain);
let another_reply = "Last one";
let reply = bob_session.encrypt(another_reply);
let plaintext = alice_session.decrypt(&reply)?;
assert_eq!(&plaintext, another_reply);
let last_text = "Nope, I'll have the last word";
let olm_message = alice_session.encrypt(last_text);
let plaintext = bob_session.decrypt(&olm_message)?;
assert_eq!(last_text, plaintext);
}
Ok(())
}
#[test]
fn inbound_session_creation() -> Result<()> {
let alice = OlmAccount::new();
let mut bob = Account::new();
bob.generate_one_time_keys(1);
let one_time_key =
bob.one_time_keys().values().next().cloned().expect("Didn't find a valid one-time key");
let alice_session = alice.create_outbound_session(
&bob.curve25519_key().to_base64(),
&one_time_key.to_base64(),
)?;
let text = "It's a secret to everybody";
let message = alice_session.encrypt(text).into();
let identity_key = PublicKey::from_base64(alice.parsed_identity_keys().curve25519())?;
let InboundCreationResult { session, plaintext } = if let OlmMessage::PreKey(m) = &message {
bob.create_inbound_session(identity_key, m)?
} else {
bail!("Got invalid message type from olm_rs {:?}", message);
};
assert_eq!(alice_session.session_id(), session.session_id());
assert!(bob.one_time_keys.private_keys.is_empty());
assert_eq!(text, plaintext);
Ok(())
}
#[test]
fn inbound_session_creation_using_fallback_keys() -> Result<()> {
let alice = OlmAccount::new();
let mut bob = Account::new();
bob.generate_fallback_key();
let one_time_key =
bob.fallback_key().values().next().cloned().expect("Didn't find a valid fallback key");
assert!(bob.one_time_keys.private_keys.is_empty());
let alice_session = alice.create_outbound_session(
&bob.curve25519_key().to_base64(),
&one_time_key.to_base64(),
)?;
let text = "It's a secret to everybody";
let message = alice_session.encrypt(text).into();
let identity_key = PublicKey::from_base64(alice.parsed_identity_keys().curve25519())?;
if let OlmMessage::PreKey(m) = &message {
let InboundCreationResult { session, plaintext } =
bob.create_inbound_session(identity_key, m)?;
assert_eq!(m.session_keys(), session.session_keys());
assert_eq!(alice_session.session_id(), session.session_id());
assert!(bob.fallback_keys.fallback_key.is_some());
assert_eq!(text, plaintext);
} else {
bail!("Got invalid message type from olm_rs");
};
Ok(())
}
#[test]
fn account_pickling_roundtrip_is_identity() -> Result<()> {
let mut account = Account::new();
account.generate_one_time_keys(50);
account.generate_fallback_key();
account.generate_fallback_key();
let pickle = account.pickle().encrypt(&PICKLE_KEY);
let decrypted_pickle = AccountPickle::from_encrypted(&pickle, &PICKLE_KEY)?;
let unpickled_account = Account::from_pickle(decrypted_pickle);
let repickle = unpickled_account.pickle();
assert_eq!(account.identity_keys(), unpickled_account.identity_keys());
let decrypted_pickle = AccountPickle::from_encrypted(&pickle, &PICKLE_KEY)?;
let pickle = serde_json::to_value(decrypted_pickle)?;
let repickle = serde_json::to_value(repickle)?;
assert_eq!(pickle, repickle);
Ok(())
}
#[test]
#[cfg(feature = "libolm-compat")]
fn libolm_unpickling() -> Result<()> {
let olm = OlmAccount::new();
olm.generate_one_time_keys(10);
olm.generate_fallback_key();
let key = b"DEFAULT_PICKLE_KEY";
let pickle = olm.pickle(olm_rs::PicklingMode::Encrypted { key: key.to_vec() });
let unpickled = Account::from_libolm_pickle(&pickle, key)?;
assert_eq!(olm.parsed_identity_keys().ed25519(), unpickled.ed25519_key().to_base64());
assert_eq!(olm.parsed_identity_keys().curve25519(), unpickled.curve25519_key().to_base64());
let mut olm_one_time_keys: Vec<_> =
olm.parsed_one_time_keys().curve25519().values().map(|k| k.to_owned()).collect();
let mut one_time_keys: Vec<_> =
unpickled.one_time_keys().values().map(|k| k.to_base64()).collect();
olm_one_time_keys.sort();
one_time_keys.sort();
assert_eq!(olm_one_time_keys, one_time_keys);
let olm_fallback_key =
olm.parsed_fallback_key().expect("libolm should have a fallback key");
assert_eq!(
olm_fallback_key.curve25519(),
unpickled
.fallback_key()
.values()
.next()
.expect("We should have a fallback key")
.to_base64()
);
Ok(())
}
#[test]
#[cfg(feature = "libolm-compat")]
fn signing_with_expanded_key() -> Result<()> {
let olm = OlmAccount::new();
olm.generate_one_time_keys(10);
olm.generate_fallback_key();
let key = b"DEFAULT_PICKLE_KEY";
let pickle = olm.pickle(olm_rs::PicklingMode::Encrypted { key: key.to_vec() });
let account_with_expanded_key = Account::from_libolm_pickle(&pickle, key)?;
let signing_key_clone = account_with_expanded_key.signing_key.clone();
signing_key_clone.sign("You met with a terrible fate, haven’t you?".as_bytes());
account_with_expanded_key.sign("You met with a terrible fate, haven’t you?");
Ok(())
}
#[test]
fn invalid_session_creation_does_not_remove_otk() -> Result<()> {
let mut alice = Account::new();
let malory = Account::new();
alice.generate_one_time_keys(1);
let mut session = malory.create_outbound_session(
alice.curve25519_key(),
*alice.one_time_keys().values().next().expect("Should have one-time key"),
);
let message = session.encrypt("Test");
if let OlmMessage::PreKey(m) = message {
let mut message = m.to_bytes();
let message_len = message.len();
message[message_len - Mac::TRUNCATED_LEN..message_len]
.copy_from_slice(&[0u8; Mac::TRUNCATED_LEN]);
let message = PreKeyMessage::try_from(message)?;
match alice.create_inbound_session(malory.curve25519_key(), &message) {
Err(SessionCreationError::Decryption(_)) => {}
e => bail!("Expected a decryption error, got {:?}", e),
}
assert!(
!alice.one_time_keys.private_keys.is_empty()
&& !alice.one_time_keys.private_keys.is_empty(),
"The one-time key was removed when it shouldn't"
);
Ok(())
} else {
bail!("Invalid message type");
}
}
}