KDF.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. #
  2. # KDF.py : a collection of Key Derivation Functions
  3. #
  4. # Part of the Python Cryptography Toolkit
  5. #
  6. # ===================================================================
  7. # The contents of this file are dedicated to the public domain. To
  8. # the extent that dedication to the public domain is not available,
  9. # everyone is granted a worldwide, perpetual, royalty-free,
  10. # non-exclusive license to exercise all rights associated with the
  11. # contents of this file for any purpose whatsoever.
  12. # No rights are reserved.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. # ===================================================================
  23. import struct
  24. from functools import reduce
  25. from Cryptodome.Util.py3compat import tobytes, bord, _copy_bytes, iter_range
  26. from Cryptodome.Hash import SHA1, SHA256, HMAC, CMAC
  27. from Cryptodome.Util.strxor import strxor
  28. from Cryptodome.Util.number import size as bit_size, long_to_bytes, bytes_to_long
  29. from Cryptodome.Util._raw_api import (load_pycryptodome_raw_lib,
  30. create_string_buffer,
  31. get_raw_buffer, c_size_t)
  32. _raw_salsa20_lib = load_pycryptodome_raw_lib("Cryptodome.Cipher._Salsa20",
  33. """
  34. int Salsa20_8_core(const uint8_t *x, const uint8_t *y,
  35. uint8_t *out);
  36. """)
  37. _raw_scrypt_lib = load_pycryptodome_raw_lib("Cryptodome.Protocol._scrypt",
  38. """
  39. typedef int (core_t)(const uint8_t [64], const uint8_t [64], uint8_t [64]);
  40. int scryptROMix(const uint8_t *data_in, uint8_t *data_out,
  41. size_t data_len, unsigned N, core_t *core);
  42. """)
  43. def PBKDF1(password, salt, dkLen, count=1000, hashAlgo=None):
  44. """Derive one key from a password (or passphrase).
  45. This function performs key derivation according to an old version of
  46. the PKCS#5 standard (v1.5) or `RFC2898
  47. <https://www.ietf.org/rfc/rfc2898.txt>`_.
  48. .. warning::
  49. Newer applications should use the more secure and versatile :func:`PBKDF2`
  50. instead.
  51. Args:
  52. password (string):
  53. The secret password to generate the key from.
  54. salt (byte string):
  55. An 8 byte string to use for better protection from dictionary attacks.
  56. This value does not need to be kept secret, but it should be randomly
  57. chosen for each derivation.
  58. dkLen (integer):
  59. The length of the desired key. The default is 16 bytes, suitable for
  60. instance for :mod:`Cryptodome.Cipher.AES`.
  61. count (integer):
  62. The number of iterations to carry out. The recommendation is 1000 or
  63. more.
  64. hashAlgo (module):
  65. The hash algorithm to use, as a module or an object from the :mod:`Cryptodome.Hash` package.
  66. The digest length must be no shorter than ``dkLen``.
  67. The default algorithm is :mod:`Cryptodome.Hash.SHA1`.
  68. Return:
  69. A byte string of length ``dkLen`` that can be used as key.
  70. """
  71. if not hashAlgo:
  72. hashAlgo = SHA1
  73. password = tobytes(password)
  74. pHash = hashAlgo.new(password+salt)
  75. digest = pHash.digest_size
  76. if dkLen > digest:
  77. raise TypeError("Selected hash algorithm has a too short digest (%d bytes)." % digest)
  78. if len(salt) != 8:
  79. raise ValueError("Salt is not 8 bytes long (%d bytes instead)." % len(salt))
  80. for i in iter_range(count-1):
  81. pHash = pHash.new(pHash.digest())
  82. return pHash.digest()[:dkLen]
  83. def PBKDF2(password, salt, dkLen=16, count=1000, prf=None, hmac_hash_module=None):
  84. """Derive one or more keys from a password (or passphrase).
  85. This function performs key derivation according to
  86. the PKCS#5 standard (v2.0).
  87. Args:
  88. password (string or byte string):
  89. The secret password to generate the key from.
  90. salt (string or byte string):
  91. A (byte) string to use for better protection from dictionary attacks.
  92. This value does not need to be kept secret, but it should be randomly
  93. chosen for each derivation. It is recommended to be at least 8 bytes long.
  94. dkLen (integer):
  95. The cumulative length of the desired keys.
  96. count (integer):
  97. The number of iterations to carry out.
  98. prf (callable):
  99. A pseudorandom function. It must be a function that returns a pseudorandom string
  100. from two parameters: a secret and a salt. If not specified,
  101. **HMAC-SHA1** is used.
  102. hmac_hash_module (module):
  103. A module from `Cryptodome.Hash` implementing a Merkle-Damgard cryptographic
  104. hash, which PBKDF2 must use in combination with HMAC.
  105. This parameter is mutually exclusive with ``prf``.
  106. Return:
  107. A byte string of length ``dkLen`` that can be used as key material.
  108. If you wanted multiple keys, just break up this string into segments of the desired length.
  109. """
  110. password = tobytes(password)
  111. salt = tobytes(salt)
  112. if prf and hmac_hash_module:
  113. raise ValueError("'prf' and 'hmac_hash_module' are mutually exlusive")
  114. if prf is None and hmac_hash_module is None:
  115. hmac_hash_module = SHA1
  116. if prf or not hasattr(hmac_hash_module, "_pbkdf2_hmac_assist"):
  117. # Generic (and slow) implementation
  118. if prf is None:
  119. prf = lambda p,s: HMAC.new(p, s, hmac_hash_module).digest()
  120. def link(s):
  121. s[0], s[1] = s[1], prf(password, s[1])
  122. return s[0]
  123. key = b''
  124. i = 1
  125. while len(key) < dkLen:
  126. s = [ prf(password, salt + struct.pack(">I", i)) ] * 2
  127. key += reduce(strxor, (link(s) for j in range(count)) )
  128. i += 1
  129. else:
  130. # Optimized implementation
  131. key = b''
  132. i = 1
  133. while len(key)<dkLen:
  134. base = HMAC.new(password, b"", hmac_hash_module)
  135. first_digest = base.copy().update(salt + struct.pack(">I", i)).digest()
  136. key += base._pbkdf2_hmac_assist(first_digest, count)
  137. i += 1
  138. return key[:dkLen]
  139. class _S2V(object):
  140. """String-to-vector PRF as defined in `RFC5297`_.
  141. This class implements a pseudorandom function family
  142. based on CMAC that takes as input a vector of strings.
  143. .. _RFC5297: http://tools.ietf.org/html/rfc5297
  144. """
  145. def __init__(self, key, ciphermod, cipher_params=None):
  146. """Initialize the S2V PRF.
  147. :Parameters:
  148. key : byte string
  149. A secret that can be used as key for CMACs
  150. based on ciphers from ``ciphermod``.
  151. ciphermod : module
  152. A block cipher module from `Cryptodome.Cipher`.
  153. cipher_params : dictionary
  154. A set of extra parameters to use to create a cipher instance.
  155. """
  156. self._key = _copy_bytes(None, None, key)
  157. self._ciphermod = ciphermod
  158. self._last_string = self._cache = b'\x00' * ciphermod.block_size
  159. # Max number of update() call we can process
  160. self._n_updates = ciphermod.block_size * 8 - 1
  161. if cipher_params is None:
  162. self._cipher_params = {}
  163. else:
  164. self._cipher_params = dict(cipher_params)
  165. @staticmethod
  166. def new(key, ciphermod):
  167. """Create a new S2V PRF.
  168. :Parameters:
  169. key : byte string
  170. A secret that can be used as key for CMACs
  171. based on ciphers from ``ciphermod``.
  172. ciphermod : module
  173. A block cipher module from `Cryptodome.Cipher`.
  174. """
  175. return _S2V(key, ciphermod)
  176. def _double(self, bs):
  177. doubled = bytes_to_long(bs)<<1
  178. if bord(bs[0]) & 0x80:
  179. doubled ^= 0x87
  180. return long_to_bytes(doubled, len(bs))[-len(bs):]
  181. def update(self, item):
  182. """Pass the next component of the vector.
  183. The maximum number of components you can pass is equal to the block
  184. length of the cipher (in bits) minus 1.
  185. :Parameters:
  186. item : byte string
  187. The next component of the vector.
  188. :Raise TypeError: when the limit on the number of components has been reached.
  189. """
  190. if self._n_updates == 0:
  191. raise TypeError("Too many components passed to S2V")
  192. self._n_updates -= 1
  193. mac = CMAC.new(self._key,
  194. msg=self._last_string,
  195. ciphermod=self._ciphermod,
  196. cipher_params=self._cipher_params)
  197. self._cache = strxor(self._double(self._cache), mac.digest())
  198. self._last_string = _copy_bytes(None, None, item)
  199. def derive(self):
  200. """"Derive a secret from the vector of components.
  201. :Return: a byte string, as long as the block length of the cipher.
  202. """
  203. if len(self._last_string) >= 16:
  204. # xorend
  205. final = self._last_string[:-16] + strxor(self._last_string[-16:], self._cache)
  206. else:
  207. # zero-pad & xor
  208. padded = (self._last_string + b'\x80' + b'\x00' * 15)[:16]
  209. final = strxor(padded, self._double(self._cache))
  210. mac = CMAC.new(self._key,
  211. msg=final,
  212. ciphermod=self._ciphermod,
  213. cipher_params=self._cipher_params)
  214. return mac.digest()
  215. def HKDF(master, key_len, salt, hashmod, num_keys=1, context=None):
  216. """Derive one or more keys from a master secret using
  217. the HMAC-based KDF defined in RFC5869_.
  218. This KDF is not suitable for deriving keys from a password or for key
  219. stretching. Use :func:`PBKDF2` instead.
  220. HKDF is a key derivation method approved by NIST in `SP 800 56C`__.
  221. Args:
  222. master (byte string):
  223. The unguessable value used by the KDF to generate the other keys.
  224. It must be a high-entropy secret, though not necessarily uniform.
  225. It must not be a password.
  226. salt (byte string):
  227. A non-secret, reusable value that strengthens the randomness
  228. extraction step.
  229. Ideally, it is as long as the digest size of the chosen hash.
  230. If empty, a string of zeroes in used.
  231. key_len (integer):
  232. The length in bytes of every derived key.
  233. hashmod (module):
  234. A cryptographic hash algorithm from :mod:`Cryptodome.Hash`.
  235. :mod:`Cryptodome.Hash.SHA512` is a good choice.
  236. num_keys (integer):
  237. The number of keys to derive. Every key is :data:`key_len` bytes long.
  238. The maximum cumulative length of all keys is
  239. 255 times the digest size.
  240. context (byte string):
  241. Optional identifier describing what the keys are used for.
  242. Return:
  243. A byte string or a tuple of byte strings.
  244. .. _RFC5869: http://tools.ietf.org/html/rfc5869
  245. .. __: http://csrc.nist.gov/publications/nistpubs/800-56C/SP-800-56C.pdf
  246. """
  247. output_len = key_len * num_keys
  248. if output_len > (255 * hashmod.digest_size):
  249. raise ValueError("Too much secret data to derive")
  250. if not salt:
  251. salt = b'\x00' * hashmod.digest_size
  252. if context is None:
  253. context = b""
  254. # Step 1: extract
  255. hmac = HMAC.new(salt, master, digestmod=hashmod)
  256. prk = hmac.digest()
  257. # Step 2: expand
  258. t = [ b"" ]
  259. n = 1
  260. tlen = 0
  261. while tlen < output_len:
  262. hmac = HMAC.new(prk, t[-1] + context + struct.pack('B', n), digestmod=hashmod)
  263. t.append(hmac.digest())
  264. tlen += hashmod.digest_size
  265. n += 1
  266. derived_output = b"".join(t)
  267. if num_keys == 1:
  268. return derived_output[:key_len]
  269. kol = [derived_output[idx:idx + key_len]
  270. for idx in iter_range(0, output_len, key_len)]
  271. return list(kol[:num_keys])
  272. def scrypt(password, salt, key_len, N, r, p, num_keys=1):
  273. """Derive one or more keys from a passphrase.
  274. This function performs key derivation according to
  275. the `scrypt`_ algorithm, introduced in Percival's paper
  276. `"Stronger key derivation via sequential memory-hard functions"`__.
  277. This implementation is based on `RFC7914`__.
  278. Args:
  279. password (string):
  280. The secret pass phrase to generate the keys from.
  281. salt (string):
  282. A string to use for better protection from dictionary attacks.
  283. This value does not need to be kept secret,
  284. but it should be randomly chosen for each derivation.
  285. It is recommended to be at least 8 bytes long.
  286. key_len (integer):
  287. The length in bytes of every derived key.
  288. N (integer):
  289. CPU/Memory cost parameter. It must be a power of 2 and less
  290. than :math:`2^{32}`.
  291. r (integer):
  292. Block size parameter.
  293. p (integer):
  294. Parallelization parameter.
  295. It must be no greater than :math:`(2^{32}-1)/(4r)`.
  296. num_keys (integer):
  297. The number of keys to derive. Every key is :data:`key_len` bytes long.
  298. By default, only 1 key is generated.
  299. The maximum cumulative length of all keys is :math:`(2^{32}-1)*32`
  300. (that is, 128TB).
  301. A good choice of parameters *(N, r , p)* was suggested
  302. by Colin Percival in his `presentation in 2009`__:
  303. - *(16384, 8, 1)* for interactive logins (<=100ms)
  304. - *(1048576, 8, 1)* for file encryption (<=5s)
  305. Return:
  306. A byte string or a tuple of byte strings.
  307. .. _scrypt: http://www.tarsnap.com/scrypt.html
  308. .. __: http://www.tarsnap.com/scrypt/scrypt.pdf
  309. .. __: https://tools.ietf.org/html/rfc7914
  310. .. __: http://www.tarsnap.com/scrypt/scrypt-slides.pdf
  311. """
  312. if 2 ** (bit_size(N) - 1) != N:
  313. raise ValueError("N must be a power of 2")
  314. if N >= 2 ** 32:
  315. raise ValueError("N is too big")
  316. if p > ((2 ** 32 - 1) * 32) // (128 * r):
  317. raise ValueError("p or r are too big")
  318. prf_hmac_sha256 = lambda p, s: HMAC.new(p, s, SHA256).digest()
  319. stage_1 = PBKDF2(password, salt, p * 128 * r, 1, prf=prf_hmac_sha256)
  320. scryptROMix = _raw_scrypt_lib.scryptROMix
  321. core = _raw_salsa20_lib.Salsa20_8_core
  322. # Parallelize into p flows
  323. data_out = []
  324. for flow in iter_range(p):
  325. idx = flow * 128 * r
  326. buffer_out = create_string_buffer(128 * r)
  327. result = scryptROMix(stage_1[idx : idx + 128 * r],
  328. buffer_out,
  329. c_size_t(128 * r),
  330. N,
  331. core)
  332. if result:
  333. raise ValueError("Error %X while running scrypt" % result)
  334. data_out += [ get_raw_buffer(buffer_out) ]
  335. dk = PBKDF2(password,
  336. b"".join(data_out),
  337. key_len * num_keys, 1,
  338. prf=prf_hmac_sha256)
  339. if num_keys == 1:
  340. return dk
  341. kol = [dk[idx:idx + key_len]
  342. for idx in iter_range(0, key_len * num_keys, key_len)]
  343. return kol