def set_prologue(self, prologue: Union[bytes, str]): if isinstance(prologue, bytes): self.noise_protocol.prologue = prologue elif isinstance(prologue, str): try: self.noise_protocol.prologue = prologue.encode('ascii') except UnicodeEncodeError: raise NoiseValueError('Prologue must be ASCII string or bytes') else: raise NoiseValueError('Prologue must be ASCII string or bytes')
def from_private_bytes(cls, private_bytes): if len(private_bytes) != 56: raise NoiseValueError( 'Invalid length of private_bytes! Should be 56') private = private_bytes public = X448.mul_5(private) return cls(private=private, public=public, public_bytes=public)
def dh(self, private_key, public_key) -> bytes: if not isinstance(private_key, x448.X448PrivateKey) or not isinstance( public_key, x448.X448PublicKey): raise NoiseValueError( 'Invalid keys! Must be x448.X448PrivateKey and x448.X448PublicKey instances' ) return private_key.exchange(public_key)
def from_name(cls, name: Union[str, bytes]): instance = cls() # Forgiving passing string. Bytes are good too, anything else will fail inside NoiseProtocol try: instance.protocol_name = name.encode('ascii') if isinstance(name, str) else name except ValueError: raise NoiseValueError('If passing string as protocol name, it must contain only ASCII characters') instance.noise_protocol = NoiseProtocol(protocol_name=name) return instance
def _25519_dh(self, private_key: 'x25519.X25519PrivateKey', public_key: 'x25519.X25519PublicKey') -> bytes: if not isinstance(private_key, x25519.X25519PrivateKey) or not isinstance( public_key, x25519.X25519PublicKey): raise NoiseValueError( 'Invalid keys! Must be x25519.X25519PrivateKey and x25519.X25519PublicKey instances' ) return private_key.exchange(public_key)
def from_private_bytes(cls, private_bytes): if len(private_bytes) != 32: raise NoiseValueError( 'Invalid length of private_bytes! Should be 32') private = x25519.X25519PrivateKey._from_private_bytes(private_bytes) public = private.public_key() return cls(private=private, public=public, public_bytes=public.public_bytes())
def from_private_bytes(cls, private_bytes): if len(private_bytes) != 32: raise NoiseValueError('Invalid length of private_bytes! Should be 32') private = x25519.X25519PrivateKey.from_private_bytes(private_bytes) public = private.public_key() return cls(private=private, public=public, public_bytes=public.public_bytes(encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw))
def from_public_bytes(cls, public_bytes): if len(public_bytes) != 56: raise NoiseValueError('Invalid length of private_bytes! Should be 56') public = x448.X448PublicKey.from_public_bytes(public_bytes) return cls(public=public, public_bytes=public.public_bytes(encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw))
def _448_dh(self, private_key: bytes, public_key: bytes) -> bytes: if len(private_key) != self.dhlen or len(public_key) != self.dhlen: raise NoiseValueError( 'Invalid length of keys! Should be {}'.format(self.dhlen)) return X448.mul(private_key, public_key)
def from_public_bytes(cls, public_bytes): if len(public_bytes) != 56: raise NoiseValueError( 'Invalid length of private_bytes! Should be 56') return cls(public=public_bytes, public_bytes=public_bytes)
def from_public_bytes(cls, public_bytes): if len(public_bytes) != 32: raise NoiseValueError( 'Invalid length of public_bytes! Should be 32') public = x25519.X25519PublicKey.from_public_bytes(public_bytes) return cls(public=public, public_bytes=public.public_bytes())