1111#include "_hacl/Hacl_HMAC.h"
1212#include "hashlib.h"
1313
14-
15- // HMAC underlying hash function static information.
14+ // --- HMAC underlying hash function static information -----------------------
1615
1716#define Py_hmac_hash_max_digest_size 64
1817
2322
2423#define Py_hmac_md5_compute_func Hacl_HMAC_compute_md5
2524
25+ #define Py_OpenSSL_LN_md5 LN_md5
26+
2627/* SHA-1 family */
2728// HACL_HID = sha1
2829#define Py_hmac_sha1_block_size 64
2930#define Py_hmac_sha1_digest_size 20
3031
3132#define Py_hmac_sha1_compute_func Hacl_HMAC_compute_sha1
3233
34+ #define Py_OpenSSL_LN_sha1 LN_sha1
35+
3336/* SHA-2 family */
3437// HACL_HID = sha2_224
3538#define Py_hmac_sha2_224_block_size 64
3639#define Py_hmac_sha2_224_digest_size 28
3740
3841#define Py_hmac_sha2_224_compute_func Hacl_HMAC_compute_sha2_224
3942
43+ #define Py_OpenSSL_LN_sha2_224 LN_sha224
44+
4045// HACL_HID = sha2_256
4146#define Py_hmac_sha2_256_block_size 64
4247#define Py_hmac_sha2_256_digest_size 32
4348
4449#define Py_hmac_sha2_256_compute_func Hacl_HMAC_compute_sha2_256
4550
51+ #define Py_OpenSSL_LN_sha2_256 LN_sha256
52+
4653// HACL_HID = sha2_384
4754#define Py_hmac_sha2_384_block_size 128
4855#define Py_hmac_sha2_384_digest_size 48
4956
5057#define Py_hmac_sha2_384_compute_func Hacl_HMAC_compute_sha2_384
5158
59+ #define Py_OpenSSL_LN_sha2_384 LN_sha384
60+
5261// HACL_HID = sha2_512
5362#define Py_hmac_sha2_512_block_size 128
5463#define Py_hmac_sha2_512_digest_size 64
5564
5665#define Py_hmac_sha2_512_compute_func Hacl_HMAC_compute_sha2_512
5766
67+ #define Py_OpenSSL_LN_sha2_512 LN_sha512
68+
5869/* SHA-3 family */
5970// HACL_HID = sha3_224
6071#define Py_hmac_sha3_224_block_size 144
6172#define Py_hmac_sha3_224_digest_size 28
6273
6374#define Py_hmac_sha3_224_compute_func Hacl_HMAC_compute_sha3_224
6475
76+ #if defined(LN_sha3_224 )
77+ # define Py_OpenSSL_LN_sha3_224 LN_sha3_224
78+ #else
79+ # define Py_OpenSSL_LN_sha3_224 "sha3_224"
80+ #endif
81+
6582// HACL_HID = sha3_256
6683#define Py_hmac_sha3_256_block_size 136
6784#define Py_hmac_sha3_256_digest_size 32
6885
6986#define Py_hmac_sha3_256_compute_func Hacl_HMAC_compute_sha3_256
7087
88+ #if defined(LN_sha3_256 )
89+ # define Py_OpenSSL_LN_sha3_256 LN_sha3_256
90+ #else
91+ # define Py_OpenSSL_LN_sha3_256 "sha3_256"
92+ #endif
93+
7194// HACL_HID = sha3_384
7295#define Py_hmac_sha3_384_block_size 104
7396#define Py_hmac_sha3_384_digest_size 48
7497
7598#define Py_hmac_sha3_384_compute_func Hacl_HMAC_compute_sha3_384
7699
100+ #if defined(LN_sha3_384 )
101+ # define Py_OpenSSL_LN_sha3_384 LN_sha3_384
102+ #else
103+ # define Py_OpenSSL_LN_sha3_384 "sha3_384"
104+ #endif
105+
77106// HACL_HID = sha3_512
78107#define Py_hmac_sha3_512_block_size 72
79108#define Py_hmac_sha3_512_digest_size 64
80109
81110#define Py_hmac_sha3_512_compute_func Hacl_HMAC_compute_sha3_512
82111
112+ #if defined(LN_sha3_512 )
113+ # define Py_OpenSSL_LN_sha3_512 LN_sha3_512
114+ #else
115+ # define Py_OpenSSL_LN_sha3_512 "sha3_512"
116+ #endif
117+
83118/* Blake family */
84119// HACL_HID = blake2s_32
85120#define Py_hmac_blake2s_32_block_size 64
86121#define Py_hmac_blake2s_32_digest_size 32
87122
88123#define Py_hmac_blake2s_32_compute_func Hacl_HMAC_compute_blake2s_32
89124
125+ #if defined(LN_blake2s256 )
126+ # define Py_OpenSSL_LN_blake2s_32 LN_blake2s256
127+ #else
128+ # define Py_OpenSSL_LN_blake2s_32 "blake2s256"
129+ #endif
130+
90131// HACL_HID = blake2b_32
91132#define Py_hmac_blake2b_32_block_size 128
92133#define Py_hmac_blake2b_32_digest_size 64
93134
94135#define Py_hmac_blake2b_32_compute_func Hacl_HMAC_compute_blake2b_32
95136
137+ #if defined(LN_blake2b512 )
138+ # define Py_OpenSSL_LN_blake2b_32 LN_blake2b512
139+ #else
140+ # define Py_OpenSSL_LN_blake2b_32 "blake2b512"
141+ #endif
142+
96143/* Enumeration indicating the underlying hash function used by HMAC. */
97144typedef enum HMAC_Hash_Kind {
98145 Py_hmac_kind_unknown = 0 ,
@@ -167,12 +214,13 @@ typedef struct py_hmac_hinfo {
167214 py_hmac_hacl_api api ;
168215
169216 const char * hashlib_name ; /* hashlib preferred name (default: name) */
170- const char * hashlib_altn ; /* hashlib alias (default: hashlib_name) */
171217 const char * openssl_name ; /* hashlib preferred OpenSSL alias (if any) */
172218
173219 Py_ssize_t refcnt ;
174220} py_hmac_hinfo ;
175221
222+ // --- HMAC module state ------------------------------------------------------
223+
176224typedef struct hmacmodule_state {
177225 _Py_hashtable_t * hinfo_table ;
178226 /* imported from _hashlib */
@@ -197,6 +245,8 @@ module _hmac
197245
198246#include "clinic/hmacmodule.c.h"
199247
248+ // --- Helpers ----------------------------------------------------------------
249+
200250static inline int
201251find_hash_info_by_utf8name (hmacmodule_state * state ,
202252 const char * name ,
@@ -230,7 +280,7 @@ find_hash_info_by_name(hmacmodule_state *state,
230280}
231281
232282/*
233- * Find the corresponding HMAC static information.
283+ * Find the corresponding HMAC hash function static information.
234284 *
235285 * If an error occurs or if nothing can be found, this
236286 * returns -1 or 0 respectively, and sets 'info' to NULL.
@@ -573,46 +623,6 @@ static PyMethodDef hmacmodule_methods[] = {
573623
574624// --- HMAC static information table ------------------------------------------
575625
576- #define Py_OpenSSL_LN_md5 LN_md5
577- #define Py_OpenSSL_LN_sha1 LN_sha1
578-
579- #define Py_OpenSSL_LN_sha2_224 LN_sha224
580- #define Py_OpenSSL_LN_sha2_256 LN_sha256
581- #define Py_OpenSSL_LN_sha2_384 LN_sha384
582- #define Py_OpenSSL_LN_sha2_512 LN_sha512
583-
584- #if defined(LN_sha3_224 )
585- # define Py_OpenSSL_LN_sha3_224 LN_sha3_224
586- #else
587- # define Py_OpenSSL_LN_sha3_224 "sha3_224"
588- #endif
589- #if defined(LN_sha3_256 )
590- # define Py_OpenSSL_LN_sha3_256 LN_sha3_256
591- #else
592- # define Py_OpenSSL_LN_sha3_256 "sha3_256"
593- #endif
594- #if defined(LN_sha3_384 )
595- # define Py_OpenSSL_LN_sha3_384 LN_sha3_384
596- #else
597- # define Py_OpenSSL_LN_sha3_384 "sha3_384"
598- #endif
599- #if defined(LN_sha3_512 )
600- # define Py_OpenSSL_LN_sha3_512 LN_sha3_512
601- #else
602- # define Py_OpenSSL_LN_sha3_512 "sha3_512"
603- #endif
604-
605- #if defined(LN_blake2s256 )
606- # define Py_OpenSSL_LN_blake2s_32 LN_blake2s256
607- #else
608- # define Py_OpenSSL_LN_blake2s_32 "blake2s256"
609- #endif
610- #if defined(LN_blake2b512 )
611- # define Py_OpenSSL_LN_blake2b_32 LN_blake2b512
612- #else
613- # define Py_OpenSSL_LN_blake2b_32 "blake2b512"
614- #endif
615-
616626/* Static information used to construct the hash table. */
617627static const py_hmac_hinfo py_hmac_static_hinfo [] = {
618628#define Py_HMAC_HINFO_HACL_API (HACL_HID ) \
@@ -621,7 +631,7 @@ static const py_hmac_hinfo py_hmac_static_hinfo[] = {
621631 .compute_py = &_hmac_compute_## HACL_HID ##_impl, \
622632 }
623633
624- #define Py_HMAC_HINFO_ENTRY (HACL_HID , HLIB_NAME , HLIB_ALTN ) \
634+ #define Py_HMAC_HINFO_ENTRY (HACL_HID , HLIB_NAME ) \
625635 { \
626636 .name = Py_STRINGIFY(HACL_HID), \
627637 .p_name = NULL, \
@@ -630,35 +640,34 @@ static const py_hmac_hinfo py_hmac_static_hinfo[] = {
630640 .digest_size = Py_hmac_## HACL_HID ##_digest_size, \
631641 .api = Py_HMAC_HINFO_HACL_API(HACL_HID), \
632642 .hashlib_name = HLIB_NAME, \
633- .hashlib_altn = HLIB_ALTN, \
634643 .openssl_name = Py_OpenSSL_LN_ ## HACL_HID, \
635644 .refcnt = 0, \
636645 }
637646 /* MD5 */
638- Py_HMAC_HINFO_ENTRY (md5 , "md5" , "MD5" ),
647+ Py_HMAC_HINFO_ENTRY (md5 , "md5" ),
639648 /* SHA-1 */
640- Py_HMAC_HINFO_ENTRY (sha1 , "sha1" , "SHA1" ),
649+ Py_HMAC_HINFO_ENTRY (sha1 , "sha1" ),
641650 /* SHA-2 family */
642- Py_HMAC_HINFO_ENTRY (sha2_224 , "sha224" , "SHA224" ),
643- Py_HMAC_HINFO_ENTRY (sha2_256 , "sha256" , "SHA256" ),
644- Py_HMAC_HINFO_ENTRY (sha2_384 , "sha384" , "SHA384" ),
645- Py_HMAC_HINFO_ENTRY (sha2_512 , "sha512" , "SHA512" ),
651+ Py_HMAC_HINFO_ENTRY (sha2_224 , "sha224" ),
652+ Py_HMAC_HINFO_ENTRY (sha2_256 , "sha256" ),
653+ Py_HMAC_HINFO_ENTRY (sha2_384 , "sha384" ),
654+ Py_HMAC_HINFO_ENTRY (sha2_512 , "sha512" ),
646655 /* SHA-3 family */
647- Py_HMAC_HINFO_ENTRY (sha3_224 , NULL , NULL ),
648- Py_HMAC_HINFO_ENTRY (sha3_256 , NULL , NULL ),
649- Py_HMAC_HINFO_ENTRY (sha3_384 , NULL , NULL ),
650- Py_HMAC_HINFO_ENTRY (sha3_512 , NULL , NULL ),
656+ Py_HMAC_HINFO_ENTRY (sha3_224 , NULL ),
657+ Py_HMAC_HINFO_ENTRY (sha3_256 , NULL ),
658+ Py_HMAC_HINFO_ENTRY (sha3_384 , NULL ),
659+ Py_HMAC_HINFO_ENTRY (sha3_512 , NULL ),
651660 /* Blake family */
652- Py_HMAC_HINFO_ENTRY (blake2s_32 , "blake2s256" , NULL ),
653- Py_HMAC_HINFO_ENTRY (blake2b_32 , "blake2b512" , NULL ),
661+ Py_HMAC_HINFO_ENTRY (blake2s_32 , "blake2s256" ),
662+ Py_HMAC_HINFO_ENTRY (blake2b_32 , "blake2b512" ),
654663#undef Py_HMAC_HINFO_ENTRY
655664#undef Py_HMAC_HINFO_HACL_API
656665 /* sentinel */
657666 {
658667 NULL , NULL ,
659668 Py_hmac_kind_unknown , 0 , 0 ,
660669 {NULL , NULL },
661- NULL , NULL , NULL ,
670+ NULL , NULL ,
662671 0
663672 },
664673};
@@ -734,7 +743,6 @@ py_hmac_hinfo_ht_new(void)
734743 } while (0)
735744 Py_HMAC_HINFO_LINK (e -> name );
736745 Py_HMAC_HINFO_LINK (e -> hashlib_name );
737- Py_HMAC_HINFO_LINK (e -> hashlib_altn );
738746 Py_HMAC_HINFO_LINK (e -> openssl_name );
739747#undef Py_HMAC_HINFO_LINK
740748 assert (value -> refcnt > 0 );
0 commit comments