@@ -1098,43 +1098,61 @@ _hmac_compute_digest_impl(PyObject *module, PyObject *key, PyObject *msg,
10981098 return info -> api .compute_py (module , key , msg );
10991099}
11001100
1101+ /*
1102+ * Obtain a view from 'key' and 'msg', storing it in 'keyview' and 'msgview'.
1103+ *
1104+ * Return 0 on success; otherwise set an exception and return -1.
1105+ */
1106+ static int
1107+ hmac_get_buffer_views (PyObject * key , Py_buffer * keyview ,
1108+ PyObject * msg , Py_buffer * msgview )
1109+ {
1110+ if (_Py_hashlib_get_buffer_view (key , keyview ) < 0 ) {
1111+ return -1 ;
1112+ }
1113+ if (!has_uint32_t_buffer_length (keyview )) {
1114+ PyBuffer_Release (keyview );
1115+ set_invalid_key_length_error ();
1116+ return -1 ;
1117+ }
1118+ if (_Py_hashlib_get_buffer_view (msg , msgview ) < 0 ) {
1119+ PyBuffer_Release (keyview );
1120+ return -1 ;
1121+ }
1122+ if (!has_uint32_t_buffer_length (msgview )) {
1123+ PyBuffer_Release (msgview );
1124+ PyBuffer_Release (keyview );
1125+ set_invalid_msg_length_error ();
1126+ return -1 ;
1127+ }
1128+ return 0 ;
1129+ }
1130+
11011131/*
11021132 * One-shot HMAC-HASH using the given HACL_HID.
11031133 *
11041134 * The length of the key and message buffers must not exceed UINT32_MAX,
11051135 * lest an OverflowError is raised. The Python implementation takes care
11061136 * of dispatching to the OpenSSL implementation in this case.
11071137 */
1108- #define Py_HMAC_HACL_ONESHOT (HACL_HID , KEY , MSG ) \
1109- do { \
1110- Py_buffer keyview, msgview; \
1111- GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \
1112- if (!has_uint32_t_buffer_length(&keyview)) { \
1113- PyBuffer_Release(&keyview); \
1114- set_invalid_key_length_error(); \
1115- return NULL; \
1116- } \
1117- GET_BUFFER_VIEW_OR_ERROR((MSG), &msgview, \
1118- PyBuffer_Release(&keyview); \
1119- return NULL); \
1120- if (!has_uint32_t_buffer_length(&msgview)) { \
1121- PyBuffer_Release(&msgview); \
1122- PyBuffer_Release(&keyview); \
1123- set_invalid_msg_length_error(); \
1124- return NULL; \
1125- } \
1126- uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \
1127- Py_hmac_## HACL_HID ##_compute_func( \
1128- out, \
1129- (uint8_t *)keyview.buf, (uint32_t)keyview.len, \
1130- (uint8_t *)msgview.buf, (uint32_t)msgview.len \
1131- ); \
1132- PyBuffer_Release(&msgview); \
1133- PyBuffer_Release(&keyview); \
1134- return PyBytes_FromStringAndSize( \
1135- (const char *)out, \
1136- Py_hmac_## HACL_HID ##_digest_size \
1137- ); \
1138+ #define Py_HMAC_HACL_ONESHOT (HACL_HID , KEY , MSG ) \
1139+ do { \
1140+ Py_buffer keyview, msgview; \
1141+ if (hmac_get_buffer_views(key, &keyview, msg, &msgview) < 0) { \
1142+ return NULL; \
1143+ } \
1144+ uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \
1145+ Py_hmac_## HACL_HID ##_compute_func( \
1146+ out, \
1147+ (uint8_t *)keyview.buf, (uint32_t)keyview.len, \
1148+ (uint8_t *)msgview.buf, (uint32_t)msgview.len \
1149+ ); \
1150+ PyBuffer_Release(&msgview); \
1151+ PyBuffer_Release(&keyview); \
1152+ return PyBytes_FromStringAndSize( \
1153+ (const char *)out, \
1154+ Py_hmac_## HACL_HID ##_digest_size \
1155+ ); \
11381156 } while (0)
11391157
11401158/*[clinic input]
@@ -1329,6 +1347,8 @@ _hmac_compute_blake2b_32_impl(PyObject *module, PyObject *key, PyObject *msg)
13291347 Py_HMAC_HACL_ONESHOT (blake2b_32 , key , msg );
13301348}
13311349
1350+ #undef Py_HMAC_HACL_ONESHOT
1351+
13321352// --- HMAC module methods ----------------------------------------------------
13331353
13341354static PyMethodDef hmacmodule_methods [] = {
0 commit comments