Skip to content

Commit 9fb6300

Browse files
committed
reduce the possibility of typos
1 parent e714135 commit 9fb6300

File tree

1 file changed

+75
-67
lines changed

1 file changed

+75
-67
lines changed

Modules/hmacmodule.c

Lines changed: 75 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,83 @@ typedef void (*HACL_HMAC_digest_func_t)(uint8_t *out,
1616
// HMAC underlying hash function static information.
1717

1818
/* MD-5 */
19+
// (HACL_HID = md5)
1920
#define Py_hmac_md5_block_size 64
2021
#define Py_hmac_md5_digest_size 16
22+
#define Py_hmac_md5_update_func NULL
2123
#define Py_hmac_md5_digest_func Hacl_HMAC_compute_md5
2224

2325
/* SHA-1 family */
26+
// HACL_HID = sha1
2427
#define Py_hmac_sha1_block_size 64
2528
#define Py_hmac_sha1_digest_size 20
29+
#define Py_hmac_sha1_update_func NULL
2630
#define Py_hmac_sha1_digest_func Hacl_HMAC_compute_sha1
2731

2832
/* SHA-2 family */
33+
// HACL_HID = sha2_224
2934
#define Py_hmac_sha2_224_block_size 64
3035
#define Py_hmac_sha2_224_digest_size 28
36+
#define Py_hmac_sha2_224_update_func NULL
3137
#define Py_hmac_sha2_224_digest_func Hacl_HMAC_compute_sha2_224
3238

39+
// HACL_HID = sha2_256
3340
#define Py_hmac_sha2_256_block_size 64
3441
#define Py_hmac_sha2_256_digest_size 32
42+
#define Py_hmac_sha2_256_update_func NULL
3543
#define Py_hmac_sha2_256_digest_func Hacl_HMAC_compute_sha2_256
3644

45+
// HACL_HID = sha2_384
3746
#define Py_hmac_sha2_384_block_size 128
3847
#define Py_hmac_sha2_384_digest_size 48
48+
#define Py_hmac_sha2_384_update_func NULL
3949
#define Py_hmac_sha2_384_digest_func Hacl_HMAC_compute_sha2_384
4050

51+
// HACL_HID = sha2_512
4152
#define Py_hmac_sha2_512_block_size 128
4253
#define Py_hmac_sha2_512_digest_size 64
54+
#define Py_hmac_sha2_512_update_func NULL
4355
#define Py_hmac_sha2_512_digest_func Hacl_HMAC_compute_sha2_512
4456

4557
/* SHA-3 family */
58+
// HACL_HID = sha3_224
4659
#define Py_hmac_sha3_224_block_size 144
4760
#define Py_hmac_sha3_224_digest_size 28
61+
#define Py_hmac_sha3_224_update_func NULL
4862
#define Py_hmac_sha3_224_digest_func Hacl_HMAC_compute_sha3_224
4963

64+
// HACL_HID = sha3_256
5065
#define Py_hmac_sha3_256_block_size 136
5166
#define Py_hmac_sha3_256_digest_size 32
67+
#define Py_hmac_sha3_256_update_func NULL
5268
#define Py_hmac_sha3_256_digest_func Hacl_HMAC_compute_sha3_256
5369

70+
// HACL_HID = sha3_384
5471
#define Py_hmac_sha3_384_block_size 104
5572
#define Py_hmac_sha3_384_digest_size 48
73+
#define Py_hmac_sha3_384_update_func NULL
5674
#define Py_hmac_sha3_384_digest_func Hacl_HMAC_compute_sha3_384
5775

76+
// HACL_HID = sha3_512
5877
#define Py_hmac_sha3_512_block_size 72
5978
#define Py_hmac_sha3_512_digest_size 64
79+
#define Py_hmac_sha3_512_update_func NULL
6080
#define Py_hmac_sha3_512_digest_func Hacl_HMAC_compute_sha3_512
6181

6282
/* Blake family */
63-
#define Py_hmac_blake2s_block_size 64
64-
#define Py_hmac_blake2s_digest_size 32
65-
#define Py_hmac_blake2s_digest_func Hacl_HMAC_compute_blake2s_32
83+
// HACL_HID = blake2s_32
84+
#define Py_hmac_blake2s_32_block_size 64
85+
#define Py_hmac_blake2s_32_digest_size 32
86+
#define Py_hmac_blake2s_32_update_func NULL
87+
#define Py_hmac_blake2s_32_digest_func Hacl_HMAC_compute_blake2s_32
6688

67-
#define Py_hmac_blake2b_block_size 128
68-
#define Py_hmac_blake2b_digest_size 64
69-
#define Py_hmac_blake2b_digest_func Hacl_HMAC_compute_blake2b_32
89+
// HACL_HID = blake2b_32
90+
#define Py_hmac_blake2b_32_block_size 128
91+
#define Py_hmac_blake2b_32_digest_size 64
92+
#define Py_hmac_blake2b_32_update_func NULL
93+
#define Py_hmac_blake2b_32_digest_func Hacl_HMAC_compute_blake2b_32
94+
95+
#define Py_hmac_hash_max_digest_size 64
7096

7197
/* Check that the buffer length fits on a uint32_t. */
7298
static inline int
@@ -79,31 +105,37 @@ has_uint32_t_buffer_length(const Py_buffer *buffer)
79105
#endif
80106
}
81107

