@@ -95,9 +95,6 @@ get_hashlib_state(PyObject *module)
95
95
return (_hashlibstate * )state ;
96
96
}
97
97
98
- #define _hashlibstate_global ((_hashlibstate *)PyModule_GetState(PyState_FindModule(&_hashlibmodule)))
99
-
100
-
101
98
typedef struct {
102
99
PyObject_HEAD
103
100
EVP_MD_CTX * ctx ; /* OpenSSL message digest context */
@@ -1763,22 +1760,30 @@ _openssl_hash_name_mapper(const EVP_MD *md, const char *from,
1763
1760
1764
1761
1765
1762
/* 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 )
1768
1765
{
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
+ }
1774
1773
1775
1774
EVP_MD_do_all (& _openssl_hash_name_mapper , & state );
1776
1775
1777
1776
if (state .error ) {
1778
1777
Py_DECREF (state .set );
1779
- return NULL ;
1778
+ return -1 ;
1780
1779
}
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 ;
1782
1787
}
1783
1788
1784
1789
/* LibreSSL doesn't support FIPS:
@@ -1885,94 +1890,136 @@ hashlib_free(void *m)
1885
1890
hashlib_clear ((PyObject * )m );
1886
1891
}
1887
1892
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
+ }
1888
1904
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 );
1900
1909
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 )
1903
1922
{
1904
- PyObject * m , * openssl_md_meth_names ;
1905
- _hashlibstate * state = NULL ;
1906
1923
#ifdef PY_OPENSSL_HAS_SHAKE
1924
+ _hashlibstate * state = get_hashlib_state (module );
1907
1925
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
+ }
1908
1946
#endif
1947
+ return 0 ;
1948
+ }
1909
1949
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
+ };
1914
1975
#endif
1915
1976
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 );
1917
1993
if (m != NULL ) {
1918
1994
Py_INCREF (m );
1919
1995
return m ;
1920
1996
}
1921
1997
1922
1998
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 ) {
1931
2000
return NULL ;
1932
2001
}
1933
- state -> EVPtype = EVPtype ;
1934
- Py_INCREF ((PyObject * )state -> EVPtype );
1935
- PyModule_AddObject (m , "HASH" , (PyObject * )state -> EVPtype );
1936
2002
1937
- PyTypeObject * HMACtype = (PyTypeObject * )PyType_FromSpec (& HMACtype_spec );
1938
- if (HMACtype == NULL ) {
2003
+ if (hashlib_openssl_legacy_init (m ) < 0 ) {
1939
2004
Py_DECREF (m );
1940
2005
return NULL ;
1941
2006
}
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 ) {
1949
2008
Py_DECREF (m );
1950
2009
return NULL ;
1951
2010
}
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 ) {
1957
2012
Py_DECREF (m );
1958
2013
return NULL ;
1959
2014
}
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 ) {
1968
2016
Py_DECREF (m );
1969
2017
return NULL ;
1970
2018
}
1971
- if (PyModule_AddObject ( m , "openssl_md_meth_names" , openssl_md_meth_names ) ) {
2019
+ if (hashlib_md_meth_names ( m ) == -1 ) {
1972
2020
Py_DECREF (m );
1973
2021
return NULL ;
1974
2022
}
1975
2023
1976
- PyState_AddModule (m , & _hashlibmodule );
1977
2024
return m ;
1978
2025
}
0 commit comments