Skip to content

Commit

Permalink
Merge branch 'main' into johannes/megolm-mutation-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Johennes committed Apr 20, 2024
2 parents 3e8d6bb + cfeb1b1 commit 779c18e
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 187 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @dkasak @poljar
62 changes: 34 additions & 28 deletions src/megolm/group_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,40 +146,46 @@ impl GroupSession {
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, crate::LibolmPickleError> {
use matrix_pickle::Decode;
use zeroize::Zeroize;
use crate::{megolm::group_session::libolm_compat::Pickle, utilities::unpickle_libolm};

use crate::{
megolm::libolm::LibolmRatchetPickle,
utilities::{unpickle_libolm, LibolmEd25519Keypair},
};
const PICKLE_VERSION: u32 = 1;
unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
}
}

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
struct Pickle {
version: u32,
ratchet: LibolmRatchetPickle,
ed25519_keypair: LibolmEd25519Keypair,
}
#[cfg(feature = "libolm-compat")]
mod libolm_compat {
use matrix_pickle::Decode;
use zeroize::Zeroize;

use super::GroupSession;
use crate::{
megolm::{libolm::LibolmRatchetPickle, SessionConfig},
utilities::LibolmEd25519Keypair,
Ed25519Keypair,
};

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
pub(super) struct Pickle {
version: u32,
ratchet: LibolmRatchetPickle,
ed25519_keypair: LibolmEd25519Keypair,
}

impl TryFrom<Pickle> for GroupSession {
type Error = crate::LibolmPickleError;
impl TryFrom<Pickle> for GroupSession {
type Error = crate::LibolmPickleError;

fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
// Removing the borrow doesn't work and clippy complains about
// this on nightly.
#[allow(clippy::needless_borrow)]
let ratchet = (&pickle.ratchet).into();
let signing_key =
Ed25519Keypair::from_expanded_key(&pickle.ed25519_keypair.private_key)?;
fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
// Removing the borrow doesn't work and clippy complains about
// this on nightly.
#[allow(clippy::needless_borrow)]
let ratchet = (&pickle.ratchet).into();
let signing_key =
Ed25519Keypair::from_expanded_key(&pickle.ed25519_keypair.private_key)?;

Ok(Self { ratchet, signing_key, config: SessionConfig::version_1() })
}
Ok(Self { ratchet, signing_key, config: SessionConfig::version_1() })
}

const PICKLE_VERSION: u32 = 1;

unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
}
}

Expand Down
87 changes: 48 additions & 39 deletions src/megolm/inbound_group_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use hmac::digest::MacError;
use serde::{Deserialize, Serialize};
use subtle::ConstantTimeEq;
use thiserror::Error;
use zeroize::Zeroize;

use super::{
default_config,
Expand Down Expand Up @@ -371,50 +370,60 @@ impl InboundGroupSession {
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, crate::LibolmPickleError> {
use matrix_pickle::Decode;

use super::libolm::LibolmRatchetPickle;
use crate::utilities::unpickle_libolm;

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
struct Pickle {
version: u32,
initial_ratchet: LibolmRatchetPickle,
latest_ratchet: LibolmRatchetPickle,
signing_key: [u8; 32],
signing_key_verified: bool,
}

impl TryFrom<Pickle> for InboundGroupSession {
type Error = crate::LibolmPickleError;

fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
// Removing the borrow doesn't work and clippy complains about
// this on nightly.
#[allow(clippy::needless_borrow)]
let initial_ratchet = (&pickle.initial_ratchet).into();
#[allow(clippy::needless_borrow)]
let latest_ratchet = (&pickle.latest_ratchet).into();
let signing_key = Ed25519PublicKey::from_slice(&pickle.signing_key)?;
let signing_key_verified = pickle.signing_key_verified;

Ok(Self {
initial_ratchet,
latest_ratchet,
signing_key,
signing_key_verified,
config: SessionConfig::version_1(),
})
}
}
use crate::{
megolm::inbound_group_session::libolm_compat::Pickle, utilities::unpickle_libolm,
};

const PICKLE_VERSION: u32 = 2;

unpickle_libolm::<Pickle, _>(pickle, pickle_key, PICKLE_VERSION)
}
}

#[cfg(feature = "libolm-compat")]
mod libolm_compat {
use matrix_pickle::Decode;
use zeroize::Zeroize;

use super::InboundGroupSession;
use crate::{
megolm::{libolm::LibolmRatchetPickle, SessionConfig},
Ed25519PublicKey,
};

#[derive(Zeroize, Decode)]
#[zeroize(drop)]
pub(super) struct Pickle {
version: u32,
initial_ratchet: LibolmRatchetPickle,
latest_ratchet: LibolmRatchetPickle,
signing_key: [u8; 32],
signing_key_verified: bool,
}

impl TryFrom<Pickle> for InboundGroupSession {
type Error = crate::LibolmPickleError;

fn try_from(pickle: Pickle) -> Result<Self, Self::Error> {
// Removing the borrow doesn't work and clippy complains about
// this on nightly.
#[allow(clippy::needless_borrow)]
let initial_ratchet = (&pickle.initial_ratchet).into();
#[allow(clippy::needless_borrow)]
let latest_ratchet = (&pickle.latest_ratchet).into();
let signing_key = Ed25519PublicKey::from_slice(&pickle.signing_key)?;
let signing_key_verified = pickle.signing_key_verified;

Ok(Self {
initial_ratchet,
latest_ratchet,
signing_key,
signing_key_verified,
config: SessionConfig::version_1(),
})
}
}
}

/// A format suitable for serialization which implements [`serde::Serialize`]
/// and [`serde::Deserialize`]. Obtainable by calling
/// [`InboundGroupSession::pickle`].
Expand Down
Loading

0 comments on commit 779c18e

Please sign in to comment.