2424
2525#include "Python.h"
2626#include "pycore_hashtable.h"
27- #include "pycore_strhex.h" // _Py_strhex()
28- #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_RELAXED
27+ #include "pycore_strhex.h" // _Py_strhex()
28+ #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_RELAXED
2929#include "hashlib.h"
3030
31- #include <openssl/opensslv.h>
32- #if OPENSSL_VERSION_NUMBER >= 0x30000000L
33- # define Py_HAS_OPENSSL3_SUPPORT
34- #endif
35-
36- #include <openssl/err.h>
3731/* EVP is the preferred interface to hashing in OpenSSL */
3832#include <openssl/evp.h>
39- #include <openssl/crypto.h> // FIPS_mode()
33+ #include <openssl/crypto.h> // FIPS_mode()
4034/* We use the object interface to discover what hashes OpenSSL supports. */
4135#include <openssl/objects.h>
36+ #include <openssl/err.h>
4237
43- #ifdef Py_HAS_OPENSSL3_SUPPORT
38+ #include <stdbool.h>
39+
40+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
41+ # define Py_HAS_OPENSSL3_SUPPORT
4442# include <openssl/core_names.h> // OSSL_MAC_PARAM_DIGEST
4543# include <openssl/params.h> // OSSL_PARAM_*()
4644#else
4745# include <openssl/hmac.h> // HMAC()
4846#endif
4947
50- #include <stdbool.h>
51-
5248#ifndef OPENSSL_THREADS
5349# error "OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL"
5450#endif
7369
7470#define PY_EVP_MD_CTX_md (CTX ) EVP_MD_CTX_get0_md(CTX)
7571
76- #define Py_HMAC_CTX_TYPE EVP_MAC_CTX
72+ #define PY_HMAC_CTX_TYPE EVP_MAC_CTX
7773#define PY_HMAC_CTX_free EVP_MAC_CTX_free
7874#define PY_HMAC_update EVP_MAC_update
7975#else
8480
8581#define PY_EVP_MD_CTX_md (CTX ) EVP_MD_CTX_md(CTX)
8682
87- #define Py_HMAC_CTX_TYPE HMAC_CTX
83+ #define PY_HMAC_CTX_TYPE HMAC_CTX
8884#define PY_HMAC_CTX_free HMAC_CTX_free
8985#define PY_HMAC_update HMAC_Update
9086#endif
@@ -749,8 +745,7 @@ get_openssl_evp_md_by_utf8name(_hashlibstate *state, const char *name,
749745 * py_ht The message digest purpose.
750746 */
751747static PY_EVP_MD *
752- get_openssl_evp_md (_hashlibstate * state ,
753- PyObject * digestmod , Py_hash_type py_ht )
748+ get_openssl_evp_md (_hashlibstate * state , PyObject * digestmod , Py_hash_type py_ht )
754749{
755750 const char * name ;
756751 if (PyUnicode_Check (digestmod )) {
@@ -1861,23 +1856,25 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
18611856/*[clinic end generated code: output=82f19965d12706ac input=0a0790cc3db45c2e]*/
18621857{
18631858 _hashlibstate * state = get_hashlib_state (module );
1864- const void * result ;
18651859 unsigned char md [EVP_MAX_MD_SIZE ] = {0 };
18661860#ifdef Py_HAS_OPENSSL3_SUPPORT
18671861 size_t md_len = 0 ;
18681862 const char * digest_name = NULL ;
18691863#else
18701864 unsigned int md_len = 0 ;
18711865#endif
1872- int is_xof ;
1866+ const void * result ;
18731867 PY_EVP_MD * evp = NULL ;
1868+ int is_xof ;
18741869
18751870 if (key -> len > INT_MAX ) {
1876- PyErr_SetString (PyExc_OverflowError , "key is too long." );
1871+ PyErr_SetString (PyExc_OverflowError ,
1872+ "key is too long." );
18771873 return NULL ;
18781874 }
18791875 if (msg -> len > INT_MAX ) {
1880- PyErr_SetString (PyExc_OverflowError , "msg is too long." );
1876+ PyErr_SetString (PyExc_OverflowError ,
1877+ "msg is too long." );
18811878 return NULL ;
18821879 }
18831880
@@ -1951,7 +1948,7 @@ py_openssl_wrapper_HMAC_CTX_new(void)
19511948}
19521949
19531950static const EVP_MD *
1954- hashlib_openssl_HMAC_evp_md_borrowed (HMACobject * self )
1951+ _hashlib_hmac_get_md (HMACobject * self )
19551952{
19561953 assert (self -> ctx != NULL );
19571954 const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
@@ -1968,13 +1965,13 @@ hashlib_HMAC_get_hashlib_digest_name(HMACobject *self)
19681965#ifdef Py_HAS_OPENSSL3_SUPPORT
19691966 return get_hashlib_utf8name_by_nid (self -> evp_md_nid );
19701967#else
1971- const EVP_MD * md = hashlib_openssl_HMAC_evp_md_borrowed (self );
1968+ const EVP_MD * md = _hashlib_hmac_get_md (self );
19721969 return md == NULL ? NULL : get_hashlib_utf8name_by_evp_md (md );
19731970#endif
19741971}
19751972
19761973static int
1977- hashlib_openssl_HMAC_update_once (Py_HMAC_CTX_TYPE * ctx , const Py_buffer * v )
1974+ hashlib_openssl_HMAC_update_once (PY_HMAC_CTX_TYPE * ctx , const Py_buffer * v )
19781975{
19791976 if (!PY_HMAC_update (ctx , (const unsigned char * )v -> buf , (size_t )v -> len )) {
19801977 notify_smart_ssl_error_occurred_in (Py_STRINGIFY (PY_HMAC_update ));
@@ -1985,7 +1982,7 @@ hashlib_openssl_HMAC_update_once(Py_HMAC_CTX_TYPE *ctx, const Py_buffer *v)
19851982
19861983/* Thin wrapper around PY_HMAC_CTX_free that allows to pass a NULL 'ctx'. */
19871984static inline void
1988- hashlib_openssl_HMAC_CTX_free (Py_HMAC_CTX_TYPE * ctx )
1985+ hashlib_openssl_HMAC_CTX_free (PY_HMAC_CTX_TYPE * ctx )
19891986{
19901987 /* The NULL check was not present in every OpenSSL versions. */
19911988 if (ctx ) {
@@ -2007,10 +2004,10 @@ hashlib_openssl_HMAC_update_with_lock(HMACobject *self, PyObject *data)
20072004 return r ;
20082005}
20092006
2010- static Py_HMAC_CTX_TYPE *
2007+ static PY_HMAC_CTX_TYPE *
20112008hashlib_openssl_HMAC_ctx_copy_with_lock (HMACobject * self )
20122009{
2013- Py_HMAC_CTX_TYPE * ctx = NULL ;
2010+ PY_HMAC_CTX_TYPE * ctx = NULL ;
20142011#ifdef Py_HAS_OPENSSL3_SUPPORT
20152012 HASHLIB_ACQUIRE_LOCK (self );
20162013 ctx = EVP_MAC_CTX_dup (self -> ctx );
@@ -2040,12 +2037,12 @@ hashlib_openssl_HMAC_ctx_copy_with_lock(HMACobject *self)
20402037 return NULL ;
20412038}
20422039
2043- static Py_HMAC_CTX_TYPE *
2040+ static PY_HMAC_CTX_TYPE *
20442041hashlib_HMAC_CTX_new_from_digestmod (_hashlibstate * state ,
20452042 Py_buffer * key , PyObject * digestmod ,
20462043 int * nid )
20472044{
2048- Py_HMAC_CTX_TYPE * ctx = NULL ;
2045+ PY_HMAC_CTX_TYPE * ctx = NULL ;
20492046 PY_EVP_MD * md = NULL ;
20502047 int is_xof , r ;
20512048#ifdef Py_HAS_OPENSSL3_SUPPORT
@@ -2148,8 +2145,8 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
21482145/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
21492146{
21502147 _hashlibstate * state = get_hashlib_state (module );
2148+ PY_HMAC_CTX_TYPE * ctx = NULL ;
21512149 HMACobject * self = NULL ;
2152- Py_HMAC_CTX_TYPE * ctx = NULL ;
21532150#ifdef Py_HAS_OPENSSL3_SUPPORT
21542151 int nid ;
21552152#endif
@@ -2215,7 +2212,7 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
22152212/*[clinic end generated code: output=29aa28b452833127 input=e2fa6a05db61a4d6]*/
22162213{
22172214 HMACobject * retval ;
2218- Py_HMAC_CTX_TYPE * ctx = hashlib_openssl_HMAC_ctx_copy_with_lock (self );
2215+ PY_HMAC_CTX_TYPE * ctx = hashlib_openssl_HMAC_ctx_copy_with_lock (self );
22192216 if (ctx == NULL ) {
22202217 return NULL ;
22212218 }
@@ -2230,7 +2227,7 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
22302227}
22312228
22322229static void
2233- _hashlib_HMAC_dealloc (PyObject * op )
2230+ _hmac_dealloc (PyObject * op )
22342231{
22352232 HMACobject * self = HMACobject_CAST (op );
22362233 PyTypeObject * tp = Py_TYPE (self );
@@ -2243,7 +2240,7 @@ _hashlib_HMAC_dealloc(PyObject *op)
22432240}
22442241
22452242static PyObject *
2246- _hashlib_HMAC_repr (PyObject * op )
2243+ _hmac_repr (PyObject * op )
22472244{
22482245 HMACobject * self = HMACobject_CAST (op );
22492246 const char * digest_name = hashlib_HMAC_get_hashlib_digest_name (self );
@@ -2287,7 +2284,7 @@ hashlib_openssl_HMAC_digest_size(HMACobject *self)
22872284 size_t digest_size = EVP_MAC_CTX_get_mac_size (self -> ctx );
22882285 assert (digest_size <= (size_t )EVP_MAX_MD_SIZE );
22892286#else
2290- const EVP_MD * md = hashlib_openssl_HMAC_evp_md_borrowed (self );
2287+ const EVP_MD * md = _hashlib_hmac_get_md (self );
22912288 if (md == NULL ) {
22922289 return BAD_DIGEST_SIZE ;
22932290 }
@@ -2321,7 +2318,7 @@ hashlib_openssl_HMAC_digest_compute(HMACobject *self, unsigned char *buf)
23212318 assert (PyErr_Occurred ());
23222319 return -1 ;
23232320 }
2324- Py_HMAC_CTX_TYPE * ctx = hashlib_openssl_HMAC_ctx_copy_with_lock (self );
2321+ PY_HMAC_CTX_TYPE * ctx = hashlib_openssl_HMAC_ctx_copy_with_lock (self );
23252322 if (ctx == NULL ) {
23262323 return -1 ;
23272324 }
@@ -2375,28 +2372,28 @@ _hashlib_HMAC_hexdigest_impl(HMACobject *self)
23752372}
23762373
23772374static PyObject *
2378- _hashlib_HMAC_digest_size_getter (PyObject * op , void * Py_UNUSED (closure ))
2375+ _hashlib_hmac_get_digest_size (PyObject * op , void * Py_UNUSED (closure ))
23792376{
23802377 HMACobject * self = HMACobject_CAST (op );
23812378 unsigned int size = hashlib_openssl_HMAC_digest_size (self );
23822379 return size == BAD_DIGEST_SIZE ? NULL : PyLong_FromLong (size );
23832380}
23842381
23852382static PyObject *
2386- _hashlib_HMAC_block_size_getter (PyObject * op , void * Py_UNUSED (closure ))
2383+ _hashlib_hmac_get_block_size (PyObject * op , void * Py_UNUSED (closure ))
23872384{
23882385 HMACobject * self = HMACobject_CAST (op );
23892386#ifdef Py_HAS_OPENSSL3_SUPPORT
23902387 assert (self -> ctx != NULL );
23912388 return PyLong_FromSize_t (EVP_MAC_CTX_get_block_size (self -> ctx ));
23922389#else
2393- const EVP_MD * md = hashlib_openssl_HMAC_evp_md_borrowed (self );
2390+ const EVP_MD * md = _hashlib_hmac_get_md (self );
23942391 return md == NULL ? NULL : PyLong_FromLong (EVP_MD_block_size (md ));
23952392#endif
23962393}
23972394
23982395static PyObject *
2399- _hashlib_HMAC_name_getter (PyObject * op , void * Py_UNUSED (closure ))
2396+ _hashlib_hmac_get_name (PyObject * op , void * Py_UNUSED (closure ))
24002397{
24012398 HMACobject * self = HMACobject_CAST (op );
24022399 const char * digest_name = hashlib_HMAC_get_hashlib_digest_name (self );
@@ -2415,15 +2412,15 @@ static PyMethodDef HMAC_methods[] = {
24152412 {NULL , NULL } /* sentinel */
24162413};
24172414
2418- static PyGetSetDef HMAC_getsets [] = {
2419- {"digest_size" , _hashlib_HMAC_digest_size_getter , NULL , NULL , NULL },
2420- {"block_size" , _hashlib_HMAC_block_size_getter , NULL , NULL , NULL },
2421- {"name" , _hashlib_HMAC_name_getter , NULL , NULL , NULL },
2415+ static PyGetSetDef HMAC_getset [] = {
2416+ {"digest_size" , _hashlib_hmac_get_digest_size , NULL , NULL , NULL },
2417+ {"block_size" , _hashlib_hmac_get_block_size , NULL , NULL , NULL },
2418+ {"name" , _hashlib_hmac_get_name , NULL , NULL , NULL },
24222419 {NULL } /* Sentinel */
24232420};
24242421
24252422
2426- PyDoc_STRVAR (HMACobject_type_doc ,
2423+ PyDoc_STRVAR (hmactype_doc ,
24272424"The object used to calculate HMAC of a message.\n\
24282425\n\
24292426Methods:\n\
@@ -2438,24 +2435,20 @@ Attributes:\n\
24382435name -- the name, including the hash algorithm used by this object\n\
24392436digest_size -- number of bytes in digest() output\n" );
24402437
2441- static PyType_Slot HMACobject_type_slots [] = {
2442- {Py_tp_doc , (char * )HMACobject_type_doc },
2443- {Py_tp_repr , _hashlib_HMAC_repr },
2444- {Py_tp_dealloc , _hashlib_HMAC_dealloc },
2438+ static PyType_Slot HMACtype_slots [] = {
2439+ {Py_tp_doc , (char * )hmactype_doc },
2440+ {Py_tp_repr , _hmac_repr },
2441+ {Py_tp_dealloc , _hmac_dealloc },
24452442 {Py_tp_methods , HMAC_methods },
2446- {Py_tp_getset , HMAC_getsets },
2443+ {Py_tp_getset , HMAC_getset },
24472444 {0 , NULL }
24482445};
24492446
2450- PyType_Spec HMACobject_type_spec = {
2451- .name = "_hashlib.HMAC" ,
2452- .basicsize = sizeof (HMACobject ),
2453- .flags = (
2454- Py_TPFLAGS_DEFAULT
2455- | Py_TPFLAGS_DISALLOW_INSTANTIATION
2456- | Py_TPFLAGS_IMMUTABLETYPE
2457- ),
2458- .slots = HMACobject_type_slots
2447+ PyType_Spec HMACtype_spec = {
2448+ "_hashlib.HMAC" , /* name */
2449+ sizeof (HMACobject ), /* basicsize */
2450+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE ,
2451+ .slots = HMACtype_slots ,
24592452};
24602453
24612454
@@ -2803,7 +2796,7 @@ hashlib_init_hmactype(PyObject *module)
28032796{
28042797 _hashlibstate * state = get_hashlib_state (module );
28052798
2806- state -> HMAC_type = (PyTypeObject * )PyType_FromSpec (& HMACobject_type_spec );
2799+ state -> HMAC_type = (PyTypeObject * )PyType_FromSpec (& HMACtype_spec );
28072800 if (state -> HMAC_type == NULL ) {
28082801 return -1 ;
28092802 }
0 commit comments