2
2
* Written in 2013 by Dmitry Chestnykh <dmitry@codingrobots.com>
3
3
* Modified for CPython by Christian Heimes <christian@python.org>
4
4
* Updated to use HACL* by Jonathan Protzenko <jonathan@protzenko.fr>
5
+ * Refactored by Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
5
6
*
6
7
* To the extent possible under law, the author have dedicated all
7
8
* copyright and related and neighboring rights to this software to
64
65
#include "_hacl/Hacl_Hash_Blake2b_Simd256.h"
65
66
#endif
66
67
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 ---------------------------------------------------
74
69
75
70
typedef struct {
76
71
PyTypeObject * blake2b_type ;
@@ -87,15 +82,63 @@ get_blake2module_state(PyObject *module)
87
82
return (blake2module_state * )state ;
88
83
}
89
84
90
- #if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
91
85
static inline blake2module_state *
92
- blake2_get_state_from_type (PyTypeObject * module )
86
+ get_blake2module_state_by_cls (PyTypeObject * cls )
93
87
{
94
- void * state = _PyType_GetModuleState (module );
88
+ void * state = _PyType_GetModuleState (cls );
95
89
assert (state != NULL );
96
90
return (blake2module_state * )state ;
97
91
}
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 ;
98
115
#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 ;
99
142
100
143
static int
101
144
blake2module_traverse (PyObject * module , visitproc visit , void * arg )
@@ -280,6 +323,9 @@ static PyModuleDef_Slot blake2module_slots[] = {
280
323
{0 , NULL }
281
324
};
282
325
326
+ PyDoc_STRVAR (blake2module__doc__ ,
327
+ "_blake2 provides BLAKE2b and BLAKE2s for hashlib\n" );
328
+
283
329
static struct PyModuleDef blake2module_def = {
284
330
PyModuleDef_HEAD_INIT ,
285
331
.m_name = "_blake2" ,
@@ -299,18 +345,6 @@ PyInit__blake2(void)
299
345
300
346
// IMPLEMENTATION OF METHODS
301
347
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
-
314
348
static inline bool
315
349
is_blake2b (blake2_impl impl )
316
350
{
@@ -327,7 +361,7 @@ static inline blake2_impl
327
361
type_to_impl (PyTypeObject * type )
328
362
{
329
363
#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 );
331
365
#endif
332
366
if (!strcmp (type -> tp_name , blake2b_type_spec .name )) {
333
367
#if HACL_CAN_COMPILE_SIMD256
@@ -346,34 +380,6 @@ type_to_impl(PyTypeObject *type)
346
380
Py_UNREACHABLE ();
347
381
}
348
382
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
-
377
383
static Blake2Object *
378
384
new_Blake2Object (PyTypeObject * type )
379
385
{
0 commit comments