2017-02-22 19:26:11 +00:00
|
|
|
from zope.interface import implementer
|
2017-02-22 02:46:06 +00:00
|
|
|
from automat import MethodicalMachine
|
2017-02-22 19:26:11 +00:00
|
|
|
from . import _interfaces
|
2017-02-22 20:51:53 +00:00
|
|
|
from ._key import derive_phase_key, encrypt_data
|
2017-02-22 02:46:06 +00:00
|
|
|
|
2017-02-22 19:26:11 +00:00
|
|
|
@implementer(_interfaces.ISend)
|
|
|
|
class Send(object):
|
2017-02-22 02:46:06 +00:00
|
|
|
m = MethodicalMachine()
|
2017-02-22 19:26:11 +00:00
|
|
|
def __init__(self, side, timing):
|
|
|
|
self._side = side
|
2017-02-22 02:46:06 +00:00
|
|
|
self._timing = timing
|
2017-02-22 19:26:11 +00:00
|
|
|
def wire(self, mailbox):
|
|
|
|
self._M = _interfaces.IMailbox(mailbox)
|
2017-02-22 02:46:06 +00:00
|
|
|
|
|
|
|
@m.state(initial=True)
|
|
|
|
def S0_no_key(self): pass
|
2017-02-22 19:26:11 +00:00
|
|
|
@m.state(terminal=True)
|
2017-02-22 02:46:06 +00:00
|
|
|
def S1_verified_key(self): pass
|
|
|
|
|
|
|
|
@m.input()
|
|
|
|
def got_verified_key(self, key): pass
|
|
|
|
@m.input()
|
2017-02-22 20:51:53 +00:00
|
|
|
def send(self, phase, plaintext): pass
|
2017-02-22 02:46:06 +00:00
|
|
|
|
|
|
|
@m.output()
|
2017-02-22 20:51:53 +00:00
|
|
|
def queue(self, phase, plaintext):
|
|
|
|
assert isinstance(phase, type("")), type(phase)
|
|
|
|
assert isinstance(plaintext, type(b"")), type(plaintext)
|
|
|
|
self._queue.append((phase, plaintext))
|
2017-02-22 02:46:06 +00:00
|
|
|
@m.output()
|
|
|
|
def record_key(self, key):
|
|
|
|
self._key = key
|
|
|
|
@m.output()
|
|
|
|
def drain(self, key):
|
|
|
|
del key
|
2017-02-22 20:51:53 +00:00
|
|
|
for (phase, plaintext) in self._queue:
|
|
|
|
self._encrypt_and_send(phase, plaintext)
|
2017-02-22 19:26:11 +00:00
|
|
|
self._queue[:] = []
|
2017-02-22 02:46:06 +00:00
|
|
|
@m.output()
|
2017-02-22 20:51:53 +00:00
|
|
|
def deliver(self, phase, plaintext):
|
|
|
|
assert isinstance(phase, type("")), type(phase)
|
|
|
|
assert isinstance(plaintext, type(b"")), type(plaintext)
|
|
|
|
self._encrypt_and_send(phase, plaintext)
|
2017-02-22 02:46:06 +00:00
|
|
|
|
2017-02-22 20:51:53 +00:00
|
|
|
def _encrypt_and_send(self, phase, plaintext):
|
|
|
|
data_key = derive_phase_key(self._side, phase)
|
|
|
|
encrypted = encrypt_data(data_key, plaintext)
|
2017-02-22 19:26:11 +00:00
|
|
|
self._M.add_message(phase, encrypted)
|
2017-02-22 02:46:06 +00:00
|
|
|
|
|
|
|
S0_no_key.upon(send, enter=S0_no_key, outputs=[queue])
|
|
|
|
S0_no_key.upon(got_verified_key, enter=S1_verified_key,
|
|
|
|
outputs=[record_key, drain])
|
|
|
|
S1_verified_key.upon(send, enter=S1_verified_key, outputs=[deliver])
|