Skip to content

Commit 0ecf2f4

Browse files
committed
add one-shot HMAC HACL* minimal API
This commit adds the interface needed to interact with one-shot HACL*-HMAC. One-shot HACL*-HMAC only supports keys or messages of 32-bit length.
1 parent 0629750 commit 0ecf2f4

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Modules/hmacmodule.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
#include "Python.h"
2020
#include "pycore_hashtable.h"
2121

22+
#include "_hacl/Hacl_HMAC.h"
2223
#include "_hacl/Hacl_Streaming_HMAC.h" // Hacl_Agile_Hash_* identifiers
24+
#include "_hacl/Hacl_Streaming_Types.h" // Hacl_Streaming_Types_error_code
2325

2426
#include <stdbool.h>
2527

2628
// --- HMAC underlying hash function static information -----------------------
2729

30+
#define UINT32_MAX_AS_SSIZE_T ((Py_ssize_t)UINT32_MAX)
31+
2832
#define Py_hmac_hash_max_block_size 128
2933
#define Py_hmac_hash_max_digest_size 64
3034

@@ -33,54 +37,78 @@
3337
#define Py_hmac_md5_block_size 64
3438
#define Py_hmac_md5_digest_size 16
3539

40+
#define Py_hmac_md5_compute_func Hacl_HMAC_compute_md5
41+
3642
/* SHA-1 family */
3743
// HACL_HID = sha1
3844
#define Py_hmac_sha1_block_size 64
3945
#define Py_hmac_sha1_digest_size 20
4046

47+
#define Py_hmac_sha1_compute_func Hacl_HMAC_compute_sha1
48+
4149
/* SHA-2 family */
4250
// HACL_HID = sha2_224
4351
#define Py_hmac_sha2_224_block_size 64
4452
#define Py_hmac_sha2_224_digest_size 28
4553

54+
#define Py_hmac_sha2_224_compute_func Hacl_HMAC_compute_sha2_224
55+
4656
// HACL_HID = sha2_256
4757
#define Py_hmac_sha2_256_block_size 64
4858
#define Py_hmac_sha2_256_digest_size 32
4959

60+
#define Py_hmac_sha2_256_compute_func Hacl_HMAC_compute_sha2_256
61+
5062
// HACL_HID = sha2_384
5163
#define Py_hmac_sha2_384_block_size 128
5264
#define Py_hmac_sha2_384_digest_size 48
5365

66+
#define Py_hmac_sha2_384_compute_func Hacl_HMAC_compute_sha2_384
67+
5468
// HACL_HID = sha2_512
5569
#define Py_hmac_sha2_512_block_size 128
5670
#define Py_hmac_sha2_512_digest_size 64
5771

72+
#define Py_hmac_sha2_512_compute_func Hacl_HMAC_compute_sha2_512
73+
5874
/* SHA-3 family */
5975
// HACL_HID = sha3_224
6076
#define Py_hmac_sha3_224_block_size 144
6177
#define Py_hmac_sha3_224_digest_size 28
6278

79+
#define Py_hmac_sha3_224_compute_func Hacl_HMAC_compute_sha3_224
80+
6381
// HACL_HID = sha3_256
6482
#define Py_hmac_sha3_256_block_size 136
6583
#define Py_hmac_sha3_256_digest_size 32
6684

85+
#define Py_hmac_sha3_256_compute_func Hacl_HMAC_compute_sha3_256
86+
6787
// HACL_HID = sha3_384
6888
#define Py_hmac_sha3_384_block_size 104
6989
#define Py_hmac_sha3_384_digest_size 48
7090

91+
#define Py_hmac_sha3_384_compute_func Hacl_HMAC_compute_sha3_384
92+
7193
// HACL_HID = sha3_512
7294
#define Py_hmac_sha3_512_block_size 72
7395
#define Py_hmac_sha3_512_digest_size 64
7496

97+
#define Py_hmac_sha3_512_compute_func Hacl_HMAC_compute_sha3_512
98+
7599
/* Blake2 family */
76100
// HACL_HID = blake2s_32
77101
#define Py_hmac_blake2s_32_block_size 64
78102
#define Py_hmac_blake2s_32_digest_size 32
79103

