2121#endif
2222
2323#include "Python.h"
24- #include "pycore_moduleobject.h" // _PyModule_GetState()
25- #include "pycore_typeobject .h" // _PyType_GetModuleState ()
26- #include "pycore_strhex .h" // _Py_strhex ()
24+ #include "pycore_moduleobject.h" // _PyModule_GetState()
25+ #include "pycore_strhex .h" // _Py_strhex ()
26+ #include "pycore_typeobject .h" // _PyType_GetModuleState ()
2727
2828#include "hashlib.h"
2929
@@ -57,21 +57,32 @@ typedef struct {
5757
5858// --- Module state -----------------------------------------------------------
5959
60+ static struct PyModuleDef sha2module_def ;
61+
6062/* We shall use run-time type information in the remainder of this module to
6163 * tell apart SHA2-224 and SHA2-256 */
6264typedef struct {
63- PyTypeObject * sha224_type ;
64- PyTypeObject * sha256_type ;
65- PyTypeObject * sha384_type ;
66- PyTypeObject * sha512_type ;
67- } sha2_state ;
65+ PyTypeObject * sha224_type ;
66+ PyTypeObject * sha256_type ;
67+ PyTypeObject * sha384_type ;
68+ PyTypeObject * sha512_type ;
69+ } sha2module_state ;
6870
69- static inline sha2_state *
70- sha2_get_state (PyObject * module )
71+ static inline sha2module_state *
72+ get_sha2module_state (PyObject * module )
7173{
7274 void * state = _PyModule_GetState (module );
7375 assert (state != NULL );
74- return (sha2_state * )state ;
76+ return (sha2module_state * )state ;
77+ }
78+
79+ static inline sha2module_state *
80+ get_sha2module_state_by_cls (PyTypeObject * cls )
81+ {
82+ _Py_hashlib_check_exported_type (cls , & sha2module_def );
83+ void * state = _PyType_GetModuleState (cls );
84+ assert (state != NULL );
85+ return (sha2module_state * )state ;
7586}
7687
7788// --- Module clinic configuration --------------------------------------------
@@ -112,7 +123,7 @@ SHA512copy(SHA512object *src, SHA512object *dest)
112123}
113124
114125static SHA256object *
115- newSHA224object (sha2_state * state )
126+ newSHA224object (sha2module_state * state )
116127{
117128 SHA256object * sha = PyObject_GC_New (SHA256object , state -> sha224_type );
118129 if (!sha ) {
@@ -125,7 +136,7 @@ newSHA224object(sha2_state *state)
125136}
126137
127138static SHA256object *
128- newSHA256object (sha2_state * state )
139+ newSHA256object (sha2module_state * state )
129140{
130141 SHA256object * sha = PyObject_GC_New (SHA256object , state -> sha256_type );
131142 if (!sha ) {
@@ -138,7 +149,7 @@ newSHA256object(sha2_state *state)
138149}
139150
140151static SHA512object *
141- newSHA384object (sha2_state * state )
152+ newSHA384object (sha2module_state * state )
142153{
143154 SHA512object * sha = PyObject_GC_New (SHA512object , state -> sha384_type );
144155 if (!sha ) {
@@ -151,7 +162,7 @@ newSHA384object(sha2_state *state)
151162}
152163
153164static SHA512object *
154- newSHA512object (sha2_state * state )
165+ newSHA512object (sha2module_state * state )
155166{
156167 SHA512object * sha = PyObject_GC_New (SHA512object , state -> sha512_type );
157168 if (!sha ) {
@@ -247,24 +258,23 @@ update_512(Hacl_Hash_SHA2_state_t_512 *state, uint8_t *buf, Py_ssize_t len)
247258/*[clinic input]
248259SHA256Type.copy
249260
250- cls:defining_class
251-
252261Return a copy of the hash object.
253262[clinic start generated code]*/
254263
255264static PyObject *
256- SHA256Type_copy_impl (SHA256object * self , PyTypeObject * cls )
257- /*[clinic end generated code: output=fabd515577805cd3 input=3137146fcb88e212 ]*/
265+ SHA256Type_copy_impl (SHA256object * self )
266+ /*[clinic end generated code: output=78d75a4f06724c3d input=f58840a618d4f2a7 ]*/
258267{
259268 int rc ;
260269 SHA256object * newobj ;
261- sha2_state * state = _PyType_GetModuleState ( cls );
270+ sha2module_state * state = get_sha2module_state_by_cls ( Py_TYPE ( self ) );
262271 if (Py_IS_TYPE (self , state -> sha256_type )) {
263272 if ((newobj = newSHA256object (state )) == NULL ) {
264273 return NULL ;
265274 }
266275 }
267276 else {
277+ assert (Py_IS_TYPE (self , state -> sha224_type ));
268278 if ((newobj = newSHA224object (state )) == NULL ) {
269279 return NULL ;
270280 }
@@ -283,25 +293,24 @@ SHA256Type_copy_impl(SHA256object *self, PyTypeObject *cls)
283293/*[clinic input]
284294SHA512Type.copy
285295
286- cls: defining_class
287-
288296Return a copy of the hash object.
289297[clinic start generated code]*/
290298
291299static PyObject *
292- SHA512Type_copy_impl (SHA512object * self , PyTypeObject * cls )
293- /*[clinic end generated code: output=66d2a8ef20de8302 input=f673a18f66527c90 ]*/
300+ SHA512Type_copy_impl (SHA512object * self )
301+ /*[clinic end generated code: output=7bebfed7849fc3ce input=9f5f31e6c457776a ]*/
294302{
295303 int rc ;
296304 SHA512object * newobj ;
297- sha2_state * state = _PyType_GetModuleState ( cls );
305+ sha2module_state * state = get_sha2module_state_by_cls ( Py_TYPE ( self ) );
298306
299- if (Py_IS_TYPE (( PyObject * ) self , state -> sha512_type )) {
307+ if (Py_IS_TYPE (self , state -> sha512_type )) {
300308 if ((newobj = newSHA512object (state )) == NULL ) {
301309 return NULL ;
302310 }
303311 }
304312 else {
313+ assert (Py_IS_TYPE (self , state -> sha384_type ));
305314 if ((newobj = newSHA384object (state )) == NULL ) {
306315 return NULL ;
307316 }
@@ -531,8 +540,6 @@ static PyType_Slot sha512_type_slots[] = {
531540 {0 ,0 }
532541};
533542
534- // Using _PyType_GetModuleState() on these types is safe since they
535- // cannot be subclassed: they don't have the Py_TPFLAGS_BASETYPE flag.
536543static PyType_Spec sha224_type_spec = {
537544 .name = "_sha2.SHA224Type" ,
538545 .basicsize = sizeof (SHA256object ),
@@ -593,7 +600,7 @@ _sha2_sha256_impl(PyObject *module, PyObject *data, int usedforsecurity,
593600 GET_BUFFER_VIEW_OR_ERROUT (string , & buf );
594601 }
595602
596- sha2_state * state = sha2_get_state (module );
603+ sha2module_state * state = get_sha2module_state (module );
597604
598605 SHA256object * new ;
599606 if ((new = newSHA256object (state )) == NULL ) {
@@ -652,7 +659,7 @@ _sha2_sha224_impl(PyObject *module, PyObject *data, int usedforsecurity,
652659 GET_BUFFER_VIEW_OR_ERROUT (string , & buf );
653660 }
654661
655- sha2_state * state = sha2_get_state (module );
662+ sha2module_state * state = get_sha2module_state (module );
656663 SHA256object * new ;
657664 if ((new = newSHA224object (state )) == NULL ) {
658665 if (string ) {
@@ -707,7 +714,7 @@ _sha2_sha512_impl(PyObject *module, PyObject *data, int usedforsecurity,
707714 return NULL ;
708715 }
709716
710- sha2_state * state = sha2_get_state (module );
717+ sha2module_state * state = get_sha2module_state (module );
711718
712719 if (string ) {
713720 GET_BUFFER_VIEW_OR_ERROUT (string , & buf );
@@ -766,7 +773,7 @@ _sha2_sha384_impl(PyObject *module, PyObject *data, int usedforsecurity,
766773 return NULL ;
767774 }
768775
769- sha2_state * state = sha2_get_state (module );
776+ sha2module_state * state = get_sha2module_state (module );
770777
771778 if (string ) {
772779 GET_BUFFER_VIEW_OR_ERROUT (string , & buf );
@@ -815,7 +822,7 @@ static struct PyMethodDef SHA2_functions[] = {
815822static int
816823_sha2_traverse (PyObject * module , visitproc visit , void * arg )
817824{
818- sha2_state * state = sha2_get_state (module );
825+ sha2module_state * state = get_sha2module_state (module );
819826 Py_VISIT (state -> sha224_type );
820827 Py_VISIT (state -> sha256_type );
821828 Py_VISIT (state -> sha384_type );
@@ -826,7 +833,7 @@ _sha2_traverse(PyObject *module, visitproc visit, void *arg)
826833static int
827834_sha2_clear (PyObject * module )
828835{
829- sha2_state * state = sha2_get_state (module );
836+ sha2module_state * state = get_sha2module_state (module );
830837 Py_CLEAR (state -> sha224_type );
831838 Py_CLEAR (state -> sha256_type );
832839 Py_CLEAR (state -> sha384_type );
@@ -843,7 +850,7 @@ _sha2_free(void *module)
843850/* Initialize this module. */
844851static int sha2_exec (PyObject * module )
845852{
846- sha2_state * state = sha2_get_state (module );
853+ sha2module_state * state = get_sha2module_state (module );
847854
848855 state -> sha224_type = (PyTypeObject * )PyType_FromModuleAndSpec (
849856 module , & sha224_type_spec , NULL );
@@ -896,10 +903,10 @@ static PyModuleDef_Slot _sha2_slots[] = {
896903 {0 , NULL }
897904};
898905
899- static struct PyModuleDef _sha2module = {
906+ static struct PyModuleDef sha2module_def = {
900907 PyModuleDef_HEAD_INIT ,
901908 .m_name = "_sha2" ,
902- .m_size = sizeof (sha2_state ),
909+ .m_size = sizeof (sha2module_state ),
903910 .m_methods = SHA2_functions ,
904911 .m_slots = _sha2_slots ,
905912 .m_traverse = _sha2_traverse ,
@@ -910,5 +917,5 @@ static struct PyModuleDef _sha2module = {
910917PyMODINIT_FUNC
911918PyInit__sha2 (void )
912919{
913- return PyModuleDef_Init (& _sha2module );
920+ return PyModuleDef_Init (& sha2module_def );
914921}
0 commit comments