|
50 | 50 |
|
51 | 51 | // --- Reusable error messages ------------------------------------------------
|
52 | 52 |
|
53 |
| -#define INVALID_KEY_LENGTH "key length exceeds UINT32_MAX" |
54 |
| -#define INVALID_MSG_LENGTH "message length exceeds UINT32_MAX" |
| 53 | +static inline void |
| 54 | +set_invalid_key_length_error(void) |
| 55 | +{ |
| 56 | + (void)PyErr_Format(PyExc_OverflowError, |
| 57 | + "key length exceeds %u", |
| 58 | + UINT32_MAX); |
| 59 | +} |
| 60 | + |
| 61 | +static inline void |
| 62 | +set_invalid_msg_length_error(void) |
| 63 | +{ |
| 64 | + (void)PyErr_Format(PyExc_OverflowError, |
| 65 | + "message length exceeds %u", |
| 66 | + UINT32_MAX); |
| 67 | +} |
55 | 68 |
|
56 | 69 | // --- HMAC underlying hash function static information -----------------------
|
57 | 70 |
|
@@ -760,7 +773,7 @@ hmac_new_initial_state(HMACObject *self, uint8_t *key, Py_ssize_t len)
|
760 | 773 | // not rely on HACL* implementation anymore. As such, we explicitly
|
761 | 774 | // reject keys that do not fit on 32 bits until HACL* handles them.
|
762 | 775 | if (len > UINT32_MAX_AS_SSIZE_T) {
|
763 |
| - PyErr_SetString(PyExc_OverflowError, INVALID_KEY_LENGTH); |
| 776 | + set_invalid_key_length_error(); |
764 | 777 | return -1;
|
765 | 778 | }
|
766 | 779 | #endif
|
@@ -1249,36 +1262,36 @@ _hmac_compute_digest_impl(PyObject *module, PyObject *key, PyObject *msg,
|
1249 | 1262 | * lest an OverflowError is raised. The Python implementation takes care
|
1250 | 1263 | * of dispatching to the OpenSSL implementation in this case.
|
1251 | 1264 | */
|
1252 |
| -#define Py_HMAC_HACL_ONESHOT(HACL_HID, KEY, MSG) \ |
1253 |
| - do { \ |
1254 |
| - Py_buffer keyview, msgview; \ |
1255 |
| - GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \ |
1256 |
| - if (!has_uint32_t_buffer_length(&keyview)) { \ |
1257 |
| - PyBuffer_Release(&keyview); \ |
1258 |
| - PyErr_SetString(PyExc_OverflowError, INVALID_KEY_LENGTH); \ |
1259 |
| - return NULL; \ |
1260 |
| - } \ |
1261 |
| - GET_BUFFER_VIEW_OR_ERROR((MSG), &msgview, \ |
1262 |
| - PyBuffer_Release(&keyview); \ |
1263 |
| - return NULL); \ |
1264 |
| - if (!has_uint32_t_buffer_length(&msgview)) { \ |
1265 |
| - PyBuffer_Release(&msgview); \ |
1266 |
| - PyBuffer_Release(&keyview); \ |
1267 |
| - PyErr_SetString(PyExc_OverflowError, INVALID_MSG_LENGTH); \ |
1268 |
| - return NULL; \ |
1269 |
| - } \ |
1270 |
| - uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \ |
1271 |
| - Py_hmac_## HACL_HID ##_compute_func( \ |
1272 |
| - out, \ |
1273 |
| - (uint8_t *)keyview.buf, (uint32_t)keyview.len, \ |
1274 |
| - (uint8_t *)msgview.buf, (uint32_t)msgview.len \ |
1275 |
| - ); \ |
1276 |
| - PyBuffer_Release(&msgview); \ |
1277 |
| - PyBuffer_Release(&keyview); \ |
1278 |
| - return PyBytes_FromStringAndSize( \ |
1279 |
| - (const char *)out, \ |
1280 |
| - Py_hmac_## HACL_HID ##_digest_size \ |
1281 |
| - ); \ |
| 1265 | +#define Py_HMAC_HACL_ONESHOT(HACL_HID, KEY, MSG) \ |
| 1266 | + do { \ |
| 1267 | + Py_buffer keyview, msgview; \ |
| 1268 | + GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \ |
| 1269 | + if (!has_uint32_t_buffer_length(&keyview)) { \ |
| 1270 | + PyBuffer_Release(&keyview); \ |
| 1271 | + set_invalid_key_length_error(); \ |
| 1272 | + return NULL; \ |
| 1273 | + } \ |
| 1274 | + GET_BUFFER_VIEW_OR_ERROR((MSG), &msgview, \ |
| 1275 | + PyBuffer_Release(&keyview); \ |
| 1276 | + return NULL); \ |
| 1277 | + if (!has_uint32_t_buffer_length(&msgview)) { \ |
| 1278 | + PyBuffer_Release(&msgview); \ |
| 1279 | + PyBuffer_Release(&keyview); \ |
| 1280 | + set_invalid_msg_length_error(); \ |
| 1281 | + return NULL; \ |
| 1282 | + } \ |
| 1283 | + uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \ |
| 1284 | + Py_hmac_## HACL_HID ##_compute_func( \ |
| 1285 | + out, \ |
| 1286 | + (uint8_t *)keyview.buf, (uint32_t)keyview.len, \ |
| 1287 | + (uint8_t *)msgview.buf, (uint32_t)msgview.len \ |
| 1288 | + ); \ |
| 1289 | + PyBuffer_Release(&msgview); \ |
| 1290 | + PyBuffer_Release(&keyview); \ |
| 1291 | + return PyBytes_FromStringAndSize( \ |
| 1292 | + (const char *)out, \ |
| 1293 | + Py_hmac_## HACL_HID ##_digest_size \ |
| 1294 | + ); \ |
1282 | 1295 | } while (0)
|
1283 | 1296 |
|
1284 | 1297 | /*[clinic input]
|
|
0 commit comments