diff --git a/Modules/blake2module.c b/Modules/blake2module.c index 2ce8c0cd3d7b6f..b0877fadc4f99a 100644 --- a/Modules/blake2module.c +++ b/Modules/blake2module.c @@ -2,6 +2,7 @@ * Written in 2013 by Dmitry Chestnykh * Modified for CPython by Christian Heimes * Updated to use HACL* by Jonathan Protzenko + * Refactored by Bénédikt Tran <10796600+picnixz@users.noreply.github.com> * * To the extent possible under law, the author have dedicated all * copyright and related and neighboring rights to this software to @@ -64,69 +65,107 @@ #include "_hacl/Hacl_Hash_Blake2b_Simd256.h" #endif -// MODULE TYPE SLOTS - -static PyType_Spec blake2b_type_spec; -static PyType_Spec blake2s_type_spec; - -PyDoc_STRVAR(blake2mod__doc__, - "_blake2 provides BLAKE2b and BLAKE2s for hashlib\n"); +// --- BLAKE-2 module state --------------------------------------------------- typedef struct { PyTypeObject *blake2b_type; PyTypeObject *blake2s_type; bool can_run_simd128; bool can_run_simd256; -} Blake2State; +} blake2module_state; -static inline Blake2State * -blake2_get_state(PyObject *module) +static inline blake2module_state * +get_blake2module_state(PyObject *module) { void *state = _PyModule_GetState(module); assert(state != NULL); - return (Blake2State *)state; + return (blake2module_state *)state; } -#if defined(HACL_CAN_COMPILE_SIMD128) || defined(HACL_CAN_COMPILE_SIMD256) -static inline Blake2State * -blake2_get_state_from_type(PyTypeObject *module) +static inline blake2module_state * +get_blake2module_state_by_cls(PyTypeObject *cls) { - void *state = _PyType_GetModuleState(module); + void *state = _PyType_GetModuleState(cls); assert(state != NULL); - return (Blake2State *)state; + return (blake2module_state *)state; } + +// --- BLAKE-2 object --------------------------------------------------------- + +// The HACL* API does not offer an agile API that can deal with either Blake2S +// or Blake2B -- the reason is that the underlying states are optimized (uint32s +// for S, uint64s for B). Therefore, we use a tagged union in this module to +// correctly dispatch. Note that the previous incarnation of this code +// transformed the Blake2b implementation into the Blake2s one using a script, +// so this is an improvement. +// +// The 128 and 256 versions are only available if i) we were able to compile +// them, and ii) if the CPU we run on also happens to have the right instruction +// set. +typedef enum { Blake2s, Blake2b, Blake2s_128, Blake2b_256 } blake2_impl; + +typedef struct { + PyObject_HEAD + HASHLIB_MUTEX_API + union { + Hacl_Hash_Blake2s_state_t *blake2s_state; + Hacl_Hash_Blake2b_state_t *blake2b_state; +#if HACL_CAN_COMPILE_SIMD128 + Hacl_Hash_Blake2s_Simd128_state_t *blake2s_128_state; #endif +#if HACL_CAN_COMPILE_SIMD256 + Hacl_Hash_Blake2b_Simd256_state_t *blake2b_256_state; +#endif + }; + blake2_impl impl; +} Blake2Object; -static struct PyMethodDef blake2mod_functions[] = { - {NULL, NULL} -}; +#define _Blake2Object_CAST(op) ((Blake2Object *)(op)) + +// --- BLAKE-2 module clinic configuration ------------------------------------ + +/*[clinic input] +module _blake2 +class _blake2.blake2b "Blake2Object *" "clinic_state()->blake2b_type" +class _blake2.blake2s "Blake2Object *" "clinic_state()->blake2s_type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7e2b2b3b67a72f18]*/ + +#define clinic_state() (get_blake2module_state_by_cls(Py_TYPE(self))) +#include "clinic/blake2module.c.h" +#undef clinic_state + +// MODULE TYPE SLOTS + +static PyType_Spec blake2b_type_spec; +static PyType_Spec blake2s_type_spec; static int -_blake2_traverse(PyObject *module, visitproc visit, void *arg) +blake2module_traverse(PyObject *module, visitproc visit, void *arg) { - Blake2State *state = blake2_get_state(module); + blake2module_state *state = get_blake2module_state(module); Py_VISIT(state->blake2b_type); Py_VISIT(state->blake2s_type); return 0; } static int -_blake2_clear(PyObject *module) +blake2module_clear(PyObject *module) { - Blake2State *state = blake2_get_state(module); + blake2module_state *state = get_blake2module_state(module); Py_CLEAR(state->blake2b_type); Py_CLEAR(state->blake2s_type); return 0; } static void -_blake2_free(void *module) +blake2module_free(void *module) { - (void)_blake2_clear((PyObject *)module); + (void)blake2module_clear((PyObject *)module); } static void -blake2module_init_cpu_features(Blake2State *state) +blake2module_init_cpu_features(blake2module_state *state) { /* This must be kept in sync with hmacmodule_init_cpu_features() * in hmacmodule.c */ @@ -202,10 +241,10 @@ blake2module_init_cpu_features(Blake2State *state) } static int -blake2_exec(PyObject *m) +blake2module_exec(PyObject *m) { - Blake2State *st = blake2_get_state(m); - blake2module_init_cpu_features(st); + blake2module_state *state = get_blake2module_state(m); + blake2module_init_cpu_features(state); #define ADD_INT(DICT, NAME, VALUE) \ do { \ @@ -229,17 +268,17 @@ blake2_exec(PyObject *m) ADD_INT_CONST("_GIL_MINSIZE", HASHLIB_GIL_MINSIZE); - st->blake2b_type = (PyTypeObject *)PyType_FromModuleAndSpec( + state->blake2b_type = (PyTypeObject *)PyType_FromModuleAndSpec( m, &blake2b_type_spec, NULL); - if (st->blake2b_type == NULL) { + if (state->blake2b_type == NULL) { return -1; } - if (PyModule_AddType(m, st->blake2b_type) < 0) { + if (PyModule_AddType(m, state->blake2b_type) < 0) { return -1; } - PyObject *d = st->blake2b_type->tp_dict; + PyObject *d = state->blake2b_type->tp_dict; ADD_INT(d, "SALT_SIZE", HACL_HASH_BLAKE2B_SALT_BYTES); ADD_INT(d, "PERSON_SIZE", HACL_HASH_BLAKE2B_PERSONAL_BYTES); ADD_INT(d, "MAX_KEY_SIZE", HACL_HASH_BLAKE2B_KEY_BYTES); @@ -251,17 +290,17 @@ blake2_exec(PyObject *m) ADD_INT_CONST("BLAKE2B_MAX_DIGEST_SIZE", HACL_HASH_BLAKE2B_OUT_BYTES); /* BLAKE2s */ - st->blake2s_type = (PyTypeObject *)PyType_FromModuleAndSpec( + state->blake2s_type = (PyTypeObject *)PyType_FromModuleAndSpec( m, &blake2s_type_spec, NULL); - if (st->blake2s_type == NULL) { + if (state->blake2s_type == NULL) { return -1; } - if (PyModule_AddType(m, st->blake2s_type) < 0) { + if (PyModule_AddType(m, state->blake2s_type) < 0) { return -1; } - d = st->blake2s_type->tp_dict; + d = state->blake2s_type->tp_dict; ADD_INT(d, "SALT_SIZE", HACL_HASH_BLAKE2S_SALT_BYTES); ADD_INT(d, "PERSON_SIZE", HACL_HASH_BLAKE2S_PERSONAL_BYTES); ADD_INT(d, "MAX_KEY_SIZE", HACL_HASH_BLAKE2S_KEY_BYTES); @@ -277,45 +316,35 @@ blake2_exec(PyObject *m) return 0; } -static PyModuleDef_Slot _blake2_slots[] = { - {Py_mod_exec, blake2_exec}, +static PyModuleDef_Slot blake2module_slots[] = { + {Py_mod_exec, blake2module_exec}, {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, {Py_mod_gil, Py_MOD_GIL_NOT_USED}, {0, NULL} }; -static struct PyModuleDef blake2_module = { - .m_base = PyModuleDef_HEAD_INIT, +PyDoc_STRVAR(blake2module__doc__, + "_blake2 provides BLAKE2b and BLAKE2s for hashlib\n"); + +static struct PyModuleDef blake2module_def = { + PyModuleDef_HEAD_INIT, .m_name = "_blake2", - .m_doc = blake2mod__doc__, - .m_size = sizeof(Blake2State), - .m_methods = blake2mod_functions, - .m_slots = _blake2_slots, - .m_traverse = _blake2_traverse, - .m_clear = _blake2_clear, - .m_free = _blake2_free, + .m_doc = blake2module__doc__, + .m_size = sizeof(blake2module_state), + .m_slots = blake2module_slots, + .m_traverse = blake2module_traverse, + .m_clear = blake2module_clear, + .m_free = blake2module_free, }; PyMODINIT_FUNC PyInit__blake2(void) { - return PyModuleDef_Init(&blake2_module); + return PyModuleDef_Init(&blake2module_def); } // IMPLEMENTATION OF METHODS -// The HACL* API does not offer an agile API that can deal with either Blake2S -// or Blake2B -- the reason is that the underlying states are optimized (uint32s -// for S, uint64s for B). Therefore, we use a tagged union in this module to -// correctly dispatch. Note that the previous incarnation of this code -// transformed the Blake2b implementation into the Blake2s one using a script, -// so this is an improvement. -// -// The 128 and 256 versions are only available if i) we were able to compile -// them, and ii) if the CPU we run on also happens to have the right instruction -// set. -typedef enum { Blake2s, Blake2b, Blake2s_128, Blake2b_256 } blake2_impl; - static inline bool is_blake2b(blake2_impl impl) { @@ -332,18 +361,18 @@ static inline blake2_impl type_to_impl(PyTypeObject *type) { #if defined(HACL_CAN_COMPILE_SIMD128) || defined(HACL_CAN_COMPILE_SIMD256) - Blake2State *st = blake2_get_state_from_type(type); + blake2module_state *state = get_blake2module_state_by_cls(type); #endif if (!strcmp(type->tp_name, blake2b_type_spec.name)) { #if HACL_CAN_COMPILE_SIMD256 - return st->can_run_simd256 ? Blake2b_256 : Blake2b; + return state->can_run_simd256 ? Blake2b_256 : Blake2b; #else return Blake2b; #endif } else if (!strcmp(type->tp_name, blake2s_type_spec.name)) { #if HACL_CAN_COMPILE_SIMD128 - return st->can_run_simd128 ? Blake2s_128 : Blake2s; + return state->can_run_simd128 ? Blake2s_128 : Blake2s; #else return Blake2s; #endif @@ -351,35 +380,6 @@ type_to_impl(PyTypeObject *type) Py_UNREACHABLE(); } -typedef struct { - PyObject_HEAD - union { - Hacl_Hash_Blake2s_state_t *blake2s_state; - Hacl_Hash_Blake2b_state_t *blake2b_state; -#if HACL_CAN_COMPILE_SIMD128 - Hacl_Hash_Blake2s_Simd128_state_t *blake2s_128_state; -#endif -#if HACL_CAN_COMPILE_SIMD256 - Hacl_Hash_Blake2b_Simd256_state_t *blake2b_256_state; -#endif - }; - blake2_impl impl; - bool use_mutex; - PyMutex mutex; -} Blake2Object; - -#define _Blake2Object_CAST(op) ((Blake2Object *)(op)) - -#include "clinic/blake2module.c.h" - -/*[clinic input] -module _blake2 -class _blake2.blake2b "Blake2Object *" "&PyBlake2_BLAKE2bType" -class _blake2.blake2s "Blake2Object *" "&PyBlake2_BLAKE2sType" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b7526666bd18af83]*/ - - static Blake2Object * new_Blake2Object(PyTypeObject *type) { diff --git a/Modules/hashlib.h b/Modules/hashlib.h index e82ec92be25c57..042ffd699decce 100644 --- a/Modules/hashlib.h +++ b/Modules/hashlib.h @@ -2,6 +2,14 @@ #include "pycore_lock.h" // PyMutex +#define HASHLIB_MUTEX_API \ + /* + * Attributes to prevent undefined behaviors + * via multiple threads entering the C API. + */ \ + bool use_mutex; \ + PyMutex mutex; + /* * Given a PyObject* obj, fill in the Py_buffer* viewp with the result * of PyObject_GetBuffer. Sets an exception and issues the erraction diff --git a/Modules/hmacmodule.c b/Modules/hmacmodule.c index b404d5732ec857..d789b976a1abfd 100644 --- a/Modules/hmacmodule.c +++ b/Modules/hmacmodule.c @@ -377,15 +377,13 @@ get_hmacmodule_state_by_cls(PyTypeObject *cls) return (hmacmodule_state *)state; } -// --- HMAC Object ------------------------------------------------------------ +// --- HMAC object ------------------------------------------------------------ typedef Hacl_Streaming_HMAC_agile_state HACL_HMAC_state; typedef struct HMACObject { PyObject_HEAD - - bool use_mutex; - PyMutex mutex; + HASHLIB_MUTEX_API // Hash function information PyObject *name; // rendered name (exact unicode object) @@ -1841,7 +1839,7 @@ static struct PyModuleDef_Slot hmacmodule_slots[] = { {0, NULL} /* sentinel */ }; -static struct PyModuleDef _hmacmodule = { +static struct PyModuleDef hmacmodule_def = { PyModuleDef_HEAD_INIT, .m_name = "_hmac", .m_size = sizeof(hmacmodule_state), @@ -1855,5 +1853,5 @@ static struct PyModuleDef _hmacmodule = { PyMODINIT_FUNC PyInit__hmac(void) { - return PyModuleDef_Init(&_hmacmodule); + return PyModuleDef_Init(&hmacmodule_def); } diff --git a/Modules/md5module.c b/Modules/md5module.c index 08dbcd2cbce844..cec29c1472900c 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -8,6 +8,7 @@ Andrew Kuchling (amk@amk.ca) Greg Stein (gstein@lyra.org) Trevor Perrin (trevp@trevp.net) + Bénédikt Tran (10796600+picnixz@users.noreply.github.com) Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) Licensed to PSF under a Contributor Agreement. @@ -23,49 +24,61 @@ #include "Python.h" #include "hashlib.h" -/*[clinic input] -module _md5 -class MD5Type "MD5object *" "&PyType_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/ +#include "_hacl/Hacl_Hash_MD5.h" /* The MD5 block size and message digest sizes, in bytes */ #define MD5_BLOCKSIZE 64 #define MD5_DIGESTSIZE 16 -#include "_hacl/Hacl_Hash_MD5.h" +// --- MD5 module state ------------------------------------------------------- + +typedef struct { + PyTypeObject *md5_type; +} md5module_state; +static inline md5module_state * +get_md5module_state(PyObject *module) +{ + void *state = PyModule_GetState(module); + assert(state != NULL); + return (md5module_state *)state; +} + +static inline md5module_state * +get_md5module_state_by_cls(PyTypeObject *cls) +{ + void *state = PyType_GetModuleState(cls); + assert(state != NULL); + return (md5module_state *)state; +} + +// --- MD5 object ------------------------------------------------------------- typedef struct { PyObject_HEAD - // Prevents undefined behavior via multiple threads entering the C API. - bool use_mutex; - PyMutex mutex; - Hacl_Hash_MD5_state_t *hash_state; + HASHLIB_MUTEX_API + Hacl_Hash_MD5_state_t *state; } MD5object; #define _MD5object_CAST(op) ((MD5object *)(op)) -#include "clinic/md5module.c.h" - +// --- MD5 module clinic configuration ---------------------------------------- -typedef struct { - PyTypeObject* md5_type; -} MD5State; +/*[clinic input] +module _md5 +class MD5Type "MD5object *" "clinic_state()->md5_type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5451859a6c7e20d]*/ -static inline MD5State* -md5_get_state(PyObject *module) -{ - void *state = PyModule_GetState(module); - assert(state != NULL); - return (MD5State *)state; -} +#define clinic_state() (get_md5module_state_by_cls(Py_TYPE(self))) +#include "clinic/md5module.c.h" +#undef clinic_state static MD5object * -newMD5object(MD5State * st) +newMD5object(md5module_state *state) { - MD5object *md5 = PyObject_GC_New(MD5object, st->md5_type); + MD5object *md5 = PyObject_GC_New(MD5object, state->md5_type); if (!md5) { return NULL; } @@ -87,7 +100,7 @@ static void MD5_dealloc(PyObject *op) { MD5object *ptr = _MD5object_CAST(op); - Hacl_Hash_MD5_free(ptr->hash_state); + Hacl_Hash_MD5_free(ptr->state); PyTypeObject *tp = Py_TYPE(op); PyObject_GC_UnTrack(ptr); PyObject_GC_Del(ptr); @@ -109,17 +122,17 @@ static PyObject * MD5Type_copy_impl(MD5object *self, PyTypeObject *cls) /*[clinic end generated code: output=bf055e08244bf5ee input=d89087dcfb2a8620]*/ { - MD5State *st = PyType_GetModuleState(cls); + md5module_state *state = get_md5module_state_by_cls(cls); MD5object *newobj; - if ((newobj = newMD5object(st)) == NULL) { + if ((newobj = newMD5object(state)) == NULL) { return NULL; } ENTER_HASHLIB(self); - newobj->hash_state = Hacl_Hash_MD5_copy(self->hash_state); + newobj->state = Hacl_Hash_MD5_copy(self->state); LEAVE_HASHLIB(self); - if (newobj->hash_state == NULL) { + if (newobj->state == NULL) { Py_DECREF(newobj); return PyErr_NoMemory(); } @@ -138,7 +151,7 @@ MD5Type_digest_impl(MD5object *self) { unsigned char digest[MD5_DIGESTSIZE]; ENTER_HASHLIB(self); - Hacl_Hash_MD5_digest(self->hash_state, digest); + Hacl_Hash_MD5_digest(self->state, digest); LEAVE_HASHLIB(self); return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE); } @@ -155,7 +168,7 @@ MD5Type_hexdigest_impl(MD5object *self) { unsigned char digest[MD5_DIGESTSIZE]; ENTER_HASHLIB(self); - Hacl_Hash_MD5_digest(self->hash_state, digest); + Hacl_Hash_MD5_digest(self->state, digest); LEAVE_HASHLIB(self); const char *hexdigits = "0123456789abcdef"; @@ -211,11 +224,11 @@ MD5Type_update_impl(MD5object *self, PyObject *obj) if (self->use_mutex) { Py_BEGIN_ALLOW_THREADS PyMutex_Lock(&self->mutex); - update(self->hash_state, buf.buf, buf.len); + update(self->state, buf.buf, buf.len); PyMutex_Unlock(&self->mutex); Py_END_ALLOW_THREADS } else { - update(self->hash_state, buf.buf, buf.len); + update(self->state, buf.buf, buf.len); } PyBuffer_Release(&buf); @@ -301,16 +314,16 @@ _md5_md5_impl(PyObject *module, PyObject *data, int usedforsecurity, GET_BUFFER_VIEW_OR_ERROUT(string, &buf); } - MD5State *st = md5_get_state(module); - if ((new = newMD5object(st)) == NULL) { + md5module_state *state = get_md5module_state(module); + if ((new = newMD5object(state)) == NULL) { if (string) { PyBuffer_Release(&buf); } return NULL; } - new->hash_state = Hacl_Hash_MD5_malloc(); - if (new->hash_state == NULL) { + new->state = Hacl_Hash_MD5_malloc(); + if (new->state == NULL) { Py_DECREF(new); if (string) { PyBuffer_Release(&buf); @@ -323,11 +336,11 @@ _md5_md5_impl(PyObject *module, PyObject *data, int usedforsecurity, /* We do not initialize self->lock here as this is the constructor * where it is not yet possible to have concurrent access. */ Py_BEGIN_ALLOW_THREADS - update(new->hash_state, buf.buf, buf.len); + update(new->state, buf.buf, buf.len); Py_END_ALLOW_THREADS } else { - update(new->hash_state, buf.buf, buf.len); + update(new->state, buf.buf, buf.len); } PyBuffer_Release(&buf); } @@ -338,43 +351,43 @@ _md5_md5_impl(PyObject *module, PyObject *data, int usedforsecurity, /* List of functions exported by this module */ -static struct PyMethodDef MD5_functions[] = { +static struct PyMethodDef md5module_methods[] = { _MD5_MD5_METHODDEF {NULL, NULL} /* Sentinel */ }; static int -_md5_traverse(PyObject *module, visitproc visit, void *arg) +md5module_traverse(PyObject *module, visitproc visit, void *arg) { - MD5State *state = md5_get_state(module); + md5module_state *state = get_md5module_state(module); Py_VISIT(state->md5_type); return 0; } static int -_md5_clear(PyObject *module) +md5module_clear(PyObject *module) { - MD5State *state = md5_get_state(module); + md5module_state *state = get_md5module_state(module); Py_CLEAR(state->md5_type); return 0; } static void -_md5_free(void *module) +md5module_free(void *module) { - _md5_clear((PyObject *)module); + (void)md5module_clear((PyObject *)module); } /* Initialize this module. */ static int -md5_exec(PyObject *m) +md5module_exec(PyObject *m) { - MD5State *st = md5_get_state(m); + md5module_state *state = get_md5module_state(m); - st->md5_type = (PyTypeObject *)PyType_FromModuleAndSpec( + state->md5_type = (PyTypeObject *)PyType_FromModuleAndSpec( m, &md5_type_spec, NULL); - if (PyModule_AddObjectRef(m, "MD5Type", (PyObject *)st->md5_type) < 0) { + if (PyModule_AddObjectRef(m, "MD5Type", (PyObject *)state->md5_type) < 0) { return -1; } if (PyModule_AddIntConstant(m, "_GIL_MINSIZE", HASHLIB_GIL_MINSIZE) < 0) { @@ -384,27 +397,27 @@ md5_exec(PyObject *m) return 0; } -static PyModuleDef_Slot _md5_slots[] = { - {Py_mod_exec, md5_exec}, +static PyModuleDef_Slot md5module_slots[] = { + {Py_mod_exec, md5module_exec}, {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, {Py_mod_gil, Py_MOD_GIL_NOT_USED}, {0, NULL} }; -static struct PyModuleDef _md5module = { +static struct PyModuleDef md5module_def = { PyModuleDef_HEAD_INIT, .m_name = "_md5", - .m_size = sizeof(MD5State), - .m_methods = MD5_functions, - .m_slots = _md5_slots, - .m_traverse = _md5_traverse, - .m_clear = _md5_clear, - .m_free = _md5_free, + .m_size = sizeof(md5module_state), + .m_methods = md5module_methods, + .m_slots = md5module_slots, + .m_traverse = md5module_traverse, + .m_clear = md5module_clear, + .m_free = md5module_free, }; PyMODINIT_FUNC PyInit__md5(void) { - return PyModuleDef_Init(&_md5module); + return PyModuleDef_Init(&md5module_def); } diff --git a/Modules/sha1module.c b/Modules/sha1module.c index a746bf74f8d4c1..d72365f898fa13 100644 --- a/Modules/sha1module.c +++ b/Modules/sha1module.c @@ -8,6 +8,7 @@ Andrew Kuchling (amk@amk.ca) Greg Stein (gstein@lyra.org) Trevor Perrin (trevp@trevp.net) + Bénédikt Tran (10796600+picnixz@users.noreply.github.com) Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) Licensed to PSF under a Contributor Agreement. @@ -24,12 +25,6 @@ #include "pycore_strhex.h" // _Py_strhex() #include "pycore_typeobject.h" // _PyType_GetModuleState() -/*[clinic input] -module _sha1 -class SHA1Type "SHA1object *" "&PyType_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/ - /* The SHA1 block size and message digest sizes, in bytes */ #define SHA1_BLOCKSIZE 64 @@ -37,36 +32,54 @@ class SHA1Type "SHA1object *" "&PyType_Type" #include "_hacl/Hacl_Hash_SHA1.h" +// --- SHA-1 module state ----------------------------------------------------- + +typedef struct { + PyTypeObject *sha1_type; +} sha1module_state; + +static inline sha1module_state * +get_sha1module_state(PyObject *module) +{ + void *state = PyModule_GetState(module); + assert(state != NULL); + return (sha1module_state *)state; +} + +static inline sha1module_state * +get_sha1module_state_by_cls(PyTypeObject *cls) +{ + void *state = PyType_GetModuleState(cls); + assert(state != NULL); + return (sha1module_state *)state; +} + +// --- SHA-1 object ----------------------------------------------------------- + typedef struct { PyObject_HEAD - // Prevents undefined behavior via multiple threads entering the C API. - bool use_mutex; - PyMutex mutex; - PyThread_type_lock lock; - Hacl_Hash_SHA1_state_t *hash_state; + HASHLIB_MUTEX_API + Hacl_Hash_SHA1_state_t *state; } SHA1object; #define _SHA1object_CAST(op) ((SHA1object *)(op)) -#include "clinic/sha1module.c.h" +// --- SHA-1 module clinic configuration -------------------------------------- +/*[clinic input] +module _sha1 +class SHA1Type "SHA1object *" "clinic_state()->sha1_type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=afc62adaf06c713f]*/ -typedef struct { - PyTypeObject* sha1_type; -} SHA1State; - -static inline SHA1State* -sha1_get_state(PyObject *module) -{ - void *state = PyModule_GetState(module); - assert(state != NULL); - return (SHA1State *)state; -} +#define clinic_state() (get_sha1module_state_by_cls(Py_TYPE(self))) +#include "clinic/sha1module.c.h" +#undef clinic_state static SHA1object * -newSHA1object(SHA1State *st) +newSHA1object(sha1module_state *state) { - SHA1object *sha = PyObject_GC_New(SHA1object, st->sha1_type); + SHA1object *sha = PyObject_GC_New(SHA1object, state->sha1_type); if (sha == NULL) { return NULL; } @@ -89,9 +102,9 @@ static void SHA1_dealloc(PyObject *op) { SHA1object *ptr = _SHA1object_CAST(op); - if (ptr->hash_state != NULL) { - Hacl_Hash_SHA1_free(ptr->hash_state); - ptr->hash_state = NULL; + if (ptr->state != NULL) { + Hacl_Hash_SHA1_free(ptr->state); + ptr->state = NULL; } PyTypeObject *tp = Py_TYPE(ptr); PyObject_GC_UnTrack(ptr); @@ -114,17 +127,17 @@ static PyObject * SHA1Type_copy_impl(SHA1object *self, PyTypeObject *cls) /*[clinic end generated code: output=b32d4461ce8bc7a7 input=6c22e66fcc34c58e]*/ { - SHA1State *st = _PyType_GetModuleState(cls); + sha1module_state *state = get_sha1module_state_by_cls(cls); SHA1object *newobj; - if ((newobj = newSHA1object(st)) == NULL) { + if ((newobj = newSHA1object(state)) == NULL) { return NULL; } ENTER_HASHLIB(self); - newobj->hash_state = Hacl_Hash_SHA1_copy(self->hash_state); + newobj->state = Hacl_Hash_SHA1_copy(self->state); LEAVE_HASHLIB(self); - if (newobj->hash_state == NULL) { + if (newobj->state == NULL) { Py_DECREF(newobj); return PyErr_NoMemory(); } @@ -143,7 +156,7 @@ SHA1Type_digest_impl(SHA1object *self) { unsigned char digest[SHA1_DIGESTSIZE]; ENTER_HASHLIB(self); - Hacl_Hash_SHA1_digest(self->hash_state, digest); + Hacl_Hash_SHA1_digest(self->state, digest); LEAVE_HASHLIB(self); return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE); } @@ -160,7 +173,7 @@ SHA1Type_hexdigest_impl(SHA1object *self) { unsigned char digest[SHA1_DIGESTSIZE]; ENTER_HASHLIB(self); - Hacl_Hash_SHA1_digest(self->hash_state, digest); + Hacl_Hash_SHA1_digest(self->state, digest); LEAVE_HASHLIB(self); return _Py_strhex((const char *)digest, SHA1_DIGESTSIZE); } @@ -207,11 +220,11 @@ SHA1Type_update_impl(SHA1object *self, PyObject *obj) if (self->use_mutex) { Py_BEGIN_ALLOW_THREADS PyMutex_Lock(&self->mutex); - update(self->hash_state, buf.buf, buf.len); + update(self->state, buf.buf, buf.len); PyMutex_Unlock(&self->mutex); Py_END_ALLOW_THREADS } else { - update(self->hash_state, buf.buf, buf.len); + update(self->state, buf.buf, buf.len); } PyBuffer_Release(&buf); @@ -296,17 +309,17 @@ _sha1_sha1_impl(PyObject *module, PyObject *data, int usedforsecurity, GET_BUFFER_VIEW_OR_ERROUT(string, &buf); } - SHA1State *st = sha1_get_state(module); - if ((new = newSHA1object(st)) == NULL) { + sha1module_state *state = get_sha1module_state(module); + if ((new = newSHA1object(state)) == NULL) { if (string) { PyBuffer_Release(&buf); } return NULL; } - new->hash_state = Hacl_Hash_SHA1_malloc(); + new->state = Hacl_Hash_SHA1_malloc(); - if (new->hash_state == NULL) { + if (new->state == NULL) { Py_DECREF(new); if (string) { PyBuffer_Release(&buf); @@ -318,11 +331,11 @@ _sha1_sha1_impl(PyObject *module, PyObject *data, int usedforsecurity, /* We do not initialize self->lock here as this is the constructor * where it is not yet possible to have concurrent access. */ Py_BEGIN_ALLOW_THREADS - update(new->hash_state, buf.buf, buf.len); + update(new->state, buf.buf, buf.len); Py_END_ALLOW_THREADS } else { - update(new->hash_state, buf.buf, buf.len); + update(new->state, buf.buf, buf.len); } PyBuffer_Release(&buf); } @@ -333,43 +346,43 @@ _sha1_sha1_impl(PyObject *module, PyObject *data, int usedforsecurity, /* List of functions exported by this module */ -static struct PyMethodDef SHA1_functions[] = { +static struct PyMethodDef sha1module_methods[] = { _SHA1_SHA1_METHODDEF {NULL, NULL} /* Sentinel */ }; static int -_sha1_traverse(PyObject *module, visitproc visit, void *arg) +sha1module_traverse(PyObject *module, visitproc visit, void *arg) { - SHA1State *state = sha1_get_state(module); + sha1module_state *state = get_sha1module_state(module); Py_VISIT(state->sha1_type); return 0; } static int -_sha1_clear(PyObject *module) +sha1module_clear(PyObject *module) { - SHA1State *state = sha1_get_state(module); + sha1module_state *state = get_sha1module_state(module); Py_CLEAR(state->sha1_type); return 0; } static void -_sha1_free(void *module) +sha1module_free(void *module) { - (void)_sha1_clear((PyObject *)module); + (void)sha1module_clear((PyObject *)module); } static int -_sha1_exec(PyObject *module) +sha1module_exec(PyObject *module) { - SHA1State* st = sha1_get_state(module); + sha1module_state *state = get_sha1module_state(module); - st->sha1_type = (PyTypeObject *)PyType_FromModuleAndSpec( + state->sha1_type = (PyTypeObject *)PyType_FromModuleAndSpec( module, &sha1_type_spec, NULL); if (PyModule_AddObjectRef(module, "SHA1Type", - (PyObject *)st->sha1_type) < 0) + (PyObject *)state->sha1_type) < 0) { return -1; } @@ -386,26 +399,26 @@ _sha1_exec(PyObject *module) /* Initialize this module. */ -static PyModuleDef_Slot _sha1_slots[] = { - {Py_mod_exec, _sha1_exec}, +static PyModuleDef_Slot sha1module_slots[] = { + {Py_mod_exec, sha1module_exec}, {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, {Py_mod_gil, Py_MOD_GIL_NOT_USED}, {0, NULL} }; -static struct PyModuleDef _sha1module = { +static struct PyModuleDef sha1module_def = { PyModuleDef_HEAD_INIT, .m_name = "_sha1", - .m_size = sizeof(SHA1State), - .m_methods = SHA1_functions, - .m_slots = _sha1_slots, - .m_traverse = _sha1_traverse, - .m_clear = _sha1_clear, - .m_free = _sha1_free + .m_size = sizeof(sha1module_state), + .m_methods = sha1module_methods, + .m_slots = sha1module_slots, + .m_traverse = sha1module_traverse, + .m_clear = sha1module_clear, + .m_free = sha1module_free }; PyMODINIT_FUNC PyInit__sha1(void) { - return PyModuleDef_Init(&_sha1module); + return PyModuleDef_Init(&sha1module_def); } diff --git a/Modules/sha2module.c b/Modules/sha2module.c index 72931910c5d720..670844b23060e1 100644 --- a/Modules/sha2module.c +++ b/Modules/sha2module.c @@ -9,6 +9,7 @@ Greg Stein (gstein@lyra.org) Trevor Perrin (trevp@trevp.net) Jonathan Protzenko (jonathan@protzenko.fr) + Bénédikt Tran (10796600+picnixz@users.noreply.github.com) Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) Licensed to PSF under a Contributor Agreement. @@ -28,14 +29,9 @@ #include "hashlib.h" -/*[clinic input] -module _sha2 -class SHA256Type "SHA256object *" "&PyType_Type" -class SHA512Type "SHA512object *" "&PyType_Type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5315a7b611c9afc]*/ - +#include "_hacl/Hacl_Hash_SHA2.h" +// TODO: Get rid of int digestsize in favor of Hacl state info? /* The SHA block sizes and maximum message digest sizes, in bytes */ #define SHA256_BLOCKSIZE 64 @@ -43,51 +39,64 @@ class SHA512Type "SHA512object *" "&PyType_Type" #define SHA512_BLOCKSIZE 128 #define SHA512_DIGESTSIZE 64 -/* Our SHA2 implementations defer to the HACL* verified library. */ +// --- SHA-2 module state ----------------------------------------------------- -#include "_hacl/Hacl_Hash_SHA2.h" +/* We shall use run-time type information in the remainder of this module to + * tell apart SHA2-224 and SHA2-256 */ +typedef struct { + PyTypeObject *sha224_type; + PyTypeObject *sha256_type; + PyTypeObject *sha384_type; + PyTypeObject *sha512_type; +} sha2module_state; -// TODO: Get rid of int digestsize in favor of Hacl state info? +static inline sha2module_state * +get_sha2module_state(PyObject *module) +{ + void *state = _PyModule_GetState(module); + assert(state != NULL); + return (sha2module_state *)state; +} + +static inline sha2module_state * +get_sha2module_state_by_cls(PyTypeObject *cls) +{ + void *state = PyType_GetModuleState(cls); + assert(state != NULL); + return (sha2module_state *)state; +} + +// --- SHA-2 object ----------------------------------------------------------- typedef struct { PyObject_HEAD int digestsize; - // Prevents undefined behavior via multiple threads entering the C API. - bool use_mutex; - PyMutex mutex; + HASHLIB_MUTEX_API Hacl_Hash_SHA2_state_t_256 *state; } SHA256object; typedef struct { PyObject_HEAD int digestsize; - // Prevents undefined behavior via multiple threads entering the C API. - bool use_mutex; - PyMutex mutex; + HASHLIB_MUTEX_API Hacl_Hash_SHA2_state_t_512 *state; } SHA512object; #define _SHA256object_CAST(op) ((SHA256object *)(op)) #define _SHA512object_CAST(op) ((SHA512object *)(op)) -#include "clinic/sha2module.c.h" +// --- SHA-2 module clinic configuration -------------------------------------- -/* We shall use run-time type information in the remainder of this module to - * tell apart SHA2-224 and SHA2-256 */ -typedef struct { - PyTypeObject* sha224_type; - PyTypeObject* sha256_type; - PyTypeObject* sha384_type; - PyTypeObject* sha512_type; -} sha2_state; +/*[clinic input] +module _sha2 +class SHA256Type "SHA256object *" "clinic_state()->sha256_type" +class SHA512Type "SHA512object *" "clinic_state()->sha512_type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e758ed2b54d457ea]*/ -static inline sha2_state* -sha2_get_state(PyObject *module) -{ - void *state = _PyModule_GetState(module); - assert(state != NULL); - return (sha2_state *)state; -} +#define clinic_state() (get_sha2module_state_by_cls(Py_TYPE(self))) +#include "clinic/sha2module.c.h" +#undef clinic_state static int SHA256copy(SHA256object *src, SHA256object *dest) @@ -114,7 +123,7 @@ SHA512copy(SHA512object *src, SHA512object *dest) } static SHA256object * -newSHA224object(sha2_state *state) +newSHA224object(sha2module_state *state) { SHA256object *sha = PyObject_GC_New(SHA256object, state->sha224_type); if (!sha) { @@ -127,7 +136,7 @@ newSHA224object(sha2_state *state) } static SHA256object * -newSHA256object(sha2_state *state) +newSHA256object(sha2module_state *state) { SHA256object *sha = PyObject_GC_New(SHA256object, state->sha256_type); if (!sha) { @@ -140,7 +149,7 @@ newSHA256object(sha2_state *state) } static SHA512object * -newSHA384object(sha2_state *state) +newSHA384object(sha2module_state *state) { SHA512object *sha = PyObject_GC_New(SHA512object, state->sha384_type); if (!sha) { @@ -153,7 +162,7 @@ newSHA384object(sha2_state *state) } static SHA512object * -newSHA512object(sha2_state *state) +newSHA512object(sha2module_state *state) { SHA512object *sha = PyObject_GC_New(SHA512object, state->sha512_type); if (!sha) { @@ -260,7 +269,7 @@ SHA256Type_copy_impl(SHA256object *self, PyTypeObject *cls) { int rc; SHA256object *newobj; - sha2_state *state = _PyType_GetModuleState(cls); + sha2module_state *state = get_sha2module_state_by_cls(cls); if (Py_IS_TYPE(self, state->sha256_type)) { if ((newobj = newSHA256object(state)) == NULL) { return NULL; @@ -296,7 +305,7 @@ SHA512Type_copy_impl(SHA512object *self, PyTypeObject *cls) { int rc; SHA512object *newobj; - sha2_state *state = _PyType_GetModuleState(cls); + sha2module_state *state = _PyType_GetModuleState(cls); if (Py_IS_TYPE((PyObject*)self, state->sha512_type)) { if ((newobj = newSHA512object(state)) == NULL) { @@ -617,7 +626,7 @@ _sha2_sha256_impl(PyObject *module, PyObject *data, int usedforsecurity, GET_BUFFER_VIEW_OR_ERROUT(string, &buf); } - sha2_state *state = sha2_get_state(module); + sha2module_state *state = get_sha2module_state(module); SHA256object *new; if ((new = newSHA256object(state)) == NULL) { @@ -680,7 +689,7 @@ _sha2_sha224_impl(PyObject *module, PyObject *data, int usedforsecurity, GET_BUFFER_VIEW_OR_ERROUT(string, &buf); } - sha2_state *state = sha2_get_state(module); + sha2module_state *state = get_sha2module_state(module); SHA256object *new; if ((new = newSHA224object(state)) == NULL) { if (string) { @@ -739,7 +748,7 @@ _sha2_sha512_impl(PyObject *module, PyObject *data, int usedforsecurity, return NULL; } - sha2_state *state = sha2_get_state(module); + sha2module_state *state = get_sha2module_state(module); if (string) { GET_BUFFER_VIEW_OR_ERROUT(string, &buf); @@ -802,7 +811,7 @@ _sha2_sha384_impl(PyObject *module, PyObject *data, int usedforsecurity, return NULL; } - sha2_state *state = sha2_get_state(module); + sha2module_state *state = get_sha2module_state(module); if (string) { GET_BUFFER_VIEW_OR_ERROUT(string, &buf); @@ -844,7 +853,7 @@ _sha2_sha384_impl(PyObject *module, PyObject *data, int usedforsecurity, /* List of functions exported by this module */ -static struct PyMethodDef SHA2_functions[] = { +static struct PyMethodDef sha2module_methods[] = { _SHA2_SHA256_METHODDEF _SHA2_SHA224_METHODDEF _SHA2_SHA512_METHODDEF @@ -853,9 +862,9 @@ static struct PyMethodDef SHA2_functions[] = { }; static int -_sha2_traverse(PyObject *module, visitproc visit, void *arg) +sha2module_traverse(PyObject *module, visitproc visit, void *arg) { - sha2_state *state = sha2_get_state(module); + sha2module_state *state = get_sha2module_state(module); Py_VISIT(state->sha224_type); Py_VISIT(state->sha256_type); Py_VISIT(state->sha384_type); @@ -864,9 +873,9 @@ _sha2_traverse(PyObject *module, visitproc visit, void *arg) } static int -_sha2_clear(PyObject *module) +sha2module_clear(PyObject *module) { - sha2_state *state = sha2_get_state(module); + sha2module_state *state = get_sha2module_state(module); Py_CLEAR(state->sha224_type); Py_CLEAR(state->sha256_type); Py_CLEAR(state->sha384_type); @@ -875,15 +884,16 @@ _sha2_clear(PyObject *module) } static void -_sha2_free(void *module) +sha2module_free(void *module) { - (void)_sha2_clear((PyObject *)module); + (void)sha2module_clear((PyObject *)module); } /* Initialize this module. */ -static int sha2_exec(PyObject *module) +static int +sha2module_exec(PyObject *module) { - sha2_state *state = sha2_get_state(module); + sha2module_state *state = get_sha2module_state(module); state->sha224_type = (PyTypeObject *)PyType_FromModuleAndSpec( module, &sha224_type_spec, NULL); @@ -929,26 +939,26 @@ static int sha2_exec(PyObject *module) return 0; } -static PyModuleDef_Slot _sha2_slots[] = { - {Py_mod_exec, sha2_exec}, +static PyModuleDef_Slot sha2module_slots[] = { + {Py_mod_exec, sha2module_exec}, {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, {Py_mod_gil, Py_MOD_GIL_NOT_USED}, {0, NULL} }; -static struct PyModuleDef _sha2module = { +static struct PyModuleDef sha2module_def = { PyModuleDef_HEAD_INIT, .m_name = "_sha2", - .m_size = sizeof(sha2_state), - .m_methods = SHA2_functions, - .m_slots = _sha2_slots, - .m_traverse = _sha2_traverse, - .m_clear = _sha2_clear, - .m_free = _sha2_free + .m_size = sizeof(sha2module_state), + .m_methods = sha2module_methods, + .m_slots = sha2module_slots, + .m_traverse = sha2module_traverse, + .m_clear = sha2module_clear, + .m_free = sha2module_free }; PyMODINIT_FUNC PyInit__sha2(void) { - return PyModuleDef_Init(&_sha2module); + return PyModuleDef_Init(&sha2module_def); } diff --git a/Modules/sha3module.c b/Modules/sha3module.c index cfbf0cbcc042c5..9a7115843c0d28 100644 --- a/Modules/sha3module.c +++ b/Modules/sha3module.c @@ -9,6 +9,7 @@ * Greg Stein (gstein@lyra.org) * Trevor Perrin (trevp@trevp.net) * Gregory P. Smith (greg@krypto.org) + * Bénédikt Tran (10796600+picnixz@users.noreply.github.com) * * Copyright (C) 2012-2022 Christian Heimes (christian@python.org) * Licensed to PSF under a Contributor Agreement. @@ -24,51 +25,64 @@ #include "pycore_typeobject.h" // _PyType_GetModuleState() #include "hashlib.h" +#include "_hacl/Hacl_Hash_SHA3.h" + #define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */ +// --- SHA-3 module state ----------------------------------------------------- + typedef struct { PyTypeObject *sha3_224_type; PyTypeObject *sha3_256_type; PyTypeObject *sha3_384_type; PyTypeObject *sha3_512_type; + PyTypeObject *shake_128_type; PyTypeObject *shake_256_type; -} SHA3State; +} sha3module_state; -static inline SHA3State* -sha3_get_state(PyObject *module) +static inline sha3module_state * +get_sha3module_state(PyObject *module) { void *state = PyModule_GetState(module); assert(state != NULL); - return (SHA3State *)state; + return (sha3module_state *)state; } -/*[clinic input] -module _sha3 -class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ" -class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ" -class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ" -class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ" -class _sha3.shake_128 "SHA3object *" "&SHAKE128type" -class _sha3.shake_256 "SHA3object *" "&SHAKE256type" -[clinic start generated code]*/ -/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/ - -/* The structure for storing SHA3 info */ +static inline sha3module_state * +get_sha3module_state_by_cls(PyTypeObject *cls) +{ + void *state = PyType_GetModuleState(cls); + assert(state != NULL); + return (sha3module_state *)state; +} -#include "_hacl/Hacl_Hash_SHA3.h" +// --- SHA-3 object ----------------------------------------------------------- typedef struct { PyObject_HEAD - // Prevents undefined behavior via multiple threads entering the C API. - bool use_mutex; - PyMutex mutex; - Hacl_Hash_SHA3_state_t *hash_state; + HASHLIB_MUTEX_API + Hacl_Hash_SHA3_state_t *state; } SHA3object; #define _SHA3object_CAST(op) ((SHA3object *)(op)) +// --- SHA-3 module clinic configuration -------------------------------------- + +/*[clinic input] +module _sha3 +class _sha3.sha3_224 "SHA3object *" "clinic_state()->sha3_224_type" +class _sha3.sha3_256 "SHA3object *" "clinic_state()->sha3_256_type" +class _sha3.sha3_384 "SHA3object *" "clinic_state()->sha3_384_type" +class _sha3.sha3_512 "SHA3object *" "clinic_state()->sha3_512_type" +class _sha3.shake_128 "SHA3object *" "clinic_state()->shake_128_type" +class _sha3.shake_256 "SHA3object *" "clinic_state()->shake_256_type" +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=83376ec869f33016]*/ + +#define clinic_state() (get_sha3module_state_by_cls(Py_TYPE(self))) #include "clinic/sha3module.c.h" +#undef clinic_state static SHA3object * newSHA3object(PyTypeObject *type) @@ -125,7 +139,7 @@ py_sha3_new_impl(PyTypeObject *type, PyObject *data_obj, int usedforsecurity, } Py_buffer buf = {NULL, NULL}; - SHA3State *state = _PyType_GetModuleState(type); + sha3module_state *state = _PyType_GetModuleState(type); SHA3object *self = newSHA3object(type); if (self == NULL) { goto error; @@ -134,29 +148,29 @@ py_sha3_new_impl(PyTypeObject *type, PyObject *data_obj, int usedforsecurity, assert(state != NULL); if (type == state->sha3_224_type) { - self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_224); + self->state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_224); } else if (type == state->sha3_256_type) { - self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_256); + self->state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_256); } else if (type == state->sha3_384_type) { - self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_384); + self->state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_384); } else if (type == state->sha3_512_type) { - self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_512); + self->state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_512); } else if (type == state->shake_128_type) { - self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_Shake128); + self->state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_Shake128); } else if (type == state->shake_256_type) { - self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_Shake256); + self->state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_Shake256); } else { PyErr_BadInternalCall(); goto error; } - if (self->hash_state == NULL) { + if (self->state == NULL) { (void)PyErr_NoMemory(); goto error; } @@ -167,11 +181,11 @@ py_sha3_new_impl(PyTypeObject *type, PyObject *data_obj, int usedforsecurity, /* We do not initialize self->lock here as this is the constructor * where it is not yet possible to have concurrent access. */ Py_BEGIN_ALLOW_THREADS - sha3_update(self->hash_state, buf.buf, buf.len); + sha3_update(self->state, buf.buf, buf.len); Py_END_ALLOW_THREADS } else { - sha3_update(self->hash_state, buf.buf, buf.len); + sha3_update(self->state, buf.buf, buf.len); } } @@ -196,9 +210,9 @@ static int SHA3_clear(PyObject *op) { SHA3object *self = _SHA3object_CAST(op); - if (self->hash_state != NULL) { - Hacl_Hash_SHA3_free(self->hash_state); - self->hash_state = NULL; + if (self->state != NULL) { + Hacl_Hash_SHA3_free(self->state); + self->state = NULL; } return 0; } @@ -239,9 +253,9 @@ _sha3_sha3_224_copy_impl(SHA3object *self) return NULL; } ENTER_HASHLIB(self); - newobj->hash_state = Hacl_Hash_SHA3_copy(self->hash_state); + newobj->state = Hacl_Hash_SHA3_copy(self->state); LEAVE_HASHLIB(self); - if (newobj->hash_state == NULL) { + if (newobj->state == NULL) { Py_DECREF(newobj); return PyErr_NoMemory(); } @@ -263,10 +277,10 @@ _sha3_sha3_224_digest_impl(SHA3object *self) // This function errors out if the algorithm is SHAKE. Here, we know this // not to be the case, and therefore do not perform error checking. ENTER_HASHLIB(self); - (void)Hacl_Hash_SHA3_digest(self->hash_state, digest); + (void)Hacl_Hash_SHA3_digest(self->state, digest); LEAVE_HASHLIB(self); return PyBytes_FromStringAndSize((const char *)digest, - Hacl_Hash_SHA3_hash_len(self->hash_state)); + Hacl_Hash_SHA3_hash_len(self->state)); } @@ -282,10 +296,10 @@ _sha3_sha3_224_hexdigest_impl(SHA3object *self) { unsigned char digest[SHA3_MAX_DIGESTSIZE]; ENTER_HASHLIB(self); - (void)Hacl_Hash_SHA3_digest(self->hash_state, digest); + (void)Hacl_Hash_SHA3_digest(self->state, digest); LEAVE_HASHLIB(self); return _Py_strhex((const char *)digest, - Hacl_Hash_SHA3_hash_len(self->hash_state)); + Hacl_Hash_SHA3_hash_len(self->state)); } @@ -312,11 +326,11 @@ _sha3_sha3_224_update_impl(SHA3object *self, PyObject *data) if (self->use_mutex) { Py_BEGIN_ALLOW_THREADS PyMutex_Lock(&self->mutex); - sha3_update(self->hash_state, buf.buf, buf.len); + sha3_update(self->state, buf.buf, buf.len); PyMutex_Unlock(&self->mutex); Py_END_ALLOW_THREADS } else { - sha3_update(self->hash_state, buf.buf, buf.len); + sha3_update(self->state, buf.buf, buf.len); } PyBuffer_Release(&buf); @@ -337,7 +351,7 @@ static PyObject * SHA3_get_block_size(PyObject *op, void *Py_UNUSED(closure)) { SHA3object *self = _SHA3object_CAST(op); - uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state); + uint32_t rate = Hacl_Hash_SHA3_block_len(self->state); return PyLong_FromLong(rate); } @@ -347,8 +361,7 @@ SHA3_get_name(PyObject *self, void *Py_UNUSED(closure)) { PyTypeObject *type = Py_TYPE(self); - SHA3State *state = _PyType_GetModuleState(type); - assert(state != NULL); + sha3module_state *state = get_sha3module_state_by_cls(type); if (type == state->sha3_224_type) { return PyUnicode_FromString("sha3_224"); @@ -374,10 +387,10 @@ SHA3_get_digest_size(PyObject *op, void *Py_UNUSED(closure)) { // Preserving previous behavior: variable-length algorithms return 0 SHA3object *self = _SHA3object_CAST(op); - if (Hacl_Hash_SHA3_is_shake(self->hash_state)) + if (Hacl_Hash_SHA3_is_shake(self->state)) return PyLong_FromLong(0); else - return PyLong_FromLong(Hacl_Hash_SHA3_hash_len(self->hash_state)); + return PyLong_FromLong(Hacl_Hash_SHA3_hash_len(self->state)); } @@ -385,7 +398,7 @@ static PyObject * SHA3_get_capacity_bits(PyObject *op, void *Py_UNUSED(closure)) { SHA3object *self = _SHA3object_CAST(op); - uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state) * 8; + uint32_t rate = Hacl_Hash_SHA3_block_len(self->state) * 8; assert(rate <= 1600); int capacity = 1600 - rate; return PyLong_FromLong(capacity); @@ -396,7 +409,7 @@ static PyObject * SHA3_get_rate_bits(PyObject *op, void *Py_UNUSED(closure)) { SHA3object *self = _SHA3object_CAST(op); - uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state) * 8; + uint32_t rate = Hacl_Hash_SHA3_block_len(self->state) * 8; return PyLong_FromLong(rate); } @@ -493,7 +506,7 @@ _SHAKE_digest(PyObject *op, unsigned long digestlen, int hex) * - the output length is zero -- we follow the existing behavior and return * an empty digest, without raising an error */ if (digestlen > 0) { - (void)Hacl_Hash_SHA3_squeeze(self->hash_state, digest, digestlen); + (void)Hacl_Hash_SHA3_squeeze(self->state, digest, digestlen); } if (hex) { result = _Py_strhex((const char *)digest, digestlen); @@ -588,9 +601,9 @@ SHA3_TYPE_SPEC(SHAKE256_spec, "shake_256", SHAKE256slots); static int -_sha3_traverse(PyObject *module, visitproc visit, void *arg) +sha3module_traverse(PyObject *module, visitproc visit, void *arg) { - SHA3State *state = sha3_get_state(module); + sha3module_state *state = get_sha3module_state(module); Py_VISIT(state->sha3_224_type); Py_VISIT(state->sha3_256_type); Py_VISIT(state->sha3_384_type); @@ -601,9 +614,9 @@ _sha3_traverse(PyObject *module, visitproc visit, void *arg) } static int -_sha3_clear(PyObject *module) +sha3module_clear(PyObject *module) { - SHA3State *state = sha3_get_state(module); + sha3module_state *state = get_sha3module_state(module); Py_CLEAR(state->sha3_224_type); Py_CLEAR(state->sha3_256_type); Py_CLEAR(state->sha3_384_type); @@ -614,24 +627,24 @@ _sha3_clear(PyObject *module) } static void -_sha3_free(void *module) +sha3module_free(void *module) { - (void)_sha3_clear((PyObject *)module); + (void)sha3module_clear((PyObject *)module); } static int -_sha3_exec(PyObject *m) +sha3module_exec(PyObject *m) { - SHA3State *st = sha3_get_state(m); + sha3module_state *state = get_sha3module_state(m); #define init_sha3type(type, typespec) \ do { \ - st->type = (PyTypeObject *)PyType_FromModuleAndSpec( \ + state->type = (PyTypeObject *)PyType_FromModuleAndSpec( \ m, &typespec, NULL); \ - if (st->type == NULL) { \ + if (state->type == NULL) { \ return -1; \ } \ - if (PyModule_AddType(m, st->type) < 0) { \ + if (PyModule_AddType(m, state->type) < 0) { \ return -1; \ } \ } while(0) @@ -654,27 +667,27 @@ _sha3_exec(PyObject *m) return 0; } -static PyModuleDef_Slot _sha3_slots[] = { - {Py_mod_exec, _sha3_exec}, +static PyModuleDef_Slot sha3module_slots[] = { + {Py_mod_exec, sha3module_exec}, {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, {Py_mod_gil, Py_MOD_GIL_NOT_USED}, {0, NULL} }; /* Initialize this module. */ -static struct PyModuleDef _sha3module = { +static struct PyModuleDef sha3module_def = { PyModuleDef_HEAD_INIT, .m_name = "_sha3", - .m_size = sizeof(SHA3State), - .m_slots = _sha3_slots, - .m_traverse = _sha3_traverse, - .m_clear = _sha3_clear, - .m_free = _sha3_free, + .m_size = sizeof(sha3module_state), + .m_slots = sha3module_slots, + .m_traverse = sha3module_traverse, + .m_clear = sha3module_clear, + .m_free = sha3module_free, }; PyMODINIT_FUNC PyInit__sha3(void) { - return PyModuleDef_Init(&_sha3module); + return PyModuleDef_Init(&sha3module_def); }