|
25 | 25 | #include <openssl/objects.h> |
26 | 26 | #include "openssl/err.h" |
27 | 27 |
|
| 28 | +#include "clinic/_hashopenssl.c.h" |
| 29 | +/*[clinic input] |
| 30 | +module _hashlib |
| 31 | +[clinic start generated code]*/ |
| 32 | +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c2b4ff081bac4be1]*/ |
| 33 | + |
28 | 34 | #define MUNCH_SIZE INT_MAX |
29 | 35 |
|
30 | 36 | #ifndef HASH_OBJ_CONSTRUCTOR |
@@ -713,6 +719,128 @@ pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict) |
713 | 719 |
|
714 | 720 | #endif |
715 | 721 |
|
| 722 | +#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(OPENSSL_NO_SCRYPT) && !defined(LIBRESSL_VERSION_NUMBER) |
| 723 | +#define PY_SCRYPT 1 |
| 724 | + |
| 725 | +/*[clinic input] |
| 726 | +_hashlib.scrypt |
| 727 | +
|
| 728 | + password: Py_buffer |
| 729 | + * |
| 730 | + salt: Py_buffer = None |
| 731 | + n as n_obj: object(subclass_of='&PyLong_Type') = None |
| 732 | + r as r_obj: object(subclass_of='&PyLong_Type') = None |
| 733 | + p as p_obj: object(subclass_of='&PyLong_Type') = None |
| 734 | + maxmem: long = 0 |
| 735 | + dklen: long = 64 |
| 736 | +
|
| 737 | +
|
| 738 | +scrypt password-based key derivation function. |
| 739 | +[clinic start generated code]*/ |
| 740 | + |
| 741 | +static PyObject * |
| 742 | +_hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, |
| 743 | + PyObject *n_obj, PyObject *r_obj, PyObject *p_obj, |
| 744 | + long maxmem, long dklen) |
| 745 | +/*[clinic end generated code: output=14849e2aa2b7b46c input=48a7d63bf3f75c42]*/ |
| 746 | +{ |
| 747 | + PyObject *key_obj = NULL; |
| 748 | + char *key; |
| 749 | + int retval; |
| 750 | + unsigned long n, r, p; |
| 751 | + |
| 752 | + if (password->len > INT_MAX) { |
| 753 | + PyErr_SetString(PyExc_OverflowError, |
| 754 | + "password is too long."); |
| 755 | + return NULL; |
| 756 | + } |
| 757 | + |
| 758 | + if (salt->buf == NULL) { |
| 759 | + PyErr_SetString(PyExc_TypeError, |
| 760 | + "salt is required"); |
| 761 | + return NULL; |
| 762 | + } |
| 763 | + if (salt->len > INT_MAX) { |
| 764 | + PyErr_SetString(PyExc_OverflowError, |
| 765 | + "salt is too long."); |
| 766 | + return NULL; |
| 767 | + } |
| 768 | + |
| 769 | + n = PyLong_AsUnsignedLong(n_obj); |
| 770 | + if (n == (unsigned long) -1 && PyErr_Occurred()) { |
| 771 | + PyErr_SetString(PyExc_TypeError, |
| 772 | + "n is required and must be an unsigned int"); |
| 773 | + return NULL; |
| 774 | + } |
| 775 | + if (n < 2 || n & (n - 1)) { |
| 776 | + PyErr_SetString(PyExc_ValueError, |
| 777 | + "n must be a power of 2."); |
| 778 | + return NULL; |
| 779 | + } |
| 780 | + |
| 781 | + r = PyLong_AsUnsignedLong(r_obj); |
| 782 | + if (r == (unsigned long) -1 && PyErr_Occurred()) { |
| 783 | + PyErr_SetString(PyExc_TypeError, |
| 784 | + "r is required and must be an unsigned int"); |
| 785 | + return NULL; |
| 786 | + } |
| 787 | + |
| 788 | + p = PyLong_AsUnsignedLong(p_obj); |
| 789 | + if (p == (unsigned long) -1 && PyErr_Occurred()) { |
| 790 | + PyErr_SetString(PyExc_TypeError, |
| 791 | + "p is required and must be an unsigned int"); |
| 792 | + return NULL; |
| 793 | + } |
| 794 | + |
| 795 | + if (maxmem < 0 || maxmem > INT_MAX) { |
| 796 | + /* OpenSSL 1.1.0 restricts maxmem to 32MB. It may change in the |
| 797 | + future. The maxmem constant is private to OpenSSL. */ |
| 798 | + PyErr_Format(PyExc_ValueError, |
| 799 | + "maxmem must be positive and smaller than %d", |
| 800 | + INT_MAX); |
| 801 | + return NULL; |
| 802 | + } |
| 803 | + |
| 804 | + if (dklen < 1 || dklen > INT_MAX) { |
| 805 | + PyErr_Format(PyExc_ValueError, |
| 806 | + "dklen must be greater than 0 and smaller than %d", |
| 807 | + INT_MAX); |
| 808 | + return NULL; |
| 809 | + } |
| 810 | + |
| 811 | + /* let OpenSSL validate the rest */ |
| 812 | + retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0); |
| 813 | + if (!retval) { |
| 814 | + /* sorry, can't do much better */ |
| 815 | + PyErr_SetString(PyExc_ValueError, |
| 816 | + "Invalid paramemter combination for n, r, p, maxmem."); |
| 817 | + return NULL; |
| 818 | + } |
| 819 | + |
| 820 | + key_obj = PyBytes_FromStringAndSize(NULL, dklen); |
| 821 | + if (key_obj == NULL) { |
| 822 | + return NULL; |
| 823 | + } |
| 824 | + key = PyBytes_AS_STRING(key_obj); |
| 825 | + |
| 826 | + Py_BEGIN_ALLOW_THREADS |
| 827 | + retval = EVP_PBE_scrypt( |
| 828 | + (const char*)password->buf, (size_t)password->len, |
| 829 | + (const unsigned char *)salt->buf, (size_t)salt->len, |
| 830 | + n, r, p, maxmem, |
| 831 | + (unsigned char *)key, (size_t)dklen |
| 832 | + ); |
| 833 | + Py_END_ALLOW_THREADS |
| 834 | + |
| 835 | + if (!retval) { |
| 836 | + Py_CLEAR(key_obj); |
| 837 | + _setException(PyExc_ValueError); |
| 838 | + return NULL; |
| 839 | + } |
| 840 | + return key_obj; |
| 841 | +} |
| 842 | +#endif |
| 843 | + |
716 | 844 | /* State for our callback function so that it can accumulate a result. */ |
717 | 845 | typedef struct _internal_name_mapper_state { |
718 | 846 | PyObject *set; |
@@ -836,6 +964,7 @@ static struct PyMethodDef EVP_functions[] = { |
836 | 964 | {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS, |
837 | 965 | pbkdf2_hmac__doc__}, |
838 | 966 | #endif |
| 967 | + _HASHLIB_SCRYPT_METHODDEF |
839 | 968 | CONSTRUCTOR_METH_DEF(md5), |
840 | 969 | CONSTRUCTOR_METH_DEF(sha1), |
841 | 970 | CONSTRUCTOR_METH_DEF(sha224), |
|
0 commit comments