Skip to main content

Rust extension providing Python bindings to the signal protocol

Project description

signal-protocol

CircleCI PyPI version

Experimental Python bindings to Rust signal protocol implementation libsignal-client. This project provides a Rust extension using PyO3 to define a signal_protocol Python module. See here for a fundamental limitation storing secrets in Python-allocated memory.

⚠️USE AT YOUR OWN RISK!⚠️

Installation

To use the wheel distributions you do not need the Rust toolchain installed. Simply run

pip install signal-protocol

Usage

Initial client setup

The following shows how to use this library to initialize a new Signal client. This is the first step that must be completed before the protocol can begin.

For an overview of the Signal protocol, see this blog post. Detailed specifications are available from Signal.

First, import these modules:

from signal_protocol import curve, identity_key, state, storage

Each client must generate a long-term identity key pair. This should be stored somewhere safe and persistent.

identity_key_pair = identity_key.IdentityKeyPair.generate()

Clients must generate prekeys. The example generates a single prekey. In practice, clients will generate many prekeys, as they are one-time use and consumed when a message from a new chat participant is sent.

pre_key_pair = curve.KeyPair.generate()

Clients must generate a registration_id and store it somewhere safe and persistent.

registration_id = 12  # TODO generate (not yet supported in upstream crate)

The InMemSignalProtocolStore is a single object which provide the four storage interfaces required: IdentityKeyStore (for one's own identity key state and the (public) identity keys for other chat participants), PreKeyStore (for one's own prekey state), SignedPreKeyStore (for one's own signed prekeys), and SessionStore (for established sessions with chat participants).

store = storage.InMemSignalProtocolStore(identity_key_pair, registration_id)

Clients should also generate a signed prekey.

signed_pre_key_pair = curve.KeyPair.generate()
serialized_signed_pre_pub_key = signed_pre_key_pair.public_key().serialize()
signed_pre_key_signature = (
    store.get_identity_key_pair()
    .private_key()
    .calculate_signature(serialized_signed_pre_pub_key)
)

Clients should store their prekeys (both one-time and signed) in the protocol store along with IDs that can be used to retrieve them later.

pre_key_id = 10
pre_key_record = state.PreKeyRecord(pre_key_id, pre_key_pair)
store.save_pre_key(pre_key_id, pre_key_record)

signed_pre_key_id = 33
signed_prekey = state.SignedPreKeyRecord(
            signed_pre_key_id,
            42, # This is a timestamp since this key should be periodically rotated
            signed_pre_key_pair,
            signed_pre_key_signature,
        )
store.save_signed_pre_key(signed_pre_key_id, signed_prekey)

Sending a message to a new participant

With a client initialized, you can create a session and send messages.

To create a session, you must fetch a prekey bundle for the recipient from the server. Here the prekey bundle is recipient_bundle for participant recipient_address.

from signal_protocol import session, session_cipher

session.process_prekey_bundle(
    recipient_address,
    store,
    recipient_bundle,
)

Once the prekey bundle is processed (storing data from the recipient in your local protocol store), you can encrypt messages:

ciphertext = session_cipher.message_encrypt(store, recipient_address, b"hello")

Developer Getting Started

You will need both Rust and Python 3.7+ installed on your system. To install the project in your virtualenv:

pip install -r requirements.txt
python setup.py develop

Then run the tests via pytest -v tests/ to confirm all is working. Tests are ported to Python from the upstream crate. You can use the tests as a reference for how to use the library.

When developing, simply run python setup.py develop as you make changes to rebuild the library. This script will handle compilation on the Rust side.

Building wheels

See instructions here. In brief:

docker pull quay.io/pypa/manylinux2014_x86_64
docker run --rm -v `pwd`:/io quay.io/pypa/manylinux2014_x86_64 /io/build-wheels.sh

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

signal-protocol-0.2.2.tar.gz (14.0 kB view details)

Uploaded Source

Built Distributions

signal_protocol-0.2.2-cp37-cp37m-macosx_10_14_x86_64.whl (8.2 MB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

File details

Details for the file signal-protocol-0.2.2.tar.gz.

File metadata

  • Download URL: signal-protocol-0.2.2.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.6

File hashes

Hashes for signal-protocol-0.2.2.tar.gz
Algorithm Hash digest
SHA256 8e20c140dd240c9b60414b44898592b64d60be428991e1e060fa88969d0ee362
MD5 d795bc385f9279cf37dc05cb23b47ff5
BLAKE2b-256 bed21d4814839b4652d2eafbd6d095e8474e58ed825987ebbb1bcab316668a98

See more details on using hashes here.

File details

Details for the file signal_protocol-0.2.2-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: signal_protocol-0.2.2-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 8.4 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.6

File hashes

Hashes for signal_protocol-0.2.2-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f12565e2e418770e136d33ccafdb244d3e9565a4910563d7cadb28d25caac47e
MD5 48f429181bea0321d18cbd6bd8ff7925
BLAKE2b-256 9ac23d97a581e19279b3abd38eb2c5ae5ac034050e21597563ef6a576d9cbe76

See more details on using hashes here.

File details

Details for the file signal_protocol-0.2.2-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: signal_protocol-0.2.2-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 8.3 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.6

File hashes

Hashes for signal_protocol-0.2.2-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b22aa4f0c41db62dc2883b29d4d9aa64c24bc6284ea034ee0d85ea280c28faef
MD5 6dc81127e3366802bcdebf24014eb8ec
BLAKE2b-256 59140f183d5961c9fdc301840eb7163fd3124ec73d4c0d74ef7f4557d457964e

See more details on using hashes here.

File details

Details for the file signal_protocol-0.2.2-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for signal_protocol-0.2.2-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9b64b02e7695f5cf6132d3107bea0abb74e0df17dcba735513c58d6a159ebd71
MD5 816bf3ea26c4e925d665218a44478d55
BLAKE2b-256 daccd77bec014b3a7e09773a7f624ed24e22d0f194adcab8ec2890f7747be88c

See more details on using hashes here.

File details

Details for the file signal_protocol-0.2.2-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: signal_protocol-0.2.2-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 8.2 MB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.7.6

File hashes

Hashes for signal_protocol-0.2.2-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 29b2e6dcb7957f6eeeea7c07b794449f866ccce64533f7a54f9f901c2a0f876d
MD5 7cd3140e660681d3e0676665c2777541
BLAKE2b-256 73bc60061cee32a939cc36c46f6e4a0badda08ca26c4220666f86766989b9b5e

See more details on using hashes here.

File details

Details for the file signal_protocol-0.2.2-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for signal_protocol-0.2.2-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 edb9145952201603c337eaed5c06a7f50f08e5d2734178eaed0c31bc742dd67c
MD5 f60fcd3f9d1af1c3780e4bd12acf7904
BLAKE2b-256 20d2cc549e9ade8c412ad7b984c3166d3b53a7d1b000a62c744ec47020297b24

See more details on using hashes here.

File details

Details for the file signal_protocol-0.2.2-cp35-cp35m-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for signal_protocol-0.2.2-cp35-cp35m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 77c13323ddf9247b88590837f93e55e4994f8328c2742fd4c09d8e09250a372b
MD5 fd24eda609a1ed05d08fec2536af2124
BLAKE2b-256 e6de0e0cd66cd5d2955cb7f63983f38f3c2fa397e1be8cdd56be63f38a9e54c5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page