diff --git a/pisa/appointment.py b/common/appointment.py similarity index 86% rename from pisa/appointment.py rename to common/appointment.py index b4b9c0f..3b31365 100644 --- a/pisa/appointment.py +++ b/common/appointment.py @@ -1,4 +1,6 @@ import json +import struct +from binascii import unhexlify from pisa.encrypted_blob import EncryptedBlob @@ -101,3 +103,23 @@ class Appointment: appointment["triggered"] = triggered return json.dumps(appointment, sort_keys=True, separators=(",", ":")) + + def serialize(self): + """ + Serializes an appointment to be signed. + + The serialization follows the same ordering as the fields in the appointment: + locator:start_time:end_time:to_self_delay:encrypted_blob + + All values are big endian. + + Returns: + :mod:`bytes`: The serialized data to be signed. + """ + return ( + unhexlify(self.locator) + + struct.pack(">I", self.start_time) + + struct.pack(">I", self.end_time) + + struct.pack(">I", self.to_self_delay) + + unhexlify(self.encrypted_blob.data) + ) diff --git a/common/cryptographer.py b/common/cryptographer.py index 1cff63f..319d7f1 100644 --- a/common/cryptographer.py +++ b/common/cryptographer.py @@ -1,4 +1,3 @@ -import json from hashlib import sha256 from binascii import unhexlify, hexlify @@ -146,23 +145,6 @@ class Cryptographer: return blob - # NOTCOVERED - @staticmethod - def signature_format(data): - """ - Serializes a given ``data`` in the right format to be signed. - - Args: - data(:mod:`str`): the data to be formatted. - - Returns: - :mod:`str`: The serialized data to be signed. - """ - - # FIXME: This is temporary serialization. A proper one is required. Data need to be unhexlified too (can't atm) - return json.dumps(data, sort_keys=True, separators=(",", ":")).encode("utf-8") - - # Deserialize public key from der data. @staticmethod def load_public_key_der(pk_der): """ @@ -195,7 +177,6 @@ class Cryptographer: return None - # Deserialize private key from der data. @staticmethod def load_private_key_der(sk_der): """ @@ -231,7 +212,7 @@ class Cryptographer: Signs a given data using a given secret key using ECDSA. Args: - data(:mod:`str`): the data to be signed. + data(:mod:`bytes`): the data to be signed. sk(:mod:`EllipticCurvePrivateKey`): the ECDSA secret key used to signed the data. rtype: the return type for the encrypted value. Can be either ``'str'`` or ``'bytes'``. @@ -263,7 +244,7 @@ class Cryptographer: Verifies if a signature is valid for a given public key and message. Args: - message(:mod:`str`): the message that is supposed have been signed. + message(:mod:`bytes`): the message that is supposed have been signed. signature(:mod:`str`): the potential signature of the message. pk(:mod:`EllipticCurvePublicKey`): the public key that is used to try to verify the signature. diff --git a/test/pisa/unit/test_appointment.py b/test/common/unit/test_appointment.py similarity index 98% rename from test/pisa/unit/test_appointment.py rename to test/common/unit/test_appointment.py index 8af351e..d562667 100644 --- a/test/pisa/unit/test_appointment.py +++ b/test/common/unit/test_appointment.py @@ -2,7 +2,7 @@ import json from pytest import fixture from pisa import c_logger -from pisa.appointment import Appointment +from common.appointment import Appointment from pisa.encrypted_blob import EncryptedBlob from test.pisa.unit.conftest import get_random_value_hex