Blame SOURCES/00210-pep466-backport-hashlib.pbkdf2_hmac.patch

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