@@ -395,26 +395,33 @@ new_Blake2Object(PyTypeObject *type)
395395 * 64 bits so we loop in <4gig chunks when needed. */ 
396396
397397#if  PY_SSIZE_T_MAX  >  UINT32_MAX 
398- #define  HACL_UPDATE_LOOP (update ,state ,buf ,len ) \
399-   while (len > UINT32_MAX) { \
400-     update(state, buf, UINT32_MAX); \
401-     len -= UINT32_MAX; \
402-     buf += UINT32_MAX; \
403-   }
398+ #  define  HACL_UPDATE_LOOP (UPDATE_FUNC , STATE , BUF , LEN )    \
399+     do {                                                    \
400+         while (LEN > UINT32_MAX) {                          \
401+             (void)UPDATE_FUNC(STATE, BUF, UINT32_MAX);      \
402+             LEN -= UINT32_MAX;                              \
403+             BUF += UINT32_MAX;                              \
404+         }                                                   \
405+     } while (0)
404406#else 
405- #define  HACL_UPDATE_LOOP (update , state , buf , len )
407+ #   define  HACL_UPDATE_LOOP (... )
406408#endif 
407409
408- #define  HACL_UPDATE (update ,state ,buf ,len ) do { \
409-   /* Note: we explicitly ignore the error code on the basis that it would take > 
410-    * 1 billion years to overflow the maximum admissible length for SHA2-256 
411-    * (namely, 2^61-1 bytes). */  \
412-   HACL_UPDATE_LOOP (update ,state ,buf ,len ) \
413-   /* Cast to uint32_t is safe: len <= UINT32_MAX at this point. */  \
414-   update (state , buf , (uint32_t ) len ); \
415- } while  (0 )
410+ /* 
411+  * Note: we explicitly ignore the error code on the basis that it would take 
412+  * more than 1 billion years to overflow the maximum admissible length for 
413+  * blake2b/2s (2^64 - 1). 
414+  */ 
415+ #define  HACL_UPDATE (UPDATE_FUNC , STATE , BUF , LEN )               \
416+     do {                                                        \
417+         HACL_UPDATE_LOOP(UPDATE_FUNC, STATE, BUF, LEN);         \
418+         assert((Py_ssize_t)(LEN) <= (Py_ssize_t)UINT32_MAX);    \
419+         (void)UPDATE_FUNC(STATE, BUF, (uint32_t)LEN);           \
420+     } while (0)
416421
417- static  void  update (Blake2Object  * self , uint8_t  * buf , Py_ssize_t  len ) {
422+ static  void 
423+ update (Blake2Object  * self , uint8_t  * buf , Py_ssize_t  len )
424+ {
418425    switch  (self -> impl ) {
419426      // These need to be ifdef'd out otherwise it's an unresolved symbol at 
420427      // link-time. 
@@ -583,21 +590,41 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
583590
584591    switch  (self -> impl ) {
585592#if  HACL_CAN_COMPILE_SIMD256 
586-         case  Blake2b_256 :
593+         case  Blake2b_256 : { 
587594            self -> blake2b_256_state  =  Hacl_Hash_Blake2b_Simd256_malloc_with_params_and_key (& params , last_node , key -> buf );
595+             if  (self -> blake2b_256_state  ==  NULL ) {
596+                 (void )PyErr_NoMemory ();
597+                 goto error ;
598+             }
588599            break ;
600+         }
589601#endif 
590602#if  HACL_CAN_COMPILE_SIMD128 
591-         case  Blake2s_128 :
603+         case  Blake2s_128 : { 
592604            self -> blake2s_128_state  =  Hacl_Hash_Blake2s_Simd128_malloc_with_params_and_key (& params , last_node , key -> buf );
605+             if  (self -> blake2s_128_state  ==  NULL ) {
606+                 (void )PyErr_NoMemory ();
607+                 goto error ;
608+             }
593609            break ;
610+         }
594611#endif 
595-         case  Blake2b :
612+         case  Blake2b : { 
596613            self -> blake2b_state  =  Hacl_Hash_Blake2b_malloc_with_params_and_key (& params , last_node , key -> buf );
614+             if  (self -> blake2b_state  ==  NULL ) {
615+                 (void )PyErr_NoMemory ();
616+                 goto error ;
617+             }
597618            break ;
598-         case  Blake2s :
619+         }
620+         case  Blake2s : {
599621            self -> blake2s_state  =  Hacl_Hash_Blake2s_malloc_with_params_and_key (& params , last_node , key -> buf );
622+             if  (self -> blake2s_state  ==  NULL ) {
623+                 (void )PyErr_NoMemory ();
624+                 goto error ;
625+             }
600626            break ;
627+         }
601628        default :
602629            Py_UNREACHABLE ();
603630    }
@@ -610,7 +637,8 @@ py_blake2b_or_s_new(PyTypeObject *type, PyObject *data, int digest_size,
610637            Py_BEGIN_ALLOW_THREADS 
611638            update (self , buf .buf , buf .len );
612639            Py_END_ALLOW_THREADS 
613-         } else  {
640+         }
641+         else  {
614642            update (self , buf .buf , buf .len );
615643        }
616644        PyBuffer_Release (& buf );
@@ -688,44 +716,77 @@ py_blake2s_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
688716    return  py_blake2b_or_s_new (type , data , digest_size , key , salt , person , fanout , depth , leaf_size , node_offset , node_depth , inner_size , last_node , usedforsecurity );
689717}
690718
691- /*[clinic input] 
692- _blake2.blake2b.copy 
693- 
694- Return a copy of the hash object. 
695- [clinic start generated code]*/ 
696- 
697- static  PyObject  * 
698- _blake2_blake2b_copy_impl (Blake2Object  * self )
699- /*[clinic end generated code: output=622d1c56b91c50d8 input=e383c2d199fd8a2e]*/ 
719+ static  int 
720+ blake2_blake2b_copy_locked (Blake2Object  * self , Blake2Object  * cpy )
700721{
701-     Blake2Object  * cpy ;
702- 
703-     if  ((cpy  =  new_Blake2Object (Py_TYPE (self ))) ==  NULL )
704-         return  NULL ;
705- 
706-     ENTER_HASHLIB (self );
707722    switch  (self -> impl ) {
708723#if  HACL_CAN_COMPILE_SIMD256 
709-         case  Blake2b_256 :
724+         case  Blake2b_256 : { 
710725            cpy -> blake2b_256_state  =  Hacl_Hash_Blake2b_Simd256_copy (self -> blake2b_256_state );
726+             if  (cpy -> blake2b_256_state  ==  NULL ) {
727+                 goto error ;
728+             }
711729            break ;
730+         }
712731#endif 
713732#if  HACL_CAN_COMPILE_SIMD128 
714-         case  Blake2s_128 :
733+         case  Blake2s_128 : { 
715734            cpy -> blake2s_128_state  =  Hacl_Hash_Blake2s_Simd128_copy (self -> blake2s_128_state );
735+             if  (cpy -> blake2s_128_state  ==  NULL ) {
736+                 goto error ;
737+             }
716738            break ;
739+         }
717740#endif 
718-         case  Blake2b :
741+         case  Blake2b : { 
719742            cpy -> blake2b_state  =  Hacl_Hash_Blake2b_copy (self -> blake2b_state );
743+             if  (cpy -> blake2b_state  ==  NULL ) {
744+                 goto error ;
745+             }
720746            break ;
721-         case  Blake2s :
747+         }
748+         case  Blake2s : {
722749            cpy -> blake2s_state  =  Hacl_Hash_Blake2s_copy (self -> blake2s_state );
750+             if  (cpy -> blake2s_state  ==  NULL ) {
751+                 goto error ;
752+             }
723753            break ;
754+         }
724755        default :
725756            Py_UNREACHABLE ();
726757    }
727758    cpy -> impl  =  self -> impl ;
759+     return  0 ;
760+ 
761+ error :
762+     (void )PyErr_NoMemory ();
763+     return  -1 ;
764+ }
765+ 
766+ /*[clinic input] 
767+ _blake2.blake2b.copy 
768+ 
769+ Return a copy of the hash object. 
770+ [clinic start generated code]*/ 
771+ 
772+ static  PyObject  * 
773+ _blake2_blake2b_copy_impl (Blake2Object  * self )
774+ /*[clinic end generated code: output=622d1c56b91c50d8 input=e383c2d199fd8a2e]*/ 
775+ {
776+     Blake2Object  * cpy ;
777+ 
778+     if  ((cpy  =  new_Blake2Object (Py_TYPE (self ))) ==  NULL ) {
779+         return  NULL ;
780+     }
781+ 
782+     int  rc ;
783+     ENTER_HASHLIB (self );
784+     rc  =  blake2_blake2b_copy_locked (self , cpy );
728785    LEAVE_HASHLIB (self );
786+     if  (rc  <  0 ) {
787+         Py_DECREF (cpy );
788+         return  NULL ;
789+     }
729790    return  (PyObject  * )cpy ;
730791}
731792
0 commit comments