Skip to content

Commit 3aae0a8

Browse files
committed
handle error code returned by Hacl_Streaming_HMAC_digest
1 parent b4bfe5e commit 3aae0a8

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

Modules/hmacmodule.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,20 +1030,32 @@ _hmac_HMAC_update_impl(HMACObject *self, PyObject *msgobj)
10301030
*
10311031
* At least 'self->digest_size' bytes should be available
10321032
* in the 'digest' pointed memory area.
1033+
*
1034+
* Return 0 on success; otherwise, set an exception and return -1 on failure.
1035+
*
1036+
* Note: this function may raise a MemoryError.
10331037
*/
1034-
static inline void
1038+
static int
10351039
hmac_digest_compute_cond_lock(HMACObject *self, uint8_t *digest)
10361040
{
10371041
assert(digest != NULL);
1042+
hacl_errno_t rc;
10381043
ENTER_HASHLIB(self); // conditionally acquire a lock
1039-
Hacl_Streaming_HMAC_digest(self->state, digest, self->digest_size);
1044+
rc = Hacl_Streaming_HMAC_digest(self->state, digest, self->digest_size);
10401045
LEAVE_HASHLIB(self);
1046+
assert(
1047+
rc == Hacl_Streaming_Types_Success ||
1048+
rc == Hacl_Streaming_Types_OutOfMemory
1049+
);
1050+
return _hacl_convert_errno(rc, NULL);
10411051
}
10421052

10431053
/*[clinic input]
10441054
_hmac.HMAC.digest
10451055
10461056
Return the digest of the bytes passed to the update() method so far.
1057+
1058+
This method may raise a MemoryError.
10471059
[clinic start generated code]*/
10481060

10491061
static PyObject *
@@ -1052,7 +1064,9 @@ _hmac_HMAC_digest_impl(HMACObject *self)
10521064
{
10531065
assert(self->digest_size <= Py_hmac_hash_max_digest_size);
10541066
uint8_t digest[Py_hmac_hash_max_digest_size];
1055-
hmac_digest_compute_cond_lock(self, digest);
1067+
if (hmac_digest_compute_cond_lock(self, digest) < 0) {
1068+
return NULL;
1069+
}
10561070
return PyBytes_FromStringAndSize((const char *)digest, self->digest_size);
10571071
}
10581072

@@ -1063,6 +1077,8 @@ Return hexadecimal digest of the bytes passed to the update() method so far.
10631077
10641078
This may be used to exchange the value safely in email or other non-binary
10651079
environments.
1080+
1081+
This method may raise a MemoryError.
10661082
[clinic start generated code]*/
10671083

10681084
static PyObject *
@@ -1071,7 +1087,9 @@ _hmac_HMAC_hexdigest_impl(HMACObject *self)
10711087
{
10721088
assert(self->digest_size <= Py_hmac_hash_max_digest_size);
10731089
uint8_t digest[Py_hmac_hash_max_digest_size];
1074-
hmac_digest_compute_cond_lock(self, digest);
1090+
if (hmac_digest_compute_cond_lock(self, digest) < 0) {
1091+
return NULL;
1092+
}
10751093
return _Py_strhex((const char *)digest, self->digest_size);
10761094
}
10771095

0 commit comments

Comments
 (0)