From bf36429fd7dfdfe94c86b5e7d6e2c72d793c9f6b Mon Sep 17 00:00:00 2001 From: Piotr Lizonczyk Date: Sun, 6 Aug 2017 00:04:16 +0200 Subject: [PATCH] * Adding handshake patterns * Adding token constants for handshake patterns --- noise/constants.py | 10 +++ noise/patterns.py | 161 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 noise/patterns.py diff --git a/noise/constants.py b/noise/constants.py index b6da454..414cc11 100644 --- a/noise/constants.py +++ b/noise/constants.py @@ -1,2 +1,12 @@ class Empty: pass + + +# Handshake pattern tokens +TOKEN_E = 'e' +TOKEN_S = 's' +TOKEN_EE = 'ee' +TOKEN_ES = 'es' +TOKEN_SE = 'se' +TOKEN_SS = 'ss' +TOKEN_PSK = 'psk' diff --git a/noise/patterns.py b/noise/patterns.py new file mode 100644 index 0000000..f3518de --- /dev/null +++ b/noise/patterns.py @@ -0,0 +1,161 @@ +from noise.constants import TOKEN_E, TOKEN_S, TOKEN_EE, TOKEN_ES, TOKEN_SE, TOKEN_SS, TOKEN_PSK + + +class Pattern(object): + """ + TODO document + """ + # As per specification, if both parties have pre-messages, the initiator is listed first. To reduce complexity, + # pre_messages shall be a list of two lists: + # the first for the initiator's pre-messages, the second for the responder + pre_messages = [ + [], + [] + ] + # TODO Comment + tokens = [] + + +# One-way patterns + +class PatternN(Pattern): + pre_messages = [ + [], + [TOKEN_S] + ] + tokens = [ + [TOKEN_E, TOKEN_ES] + ] + + +class PatternK(Pattern): + pre_messages = [ + [TOKEN_S], + [TOKEN_S] + ] + tokens = [ + [TOKEN_E, TOKEN_ES, TOKEN_SS] + ] + + +class PatternX(Pattern): + pre_messages = [ + [], + [TOKEN_S] + ] + tokens = [ + [TOKEN_E, TOKEN_ES, TOKEN_S, TOKEN_SS] + ] + + +# Interactive patterns + +class PatternNN(Pattern): + tokens = [ + [TOKEN_E], + [TOKEN_E, TOKEN_EE] + ] + + +class PatternKN(Pattern): + pre_messages = [ + [TOKEN_S], + [] + ] + tokens = [ + [TOKEN_E], + [TOKEN_E, TOKEN_EE, TOKEN_SE] + ] + + +class PatternNK(Pattern): + pre_messages = [ + [], + [TOKEN_S] + ] + tokens = [ + [TOKEN_E, TOKEN_ES], + [TOKEN_E, TOKEN_EE] + ] + + +class PatternKK(Pattern): + pre_messages = [ + [TOKEN_S], + [TOKEN_S] + ] + tokens = [ + [TOKEN_E, TOKEN_ES, TOKEN_SS], + [TOKEN_E, TOKEN_EE, TOKEN_SE] + ] + + +class PatternNX(Pattern): + tokens = [ + [TOKEN_E], + [TOKEN_E, TOKEN_EE, TOKEN_S, TOKEN_ES] + ] + + +class PatternKX(Pattern): + pre_messages = [ + [TOKEN_S], + [] + ] + tokens = [ + [TOKEN_E], + [TOKEN_E, TOKEN_EE, TOKEN_SE, TOKEN_S, TOKEN_ES] + ] + + +class PatternXN(Pattern): + tokens = [ + [TOKEN_E], + [TOKEN_E, TOKEN_EE], + [TOKEN_S, TOKEN_SE] + ] + + +class PatternIN(Pattern): + tokens = [ + [TOKEN_E, TOKEN_S], + [TOKEN_E, TOKEN_EE, TOKEN_SE] + ] + + +class PatternXK(Pattern): + pre_messages = [ + [], + [TOKEN_S] + ] + tokens = [ + [TOKEN_E, TOKEN_ES], + [TOKEN_E, TOKEN_EE], + [TOKEN_S, TOKEN_SE] + ] + + +class PatternIK(Pattern): + pre_messages = [ + [], + [TOKEN_S] + ] + tokens = [ + [TOKEN_E, TOKEN_ES, TOKEN_S, TOKEN_SS], + [TOKEN_E, TOKEN_EE, TOKEN_SE] + ] + + +class PatternXX(Pattern): + tokens = [ + [TOKEN_E], + [TOKEN_E, TOKEN_EE, TOKEN_S, TOKEN_ES], + [TOKEN_S, TOKEN_SE] + ] + + +class PatternIX(Pattern): + tokens = [ + [TOKEN_E, TOKEN_S], + [TOKEN_E, TOKEN_EE, TOKEN_SE, TOKEN_S, TOKEN_ES] + ]