p2panda-encryption 0.5.2

Decentralised data- and message encryption for groups with post-compromise security and optional forward secrecy
Documentation
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Manager for our own secret key material.
//!
//! Peers should automatically rotate their key bundles if the lifetime is close to expiring. It is
//! recommended to do this in good time before the actual expiration date to allow others to
//! receive it even when the network is unstable or peers are longer offline.
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::crypto::x25519::{PublicKey, SecretKey, X25519Error};
use crate::crypto::xeddsa::{XEdDSAError, XSignature};
use crate::crypto::{Rng, RngError};
use crate::key_bundle::{
    Lifetime, LongTermKeyBundle, OneTimeKeyBundle, OneTimePreKey, OneTimePreKeyId, PreKey,
    PreKeyId, latest_prekey,
};
use crate::traits::{IdentityManager, PreKeyManager};

/// Key manager to maintain secret key material (like our identity key) and to generate signed
/// public pre-key bundles.
#[derive(Clone, Debug)]
pub struct KeyManager;

/// Serializable state of key manager.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct KeyManagerState {
    identity_secret: SecretKey,
    identity_key: PublicKey,
    prekeys: PreKeyBundlesState,
    // @TODO(adz): Could make sense to factor out one-time secrets into a similar structure like
    // pre-keys as well, so they can be independently handled in a storage layer.
    onetime_secrets: HashMap<OneTimePreKeyId, (PreKeyId, SecretKey)>,
    onetime_next_id: OneTimePreKeyId,
}

impl KeyManagerState {
    pub fn prekey_bundles(&self) -> &PreKeyBundlesState {
        &self.prekeys
    }
}

/// Collection of all known, publishable pre-keys with their regarding secrets and signatures in
/// this key manager. Offers a form of "garbage collection" to automatically remove expired
/// pre-keys.
///
/// This can be serialized and independently stored from the identity secrets.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct PreKeyBundlesState(HashMap<PreKeyId, PreKeyBundle>);

impl PreKeyBundlesState {
    /// Initializes and returns new instance of pre-key bundles state.
    fn new() -> Self {
        Self::default()
    }

    /// Returns latest pre-key if valid and available and `None` otherwise.
    fn latest(&self) -> Option<PreKeyBundle> {
        let prekeys = self.0.values().map(|state| &state.prekey).collect();
        let latest = latest_prekey(prekeys);
        latest.map(|prekey| {
            self.0
                .get(prekey.key())
                .expect("we know the item exists in the set")
                .clone()
        })
    }

    fn contains(&self, id: &PreKeyId) -> bool {
        self.0.contains_key(id)
    }

    #[allow(unused)]
    fn len(&self) -> usize {
        self.0.len()
    }

    fn get(&self, id: &PreKeyId) -> Option<&PreKeyBundle> {
        self.0.get(id)
    }

    /// Insert pre-key bundle into set.
    fn insert(mut self, bundle: PreKeyBundle) -> Self {
        self.0.insert(bundle.id(), bundle);
        self
    }

    /// Remove all expired key bundles from manager.
    #[allow(clippy::manual_retain)]
    fn remove_expired(self) -> Self {
        Self(
            self.0
                .into_iter()
                .filter(|(_, prekey)| prekey.prekey.verify_lifetime().is_ok())
                .collect(),
        )
    }
}

/// Extended pre-key struct holding the public and secret parts and signature, authenticating the
/// pre-key with an identity.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PreKeyBundle {
    prekey: PreKey,
    signature: XSignature,
    secret: SecretKey,
}

impl PreKeyBundle {
    /// Generates, signs new pre-key and returns struct holding signature and pre-key secret
    /// next to the public part.
    pub fn new(
        identity_secret: &SecretKey,
        lifetime: Lifetime,
        rng: &Rng,
    ) -> Result<Self, KeyManagerError> {
        let secret = SecretKey::from_bytes(rng.random_array()?);
        let prekey = PreKey::new(secret.public_key()?, lifetime);
        let signature = prekey.sign(identity_secret, rng)?;

        Ok(Self {
            prekey,
            signature,
            secret,
        })
    }

