1717#endif
1818
1919#include "Python.h"
20+ #include "pycore_hashtable.h"
21+
22+ #include "_hacl/Hacl_Streaming_HMAC.h" // Hacl_Agile_Hash_* identifiers
23+
24+ // --- HMAC underlying hash function static information -----------------------
25+
26+ #define Py_hmac_hash_max_block_size 128
27+ #define Py_hmac_hash_max_digest_size 64
28+
29+ /* MD-5 */
30+ // HACL_HID = md5
31+ #define Py_hmac_md5_block_size 64
32+ #define Py_hmac_md5_digest_size 16
33+
34+ /* SHA-1 family */
35+ // HACL_HID = sha1
36+ #define Py_hmac_sha1_block_size 64
37+ #define Py_hmac_sha1_digest_size 20
38+
39+ /* SHA-2 family */
40+ // HACL_HID = sha2_224
41+ #define Py_hmac_sha2_224_block_size 64
42+ #define Py_hmac_sha2_224_digest_size 28
43+
44+ // HACL_HID = sha2_256
45+ #define Py_hmac_sha2_256_block_size 64
46+ #define Py_hmac_sha2_256_digest_size 32
47+
48+ // HACL_HID = sha2_384
49+ #define Py_hmac_sha2_384_block_size 128
50+ #define Py_hmac_sha2_384_digest_size 48
51+
52+ // HACL_HID = sha2_512
53+ #define Py_hmac_sha2_512_block_size 128
54+ #define Py_hmac_sha2_512_digest_size 64
55+
56+ /* SHA-3 family */
57+ // HACL_HID = sha3_224
58+ #define Py_hmac_sha3_224_block_size 144
59+ #define Py_hmac_sha3_224_digest_size 28
60+
61+ // HACL_HID = sha3_256
62+ #define Py_hmac_sha3_256_block_size 136
63+ #define Py_hmac_sha3_256_digest_size 32
64+
65+ // HACL_HID = sha3_384
66+ #define Py_hmac_sha3_384_block_size 104
67+ #define Py_hmac_sha3_384_digest_size 48
68+
69+ // HACL_HID = sha3_512
70+ #define Py_hmac_sha3_512_block_size 72
71+ #define Py_hmac_sha3_512_digest_size 64
72+
73+ /* Blake2 family */
74+ // HACL_HID = blake2s_32
75+ #define Py_hmac_blake2s_32_block_size 64
76+ #define Py_hmac_blake2s_32_digest_size 32
77+
78+ // HACL_HID = blake2b_32
79+ #define Py_hmac_blake2b_32_block_size 128
80+ #define Py_hmac_blake2b_32_digest_size 64
81+
82+ /* Enumeration indicating the underlying hash function used by HMAC. */
83+ typedef enum HMAC_Hash_Kind {
84+ Py_hmac_kind_hash_unknown = -1 ,
85+ #define DECL_HACL_HMAC_HASH_KIND(NAME, HACL_NAME) \
86+ Py_hmac_kind_hmac_ ## NAME = Hacl_Agile_Hash_ ## HACL_NAME,
87+ /* MD5 */
88+ DECL_HACL_HMAC_HASH_KIND (md5 , MD5 )
89+ /* SHA-1 */
90+ DECL_HACL_HMAC_HASH_KIND (sha1 , SHA1 )
91+ /* SHA-2 family */
92+ DECL_HACL_HMAC_HASH_KIND (sha2_224 , SHA2_224 )
93+ DECL_HACL_HMAC_HASH_KIND (sha2_256 , SHA2_256 )
94+ DECL_HACL_HMAC_HASH_KIND (sha2_384 , SHA2_384 )
95+ DECL_HACL_HMAC_HASH_KIND (sha2_512 , SHA2_512 )
96+ /* SHA-3 family */
97+ DECL_HACL_HMAC_HASH_KIND (sha3_224 , SHA3_224 )
98+ DECL_HACL_HMAC_HASH_KIND (sha3_256 , SHA3_256 )
99+ DECL_HACL_HMAC_HASH_KIND (sha3_384 , SHA3_384 )
100+ DECL_HACL_HMAC_HASH_KIND (sha3_512 , SHA3_512 )
101+ /* Blake family */
102+ DECL_HACL_HMAC_HASH_KIND (blake2s_32 , Blake2S_32 )
103+ DECL_HACL_HMAC_HASH_KIND (blake2b_32 , Blake2B_32 )
104+ #undef DECL_HACL_HMAC_HASH_KIND
105+ } HMAC_Hash_Kind ;
106+
107+ /*
108+ * HMAC underlying hash function static information.
109+ */
110+ typedef struct py_hmac_hinfo {
111+ /*
112+ * Name of the hash function used by the HACL* HMAC module.
113+ *
114+ * This name may differ from the hashlib names. For instance,
115+ * SHA-2/224 is named "sha2_224" instead of "sha224" as it is
116+ * done by 'hashlib'.
117+ */
118+ const char * name ;
119+
120+ /* hash function information */
121+ HMAC_Hash_Kind kind ;
122+ uint32_t block_size ;
123+ uint32_t digest_size ;
124+
125+ /*
126+ * Cached field storing the 'hashlib_name' field as a Python string.
127+ *
128+ * This field is NULL by default in the items of "py_hmac_static_hinfo"
129+ * but will be populated when creating the module's state "hinfo_table".
130+ */
131+ PyObject * display_name ;
132+ const char * hashlib_name ; /* hashlib preferred name (default: name) */
133+
134+ Py_ssize_t refcnt ;
135+ } py_hmac_hinfo ;
20136
21137// --- HMAC module state ------------------------------------------------------
22138
23139typedef struct hmacmodule_state {
140+ _Py_hashtable_t * hinfo_table ;
24141} hmacmodule_state ;
25142
26143static inline hmacmodule_state *
@@ -38,6 +155,46 @@ module _hmac
38155[clinic start generated code]*/
39156/*[clinic end generated code: output=da39a3ee5e6b4b0d input=799f0f10157d561f]*/
40157
158+ // --- Helpers ----------------------------------------------------------------
159+
160+ /* Static information used to construct the hash table. */
161+ static const py_hmac_hinfo py_hmac_static_hinfo [] = {
162+ #define Py_HMAC_HINFO_ENTRY (HACL_HID , HLIB_NAME ) \
163+ { \
164+ .name = Py_STRINGIFY (HACL_HID ), \
165+ .kind = Py_hmac_kind_hmac_ ## HACL_HID , \
166+ .block_size = Py_hmac_## HACL_HID ##_block_size, \
167+ .digest_size = Py_hmac_## HACL_HID ##_digest_size, \
168+ .display_name = NULL, \
169+ .hashlib_name = HLIB_NAME, \
170+ .refcnt = 0, \
171+ }
172+ /* MD5 */
173+ Py_HMAC_HINFO_ENTRY (md5 , NULL ),
174+ /* SHA-1 */
175+ Py_HMAC_HINFO_ENTRY (sha1 , NULL ),
176+ /* SHA-2 family */
177+ Py_HMAC_HINFO_ENTRY (sha2_224 , "sha224" ),
178+ Py_HMAC_HINFO_ENTRY (sha2_256 , "sha256" ),
179+ Py_HMAC_HINFO_ENTRY (sha2_384 , "sha384" ),
180+ Py_HMAC_HINFO_ENTRY (sha2_512 , "sha512" ),
181+ /* SHA-3 family */
182+ Py_HMAC_HINFO_ENTRY (sha3_224 , NULL ),
183+ Py_HMAC_HINFO_ENTRY (sha3_256 , NULL ),
184+ Py_HMAC_HINFO_ENTRY (sha3_384 , NULL ),
185+ Py_HMAC_HINFO_ENTRY (sha3_512 , NULL ),
186+ /* Blake family */
187+ Py_HMAC_HINFO_ENTRY (blake2s_32 , "blake2s" ),
188+ Py_HMAC_HINFO_ENTRY (blake2b_32 , "blake2b" ),
189+ #undef Py_HMAC_HINFO_ENTRY
190+ /* sentinel */
191+ {
192+ NULL , Py_hmac_kind_hash_unknown , 0 , 0 ,
193+ NULL , NULL ,
194+ 0 ,
195+ },
196+ };
197+
41198// --- HMAC module methods ----------------------------------------------------
42199
43200static PyMethodDef hmacmodule_methods [] = {
0 commit comments