p2panda_encryption/traits/
key_manager.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use std::error::Error;
4use std::fmt::Debug;
5
6use serde::{Deserialize, Serialize};
7
8use crate::crypto::Rng;
9use crate::crypto::x25519::SecretKey;
10use crate::key_bundle::{Lifetime, LongTermKeyBundle, OneTimeKeyBundle, OneTimePreKeyId, PreKeyId};
11
12/// Manages our own identity secret.
13pub trait IdentityManager<Y> {
14    fn identity_secret(y: &Y) -> &SecretKey;
15}
16
17/// Manages our own pre-key secrets to generate public key bundles.
18pub trait PreKeyManager {
19    type State: Debug + Serialize + for<'a> Deserialize<'a>;
20
21    type Error: Error;
22
23    fn prekey_secret<'a>(
24        y: &'a Self::State,
25        id: &'a PreKeyId,
26    ) -> Result<&'a SecretKey, Self::Error>;
27
28    fn rotate_prekey(
29        y: Self::State,
30        lifetime: Lifetime,
31        rng: &Rng,
32    ) -> Result<Self::State, Self::Error>;
33
34    fn prekey_bundle(y: &Self::State) -> Result<LongTermKeyBundle, Self::Error>;
35
36    fn generate_onetime_bundle(
37        y: Self::State,
38        rng: &Rng,
39    ) -> Result<(Self::State, OneTimeKeyBundle), Self::Error>;
40
41    fn use_onetime_secret(
42        y: Self::State,
43        id: OneTimePreKeyId,
44    ) -> Result<(Self::State, Option<SecretKey>), Self::Error>;
45}