104+
#define Py_hmac_blake2s_32_compute_func Hacl_HMAC_compute_blake2s_32
105+
80106
// HACL_HID = blake2b_32
81107
#define Py_hmac_blake2b_32_block_size 128
82108
#define Py_hmac_blake2b_32_digest_size 64
83109

110+
#define Py_hmac_blake2b_32_compute_func Hacl_HMAC_compute_blake2b_32
111+
84112
/* Enumeration indicating the underlying hash function used by HMAC. */
85113
typedef enum HMAC_Hash_Kind {
86114
Py_hmac_kind_hash_unknown = -1,
@@ -106,6 +134,29 @@ typedef enum HMAC_Hash_Kind {
106134
#undef DECL_HACL_HMAC_HASH_KIND
107135
} HMAC_Hash_Kind;
108136

137+
typedef Hacl_Streaming_Types_error_code hacl_errno_t;
138+
139+
/* Function pointer type for 1-shot HACL* HMAC functions. */
140+
typedef void
141+
(*HACL_HMAC_compute_func)(uint8_t *out,
142+
uint8_t *key, uint32_t keylen,
143+
uint8_t *msg, uint32_t msglen);
144+
/* Function pointer type for 1-shot HACL* HMAC CPython AC functions. */
145+
typedef PyObject *
146+
(*PyAC_HMAC_compute_func)(PyObject *module, PyObject *key, PyObject *msg);
147+
148+
/*
149+
* HACL* HMAC minimal interface.
150+
*/
151+
typedef struct py_hmac_hacl_api {
152+
HACL_HMAC_compute_func compute;
153+
PyAC_HMAC_compute_func compute_py;
154+
} py_hmac_hacl_api;
155+
156+
#if PY_SSIZE_T_MAX > UINT32_MAX
157+
#define Py_HMAC_SSIZE_LARGER_THAN_UINT32
158+
#endif
159+
109160
/*
110161
* HMAC underlying hash function static information.
111162
*/
@@ -124,6 +175,9 @@ typedef struct py_hmac_hinfo {
124175
uint32_t block_size;
125176
uint32_t digest_size;
126177

178+
/* HACL* HMAC API */
179+
py_hmac_hacl_api api;
180+
127181
/*
128182
* Cached field storing the 'hashlib_name' field as a Python string.
129183
*
@@ -164,12 +218,20 @@ module _hmac
164218

165219
/* Static information used to construct the hash table. */
166220
static const py_hmac_hinfo py_hmac_static_hinfo[] = {
221+
#define Py_HMAC_HINFO_HACL_API(HACL_HID) \
222+
{ \
223+
/* one-shot helpers */ \
224+
.compute = &Py_hmac_## HACL_HID ##_compute_func, \
225+
.compute_py = NULL, \
226+
}
227+
167228
#define Py_HMAC_HINFO_ENTRY(HACL_HID, HLIB_NAME) \
168229
{ \
169230
.name = Py_STRINGIFY(HACL_HID), \
170231
.kind = Py_hmac_kind_hmac_ ## HACL_HID, \
171232
.block_size = Py_hmac_## HACL_HID ##_block_size, \
172233
.digest_size = Py_hmac_## HACL_HID ##_digest_size, \
234+
.api = Py_HMAC_HINFO_HACL_API(HACL_HID), \
173235
.display_name = NULL, \
174236
.hashlib_name = HLIB_NAME, \
175237
.refcnt = 0, \
@@ -192,9 +254,11 @@ static const py_hmac_hinfo py_hmac_static_hinfo[] = {
192254
Py_HMAC_HINFO_ENTRY(blake2s_32, "blake2s"),
193255
Py_HMAC_HINFO_ENTRY(blake2b_32, "blake2b"),
194256
#undef Py_HMAC_HINFO_ENTRY
257+
#undef Py_HMAC_HINFO_HACL_API
195258
/* sentinel */
196259
{
197260
NULL, Py_hmac_kind_hash_unknown, 0, 0,
261+
{NULL, NULL},
198262
NULL, NULL,
199263
0,
200264
},

0 commit comments

Comments
 (0)