3232#include "_hacl/Hacl_Hash_MD5.h"
3333
3434typedef struct {
35- HASHLIB_OBJECT_HEAD
35+ PyObject_HEAD
36+ HASHLIB_LOCK_HEAD
3637 Hacl_Hash_MD5_state_t * state ;
3738} MD5object ;
3839
@@ -181,7 +182,7 @@ static void
181182md5_update_state_with_lock (MD5object * self , uint8_t * buf , Py_ssize_t len )
182183{
183184 Py_BEGIN_ALLOW_THREADS
184- PyMutex_Lock (& self -> mutex ); // unconditionally acquire a lock
185+ PyMutex_Lock (& self -> mutex ); // unconditionally acquire a lock
185186 _hacl_md5_update (self -> state , buf , len );
186187 PyMutex_Unlock (& self -> mutex );
187188 Py_END_ALLOW_THREADS
@@ -190,7 +191,7 @@ md5_update_state_with_lock(MD5object *self, uint8_t *buf, Py_ssize_t len)
190191static void
191192md5_update_state_cond_lock (MD5object * self , uint8_t * buf , Py_ssize_t len )
192193{
193- ENTER_HASHLIB (self ); // conditionally acquire a lock
194+ ENTER_HASHLIB (self ); // conditionally acquire a lock
194195 _hacl_md5_update (self -> state , buf , len );
195196 LEAVE_HASHLIB (self );
196197}
@@ -200,10 +201,16 @@ md5_update_state(MD5object *self, uint8_t *buf, Py_ssize_t len)
200201{
201202 assert (buf != 0 );
202203 assert (len >= 0 );
203- if (len != 0 ) {
204- len < HASHLIB_GIL_MINSIZE
205- ? md5_update_state_cond_lock (self , buf , len )
206- : md5_update_state_with_lock (self , buf , len );
204+ if (len == 0 ) {
205+ return ;
206+ }
207+ if (len < HASHLIB_GIL_MINSIZE ) {
208+ md5_update_state_cond_lock (self , buf , len );
209+ }
210+ else {
211+ HASHLIB_SET_MUTEX_POLICY (self , 1 );
212+ md5_update_state_with_lock (self , buf , len );
213+ HASHLIB_SET_MUTEX_POLICY (self , 0 );
207214 }
208215}
209216
@@ -316,7 +323,7 @@ _md5_md5_impl(PyObject *module, PyObject *data, int usedforsecurity,
316323 Py_buffer buf ;
317324 GET_BUFFER_VIEW_OR_ERROR (string , & buf , goto error );
318325 if (buf .len >= HASHLIB_GIL_MINSIZE ) {
319- /* We do not initialize self->lock here as this is the constructor
326+ /* Do not use self->mutex here as this is the constructor
320327 * where it is not yet possible to have concurrent access. */
321328 Py_BEGIN_ALLOW_THREADS
322329 _hacl_md5_update (self -> state , buf .buf , buf .len );
0 commit comments