2017-02-24 02:11:07 +00:00
|
|
|
from __future__ import print_function, absolute_import, unicode_literals
|
2017-02-22 19:26:11 +00:00
|
|
|
from hashlib import sha256
|
2017-02-25 02:30:00 +00:00
|
|
|
import six
|
2017-02-22 19:26:11 +00:00
|
|
|
from zope.interface import implementer
|
2017-02-23 00:56:39 +00:00
|
|
|
from attr import attrs, attrib
|
2017-02-24 02:11:07 +00:00
|
|
|
from attr.validators import provides, instance_of
|
2017-02-22 02:46:06 +00:00
|
|
|
from spake2 import SPAKE2_Symmetric
|
|
|
|
from hkdf import Hkdf
|
|
|
|
from nacl.secret import SecretBox
|
2017-02-22 19:26:11 +00:00
|
|
|
from nacl.exceptions import CryptoError
|
2017-02-22 20:51:53 +00:00
|
|
|
from nacl import utils
|
2017-02-22 19:26:11 +00:00
|
|
|
from automat import MethodicalMachine
|
2017-02-22 20:51:53 +00:00
|
|
|
from .util import (to_bytes, bytes_to_hexstr, hexstr_to_bytes,
|
|
|
|
bytes_to_dict, dict_to_bytes)
|
2017-02-22 19:26:11 +00:00
|
|
|
from . import _interfaces
|
|
|
|
CryptoError
|
|
|
|
__all__ = ["derive_key", "derive_phase_key", "CryptoError",
|
|
|
|
"Key"]
|
2017-02-22 02:46:06 +00:00
|
|
|
|
|
|
|
def HKDF(skm, outlen, salt=None, CTXinfo=b""):
|
|
|
|
return Hkdf(salt, skm).expand(CTXinfo, outlen)
|
|
|
|
|
|
|
|
def derive_key(key, purpose, length=SecretBox.KEY_SIZE):
|
|
|
|
if not isinstance(key, type(b"")): raise TypeError(type(key))
|
|
|
|
if not isinstance(purpose, type(b"")): raise TypeError(type(purpose))
|
2017-02-25 02:30:00 +00:00
|
|
|
if not isinstance(length, six.integer_types): raise TypeError(type(length))
|
2017-02-22 02:46:06 +00:00
|
|
|
return HKDF(key, length, CTXinfo=purpose)
|
|
|
|
|
2017-02-25 02:30:00 +00:00
|
|
|
def derive_phase_key(key, side, phase):
|
2017-02-22 19:26:11 +00:00
|
|
|
assert isinstance(side, type("")), type(side)
|
|
|
|
assert isinstance(phase, type("")), type(phase)
|
|
|
|
side_bytes = side.encode("ascii")
|
|
|
|
phase_bytes = phase.encode("ascii")
|
|
|
|
purpose = (b"wormhole:phase:"
|
|
|
|
+ sha256(side_bytes).digest()
|
|
|
|
+ sha256(phase_bytes).digest())
|
2017-02-25 02:30:00 +00:00
|
|
|
return derive_key(key, purpose)
|
2017-02-22 19:26:11 +00:00
|
|
|
|
|
|
|
def decrypt_data(key, encrypted):
|
|
|
|
assert isinstance(key, type(b"")), type(key)
|
|
|
|
assert isinstance(encrypted, type(b"")), type(encrypted)
|
|
|
|
assert len(key) == SecretBox.KEY_SIZE, len(key)
|
|
|
|
box = SecretBox(key)
|
|
|
|
data = box.decrypt(encrypted)
|
|
|
|
return data
|
|
|
|
|
2017-02-22 20:51:53 +00:00
|
|
|
def encrypt_data(key, plaintext):
|
|
|
|
assert isinstance(key, type(b"")), type(key)
|
|
|
|
assert isinstance(plaintext, type(b"")), type(plaintext)
|
|
|
|
assert len(key) == SecretBox.KEY_SIZE, len(key)
|
|
|
|
box = SecretBox(key)
|
|
|
|
nonce = utils.random(SecretBox.NONCE_SIZE)
|
|
|
|
return box.encrypt(plaintext, nonce)
|
|
|
|
|
2017-02-23 00:56:39 +00:00
|
|
|
@attrs
|
2017-02-22 19:26:11 +00:00
|
|
|
@implementer(_interfaces.IKey)
|
|
|
|
class Key(object):
|
2017-02-24 02:11:07 +00:00
|
|
|
_appid = attrib(validator=instance_of(type(u"")))
|
2017-02-25 02:30:00 +00:00
|
|
|
_side = attrib(validator=instance_of(type(u"")))
|
2017-02-23 00:56:39 +00:00
|
|
|
_timing = attrib(validator=provides(_interfaces.ITiming))
|
2017-02-22 02:46:06 +00:00
|
|
|
m = MethodicalMachine()
|
2017-02-23 00:56:39 +00:00
|
|
|
|
2017-02-22 21:45:18 +00:00
|
|
|
def wire(self, boss, mailbox, receive):
|
|
|
|
self._B = _interfaces.IBoss(boss)
|
2017-02-22 19:26:11 +00:00
|
|
|
self._M = _interfaces.IMailbox(mailbox)
|
|
|
|
self._R = _interfaces.IReceive(receive)
|
2017-02-22 02:46:06 +00:00
|
|
|
|
|
|
|
@m.state(initial=True)
|
|
|
|
def S0_know_nothing(self): pass
|
|
|
|
@m.state()
|
|
|
|
def S1_know_code(self): pass
|
|
|
|
@m.state()
|
|
|
|
def S2_know_key(self): pass
|
2017-02-22 19:26:11 +00:00
|
|
|
@m.state(terminal=True)
|
2017-02-22 02:46:06 +00:00
|
|
|
def S3_scared(self): pass
|
|
|
|
|
2017-02-22 21:45:18 +00:00
|
|
|
# from Boss
|
|
|
|
@m.input()
|
2017-02-23 00:56:39 +00:00
|
|
|
def got_code(self, code): pass
|
2017-02-22 21:45:18 +00:00
|
|
|
|
|
|
|
# from Ordering
|
2017-02-22 20:51:53 +00:00
|
|
|
def got_pake(self, body):
|
|
|
|
assert isinstance(body, type(b"")), type(body)
|
|
|
|
payload = bytes_to_dict(body)
|
2017-02-22 02:46:06 +00:00
|
|
|
if "pake_v1" in payload:
|
|
|
|
self.got_pake_good(hexstr_to_bytes(payload["pake_v1"]))
|
|
|
|
else:
|
|
|
|
self.got_pake_bad()
|
|
|
|
@m.input()
|
|
|
|
def got_pake_good(self, msg2): pass
|
|
|
|
@m.input()
|
|
|
|
def got_pake_bad(self): pass
|
|
|
|
|
|
|
|
@m.output()
|
|
|
|
def build_pake(self, code):
|
|
|
|
with self._timing.add("pake1", waiting="crypto"):
|
|
|
|
self._sp = SPAKE2_Symmetric(to_bytes(code),
|
|
|
|
idSymmetric=to_bytes(self._appid))
|
|
|
|
msg1 = self._sp.start()
|
2017-02-22 20:51:53 +00:00
|
|
|
body = dict_to_bytes({"pake_v1": bytes_to_hexstr(msg1)})
|
|
|
|
self._M.add_message("pake", body)
|
2017-02-22 02:46:06 +00:00
|
|
|
|
|
|
|
@m.output()
|
|
|
|
def scared(self):
|
2017-02-22 21:45:18 +00:00
|
|
|
self._B.scared()
|
2017-02-22 02:46:06 +00:00
|
|
|
@m.output()
|
|
|
|
def compute_key(self, msg2):
|
|
|
|
assert isinstance(msg2, type(b""))
|
|
|
|
with self._timing.add("pake2", waiting="crypto"):
|
|
|
|
key = self._sp.finish(msg2)
|
2017-02-22 21:45:18 +00:00
|
|
|
self._B.got_verifier(derive_key(key, b"wormhole:verifier"))
|
2017-02-25 02:30:00 +00:00
|
|
|
phase = "version"
|
|
|
|
data_key = derive_phase_key(key, self._side, phase)
|
|
|
|
my_versions = {} # TODO: get from Wormhole?
|
|
|
|
plaintext = dict_to_bytes(my_versions)
|
|
|
|
encrypted = encrypt_data(data_key, plaintext)
|
|
|
|
self._M.add_message(phase, encrypted)
|
2017-02-22 19:26:11 +00:00
|
|
|
self._R.got_key(key)
|
2017-02-22 02:46:06 +00:00
|
|
|
|
2017-02-23 00:56:39 +00:00
|
|
|
S0_know_nothing.upon(got_code, enter=S1_know_code, outputs=[build_pake])
|
2017-02-22 02:46:06 +00:00
|
|
|
S1_know_code.upon(got_pake_good, enter=S2_know_key, outputs=[compute_key])
|
|
|
|
S1_know_code.upon(got_pake_bad, enter=S3_scared, outputs=[scared])
|