Beispiel #1
0
    def __init__(self,
                 peer_key: x25519.X25519PublicKey,
                 my_private_key: x25519.X25519PrivateKey = None,
                 min_peer_tag_len: int = MAX_TAG_LEN):
        """Initialize Glome class.

        Performs the handshake and generates keys.

        Args:
            peer_key: Your peer's public key.
            my_private_key: Your private key.
            min_peer_tag_len: Desired length (in bytes) for the tag.
              Must be an integer in range 1-32.
        Raises:
            ValueError: Raised whenever min_peer_tag_len is not in range 1-32.
            ExchangeError: Raised whenever null shared secret is derived from
              user/peer key pair.
        """

        if my_private_key is None:
            my_private_key, my_public_key = generate_keys()
        else:
            my_public_key = my_private_key.public_key()

        if not Glome.MIN_TAG_LEN < min_peer_tag_len <= Glome.MAX_TAG_LEN:
            raise ValueError('min_peer_tag_len must be in range {}-{}'.format(
                Glome.MIN_TAG_LEN, Glome.MAX_TAG_LEN))

        try:
            shared_secret = my_private_key.exchange(peer_key)
        except ValueError as value_error:
            raise ExchangeError(
                'Failed to deduce shared secret') from value_error

        self._send_key = shared_secret + _public_key_encode(
            peer_key) + _public_key_encode(my_public_key)
        self._receive_key = shared_secret + _public_key_encode(
            my_public_key) + _public_key_encode(peer_key)
        self._peer_key = peer_key
        self._my_keys = KeyPair(my_private_key, my_public_key)
        self._min_peer_tag_len = min_peer_tag_len
Beispiel #2
0
def encode_publickey(key: x25519.X25519PrivateKey) -> str:
    return b64encode(key.public_key().public_bytes(
        encoding=serialization.Encoding.Raw,
        format=serialization.PublicFormat.Raw)).decode("ascii")