Skip to content

Add Python Attribute Accessors for PkEncryption#15

Merged
PaarthShah merged 16 commits intomatrix-nio:mainfrom
TrevisGordan:olm_compatibility
Jun 30, 2025
Merged

Add Python Attribute Accessors for PkEncryption#15
PaarthShah merged 16 commits intomatrix-nio:mainfrom
TrevisGordan:olm_compatibility

Conversation

@TrevisGordan
Copy link
Contributor

@TrevisGordan TrevisGordan commented Dec 10, 2024

This PR adds missing Python attribute accessors and improves compatibility with python-olm by enabling cross-library encryption and decryption tests.

Details:

  • Adds #[pyo3(get)] annotations to the Message struct fields (ciphertext, mac, ephemeral_key), making these attributes accessible from Python.
  • Implements a Message constructor in Rust (via #[new]), allowing Python code to create Message instances directly.
  • Introduces tests to verify that attribute access works as intended.
  • Adds integration tests to ensure PkEncryption is compatible with python-olm. Specifically:
    • Encrypt with Olm and decrypt with Vodozemac.
    • Encrypt with Vodozemac and decrypt with Olm.

Next Steps:

To achieve seamless compatibility, the user will need to Base64-encode Vodozemac message attributes (ciphertext, mac, ephemeral_key) before passing them to Olm. For example:

vodo_encryption = PkEncryption.from_key(public_key)
vodo_message = vodo_encryption.encrypt(PLAINTEXT).encode())

vodo_mac = base64.b64encode(vodo_message.mac).decode('ascii')
vodo_ciphertext = base64.b64encode(vodo_message.ciphertext).decode('ascii')
vodo_ephemeral_key = base64.b64encode(vodo_message.ephemeral_key).decode('ascii')

A future enhancement could add a Rust helper function to handle this encoding automatically, such as:

(ciphertext, mac, ephemeral_key) = encryption.encrypt(json.dumps(media_body).encode()).to_base64()

Also unpad_base64_encode and pad_base64_decode functions.
This would streamline the process and reduce the amount of Python-side encoding logic required.

Adds python-olm Dependencie (at 3.2.16).
Adds PkEncryption (python-olm) compatibility tests.
Copy link
Collaborator

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for writing this and sorry for the delay here. I left some small nits.

Copy link
Collaborator

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some two minor things left to do, then I think we can merge.

Copy link
Collaborator

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I think this is good to go functionality-wise.

Could you just fix the formatting you'll have to use rustmfmt nightly for this:

$ cargo +nightly fmt

@TrevisGordan
Copy link
Contributor Author

TrevisGordan commented Mar 11, 2025

Hey @poljar

The CI/CD Error appears to be a known issue – libolm cannot be built on Windows easily (see matrix-org/olm#25). Given its deprecation, I don’t expect Windows support to ever be added.

Skipping all Windows tests might be overkill. Instead, we could skip the libolm tests via pytest options and ignore them to avoid import errors. One option is to create a separate requirements file (e.g. requirements-dev-win.txt); another option is to dynamically modify the requirements-dev.txt file in the noxfile by removing the libolm dependency (a bit hacky tho).

For example:

import nox
import os

@nox.session(python=["3.9", "3.10", "3.11", "3.12"])
def test(session):
    if os.name == 'nt':  # Check if the OS is Windows
        session.log("Skipping libolm tests on Windows due to known issues.")
        # Option 1: Use a separate requirements file (requirements-dev-win.txt)
        # Option 2: Dynamically remove the "python-olm==3.2.16" dependency from requirements-dev.txt
        session.install("-r requirements-dev-win.txt")
        session.install("-e", ".", "--no-build-isolation")
        session.run("pytest --ignore=tests/pk_encryption_test.py")
        return

    session.install("-r requirements-dev.txt")
    session.install("-e", ".", "--no-build-isolation")
    session.run("pytest")

What do you think? Or you may even have a better idea?

@guywithface
Copy link

I would add

python-olm==3.2.16; sys_platform != 'win32'  # Windows not supported

to the requirements file.
Then in the tests I would do something like

try:
    import olm
except ImportError:
    olm = None
...
# For tests that require olm
@pytest.skipif(olm is None, reason="python-olm is not installed")
def test_olm_encrypt_vodo_decrypt...

@TrevisGordan
Copy link
Contributor Author

Hey @guywithface,
thanks - this too could work (not as hacky!).
@poljar - what do you think?
→ could we please re-run the Pipeline? - the logs have expired.

@PaarthShah
Copy link
Collaborator

@TrevisGordan I'm going to start handling this; feel free to message me directly as-desired

@PaarthShah PaarthShah changed the title Add Python Attribute Accessors and Cross-Library Compatibility for PkEncryption Add Python Attribute Accessors for PkEncryption Jun 30, 2025
Copy link
Collaborator

@PaarthShah PaarthShah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to make a decisive stance; given that python-olm no longer compiles even on linux (see matrix-nio/matrix-nio#541 (comment)) (nor macos and windows), and given the official stance that the library is deprecated, I'll go ahead and say that compatibility between the libraries is no longer a useful goal of vodozemac-python (though it was a very noble effort and it would've been cool if it worked).

As such, I think we can get away with removing those tests outright. I've pushed changes to the branch to this effect; if you and @poljar are good for it, then so am I. But I can't get python-olm to install anymore, and I'm really over it

@poljar
Copy link
Collaborator

poljar commented Jun 30, 2025

I've pushed changes to the branch to this effect; if you and @poljar are good for it, then so am I. But I can't get python-olm to install anymore, and I'm really over it

Sounds sensible to me.

@TrevisGordan
Copy link
Contributor Author

LGTM Thanks Guys!

@PaarthShah PaarthShah merged commit f9ebdfe into matrix-nio:main Jun 30, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants