Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into publication
Browse files Browse the repository at this point in the history
  • Loading branch information
PaarthShah committed Dec 2, 2024
2 parents 785d3a6 + ed07792 commit 5892502
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 202 deletions.
7 changes: 7 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
edition = "2021"
max_width = 100
comment_width = 80
wrap_comments = true
imports_granularity = "Crate"
use_small_heuristics = "Max"
group_imports = "StdExternalCrate"
10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vodozemac-python"
version = "0.7.0"
version = "0.8.1"
edition = "2021"
repository = "https://github.com/matrix-nio/vodozemac-python"
homepage = "https://github.com/matrix-nio/vodozemac-python"
Expand All @@ -14,16 +14,12 @@ crate-type = ["cdylib"]
paste = "1.0.15"
thiserror = "1.0.63"
[dependencies.vodozemac]
git = "https://github.com/matrix-org/vodozemac.git"
rev = "12f9036bf7f2536c172273602afcdc9aeddf8cf7"
version = "0.8.1"
features = ["insecure-pk-encryption"]

[package.metadata.maturin]
name = "vodozemac"

[dependencies.pyo3]
version = "0.22.2"
version = "0.23.2"
features = ["extension-module"]

[features]
gil-refs = []
42 changes: 11 additions & 31 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ use pyo3::{
};
use vodozemac::olm::SessionConfig;

use super::session::Session;
use crate::{
convert_to_pybytes,
error::{LibolmPickleError, PickleError, SessionError},
types::{Curve25519PublicKey, Ed25519PublicKey, Ed25519Signature, PreKeyMessage},
};

use super::session::Session;

