Skip to content

Commit 074f9ab

Browse files
committed
add #define for error messages
1 parent 960aa73 commit 074f9ab

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

Modules/hmacmodule.c

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
# define Py_EVP_MD_free(MD) do {} while(0)
2424
#endif
2525

26+
// --- Reusable error messages ------------------------------------------------
27+
28+
#define INVALID_KEY_LENGTH "key length exceeds UINT32_MAX"
29+
#define INVALID_MSG_LENGTH "message length exceeds UINT32_MAX"
30+
2631
// --- HMAC underlying hash function static information -----------------------
2732

2833
#define Py_hmac_hash_max_digest_size 64
@@ -507,39 +512,36 @@ _hmac_compute_digest_impl(PyObject *module, PyObject *key, PyObject *msg,
507512
* lest an OverflowError is raised. The Python implementation takes care
508513
* of dispatching to the OpenSSL implementation in this case.
509514
*/
510-
#define Py_HMAC_HACL_ONESHOT(HACL_HID, KEY, MSG) \
511-
do { \
512-
Py_buffer keyview, msgview; \
513-
GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \
514-
if (!has_uint32_t_buffer_length(&keyview)) { \
515-
PyBuffer_Release(&keyview); \
516-
PyErr_SetString(PyExc_OverflowError, \
517-
"key length exceeds UINT32_MAX"); \
518-
return NULL; \
519-
} \
520-
GET_BUFFER_VIEW_OR_ERROR( \
521-
(MSG), &msgview, \
522-
PyBuffer_Release(&keyview); return NULL \
523-
); \
524-
if (!has_uint32_t_buffer_length(&msgview)) { \
525-
PyBuffer_Release(&msgview); \
526-
PyBuffer_Release(&keyview); \
527-
PyErr_SetString(PyExc_OverflowError, \
528-
"message length exceeds UINT32_MAX"); \
529-
return NULL; \
530-
} \
531-
uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \
532-
Py_hmac_## HACL_HID ##_compute_func( \
533-
out, \
534-
(uint8_t *)keyview.buf, (uint32_t)keyview.len, \
535-
(uint8_t *)msgview.buf, (uint32_t)msgview.len \
536-
); \
537-
PyBuffer_Release(&msgview); \
538-
PyBuffer_Release(&keyview); \
539-
return PyBytes_FromStringAndSize( \
540-
(const char *)out, \
541-
Py_hmac_## HACL_HID ##_digest_size \
542-
); \
515+
#define Py_HMAC_HACL_ONESHOT(HACL_HID, KEY, MSG) \
516+
do { \
517+
Py_buffer keyview, msgview; \
518+
GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \
519+
if (!has_uint32_t_buffer_length(&keyview)) { \
520+
PyBuffer_Release(&keyview); \
521+
PyErr_SetString(PyExc_OverflowError, INVALID_KEY_LENGTH); \
522+
return NULL; \
523+
} \
524+
GET_BUFFER_VIEW_OR_ERROR((MSG), &msgview, \
525+
PyBuffer_Release(&keyview); \
526+
return NULL); \
527+
if (!has_uint32_t_buffer_length(&msgview)) { \
528+
PyBuffer_Release(&msgview); \
529+
PyBuffer_Release(&keyview); \
530+
PyErr_SetString(PyExc_OverflowError, INVALID_MSG_LENGTH); \
531+
return NULL; \
532+
} \
533+
uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \
534+
Py_hmac_## HACL_HID ##_compute_func( \
535+
out, \
536+
(uint8_t *)keyview.buf, (uint32_t)keyview.len, \
537+
(uint8_t *)msgview.buf, (uint32_t)msgview.len \
538+
); \
539+
PyBuffer_Release(&msgview); \
540+
PyBuffer_Release(&keyview); \
541+
return PyBytes_FromStringAndSize( \
542+
(const char *)out, \
543+
Py_hmac_## HACL_HID ##_digest_size \
544+
); \
543545
} while (0)
544546

545547
/*[clinic input]

0 commit comments

Comments
 (0)