@@ -1099,42 +1099,60 @@ _hmac_compute_digest_impl(PyObject *module, PyObject *key, PyObject *msg,
1099
1099
}
1100
1100
1101
1101
/*
1102
- * One-shot HMAC-HASH using the given HACL_HID.
1102
+ * Obtain a view for 'key' and 'msg', storing it in 'keyview' and 'msgview'.
1103
+ *
1104
+ * Return 0 on success; otherwise set an exception and return -1.
1103
1105
*
1104
1106
* The length of the key and message buffers must not exceed UINT32_MAX,
1105
1107
* lest an OverflowError is raised. The Python implementation takes care
1106
1108
* of dispatching to the OpenSSL implementation in this case.
1107
1109
*/
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
- ); \
1110
+ static int
1111
+ hmac_get_buffer_views (PyObject * key , Py_buffer * keyview ,
1112
+ PyObject * msg , Py_buffer * msgview )
1113
+ {
1114
+ if (_Py_hashlib_get_buffer_view (key , keyview ) < 0 ) {
1115
+ return -1 ;
1116
+ }
1117
+ if (!has_uint32_t_buffer_length (keyview )) {
1118
+ PyBuffer_Release (keyview );
1119
+ set_invalid_key_length_error ();
1120
+ return -1 ;
1121
+ }
1122
+ if (_Py_hashlib_get_buffer_view (msg , msgview ) < 0 ) {
1123
+ PyBuffer_Release (keyview );
1124
+ return -1 ;
1125
+ }
1126
+ if (!has_uint32_t_buffer_length (msgview )) {
1127
+ PyBuffer_Release (msgview );
1128
+ PyBuffer_Release (keyview );
1129
+ set_invalid_msg_length_error ();
1130
+ return -1 ;
1131
+ }
1132
+ return 0 ;
1133
+ }
1134
+
1135
+ /*
1136
+ * One-shot HMAC-HASH using the given HACL_HID.
1137
+ */
1138
+ #define HACL_HMAC_COMPUTE_NAMED_DIGEST (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
+ ); \
1138
1156
} while (0)
1139
1157
1140
1158
/*[clinic input]
@@ -1150,7 +1168,7 @@ static PyObject *
1150
1168
_hmac_compute_md5_impl (PyObject * module , PyObject * key , PyObject * msg )
1151
1169
/*[clinic end generated code: output=7837a4ceccbbf636 input=77a4b774c7d61218]*/
1152
1170
{
1153
- Py_HMAC_HACL_ONESHOT (md5 , key , msg );
1171
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (md5 , key , msg );
1154
1172
}
1155
1173
1156
1174
/*[clinic input]
@@ -1166,7 +1184,7 @@ static PyObject *
1166
1184
_hmac_compute_sha1_impl (PyObject * module , PyObject * key , PyObject * msg )
1167
1185
/*[clinic end generated code: output=79fd7689c83691d8 input=3b64dccc6bdbe4ba]*/
1168
1186
{
1169
- Py_HMAC_HACL_ONESHOT (sha1 , key , msg );
1187
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha1 , key , msg );
1170
1188
}
1171
1189
1172
1190
/*[clinic input]
@@ -1182,7 +1200,7 @@ static PyObject *
1182
1200
_hmac_compute_sha2_224_impl (PyObject * module , PyObject * key , PyObject * msg )
1183
1201
/*[clinic end generated code: output=7f21f1613e53979e input=a1a75f25f23449af]*/
1184
1202
{
1185
- Py_HMAC_HACL_ONESHOT (sha2_224 , key , msg );
1203
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha2_224 , key , msg );
1186
1204
}
1187
1205
1188
1206
/*[clinic input]
@@ -1198,7 +1216,7 @@ static PyObject *
1198
1216
_hmac_compute_sha2_256_impl (PyObject * module , PyObject * key , PyObject * msg )
1199
1217
/*[clinic end generated code: output=d4a291f7d9a82459 input=5c9ccf2df048ace3]*/
1200
1218
{
1201
- Py_HMAC_HACL_ONESHOT (sha2_256 , key , msg );
1219
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha2_256 , key , msg );
1202
1220
}
1203
1221
1204
1222
/*[clinic input]
@@ -1214,7 +1232,7 @@ static PyObject *
1214
1232
_hmac_compute_sha2_384_impl (PyObject * module , PyObject * key , PyObject * msg )
1215
1233
/*[clinic end generated code: output=f211fa26e3700c27 input=2fee2c14766af231]*/
1216
1234
{
1217
- Py_HMAC_HACL_ONESHOT (sha2_384 , key , msg );
1235
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha2_384 , key , msg );
1218
1236
}
1219
1237
1220
1238
/*[clinic input]
@@ -1230,7 +1248,7 @@ static PyObject *
1230
1248
_hmac_compute_sha2_512_impl (PyObject * module , PyObject * key , PyObject * msg )
1231
1249
/*[clinic end generated code: output=d5c20373762cecca input=3371eaac315c7864]*/
1232
1250
{
1233
- Py_HMAC_HACL_ONESHOT (sha2_512 , key , msg );
1251
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha2_512 , key , msg );
1234
1252
}
1235
1253
1236
1254
/*[clinic input]
@@ -1246,7 +1264,7 @@ static PyObject *
1246
1264
_hmac_compute_sha3_224_impl (PyObject * module , PyObject * key , PyObject * msg )
1247
1265
/*[clinic end generated code: output=a242ccac9ad9c22b input=d0ab0c7d189c3d87]*/
1248
1266
{
1249
- Py_HMAC_HACL_ONESHOT (sha3_224 , key , msg );
1267
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha3_224 , key , msg );
1250
1268
}
1251
1269
1252
1270
/*[clinic input]
@@ -1262,7 +1280,7 @@ static PyObject *
1262
1280
_hmac_compute_sha3_256_impl (PyObject * module , PyObject * key , PyObject * msg )
1263
1281
/*[clinic end generated code: output=b539dbb61af2fe0b input=f05d7b6364b35d02]*/
1264
1282
{
1265
- Py_HMAC_HACL_ONESHOT (sha3_256 , key , msg );
1283
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha3_256 , key , msg );
1266
1284
}
1267
1285
1268
1286
/*[clinic input]
@@ -1278,7 +1296,7 @@ static PyObject *
1278
1296
_hmac_compute_sha3_384_impl (PyObject * module , PyObject * key , PyObject * msg )
1279
1297
/*[clinic end generated code: output=5eb372fb5c4ffd3a input=d842d393e7aa05ae]*/
1280
1298
{
1281
- Py_HMAC_HACL_ONESHOT (sha3_384 , key , msg );
1299
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha3_384 , key , msg );
1282
1300
}
1283
1301
1284
1302
/*[clinic input]
@@ -1294,7 +1312,7 @@ static PyObject *
1294
1312
_hmac_compute_sha3_512_impl (PyObject * module , PyObject * key , PyObject * msg )
1295
1313
/*[clinic end generated code: output=154bcbf8c2eacac1 input=166fe5baaeaabfde]*/
1296
1314
{
1297
- Py_HMAC_HACL_ONESHOT (sha3_512 , key , msg );
1315
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (sha3_512 , key , msg );
1298
1316
}
1299
1317
1300
1318
/*[clinic input]
@@ -1310,7 +1328,7 @@ static PyObject *
1310
1328
_hmac_compute_blake2s_32_impl (PyObject * module , PyObject * key , PyObject * msg )
1311
1329
/*[clinic end generated code: output=cfc730791bc62361 input=d22c36e7fe31a985]*/
1312
1330
{
1313
- Py_HMAC_HACL_ONESHOT (blake2s_32 , key , msg );
1331
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (blake2s_32 , key , msg );
1314
1332
}
1315
1333
1316
1334
/*[clinic input]
@@ -1326,9 +1344,11 @@ static PyObject *
1326
1344
_hmac_compute_blake2b_32_impl (PyObject * module , PyObject * key , PyObject * msg )
1327
1345
/*[clinic end generated code: output=765c5c4fb9124636 input=4a35ee058d172f4b]*/
1328
1346
{
1329
- Py_HMAC_HACL_ONESHOT (blake2b_32 , key , msg );
1347
+ HACL_HMAC_COMPUTE_NAMED_DIGEST (blake2b_32 , key , msg );
1330
1348
}
1331
1349
1350
+ #undef HACL_HMAC_COMPUTE_NAMED_DIGEST
1351
+
1332
1352
// --- HMAC module methods ----------------------------------------------------
1333
1353
1334
1354
static PyMethodDef hmacmodule_methods [] = {
0 commit comments