22 * Written in 2013 by Dmitry Chestnykh <dmitry@codingrobots.com>
33 * Modified for CPython by Christian Heimes <christian@python.org>
44 * Updated to use HACL* by Jonathan Protzenko <jonathan@protzenko.fr>
5+ * Refactored by Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
56 *
67 * To the extent possible under law, the author have dedicated all
78 * copyright and related and neighboring rights to this software to
6465#include "_hacl/Hacl_Hash_Blake2b_Simd256.h"
6566#endif
6667
67- // MODULE TYPE SLOTS
68-
69- static PyType_Spec blake2b_type_spec ;
70- static PyType_Spec blake2s_type_spec ;
71-
72- PyDoc_STRVAR (blake2module__doc__ ,
73- "_blake2 provides BLAKE2b and BLAKE2s for hashlib\n" );
68+ // --- BLAKE-2 module state ---------------------------------------------------
7469
7570typedef struct {
7671 PyTypeObject * blake2b_type ;
@@ -87,15 +82,63 @@ get_blake2module_state(PyObject *module)
8782 return (blake2module_state * )state ;
8883}
8984
90- #if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
9185static inline blake2module_state *
92- blake2_get_state_from_type (PyTypeObject * module )
86+ get_blake2module_state_by_cls (PyTypeObject * cls )
9387{
94- void * state = _PyType_GetModuleState (module );
88+ void * state = _PyType_GetModuleState (cls );
9589 assert (state != NULL );
9690 return (blake2module_state * )state ;
9791}
92+
93+ // --- BLAKE-2 object ---------------------------------------------------------
94+
95+ // The HACL* API does not offer an agile API that can deal with either Blake2S
96+ // or Blake2B -- the reason is that the underlying states are optimized (uint32s
97+ // for S, uint64s for B). Therefore, we use a tagged union in this module to
98+ // correctly dispatch. Note that the previous incarnation of this code
99+ // transformed the Blake2b implementation into the Blake2s one using a script,
100+ // so this is an improvement.
101+ //
102+ // The 128 and 256 versions are only available if i) we were able to compile
103+ // them, and ii) if the CPU we run on also happens to have the right instruction
104+ // set.
105+ typedef enum { Blake2s , Blake2b , Blake2s_128 , Blake2b_256 } blake2_impl ;
106+
107+ typedef struct {
108+ PyObject_HEAD
109+ HASHLIB_MUTEX_API
110+ union {
111+ Hacl_Hash_Blake2s_state_t * blake2s_state ;
112+ Hacl_Hash_Blake2b_state_t * blake2b_state ;
113+ #if HACL_CAN_COMPILE_SIMD128
114+ Hacl_Hash_Blake2s_Simd128_state_t * blake2s_128_state ;
98115#endif
116+ #if HACL_CAN_COMPILE_SIMD256
117+ Hacl_Hash_Blake2b_Simd256_state_t * blake2b_256_state ;
118+ #endif
119+ };
120+ blake2_impl impl ;
121+ } Blake2Object ;
122+
123+ #define _Blake2Object_CAST (op ) ((Blake2Object *)(op))
124+
125+ // --- BLAKE-2 module clinic configuration ------------------------------------
126+
127+ /*[clinic input]
128+ module _blake2
129+ class _blake2.blake2b "Blake2Object *" "clinic_state()->blake2b_type"
130+ class _blake2.blake2s "Blake2Object *" "clinic_state()->blake2s_type"
131+ [clinic start generated code]*/
132+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7e2b2b3b67a72f18]*/
133+
134+ #define clinic_state () (get_blake2module_state_by_cls(Py_TYPE(self)))
135+ #include "clinic/blake2module.c.h"
136+ #undef clinic_state
137+
138+ // MODULE TYPE SLOTS
139+
140+ static PyType_Spec blake2b_type_spec ;
141+ static PyType_Spec blake2s_type_spec ;
99142
100143static int
101144blake2module_traverse (PyObject * module , visitproc visit , void * arg )
@@ -280,6 +323,9 @@ static PyModuleDef_Slot blake2module_slots[] = {
280323 {0 , NULL }
281324};
282325
326+ PyDoc_STRVAR (blake2module__doc__ ,
327+ "_blake2 provides BLAKE2b and BLAKE2s for hashlib\n" );
328+
283329static struct PyModuleDef blake2module_def = {
284330 PyModuleDef_HEAD_INIT ,
285331 .m_name = "_blake2" ,
@@ -299,18 +345,6 @@ PyInit__blake2(void)
299345
300346// IMPLEMENTATION OF METHODS
301347
302- // The HACL* API does not offer an agile API that can deal with either Blake2S
303- // or Blake2B -- the reason is that the underlying states are optimized (uint32s
304- // for S, uint64s for B). Therefore, we use a tagged union in this module to
305- // correctly dispatch. Note that the previous incarnation of this code
306- // transformed the Blake2b implementation into the Blake2s one using a script,
307- // so this is an improvement.
308- //
309- // The 128 and 256 versions are only available if i) we were able to compile
310- // them, and ii) if the CPU we run on also happens to have the right instruction
311- // set.
312- typedef enum { Blake2s , Blake2b , Blake2s_128 , Blake2b_256 } blake2_impl ;
313-
314348static inline bool
315349is_blake2b (blake2_impl impl )
316350{
@@ -327,7 +361,7 @@ static inline blake2_impl
327361type_to_impl (PyTypeObject * type )
328362{
329363#if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
330- blake2module_state * state = blake2_get_state_from_type (type );
364+ blake2module_state * state = get_blake2module_state_by_cls (type );
331365#endif
332366 if (!strcmp (type -> tp_name , blake2b_type_spec .name )) {
333367#if HACL_CAN_COMPILE_SIMD256
@@ -346,34 +380,6 @@ type_to_impl(PyTypeObject *type)
346380 Py_UNREACHABLE ();
347381}
348382
349- typedef struct {
350- PyObject_HEAD
351- HASHLIB_MUTEX_API
352- union {
353- Hacl_Hash_Blake2s_state_t * blake2s_state ;
354- Hacl_Hash_Blake2b_state_t * blake2b_state ;
355- #if HACL_CAN_COMPILE_SIMD128
356- Hacl_Hash_Blake2s_Simd128_state_t * blake2s_128_state ;
357- #endif
358- #if HACL_CAN_COMPILE_SIMD256
359- Hacl_Hash_Blake2b_Simd256_state_t * blake2b_256_state ;
360- #endif
361- };
362- blake2_impl impl ;
363- } Blake2Object ;
364-
365- #define _Blake2Object_CAST (op ) ((Blake2Object *)(op))
366-
367- #include "clinic/blake2module.c.h"
368-
369- /*[clinic input]
370- module _blake2
371- class _blake2.blake2b "Blake2Object *" "&PyBlake2_BLAKE2bType"
372- class _blake2.blake2s "Blake2Object *" "&PyBlake2_BLAKE2sType"
373- [clinic start generated code]*/
374- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b7526666bd18af83]*/
375-
376-
377383static Blake2Object *
378384new_Blake2Object (PyTypeObject * type )
379385{
0 commit comments