|
|
925e6b |
|
|
|
925e6b |
# HG changeset patch
|
|
|
925e6b |
# User Benjamin Peterson <benjamin@python.org>
|
|
|
925e6b |
# Date 1401567982 25200
|
|
|
925e6b |
# Node ID e4da3ba9dcac4374ca0ccc46a48c32be6f951038
|
|
|
925e6b |
# Parent 8fa8c290c165dccd613632b69a816623b51e801e
|
|
|
925e6b |
backport hashlib.pbkdf2_hmac per PEP 466 (closes #21304)
|
|
|
925e6b |
|
|
|
925e6b |
Backport by Alex Gaynor.
|
|
|
925e6b |
|
|
|
925e6b |
diff --git a/Doc/library/hashlib.rst b/Doc/library/hashlib.rst
|
|
|
925e6b |
--- a/Doc/library/hashlib.rst
|
|
|
925e6b |
+++ b/Doc/library/hashlib.rst
|
|
|
925e6b |
@@ -135,6 +135,46 @@ A hash object has the following methods:
|
|
|
925e6b |
compute the digests of strings that share a common initial substring.
|
|
|
925e6b |
|
|
|
925e6b |
|
|
|
925e6b |
+Key Derivation Function
|
|
|
925e6b |
+-----------------------
|
|
|
925e6b |
+
|
|
|
925e6b |
+Key derivation and key stretching algorithms are designed for secure password
|
|
|
925e6b |
+hashing. Naive algorithms such as ``sha1(password)`` are not resistant against
|
|
|
925e6b |
+brute-force attacks. A good password hashing function must be tunable, slow, and
|
|
|
925e6b |
+include a `salt <https://en.wikipedia.org/wiki/Salt_%28cryptography%29>`_.
|
|
|
925e6b |
+
|
|
|
925e6b |
+
|
|
|
925e6b |
+.. function:: pbkdf2_hmac(name, password, salt, rounds, dklen=None)
|
|
|
925e6b |
+
|
|
|
925e6b |
+ The function provides PKCS#5 password-based key derivation function 2. It
|
|
|
925e6b |
+ uses HMAC as pseudorandom function.
|
|
|
925e6b |
+
|
|
|
925e6b |
+ The string *name* is the desired name of the hash digest algorithm for
|
|
|
925e6b |
+ HMAC, e.g. 'sha1' or 'sha256'. *password* and *salt* are interpreted as
|
|
|
925e6b |
+ buffers of bytes. Applications and libraries should limit *password* to
|
|
|
925e6b |
+ a sensible value (e.g. 1024). *salt* should be about 16 or more bytes from
|
|
|
925e6b |
+ a proper source, e.g. :func:`os.urandom`.
|
|
|
925e6b |
+
|
|
|
925e6b |
+ The number of *rounds* should be chosen based on the hash algorithm and
|
|
|
925e6b |
+ computing power. As of 2013, at least 100,000 rounds of SHA-256 is suggested.
|
|
|
925e6b |
+
|
|
|
925e6b |
+ *dklen* is the length of the derived key. If *dklen* is ``None`` then the
|
|
|
925e6b |
+ digest size of the hash algorithm *name* is used, e.g. 64 for SHA-512.
|
|
|
925e6b |
+
|
|
|
925e6b |
+ >>> import hashlib, binascii
|
|
|
925e6b |
+ >>> dk = hashlib.pbkdf2_hmac('sha256', b'password', b'salt', 100000)
|
|
|
925e6b |
+ >>> binascii.hexlify(dk)
|
|
|
925e6b |
+ b'0394a2ede332c9a13eb82e9b24631604c31df978b4e2f0fbd2c549944f9d79a5'
|
|
|
925e6b |
+
|
|
|
925e6b |
+ .. versionadded:: 2.7.8
|
|
|
925e6b |
+
|
|
|
925e6b |
+ .. note::
|
|
|
925e6b |
+
|
|
|
925e6b |
+ A fast implementation of *pbkdf2_hmac* is available with OpenSSL. The
|
|
|
925e6b |
+ Python implementation uses an inline version of :mod:`hmac`. It is about
|
|
|
925e6b |
+ three times slower and doesn't release the GIL.
|
|
|
925e6b |
+
|
|
|
925e6b |
+
|
|
|
925e6b |
.. seealso::
|
|
|
925e6b |
|
|
|
925e6b |
Module :mod:`hmac`
|
|
|
925e6b |
diff --git a/Lib/hashlib.py b/Lib/hashlib.py
|
|
|
925e6b |
--- a/Lib/hashlib.py
|
|
|
925e6b |
+++ b/Lib/hashlib.py
|
|
|
925e6b |
@@ -77,7 +77,7 @@ __always_supported = ('md5', 'sha1', 'sh
|
|
|
925e6b |
|
|
|
925e6b |
algorithms = __always_supported
|
|
|
925e6b |
|
|
|
925e6b |
-__all__ = __always_supported + ('new', 'algorithms')
|
|
|
925e6b |
+__all__ = __always_supported + ('new', 'algorithms', 'pbkdf2_hmac')
|
|
|
925e6b |
|
|
|
925e6b |
|
|
|
925e6b |
def __get_openssl_constructor(name):
|
|
|
925e6b |
@@ -123,6 +123,72 @@ for __func_name in __always_supported:
|
|
|
925e6b |
import logging
|
|
|
925e6b |
logging.exception('code for hash %s was not found.', __func_name)
|
|
|
925e6b |
|
|
|
925e6b |
+try:
|
|
|
925e6b |
+ # OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA
|
|
|
925e6b |
+ from _hashlib import pbkdf2_hmac
|
|
|
925e6b |
+except ImportError:
|
|
|
925e6b |
+ import binascii
|
|
|
925e6b |
+ import struct
|
|
|
925e6b |
+
|
|
|
925e6b |
+ _trans_5C = b"".join(chr(x ^ 0x5C) for x in range(256))
|
|
|
925e6b |
+ _trans_36 = b"".join(chr(x ^ 0x36) for x in range(256))
|
|
|
925e6b |
+
|
|
|
925e6b |
+ def pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None):
|
|
|
925e6b |
+ """Password based key derivation function 2 (PKCS #5 v2.0)
|
|
|
925e6b |
+
|
|
|
925e6b |
+ This Python implementations based on the hmac module about as fast
|
|
|
925e6b |
+ as OpenSSL's PKCS5_PBKDF2_HMAC for short passwords and much faster
|
|
|
925e6b |
+ for long passwords.
|
|
|
925e6b |
+ """
|
|
|
925e6b |
+ if not isinstance(hash_name, str):
|
|
|
925e6b |
+ raise TypeError(hash_name)
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if not isinstance(password, (bytes, bytearray)):
|
|
|
925e6b |
+ password = bytes(buffer(password))
|
|
|
925e6b |
+ if not isinstance(salt, (bytes, bytearray)):
|
|
|
925e6b |
+ salt = bytes(buffer(salt))
|
|
|
925e6b |
+
|
|
|
925e6b |
+ # Fast inline HMAC implementation
|
|
|
925e6b |
+ inner = new(hash_name)
|
|
|
925e6b |
+ outer = new(hash_name)
|
|
|
925e6b |
+ blocksize = getattr(inner, 'block_size', 64)
|
|
|
925e6b |
+ if len(password) > blocksize:
|
|
|
925e6b |
+ password = new(hash_name, password).digest()
|
|
|
925e6b |
+ password = password + b'\x00' * (blocksize - len(password))
|
|
|
925e6b |
+ inner.update(password.translate(_trans_36))
|
|
|
925e6b |
+ outer.update(password.translate(_trans_5C))
|
|
|
925e6b |
+
|
|
|
925e6b |
+ def prf(msg, inner=inner, outer=outer):
|
|
|
925e6b |
+ # PBKDF2_HMAC uses the password as key. We can re-use the same
|
|
|
925e6b |
+ # digest objects and and just update copies to skip initialization.
|
|
|
925e6b |
+ icpy = inner.copy()
|
|
|
925e6b |
+ ocpy = outer.copy()
|
|
|
925e6b |
+ icpy.update(msg)
|
|
|
925e6b |
+ ocpy.update(icpy.digest())
|
|
|
925e6b |
+ return ocpy.digest()
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if iterations < 1:
|
|
|
925e6b |
+ raise ValueError(iterations)
|
|
|
925e6b |
+ if dklen is None:
|
|
|
925e6b |
+ dklen = outer.digest_size
|
|
|
925e6b |
+ if dklen < 1:
|
|
|
925e6b |
+ raise ValueError(dklen)
|
|
|
925e6b |
+
|
|
|
925e6b |
+ hex_format_string = "%%0%ix" % (new(hash_name).digest_size * 2)
|
|
|
925e6b |
+
|
|
|
925e6b |
+ dkey = b''
|
|
|
925e6b |
+ loop = 1
|
|
|
925e6b |
+ while len(dkey) < dklen:
|
|
|
925e6b |
+ prev = prf(salt + struct.pack(b'>I', loop))
|
|
|
925e6b |
+ rkey = int(binascii.hexlify(prev), 16)
|
|
|
925e6b |
+ for i in xrange(iterations - 1):
|
|
|
925e6b |
+ prev = prf(prev)
|
|
|
925e6b |
+ rkey ^= int(binascii.hexlify(prev), 16)
|
|
|
925e6b |
+ loop += 1
|
|
|
925e6b |
+ dkey += binascii.unhexlify(hex_format_string % rkey)
|
|
|
925e6b |
+
|
|
|
925e6b |
+ return dkey[:dklen]
|
|
|
925e6b |
+
|
|
|
925e6b |
# Cleanup locals()
|
|
|
925e6b |
del __always_supported, __func_name, __get_hash
|
|
|
925e6b |
del __hash_new, __get_openssl_constructor
|
|
|
925e6b |
diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py
|
|
|
925e6b |
--- a/Lib/test/test_hashlib.py
|
|
|
925e6b |
+++ b/Lib/test/test_hashlib.py
|
|
|
925e6b |
@@ -16,6 +16,8 @@ except ImportError:
|
|
|
925e6b |
threading = None
|
|
|
925e6b |
import unittest
|
|
|
925e6b |
import warnings
|
|
|
925e6b |
+from binascii import unhexlify
|
|
|
925e6b |
+
|
|
|
925e6b |
from test import test_support
|
|
|
925e6b |
from test.test_support import _4G, precisionbigmemtest
|
|
|
925e6b |
|
|
|
925e6b |
@@ -436,8 +438,72 @@ class HashLibTestCase(unittest.TestCase)
|
|
|
925e6b |
|
|
|
925e6b |
|
|
|
925e6b |
|
|
|
925e6b |
+class KDFTests(unittest.TestCase):
|
|
|
925e6b |
+ pbkdf2_test_vectors = [
|
|
|
925e6b |
+ (b'password', b'salt', 1, None),
|
|
|
925e6b |
+ (b'password', b'salt', 2, None),
|
|
|
925e6b |
+ (b'password', b'salt', 4096, None),
|
|
|
925e6b |
+ # too slow, it takes over a minute on a fast CPU.
|
|
|
925e6b |
+ #(b'password', b'salt', 16777216, None),
|
|
|
925e6b |
+ (b'passwordPASSWORDpassword', b'saltSALTsaltSALTsaltSALTsaltSALTsalt',
|
|
|
925e6b |
+ 4096, -1),
|
|
|
925e6b |
+ (b'pass\0word', b'sa\0lt', 4096, 16),
|
|
|
925e6b |
+ ]
|
|
|
925e6b |
+
|
|
|
925e6b |
+ pbkdf2_results = {
|
|
|
925e6b |
+ "sha1": [
|
|
|
925e6b |
+ # offical test vectors from RFC 6070
|
|
|
925e6b |
+ (unhexlify('0c60c80f961f0e71f3a9b524af6012062fe037a6'), None),
|
|
|
925e6b |
+ (unhexlify('ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957'), None),
|
|
|
925e6b |
+ (unhexlify('4b007901b765489abead49d926f721d065a429c1'), None),
|
|
|
925e6b |
+ #(unhexlify('eefe3d61cd4da4e4e9945b3d6ba2158c2634e984'), None),
|
|
|
925e6b |
+ (unhexlify('3d2eec4fe41c849b80c8d83662c0e44a8b291a964c'
|
|
|
925e6b |
+ 'f2f07038'), 25),
|
|
|
925e6b |
+ (unhexlify('56fa6aa75548099dcc37d7f03425e0c3'), None),],
|
|
|
925e6b |
+ "sha256": [
|
|
|
925e6b |
+ (unhexlify('120fb6cffcf8b32c43e7225256c4f837'
|
|
|
925e6b |
+ 'a86548c92ccc35480805987cb70be17b'), None),
|
|
|
925e6b |
+ (unhexlify('ae4d0c95af6b46d32d0adff928f06dd0'
|
|
|
925e6b |
+ '2a303f8ef3c251dfd6e2d85a95474c43'), None),
|
|
|
925e6b |
+ (unhexlify('c5e478d59288c841aa530db6845c4c8d'
|
|
|
925e6b |
+ '962893a001ce4e11a4963873aa98134a'), None),
|
|
|
925e6b |
+ #(unhexlify('cf81c66fe8cfc04d1f31ecb65dab4089'
|
|
|
925e6b |
+ # 'f7f179e89b3b0bcb17ad10e3ac6eba46'), None),
|
|
|
925e6b |
+ (unhexlify('348c89dbcbd32b2f32d814b8116e84cf2b17'
|
|
|
925e6b |
+ '347ebc1800181c4e2a1fb8dd53e1c635518c7dac47e9'), 40),
|
|
|
925e6b |
+ (unhexlify('89b69d0516f829893c696226650a8687'), None),],
|
|
|
925e6b |
+ "sha512": [
|
|
|
925e6b |
+ (unhexlify('867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5'
|
|
|
925e6b |
+ 'd513554e1c8cf252c02d470a285a0501bad999bfe943c08f'
|
|
|
925e6b |
+ '050235d7d68b1da55e63f73b60a57fce'), None),
|
|
|
925e6b |
+ (unhexlify('e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f004071'
|
|
|
925e6b |
+ '3f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82'
|
|
|
925e6b |
+ 'be67335c77a6068e04112754f27ccf4e'), None),
|
|
|
925e6b |
+ (unhexlify('d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f8'
|
|
|
925e6b |
+ '7f6902e072f457b5143f30602641b3d55cd335988cb36b84'
|
|
|
925e6b |
+ '376060ecd532e039b742a239434af2d5'), None),
|
|
|
925e6b |
+ (unhexlify('8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b8'
|
|
|
925e6b |
+ '68c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30'
|
|
|
925e6b |
+ '225c583a186cd82bd4daea9724a3d3b8'), 64),
|
|
|
925e6b |
+ (unhexlify('9d9e9c4cd21fe4be24d5b8244c759665'), None),],
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+
|
|
|
925e6b |
+ def test_pbkdf2_hmac(self):
|
|
|
925e6b |
+ for digest_name, results in self.pbkdf2_results.items():
|
|
|
925e6b |
+ for i, vector in enumerate(self.pbkdf2_test_vectors):
|
|
|
925e6b |
+ password, salt, rounds, dklen = vector
|
|
|
925e6b |
+ expected, overwrite_dklen = results[i]
|
|
|
925e6b |
+ if overwrite_dklen:
|
|
|
925e6b |
+ dklen = overwrite_dklen
|
|
|
925e6b |
+ out = hashlib.pbkdf2_hmac(
|
|
|
925e6b |
+ digest_name, password, salt, rounds, dklen)
|
|
|
925e6b |
+ self.assertEqual(out, expected,
|
|
|
925e6b |
+ (digest_name, password, salt, rounds, dklen))
|
|
|
925e6b |
+
|
|
|
925e6b |
+
|
|
|
925e6b |
+
|
|
|
925e6b |
def test_main():
|
|
|
925e6b |
- test_support.run_unittest(HashLibTestCase)
|
|
|
925e6b |
+ test_support.run_unittest(HashLibTestCase, KDFTests)
|
|
|
925e6b |
|
|
|
925e6b |
if __name__ == "__main__":
|
|
|
925e6b |
test_main()
|
|
|
925e6b |
diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c
|
|
|
925e6b |
--- a/Modules/_hashopenssl.c
|
|
|
925e6b |
+++ b/Modules/_hashopenssl.c
|
|
|
925e6b |
@@ -39,6 +39,7 @@
|
|
|
925e6b |
#include <openssl/ssl.h>
|
|
|
925e6b |
#include <openssl/err.h>
|
|
|
925e6b |
#include <openssl/evp.h>
|
|
|
925e6b |
+#include <openssl/hmac.h>
|
|
|
925e6b |
|
|
|
925e6b |
#define MUNCH_SIZE INT_MAX
|
|
|
925e6b |
|
|
|
925e6b |
@@ -563,6 +564,226 @@ EVP_new(PyObject *self, PyObject *args,
|
|
|
925e6b |
return ret_obj;
|
|
|
925e6b |
}
|
|
|
925e6b |
|
|
|
925e6b |
+
|
|
|
925e6b |
+
|
|
|
925e6b |
+#if (OPENSSL_VERSION_NUMBER >= 0x10000000 && !defined(OPENSSL_NO_HMAC) \
|
|
|
925e6b |
+ && !defined(OPENSSL_NO_SHA))
|
|
|
925e6b |
+
|
|
|
925e6b |
+#define PY_PBKDF2_HMAC 1
|
|
|
925e6b |
+
|
|
|
925e6b |
+/* Improved implementation of PKCS5_PBKDF2_HMAC()
|
|
|
925e6b |
+ *
|
|
|
925e6b |
+ * PKCS5_PBKDF2_HMAC_fast() hashes the password exactly one time instead of
|
|
|
925e6b |
+ * `iter` times. Today (2013) the iteration count is typically 100,000 or
|
|
|
925e6b |
+ * more. The improved algorithm is not subject to a Denial-of-Service
|
|
|
925e6b |
+ * vulnerability with overly large passwords.
|
|
|
925e6b |
+ *
|
|
|
925e6b |
+ * Also OpenSSL < 1.0 don't provide PKCS5_PBKDF2_HMAC(), only
|
|
|
925e6b |
+ * PKCS5_PBKDF2_SHA1.
|
|
|
925e6b |
+ */
|
|
|
925e6b |
+static int
|
|
|
925e6b |
+PKCS5_PBKDF2_HMAC_fast(const char *pass, int passlen,
|
|
|
925e6b |
+ const unsigned char *salt, int saltlen,
|
|
|
925e6b |
+ int iter, const EVP_MD *digest,
|
|
|
925e6b |
+ int keylen, unsigned char *out)
|
|
|
925e6b |
+{
|
|
|
925e6b |
+ unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4];
|
|
|
925e6b |
+ int cplen, j, k, tkeylen, mdlen;
|
|
|
925e6b |
+ unsigned long i = 1;
|
|
|
925e6b |
+ HMAC_CTX hctx_tpl, hctx;
|
|
|
925e6b |
+
|
|
|
925e6b |
+ mdlen = EVP_MD_size(digest);
|
|
|
925e6b |
+ if (mdlen < 0)
|
|
|
925e6b |
+ return 0;
|
|
|
925e6b |
+
|
|
|
925e6b |
+ HMAC_CTX_init(&hctx_tpl);
|
|
|
925e6b |
+ HMAC_CTX_init(&hctx);
|
|
|
925e6b |
+ p = out;
|
|
|
925e6b |
+ tkeylen = keylen;
|
|
|
925e6b |
+ if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) {
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx_tpl);
|
|
|
925e6b |
+ return 0;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ while(tkeylen) {
|
|
|
925e6b |
+ if(tkeylen > mdlen)
|
|
|
925e6b |
+ cplen = mdlen;
|
|
|
925e6b |
+ else
|
|
|
925e6b |
+ cplen = tkeylen;
|
|
|
925e6b |
+ /* We are unlikely to ever use more than 256 blocks (5120 bits!)
|
|
|
925e6b |
+ * but just in case...
|
|
|
925e6b |
+ */
|
|
|
925e6b |
+ itmp[0] = (unsigned char)((i >> 24) & 0xff);
|
|
|
925e6b |
+ itmp[1] = (unsigned char)((i >> 16) & 0xff);
|
|
|
925e6b |
+ itmp[2] = (unsigned char)((i >> 8) & 0xff);
|
|
|
925e6b |
+ itmp[3] = (unsigned char)(i & 0xff);
|
|
|
925e6b |
+ if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx_tpl);
|
|
|
925e6b |
+ return 0;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ if (!HMAC_Update(&hctx, salt, saltlen)
|
|
|
925e6b |
+ || !HMAC_Update(&hctx, itmp, 4)
|
|
|
925e6b |
+ || !HMAC_Final(&hctx, digtmp, NULL)) {
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx_tpl);
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx);
|
|
|
925e6b |
+ return 0;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx);
|
|
|
925e6b |
+ memcpy(p, digtmp, cplen);
|
|
|
925e6b |
+ for (j = 1; j < iter; j++) {
|
|
|
925e6b |
+ if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) {
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx_tpl);
|
|
|
925e6b |
+ return 0;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ if (!HMAC_Update(&hctx, digtmp, mdlen)
|
|
|
925e6b |
+ || !HMAC_Final(&hctx, digtmp, NULL)) {
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx_tpl);
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx);
|
|
|
925e6b |
+ return 0;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx);
|
|
|
925e6b |
+ for (k = 0; k < cplen; k++) {
|
|
|
925e6b |
+ p[k] ^= digtmp[k];
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ tkeylen-= cplen;
|
|
|
925e6b |
+ i++;
|
|
|
925e6b |
+ p+= cplen;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ HMAC_CTX_cleanup(&hctx_tpl);
|
|
|
925e6b |
+ return 1;
|
|
|
925e6b |
+}
|
|
|
925e6b |
+
|
|
|
925e6b |
+/* LCOV_EXCL_START */
|
|
|
925e6b |
+static PyObject *
|
|
|
925e6b |
+_setException(PyObject *exc)
|
|
|
925e6b |
+{
|
|
|
925e6b |
+ unsigned long errcode;
|
|
|
925e6b |
+ const char *lib, *func, *reason;
|
|
|
925e6b |
+
|
|
|
925e6b |
+ errcode = ERR_peek_last_error();
|
|
|
925e6b |
+ if (!errcode) {
|
|
|
925e6b |
+ PyErr_SetString(exc, "unknown reasons");
|
|
|
925e6b |
+ return NULL;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ ERR_clear_error();
|
|
|
925e6b |
+
|
|
|
925e6b |
+ lib = ERR_lib_error_string(errcode);
|
|
|
925e6b |
+ func = ERR_func_error_string(errcode);
|
|
|
925e6b |
+ reason = ERR_reason_error_string(errcode);
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if (lib && func) {
|
|
|
925e6b |
+ PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ else if (lib) {
|
|
|
925e6b |
+ PyErr_Format(exc, "[%s] %s", lib, reason);
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ else {
|
|
|
925e6b |
+ PyErr_SetString(exc, reason);
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ return NULL;
|
|
|
925e6b |
+}
|
|
|
925e6b |
+/* LCOV_EXCL_STOP */
|
|
|
925e6b |
+
|
|
|
925e6b |
+PyDoc_STRVAR(pbkdf2_hmac__doc__,
|
|
|
925e6b |
+"pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\
|
|
|
925e6b |
+\n\
|
|
|
925e6b |
+Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\
|
|
|
925e6b |
+pseudorandom function.");
|
|
|
925e6b |
+
|
|
|
925e6b |
+static PyObject *
|
|
|
925e6b |
+pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
|
|
|
925e6b |
+{
|
|
|
925e6b |
+ static char *kwlist[] = {"hash_name", "password", "salt", "iterations",
|
|
|
925e6b |
+ "dklen", NULL};
|
|
|
925e6b |
+ PyObject *key_obj = NULL, *dklen_obj = Py_None;
|
|
|
925e6b |
+ char *name, *key;
|
|
|
925e6b |
+ Py_buffer password, salt;
|
|
|
925e6b |
+ long iterations, dklen;
|
|
|
925e6b |
+ int retval;
|
|
|
925e6b |
+ const EVP_MD *digest;
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ss*s*l|O:pbkdf2_hmac",
|
|
|
925e6b |
+ kwlist, &name, &password, &salt,
|
|
|
925e6b |
+ &iterations, &dklen_obj)) {
|
|
|
925e6b |
+ return NULL;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+
|
|
|
925e6b |
+ digest = EVP_get_digestbyname(name);
|
|
|
925e6b |
+ if (digest == NULL) {
|
|
|
925e6b |
+ PyErr_SetString(PyExc_ValueError, "unsupported hash type");
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if (password.len > INT_MAX) {
|
|
|
925e6b |
+ PyErr_SetString(PyExc_OverflowError,
|
|
|
925e6b |
+ "password is too long.");
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if (salt.len > INT_MAX) {
|
|
|
925e6b |
+ PyErr_SetString(PyExc_OverflowError,
|
|
|
925e6b |
+ "salt is too long.");
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if (iterations < 1) {
|
|
|
925e6b |
+ PyErr_SetString(PyExc_ValueError,
|
|
|
925e6b |
+ "iteration value must be greater than 0.");
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ if (iterations > INT_MAX) {
|
|
|
925e6b |
+ PyErr_SetString(PyExc_OverflowError,
|
|
|
925e6b |
+ "iteration value is too great.");
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if (dklen_obj == Py_None) {
|
|
|
925e6b |
+ dklen = EVP_MD_size(digest);
|
|
|
925e6b |
+ } else {
|
|
|
925e6b |
+ dklen = PyLong_AsLong(dklen_obj);
|
|
|
925e6b |
+ if ((dklen == -1) && PyErr_Occurred()) {
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ if (dklen < 1) {
|
|
|
925e6b |
+ PyErr_SetString(PyExc_ValueError,
|
|
|
925e6b |
+ "key length must be greater than 0.");
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ if (dklen > INT_MAX) {
|
|
|
925e6b |
+ /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
|
|
|
925e6b |
+ PyErr_SetString(PyExc_OverflowError,
|
|
|
925e6b |
+ "key length is too great.");
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+
|
|
|
925e6b |
+ key_obj = PyBytes_FromStringAndSize(NULL, dklen);
|
|
|
925e6b |
+ if (key_obj == NULL) {
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+ key = PyBytes_AS_STRING(key_obj);
|
|
|
925e6b |
+
|
|
|
925e6b |
+ Py_BEGIN_ALLOW_THREADS
|
|
|
925e6b |
+ retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, (int)password.len,
|
|
|
925e6b |
+ (unsigned char *)salt.buf, (int)salt.len,
|
|
|
925e6b |
+ iterations, digest, dklen,
|
|
|
925e6b |
+ (unsigned char *)key);
|
|
|
925e6b |
+ Py_END_ALLOW_THREADS
|
|
|
925e6b |
+
|
|
|
925e6b |
+ if (!retval) {
|
|
|
925e6b |
+ Py_CLEAR(key_obj);
|
|
|
925e6b |
+ _setException(PyExc_ValueError);
|
|
|
925e6b |
+ goto end;
|
|
|
925e6b |
+ }
|
|
|
925e6b |
+
|
|
|
925e6b |
+ end:
|
|
|
925e6b |
+ PyBuffer_Release(&password);
|
|
|
925e6b |
+ PyBuffer_Release(&salt);
|
|
|
925e6b |
+ return key_obj;
|
|
|
925e6b |
+}
|
|
|
925e6b |
+
|
|
|
925e6b |
+#endif
|
|
|
925e6b |
+
|
|
|
925e6b |
/*
|
|
|
925e6b |
* This macro and function generates a family of constructor function
|
|
|
925e6b |
* definitions for specific hash algorithms. These constructors are much
|
|
|
925e6b |
@@ -690,6 +911,10 @@ static struct PyMethodDef EVP_functions[
|
|
|
925e6b |
CONSTRUCTOR_METH_DEF(sha384),
|
|
|
925e6b |
CONSTRUCTOR_METH_DEF(sha512),
|
|
|
925e6b |
#endif
|
|
|
925e6b |
+#ifdef PY_PBKDF2_HMAC
|
|
|
925e6b |
+ {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS,
|
|
|
925e6b |
+ pbkdf2_hmac__doc__},
|
|
|
925e6b |
+#endif
|
|
|
925e6b |
{NULL, NULL} /* Sentinel */
|
|
|
925e6b |
};
|
|
|
925e6b |
|
|
|
925e6b |
diff -up Python-2.7.5/Lib/test/test_hmac.py.cod Python-2.7.5/Lib/test/test_hmac.py
|
|
|
925e6b |
--- Python-2.7.5/Lib/test/test_hmac.py.cod 2015-02-23 10:37:13.448594606 +0100
|
|
|
925e6b |
+++ Python-2.7.5/Lib/test/test_hmac.py 2015-02-23 10:37:27.581717509 +0100
|
|
|
925e6b |
@@ -1,3 +1,5 @@
|
|
|
925e6b |
+# coding: utf-8
|
|
|
925e6b |
+
|
|
|
925e6b |
import hmac
|
|
|
925e6b |
import hashlib
|
|
|
925e6b |
import unittest
|