@@ -79,7 +79,7 @@ typedef struct {
79
79
bool can_run_simd256 ;
80
80
} Blake2State ;
81
81
82
- static inline Blake2State *
82
+ static inline Blake2State *
83
83
blake2_get_state (PyObject * module )
84
84
{
85
85
void * state = _PyModule_GetState (module );
@@ -88,7 +88,7 @@ blake2_get_state(PyObject *module)
88
88
}
89
89
90
90
#if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
91
- static inline Blake2State *
91
+ static inline Blake2State *
92
92
blake2_get_state_from_type (PyTypeObject * module )
93
93
{
94
94
void * state = _PyType_GetModuleState (module );
@@ -201,29 +201,32 @@ blake2module_init_cpu_features(Blake2State *state)
201
201
#endif
202
202
}
203
203
204
- #define ADD_INT (d , name , value ) do { \
205
- PyObject *x = PyLong_FromLong(value); \
206
- if (!x) \
207
- return -1; \
208
- if (PyDict_SetItemString(d, name, x) < 0) { \
209
- Py_DECREF(x); \
210
- return -1; \
211
- } \
212
- Py_DECREF(x); \
213
- } while(0)
214
-
215
- #define ADD_INT_CONST (NAME , VALUE ) do { \
216
- if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) { \
217
- return -1; \
218
- } \
219
- } while (0)
220
-
221
204
static int
222
205
blake2_exec (PyObject * m )
223
206
{
224
207
Blake2State * st = blake2_get_state (m );
225
208
blake2module_init_cpu_features (st );
226
209
210
+ #define ADD_INT (DICT , NAME , VALUE ) \
211
+ do { \
212
+ PyObject *x = PyLong_FromLong(VALUE); \
213
+ if (x == NULL) { \
214
+ return -1; \
215
+ } \
216
+ int rc = PyDict_SetItemString(DICT, NAME, x); \
217
+ Py_DECREF(x); \
218
+ if (rc < 0) { \
219
+ return -1; \
220
+ } \
221
+ } while(0)
222
+
223
+ #define ADD_INT_CONST (NAME , VALUE ) \
224
+ do { \
225
+ if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) { \
226
+ return -1; \
227
+ } \
228
+ } while (0)
229
+
227
230
ADD_INT_CONST ("_GIL_MINSIZE" , HASHLIB_GIL_MINSIZE );
228
231
229
232
st -> blake2b_type = (PyTypeObject * )PyType_FromModuleAndSpec (
@@ -232,7 +235,6 @@ blake2_exec(PyObject *m)
232
235
if (st -> blake2b_type == NULL ) {
233
236
return -1 ;
234
237
}
235
- /* BLAKE2b */
236
238
if (PyModule_AddType (m , st -> blake2b_type ) < 0 ) {
237
239
return -1 ;
238
240
}
@@ -252,9 +254,9 @@ blake2_exec(PyObject *m)
252
254
st -> blake2s_type = (PyTypeObject * )PyType_FromModuleAndSpec (
253
255
m , & blake2s_type_spec , NULL );
254
256
255
- if (NULL == st -> blake2s_type )
257
+ if (st -> blake2s_type == NULL ) {
256
258
return -1 ;
257
-
259
+ }
258
260
if (PyModule_AddType (m , st -> blake2s_type ) < 0 ) {
259
261
return -1 ;
260
262
}
@@ -270,12 +272,11 @@ blake2_exec(PyObject *m)
270
272
ADD_INT_CONST ("BLAKE2S_MAX_KEY_SIZE" , HACL_HASH_BLAKE2S_KEY_BYTES );
271
273
ADD_INT_CONST ("BLAKE2S_MAX_DIGEST_SIZE" , HACL_HASH_BLAKE2S_OUT_BYTES );
272
274
275
+ #undef ADD_INT_CONST
276
+ #undef ADD_INT
273
277
return 0 ;
274
278
}
275
279
276
- #undef ADD_INT
277
- #undef ADD_INT_CONST
278
-
279
280
static PyModuleDef_Slot _blake2_slots [] = {
280
281
{Py_mod_exec , blake2_exec },
281
282
{Py_mod_multiple_interpreters , Py_MOD_PER_INTERPRETER_GIL_SUPPORTED },
@@ -315,16 +316,21 @@ PyInit__blake2(void)
315
316
// set.
316
317
typedef enum { Blake2s , Blake2b , Blake2s_128 , Blake2b_256 } blake2_impl ;
317
318
318
- static inline bool is_blake2b (blake2_impl impl ) {
319
- return impl == Blake2b || impl == Blake2b_256 ;
319
+ static inline bool
320
+ is_blake2b (blake2_impl impl )
321
+ {
322
+ return impl == Blake2b || impl == Blake2b_256 ;
320
323
}
321
324
322
- static inline bool is_blake2s (blake2_impl impl ) {
323
- return !is_blake2b (impl );
325
+ static inline bool
326
+ is_blake2s (blake2_impl impl )
327
+ {
328
+ return impl == Blake2s || impl == Blake2s_128 ;
324
329
}
325
330
326
331
static inline blake2_impl
327
- type_to_impl (PyTypeObject * type ) {
332
+ type_to_impl (PyTypeObject * type )
333
+ {
328
334
#if defined(HACL_CAN_COMPILE_SIMD128 ) || defined(HACL_CAN_COMPILE_SIMD256 )
329
335
Blake2State * st = blake2_get_state_from_type (type );
330
336
#endif
@@ -423,19 +429,23 @@ update(Blake2Object *self, uint8_t *buf, Py_ssize_t len)
423
429
// otherwise this results in an unresolved symbol at link-time.
424
430
#if HACL_CAN_COMPILE_SIMD256
425
431
case Blake2b_256 :
426
- HACL_UPDATE (Hacl_Hash_Blake2b_Simd256_update ,self -> blake2b_256_state , buf , len );
432
+ HACL_UPDATE (Hacl_Hash_Blake2b_Simd256_update ,
433
+ self -> blake2b_256_state , buf , len );
427
434
return ;
428
435
#endif
429
436
#if HACL_CAN_COMPILE_SIMD128
430
437
case Blake2s_128 :
431
- HACL_UPDATE (Hacl_Hash_Blake2s_Simd128_update ,self -> blake2s_128_state , buf , len );
438
+ HACL_UPDATE (Hacl_Hash_Blake2s_Simd128_update ,
439
+ self -> blake2s_128_state , buf , len );
432
440
return ;
433
441
#endif
434
442
case Blake2b :
435
- HACL_UPDATE (Hacl_Hash_Blake2b_update ,self -> blake2b_state , buf , len );
443
+ HACL_UPDATE (Hacl_Hash_Blake2b_update ,
444
+ self -> blake2b_state , buf , len );
436
445
return ;
F438
437
446
case Blake2s :
438
- HACL_UPDATE (Hacl_Hash_Blake2s_update ,self -> blake2s_state , buf , len );
447
+ HACL_UPDATE (Hacl_Hash_Blake2s_update ,
448
+ self -> blake2s_state , buf , len );
439
449
return ;
440
450
default :
441
451
Py_UNREACHABLE ();
@@ -825,7 +835,8 @@ _blake2_blake2b_update_impl(Blake2Object *self, PyObject *data)
825
835
update (self , buf .buf , buf .len );
826
836
PyMutex_Unlock (& self -> mutex );
827
837
Py_END_ALLOW_THREADS
828
- } else {
838
+ }
839
+ else {
829
840
update (self , buf .buf , buf .len );
830
841
}
831
842
@@ -1021,7 +1032,7 @@ static PyType_Slot blake2b_type_slots[] = {
1021
1032
{Py_tp_methods , py_blake2b_methods },
1022
1033
{Py_tp_getset , py_blake2b_getsetters },
1023
1034
{Py_tp_new , py_blake2b_new },
1024
- {0 ,0 }
1035
+ {0 , 0 }
1025
1036
};
1026
1037
1027
1038
static PyType_Slot blake2s_type_slots [] = {
@@ -1034,20 +1045,20 @@ static PyType_Slot blake2s_type_slots[] = {
1034
1045
// only the constructor differs, so that it can receive a clinic-generated
1035
1046
// default digest length suitable for blake2s
1036
1047
{Py_tp_new , py_blake2s_new },
1037
- {0 ,0 }
1048
+ {0 , 0 }
1038
1049
};
1039
1050
1040
1051
static PyType_Spec blake2b_type_spec = {
1041
1052
.name = "_blake2.blake2b" ,
1042
- .basicsize = sizeof (Blake2Object ),
1053
+ .basicsize = sizeof (Blake2Object ),
1043
1054
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE
1044
1055
| Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HEAPTYPE ,
1045
1056
.slots = blake2b_type_slots
1046
1057
};
1047
1058
1048
1059
static PyType_Spec blake2s_type_spec = {
1049
1060
.name = "_blake2.blake2s" ,
1050
- .basicsize = sizeof (Blake2Object ),
1061
+ .basicsize = sizeof (Blake2Object ),
1051
1062
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE
1052
1063
| Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HEAPTYPE ,
1053
1064
.slots = blake2s_type_slots
0 commit comments