diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..df7fffd --- /dev/null +++ b/.rustfmt.toml @@ -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" diff --git a/Cargo.toml b/Cargo.toml index d6676cf..c39f7a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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 = [] diff --git a/src/account.rs b/src/account.rs index 5eb6260..d2109c9 100644 --- a/src/account.rs +++ b/src/account.rs @@ -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, @@ -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] @@ -34,9 +31,8 @@ impl Account { pickle: &str, pickle_key: &[u8], ) -> Result { - 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)?; @@ -57,9 +53,8 @@ impl Account { } fn pickle(&self, pickle_key: &[u8]) -> Result { - 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)) } @@ -80,11 +75,7 @@ impl Account { #[getter] fn one_time_keys(&self) -> HashMap { - 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] @@ -98,11 +89,7 @@ impl Account { #[getter] fn fallback_key(&self) -> HashMap { - 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) { @@ -132,15 +119,8 @@ impl Account { identity_key: &Curve25519PublicKey, message: &PreKeyMessage, ) -> Result<(Session, Py), 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()))) } } diff --git a/src/error.rs b/src/error.rs index 6f64b5f..c91c256 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 { @@ -140,8 +128,8 @@ impl From 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")] @@ -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 for PyErr { diff --git a/src/group_sessions.rs b/src/group_sessions.rs index ce7390e..a239776 100644 --- a/src/group_sessions.rs +++ b/src/group_sessions.rs @@ -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] @@ -44,9 +42,8 @@ impl GroupSession { } fn pickle(&self, pickle_key: &[u8]) -> Result { - 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)) } @@ -57,9 +54,8 @@ impl GroupSession { pickle: &str, pickle_key: &[u8], ) -> Result { - 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); @@ -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 } } } @@ -135,16 +128,12 @@ impl InboundGroupSession { ) -> Result { 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 { - 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)) } @@ -155,9 +144,8 @@ impl InboundGroupSession { pickle: &str, pickle_key: &[u8], ) -> Result { - 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)?; diff --git a/src/lib.rs b/src/lib.rs index 556d01e..0894407 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,46 +33,22 @@ fn my_module(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; - m.add("KeyException", py.get_type_bound::())?; - m.add( - "SignatureException", - py.get_type_bound::(), - )?; - m.add("DecodeException", py.get_type_bound::())?; - m.add( - "LibolmPickleException", - py.get_type_bound::(), - )?; - m.add( - "SessionKeyDecodeException", - py.get_type_bound::(), - )?; - m.add("PickleException", py.get_type_bound::())?; - m.add( - "SessionCreationException", - py.get_type_bound::(), - )?; - m.add("SasException", py.get_type_bound::())?; - m.add( - "OlmDecryptionException", - py.get_type_bound::(), - )?; - m.add( - "MegolmDecryptionException", - py.get_type_bound::(), - )?; - m.add( - "PkInvalidKeySizeException", - py.get_type_bound::(), - )?; - m.add( - "PkDecodeException", - py.get_type_bound::(), - )?; + m.add("KeyException", py.get_type::())?; + m.add("SignatureException", py.get_type::())?; + m.add("DecodeException", py.get_type::())?; + m.add("LibolmPickleException", py.get_type::())?; + m.add("SessionKeyDecodeException", py.get_type::())?; + m.add("PickleException", py.get_type::())?; + m.add("SessionCreationException", py.get_type::())?; + m.add("SasException", py.get_type::())?; + m.add("OlmDecryptionException", py.get_type::())?; + m.add("MegolmDecryptionException", py.get_type::())?; + m.add("PkInvalidKeySizeException", py.get_type::())?; + m.add("PkDecodeException", py.get_type::())?; Ok(()) } pub(crate) fn convert_to_pybytes(bytes: &[u8]) -> Py { - Python::with_gil(|py| PyBytes::new_bound(py, bytes).into()) + Python::with_gil(|py| PyBytes::new(py, bytes).into()) } diff --git a/src/pk_encryption.rs b/src/pk_encryption.rs index a3640d0..f93f862 100644 --- a/src/pk_encryption.rs +++ b/src/pk_encryption.rs @@ -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. @@ -55,9 +53,7 @@ impl PkDecryption { _cls: &Bound<'_, PyType>, key: Curve25519SecretKey, ) -> Result { - 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. @@ -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) } } @@ -117,17 +113,15 @@ impl PkEncryption { _cls: &Bound<'_, PyType>, key: Curve25519PublicKey, ) -> Result { - 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 { diff --git a/src/sas.rs b/src/sas.rs index a06892a..86c8341 100644 --- a/src/sas.rs +++ b/src/sas.rs @@ -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] diff --git a/src/session.rs b/src/session.rs index c0df45e..d256db1 100644 --- a/src/session.rs +++ b/src/session.rs @@ -22,9 +22,8 @@ impl Session { } fn pickle(&self, pickle_key: &[u8]) -> Result { - 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)) } @@ -39,9 +38,8 @@ impl Session { pickle: &str, pickle_key: &[u8], ) -> Result { - 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::SessionPickle::from_encrypted(pickle, pickle_key)?; let session = vodozemac::olm::Session::from_pickle(pickle); @@ -66,8 +64,6 @@ impl Session { } fn decrypt(&mut self, message: &AnyOlmMessage) -> Result, SessionError> { - Ok(convert_to_pybytes( - self.inner.decrypt(&message.inner)?.as_slice(), - )) + Ok(convert_to_pybytes(self.inner.decrypt(&message.inner)?.as_slice())) } } diff --git a/src/types/curve25519.rs b/src/types/curve25519.rs index 5889fc8..8c11b08 100644 --- a/src/types/curve25519.rs +++ b/src/types/curve25519.rs @@ -1,10 +1,11 @@ -use crate::{convert_to_pybytes, error::*}; use pyo3::{ prelude::*, types::{PyBytes, PyType}, }; use vodozemac::{base64_decode, base64_encode}; +use crate::{convert_to_pybytes, error::*}; + #[pyclass] #[derive(Clone)] pub struct Curve25519PublicKey { @@ -21,9 +22,7 @@ impl From for Curve25519PublicKey { impl Curve25519PublicKey { #[classmethod] pub fn from_base64(_cls: &Bound<'_, PyType>, key: &str) -> Result { - Ok(Self { - inner: vodozemac::Curve25519PublicKey::from_base64(key)?, - }) + Ok(Self { inner: vodozemac::Curve25519PublicKey::from_base64(key)? }) } #[classmethod] @@ -36,9 +35,7 @@ impl Curve25519PublicKey { }) })?; - Ok(Self { - inner: vodozemac::Curve25519PublicKey::from_slice(key)?, - }) + Ok(Self { inner: vodozemac::Curve25519PublicKey::from_slice(key)? }) } pub fn to_base64(&self) -> String { @@ -75,9 +72,7 @@ impl Curve25519SecretKey { /// Generate a new, random, Curve25519SecretKey. #[new] fn new() -> Self { - Self { - inner: vodozemac::Curve25519SecretKey::new(), - } + Self { inner: vodozemac::Curve25519SecretKey::new() } } /// Create a `Curve25519SecretKey` from the given base64-encoded string. @@ -102,9 +97,7 @@ impl Curve25519SecretKey { }) })?; - Ok(Self { - inner: vodozemac::Curve25519SecretKey::from_slice(key), - }) + Ok(Self { inner: vodozemac::Curve25519SecretKey::from_slice(key) }) } /// Convert the `Curve25519SecretKey` to a base64-encoded string. @@ -117,10 +110,9 @@ impl Curve25519SecretKey { convert_to_pybytes(self.inner.to_bytes().as_slice()) } - /// Give the `Curve25519PublicKey` associated with this `Curve25519SecretKey`. + /// Give the `Curve25519PublicKey` associated with this + /// `Curve25519SecretKey`. pub fn public_key(&self) -> Curve25519PublicKey { - Curve25519PublicKey { - inner: vodozemac::Curve25519PublicKey::from(&self.inner), - } + Curve25519PublicKey { inner: vodozemac::Curve25519PublicKey::from(&self.inner) } } } diff --git a/src/types/ed25519.rs b/src/types/ed25519.rs index e0553eb..e822aae 100644 --- a/src/types/ed25519.rs +++ b/src/types/ed25519.rs @@ -1,6 +1,7 @@ -use crate::error::*; use pyo3::{prelude::*, types::PyType}; +use crate::error::*; + #[pyclass] pub struct Ed25519PublicKey { pub(crate) inner: vodozemac::Ed25519PublicKey, @@ -10,9 +11,7 @@ pub struct Ed25519PublicKey { impl Ed25519PublicKey { #[classmethod] pub fn from_base64(_cls: &Bound<'_, PyType>, key: &str) -> Result { - Ok(Self { - inner: vodozemac::Ed25519PublicKey::from_base64(key)?, - }) + Ok(Self { inner: vodozemac::Ed25519PublicKey::from_base64(key)? }) } pub fn to_base64(&self) -> String { @@ -55,9 +54,7 @@ impl Ed25519Signature { _cls: &Bound<'_, PyType>, session_key: &str, ) -> Result { - Ok(Self { - inner: vodozemac::Ed25519Signature::from_base64(session_key)?, - }) + Ok(Self { inner: vodozemac::Ed25519Signature::from_base64(session_key)? }) } pub fn to_base64(&self) -> String { diff --git a/src/types/messages.rs b/src/types/messages.rs index 475f553..fdf6691 100644 --- a/src/types/messages.rs +++ b/src/types/messages.rs @@ -1,9 +1,10 @@ -use crate::{convert_to_pybytes, error::*}; use pyo3::{ prelude::*, types::{PyBytes, PyType}, }; +use crate::{convert_to_pybytes, error::*}; + #[pyclass] pub struct AnyOlmMessage { pub(crate) inner: vodozemac::olm::OlmMessage, @@ -13,23 +14,17 @@ pub struct AnyOlmMessage { impl AnyOlmMessage { #[classmethod] pub fn pre_key(_cls: &Bound<'_, PyType>, message: &[u8]) -> Result { - Ok(Self { - inner: vodozemac::olm::PreKeyMessage::from_bytes(message)?.into(), - }) + Ok(Self { inner: vodozemac::olm::PreKeyMessage::from_bytes(message)?.into() }) } #[classmethod] pub fn normal(_cls: &Bound<'_, PyType>, message: &[u8]) -> Result { - Ok(Self { - inner: vodozemac::olm::Message::from_bytes(message)?.into(), - }) + Ok(Self { inner: vodozemac::olm::Message::from_bytes(message)?.into() }) } pub fn to_pre_key(&self) -> Option { if let vodozemac::olm::OlmMessage::PreKey(message) = &self.inner { - Some(PreKeyMessage { - inner: message.clone(), - }) + Some(PreKeyMessage { inner: message.clone() }) } else { None } @@ -41,9 +36,7 @@ impl AnyOlmMessage { message_type: usize, ciphertext: &[u8], ) -> Result { - Ok(Self { - inner: vodozemac::olm::OlmMessage::from_parts(message_type, ciphertext)?, - }) + Ok(Self { inner: vodozemac::olm::OlmMessage::from_parts(message_type, ciphertext)? }) } pub fn to_parts(&self) -> (usize, Py) { @@ -61,15 +54,11 @@ pub struct PreKeyMessage { impl PreKeyMessage { #[classmethod] pub fn from_base64(_cls: &Bound<'_, PyType>, message: &str) -> Result { - Ok(Self { - inner: vodozemac::olm::PreKeyMessage::from_base64(message)?, - }) + Ok(Self { inner: vodozemac::olm::PreKeyMessage::from_base64(message)? }) } pub fn to_any(&self) -> AnyOlmMessage { - AnyOlmMessage { - inner: vodozemac::olm::OlmMessage::PreKey(self.inner.clone()), - } + AnyOlmMessage { inner: vodozemac::olm::OlmMessage::PreKey(self.inner.clone()) } } pub fn session_id(&self) -> String { @@ -79,8 +68,6 @@ impl PreKeyMessage { impl From for AnyOlmMessage { fn from(value: PreKeyMessage) -> Self { - Self { - inner: vodozemac::olm::OlmMessage::PreKey(value.inner.clone()), - } + Self { inner: vodozemac::olm::OlmMessage::PreKey(value.inner.clone()) } } } diff --git a/src/types/session_keys.rs b/src/types/session_keys.rs index 9ec8786..662c262 100644 --- a/src/types/session_keys.rs +++ b/src/types/session_keys.rs @@ -1,6 +1,7 @@ -use crate::error::*; use pyo3::prelude::*; +use crate::error::*; + #[pyclass] pub struct SessionKey { pub(crate) inner: vodozemac::megolm::SessionKey, @@ -10,9 +11,7 @@ pub struct SessionKey { impl SessionKey { #[new] pub fn from_base64(session_key: &str) -> Result { - Ok(Self { - inner: vodozemac::megolm::SessionKey::from_base64(session_key)?, - }) + Ok(Self { inner: vodozemac::megolm::SessionKey::from_base64(session_key)? }) } pub fn to_base64(&self) -> String { @@ -35,9 +34,7 @@ pub struct ExportedSessionKey { impl ExportedSessionKey { #[new] pub fn from_base64(session_key: &str) -> Result { - Ok(Self { - inner: vodozemac::megolm::ExportedSessionKey::from_base64(session_key)?, - }) + Ok(Self { inner: vodozemac::megolm::ExportedSessionKey::from_base64(session_key)? }) } pub fn to_base64(&self) -> String {