82-
/* One-shot HMAC-HASH using the given HACL_HMAC_FUNCTION. */
83-
#define Py_HACL_HMAC_ONESHOT(HACL_HMAC_FUNCTION, DIGEST_SIZE, KEY, MSG) \
84-
do { \
85-
Py_buffer keyview, msgview; \
86-
GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \
87-
if (!has_uint32_t_buffer_length(&keyview)) { \
88-
PyErr_SetString(PyExc_ValueError, \
89-
"key length exceeds UINT32_MAX"); \
90-
return NULL; \
91-
} \
92-
GET_BUFFER_VIEW_OR_ERROUT((MSG), &msgview); \
93-
if (!has_uint32_t_buffer_length(&msgview)) { \
94-
PyErr_SetString(PyExc_ValueError, \
95-
"message length exceeds UINT32_MAX"); \
96-
return NULL; \
97-
} \
98-
uint8_t out[(DIGEST_SIZE)]; \
99-
HACL_HMAC_FUNCTION( \
100-
out, \
101-
(uint8_t *)keyview.buf, (uint32_t)keyview.len, \
102-
(uint8_t *)msgview.buf, (uint32_t)msgview.len \
103-
); \
104-
PyBuffer_Release(&msgview); \
105-
PyBuffer_Release(&keyview); \
106-
return PyBytes_FromStringAndSize((const char *)out, (DIGEST_SIZE)); \
108+
/* One-shot HMAC-HASH using the given HACL_HID. */
109+
#define Py_HACL_HMAC_ONESHOT(HACL_HID, KEY, MSG) \
110+
do { \
111+
Py_buffer keyview, msgview; \
112+
GET_BUFFER_VIEW_OR_ERROUT((KEY), &keyview); \
113+
if (!has_uint32_t_buffer_length(&keyview)) { \
114+
PyBuffer_Release(&keyview); \
115+
PyErr_SetString(PyExc_ValueError, \
116+
"key length exceeds UINT32_MAX"); \
117+
return NULL; \
118+
} \
119+
GET_BUFFER_VIEW_OR_ERROUT((MSG), &msgview); \
120+
if (!has_uint32_t_buffer_length(&msgview)) { \
121+
PyBuffer_Release(&msgview); \
122+
PyBuffer_Release(&keyview); \
123+
PyErr_SetString(PyExc_ValueError, \
124+
"message length exceeds UINT32_MAX"); \
125+
return NULL; \
126+
} \
127+
uint8_t out[Py_hmac_## HACL_HID ##_digest_size]; \
128+
Py_hmac_## HACL_HID ##_digest_func( \
129+
out, \
130+
(uint8_t *)keyview.buf, (uint32_t)keyview.len, \
131+
(uint8_t *)msgview.buf, (uint32_t)msgview.len \
132+
); \
133+
PyBuffer_Release(&msgview); \
134+
PyBuffer_Release(&keyview); \
135+
return PyBytes_FromStringAndSize( \
136+
(const char *)out, \
137+
Py_hmac_## HACL_HID ##_digest_size \
138+
); \
107139
} while (0)
108140

109141
/*[clinic input]
@@ -124,9 +156,7 @@ static PyObject *
124156
_hmac_compute_md5_impl(PyObject *module, PyObject *key, PyObject *msg)
125157
/*[clinic end generated code: output=7837a4ceccbbf636 input=77a4b774c7d61218]*/
126158
{
127-
Py_HACL_HMAC_ONESHOT(Py_hmac_md5_digest_func,
128-
Py_hmac_md5_digest_size,
129-
key, msg);
159+
Py_HACL_HMAC_ONESHOT(md5, key, msg);
130160
}
131161

132162
/*[clinic input]
@@ -142,9 +172,7 @@ static PyObject *
142172
_hmac_compute_sha1_impl(PyObject *module, PyObject *key, PyObject *msg)
143173
/*[clinic end generated code: output=79fd7689c83691d8 input=3b64dccc6bdbe4ba]*/
144174
{
145-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha1_digest_func,
146-
Py_hmac_sha1_digest_size,
147-
key, msg);
175+
Py_HACL_HMAC_ONESHOT(sha1, key, msg);
148176
}
149177

150178
/*[clinic input]
@@ -160,9 +188,7 @@ static PyObject *
160188
_hmac_compute_sha2_224_impl(PyObject *module, PyObject *key, PyObject *msg)
161189
/*[clinic end generated code: output=7f21f1613e53979e input=bcaac7a3637484ce]*/
162190
{
163-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha2_224_digest_func,
164-
Py_hmac_sha2_224_digest_size,
165-
key, msg);
191+
Py_HACL_HMAC_ONESHOT(sha2_224, key, msg);
166192
}
167193

