예제 #1
0
 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')
예제 #2
0
 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)
예제 #3
0
 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)
예제 #4
0
 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
예제 #5
0
 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)
예제 #6
0
 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())
예제 #7
0
 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))
예제 #8
0
 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))
예제 #9
0
 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)
예제 #10
0
 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)
예제 #11
0
 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())