Skip to content

Commit b4d5d1e

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 3c140fa commit b4d5d1e

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
@@ -20,12 +20,16 @@
2020
#include "Python.h"
2121
#include "pycore_hashtable.h"
2222

23+
#include "_hacl/Hacl_HMAC.h"
2324
#include "_hacl/Hacl_Streaming_HMAC.h" // Hacl_Agile_Hash_* identifiers
25+
#include "_hacl/Hacl_Streaming_Types.h" // Hacl_Streaming_Types_error_code
2426

2527
#include <stdbool.h>
2628

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

31+
#define UINT32_MAX_AS_SSIZE_T ((Py_ssize_t)UINT32_MAX)
32+
2933
#define Py_hmac_hash_max_block_size 128
3034
#define Py_hmac_hash_max_digest_size 64
3135

@@ -34,54 +38,78 @@
3438
#define Py_hmac_md5_block_size 64
3539
#define Py_hmac_md5_digest_size 16
3640

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

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

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

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

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

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

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

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

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

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

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

111+
#define Py_hmac_blake2b_32_compute_func Hacl_HMAC_compute_blake2b_32
112+
85113
/* Enumeration indicating the underlying hash function used by HMAC. */
86114
typedef enum HMAC_Hash_Kind {
87115
Py_hmac_kind_hash_unknown = -1,
@@ -107,6 +135,29 @@ typedef enum HMAC_Hash_Kind {
107135
#undef DECL_HACL_HMAC_HASH_KIND
108136
} HMAC_Hash_Kind;
109137

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

179+
/* HACL* HMAC API */
180+
py_hmac_hacl_api api;
181+
128182
/*
129183
* Cached field storing the 'hashlib_name' field as a Python string.
130184
*
@@ -165,12 +219,20 @@ module _hmac
165219

166220
/* Static information used to construct the hash table. */
167221
static const py_hmac_hinfo py_hmac_static_hinfo[] = {
222+
#define Py_HMAC_HINFO_HACL_API(HACL_HID) \
223+
{ \
224+
/* one-shot helpers */ \
225+
.compute = &Py_hmac_## HACL_HID ##_compute_func, \
226+
.compute_py = NULL, \
227+
}
228+
168229
#define Py_HMAC_HINFO_ENTRY(HACL_HID, HLIB_NAME) \
169230
{ \
170231
.name = Py_STRINGIFY(HACL_HID), \
171232
.kind = Py_hmac_kind_hmac_ ## HACL_HID, \
172233
.block_size = Py_hmac_## HACL_HID ##_block_size, \
173234
.digest_size = Py_hmac_## HACL_HID ##_digest_size, \
235+
.api = Py_HMAC_HINFO_HACL_API(HACL_HID), \
174236
.display_name = NULL, \
175237
.hashlib_name = HLIB_NAME, \
176238
.refcnt = 0, \
@@ -193,9 +255,11 @@ static const py_hmac_hinfo py_hmac_static_hinfo[] = {
193255
Py_HMAC_HINFO_ENTRY(blake2s_32, "blake2s"),
194256
Py_HMAC_HINFO_ENTRY(blake2b_32, "blake2b"),
195257
#undef Py_HMAC_HINFO_ENTRY
258+
#undef Py_HMAC_HINFO_HACL_API
196259
/* sentinel */
197260
{
198261
NULL, Py_hmac_kind_hash_unknown, 0, 0,
262+
{NULL, NULL},
199263
NULL, NULL,
200264
0,
201265
},

0 commit comments

Comments
 (0)