1515#endif
1616
1717#include "Python.h"
18+ #include "pycore_moduleobject.h" // _PyModule_GetState()
19+ #include "pycore_strhex.h" // _Py_strhex()
20+ #include "pycore_typeobject.h" // _PyType_GetModuleState()
21+
1822#include "hashlib.h"
19- #include "pycore_strhex.h" // _Py_strhex()
20- #include "pycore_typeobject.h"
21- #include "pycore_moduleobject.h"
2223
2324// QUICK CPU AUTODETECTION
2425//
6768
6869// MODULE TYPE SLOTS
6970
71+ static struct PyModuleDef blake2module_def ;
7072static PyType_Spec blake2b_type_spec ;
7173static PyType_Spec blake2s_type_spec ;
7274
@@ -78,23 +80,24 @@ typedef struct {
7880 PyTypeObject * blake2s_type ;
7981 bool can_run_simd128 ;
8082 bool can_run_simd256 ;
81- } Blake2State ;
83+ } blake2module_state ;
8284
83- static inline Blake2State *
84- blake2_get_state (PyObject * module )
85+ static inline blake2module_state *
86+ get_blake2module_state (PyObject * module )
8587{
8688 void * state = _PyModule_GetState (module );
8789 assert (state != NULL );
88- return (Blake2State * )state ;
90+ return (blake2module_state * )state ;
8991}
9092
9193#if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
92- static inline Blake2State *
93- blake2_get_state_from_type (PyTypeObject * module )
94+ static inline blake2module_state *
95+ get_blake2module_state_by_cls (PyTypeObject * cls )
9496{
95- void * state = _PyType_GetModuleState (module );
97+ _Py_hashlib_check_exported_type (cls , & blake2module_def );
98+ void * state = _PyType_GetModuleState (cls );
9699 assert (state != NULL );
97- return (Blake2State * )state ;
100+ return (blake2module_state * )state ;
98101}
99102#endif
100103
@@ -105,7 +108,7 @@ static struct PyMethodDef blake2mod_functions[] = {
105108static int
106109_blake2_traverse (PyObject * module , visitproc visit , void * arg )
107110{
108- Blake2State * state = blake2_get_state (module );
111+ blake2module_state * state = get_blake2module_state (module );
109112 Py_VISIT (state -> blake2b_type );
110113 Py_VISIT (state -> blake2s_type );
111114 return 0 ;
@@ -114,7 +117,7 @@ _blake2_traverse(PyObject *module, visitproc visit, void *arg)
114117static int
115118_blake2_clear (PyObject * module )
116119{
117- Blake2State * state = blake2_get_state (module );
120+ blake2module_state * state = get_blake2module_state (module );
118121 Py_CLEAR (state -> blake2b_type );
119122 Py_CLEAR (state -> blake2s_type );
120123 return 0 ;
@@ -127,7 +130,7 @@ _blake2_free(void *module)
127130}
128131
129132static void
130- blake2module_init_cpu_features (Blake2State * state )
133+ blake2module_init_cpu_features (blake2module_state * state )
131134{
132135 /* This must be kept in sync with hmacmodule_init_cpu_features()
133136 * in hmacmodule.c */
@@ -205,7 +208,7 @@ blake2module_init_cpu_features(Blake2State *state)
205208static int
206209blake2_exec (PyObject * m )
207210{
208- Blake2State * st = blake2_get_state (m );
211+ blake2module_state * st = get_blake2module_state (m );
209212 blake2module_init_cpu_features (st );
210213
211214#define ADD_INT (DICT , NAME , VALUE ) \
@@ -285,11 +288,11 @@ static PyModuleDef_Slot _blake2_slots[] = {
285288 {0 , NULL }
286289};
287290
288- static struct PyModuleDef blake2_module = {
291+ static struct PyModuleDef blake2module_def = {
289292 .m_base = PyModuleDef_HEAD_INIT ,
290293 .m_name = "_blake2" ,
291294 .m_doc = blake2mod__doc__ ,
292- .m_size = sizeof (Blake2State ),
295+ .m_size = sizeof (blake2module_state ),
293296 .m_methods = blake2mod_functions ,
294297 .m_slots = _blake2_slots ,
295298 .m_traverse = _blake2_traverse ,
@@ -300,7 +303,7 @@ static struct PyModuleDef blake2_module = {
300303PyMODINIT_FUNC
301304PyInit__blake2 (void )
302305{
303- return PyModuleDef_Init (& blake2_module );
306+ return PyModuleDef_Init (& blake2module_def );
304307}
305308
306309// IMPLEMENTATION OF METHODS
@@ -333,7 +336,7 @@ static inline blake2_impl
333336type_to_impl (PyTypeObject * type )
334337{
335338#if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
336- Blake2State * st = blake2_get_state_from_type (type );
339+ blake2module_state * st = get_blake2module_state_by_cls (type );
337340#endif
338341 if (!strcmp (type -> tp_name , blake2b_type_spec .name )) {
339342#if HACL_CAN_COMPILE_SIMD256
@@ -385,6 +388,7 @@ class _blake2.blake2s "Blake2Object *" "&PyType_Type"
385388static Blake2Object *
386389new_Blake2Object (PyTypeObject * type )
387390{
391+ _Py_hashlib_check_exported_type (type , & blake2module_def );
388392 Blake2Object * self = PyObject_GC_New (Blake2Object , type );
389393 if (self == NULL ) {
390394 return NULL ;
0 commit comments