24
24
25
25
#include "Python.h"
26
26
#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
29
29
#include "hashlib.h"
30
30
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>
37
31
/* EVP is the preferred interface to hashing in OpenSSL */
38
32
#include <openssl/evp.h>
39
- #include <openssl/crypto.h> // FIPS_mode()
33
+ #include <openssl/crypto.h> // FIPS_mode()
40
34
/* We use the object interface to discover what hashes OpenSSL supports. */
41
35
#include <openssl/objects.h>
36
+ #include <openssl/err.h>
42
37
43
- #ifdef Py_HAS_OPENSSL3_SUPPORT
38
+ #include <stdbool.h>
39
+
40
+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
41
+ # define Py_HAS_OPENSSL3_SUPPORT
44
42
# include <openssl/core_names.h> // OSSL_MAC_PARAM_DIGEST
45
43
# include <openssl/params.h> // OSSL_PARAM_*()
46
44
#else
47
45
# include <openssl/hmac.h> // HMAC()
48
46
#endif
49
47
50
- #include <stdbool.h>
51
-
52
48
#ifndef OPENSSL_THREADS
53
49
# error "OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL"
54
50
#endif
73
69
74
70
#define PY_EVP_MD_CTX_md (CTX ) EVP_MD_CTX_get0_md(CTX)
75
71
76
- #define Py_HMAC_CTX_TYPE EVP_MAC_CTX
72
+ #define PY_HMAC_CTX_TYPE EVP_MAC_CTX
77
73
#define PY_HMAC_CTX_free EVP_MAC_CTX_free
78
74
#define PY_HMAC_update EVP_MAC_update
79
75
#else
84
80
85
81
#define PY_EVP_MD_CTX_md (CTX ) EVP_MD_CTX_md(CTX)
86
82
87
- #define Py_HMAC_CTX_TYPE HMAC_CTX
83
+ #define PY_HMAC_CTX_TYPE HMAC_CTX
88
84
#define PY_HMAC_CTX_free HMAC_CTX_free
89
85
#define PY_HMAC_update HMAC_Update
90
86
#endif
@@ -749,8 +745,7 @@ get_openssl_evp_md_by_utf8name(_hashlibstate *state, const char *name,
749
745
* py_ht The message digest purpose.
750
746
*/
751
747
static 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 )
754
749
{
755
750
const char * name ;
756
751
if (PyUnicode_Check (digestmod )) {
@@ -1861,23 +1856,25 @@ _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
1861
1856
/*[clinic end generated code: output=82f19965d12706ac input=0a0790cc3db45c2e]*/
1862
1857
{
1863
1858
_hashlibstate * state = get_hashlib_state (module );
1864
- const void * result ;
1865
1859
unsigned char md [EVP_MAX_MD_SIZE ] = {0 };
1866
1860
#ifdef Py_HAS_OPENSSL3_SUPPORT
1867
1861
size_t md_len = 0 ;
1868
1862
const char * digest_name = NULL ;
1869
1863
#else
1870
1864
unsigned int md_len = 0 ;
1871
1865
#endif
1872
- int is_xof ;
1866
+ const void * result ;
1873
1867
PY_EVP_MD * evp = NULL ;
1868
+ int is_xof ;
1874
1869
1875
1870
if (key -> len > INT_MAX ) {
1876
- PyErr_SetString (PyExc_OverflowError , "key is too long." );
1871
+ PyErr_SetString (PyExc_OverflowError ,
1872
+ "key is too long." );
1877
1873
return NULL ;
1878
1874
}
1879
1875
if (msg -> len > INT_MAX ) {
1880
- PyErr_SetString (PyExc_OverflowError , "msg is too long." );
1876
+ PyErr_SetString (PyExc_OverflowError ,
1877
+ "msg is too long." );
1881
1878
return NULL ;
1882
1879
}
1883
1880
@@ -1951,7 +1948,7 @@ py_openssl_wrapper_HMAC_CTX_new(void)
1951
1948
}
1952
1949
1953
1950
static const EVP_MD *
1954
- hashlib_openssl_HMAC_evp_md_borrowed (HMACobject * self )
1951
+ _hashlib_hmac_get_md (HMACobject * self )
1955
1952
{
1956
1953
assert (self -> ctx != NULL );
1957
1954
const EVP_MD * md = HMAC_CTX_get_md (self -> ctx );
@@ -1968,13 +1965,13 @@ hashlib_HMAC_get_hashlib_digest_name(HMACobject *self)
1968
1965
#ifdef Py_HAS_OPENSSL3_SUPPORT
1969
1966
return get_hashlib_utf8name_by_nid (self -> evp_md_nid );
1970
1967
#else
1971
- const EVP_MD * md = hashlib_openssl_HMAC_evp_md_borrowed (self );
1968
+ const EVP_MD * md = _hashlib_hmac_get_md (self );
1972
1969
return md == NULL ? NULL : get_hashlib_utf8name_by_evp_md (md );
1973
1970
#endif
1974
1971
}
1975
1972
1976
1973
static 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 )
1978
1975
{
1979
1976
if (!PY_HMAC_update (ctx , (const unsigned char * )v -> buf , (size_t )v -> len )) {
1980
1977
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)
1985
1982
1986
1983
/* Thin wrapper around PY_HMAC_CTX_free that allows to pass a NULL 'ctx'. */
1987
1984
static inline void
1988
- hashlib_openssl_HMAC_CTX_free (Py_HMAC_CTX_TYPE * ctx )
1985
+ hashlib_openssl_HMAC_CTX_free (PY_HMAC_CTX_TYPE * ctx )
1989
1986
{
1990
1987
/* The NULL check was not present in every OpenSSL versions. */
1991
1988
if (ctx ) {
@@ -2007,10 +2004,10 @@ hashlib_openssl_HMAC_update_with_lock(HMACobject *self, PyObject *data)
2007
2004
return r ;
2008
2005
}
2009
2006
2010
- static Py_HMAC_CTX_TYPE *
2007
+ static PY_HMAC_CTX_TYPE *
2011
2008
hashlib_openssl_HMAC_ctx_copy_with_lock (HMACobject * self )
2012
2009
{
2013
- Py_HMAC_CTX_TYPE * ctx = NULL ;
2010
+ PY_HMAC_CTX_TYPE * ctx = NULL ;
2014
2011
#ifdef Py_HAS_OPENSSL3_SUPPORT
2015
2012
HASHLIB_ACQUIRE_LOCK (self );
2016
2013
ctx = EVP_MAC_CTX_dup (self -> ctx );
@@ -2040,12 +2037,12 @@ hashlib_openssl_HMAC_ctx_copy_with_lock(HMACobject *self)
2040
2037
return NULL ;
2041
2038
}
2042
2039
2043
- static Py_HMAC_CTX_TYPE *
2040
+ static PY_HMAC_CTX_TYPE *
2044
2041
hashlib_HMAC_CTX_new_from_digestmod (_hashlibstate * state ,
2045
2042
Py_buffer * key , PyObject * digestmod ,
2046
2043
int * nid )
2047
2044
{
2048
- Py_HMAC_CTX_TYPE * ctx = NULL ;
2045
+ PY_HMAC_CTX_TYPE * ctx = NULL ;
2049
2046
PY_EVP_MD * md = NULL ;
2050
2047
int is_xof , r ;
2051
2048
#ifdef Py_HAS_OPENSSL3_SUPPORT
@@ -2148,8 +2145,8 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
2148
2145
/*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
2149
2146
{
2150
2147
_hashlibstate * state = get_hashlib_state (module );
2148
+ PY_HMAC_CTX_TYPE * ctx = NULL ;
2151
2149
HMACobject * self = NULL ;
2152
- Py_HMAC_CTX_TYPE * ctx = NULL ;
2153
2150
#ifdef Py_HAS_OPENSSL3_SUPPORT
2154
2151
int nid ;
2155
2152
#endif
@@ -2215,7 +2212,7 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
2215
2212
/*[clinic end generated code: output=29aa28b452833127 input=e2fa6a05db61a4d6]*/
2216
2213
{
2217
2214
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 );
2219
2216
if (ctx == NULL ) {
2220
2217
return NULL ;
2221
2218
}
@@ -2230,7 +2227,7 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
2230
2227
}
2231
2228
2232
2229
static void
2233
- _hashlib_HMAC_dealloc (PyObject * op )
2230
+ _hmac_dealloc (PyObject * op )
2234
2231
{
2235
2232
HMACobject * self = HMACobject_CAST (op );
2236
2233
PyTypeObject * tp = Py_TYPE (self );
@@ -2243,7 +2240,7 @@ _hashlib_HMAC_dealloc(PyObject *op)
2243
2240
}
2244
2241
2245
2242
static PyObject *
2246
- _hashlib_HMAC_repr (PyObject * op )
2243
+ _hmac_repr (PyObject * op )
2247
2244
{
2248
2245
HMACobject * self = HMACobject_CAST (op );
2249
2246
const char * digest_name = hashlib_HMAC_get_hashlib_digest_name (self );
@@ -2287,7 +2284,7 @@ hashlib_openssl_HMAC_digest_size(HMACobject *self)
2287
2284
size_t digest_size = EVP_MAC_CTX_get_mac_size (self -> ctx );
2288
2285
assert (digest_size <= (size_t )EVP_MAX_MD_SIZE );
2289
2286
#else
2290
- const EVP_MD * md = hashlib_openssl_HMAC_evp_md_borrowed (self );
2287
+ const EVP_MD * md = _hashlib_hmac_get_md (self );
2291
2288
if (md == NULL ) {
2292
2289
return BAD_DIGEST_SIZE ;
2293
2290
}
@@ -2321,7 +2318,7 @@ hashlib_openssl_HMAC_digest_compute(HMACobject *self, unsigned char *buf)
2321
2318
assert (PyErr_Occurred ());
2322
2319
return -1 ;
2323
2320
}
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 );
2325
2322
if (ctx == NULL ) {
2326
2323
return -1 ;
2327
2324
}
@@ -2375,28 +2372,28 @@ _hashlib_HMAC_hexdigest_impl(HMACobject *self)
2375
2372
}
2376
2373
2377
2374
static 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 ))
2379
2376
{
2380
2377
HMACobject * self = HMACobject_CAST (op );
2381
2378
unsigned int size = hashlib_openssl_HMAC_digest_size (self );
2382
2379
return size == BAD_DIGEST_SIZE ? NULL : PyLong_FromLong (size );
2383
2380
}
2384
2381
2385
2382
static 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 ))
2387
2384
{
2388
2385
HMACobject * self = HMACobject_CAST (op );
2389
2386
#ifdef Py_HAS_OPENSSL3_SUPPORT
2390
2387
assert (self -> ctx != NULL );
2391
2388
return PyLong_FromSize_t (EVP_MAC_CTX_get_block_size (self -> ctx ));
2392
2389
#else
2393
- const EVP_MD * md = hashlib_openssl_HMAC_evp_md_borrowed (self );
2390
+ const EVP_MD * md = _hashlib_hmac_get_md (self );
2394
2391
return md == NULL ? NULL : PyLong_FromLong (EVP_MD_block_size (md ));
2395
2392
#endif
2396
2393
}
2397
2394
2398
2395
static PyObject *
2399
- _hashlib_HMAC_name_getter (PyObject * op , void * Py_UNUSED (closure ))
2396
+ _hashlib_hmac_get_name (PyObject * op , void * Py_UNUSED (closure ))
2400
2397
{
2401
2398
HMACobject * self = HMACobject_CAST (op );
2402
2399
const char * digest_name = hashlib_HMAC_get_hashlib_digest_name (self );
@@ -2415,15 +2412,15 @@ static PyMethodDef HMAC_methods[] = {
2415
2412
{NULL , NULL } /* sentinel */
2416
2413
};
2417
2414
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 },
2422
2419
{NULL } /* Sentinel */
2423
2420
};
2424
2421
2425
2422
2426
- PyDoc_STRVAR (HMACobject_type_doc ,
2423
+ PyDoc_STRVAR (hmactype_doc ,
2427
2424
"The object used to calculate HMAC of a message.\n\
2428
2425
\n\
2429
2426
Methods:\n\
@@ -2438,24 +2435,20 @@ Attributes:\n\
2438
2435
name -- the name, including the hash algorithm used by this object\n\
2439
2436
digest_size -- number of bytes in digest() output\n" );
2440
2437
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 },
2445
2442
{Py_tp_methods , HMAC_methods },
2446
- {Py_tp_getset , HMAC_getsets },
2443
+ {Py_tp_getset , HMAC_getset },
2447
2444
{0 , NULL }
2448
2445
};
2449
2446
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 ,
2459
2452
};
2460
2453
2461
2454
@@ -2803,7 +2796,7 @@ hashlib_init_hmactype(PyObject *module)
2803
2796
{
2804
2797
_hashlibstate * state = get_hashlib_state (module );
2805
2798
2806
- state -> HMAC_type = (PyTypeObject * )PyType_FromSpec (& HMACobject_type_spec );
2799
+ state -> HMAC_type = (PyTypeObject * )PyType_FromSpec (& HMACtype_spec );
2807
2800
if (state -> HMAC_type == NULL ) {
2808
2801
return -1 ;
2809
2802
}
0 commit comments