cpython: d926fa1a833c (original) (raw)

Mercurial > cpython

changeset 103127:d926fa1a833c

Issue #27928: Add scrypt (password-based key derivation function) to hashlib module (requires OpenSSL 1.1.0). [#27928]

Christian Heimes christian@python.org
date Tue, 06 Sep 2016 20:22:28 +0200
parents f586742e56cb
children ae03163b6378
files Doc/library/hashlib.rst Lib/hashlib.py Lib/test/test_hashlib.py Misc/NEWS Modules/_hashopenssl.c Modules/clinic/_hashopenssl.c.h
diffstat 6 files changed, 262 insertions(+), 0 deletions(-)[+] [-] Doc/library/hashlib.rst 17 Lib/hashlib.py 6 Lib/test/test_hashlib.py 47 Misc/NEWS 3 Modules/_hashopenssl.c 129 Modules/clinic/_hashopenssl.c.h 60

line wrap: on

line diff

--- a/Doc/library/hashlib.rst +++ b/Doc/library/hashlib.rst @@ -225,6 +225,23 @@ include a salt <https://en.wikipedia.or[](#l1.3) Python implementation uses an inline version of :mod:hmac`. It is about three times slower and doesn't release the GIL. +.. function:: scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64) +

.. seealso::

--- a/Lib/hashlib.py +++ b/Lib/hashlib.py @@ -202,6 +202,12 @@ except ImportError: return dkey[:dklen] +try:

+except ImportError:

+ for __func_name in __always_supported: # try them all, some may not work due to the OpenSSL

--- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -7,6 +7,7 @@ # import array +from binascii import unhexlify import hashlib import itertools import os @@ -447,6 +448,12 @@ class KDFTests(unittest.TestCase): (b'pass\0word', b'sa\0lt', 4096, 16), ]

@@ -526,5 +533,45 @@ class KDFTests(unittest.TestCase): self._test_pbkdf2_hmac(c_hashlib.pbkdf2_hmac)

+

+ + if name == "main": unittest.main()

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -85,6 +85,9 @@ Core and Builtins Library ------- +- Issue #27928: Add scrypt (password-based key derivation function) to

--- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -25,6 +25,12 @@ #include <openssl/objects.h> #include "openssl/err.h" +#include "clinic/_hashopenssl.c.h" +/[clinic input] +module _hashlib +[clinic start generated code]/ +/[clinic end generated code: output=da39a3ee5e6b4b0d input=c2b4ff081bac4be1]/ + #define MUNCH_SIZE INT_MAX #ifndef HASH_OBJ_CONSTRUCTOR @@ -713,6 +719,128 @@ pbkdf2_hmac(PyObject *self, PyObject ar #endif +#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(OPENSSL_NO_SCRYPT) && !defined(LIBRESSL_VERSION_NUMBER) +#define PY_SCRYPT 1 + +/[clinic input] +_hashlib.scrypt +

+ + +scrypt password-based key derivation function. +[clinic start generated code]*/ + +static PyObject * +_hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,

+/[clinic end generated code: output=14849e2aa2b7b46c input=48a7d63bf3f75c42]/ +{

+

+

+

+

+

+

+

+

+

+

+} +#endif + /* State for our callback function so that it can accumulate a result. */ typedef struct internal_name_mapper_state { PyObject *set; @@ -836,6 +964,7 @@ static struct PyMethodDef EVP_functions[ {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS, pbkdf2_hmac__doc_}, #endif

new file mode 100644 --- /dev/null +++ b/Modules/clinic/hashopenssl.c.h @@ -0,0 +1,60 @@ +/[clinic input] +preserve +[clinic start generated code]/ + +#if (OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(OPENSSL_NO_SCRYPT) && !defined(LIBRESSL_VERSION_NUMBER)) + +PyDoc_STRVAR(hashlib_scrypt__doc, +"scrypt($module, /, password, *, salt=None, n=None, r=None, p=None,\n" +" maxmem=0, dklen=64)\n" +"--\n" +"\n" +"scrypt password-based key derivation function."); + +#define _HASHLIB_SCRYPT_METHODDEF [](#l6.18)

+ +static PyObject * +_hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,

+ +static PyObject * +_hashlib_scrypt(PyObject *module, PyObject *args, PyObject *kwargs) +{

+

+ +exit:

+

+} + +#endif /* (OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(OPENSSL_NO_SCRYPT) && !defined(LIBRESSL_VERSION_NUMBER)) */ + +#ifndef _HASHLIB_SCRYPT_METHODDEF

+#endif /* !defined(_HASHLIB_SCRYPT_METHODDEF) / +/[clinic end generated code: output=8c5386789f77430a input=a9049054013a1b77]*/