Skip to content

Commit

Permalink
Little cleanup release in preparation for the debain freeze.
Browse files Browse the repository at this point in the history
- Removed some unsafe debug code
- Cleaned up serialization a little
- Cleaned up dependencies a little
  • Loading branch information
Tim Henkes committed Dec 15, 2018
1 parent c6e9fad commit 1d74f13
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 80 deletions.
1 change: 1 addition & 0 deletions docs/x3dh/package.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ x3dh
Module: keypair <keypair>
Module: publicbundle <publicbundle>
Module: publickeyencoder <publickeyencoder>
Module: serializable <serializable>
Module: state <state>

Package: exceptions <exceptions/package>
Expand Down
9 changes: 9 additions & 0 deletions docs/x3dh/serializable.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
serializable
============

.. autoclass:: x3dh.Serializable
:members:
:special-members:
:member-order: bysource
:exclude-members: __dict__, __weakref__, __module__
:show-inheritance:
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
cryptography>=1.7.1
XEdDSA>=0.4.2
XEdDSA>=0.4.5,<0.5
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
author_email = "[email protected]",
license = "MIT",
packages = find_packages(),
install_requires = [ "cryptography>=1.7.1", "XEdDSA>=0.4.2" ],
install_requires = [ "cryptography>=1.7.1", "XEdDSA>=0.4.5,<0.5" ],
python_requires = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, <4",
zip_safe = False,
classifiers = [
Expand Down
1 change: 1 addition & 0 deletions x3dh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
from .keypair import KeyPair
from .publicbundle import PublicBundle
from .publickeyencoder import PublicKeyEncoder
from .serializable import Serializable
from .state import State
40 changes: 5 additions & 35 deletions x3dh/keypair.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class KeyPair(object):
from __future__ import absolute_import

from .serializable import Serializable

class KeyPair(Serializable):
"""
The interface of a key pair. A key pair is a pair consisting of a private and a public
key used for en- and decryption.
Expand All @@ -22,40 +26,6 @@ def generate(cls):

raise NotImplementedError

def serialize(self):
"""
:returns: A serializable Python structure, which contains all the state
information of this object.
Use together with the fromSerialized method.
Here, "serializable" means, that the structure consists of any combination of the
following types:
* dictionaries
* lists
* strings
* integers
* floats
* booleans
* None
"""

return None

@classmethod
def fromSerialized(cls, serialized, *args, **kwargs):
"""
:param serialized: A serializable Python object.
:returns: Return a new instance that was set to the state that was saved into the
serialized object.
Use together with the serialize method.
Notice: You have to pass all positional parameters required by the constructor of
the class you call fromSerialized on.
"""

return cls(*args, **kwargs)

@property
def priv(self):
"""
Expand Down
34 changes: 34 additions & 0 deletions x3dh/serializable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Serializable(object):
def serialize(self):
"""
:returns: A serializable Python structure, which contains all the state
information of this object.
Use together with the fromSerialized method.
Here, "serializable" means, that the structure consists of any combination of the
following types:
* dictionaries
* lists
* strings
* integers
* floats
* booleans
* None
"""

return None

@classmethod
def fromSerialized(cls, serialized, *args, **kwargs):
"""
:param serialized: A serializable Python object.
:returns: Return a new instance that was set to the state that was saved into the
serialized object.
Use together with the serialize method.
Notice: You have to pass all positional parameters required by the constructor of
the class you call fromSerialized on.
"""

return cls(*args, **kwargs)
48 changes: 6 additions & 42 deletions x3dh/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .exceptions import KeyExchangeException
from .implementations import KeyPairCurve25519
from .publicbundle import PublicBundle
from .serializable import Serializable

from xeddsa.implementations import XEdDSA25519

Expand All @@ -22,7 +23,7 @@ def _changes(*args, **kwargs):
return f(*args, **kwargs)
return _changes

class State(object):
class State(Serializable):
"""
The state is the core of the X3DH protocol. It manages a collection of key pairs and
signatures and offers methods to do key exchanges with other parties.
Expand Down Expand Up @@ -99,23 +100,6 @@ def __init__(
#################

def serialize(self):
"""
:returns: A serializable Python structure, which contains all the state
information of this object.
Use together with the fromSerialized method.
Here, "serializable" means, that the structure consists of any combination of the
following types:
* dictionaries
* lists
* strings
* integers
* floats
* booleans
* None
"""

spk = {
"key" : self.__spk["key"].serialize(),
"signature" : base64.b64encode(self.__spk["signature"]).decode("US-ASCII"),
Expand All @@ -126,22 +110,12 @@ def serialize(self):
"changed" : self._changed,
"ik" : self.__ik.serialize(),
"spk" : spk,
"otpks" : [ x.serialize() for x in self.__otpks ],
"hidden_otpks" : [ x.serialize() for x in self.__hidden_otpks ]
"otpks" : [ otpk.serialize() for otpk in self.__otpks ],
"hidden_otpks" : [ otpk.serialize() for otpk in self.__hidden_otpks ]
}

@classmethod
def fromSerialized(cls, serialized, *args, **kwargs):
"""
:param serialized: A serializable Python object.
:returns: Return a new instance that was set to the state that was saved into the
serialized object.
Use together with the serialize method.
Notice: You have to pass all positional parameters required by the constructor of
the class you call fromSerialized on.
"""

self = cls(*args, **kwargs)

parseKeyPair = self.__KeyPair.fromSerialized
Expand Down Expand Up @@ -355,8 +329,7 @@ def changed(self):
def getSharedSecretActive(
self,
other_public_bundle,
allow_zero_otpks = False,
_DEBUG_ek = None
allow_zero_otpks = False
):
"""
Do the key exchange, as the active party. This involves selecting keys from the
Expand Down Expand Up @@ -425,16 +398,7 @@ def getSharedSecretActive(
"The signature of this public bundle's spk could not be verifified."
)

if _DEBUG_ek == None:
ek = self.__KeyPair.generate()
else:
import logging

logging.getLogger("x3dh.State").error(
"WARNING: RUNNING UNSAFE DEBUG-ONLY OPERATION"
)

ek = _DEBUG_ek
ek = self.__KeyPair.generate()

dh1 = self.__ik.getSharedSecret(other_spk["key"])
dh2 = ek.getSharedSecret(other_ik)
Expand Down
2 changes: 1 addition & 1 deletion x3dh/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.5"
__version__ = "0.5.6"

0 comments on commit 1d74f13

Please sign in to comment.