Skip to content

Commit 7f712cb

Browse files
committed
reduce diff
1 parent 4743294 commit 7f712cb

File tree

1 file changed

+53
-47
lines changed

1 file changed

+53
-47
lines changed

Modules/_hashopenssl.c

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,7 +1950,11 @@ py_openssl_wrapper_HMAC_CTX_new(void)
19501950
}
19511951
return ctx;
19521952
}
1953+
#endif
1954+
1955+
static int _hmac_update(HMACobject*, PyObject*);
19531956

1957+
#ifndef Py_HAS_OPENSSL3_SUPPORT
19541958
static const EVP_MD *
19551959
_hashlib_hmac_get_md(HMACobject *self)
19561960
{
@@ -1994,20 +1998,6 @@ hashlib_openssl_HMAC_CTX_free(PY_HMAC_CTX_TYPE *ctx)
19941998
}
19951999
}
19962000

1997-
static int
1998-
_hmac_update(HMACobject *self, PyObject *data)
1999-
{
2000-
int r;
2001-
Py_buffer view = {0};
2002-
GET_BUFFER_VIEW_OR_ERROR(data, &view, return -1);
2003-
HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(
2004-
self, view.len,
2005-
r = hashlib_openssl_HMAC_update_once(self->ctx, &view)
2006-
);
2007-
PyBuffer_Release(&view);
2008-
return r;
2009-
}
2010-
20112001
static PY_HMAC_CTX_TYPE *
20122002
hashlib_openssl_HMAC_ctx_copy_with_lock(HMACobject *self)
20132003
{
@@ -2205,6 +2195,55 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
22052195
return NULL;
22062196
}
22072197

2198+
/* helper functions */
2199+
#define BAD_DIGEST_SIZE 0
2200+
2201+
/*
2202+
* Return the digest size in bytes.
2203+
*
2204+
* On error, set an exception and return BAD_DIGEST_SIZE.
2205+
*/
2206+
static unsigned int
2207+
_hashlib_hmac_digest_size(HMACobject *self)
2208+
{
2209+
assert(EVP_MAX_MD_SIZE < INT_MAX);
2210+
#ifdef Py_HAS_OPENSSL3_SUPPORT
2211+
assert(self->ctx != NULL);
2212+
size_t digest_size = EVP_MAC_CTX_get_mac_size(self->ctx);
2213+
assert(digest_size <= (size_t)EVP_MAX_MD_SIZE);
2214+
#else
2215+
const EVP_MD *md = _hashlib_hmac_get_md(self);
2216+
if (md == NULL) {
2217+
return BAD_DIGEST_SIZE;
2218+
}
2219+
int digest_size = EVP_MD_size(md);
2220+
/* digest_size < 0 iff EVP_MD context is NULL (which is impossible here) */
2221+
assert(digest_size >= 0);
2222+
assert(digest_size <= (int)EVP_MAX_MD_SIZE);
2223+
#endif
2224+
/* digest_size == 0 means that the context is not entirely initialized */
2225+
if (digest_size == 0) {
2226+
raise_ssl_error(PyExc_ValueError, "missing digest size");
2227+
return BAD_DIGEST_SIZE;
2228+
}
2229+
return (unsigned int)digest_size;
2230+
}
2231+
2232+
2233+
static int
2234+
_hmac_update(HMACobject *self, PyObject *data)
2235+
{
2236+
int r;
2237+
Py_buffer view = {0};
2238+
GET_BUFFER_VIEW_OR_ERROR(data, &view, return -1);
2239+
HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(
2240+
self, view.len,
2241+
r = hashlib_openssl_HMAC_update_once(self->ctx, &view)
2242+
);
2243+
PyBuffer_Release(&view);
2244+
return r;
2245+
}
2246+
22082247
/*[clinic input]
22092248
_hashlib.HMAC.copy
22102249
@@ -2272,39 +2311,6 @@ _hashlib_HMAC_update_impl(HMACobject *self, PyObject *msg)
22722311
Py_RETURN_NONE;
22732312
}
22742313

2275-
#define BAD_DIGEST_SIZE 0
2276-
2277-
/*
2278-
* Return the digest size in bytes.
2279-
*
2280-
* On error, set an exception and return BAD_DIGEST_SIZE.
2281-
*/
2282-
static unsigned int
2283-
_hashlib_hmac_digest_size(HMACobject *self)
2284-
{
2285-
assert(EVP_MAX_MD_SIZE < INT_MAX);
2286-
#ifdef Py_HAS_OPENSSL3_SUPPORT
2287-
assert(self->ctx != NULL);
2288-
size_t digest_size = EVP_MAC_CTX_get_mac_size(self->ctx);
2289-
assert(digest_size <= (size_t)EVP_MAX_MD_SIZE);
2290-
#else
2291-
const EVP_MD *md = _hashlib_hmac_get_md(self);
2292-
if (md == NULL) {
2293-
return BAD_DIGEST_SIZE;
2294-
}
2295-
int digest_size = EVP_MD_size(md);
2296-
/* digest_size < 0 iff EVP_MD context is NULL (which is impossible here) */
2297-
assert(digest_size >= 0);
2298-
assert(digest_size <= (int)EVP_MAX_MD_SIZE);
2299-
#endif
2300-
/* digest_size == 0 means that the context is not entirely initialized */
2301-
if (digest_size == 0) {
2302-
raise_ssl_error(PyExc_ValueError, "missing digest size");
2303-
return BAD_DIGEST_SIZE;
2304-
}
2305-
return (unsigned int)digest_size;
2306-
}
2307-
23082314
/*
23092315
* Extract the MAC value to 'buf' and return the digest size.
23102316
*

0 commit comments

Comments
 (0)