    pub fn id(&self) -> PreKeyId {
        *self.prekey.key()
    }

    pub fn lifetime(&self) -> &Lifetime {
        self.prekey.lifetime()
    }
}

impl KeyManager {
    /// Returns newly initialised key-manager state, holding our identity secret.
    pub fn init(identity_secret: &SecretKey) -> Result<KeyManagerState, KeyManagerError> {
        Ok(KeyManagerState {
            identity_key: identity_secret.public_key()?,
            identity_secret: identity_secret.clone(),
            prekeys: PreKeyBundlesState::new(),
            onetime_secrets: HashMap::new(),
            onetime_next_id: 0,
        })
    }

    /// Returns newly initialised key-manager state, holding our identity secret with existing
    /// pre-key bundles.
    pub fn init_from_prekey_bundles(
        identity_secret: &SecretKey,
        prekeys: PreKeyBundlesState,
    ) -> Result<KeyManagerState, KeyManagerError> {
        Ok(KeyManagerState {
            identity_key: identity_secret.public_key()?,
            identity_secret: identity_secret.clone(),
            prekeys,
            onetime_secrets: HashMap::new(),
            onetime_next_id: 0,
        })
    }

    /// Returns newly initialised key-manager state, holding our identity secret and an
    /// automatically generated, first pre-key secret which can be used to generate key-bundles.
    #[cfg(any(test, feature = "test_utils"))]
    pub fn init_and_generate_prekey(
        identity_secret: &SecretKey,
        lifetime: Lifetime,
        rng: &Rng,
    ) -> Result<KeyManagerState, KeyManagerError> {
        let bundle = PreKeyBundle::new(identity_secret, lifetime, rng)?;
        let prekeys = PreKeyBundlesState::new().insert(bundle);

        Ok(KeyManagerState {
            identity_key: identity_secret.public_key()?,
            identity_secret: identity_secret.clone(),
            prekeys,
            onetime_secrets: HashMap::new(),
            onetime_next_id: 0,
        })
    }

    /// Remove all expired pre-key bundles from manager.
    #[allow(clippy::manual_retain)]
    pub fn remove_expired(mut y: KeyManagerState) -> KeyManagerState {
        // Remove all expired pre keys.
        y.prekeys = y.prekeys.remove_expired();

        // Remove one-time bundles which do not have a valid pre-key anymore.
        y.onetime_secrets = y
            .onetime_secrets
            .into_iter()
            .filter(|(_, (prekey_id, _))| y.prekeys.contains(prekey_id))
            .collect();

        y
    }
}

impl IdentityManager<KeyManagerState> for KeyManager {
    /// Returns identity key secret.
    fn identity_secret(y: &KeyManagerState) -> &SecretKey {
        &y.identity_secret
    }
}

impl PreKeyManager for KeyManager {
    type State = KeyManagerState;

    type Error = KeyManagerError;

