1# This file is dual licensed under the terms of the Apache License, Version 2# 2.0, and the BSD License. See the LICENSE file in the root of this repository 3# for complete details. 4 5from __future__ import absolute_import, division, print_function 6 7import six 8 9from cryptography import utils 10from cryptography.exceptions import ( 11 AlreadyFinalized, 12 InvalidKey, 13 UnsupportedAlgorithm, 14 _Reasons, 15) 16from cryptography.hazmat.backends import _get_backend 17from cryptography.hazmat.backends.interfaces import HMACBackend 18from cryptography.hazmat.primitives import constant_time, hmac 19from cryptography.hazmat.primitives.kdf import KeyDerivationFunction 20 21 22@utils.register_interface(KeyDerivationFunction) 23class HKDF(object): 24 def __init__(self, algorithm, length, salt, info, backend=None): 25 backend = _get_backend(backend) 26 if not isinstance(backend, HMACBackend): 27 raise UnsupportedAlgorithm( 28 "Backend object does not implement HMACBackend.", 29 _Reasons.BACKEND_MISSING_INTERFACE, 30 ) 31 32 self._algorithm = algorithm 33 34 if salt is None: 35 salt = b"\x00" * self._algorithm.digest_size 36 else: 37 utils._check_bytes("salt", salt) 38 39 self._salt = salt 40 41 self._backend = backend 42 43 self._hkdf_expand = HKDFExpand(self._algorithm, length, info, backend) 44 45 def _extract(self, key_material): 46 h = hmac.HMAC(self._salt, self._algorithm, backend=self._backend) 47 h.update(key_material) 48 return h.finalize() 49 50 def derive(self, key_material): 51 utils._check_byteslike("key_material", key_material) 52 return self._hkdf_expand.derive(self._extract(key_material)) 53 54 def verify(self, key_material, expected_key): 55 if not constant_time.bytes_eq(self.derive(key_material), expected_key): 56 raise InvalidKey 57 58 59@utils.register_interface(KeyDerivationFunction) 60class HKDFExpand(object): 61 def __init__(self, algorithm, length, info, backend=None): 62 backend = _get_backend(backend) 63 if not isinstance(backend, HMACBackend): 64 raise UnsupportedAlgorithm( 65 "Backend object does not implement HMACBackend.", 66 _Reasons.BACKEND_MISSING_INTERFACE, 67 ) 68 69 self._algorithm = algorithm 70 71 self._backend = backend 72 73 max_length = 255 * algorithm.digest_size 74 75 if length > max_length: 76 raise ValueError( 77 "Can not derive keys larger than {} octets.".format(max_length) 78 ) 79 80 self._length = length 81 82 if info is None: 83 info = b"" 84 else: 85 utils._check_bytes("info", info) 86 87 self._info = info 88 89 self._used = False 90 91 def _expand(self, key_material): 92 output = [b""] 93 counter = 1 94 95 while self._algorithm.digest_size * (len(output) - 1) < self._length: 96 h = hmac.HMAC(key_material, self._algorithm, backend=self._backend) 97 h.update(output[-1]) 98 h.update(self._info) 99 h.update(six.int2byte(counter)) 100 output.append(h.finalize()) 101 counter += 1 102 103 return b"".join(output)[: self._length] 104 105 def derive(self, key_material): 106 utils._check_byteslike("key_material", key_material) 107 if self._used: 108 raise AlreadyFinalized 109 110 self._used = True 111 return self._expand(key_material) 112 113 def verify(self, key_material, expected_key): 114 if not constant_time.bytes_eq(self.derive(key_material), expected_key): 115 raise InvalidKey 116