Files
noiseprotocol/noise/backends/default/ciphers.py
Piotr Lizończyk e84db3c232 Added possibilty to use different crypto backends.
* Created NoiseBackend class serving as a base for backends

* Refactored NoiseProtocol name parsing

* Refactored existing spec-defined functions into abstract classes.
Implementing classes are connecting crypto primitives to expected
interfaces.

* Refactored existing usage of Cryptography as source of crypto into
"default" backend (along with in-house implementation of X448).

* Provisioned "experimental" backend, it will contain e.g. non-default
crypto algorithms

* Backend can be chosen while creating NoiseConnection, though by
default, the Cryptography backend ("default") is used

Closes #7
2018-07-16 01:47:29 +02:00

36 lines
1001 B
Python

import abc
from cryptography.hazmat.primitives.ciphers.aead import AESGCM, ChaCha20Poly1305
from noise.functions.cipher import Cipher
class CryptographyCipher(Cipher, metaclass=abc.ABCMeta):
def encrypt(self, k, n, ad, plaintext):
return self.cipher.encrypt(nonce=self.format_nonce(n), data=plaintext, associated_data=ad)
def decrypt(self, k, n, ad, ciphertext):
return self.cipher.decrypt(nonce=self.format_nonce(n), data=ciphertext, associated_data=ad)
@abc.abstractmethod
def format_nonce(self, n):
raise NotImplementedError
class AESGCMCipher(CryptographyCipher):
@property
def klass(self):
return AESGCM
def format_nonce(self, n):
return b'\x00\x00\x00\x00' + n.to_bytes(length=8, byteorder='big')
class ChaCha20Cipher(CryptographyCipher):
@property
def klass(self):
return ChaCha20Poly1305
def format_nonce(self, n):
return b'\x00\x00\x00\x00' + n.to_bytes(length=8, byteorder='little')