Skip to content

Commit d057cb4

Browse files
[3.14] gh-135532: simplify handling of HACL* errors in _hmac (GH-135740) (#135743)
gh-135532: simplify handling of HACL* errors in `_hmac` (GH-135740) (cherry picked from commit 2dbada1) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 60fc42c commit d057cb4

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

Modules/hmacmodule.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,24 @@ typedef struct py_hmac_hacl_api {
234234
*
235235
* The formal signature of this macro is:
236236
*
237-
* (HACL_HMAC_state *, uint8_t *, uint32_t, PyObject *, (C statements))
237+
* (HACL_HMAC_state *, uint8_t *, uint32_t, (C statements))
238238
*/
239239
#ifndef NDEBUG
240240
#define Py_HMAC_HACL_UPDATE_ONCE( \
241241
HACL_STATE, BUF, LEN, \
242-
ALGORITHM, ERRACTION \
242+
ERRACTION \
243243
) \
244244
do { \
245245
Py_CHECK_HACL_UINT32_T_LENGTH(LEN); \
246246
hacl_errno_t code = Py_HMAC_HACL_UPDATE_CALL(HACL_STATE, BUF, LEN); \
247-
if (_hacl_convert_errno(code, (ALGORITHM)) < 0) { \
247+
if (_hacl_convert_errno(code) < 0) { \
248248
ERRACTION; \
249249
} \
250250
} while (0)
251251
#else
252252
#define Py_HMAC_HACL_UPDATE_ONCE( \
253253
HACL_STATE, BUF, LEN, \
254-
_ALGORITHM, _ERRACTION \
254+
_ERRACTION \
255255
) \
256256
do { \
257257
(void)Py_HMAC_HACL_UPDATE_CALL(HACL_STATE, BUF, (LEN)); \
@@ -274,25 +274,25 @@ typedef struct py_hmac_hacl_api {
274274
*
275275
* The formal signature of this macro is:
276276
*
277-
* (HACL_HMAC_state *, uint8_t *, C integer, PyObject *, (C statements))
277+
* (HACL_HMAC_state *, uint8_t *, C integer, (C statements))
278278
*/
279279
#ifdef Py_HMAC_SSIZE_LARGER_THAN_UINT32
280280
#define Py_HMAC_HACL_UPDATE_LOOP( \
281281
HACL_STATE, BUF, LEN, \
282-
ALGORITHM, ERRACTION \
282+
ERRACTION \
283283
) \
284284
do { \
285285
while ((Py_ssize_t)LEN > UINT32_MAX_AS_SSIZE_T) { \
286286
Py_HMAC_HACL_UPDATE_ONCE(HACL_STATE, BUF, UINT32_MAX, \
287-
ALGORITHM, ERRACTION); \
287+
ERRACTION); \
288288
BUF += UINT32_MAX; \
289289
LEN -= UINT32_MAX; \
290290
} \
291291
} while (0)
292292
#else
293293
#define Py_HMAC_HACL_UPDATE_LOOP( \
294294
HACL_STATE, BUF, LEN, \
295-
_ALGORITHM, _ERRACTION \
295+
_ERRACTION \
296296
)
297297
#endif
298298

@@ -301,17 +301,17 @@ typedef struct py_hmac_hacl_api {
301301
*
302302
* The formal signature of this macro is:
303303
*
304-
* (HACL_HMAC_state *, uint8_t *, C integer, PyObject *, (C statements))
304+
* (HACL_HMAC_state *, uint8_t *, C integer, (C statements))
305305
*/
306306
#define Py_HMAC_HACL_UPDATE( \
307307
HACL_STATE, BUF, LEN, \
308-
ALGORITHM, ERRACTION \
308+
ERRACTION \
309309
) \
310310
do { \
311311
Py_HMAC_HACL_UPDATE_LOOP(HACL_STATE, BUF, LEN, \
312-
ALGORITHM, ERRACTION); \
312+
ERRACTION); \
313313
Py_HMAC_HACL_UPDATE_ONCE(HACL_STATE, BUF, LEN, \
314-
ALGORITHM, ERRACTION); \
314+
ERRACTION); \
315315
} while (0)
316316

