Skip to content

Commit 0d39305

Browse files
committed
add HMAC-HASH static information
Static information for each hash function supported by HACL* HMAC is added (discriminated type, names, block size, digest size). By convention, `_hmac` defines its own standard names but allows hashlib-based aliases (e.g., "sha2_224" is the standard HMAC name that can also be obtained when using "sha224").
1 parent 7c7f401 commit 0d39305

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

Modules/hmacmodule.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,127 @@
1818
#endif
1919

2020
#include "Python.h"
21+
#include "pycore_hashtable.h"
22+
23+
#include "_hacl/Hacl_Streaming_HMAC.h" // Hacl_Agile_Hash_* identifiers
24+
25+
// --- HMAC underlying hash function static information -----------------------
26+
27+
#define Py_hmac_hash_max_block_size 128
28+
#define Py_hmac_hash_max_digest_size 64
29+
30+
/* MD-5 */
31+
// HACL_HID = md5
32+
#define Py_hmac_md5_block_size 64
33+
#define Py_hmac_md5_digest_size 16
34+
35+
/* SHA-1 family */
36+
// HACL_HID = sha1
37+
#define Py_hmac_sha1_block_size 64
38+
#define Py_hmac_sha1_digest_size 20
39+
40+
/* SHA-2 family */
41+
// HACL_HID = sha2_224
42+
#define Py_hmac_sha2_224_block_size 64
43+
#define Py_hmac_sha2_224_digest_size 28
44+
45+
// HACL_HID = sha2_256
46+
#define Py_hmac_sha2_256_block_size 64
47+
#define Py_hmac_sha2_256_digest_size 32
48+
49+
// HACL_HID = sha2_384
50+
#define Py_hmac_sha2_384_block_size 128
51+
#define Py_hmac_sha2_384_digest_size 48
52+
53+
// HACL_HID = sha2_512
54+
#define Py_hmac_sha2_512_block_size 128
55+
#define Py_hmac_sha2_512_digest_size 64
56+
57+
/* SHA-3 family */
58+
// HACL_HID = sha3_224
59+
#define Py_hmac_sha3_224_block_size 144
60+
#define Py_hmac_sha3_224_digest_size 28
61+
62+
// HACL_HID = sha3_256
63+
#define Py_hmac_sha3_256_block_size 136
64+
#define Py_hmac_sha3_256_digest_size 32
65+
66+
// HACL_HID = sha3_384
67+
#define Py_hmac_sha3_384_block_size 104
68+
#define Py_hmac_sha3_384_digest_size 48
69+
70+
// HACL_HID = sha3_512
71+
#define Py_hmac_sha3_512_block_size 72
72+
#define Py_hmac_sha3_512_digest_size 64
73+
74+
/* Blake2 family */
75+
// HACL_HID = blake2s_32
76+
#define Py_hmac_blake2s_32_block_size 64
77+
#define Py_hmac_blake2s_32_digest_size 32
78+
79+
// HACL_HID = blake2b_32
80+
#define Py_hmac_blake2b_32_block_size 128
81+
#define Py_hmac_blake2b_32_digest_size 64
82+
83+
/* Enumeration indicating the underlying hash function used by HMAC. */
84+
typedef enum HMAC_Hash_Kind {
85+
Py_hmac_kind_hash_unknown = -1,
86+
#define DECL_HACL_HMAC_HASH_KIND(NAME, HACL_NAME) \
87+
Py_hmac_kind_hmac_ ## NAME = Hacl_Agile_Hash_ ## HACL_NAME,
88+
/* MD5 */
89+
DECL_HACL_HMAC_HASH_KIND(md5, MD5)
90+
/* SHA-1 */
91+
DECL_HACL_HMAC_HASH_KIND(sha1, SHA1)
92+
/* SHA-2 family */
93+
DECL_HACL_HMAC_HASH_KIND(sha2_224, SHA2_224)
94+
DECL_HACL_HMAC_HASH_KIND(sha2_256, SHA2_256)
95+
DECL_HACL_HMAC_HASH_KIND(sha2_384, SHA2_384)
96+
DECL_HACL_HMAC_HASH_KIND(sha2_512, SHA2_512)
97+
/* SHA-3 family */
98+
DECL_HACL_HMAC_HASH_KIND(sha3_224, SHA3_224)
99+
DECL_HACL_HMAC_HASH_KIND(sha3_256, SHA3_256)
100+
DECL_HACL_HMAC_HASH_KIND(sha3_384, SHA3_384)
101+
DECL_HACL_HMAC_HASH_KIND(sha3_512, SHA3_512)
102+
/* Blake family */
103+
DECL_HACL_HMAC_HASH_KIND(blake2s_32, Blake2S_32)
104+
DECL_HACL_HMAC_HASH_KIND(blake2b_32, Blake2B_32)
105+
#undef DECL_HACL_HMAC_HASH_KIND
106+
} HMAC_Hash_Kind;
107+
108+
/*
109+
* HMAC underlying hash function static information.
110+
*/
111+
typedef struct py_hmac_hinfo {
112+
/*
113+
* Name of the hash function used by the HACL* HMAC module.
114+
*
115+
* This name may differ from the hashlib names. For instance,
116+
* SHA-2/224 is named "sha2_224" instead of "sha224" as it is
117+
* done by 'hashlib'.
118+
*/
119+
const char *name;
120+
121+
/* hash function information */
122+
HMAC_Hash_Kind kind;
123+
uint32_t block_size;
124+
uint32_t digest_size;
125+
126+
/*
127+
* Cached field storing the 'hashlib_name' field as a Python string.
128+
*
129+
* This field is NULL by default in the items of "py_hmac_static_hinfo"
130+
* but will be populated when creating the module's state "hinfo_table".
131+
*/
132+
PyObject *display_name;
133+
const char *hashlib_name; /* hashlib preferred name (default: name) */
134+
135+
Py_ssize_t refcnt;
136+
} py_hmac_hinfo;
21137

