/* SHA2 module */ /* This provides an interface to NIST's SHA2 224, 256, 384, & 512 Algorithms */ /* See below for information about the original code this module was based upon. Additional work performed by: Andrew Kuchling ([email protected]) Greg Stein ([email protected]) Trevor Perrin ([email protected]) Jonathan Protzenko ([email protected]) Copyright (C) 2005-2007 Gregory P. Smith ([email protected]) Licensed to PSF under a Contributor Agreement. */ /* SHA objects */ #ifndef Py_BUILD_CORE_BUILTIN #define Py_BUILD_CORE_MODULE … #endif #include "Python.h" #include "pycore_bitutils.h" // _Py_bswap32() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_typeobject.h" // _PyType_GetModuleState() #include "pycore_strhex.h" // _Py_strhex() #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]*/ /* The SHA block sizes and maximum message digest sizes, in bytes */ #define SHA256_BLOCKSIZE … #define SHA256_DIGESTSIZE … #define SHA512_BLOCKSIZE … #define SHA512_DIGESTSIZE … /* Our SHA2 implementations defer to the HACL* verified library. */ #include "_hacl/Hacl_Hash_SHA2.h" // TODO: Get rid of int digestsize in favor of Hacl state info? SHA256object; SHA512object; #include "clinic/sha2module.c.h" /* We shall use run-time type information in the remainder of this module to * tell apart SHA2-224 and SHA2-256 */ sha2_state; static inline sha2_state* sha2_get_state(PyObject *module) { … } static void SHA256copy(SHA256object *src, SHA256object *dest) { … } static void SHA512copy(SHA512object *src, SHA512object *dest) { … } static SHA256object * newSHA224object(sha2_state *state) { … } static SHA256object * newSHA256object(sha2_state *state) { … } static SHA512object * newSHA384object(sha2_state *state) { … } static SHA512object * newSHA512object(sha2_state *state) { … } /* Internal methods for our hash objects. */ static int SHA2_traverse(PyObject *ptr, visitproc visit, void *arg) { … } static void SHA256_dealloc(SHA256object *ptr) { … } static void SHA512_dealloc(SHA512object *ptr) { … } /* HACL* takes a uint32_t for the length of its parameter, but Py_ssize_t can be * 64 bits so we loop in <4gig chunks when needed. */ static void update_256(Hacl_Hash_SHA2_state_t_256 *state, uint8_t *buf, Py_ssize_t len) { … } static void update_512(Hacl_Hash_SHA2_state_t_512 *state, uint8_t *buf, Py_ssize_t len) { … } /* External methods for our hash objects */ /*[clinic input] SHA256Type.copy cls:defining_class Return a copy of the hash object. [clinic start generated code]*/ static PyObject * SHA256Type_copy_impl(SHA256object *self, PyTypeObject *cls) /*[clinic end generated code: output=fabd515577805cd3 input=3137146fcb88e212]*/ { … } /*[clinic input] SHA512Type.copy cls: defining_class Return a copy of the hash object. [clinic start generated code]*/ static PyObject * SHA512Type_copy_impl(SHA512object *self, PyTypeObject *cls) /*[clinic end generated code: output=66d2a8ef20de8302 input=f673a18f66527c90]*/ { … } /*[clinic input] SHA256Type.digest Return the digest value as a bytes object. [clinic start generated code]*/ static PyObject * SHA256Type_digest_impl(SHA256object *self) /*[clinic end generated code: output=3a2e3997a98ee792 input=f1f4cfea5cbde35c]*/ { … } /*[clinic input] SHA512Type.digest Return the digest value as a bytes object. [clinic start generated code]*/ static PyObject * SHA512Type_digest_impl(SHA512object *self) /*[clinic end generated code: output=dd8c6320070458e0 input=f6470dd359071f4b]*/ { … } /*[clinic input] SHA256Type.hexdigest Return the digest value as a string of hexadecimal digits. [clinic start generated code]*/ static PyObject * SHA256Type_hexdigest_impl(SHA256object *self) /*[clinic end generated code: output=96cb68996a780ab3 input=0cc4c714693010d1]*/ { … } /*[clinic input] SHA512Type.hexdigest Return the digest value as a string of hexadecimal digits. [clinic start generated code]*/ static PyObject * SHA512Type_hexdigest_impl(SHA512object *self) /*[clinic end generated code: output=cbd6f844aba1fe7c input=498b877b25cbe0a2]*/ { … } /*[clinic input] SHA256Type.update obj: object / Update this hash object's state with the provided string. [clinic start generated code]*/ static PyObject * SHA256Type_update(SHA256object *self, PyObject *obj) /*[clinic end generated code: output=1b240f965ddbd8c6 input=b2d449d5b30f0f5a]*/ { … } /*[clinic input] SHA512Type.update obj: object / Update this hash object's state with the provided string. [clinic start generated code]*/ static PyObject * SHA512Type_update(SHA512object *self, PyObject *obj) /*[clinic end generated code: output=745f51057a985884 input=ded2b46656566283]*/ { … } static PyMethodDef SHA256_methods[] = …; static PyMethodDef SHA512_methods[] = …; static PyObject * SHA256_get_block_size(PyObject *self, void *closure) { … } static PyObject * SHA512_get_block_size(PyObject *self, void *closure) { … } static PyObject * SHA256_get_digest_size(SHA256object *self, void *closure) { … } static PyObject * SHA512_get_digest_size(SHA512object *self, void *closure) { … } static PyObject * SHA256_get_name(SHA256object *self, void *closure) { … } static PyObject * SHA512_get_name(SHA512object *self, void *closure) { … } static PyGetSetDef SHA256_getseters[] = …; static PyGetSetDef SHA512_getseters[] = …; static PyType_Slot sha256_types_slots[] = …; static PyType_Slot sha512_type_slots[] = …; // Using _PyType_GetModuleState() on these types is safe since they // cannot be subclassed: they don't have the Py_TPFLAGS_BASETYPE flag. static PyType_Spec sha224_type_spec = …; static PyType_Spec sha256_type_spec = …; static PyType_Spec sha384_type_spec = …; static PyType_Spec sha512_type_spec = …; /* The module-level constructors. */ /*[clinic input] _sha2.sha256 string: object(c_default="NULL") = b'' * usedforsecurity: bool = True Return a new SHA-256 hash object; optionally initialized with a string. [clinic start generated code]*/ static PyObject * _sha2_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity) /*[clinic end generated code: output=243c9dd289931f87 input=6249da1de607280a]*/ { … } /*[clinic input] _sha2.sha224 string: object(c_default="NULL") = b'' * usedforsecurity: bool = True Return a new SHA-224 hash object; optionally initialized with a string. [clinic start generated code]*/ static PyObject * _sha2_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity) /*[clinic end generated code: output=68191f232e4a3843 input=c42bcba47fd7d2b7]*/ { … } /*[clinic input] _sha2.sha512 string: object(c_default="NULL") = b'' * usedforsecurity: bool = True Return a new SHA-512 hash object; optionally initialized with a string. [clinic start generated code]*/ static PyObject * _sha2_sha512_impl(PyObject *module, PyObject *string, int usedforsecurity) /*[clinic end generated code: output=d55c8996eca214d7 input=0576ae2a6ebfad25]*/ { … } /*[clinic input] _sha2.sha384 string: object(c_default="NULL") = b'' * usedforsecurity: bool = True Return a new SHA-384 hash object; optionally initialized with a string. [clinic start generated code]*/ static PyObject * _sha2_sha384_impl(PyObject *module, PyObject *string, int usedforsecurity) /*[clinic end generated code: output=b29a0d81d51d1368 input=4e9199d8de0d2f9b]*/ { … } /* List of functions exported by this module */ static struct PyMethodDef SHA2_functions[] = …; static int _sha2_traverse(PyObject *module, visitproc visit, void *arg) { … } static int _sha2_clear(PyObject *module) { … } static void _sha2_free(void *module) { … } /* Initialize this module. */ static int sha2_exec(PyObject *module) { … } static PyModuleDef_Slot _sha2_slots[] = …; static struct PyModuleDef _sha2module = …; PyMODINIT_FUNC PyInit__sha2(void) { … }