magic-wormhole/src/wormhole/_send.py

78 lines
2.2 KiB
Python
Raw Normal View History

2018-04-21 07:30:08 +00:00
from __future__ import absolute_import, print_function, unicode_literals
from attr import attrib, attrs
from attr.validators import instance_of, provides
from automat import MethodicalMachine
2018-04-21 07:30:08 +00:00
from zope.interface import implementer
from . import _interfaces
2017-02-22 20:51:53 +00:00
from ._key import derive_phase_key, encrypt_data
2018-04-21 07:30:08 +00:00
2017-02-24 02:11:07 +00:00
@attrs
@implementer(_interfaces.ISend)
class Send(object):
2017-02-24 02:11:07 +00:00
_side = attrib(validator=instance_of(type(u"")))
_timing = attrib(validator=provides(_interfaces.ITiming))
m = MethodicalMachine()
2018-04-21 07:30:08 +00:00
set_trace = getattr(m, "_setTrace",
lambda self, f: None) # pragma: no cover
2017-02-24 02:11:07 +00:00
2017-02-25 02:30:00 +00:00
def __attrs_post_init__(self):
self._queue = []
def wire(self, mailbox):
self._M = _interfaces.IMailbox(mailbox)
@m.state(initial=True)
2018-04-21 07:30:08 +00:00
def S0_no_key(self):
pass # pragma: no cover
@m.state(terminal=True)
2018-04-21 07:30:08 +00:00
def S1_verified_key(self):
pass # pragma: no cover
# from Receive
@m.input()
2018-04-21 07:30:08 +00:00
def got_verified_key(self, key):
pass
# from Boss
@m.input()
2018-04-21 07:30:08 +00:00
def send(self, phase, plaintext):
pass
@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))
2018-04-21 07:30:08 +00:00
@m.output()
def record_key(self, key):
self._key = key
2018-04-21 07:30:08 +00:00
@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)
self._queue[:] = []
2018-04-21 07:30:08 +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 20:51:53 +00:00
def _encrypt_and_send(self, phase, plaintext):
2017-02-25 02:30:00 +00:00
assert self._key
data_key = derive_phase_key(self._key, self._side, phase)
2017-02-22 20:51:53 +00:00
encrypted = encrypt_data(data_key, plaintext)
self._M.add_message(phase, encrypted)
S0_no_key.upon(send, enter=S0_no_key, outputs=[queue])
2018-04-21 07:30:08 +00:00
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])