22138
// --- HMAC module state ------------------------------------------------------
23139

24140
typedef struct hmacmodule_state {
141+
_Py_hashtable_t *hinfo_table;
25142
} hmacmodule_state;
26143

27144
static inline hmacmodule_state *
@@ -39,6 +156,46 @@ module _hmac
39156
[clinic start generated code]*/
40157
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=799f0f10157d561f]*/
41158

159+
// --- Helpers ----------------------------------------------------------------
160+
161+
/* Static information used to construct the hash table. */
162+
static const py_hmac_hinfo py_hmac_static_hinfo[] = {
163+
#define Py_HMAC_HINFO_ENTRY(HACL_HID, HLIB_NAME) \
164+
{ \
165+
.name = Py_STRINGIFY(HACL_HID), \
166+
.kind = Py_hmac_kind_hmac_ ## HACL_HID, \
167+
.block_size = Py_hmac_## HACL_HID ##_block_size, \
168+
.digest_size = Py_hmac_## HACL_HID ##_digest_size, \
169+
.display_name = NULL, \
170+
.hashlib_name = HLIB_NAME, \
171+
.refcnt = 0, \
172+
}
173+
/* MD5 */
174+
Py_HMAC_HINFO_ENTRY(md5, NULL),
175+
/* SHA-1 */
176+
Py_HMAC_HINFO_ENTRY(sha1, NULL),
177+
/* SHA-2 family */
178+
Py_HMAC_HINFO_ENTRY(sha2_224, "sha224"),
179+
Py_HMAC_HINFO_ENTRY(sha2_256, "sha256"),
180+
Py_HMAC_HINFO_ENTRY(sha2_384, "sha384"),
181+
Py_HMAC_HINFO_ENTRY(sha2_512, "sha512"),
182+
/* SHA-3 family */
183+
Py_HMAC_HINFO_ENTRY(sha3_224, NULL),
184+
Py_HMAC_HINFO_ENTRY(sha3_256, NULL),
185+
Py_HMAC_HINFO_ENTRY(sha3_384, NULL),
186+
Py_HMAC_HINFO_ENTRY(sha3_512, NULL),
187+
/* Blake family */
188+
Py_HMAC_HINFO_ENTRY(blake2s_32, "blake2s"),
189+
Py_HMAC_HINFO_ENTRY(blake2b_32, "blake2b"),
190+
#undef Py_HMAC_HINFO_ENTRY
191+
/* sentinel */
192+
{
193+
NULL, Py_hmac_kind_hash_unknown, 0, 0,
194+
NULL, NULL,
195+
0,
196+
},
197+
};
198+
42199
// --- HMAC module methods ----------------------------------------------------
43200

44201
static PyMethodDef hmacmodule_methods[] = {

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Modules/cmathmodule.c - sqrt_special_values -
305305
Modules/cmathmodule.c - tanh_special_values -
306306
Modules/config.c - _PyImport_Inittab -
307307
Modules/faulthandler.c - faulthandler_handlers -
308+
Modules/faulthandler.c - py_hmac_static_hinfo -
308309
Modules/getnameinfo.c - gni_afdl -
309310
Modules/posixmodule.c os_getxattr_impl buffer_sizes -
310311
Modules/posixmodule.c os_listxattr_impl buffer_sizes -

0 commit comments

Comments
 (0)