Commit f160d693 authored by poljar's avatar poljar Committed by Hubert Chathi

python: Add PK bindings.

This patch adds bindings to the PK part of the Olm library contained in
the pk.h header file.

Encryption, decryption as well as pickling/unpickling of the decryption
object is supported.
Signed-off-by: poljar's avatarDamir Jelić <[email protected]>
parent 0883a922
all: olm-python2 olm-python3
include/olm/olm.h: ../include/olm/olm.h ../include/olm/inbound_group_session.h ../include/olm/outbound_group_session.h
OLM_HEADERS = ../include/olm/olm.h ../include/olm/inbound_group_session.h \
../include/olm/outbound_group_session.h \
include/olm/olm.h: $(OLM_HEADERS)
mkdir -p include/olm
$(CPP) -I dummy -I ../include ../include/olm/olm.h -o include/olm/olm.h
# add memset to the header so that we can use it to clear buffers
echo 'void *memset(void *s, int c, size_t n);' >> include/olm/olm.h
olm-python2: include/olm/olm.h
include/olm/pk.h: include/olm/olm.h ../include/olm/pk.h
$(CPP) -I dummy -I ../include ../include/olm/pk.h -o include/olm/pk.h
olm-python2: include/olm/olm.h include/olm/pk.h
DEVELOP=$(DEVELOP) python2 setup.py build
olm-python3: include/olm/olm.h
olm-python3: include/olm/olm.h include/olm/pk.h
DEVELOP=$(DEVELOP) python3 setup.py build
install: install-python2 install-python3
......
......@@ -36,3 +36,10 @@ from .group_session import (
OutboundGroupSession,
OlmGroupSessionError
)
from .pk import (
PkMessage,
PkEncryption,
PkDecryption,
PkEncryptionError,
PkDecryptionError
)
This diff is collapsed.
......@@ -39,6 +39,7 @@ ffibuilder.set_source(
#include <olm/olm.h>
#include <olm/inbound_group_session.h>
#include <olm/outbound_group_session.h>
#include <olm/pk.h>
""",
libraries=["olm"],
extra_compile_args=compile_args,
......@@ -47,5 +48,8 @@ ffibuilder.set_source(
with open(os.path.join(PATH, "include/olm/olm.h")) as f:
ffibuilder.cdef(f.read(), override=True)
with open(os.path.join(PATH, "include/olm/pk.h")) as f:
ffibuilder.cdef(f.read(), override=True)
if __name__ == "__main__":
ffibuilder.compile(verbose=True)
import pytest
from olm import PkDecryption, PkDecryptionError, PkEncryption
class TestClass(object):
def test_invalid_encryption(self):
with pytest.raises(ValueError):
PkEncryption("")
def test_decrytion(self):
decryption = PkDecryption()
encryption = PkEncryption(decryption.public_key)
plaintext = "It's a secret to everybody."
message = encryption.encrypt(plaintext)
decrypted_plaintext = decryption.decrypt(message)
isinstance(decrypted_plaintext, str)
assert plaintext == decrypted_plaintext
def test_invalid_decrytion(self):
decryption = PkDecryption()
encryption = PkEncryption(decryption.public_key)
plaintext = "It's a secret to everybody."
message = encryption.encrypt(plaintext)
message.ephemeral_key = "?"
with pytest.raises(PkDecryptionError):
decryption.decrypt(message)
def test_pickling(self):
decryption = PkDecryption()
encryption = PkEncryption(decryption.public_key)
plaintext = "It's a secret to everybody."
message = encryption.encrypt(plaintext)
pickle = decryption.pickle()
unpickled = PkDecryption.from_pickle(pickle)
decrypted_plaintext = unpickled.decrypt(message)
assert plaintext == decrypted_plaintext
def test_invalid_unpickling(self):
with pytest.raises(ValueError):
PkDecryption.from_pickle("")
def test_invalid_pass_pickling(self):
decryption = PkDecryption()
pickle = decryption.pickle("Secret")
with pytest.raises(PkDecryptionError):
PkDecryption.from_pickle(pickle, "Not secret")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment