@@ -1259,42 +1259,60 @@ _hmac_compute_digest_impl(PyObject *module, PyObject *key, PyObject *msg,
1259
1259
}
1260
1260
1261
1261
/*
1262
- * One-shot HMAC-HASH using the given HACL_HID.
1262
+ * Obtain a view for 'key' and 'msg', storing it in 'keyview' and 'msgview'.
1263
+ *
1264
+ * Return 0 on success; otherwise set an exception and return -1.
1263
1265
*
1264
1266
* The length of the key and message buffers must not exceed UINT32_MAX,
1265
1267
* lest an OverflowError is raised. The Python implementation takes care
1266
1268
* of dispatching to the OpenSSL implementation in this case.
1267
1269
*/
1268
- #define Py_HMAC_HACL_ONESHOT (HACL_HID , KEY , MSG ) \
1269
- do { \
1270
- Py_buffer keyview, msgview; \
1271
- GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \
1272
- if (!has_uint32_t_buffer_length(&keyview)) { \
1273
- PyBuffer_Release(&keyview); \
1274
- set_invalid_key_length_error(); \
1275
- return NULL; \
1276
- } \
1277
- GET_BUFFER_VIEW_OR_ERROR((MSG), &msgview, \
1278
- PyBuffer_Release(&keyview); \
1279
- return NULL); \
1280
- if (!has_uint32_t_buffer_length(&msgview)) { \
1281
- PyBuffer_Release(&msgview); \
1282
- PyBuffer_Release(&keyview); \
1283
- set_invalid_msg_length_error(); \
1284
- return NULL; \
1285
- } \
1286
- uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \
1287
- Py_hmac_## HACL_HID ##_compute_func( \
1288
- out, \
1289
- (uint8_t *)keyview.buf, (uint32_t)keyview.len, \
1290
- (uint8_t *)msgview.buf, (uint32_t)msgview.len \
1291
- ); \
1292
- PyBuffer_Release(&msgview); \
1293
- PyBuffer_Release(&keyview); \
1294
- return PyBytes_FromStringAndSize( \
1295
- (const char *)out, \
1296
- Py_hmac_## HACL_HID ##_digest_size \
1297
- ); \
1270
+ static int
1271
+ hmac_get_buffer_views (PyObject * key , Py_buffer * keyview ,
1272
+ PyObject * msg , Py_buffer * msgview )
1273
+ {
1274
+ if (_Py_hashlib_get_buffer_view (key , keyview ) < 0 ) {
1275
+ return -1 ;
1276
+ }
1277
+ if (!has_uint32_t_buffer_length (keyview )) {
1278
+ PyBuffer_Release (keyview );
1279
+ set_invalid_key_length_error ();
1280
+ return -1 ;
1281
+ }
1282
+ if (_Py_hashlib_get_buffer_view (msg , msgview ) < 0 ) {
1283
+ PyBuffer_Release (keyview );
1284
+ return -1 ;
1285
+ }
1286
+ if (!has_uint32_t_buffer_length (msgview )) {
1287
+ PyBuffer_Release (msgview );
1288
+ PyBuffer_Release (keyview );
1289
+ set_invalid_msg_length_error ();
1290
+ return -1 ;
1291
+ }
1292
+ return 0 ;
1293
+ }
1294
+
1295
+ /*
1296
+ * One-shot HMAC-HASH using the given HACL_HID.
1297
+ */
1298
+ #define HACL_HMAC_COMPUTE_NAMED_DIGEST (HACL_HID , KEY , MSG ) \
1299
+ do { \
1300
+ Py_buffer keyview, msgview; \
1301
+ if (hmac_get_buffer_views(key, &keyview, msg, &msgview) < 0) { \
1302
+ return NULL; \
1303
+ } \
1304
+ uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \
1305
+ Py_hmac_## HACL_HID ##_compute_func( \
1306
+ out, \
1307
+ (uint8_t *)keyview.buf, (uint32_t)keyview.len, \
1308
+ (uint8_t *)msgview.buf, (uint32_t)msgview.len \
1309
+ ); \
1310
+ PyBuffer_Release(&msgview); \
1311
+ PyBuffer_Release(&keyview); \
1312
+ return PyBytes_FromStringAndSize( \
1313
+ (const char *)out, \
1314
+ Py_hmac_## HACL_HID ##_digest_size \
1315
+ ); \
1298
1316
} while (0)
1299
1317
1300
1318
/*[clinic input]
@@ -1310,7 +1328,7 @@ static PyObject *
1310
1328
_hmac_compute_md5_impl (PyObject * module , PyObject * key , PyObject * msg )
1311
1329
/*[clinic end generated code: output=7837a4ceccbbf636 input=77a4b774c7d61218]*/
1312
1330
{
1313
- Py_HMAC_HACL_ONESHOT (md5 , key , msg );
1331
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (md5 , key , msg );
1314
1332
}
1315
1333
1316
1334
/*[clinic input]
@@ -1326,7 +1344,7 @@ static PyObject *
1326
1344
_hmac_compute_sha1_impl (PyObject * module , PyObject * key , PyObject * msg )
1327
1345
/*[clinic end generated code: output=79fd7689c83691d8 input=3b64dccc6bdbe4ba]*/
1328
1346
{
1329
- Py_HMAC_HACL_ONESHOT (sha1 , key , msg );
1347
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha1 , key , msg );
1330
1348
}
1331
1349
1332
1350
/*[clinic input]
@@ -1342,7 +1360,7 @@ static PyObject *
1342
1360
_hmac_compute_sha2_224_impl (PyObject * module , PyObject * key , PyObject * msg )
1343
1361
/*[clinic end generated code: output=7f21f1613e53979e input=a1a75f25f23449af]*/
1344
1362
{
1345
- Py_HMAC_HACL_ONESHOT (sha2_224 , key , msg );
1363
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha2_224 , key , msg );
1346
1364
}
1347
1365
1348
1366
/*[clinic input]
@@ -1358,7 +1376,7 @@ static PyObject *
1358
1376
_hmac_compute_sha2_256_impl (PyObject * module , PyObject * key , PyObject * msg )
1359
1377
/*[clinic end generated code: output=d4a291f7d9a82459 input=5c9ccf2df048ace3]*/
1360
1378
{
1361
- Py_HMAC_HACL_ONESHOT (sha2_256 , key , msg );
1379
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha2_256 , key , msg );
1362
1380
}
1363
1381
1364
1382
/*[clinic input]
@@ -1374,7 +1392,7 @@ static PyObject *
1374
1392
_hmac_compute_sha2_384_impl (PyObject * module , PyObject * key , PyObject * msg )
1375
1393
/*[clinic end generated code: output=f211fa26e3700c27 input=2fee2c14766af231]*/
1376
1394
{
1377
- Py_HMAC_HACL_ONESHOT (sha2_384 , key , msg );
1395
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha2_384 , key , msg );
1378
1396
}
1379
1397
1380
1398
/*[clinic input]
@@ -1390,7 +1408,7 @@ static PyObject *
1390
1408
_hmac_compute_sha2_512_impl (PyObject * module , PyObject * key , PyObject * msg )
1391
1409
/*[clinic end generated code: output=d5c20373762cecca input=3371eaac315c7864]*/
1392
1410
{
1393
- Py_HMAC_HACL_ONESHOT (sha2_512 , key , msg );
1411
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha2_512 , key , msg );
1394
1412
}
1395
1413
1396
1414
/*[clinic input]
@@ -1406,7 +1424,7 @@ static PyObject *
1406
1424
_hmac_compute_sha3_224_impl (PyObject * module , PyObject * key , PyObject * msg )
1407
1425
/*[clinic end generated code: output=a242ccac9ad9c22b input=d0ab0c7d189c3d87]*/
1408
1426
{
1409
- Py_HMAC_HACL_ONESHOT (sha3_224 , key , msg );
1427
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha3_224 , key , msg );
1410
1428
}
1411
1429
1412
1430
/*[clinic input]
@@ -1422,7 +1440,7 @@ static PyObject *
1422
1440
_hmac_compute_sha3_256_impl (PyObject * module , PyObject * key , PyObject * msg )
1423
1441
/*[clinic end generated code: output=b539dbb61af2fe0b input=f05d7b6364b35d02]*/
1424
1442
{
1425
- Py_HMAC_HACL_ONESHOT (sha3_256 , key , msg );
1443
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha3_256 , key , msg );
1426
1444
}
1427
1445
1428
1446
/*[clinic input]
@@ -1438,7 +1456,7 @@ static PyObject *
1438
1456
_hmac_compute_sha3_384_impl (PyObject * module , PyObject * key , PyObject * msg )
1439
1457
/*[clinic end generated code: output=5eb372fb5c4ffd3a input=d842d393e7aa05ae]*/
1440
1458
{
1441
- Py_HMAC_HACL_ONESHOT (sha3_384 , key , msg );
1459
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha3_384 , key , msg );
1442
1460
}
1443
1461
1444
1462
/*[clinic input]
@@ -1454,7 +1472,7 @@ static PyObject *
1454
1472
_hmac_compute_sha3_512_impl (PyObject * module , PyObject * key , PyObject * msg )
1455
1473
/*[clinic end generated code: output=154bcbf8c2eacac1 input=166fe5baaeaabfde]*/
1456
1474
{
1457
- Py_HMAC_HACL_ONESHOT (sha3_512 , key , msg );
1475
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha3_512 , key , msg );
1458
1476
}
1459
1477
1460
1478
/*[clinic input]
@@ -1470,7 +1488,7 @@ static PyObject *
1470
1488
_hmac_compute_blake2s_32_impl (PyObject * module , PyObject * key , PyObject * msg )
1471
1489
/*[clinic end generated code: output=cfc730791bc62361 input=d22c36e7fe31a985]*/
1472
1490
{
1473
- Py_HMAC_HACL_ONESHOT (blake2s_32 , key , msg );
1491
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (blake2s_32 , key , msg );
1474
1492
}
1475
1493
1476
1494
/*[clinic input]
@@ -1486,9 +1504,11 @@ static PyObject *
1486
1504
_hmac_compute_blake2b_32_impl (PyObject * module , PyObject * key , PyObject * msg )
1487
1505
/*[clinic end generated code: output=765c5c4fb9124636 input=4a35ee058d172f4b]*/
1488
1506
{
1489
- Py_HMAC_HACL_ONESHOT (blake2b_32 , key , msg );
1507
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (blake2b_32 , key , msg );
1490
1508
}
1491
1509
1510
+ #undef HACL_HMAC_COMPUTE_NAMED_DIGEST
1511
+
1492
1512
// --- HMAC module methods ----------------------------------------------------
1493
1513
1494
1514
static PyMethodDef hmacmodule_methods [] = {
0 commit comments