mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-03-27 22:53:07 +00:00
Rename SecretKey to SymmetricKey32
This commit is contained in:
parent
14535369fd
commit
7abd7f1f80
@ -6,7 +6,7 @@ use chat_proto::logoschat::{
|
|||||||
convos::private_v1::{PrivateV1Frame, private_v1_frame::FrameType},
|
convos::private_v1::{PrivateV1Frame, private_v1_frame::FrameType},
|
||||||
encryption::{Doubleratchet, EncryptedPayload, encrypted_payload::Encryption},
|
encryption::{Doubleratchet, EncryptedPayload, encrypted_payload::Encryption},
|
||||||
};
|
};
|
||||||
use crypto::SecretKey;
|
use crypto::SymmetricKey32;
|
||||||
use double_ratchets::{Header, InstallationKeyPair, RatchetState};
|
use double_ratchets::{Header, InstallationKeyPair, RatchetState};
|
||||||
use prost::{Message, bytes::Bytes};
|
use prost::{Message, bytes::Bytes};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
@ -38,7 +38,7 @@ impl Role {
|
|||||||
struct BaseConvoId([u8; 18]);
|
struct BaseConvoId([u8; 18]);
|
||||||
|
|
||||||
impl BaseConvoId {
|
impl BaseConvoId {
|
||||||
fn new(key: &SecretKey) -> Self {
|
fn new(key: &SymmetricKey32) -> Self {
|
||||||
let base = Blake2bMac::<U18>::new_with_salt_and_personal(key.as_bytes(), b"", b"L-PV1-CID")
|
let base = Blake2bMac::<U18>::new_with_salt_and_personal(key.as_bytes(), b"", b"L-PV1-CID")
|
||||||
.expect("fixed inputs should never fail");
|
.expect("fixed inputs should never fail");
|
||||||
Self(base.finalize_fixed().into())
|
Self(base.finalize_fixed().into())
|
||||||
@ -60,12 +60,12 @@ pub struct PrivateV1Convo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl PrivateV1Convo {
|
impl PrivateV1Convo {
|
||||||
pub fn new_initiator(seed_key: SecretKey, remote: PublicKey) -> Self {
|
pub fn new_initiator(seed_key: SymmetricKey32, remote: PublicKey) -> Self {
|
||||||
let base_convo_id = BaseConvoId::new(&seed_key);
|
let base_convo_id = BaseConvoId::new(&seed_key);
|
||||||
let local_convo_id = base_convo_id.id_for_participant(Role::Initiator);
|
let local_convo_id = base_convo_id.id_for_participant(Role::Initiator);
|
||||||
let remote_convo_id = base_convo_id.id_for_participant(Role::Responder);
|
let remote_convo_id = base_convo_id.id_for_participant(Role::Responder);
|
||||||
|
|
||||||
// TODO: Danger - Fix double-ratchets types to Accept SecretKey
|
// TODO: Danger - Fix double-ratchets types to Accept SymmetricKey32
|
||||||
// perhaps update the DH to work with cryptocrate.
|
// perhaps update the DH to work with cryptocrate.
|
||||||
// init_sender doesn't take ownership of the key so a reference can be used.
|
// init_sender doesn't take ownership of the key so a reference can be used.
|
||||||
let shared_secret: [u8; 32] = seed_key.as_bytes().to_vec().try_into().unwrap();
|
let shared_secret: [u8; 32] = seed_key.as_bytes().to_vec().try_into().unwrap();
|
||||||
@ -79,14 +79,14 @@ impl PrivateV1Convo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_responder(
|
pub fn new_responder(
|
||||||
seed_key: SecretKey,
|
seed_key: SymmetricKey32,
|
||||||
dh_self: InstallationKeyPair, // TODO: (P3) Rename; This accepts a Ephemeral key in most cases
|
dh_self: InstallationKeyPair, // TODO: (P3) Rename; This accepts a Ephemeral key in most cases
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let base_convo_id = BaseConvoId::new(&seed_key);
|
let base_convo_id = BaseConvoId::new(&seed_key);
|
||||||
let local_convo_id = base_convo_id.id_for_participant(Role::Responder);
|
let local_convo_id = base_convo_id.id_for_participant(Role::Responder);
|
||||||
let remote_convo_id = base_convo_id.id_for_participant(Role::Initiator);
|
let remote_convo_id = base_convo_id.id_for_participant(Role::Initiator);
|
||||||
|
|
||||||
// TODO: Danger - Fix double-ratchets types to Accept SecretKey
|
// TODO: Danger - Fix double-ratchets types to Accept SymmetricKey32
|
||||||
let dr_state = RatchetState::init_receiver(seed_key.DANGER_to_bytes(), dh_self);
|
let dr_state = RatchetState::init_receiver(seed_key.DANGER_to_bytes(), dh_self);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@ -6,7 +6,7 @@ use rand_core::OsRng;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crypto::{PrekeyBundle, SecretKey};
|
use crypto::{PrekeyBundle, SymmetricKey32};
|
||||||
|
|
||||||
use crate::context::Introduction;
|
use crate::context::Introduction;
|
||||||
use crate::conversation::{ChatError, ConversationId, Convo, Id, PrivateV1Convo};
|
use crate::conversation::{ChatError, ConversationId, Convo, Id, PrivateV1Convo};
|
||||||
@ -172,7 +172,7 @@ impl Inbox {
|
|||||||
ephemeral_key: &StaticSecret,
|
ephemeral_key: &StaticSecret,
|
||||||
header: proto::InboxHeaderV1,
|
header: proto::InboxHeaderV1,
|
||||||
bytes: Bytes,
|
bytes: Bytes,
|
||||||
) -> Result<(SecretKey, proto::InboxV1Frame), ChatError> {
|
) -> Result<(SymmetricKey32, proto::InboxV1Frame), ChatError> {
|
||||||
// Get PublicKeys from protobuf
|
// Get PublicKeys from protobuf
|
||||||
let initator_static = PublicKey::from(
|
let initator_static = PublicKey::from(
|
||||||
<[u8; 32]>::try_from(header.initiator_static.as_ref())
|
<[u8; 32]>::try_from(header.initiator_static.as_ref())
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use blake2::{
|
|||||||
Blake2bMac,
|
Blake2bMac,
|
||||||
digest::{FixedOutput, consts::U32},
|
digest::{FixedOutput, consts::U32},
|
||||||
};
|
};
|
||||||
use crypto::{DomainSeparator, PrekeyBundle, SecretKey, X3Handshake};
|
use crypto::{DomainSeparator, PrekeyBundle, SymmetricKey32, X3Handshake};
|
||||||
use rand_core::{CryptoRng, RngCore};
|
use rand_core::{CryptoRng, RngCore};
|
||||||
|
|
||||||
use crate::crypto::{PublicKey, StaticSecret};
|
use crate::crypto::{PublicKey, StaticSecret};
|
||||||
@ -24,7 +24,7 @@ impl InboxHandshake {
|
|||||||
identity_keypair: &StaticSecret,
|
identity_keypair: &StaticSecret,
|
||||||
recipient_bundle: &PrekeyBundle,
|
recipient_bundle: &PrekeyBundle,
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
) -> (SecretKey, PublicKey) {
|
) -> (SymmetricKey32, PublicKey) {
|
||||||
// Perform X3DH handshake to get shared secret
|
// Perform X3DH handshake to get shared secret
|
||||||
let (shared_secret, ephemeral_public) =
|
let (shared_secret, ephemeral_public) =
|
||||||
InboxKeyExchange::initator(identity_keypair, recipient_bundle, rng);
|
InboxKeyExchange::initator(identity_keypair, recipient_bundle, rng);
|
||||||
@ -47,7 +47,7 @@ impl InboxHandshake {
|
|||||||
onetime_prekey: Option<&StaticSecret>,
|
onetime_prekey: Option<&StaticSecret>,
|
||||||
initiator_identity: &PublicKey,
|
initiator_identity: &PublicKey,
|
||||||
initiator_ephemeral: &PublicKey,
|
initiator_ephemeral: &PublicKey,
|
||||||
) -> SecretKey {
|
) -> SymmetricKey32 {
|
||||||
// Perform X3DH to get shared secret
|
// Perform X3DH to get shared secret
|
||||||
let shared_secret = InboxKeyExchange::responder(
|
let shared_secret = InboxKeyExchange::responder(
|
||||||
identity_keypair,
|
identity_keypair,
|
||||||
@ -61,7 +61,7 @@ impl InboxHandshake {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Derive keys from X3DH shared secret
|
/// Derive keys from X3DH shared secret
|
||||||
fn derive_keys_from_shared_secret(shared_secret: SecretKey) -> SecretKey {
|
fn derive_keys_from_shared_secret(shared_secret: SymmetricKey32) -> SymmetricKey32 {
|
||||||
let seed_key: [u8; 32] = Blake2bMac256::new_with_salt_and_personal(
|
let seed_key: [u8; 32] = Blake2bMac256::new_with_salt_and_personal(
|
||||||
shared_secret.as_slice(),
|
shared_secret.as_slice(),
|
||||||
&[], // No salt - input already has high entropy
|
&[], // No salt - input already has high entropy
|
||||||
|
|||||||
@ -2,6 +2,6 @@ mod keys;
|
|||||||
mod x3dh;
|
mod x3dh;
|
||||||
mod xeddsa_sign;
|
mod xeddsa_sign;
|
||||||
|
|
||||||
pub use keys::{GenericArray, SecretKey};
|
pub use keys::{GenericArray, SymmetricKey32};
|
||||||
pub use x3dh::{DomainSeparator, PrekeyBundle, X3Handshake};
|
pub use x3dh::{DomainSeparator, PrekeyBundle, X3Handshake};
|
||||||
pub use xeddsa_sign::{Ed25519Signature, SignatureError, xeddsa_sign, xeddsa_verify};
|
pub use xeddsa_sign::{Ed25519Signature, SignatureError, xeddsa_sign, xeddsa_verify};
|
||||||
|
|||||||
@ -5,7 +5,7 @@ use rand_core::{CryptoRng, RngCore};
|
|||||||
use sha2::Sha256;
|
use sha2::Sha256;
|
||||||
use x25519_dalek::{PublicKey, SharedSecret, StaticSecret};
|
use x25519_dalek::{PublicKey, SharedSecret, StaticSecret};
|
||||||
|
|
||||||
use crate::keys::SecretKey;
|
use crate::keys::SymmetricKey32;
|
||||||
use crate::xeddsa_sign::Ed25519Signature;
|
use crate::xeddsa_sign::Ed25519Signature;
|
||||||
|
|
||||||
/// A prekey bundle containing the public keys needed to initiate an X3DH key exchange.
|
/// A prekey bundle containing the public keys needed to initiate an X3DH key exchange.
|
||||||
@ -36,7 +36,7 @@ impl<D: DomainSeparator> X3Handshake<D> {
|
|||||||
dh2: &SharedSecret,
|
dh2: &SharedSecret,
|
||||||
dh3: &SharedSecret,
|
dh3: &SharedSecret,
|
||||||
dh4: Option<&SharedSecret>,
|
dh4: Option<&SharedSecret>,
|
||||||
) -> SecretKey {
|
) -> SymmetricKey32 {
|
||||||
// Concatenate all DH outputs
|
// Concatenate all DH outputs
|
||||||
let mut km = Vec::new();
|
let mut km = Vec::new();
|
||||||
km.extend_from_slice(dh1.as_bytes());
|
km.extend_from_slice(dh1.as_bytes());
|
||||||
@ -53,7 +53,7 @@ impl<D: DomainSeparator> X3Handshake<D> {
|
|||||||
hk.expand(Self::domain_separator(), &mut output)
|
hk.expand(Self::domain_separator(), &mut output)
|
||||||
.expect("32 bytes is valid HKDF output length");
|
.expect("32 bytes is valid HKDF output length");
|
||||||
|
|
||||||
// Move into SecretKey so it gets zeroized on drop.
|
// Move into SymmetricKey32 so it gets zeroized on drop.
|
||||||
output.into()
|
output.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ impl<D: DomainSeparator> X3Handshake<D> {
|
|||||||
identity_keypair: &StaticSecret,
|
identity_keypair: &StaticSecret,
|
||||||
recipient_bundle: &PrekeyBundle,
|
recipient_bundle: &PrekeyBundle,
|
||||||
rng: &mut R,
|
rng: &mut R,
|
||||||
) -> (SecretKey, PublicKey) {
|
) -> (SymmetricKey32, PublicKey) {
|
||||||
// Generate ephemeral key for this handshake (using StaticSecret for multiple DH operations)
|
// Generate ephemeral key for this handshake (using StaticSecret for multiple DH operations)
|
||||||
let ephemeral_secret = StaticSecret::random_from_rng(rng);
|
let ephemeral_secret = StaticSecret::random_from_rng(rng);
|
||||||
let ephemeral_public = PublicKey::from(&ephemeral_secret);
|
let ephemeral_public = PublicKey::from(&ephemeral_secret);
|
||||||
@ -107,7 +107,7 @@ impl<D: DomainSeparator> X3Handshake<D> {
|
|||||||
onetime_prekey: Option<&StaticSecret>,
|
onetime_prekey: Option<&StaticSecret>,
|
||||||
initiator_identity: &PublicKey,
|
initiator_identity: &PublicKey,
|
||||||
initiator_ephemeral: &PublicKey,
|
initiator_ephemeral: &PublicKey,
|
||||||
) -> SecretKey {
|
) -> SymmetricKey32 {
|
||||||
let dh1 = signed_prekey.diffie_hellman(initiator_identity);
|
let dh1 = signed_prekey.diffie_hellman(initiator_identity);
|
||||||
let dh2 = identity_keypair.diffie_hellman(initiator_ephemeral);
|
let dh2 = identity_keypair.diffie_hellman(initiator_ephemeral);
|
||||||
let dh3 = signed_prekey.diffie_hellman(initiator_ephemeral);
|
let dh3 = signed_prekey.diffie_hellman(initiator_ephemeral);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user