#[pyclass]
pub struct Account {
inner: vodozemac::olm::Account,
Expand All @@ -23,9 +22,7 @@ pub struct Account {
impl Account {
#[new]
fn new() -> Self {
Self {
inner: vodozemac::olm::Account::new(),
}
Self { inner: vodozemac::olm::Account::new() }
}

#[classmethod]
Expand All @@ -34,9 +31,8 @@ impl Account {
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, PickleError> {
let pickle_key: &[u8; 32] = pickle_key
.try_into()
.map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle_key: &[u8; 32] =
pickle_key.try_into().map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;

let pickle = vodozemac::olm::AccountPickle::from_encrypted(pickle, pickle_key)?;

Expand All @@ -57,9 +53,8 @@ impl Account {
}

fn pickle(&self, pickle_key: &[u8]) -> Result<String, PickleError> {
let pickle_key: &[u8; 32] = pickle_key
.try_into()
.map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle_key: &[u8; 32] =
pickle_key.try_into().map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;

Ok(self.inner.pickle().encrypt(pickle_key))
}
Expand All @@ -80,11 +75,7 @@ impl Account {

#[getter]
fn one_time_keys(&self) -> HashMap<String, Curve25519PublicKey> {
self.inner
.one_time_keys()
.into_iter()
.map(|(k, v)| (k.to_base64(), v.into()))
.collect()
self.inner.one_time_keys().into_iter().map(|(k, v)| (k.to_base64(), v.into())).collect()
}

#[getter]
Expand All @@ -98,11 +89,7 @@ impl Account {

#[getter]
fn fallback_key(&self) -> HashMap<String, Curve25519PublicKey> {
self.inner
.fallback_key()
.into_iter()
.map(|(k, v)| (k.to_base64(), v.into()))
.collect()
self.inner.fallback_key().into_iter().map(|(k, v)| (k.to_base64(), v.into())).collect()
}

fn generate_fallback_key(&mut self) {
Expand Down Expand Up @@ -132,15 +119,8 @@ impl Account {
identity_key: &Curve25519PublicKey,
message: &PreKeyMessage,
) -> Result<(Session, Py<PyBytes>), SessionError> {
let result = self
.inner
.create_inbound_session(identity_key.inner, &message.inner)?;

Ok((
Session {
inner: result.session,
},
convert_to_pybytes(result.plaintext.as_slice()),
))
let result = self.inner.create_inbound_session(identity_key.inner, &message.inner)?;

Ok((Session { inner: result.session }, convert_to_pybytes(result.plaintext.as_slice())))
}
}
28 changes: 6 additions & 22 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,10 @@ create_error!(vodozemac::megolm::SessionKeyDecodeError, SessionKeyDecode);
create_error!(vodozemac::DecodeError, Decode);

pyo3::create_exception!(module, PickleException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(
module,
SessionCreationException,
pyo3::exceptions::PyValueError
);
pyo3::create_exception!(module, SessionCreationException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(module, SasException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(
module,
OlmDecryptionException,
pyo3::exceptions::PyValueError
);
pyo3::create_exception!(
module,
MegolmDecryptionException,
pyo3::exceptions::PyValueError
);
pyo3::create_exception!(module, OlmDecryptionException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(module, MegolmDecryptionException, pyo3::exceptions::PyValueError);

#[derive(Debug, Error)]
pub enum MegolmDecryptionError {
Expand Down Expand Up @@ -140,8 +128,8 @@ impl From<PickleError> for PyErr {
}
}

/// An error type describing failures which can happen during the use of `PkEncryption`
/// and `PkDecryption` objects.
/// An error type describing failures which can happen during the use of
/// `PkEncryption` and `PkDecryption` objects.
#[derive(Debug, Error)]
pub enum PkEncryptionError {
#[error("The key doesn't have the correct size, got {0}, expected 32 bytes")]
Expand All @@ -150,11 +138,7 @@ pub enum PkEncryptionError {
Decode(#[from] vodozemac::pk_encryption::Error),
}

pyo3::create_exception!(
module,
PkInvalidKeySizeException,
pyo3::exceptions::PyValueError
);
pyo3::create_exception!(module, PkInvalidKeySizeException, pyo3::exceptions::PyValueError);
pyo3::create_exception!(module, PkDecodeException, pyo3::exceptions::PyValueError);

impl From<PkEncryptionError> for PyErr {
Expand Down
34 changes: 11 additions & 23 deletions src/group_sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ pub struct GroupSession {
impl GroupSession {
#[new]
fn new() -> Self {
Self {
inner: vodozemac::megolm::GroupSession::new(SessionConfig::version_1()),
}
Self { inner: vodozemac::megolm::GroupSession::new(SessionConfig::version_1()) }
}

#[getter]
Expand All @@ -44,9 +42,8 @@ impl GroupSession {
}

fn pickle(&self, pickle_key: &[u8]) -> Result<String, PickleError> {
let pickle_key: &[u8; 32] = pickle_key
.try_into()
.map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle_key: &[u8; 32] =
pickle_key.try_into().map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;

Ok(self.inner.pickle().encrypt(pickle_key))
}
Expand All @@ -57,9 +54,8 @@ impl GroupSession {
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, PickleError> {
let pickle_key: &[u8; 32] = pickle_key
.try_into()
.map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle_key: &[u8; 32] =
pickle_key.try_into().map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle = vodozemac::megolm::GroupSessionPickle::from_encrypted(pickle, pickle_key)?;

let session = vodozemac::megolm::GroupSession::from_pickle(pickle);
Expand All @@ -78,10 +74,7 @@ pub struct DecryptedMessage {

impl DecryptedMessage {
fn new(plaintext: &[u8], message_index: u32) -> Self {
DecryptedMessage {
plaintext: convert_to_pybytes(plaintext),
message_index,
}
DecryptedMessage { plaintext: convert_to_pybytes(plaintext), message_index }
}
}

Expand Down Expand Up @@ -135,16 +128,12 @@ impl InboundGroupSession {
) -> Result<DecryptedMessage, MegolmDecryptionError> {
let ret = self.inner.decrypt(&message.inner)?;

Ok(DecryptedMessage::new(
ret.plaintext.as_slice(),
ret.message_index,
))
Ok(DecryptedMessage::new(ret.plaintext.as_slice(), ret.message_index))
}

fn pickle(&self, pickle_key: &[u8]) -> Result<String, PickleError> {
let pickle_key: &[u8; 32] = pickle_key
.try_into()
.map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle_key: &[u8; 32] =
pickle_key.try_into().map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;

Ok(self.inner.pickle().encrypt(pickle_key))
}
Expand All @@ -155,9 +144,8 @@ impl InboundGroupSession {
pickle: &str,
pickle_key: &[u8],
) -> Result<Self, PickleError> {
let pickle_key: &[u8; 32] = pickle_key
.try_into()
.map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle_key: &[u8; 32] =
pickle_key.try_into().map_err(|_| PickleError::InvalidKeySize(pickle_key.len()))?;
let pickle =
vodozemac::megolm::InboundGroupSessionPickle::from_encrypted(pickle, pickle_key)?;

Expand Down
50 changes: 13 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,46 +33,22 @@ fn my_module(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<pk_encryption::PkEncryption>()?;
m.add_class::<pk_encryption::Message>()?;

m.add("KeyException", py.get_type_bound::<KeyException>())?;
m.add(
"SignatureException",
py.get_type_bound::<SignatureException>(),
)?;
m.add("DecodeException", py.get_type_bound::<DecodeException>())?;
m.add(
"LibolmPickleException",
py.get_type_bound::<LibolmPickleException>(),
)?;
m.add(
"SessionKeyDecodeException",
py.get_type_bound::<SessionKeyDecodeException>(),
)?;
m.add("PickleException", py.get_type_bound::<PickleException>())?;
m.add(
"SessionCreationException",
py.get_type_bound::<SessionCreationException>(),
)?;
m.add("SasException", py.get_type_bound::<SasException>())?;
m.add(
"OlmDecryptionException",
py.get_type_bound::<OlmDecryptionException>(),
)?;
m.add(
"MegolmDecryptionException",
py.get_type_bound::<MegolmDecryptionException>(),
)?;
m.add(
"PkInvalidKeySizeException",
py.get_type_bound::<PkInvalidKeySizeException>(),
)?;
m.add(
"PkDecodeException",
py.get_type_bound::<PkDecodeException>(),
)?;
m.add("KeyException", py.get_type::<KeyException>())?;
m.add("SignatureException", py.get_type::<SignatureException>())?;
m.add("DecodeException", py.get_type::<DecodeException>())?;
m.add("LibolmPickleException", py.get_type::<LibolmPickleException>())?;
m.add("SessionKeyDecodeException", py.get_type::<SessionKeyDecodeException>())?;
m.add("PickleException", py.get_type::<PickleException>())?;
m.add("SessionCreationException", py.get_type::<SessionCreationException>())?;
m.add("SasException", py.get_type::<SasException>())?;
m.add("OlmDecryptionException", py.get_type::<OlmDecryptionException>())?;
m.add("MegolmDecryptionException", py.get_type::<MegolmDecryptionException>())?;
m.add("PkInvalidKeySizeException", py.get_type::<PkInvalidKeySizeException>())?;
m.add("PkDecodeException", py.get_type::<PkDecodeException>())?;

Ok(())
}

pub(crate) fn convert_to_pybytes(bytes: &[u8]) -> Py<PyBytes> {
Python::with_gil(|py| PyBytes::new_bound(py, bytes).into())
Python::with_gil(|py| PyBytes::new(py, bytes).into())
}
26 changes: 10 additions & 16 deletions src/pk_encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ impl PkDecryption {
/// Create a new random PkDecryption object.
#[new]
fn new() -> Self {
Self {
inner: vodozemac::pk_encryption::PkDecryption::new(),
}
Self { inner: vodozemac::pk_encryption::PkDecryption::new() }
}

/// Create a PkDecryption object from the secret key bytes.
Expand All @@ -55,9 +53,7 @@ impl PkDecryption {
_cls: &Bound<'_, PyType>,
key: Curve25519SecretKey,
) -> Result<Self, PkEncryptionError> {
Ok(Self {
inner: vodozemac::pk_encryption::PkDecryption::from_key(key.inner),
})
Ok(Self { inner: vodozemac::pk_encryption::PkDecryption::from_key(key.inner) })
}

/// The secret key used to decrypt messages.
Expand Down Expand Up @@ -89,7 +85,7 @@ impl PkDecryption {

self.inner
.decrypt(&message)
.map(|vec| Python::with_gil(|py| PyBytes::new_bound(py, vec.as_slice()).into()))
.map(|vec| Python::with_gil(|py| PyBytes::new(py, vec.as_slice()).into()))
.map_err(PkEncryptionError::Decode)
}
}
Expand Down Expand Up @@ -117,17 +113,15 @@ impl PkEncryption {
_cls: &Bound<'_, PyType>,
key: Curve25519PublicKey,
) -> Result<Self, PkEncryptionError> {
Ok(Self {
inner: vodozemac::pk_encryption::PkEncryption::from_key(key.inner),
})
Ok(Self { inner: vodozemac::pk_encryption::PkEncryption::from_key(key.inner) })
}

/// Encrypt a plaintext for the recipient. Writes to the ciphertext, mac, and
/// ephemeral_key buffers, whose values should be sent to the recipient. mac is
/// a Message Authentication Code to ensure that the data is received and
/// decrypted properly. ephemeral_key is the public part of the ephemeral key
/// used (together with the recipient's key) to generate a symmetric encryption
/// key.
/// Encrypt a plaintext for the recipient. Writes to the ciphertext, mac,
/// and ephemeral_key buffers, whose values should be sent to the
/// recipient. mac is a Message Authentication Code to ensure that the
/// data is received and decrypted properly. ephemeral_key is the public
/// part of the ephemeral key used (together with the recipient's key)
/// to generate a symmetric encryption key.
pub fn encrypt(&self, message: &[u8]) -> Message {
let msg = self.inner.encrypt(message);
Message {
Expand Down
5 changes: 1 addition & 4 deletions src/sas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ impl Sas {
let sas = vodozemac::sas::Sas::new();
let public_key = sas.public_key();

Self {
inner: Some(sas),
public_key,
}
Self { inner: Some(sas), public_key }
}

#[getter]
Expand Down
Loading

0 comments on commit 5892502

Please sign in to comment.