Skip to content

Commit 99264d2

Browse files
committed
Update PrivateKey -> X25519PrivateKey
1 parent 85ef763 commit 99264d2

11 files changed

Lines changed: 75 additions & 67 deletions

File tree

conversations/src/conversation/privatev1.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ impl Debug for PrivateV1Convo {
221221

222222
#[cfg(test)]
223223
mod tests {
224-
use x25519_dalek::StaticSecret;
224+
use crypto::X25519PrivateKey;
225225

226226
use super::*;
227227

228228
#[test]
229229
fn test_encrypt_roundtrip() {
230-
let saro = StaticSecret::random();
231-
let raya = StaticSecret::random();
230+
let saro = X25519PrivateKey::random();
231+
let raya = X25519PrivateKey::random();
232232

233233
let pub_raya = X25519PublicKey::from(&raya);
234234

conversations/src/crypto.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
pub use crypto::X25519PublicKey;
2-
pub use x25519_dalek::StaticSecret;
1+
pub use crypto::{X25519PrivateKey, X25519PublicKey};
32

43
use prost::bytes::Bytes;
54

conversations/src/identity.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::fmt;
22

3-
use crate::crypto::{StaticSecret, X25519PublicKey};
3+
use crate::crypto::{X25519PrivateKey, X25519PublicKey};
44

55
pub struct Identity {
6-
secret: StaticSecret,
6+
secret: X25519PrivateKey,
77
}
88

99
impl fmt::Debug for Identity {
@@ -18,15 +18,15 @@ impl fmt::Debug for Identity {
1818
impl Identity {
1919
pub fn new() -> Self {
2020
Self {
21-
secret: StaticSecret::random(),
21+
secret: X25519PrivateKey::random(),
2222
}
2323
}
2424

2525
pub fn public_key(&self) -> X25519PublicKey {
2626
X25519PublicKey::from(&self.secret)
2727
}
2828

29-
pub fn secret(&self) -> &StaticSecret {
29+
pub fn secret(&self) -> &X25519PrivateKey {
3030
&self.secret
3131
}
3232
}

conversations/src/inbox/handler.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crypto::{PrekeyBundle, SymmetricKey32};
1010

1111
use crate::context::Introduction;
1212
use crate::conversation::{ChatError, ConversationId, Convo, Id, PrivateV1Convo};
13-
use crate::crypto::{CopyBytes, StaticSecret, X25519PublicKey};
13+
use crate::crypto::{CopyBytes, X25519PrivateKey, X25519PublicKey};
1414
use crate::identity::Identity;
1515
use crate::inbox::handshake::InboxHandshake;
1616
use crate::proto;
@@ -25,7 +25,7 @@ fn delivery_address_for_installation(_: X25519PublicKey) -> String {
2525
pub struct Inbox {
2626
ident: Rc<Identity>,
2727
local_convo_id: String,
28-
ephemeral_keys: HashMap<String, StaticSecret>,
28+
ephemeral_keys: HashMap<String, X25519PrivateKey>,
2929
}
3030

3131
impl std::fmt::Debug for Inbox {
@@ -47,12 +47,12 @@ impl Inbox {
4747
Self {
4848
ident,
4949
local_convo_id,
50-
ephemeral_keys: HashMap::<String, StaticSecret>::new(),
50+
ephemeral_keys: HashMap::<String, X25519PrivateKey>::new(),
5151
}
5252
}
5353

5454
pub fn create_intro_bundle(&mut self) -> Introduction {
55-
let ephemeral = StaticSecret::random();
55+
let ephemeral = X25519PrivateKey::random();
5656

5757
let ephemeral_key: X25519PublicKey = (&ephemeral).into();
5858
self.ephemeral_keys
@@ -169,7 +169,7 @@ impl Inbox {
169169

170170
fn perform_handshake(
171171
&self,
172-
ephemeral_key: &StaticSecret,
172+
ephemeral_key: &X25519PrivateKey,
173173
header: proto::InboxHeaderV1,
174174
bytes: Bytes,
175175
) -> Result<(SymmetricKey32, proto::InboxV1Frame), ChatError> {
@@ -215,7 +215,7 @@ impl Inbox {
215215
Ok(frame)
216216
}
217217

218-
fn lookup_ephemeral_key(&self, key: &str) -> Result<&StaticSecret, ChatError> {
218+
fn lookup_ephemeral_key(&self, key: &str) -> Result<&X25519PrivateKey, ChatError> {
219219
self.ephemeral_keys
220220
.get(key)
221221
.ok_or(ChatError::UnknownEphemeralKey())

conversations/src/inbox/handshake.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use blake2::{
55
use crypto::{DomainSeparator, PrekeyBundle, SymmetricKey32, X3Handshake};
66
use rand_core::{CryptoRng, RngCore};
77

8-
use crate::crypto::{StaticSecret, X25519PublicKey};
8+
use crate::crypto::{X25519PrivateKey, X25519PublicKey};
99

1010
type Blake2bMac256 = Blake2bMac<U32>;
1111

@@ -21,7 +21,7 @@ pub struct InboxHandshake {}
2121
impl InboxHandshake {
2222
/// Performs
2323
pub fn perform_as_initiator<R: RngCore + CryptoRng>(
24-
identity_keypair: &StaticSecret,
24+
identity_keypair: &X25519PrivateKey,
2525
recipient_bundle: &PrekeyBundle,
2626
rng: &mut R,
2727
) -> (SymmetricKey32, X25519PublicKey) {
@@ -42,9 +42,9 @@ impl InboxHandshake {
4242
/// * `initiator_identity` - Initiator's identity public key
4343
/// * `initiator_ephemeral` - Initiator's ephemeral public key
4444
pub fn perform_as_responder(
45-
identity_keypair: &StaticSecret,
46-
signed_prekey: &StaticSecret,
47-
onetime_prekey: Option<&StaticSecret>,
45+
identity_keypair: &X25519PrivateKey,
46+
signed_prekey: &X25519PrivateKey,
47+
onetime_prekey: Option<&X25519PrivateKey>,
4848
initiator_identity: &X25519PublicKey,
4949
initiator_ephemeral: &X25519PublicKey,
5050
) -> SymmetricKey32 {
@@ -85,12 +85,12 @@ mod tests {
8585
let mut rng = OsRng;
8686

8787
// Alice (initiator) generates her identity key
88-
let alice_identity = StaticSecret::random_from_rng(rng);
88+
let alice_identity = X25519PrivateKey::random_from_rng(rng);
8989
let alice_identity_pub = X25519PublicKey::from(&alice_identity);
9090

9191
// Bob (responder) generates his keys
92-
let bob_identity = StaticSecret::random_from_rng(rng);
93-
let bob_signed_prekey = StaticSecret::random_from_rng(rng);
92+
let bob_identity = X25519PrivateKey::random_from_rng(rng);
93+
let bob_signed_prekey = X25519PrivateKey::random_from_rng(rng);
9494
let bob_signed_prekey_pub = X25519PublicKey::from(&bob_signed_prekey);
9595

9696
// Create Bob's prekey bundle

conversations/src/inbox/introduction.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
22
use chat_proto::logoschat::intro::IntroBundle;
3-
use crypto::{Ed25519Signature, X25519PublicKey};
3+
use crypto::{Ed25519Signature, X25519PrivateKey, X25519PublicKey};
44
use prost::Message;
55
use rand_core::{CryptoRng, RngCore};
6-
use x25519_dalek::StaticSecret;
76

87
use crate::errors::ChatError;
98

@@ -17,7 +16,7 @@ fn intro_binding_message(ephemeral: &X25519PublicKey) -> Vec<u8> {
1716
}
1817

1918
pub(crate) fn sign_intro_binding<R: RngCore + CryptoRng>(
20-
secret: &StaticSecret,
19+
secret: &X25519PrivateKey,
2120
ephemeral: &X25519PublicKey,
2221
rng: R,
2322
) -> Ed25519Signature {
@@ -44,7 +43,7 @@ pub struct Introduction {
4443
impl Introduction {
4544
/// Create a new `Introduction` by signing the ephemeral key with the installation secret.
4645
pub(crate) fn new<R: RngCore + CryptoRng>(
47-
installation_secret: &StaticSecret,
46+
installation_secret: &X25519PrivateKey,
4847
ephemeral_key: X25519PublicKey,
4948
rng: R,
5049
) -> Self {
@@ -147,9 +146,9 @@ mod tests {
147146
use rand_core::OsRng;
148147

149148
fn create_test_introduction() -> Introduction {
150-
let install_secret = StaticSecret::random_from_rng(OsRng);
149+
let install_secret = X25519PrivateKey::random_from_rng(OsRng);
151150

152-
let ephemeral_secret = StaticSecret::random_from_rng(OsRng);
151+
let ephemeral_secret = X25519PrivateKey::random_from_rng(OsRng);
153152
let ephemeral_pub: X25519PublicKey = (&ephemeral_secret).into();
154153

155154
Introduction::new(&install_secret, ephemeral_pub, OsRng)

crypto/src/keys.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub use generic_array::{GenericArray, typenum::U32};
22

33
use rand_core::{CryptoRng, OsRng, RngCore};
44
use std::{fmt::Debug, ops::Deref};
5-
use x25519_dalek::{PublicKey, SharedSecret, StaticSecret};
5+
use x25519_dalek;
66
use xeddsa::xed25519;
77
use zeroize::{Zeroize, ZeroizeOnDrop};
88

@@ -15,9 +15,9 @@ impl From<x25519_dalek::PublicKey> for X25519PublicKey {
1515
}
1616
}
1717

18-
impl From<&StaticSecret> for X25519PublicKey {
19-
fn from(value: &StaticSecret) -> Self {
20-
Self(x25519_dalek::PublicKey::from(value))
18+
impl From<&X25519PrivateKey> for X25519PublicKey {
19+
fn from(value: &X25519PrivateKey) -> Self {
20+
Self(x25519_dalek::PublicKey::from(&value.0))
2121
}
2222
}
2323

@@ -28,7 +28,7 @@ impl From<[u8; 32]> for X25519PublicKey {
2828
}
2929

3030
impl Deref for X25519PublicKey {
31-
type Target = PublicKey;
31+
type Target = x25519_dalek::PublicKey;
3232
fn deref(&self) -> &Self::Target {
3333
&self.0
3434
}
@@ -60,13 +60,25 @@ impl X25519PrivateKey {
6060
}
6161
}
6262

63+
impl From<[u8; 32]> for X25519PrivateKey {
64+
fn from(value: [u8; 32]) -> Self {
65+
Self(x25519_dalek::StaticSecret::from(value))
66+
}
67+
}
68+
6369
impl Deref for X25519PrivateKey {
6470
type Target = x25519_dalek::StaticSecret;
6571
fn deref(&self) -> &Self::Target {
6672
&self.0
6773
}
6874
}
6975

76+
impl From<&X25519PrivateKey> for xed25519::PrivateKey {
77+
fn from(value: &X25519PrivateKey) -> Self {
78+
Self::from(&value.0)
79+
}
80+
}
81+
7082
/// A Generic secret key container for symmetric keys.
7183
/// SymmetricKey retains ownership of bytes to ensure they are Zeroized on drop.
7284
#[derive(Clone, Zeroize, ZeroizeOnDrop, PartialEq)]
@@ -107,10 +119,10 @@ impl From<GenericArray<u8, U32>> for SymmetricKey32 {
107119
}
108120
}
109121

110-
impl From<&SharedSecret> for SymmetricKey32 {
122+
impl From<&x25519_dalek::SharedSecret> for SymmetricKey32 {
111123
// This relies on the feature 'zeroize' being set for x25519-dalek.
112124
// If not the SharedSecret will need to manually zeroized
113-
fn from(value: &SharedSecret) -> Self {
125+
fn from(value: &x25519_dalek::SharedSecret) -> Self {
114126
value.to_bytes().into()
115127
}
116128
}

crypto/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ mod keys;
22
mod x3dh;
33
mod xeddsa_sign;
44

5-
pub use keys::{GenericArray, SymmetricKey32, X25519PublicKey};
5+
pub use keys::{GenericArray, SymmetricKey32, X25519PrivateKey, X25519PublicKey};
66
pub use x3dh::{DomainSeparator, PrekeyBundle, X3Handshake};
77
pub use xeddsa_sign::{Ed25519Signature, SignatureError, xeddsa_sign, xeddsa_verify};

crypto/src/x3dh.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use std::marker::PhantomData;
33
use hkdf::Hkdf;
44
use rand_core::{CryptoRng, RngCore};
55
use sha2::Sha256;
6-
use x25519_dalek::{SharedSecret, StaticSecret};
6+
use x25519_dalek::SharedSecret;
77

8-
use crate::keys::{SymmetricKey32, X25519PublicKey};
8+
use crate::keys::{SymmetricKey32, X25519PrivateKey, X25519PublicKey};
99
use crate::xeddsa_sign::Ed25519Signature;
1010

1111
/// A prekey bundle containing the public keys needed to initiate an X3DH key exchange.
@@ -67,12 +67,12 @@ impl<D: DomainSeparator> X3Handshake<D> {
6767
/// # Returns
6868
/// A tuple of (shared secret bytes, ephemeral public key)
6969
pub fn initator<R: RngCore + CryptoRng>(
70-
identity_keypair: &StaticSecret,
70+
identity_keypair: &X25519PrivateKey,
7171
recipient_bundle: &PrekeyBundle,
7272
rng: &mut R,
7373
) -> (SymmetricKey32, X25519PublicKey) {
74-
// Generate ephemeral key for this handshake (using StaticSecret for multiple DH operations)
75-
let ephemeral_secret = StaticSecret::random_from_rng(rng);
74+
// Generate ephemeral key for this handshake (using X25519PrivateKey for multiple DH operations)
75+
let ephemeral_secret = X25519PrivateKey::random_from_rng(rng);
7676
let ephemeral_public = X25519PublicKey::from(&ephemeral_secret);
7777

7878
// Perform the 4 Diffie-Hellman operations
@@ -102,9 +102,9 @@ impl<D: DomainSeparator> X3Handshake<D> {
102102
/// # Returns
103103
/// The derived shared secret bytes
104104
pub fn responder(
105-
identity_keypair: &StaticSecret,
106-
signed_prekey: &StaticSecret,
107-
onetime_prekey: Option<&StaticSecret>,
105+
identity_keypair: &X25519PrivateKey,
106+
signed_prekey: &X25519PrivateKey,
107+
onetime_prekey: Option<&X25519PrivateKey>,
108108
initiator_identity: &X25519PublicKey,
109109
initiator_ephemeral: &X25519PublicKey,
110110
) -> SymmetricKey32 {
@@ -135,17 +135,17 @@ mod tests {
135135
let mut rng = OsRng;
136136

137137
// Alice (initiator) generates her identity key
138-
let alice_identity = StaticSecret::random_from_rng(rng);
138+
let alice_identity = X25519PrivateKey::random_from_rng(rng);
139139
let alice_identity_pub = X25519PublicKey::from(&alice_identity);
140140

141141
// Bob (responder) generates his keys
142-
let bob_identity = StaticSecret::random_from_rng(rng);
142+
let bob_identity = X25519PrivateKey::random_from_rng(rng);
143143
let bob_identity_pub = X25519PublicKey::from(&bob_identity);
144144

145-
let bob_signed_prekey = StaticSecret::random_from_rng(rng);
145+
let bob_signed_prekey = X25519PrivateKey::random_from_rng(rng);
146146
let bob_signed_prekey_pub = X25519PublicKey::from(&bob_signed_prekey);
147147

148-
let bob_onetime_prekey = StaticSecret::random_from_rng(rng);
148+
let bob_onetime_prekey = X25519PrivateKey::random_from_rng(rng);
149149
let bob_onetime_prekey_pub = X25519PublicKey::from(&bob_onetime_prekey);
150150

151151
// Create Bob's prekey bundle (with one-time prekey)
@@ -178,14 +178,14 @@ mod tests {
178178
let mut rng = OsRng;
179179

180180
// Alice (initiator) generates her identity key
181-
let alice_identity = StaticSecret::random_from_rng(rng);
181+
let alice_identity = X25519PrivateKey::random_from_rng(rng);
182182
let alice_identity_pub = X25519PublicKey::from(&alice_identity);
183183

184184
// Bob (responder) generates his keys
185-
let bob_identity = StaticSecret::random_from_rng(rng);
185+
let bob_identity = X25519PrivateKey::random_from_rng(rng);
186186
let bob_identity_pub = X25519PublicKey::from(&bob_identity);
187187

188-
let bob_signed_prekey = StaticSecret::random_from_rng(rng);
188+
let bob_signed_prekey = X25519PrivateKey::random_from_rng(rng);
189189
let bob_signed_prekey_pub = X25519PublicKey::from(&bob_signed_prekey);
190190

191191
// Create Bob's prekey bundle (without one-time prekey)

0 commit comments

Comments
 (0)