From 8cddef8ae5d099a798cd9a6113cef6724c50789e Mon Sep 17 00:00:00 2001 From: Piotr Lizonczyk Date: Tue, 9 May 2017 19:04:13 +0200 Subject: [PATCH] Moving echo-script related code into suitable directory Adding Empty class and switching some Nones to it where required by specification Some additions to CipherState --- noise/constants.py | 51 +------------------ noise/echo/__init__.py | 0 .../client_script.py} | 8 +-- noise/echo/echo_constants.py | 49 ++++++++++++++++++ noise/state.py | 25 +++++---- 5 files changed, 68 insertions(+), 65 deletions(-) create mode 100644 noise/echo/__init__.py rename noise/{client-script.py => echo/client_script.py} (94%) create mode 100644 noise/echo/echo_constants.py diff --git a/noise/constants.py b/noise/constants.py index 14514de..b6da454 100644 --- a/noise/constants.py +++ b/noise/constants.py @@ -1,49 +1,2 @@ -from enum import Enum - - -class PSK(Enum): - PSK_DISABLED = 0x00 - PSK_ENABLED = 0x01 - - -class PATTERN(Enum): - PATTERN_NN = 0x00 - PATTERN_KN = 0x01 - PATTERN_NK = 0x02 - PATTERN_KK = 0x03 - PATTERN_NX = 0x04 - PATTERN_KX = 0x05 - PATTERN_XN = 0x06 - PATTERN_IN = 0x07 - PATTERN_XK = 0x08 - PATTERN_IK = 0x09 - PATTERN_XX = 0x0A - PATTERN_IX = 0x0B - PATTERN_HFS = 0x80 - - -class CIPHER(Enum): - CIPHER_CHACHAPOLY = 0x00 - CIPHER_AESGCM = 0x01 - - -class DH(Enum): - DH_25519 = 0x00 - DH_448 = 0x01 - DH_NEWHOPE = 0x02 - DH_MASK = 0x0F - - -class HYBRID(Enum): - HYBRID_NONE = 0x00 - HYBRID_25519 = 0x10 - HYBRID_448 = 0x20 - HYBRID_NEWHOPE = 0x30 - HYBRID_MASK = 0xF0 - - -class HASH(Enum): - HASH_SHA256 = 0x00 - HASH_SHA512 = 0x01 - HASH_BLAKE2s = 0x02 - HASH_BLAKE2b = 0x03 +class Empty: + pass diff --git a/noise/echo/__init__.py b/noise/echo/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/noise/client-script.py b/noise/echo/client_script.py similarity index 94% rename from noise/client-script.py rename to noise/echo/client_script.py index ca90767..5e8c11b 100755 --- a/noise/client-script.py +++ b/noise/echo/client_script.py @@ -1,12 +1,8 @@ -import base64 -import socket - import logging +import socket import struct -import nacl - -from noise.constants import * +from noise.echo.echo_constants import PSK, PATTERN, CIPHER, DH, HASH logger = logging.getLogger(__name__) diff --git a/noise/echo/echo_constants.py b/noise/echo/echo_constants.py new file mode 100644 index 0000000..d6feb16 --- /dev/null +++ b/noise/echo/echo_constants.py @@ -0,0 +1,49 @@ +from enum import Enum + + +class PSK(Enum): + PSK_DISABLED = 0x00 + PSK_ENABLED = 0x01 + + +class PATTERN(Enum): + PATTERN_NN = 0x00 + PATTERN_KN = 0x01 + PATTERN_NK = 0x02 + PATTERN_KK = 0x03 + PATTERN_NX = 0x04 + PATTERN_KX = 0x05 + PATTERN_XN = 0x06 + PATTERN_IN = 0x07 + PATTERN_XK = 0x08 + PATTERN_IK = 0x09 + PATTERN_XX = 0x0A + PATTERN_IX = 0x0B + PATTERN_HFS = 0x80 + + +class CIPHER(Enum): + CIPHER_CHACHAPOLY = 0x00 + CIPHER_AESGCM = 0x01 + + +class DH(Enum): + DH_25519 = 0x00 + DH_448 = 0x01 + DH_NEWHOPE = 0x02 + DH_MASK = 0x0F + + +class HYBRID(Enum): + HYBRID_NONE = 0x00 + HYBRID_25519 = 0x10 + HYBRID_448 = 0x20 + HYBRID_NEWHOPE = 0x30 + HYBRID_MASK = 0xF0 + + +class HASH(Enum): + HASH_SHA256 = 0x00 + HASH_SHA512 = 0x01 + HASH_BLAKE2s = 0x02 + HASH_BLAKE2b = 0x03 \ No newline at end of file diff --git a/noise/state.py b/noise/state.py index 7a5be34..4ea360e 100644 --- a/noise/state.py +++ b/noise/state.py @@ -1,25 +1,29 @@ +from noise.constants import Empty + + class CipherState(object): """ """ def __init__(self): - self.k = None + self.k = Empty() self.n = None def initialize_key(self, key): """ - - :param key: + + :param key: :return: """ - pass + self.k = key + self.n = 0 def has_key(self): """ - :return: + :return: True if self.k is not an instance of Empty """ - return self.k is not None + return not isinstance(self.k, Empty) def encrypt_with_ad(self, ad, plaintext): """ @@ -98,13 +102,14 @@ class HandshakeState(object): """ def __init__(self): + self.symmetric_state = Empty() self.handshake_pattern = None self.initiator = None self.prologue = b'' - self.s = None - self.e = None - self.rs = None - self.re = None + self.s = Empty() + self.e = Empty() + self.rs = Empty() + self.re = Empty() def initialize(self, handshake_pattern, initiator, prologue=b'', s=None, e=None, rs=None, re=None): """