    /// Returns long-term pre-key secret by id.
    ///
    /// Throws an error if pre-key was not found (for example because it expired and was removed).
    fn prekey_secret<'a>(
        y: &'a Self::State,
        id: &'a PreKeyId,
    ) -> Result<&'a SecretKey, Self::Error> {
        match y.prekeys.get(id) {
            Some(prekey) => Ok(&prekey.secret),
            None => Err(KeyManagerError::UnknownPreKeySecret(*id)),
        }
    }

    /// Generates a new long-term pre-key secret with the given lifetime.
    fn rotate_prekey(
        mut y: Self::State,
        lifetime: Lifetime,
        rng: &Rng,
    ) -> Result<Self::State, Self::Error> {
        let prekey = PreKeyBundle::new(&y.identity_secret, lifetime, rng)?;
        y.prekeys = y.prekeys.insert(prekey);
        Ok(y)
    }

    /// Returns latest, public long-term key-bundle which can be published on the network.
    ///
    /// Note that key bundles can be expired and thus invalid, this method will return an error in
    /// this case and applications need to generate new ones when necessary.
    fn prekey_bundle(y: &Self::State) -> Result<LongTermKeyBundle, Self::Error> {
        y.prekeys
            .latest()
            .map(|latest| LongTermKeyBundle::new(y.identity_key, latest.prekey, latest.signature))
            .ok_or(KeyManagerError::NoPreKeysAvailable)
    }

    /// Creates a new public one-time key-bundle.
    fn generate_onetime_bundle(
        mut y: Self::State,
        rng: &Rng,
    ) -> Result<(Self::State, OneTimeKeyBundle), Self::Error> {
        let latest = y
            .prekeys
            .latest()
            .ok_or(KeyManagerError::NoPreKeysAvailable)?;

        let onetime_secret = SecretKey::from_bytes(rng.random_array()?);
        let onetime_key = OneTimePreKey::new(onetime_secret.public_key()?, y.onetime_next_id);

        {
            let existing_key = y
                .onetime_secrets
                .insert(onetime_key.id(), (latest.id(), onetime_secret));
            // Sanity check.
            assert!(
                existing_key.is_none(),
                "should never insert same id more than once"
            );
        };

        let bundle = OneTimeKeyBundle::new(
            y.identity_key,
            latest.prekey,
            latest.signature,
            Some(onetime_key),
        );

        y.onetime_next_id += 1;

        Ok((y, bundle))
    }

    /// Returns one-time pre-key secret used by a sender during X3DH.
    ///
    /// Throws an error when requested pre-key secret is unknown (and thus probably was already
    /// used once).
    ///
    /// Returns none when this key-manager doesn't have any one-time pre-keys. New ones can be
    /// created by calling `generate_onetime_bundle`.
    fn use_onetime_secret(
        mut y: Self::State,
        id: OneTimePreKeyId,
    ) -> Result<(Self::State, Option<SecretKey>), Self::Error> {
        match y.onetime_secrets.remove(&id) {
            Some(secret) => Ok((y, Some(secret.1))),
            None => Err(KeyManagerError::UnknownOneTimeSecret(id)),
        }
    }
}