317317
/*
@@ -491,7 +491,7 @@ narrow_hmac_hash_kind(hmacmodule_state *state, HMAC_Hash_Kind kind)
491491
* Otherwise, this sets an appropriate exception and returns -1.
492492
*/
493493
static int
494-
_hacl_convert_errno(hacl_errno_t code, PyObject *algorithm)
494+
_hacl_convert_errno(hacl_errno_t code)
495495
{
496496
assert(PyGILState_GetThisThreadState() != NULL);
497497
if (code == Hacl_Streaming_Types_Success) {
@@ -501,10 +501,7 @@ _hacl_convert_errno(hacl_errno_t code, PyObject *algorithm)
501501
PyGILState_STATE gstate = PyGILState_Ensure();
502502
switch (code) {
503503
case Hacl_Streaming_Types_InvalidAlgorithm: {
504-
// only makes sense if an algorithm is known at call time
505-
assert(algorithm != NULL);
506-
assert(PyUnicode_CheckExact(algorithm));
507-
PyErr_Format(PyExc_ValueError, "invalid algorithm: %U", algorithm);
504+
PyErr_SetString(PyExc_ValueError, "invalid HACL* algorithm");
508505
break;
509506
}
510507
case Hacl_Streaming_Types_InvalidLength: {
@@ -521,7 +518,7 @@ _hacl_convert_errno(hacl_errno_t code, PyObject *algorithm)
521518
}
522519
default: {
523520
PyErr_Format(PyExc_RuntimeError,
524-
"HACL* internal routine failed with error code: %d",
521+
"HACL* internal routine failed with error code: %u",
525522
code);
526523
break;
527524
}
@@ -541,7 +538,7 @@ _hacl_hmac_state_new(HMAC_Hash_Kind kind, uint8_t *key, uint32_t len)
541538
assert(kind != Py_hmac_kind_hash_unknown);
542539
HACL_HMAC_state *state = NULL;
543540
hacl_errno_t retcode = Hacl_Streaming_HMAC_malloc_(kind, key, len, &state);
544-
if (_hacl_convert_errno(retcode, NULL) < 0) {
541+
if (_hacl_convert_errno(retcode) < 0) {
545542
assert(state == NULL);
546543
return NULL;
547544
}
@@ -809,13 +806,13 @@ hmac_feed_initial_data(HMACObject *self, uint8_t *msg, Py_ssize_t len)
809806
}
810807

811808
if (len < HASHLIB_GIL_MINSIZE) {
812-
Py_HMAC_HACL_UPDATE(self->state, msg, len, self->name, return -1);
809+
Py_HMAC_HACL_UPDATE(self->state, msg, len, return -1);
813810
return 0;
814811
}
815812

816813
int res = 0;
817814
Py_BEGIN_ALLOW_THREADS
818-
Py_HMAC_HACL_UPDATE(self->state, msg, len, self->name, goto error);
815+
Py_HMAC_HACL_UPDATE(self->state, msg, len, goto error);
819816
goto done;
820817
#ifndef NDEBUG
821818
error:
@@ -983,7 +980,7 @@ hmac_update_state_with_lock(HMACObject *self, uint8_t *buf, Py_ssize_t len)
983980
int res = 0;
984981
Py_BEGIN_ALLOW_THREADS
985982
PyMutex_Lock(&self->mutex); // unconditionally acquire a lock
986-
Py_HMAC_HACL_UPDATE(self->state, buf, len, self->name, goto error);
983+
Py_HMAC_HACL_UPDATE(self->state, buf, len, goto error);
987984
goto done;
988985
#ifndef NDEBUG
989986
error:
@@ -1010,7 +1007,7 @@ static int
10101007
hmac_update_state_cond_lock(HMACObject *self, uint8_t *buf, Py_ssize_t len)
10111008
{
10121009
ENTER_HASHLIB(self); // conditionally acquire a lock
1013-
Py_HMAC_HACL_UPDATE(self->state, buf, len, self->name, goto error);
1010+
Py_HMAC_HACL_UPDATE(self->state, buf, len, goto error);
10141011
LEAVE_HASHLIB(self);
10151012
return 0;
10161013

@@ -1081,7 +1078,7 @@ hmac_digest_compute_cond_lock(HMACObject *self, uint8_t *digest)
10811078
rc == Hacl_Streaming_Types_Success ||
10821079
rc == Hacl_Streaming_Types_OutOfMemory
10831080
);
1084-
return _hacl_convert_errno(rc, NULL);
1081+
return _hacl_convert_errno(rc);
10851082
}
10861083

10871084
/*[clinic input]

0 commit comments

Comments
 (0)