168194
/*[clinic input]
@@ -178,9 +204,7 @@ static PyObject *
178204
_hmac_compute_sha2_256_impl(PyObject *module, PyObject *key, PyObject *msg)
179205
/*[clinic end generated code: output=d4a291f7d9a82459 input=6e2d1f6fe9c56d21]*/
180206
{
181-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha2_256_digest_func,
182-
Py_hmac_sha2_256_digest_size,
183-
key, msg);
207+
Py_HACL_HMAC_ONESHOT(sha2_256, key, msg);
184208
}
185209

186210
/*[clinic input]
@@ -196,9 +220,7 @@ static PyObject *
196220
_hmac_compute_sha2_384_impl(PyObject *module, PyObject *key, PyObject *msg)
197221
/*[clinic end generated code: output=f211fa26e3700c27 input=9ce8de89dda79e62]*/
198222
{
199-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha2_384_digest_func,
200-
Py_hmac_sha2_384_digest_size,
201-
key, msg);
223+
Py_HACL_HMAC_ONESHOT(sha2_384, key, msg);
202224
}
203225

204226
/*[clinic input]
@@ -214,9 +236,7 @@ static PyObject *
214236
_hmac_compute_sha2_512_impl(PyObject *module, PyObject *key, PyObject *msg)
215237
/*[clinic end generated code: output=d5c20373762cecca input=b964bb8487d7debd]*/
216238
{
217-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha2_512_digest_func,
218-
Py_hmac_sha2_512_digest_size,
219-
key, msg);
239+
Py_HACL_HMAC_ONESHOT(sha2_512, key, msg);
220240
}
221241

222242
/*[clinic input]
@@ -232,9 +252,7 @@ static PyObject *
232252
_hmac_compute_sha3_224_impl(PyObject *module, PyObject *key, PyObject *msg)
233253
/*[clinic end generated code: output=a242ccac9ad9c22b input=d0ab0c7d189c3d87]*/
234254
{
235-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha3_224_digest_func,
236-
Py_hmac_sha3_224_digest_size,
237-
key, msg);
255+
Py_HACL_HMAC_ONESHOT(sha3_224, key, msg);
238256
}
239257

240258
/*[clinic input]
@@ -250,9 +268,7 @@ static PyObject *
250268
_hmac_compute_sha3_256_impl(PyObject *module, PyObject *key, PyObject *msg)
251269
/*[clinic end generated code: output=b539dbb61af2fe0b input=f05d7b6364b35d02]*/
252270
{
253-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha3_256_digest_func,
254-
Py_hmac_sha3_256_digest_size,
255-
key, msg);
271+
Py_HACL_HMAC_ONESHOT(sha3_256, key, msg);
256272
}
257273

258274
/*[clinic input]
@@ -268,9 +284,7 @@ static PyObject *
268284
_hmac_compute_sha3_384_impl(PyObject *module, PyObject *key, PyObject *msg)
269285
/*[clinic end generated code: output=5eb372fb5c4ffd3a input=d842d393e7aa05ae]*/
270286
{
271-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha3_384_digest_func,
272-
Py_hmac_sha3_384_digest_size,
273-
key, msg);
287+
Py_HACL_HMAC_ONESHOT(sha3_384, key, msg);
274288
}
275289

276290
/*[clinic input]
@@ -286,9 +300,7 @@ static PyObject *
286300
_hmac_compute_sha3_512_impl(PyObject *module, PyObject *key, PyObject *msg)
287301
/*[clinic end generated code: output=154bcbf8c2eacac1 input=166fe5baaeaabfde]*/
288302
{
289-
Py_HACL_HMAC_ONESHOT(Py_hmac_sha3_512_digest_func,
290-
Py_hmac_sha3_512_digest_size,
291-
key, msg);
303+
Py_HACL_HMAC_ONESHOT(sha3_512, key, msg);
292304
}
293305

294306
/*[clinic input]
@@ -304,9 +316,7 @@ static PyObject *
304316
_hmac_compute_blake2s_32_impl(PyObject *module, PyObject *key, PyObject *msg)
305317
/*[clinic end generated code: output=cfc730791bc62361 input=d22c36e7fe31a985]*/
306318
{
307-
Py_HACL_HMAC_ONESHOT(Py_hmac_blake2s_digest_func,
308-
Py_hmac_blake2s_digest_size,
309-
key, msg);
319+
Py_HACL_HMAC_ONESHOT(blake2s_32, key, msg);
310320
}
311321

312322
/*[clinic input]
@@ -322,9 +332,7 @@ static PyObject *
322332
_hmac_compute_blake2b_32_impl(PyObject *module, PyObject *key, PyObject *msg)
323333
/*[clinic end generated code: output=765c5c4fb9124636 input=4a35ee058d172f4b]*/
324334
{
325-
Py_HACL_HMAC_ONESHOT(Py_hmac_blake2b_digest_func,
326-
Py_hmac_blake2b_digest_size,
327-
key, msg);
335+
Py_HACL_HMAC_ONESHOT(blake2b_32, key, msg);
328336
}
329337

330338
static PyMethodDef hmacmodule_methods[] = {

0 commit comments

Comments
 (0)