libsignal_protocol/ratchet/
params.rs

1//
2// Copyright 2020 Signal Messenger, LLC.
3// SPDX-License-Identifier: AGPL-3.0-only
4//
5
6use crate::{kem, IdentityKey, IdentityKeyPair, KeyPair, PublicKey};
7
8#[derive(Clone, Copy)]
9pub enum UsePQRatchet {
10    No,
11    Yes,
12}
13
14impl From<bool> for UsePQRatchet {
15    fn from(value: bool) -> Self {
16        if value {
17            UsePQRatchet::Yes
18        } else {
19            UsePQRatchet::No
20        }
21    }
22}
23
24pub struct AliceSignalProtocolParameters {
25    our_identity_key_pair: IdentityKeyPair,
26    our_base_key_pair: KeyPair,
27
28    their_identity_key: IdentityKey,
29    their_signed_pre_key: PublicKey,
30    their_one_time_pre_key: Option<PublicKey>,
31    their_ratchet_key: PublicKey,
32    their_kyber_pre_key: kem::PublicKey,
33
34    use_pq_ratchet: UsePQRatchet,
35}
36
37impl AliceSignalProtocolParameters {
38    pub fn new(
39        our_identity_key_pair: IdentityKeyPair,
40        our_base_key_pair: KeyPair,
41        their_identity_key: IdentityKey,
42        their_signed_pre_key: PublicKey,
43        their_ratchet_key: PublicKey,
44        their_kyber_pre_key: kem::PublicKey,
45        use_pq_ratchet: UsePQRatchet,
46    ) -> Self {
47        Self {
48            our_identity_key_pair,
49            our_base_key_pair,
50            their_identity_key,
51            their_signed_pre_key,
52            their_one_time_pre_key: None,
53            their_ratchet_key,
54            their_kyber_pre_key,
55            use_pq_ratchet,
56        }
57    }
58
59    pub fn set_their_one_time_pre_key(&mut self, ec_public: PublicKey) {
60        self.their_one_time_pre_key = Some(ec_public);
61    }
62
63    pub fn with_their_one_time_pre_key(mut self, ec_public: PublicKey) -> Self {
64        self.set_their_one_time_pre_key(ec_public);
65        self
66    }
67
68    #[inline]
69    pub fn our_identity_key_pair(&self) -> &IdentityKeyPair {
70        &self.our_identity_key_pair
71    }
72
73    #[inline]
74    pub fn our_base_key_pair(&self) -> &KeyPair {
75        &self.our_base_key_pair
76    }
77
78    #[inline]
79    pub fn their_identity_key(&self) -> &IdentityKey {
80        &self.their_identity_key
81    }
82
83    #[inline]
84    pub fn their_signed_pre_key(&self) -> &PublicKey {
85        &self.their_signed_pre_key
86    }
87
88    #[inline]
89    pub fn their_one_time_pre_key(&self) -> Option<&PublicKey> {
90        self.their_one_time_pre_key.as_ref()
91    }
92
93    #[inline]
94    pub fn their_kyber_pre_key(&self) -> &kem::PublicKey {
95        &self.their_kyber_pre_key
96    }
97
98    #[inline]
99    pub fn their_ratchet_key(&self) -> &PublicKey {
100        &self.their_ratchet_key
101    }
102
103    #[inline]
104    pub fn use_pq_ratchet(&self) -> UsePQRatchet {
105        self.use_pq_ratchet
106    }
107}
108
109pub struct BobSignalProtocolParameters<'a> {
110    our_identity_key_pair: IdentityKeyPair,
111    our_signed_pre_key_pair: KeyPair,
112    our_one_time_pre_key_pair: Option<KeyPair>,
113    our_ratchet_key_pair: KeyPair,
114    our_kyber_pre_key_pair: kem::KeyPair,
115
116    their_identity_key: IdentityKey,
117    their_base_key: PublicKey,
118    their_kyber_ciphertext: &'a kem::SerializedCiphertext,
119
120    use_pq_ratchet: UsePQRatchet,
121}
122
123impl<'a> BobSignalProtocolParameters<'a> {
124    #[allow(clippy::too_many_arguments)]
125    pub fn new(
126        our_identity_key_pair: IdentityKeyPair,
127        our_signed_pre_key_pair: KeyPair,
128        our_one_time_pre_key_pair: Option<KeyPair>,
129        our_ratchet_key_pair: KeyPair,
130        our_kyber_pre_key_pair: kem::KeyPair,
131        their_identity_key: IdentityKey,
132        their_base_key: PublicKey,
133        their_kyber_ciphertext: &'a kem::SerializedCiphertext,
134        use_pq_ratchet: UsePQRatchet,
135    ) -> Self {
136        Self {
137            our_identity_key_pair,
138            our_signed_pre_key_pair,
139            our_one_time_pre_key_pair,
140            our_ratchet_key_pair,
141            our_kyber_pre_key_pair,
142            their_identity_key,
143            their_base_key,
144            their_kyber_ciphertext,
145            use_pq_ratchet,
146        }
147    }
148
149    #[inline]
150    pub fn our_identity_key_pair(&self) -> &IdentityKeyPair {
151        &self.our_identity_key_pair
152    }
153
154    #[inline]
155    pub fn our_signed_pre_key_pair(&self) -> &KeyPair {
156        &self.our_signed_pre_key_pair
157    }
158
159    #[inline]
160    pub fn our_one_time_pre_key_pair(&self) -> Option<&KeyPair> {
161        self.our_one_time_pre_key_pair.as_ref()
162    }
163
164    #[inline]
165    pub fn our_ratchet_key_pair(&self) -> &KeyPair {
166        &self.our_ratchet_key_pair
167    }
168
169    #[inline]
170    pub fn our_kyber_pre_key_pair(&self) -> &kem::KeyPair {
171        &self.our_kyber_pre_key_pair
172    }
173
174    #[inline]
175    pub fn their_identity_key(&self) -> &IdentityKey {
176        &self.their_identity_key
177    }
178
179    #[inline]
180    pub fn their_base_key(&self) -> &PublicKey {
181        &self.their_base_key
182    }
183
184    #[inline]
185    pub fn their_kyber_ciphertext(&self) -> &kem::SerializedCiphertext {
186        self.their_kyber_ciphertext
187    }
188
189    #[inline]
190    pub fn use_pq_ratchet(&self) -> UsePQRatchet {
191        self.use_pq_ratchet
192    }
193}