#[derive(Debug, Error)]
pub enum KeyManagerError {
    #[error(transparent)]
    Rng(#[from] RngError),

    #[error(transparent)]
    XEdDSA(#[from] XEdDSAError),

    #[error(transparent)]
    X25519(#[from] X25519Error),

    #[error("could not find one-time pre-key secret with id {0}")]
    UnknownOneTimeSecret(OneTimePreKeyId),

    #[error("could not find pre-key secret with id {0}")]
    UnknownPreKeySecret(PreKeyId),

    #[error("no valid pre-keys available, they are either expired or too early")]
    NoPreKeysAvailable,
}

#[cfg(test)]
mod tests {
    use std::time::{SystemTime, UNIX_EPOCH};

    use crate::crypto::Rng;
    use crate::crypto::x25519::SecretKey;
    use crate::key_bundle::Lifetime;
    use crate::key_manager::KeyManagerError;
    use crate::traits::KeyBundle;

    use super::{KeyManager, PreKeyManager};

    #[test]
    fn generate_onetime_keys() {
        let rng = Rng::from_seed([1; 32]);

        let identity_secret = SecretKey::from_bytes(rng.random_array().unwrap());
        let state =
            KeyManager::init_and_generate_prekey(&identity_secret, Lifetime::default(), &rng)
                .unwrap();

        let (state, bundle_1) = KeyManager::generate_onetime_bundle(state, &rng).unwrap();
        let (state, bundle_2) = KeyManager::generate_onetime_bundle(state, &rng).unwrap();

        // Prekey stays the same between each bundle and match the secret key.
        assert_eq!(
            bundle_1.signed_prekey(),
            &KeyManager::prekey_secret(&state, bundle_1.signed_prekey())
                .expect("non-expired prekey exists")
                .public_key()
                .unwrap()
        );
        assert_eq!(bundle_1.signed_prekey(), bundle_2.signed_prekey());

        // Identity key matches the identity secret.
        assert_eq!(
            bundle_1.identity_key(),
            &identity_secret.public_key().unwrap()
        );
        assert_eq!(
            bundle_2.identity_key(),
            &identity_secret.public_key().unwrap()
        );

        // Signature is correct.
        assert!(bundle_1.verify().is_ok());
        assert!(bundle_2.verify().is_ok());

        let (state, onetime_secret_1) =
            KeyManager::use_onetime_secret(state, bundle_1.onetime_prekey_id().unwrap()).unwrap();
        let (state, onetime_secret_2) =
            KeyManager::use_onetime_secret(state, bundle_2.onetime_prekey_id().unwrap()).unwrap();

        // Secrets got removed from state.
        assert_eq!(state.onetime_secrets.len(), 0);

        // Retrieving unknown one-time prekeys throws an error.
        assert!(KeyManager::use_onetime_secret(state.clone(), 42).is_err());

        // Re-retrieving known one-time prekeys throws an error.
        assert!(
            KeyManager::use_onetime_secret(state.clone(), bundle_1.onetime_prekey_id().unwrap())
                .is_err()
        );
        assert!(
            KeyManager::use_onetime_secret(state.clone(), bundle_2.onetime_prekey_id().unwrap())
                .is_err()
        );

        // One-time prekeys match the secret.
        assert_eq!(
            bundle_1.onetime_prekey().unwrap(),
            &onetime_secret_1.unwrap().public_key().unwrap()
        );
        assert_eq!(
            bundle_2.onetime_prekey().unwrap(),
            &onetime_secret_2.unwrap().public_key().unwrap()
        );

        // One-time prekeys are unique.
        assert_ne!(bundle_1.onetime_prekey(), bundle_2.onetime_prekey());
        assert_ne!(bundle_1.onetime_prekey_id(), bundle_2.onetime_prekey_id());
    }

    #[test]
    fn expired_prekey_bundles() {
        let rng = Rng::from_seed([1; 32]);
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("SystemTime before UNIX EPOCH!")
            .as_secs();

        let identity_secret = SecretKey::from_bytes(rng.random_array().unwrap());

        let y = KeyManager::init_and_generate_prekey(
            &identity_secret,
            Lifetime::from_range(now - 120, now - 60), // expired lifetime
            &rng,
        )
        .unwrap();

        // Current pre-key bundle is invalid.
        assert!(matches!(
            KeyManager::prekey_bundle(&y),
            Err(KeyManagerError::NoPreKeysAvailable)
        ));

        // Can't generate one-time key bundle with expired pre keys.
        assert!(matches!(
            KeyManager::generate_onetime_bundle(y.clone(), &rng),
            Err(KeyManagerError::NoPreKeysAvailable)
        ));

        // Generate a new one.
        let y_i = KeyManager::rotate_prekey(y, Lifetime::default(), &rng).unwrap();
        assert!(KeyManager::prekey_bundle(&y_i).is_ok());
    }

    #[test]
    fn garbage_collection() {
        let rng = Rng::from_seed([1; 32]);
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("SystemTime before UNIX EPOCH!")
            .as_secs();

        let identity_secret = SecretKey::from_bytes(rng.random_array().unwrap());

        // Initialise key manager with one invalid key bundle.
        let y = KeyManager::init_and_generate_prekey(
            &identity_secret,
            Lifetime::from_range(now - 120, now - 60), // expired lifetime
            &rng,
        )
        .unwrap();
        assert_eq!(y.prekeys.len(), 1);

        // Add second _valid_ key bundle.
        let y = KeyManager::rotate_prekey(y, Lifetime::default(), &rng).unwrap();
        assert_eq!(y.prekeys.len(), 2);

        // Remove all expired bundles.
        let y = KeyManager::remove_expired(y);
        assert_eq!(y.prekeys.len(), 1);
    }
}