factor out HKDF

This commit is contained in:
Brian Warner 2018-12-22 17:27:54 -05:00
parent b4c90b40a2
commit e7cb1df785
5 changed files with 9 additions and 19 deletions

View File

@ -11,11 +11,11 @@ from twisted.internet.defer import DeferredList
from twisted.internet.endpoints import serverFromString from twisted.internet.endpoints import serverFromString
from twisted.internet.protocol import ClientFactory, ServerFactory from twisted.internet.protocol import ClientFactory, ServerFactory
from twisted.python import log from twisted.python import log
from hkdf import Hkdf
from .. import ipaddrs # TODO: move into _dilation/ from .. import ipaddrs # TODO: move into _dilation/
from .._interfaces import IDilationConnector, IDilationManager from .._interfaces import IDilationConnector, IDilationManager
from ..timing import DebugTiming from ..timing import DebugTiming
from ..observer import EmptyableSet from ..observer import EmptyableSet
from ..util import HKDF
from .connection import DilatedConnectionProtocol, KCM from .connection import DilatedConnectionProtocol, KCM
from .roles import LEADER from .roles import LEADER
@ -24,11 +24,6 @@ from .._hints import (DirectTCPV1Hint, TorTCPV1Hint, RelayV1Hint,
encode_hint) encode_hint)
def HKDF(skm, outlen, salt=None, CTXinfo=b""):
return Hkdf(salt, skm).expand(CTXinfo, outlen)
def build_sided_relay_handshake(key, side): def build_sided_relay_handshake(key, side):
assert isinstance(side, type(u"")) assert isinstance(side, type(u""))
assert len(side) == 8 * 2 assert len(side) == 8 * 2

View File

@ -6,7 +6,6 @@ import six
from attr import attrib, attrs from attr import attrib, attrs
from attr.validators import instance_of, provides from attr.validators import instance_of, provides
from automat import MethodicalMachine from automat import MethodicalMachine
from hkdf import Hkdf
from nacl import utils from nacl import utils
from nacl.exceptions import CryptoError from nacl.exceptions import CryptoError
from nacl.secret import SecretBox from nacl.secret import SecretBox
@ -15,16 +14,12 @@ from zope.interface import implementer
from . import _interfaces from . import _interfaces
from .util import (bytes_to_dict, bytes_to_hexstr, dict_to_bytes, from .util import (bytes_to_dict, bytes_to_hexstr, dict_to_bytes,
hexstr_to_bytes, to_bytes) hexstr_to_bytes, to_bytes, HKDF)
CryptoError CryptoError
__all__ = ["derive_key", "derive_phase_key", "CryptoError", "Key"] __all__ = ["derive_key", "derive_phase_key", "CryptoError", "Key"]
def HKDF(skm, outlen, salt=None, CTXinfo=b""):
return Hkdf(salt, skm).expand(CTXinfo, outlen)
def derive_key(key, purpose, length=SecretBox.KEY_SIZE): def derive_key(key, purpose, length=SecretBox.KEY_SIZE):
if not isinstance(key, type(b"")): if not isinstance(key, type(b"")):
raise TypeError(type(key)) raise TypeError(type(key))

View File

@ -19,6 +19,7 @@ from wormhole_transit_relay import transit_server
from .. import transit from .. import transit
from .._hints import DirectTCPV1Hint from .._hints import DirectTCPV1Hint
from ..errors import InternalError from ..errors import InternalError
from ..util import HKDF
from .common import ServerBase from .common import ServerBase
@ -1526,7 +1527,7 @@ class Transit(unittest.TestCase):
class RelayHandshake(unittest.TestCase): class RelayHandshake(unittest.TestCase):
def old_build_relay_handshake(self, key): def old_build_relay_handshake(self, key):
token = transit.HKDF(key, 32, CTXinfo=b"transit_relay_token") token = HKDF(key, 32, CTXinfo=b"transit_relay_token")
return (token, b"please relay " + hexlify(token) + b"\n") return (token, b"please relay " + hexlify(token) + b"\n")
def test_old(self): def test_old(self):

View File

@ -9,7 +9,6 @@ from binascii import hexlify, unhexlify
from collections import deque from collections import deque
import six import six
from hkdf import Hkdf
from nacl.secret import SecretBox from nacl.secret import SecretBox
from twisted.internet import (address, defer, endpoints, error, interfaces, from twisted.internet import (address, defer, endpoints, error, interfaces,
protocol, reactor, task) protocol, reactor, task)
@ -22,16 +21,12 @@ from zope.interface import implementer
from . import ipaddrs from . import ipaddrs
from .errors import InternalError from .errors import InternalError
from .timing import DebugTiming from .timing import DebugTiming
from .util import bytes_to_hexstr from .util import bytes_to_hexstr, HKDF
from ._hints import (DirectTCPV1Hint, RelayV1Hint, from ._hints import (DirectTCPV1Hint, RelayV1Hint,
parse_hint_argv, describe_hint_obj, endpoint_from_hint_obj, parse_hint_argv, describe_hint_obj, endpoint_from_hint_obj,
parse_tcp_v1_hint) parse_tcp_v1_hint)
def HKDF(skm, outlen, salt=None, CTXinfo=b""):
return Hkdf(salt, skm).expand(CTXinfo, outlen)
class TransitError(Exception): class TransitError(Exception):
pass pass

View File

@ -3,8 +3,12 @@ import json
import os import os
import unicodedata import unicodedata
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from hkdf import Hkdf
def HKDF(skm, outlen, salt=None, CTXinfo=b""):
return Hkdf(salt, skm).expand(CTXinfo, outlen)
def to_bytes(u): def to_bytes(u):
return unicodedata.normalize("NFC", u).encode("utf-8") return unicodedata.normalize("NFC", u).encode("utf-8")