mirror of
https://github.com/morgan9e/noiseprotocol
synced 2026-04-14 00:14:05 +09:00
* 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
36 lines
1001 B
Python
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')
|