Skip to content

Commit 20c22db

Browse files
authored
bpo-40671: Prepare _hashlib for PEP 489 (GH-20180)
1 parent 4cc2f93 commit 20c22db

File tree

2 files changed

+115
-67
lines changed

2 files changed

+115
-67
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prepare ``_hashlib`` for :pep:`489` and use :c:func:`PyModule_AddType`.

Modules/_hashopenssl.c

Lines changed: 114 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ get_hashlib_state(PyObject *module)
9595
return (_hashlibstate *)state;
9696
}
9797

98-
#define _hashlibstate_global ((_hashlibstate *)PyModule_GetState(PyState_FindModule(&_hashlibmodule)))
99-
100-
10198
typedef struct {
10299
PyObject_HEAD
103100
EVP_MD_CTX *ctx; /* OpenSSL message digest context */
@@ -1763,22 +1760,30 @@ _openssl_hash_name_mapper(const EVP_MD *md, const char *from,
17631760

17641761

17651762
/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
1766-
static PyObject*
1767-
generate_hash_name_list(void)
1763+
static int
1764+
hashlib_md_meth_names(PyObject *module)
17681765
{
1769-
_InternalNameMapperState state;
1770-
state.set = PyFrozenSet_New(NULL);
1771-
if (state.set == NULL)
1772-
return NULL;
1773-
state.error = 0;
1766+
_InternalNameMapperState state = {
1767+
.set = PyFrozenSet_New(NULL),
1768+
.error = 0
1769+
};
1770+
if (state.set == NULL) {
1771+
return -1;
1772+
}
17741773

17751774
EVP_MD_do_all(&_openssl_hash_name_mapper, &state);
17761775

17771776
if (state.error) {
17781777
Py_DECREF(state.set);
1779-
return NULL;
1778+
return -1;
17801779
}
1781-
return state.set;
1780+
1781+
if (PyModule_AddObject(module, "openssl_md_meth_names", state.set) < 0) {
1782+
Py_DECREF(state.set);
1783+
return -1;
1784+
}
1785+
1786+
return 0;
17821787
}
17831788

17841789
/* LibreSSL doesn't support FIPS:
@@ -1885,94 +1890,136 @@ hashlib_free(void *m)
18851890
hashlib_clear((PyObject *)m);
18861891
}
18871892

1893+
/* Py_mod_exec functions */
1894+
static int
1895+
hashlib_openssl_legacy_init(PyObject *module)
1896+
{
1897+
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1898+
/* Load all digest algorithms and initialize cpuid */
1899+
OPENSSL_add_all_algorithms_noconf();
1900+
ERR_load_crypto_strings();
1901+
#endif
1902+
return 0;
1903+
}
18881904

1889-
static struct PyModuleDef _hashlibmodule = {
1890-
PyModuleDef_HEAD_INIT,
1891-
"_hashlib",
1892-
NULL,
1893-
sizeof(_hashlibstate),
1894-
EVP_functions,
1895-
NULL,
1896-
hashlib_traverse,
1897-
hashlib_clear,
1898-
hashlib_free
1899-
};
1905+
static int
1906+
hashlib_init_evptype(PyObject *module)
1907+
{
1908+
_hashlibstate *state = get_hashlib_state(module);
19001909

1901-
PyMODINIT_FUNC
1902-
PyInit__hashlib(void)
1910+
state->EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec);
1911+
if (state->EVPtype == NULL) {
1912+
return -1;
1913+
}
1914+
if (PyModule_AddType(module, state->EVPtype) < 0) {
1915+
return -1;
1916+
}
1917+
return 0;
1918+
}
1919+
1920+
static int
1921+
hashlib_init_evpxoftype(PyObject *module)
19031922
{
1904-
PyObject *m, *openssl_md_meth_names;
1905-
_hashlibstate *state = NULL;
19061923
#ifdef PY_OPENSSL_HAS_SHAKE
1924+
_hashlibstate *state = get_hashlib_state(module);
19071925
PyObject *bases;
1926+
1927+
if (state->EVPtype == NULL) {
1928+
return -1;
1929+
}
1930+
1931+
bases = PyTuple_Pack(1, state->EVPtype);
1932+
if (bases == NULL) {
1933+
return -1;
1934+
}
1935+
1936+
state->EVPXOFtype = (PyTypeObject *)PyType_FromSpecWithBases(
1937+
&EVPXOFtype_spec, bases
1938+
);
1939+
Py_DECREF(bases);
1940+
if (state->EVPXOFtype == NULL) {
1941+
return -1;
1942+
}
1943+
if (PyModule_AddType(module, state->EVPXOFtype) < 0) {
1944+
return -1;
1945+
}
19081946
#endif
1947+
return 0;
1948+
}
19091949

1910-
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
1911-
/* Load all digest algorithms and initialize cpuid */
1912-
OPENSSL_add_all_algorithms_noconf();
1913-
ERR_load_crypto_strings();
1950+
static int
1951+
hashlib_init_hmactype(PyObject *module)
1952+
{
1953+
_hashlibstate *state = get_hashlib_state(module);
1954+
1955+
state->HMACtype = (PyTypeObject *)PyType_FromSpec(&HMACtype_spec);
1956+
if (state->HMACtype == NULL) {
1957+
return -1;
1958+
}
1959+
if (PyModule_AddType(module, state->HMACtype) < 0) {
1960+
return -1;
1961+
}
1962+
return 0;
1963+
}
1964+
1965+
#if 0
1966+
static PyModuleDef_Slot hashlib_slots[] = {
1967+
/* OpenSSL 1.0.2 and LibreSSL */
1968+
{Py_mod_exec, hashlib_openssl_legacy_init},
1969+
{Py_mod_exec, hashlib_init_evptype},
1970+
{Py_mod_exec, hashlib_init_evpxoftype},
1971+
{Py_mod_exec, hashlib_init_hmactype},
1972+
{Py_mod_exec, hashlib_md_meth_names},
1973+
{0, NULL}
1974+
};
19141975
#endif
19151976

1916-
m = PyState_FindModule(&_hashlibmodule);
1977+
static struct PyModuleDef _hashlibmodule = {
1978+
PyModuleDef_HEAD_INIT,
1979+
.m_name = "_hashlib",
1980+
.m_doc = "OpenSSL interface for hashlib module",
1981+
.m_size = sizeof(_hashlibstate),
1982+
.m_methods = EVP_functions,
1983+
.m_slots = NULL,
1984+
.m_traverse = hashlib_traverse,
1985+
.m_clear = hashlib_clear,
1986+
.m_free = hashlib_free
1987+
};
1988+
1989+
PyMODINIT_FUNC
1990+
PyInit__hashlib(void)
1991+
{
1992+
PyObject *m = PyState_FindModule(&_hashlibmodule);
19171993
if (m != NULL) {
19181994
Py_INCREF(m);
19191995
return m;
19201996
}
19211997

19221998
m = PyModule_Create(&_hashlibmodule);
1923-
if (m == NULL)
1924-
return NULL;
1925-
1926-
state = get_hashlib_state(m);
1927-
1928-
PyTypeObject *EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec);
1929-
if (EVPtype == NULL) {
1930-
Py_DECREF(m);
1999+
if (m == NULL) {
19312000
return NULL;
19322001
}
1933-
state->EVPtype = EVPtype;
1934-
Py_INCREF((PyObject *)state->EVPtype);
1935-
PyModule_AddObject(m, "HASH", (PyObject *)state->EVPtype);
19362002

1937-
PyTypeObject *HMACtype = (PyTypeObject *)PyType_FromSpec(&HMACtype_spec);
1938-
if (HMACtype == NULL) {
2003+
if (hashlib_openssl_legacy_init(m) < 0) {
19392004
Py_DECREF(m);
19402005
return NULL;
19412006
}
1942-
state->HMACtype = HMACtype;
1943-
Py_INCREF((PyObject *)state->HMACtype);
1944-
PyModule_AddObject(m, "HMAC", (PyObject *)state->HMACtype);
1945-
1946-
#ifdef PY_OPENSSL_HAS_SHAKE
1947-
bases = PyTuple_Pack(1, (PyObject *)EVPtype);
1948-
if (bases == NULL) {
2007+
if (hashlib_init_evptype(m) < 0) {
19492008
Py_DECREF(m);
19502009
return NULL;
19512010
}
1952-
PyTypeObject *EVPXOFtype = (PyTypeObject *)PyType_FromSpecWithBases(
1953-
&EVPXOFtype_spec, bases
1954-
);
1955-
Py_DECREF(bases);
1956-
if (EVPXOFtype == NULL) {
2011+
if (hashlib_init_evpxoftype(m) < 0) {
19572012
Py_DECREF(m);
19582013
return NULL;
19592014
}
1960-
state->EVPXOFtype = EVPXOFtype;
1961-
1962-
Py_INCREF((PyObject *)state->EVPXOFtype);
1963-
PyModule_AddObject(m, "HASHXOF", (PyObject *)state->EVPXOFtype);
1964-
#endif
1965-
1966-
openssl_md_meth_names = generate_hash_name_list();
1967-
if (openssl_md_meth_names == NULL) {
2015+
if (hashlib_init_hmactype(m) < 0) {
19682016
Py_DECREF(m);
19692017
return NULL;
19702018
}
1971-
if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) {
2019+
if (hashlib_md_meth_names(m) == -1) {
19722020
Py_DECREF(m);
19732021
return NULL;
19742022
}
19752023

1976-
PyState_AddModule(m, &_hashlibmodule);
19772024
return m;
19782025
}

0 commit comments

Comments
 (0)