10000 gh-99108: add HACL*-based 1-shot HMAC implementation by picnixz · Pull Request #126359 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99108: add HACL*-based 1-shot HMAC implementation #126359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 38 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6336160
Pull HACL* HMAC.
picnixz Nov 3, 2024
5a410f8
Update SBOM files
picnixz Nov 3, 2024
c1c7c92
Update HACL (CPython) namespace
picnixz Nov 3, 2024
2901987
Update HACL (CPython) README
picnixz Nov 3, 2024
92a1e76
Update `configure` script
picnixz Nov 3, 2024
027964b
Update Makefile scripts
picnixz Nov 3, 2024
17a2e46
Update MSVC project
picnixz Nov 3, 2024
186094b
Implement HACL* HMAC module
picnixz Nov 3, 2024
b10c729
fix blake2b digest size
picnixz Nov 7, 2024
6a3515f
drop un-necessary `@critical_section`
picnixz Nov 7, 2024
612974e
Improve 1-shot macro
picnixz Nov 7, 2024
e714135
Define HMAC static information
picnixz Nov 7, 2024
9fb6300
reduce the possibility of typos
picnixz Nov 8, 2024
755d6c0
update names
picnixz Nov 8, 2024
89c5f89
cleanup
picnixz Nov 8, 2024
755aca6
cleanup
picnixz Nov 8, 2024
1ddbe26
improve naming
picnixz Nov 8, 2024
12fbfc4
Update HACL* project.
picnixz Nov 8, 2024
448b0ba
Merge remote-tracking branch 'upstream/main' into hacl/HMAC-99108
picnixz Nov 8, 2024
39be4b1
update SBOM files
picnixz Nov 9, 2024
5ae6b6d
cosmetic cleanups
picnixz Nov 9, 2024
49083aa
unconditionally build `_hmac` extension module
picnixz Nov 11, 2024
dd80600
implement hash algorithm resolution
picnixz Nov 11, 2024
542738c
raise OverflowError instead of ValueError in 1-shot HMAC
picnixz Nov 11, 2024
09c631a
reduce import time
picnixz Nov 11, 2024
9bee955
expose 1-shot HMAC
picnixz Nov 11, 2024
d36977f
ensure that openssl is linked
picnixz Nov 11, 2024
b3aa599
update globals
picnixz Nov 11, 2024
49a1294
fix LN_* macro values
picnixz Nov 12, 2024
06b012d
improve OpenSSL name resolution
picnixz Nov 12, 2024
82c610c
update comment
picnixz Nov 12, 2024
ff8cf2f
fix configure?
picnixz Nov 12, 2024
7af7eb6
Merge remote-tracking branch 'upstream/main' into hacl/HMAC-99108
picnixz Nov 15, 2024
fba3778
sbom
picnixz Nov 15, 2024
5c3cbfd
remove unused imports
picnixz Nov 15, 2024
379cbef
cosmetic changes for future resolution
picnixz Nov 19, 2024
960aa73
fix memory leak
picnixz Nov 28, 2024
074f9ab
add #define for error messages
picnixz Nov 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update globals
  • Loading branch information
picnixz committed Nov 11, 2024
commit b3aa599d9281009b8ff7f847299214f4e57d266b
128 changes: 68 additions & 60 deletions Modules/hmacmodule.c
< 10000 td id="diff-c1a0c4b200e7b9390d6af70efb2fd75fcce77c7bb63e26dd08946456fd6bf5f4L86" data-line-number="86" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
#include "pycore_hashtable.h"
#include "pycore_strhex.h" // _Py_strhex()

#include <openssl/obj_mac.h> // LN_* macros

Check failure on line 9 in Modules/hmacmodule.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

Cannot open include file: 'openssl/obj_mac.h': No such file or directory [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check failure on line 9 in Modules/hmacmodule.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

Cannot open include file: 'openssl/obj_mac.h': No such file or directory [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

#include "_hacl/Hacl_HMAC.h"
#include "hashlib.h"


// HMAC underlying hash function static information.
// --- HMAC underlying hash function static information -----------------------

#define Py_hmac_hash_max_digest_size 64

Expand All @@ -23,76 +22,124 @@

#define Py_hmac_md5_compute_func Hacl_HMAC_compute_md5

#define Py_OpenSSL_LN_md5 LN_md5

/* SHA-1 family */
// HACL_HID = sha1
#define Py_hmac_sha1_block_size 64
#define Py_hmac_sha1_digest_size 20

#define Py_hmac_sha1_compute_func Hacl_HMAC_compute_sha1

#define Py_OpenSSL_LN_sha1 LN_sha1

/* SHA-2 family */
// HACL_HID = sha2_224
#define Py_hmac_sha2_224_block_size 64
#define Py_hmac_sha2_224_digest_size 28

#define Py_hmac_sha2_224_compute_func Hacl_HMAC_compute_sha2_224

#define Py_OpenSSL_LN_sha2_224 LN_sha224

// HACL_HID = sha2_256
#define Py_hmac_sha2_256_block_size 64
#define Py_hmac_sha2_256_digest_size 32

#define Py_hmac_sha2_256_compute_func Hacl_HMAC_compute_sha2_256

#define Py_OpenSSL_LN_sha2_256 LN_sha256

// HACL_HID = sha2_384
#define Py_hmac_sha2_384_block_size 128
#define Py_hmac_sha2_384_digest_size 48

#define Py_hmac_sha2_384_compute_func Hacl_HMAC_compute_sha2_384

#define Py_OpenSSL_LN_sha2_384 LN_sha384

// HACL_HID = sha2_512
#define Py_hmac_sha2_512_block_size 128
#define Py_hmac_sha2_512_digest_size 64

#define Py_hmac_sha2_512_compute_func Hacl_HMAC_compute_sha2_512

#define Py_OpenSSL_LN_sha2_512 LN_sha512

/* SHA-3 family */
// HACL_HID = sha3_224
#define Py_hmac_sha3_224_block_size 144
#define Py_hmac_sha3_224_digest_size 28

#define Py_hmac_sha3_224_compute_func Hacl_HMAC_compute_sha3_224

#if defined(LN_sha3_224)
# define Py_OpenSSL_LN_sha3_224 LN_sha3_224
#else
# define Py_OpenSSL_LN_sha3_224 "sha3_224"
#endif

// HACL_HID = sha3_256
#define Py_hmac_sha3_256_block_size 136
#define Py_hmac_sha3_256_digest_size 32

#define Py_hmac_sha3_256_compute_func Hacl_HMAC_compute_sha3_256

#if defined(LN_sha3_256)
# define Py_OpenSSL_LN_sha3_256 LN_sha3_256
#else
# define Py_OpenSSL_LN_sha3_256 "sha3_256"
#endif

// HACL_HID = sha3_384
#define Py_hmac_sha3_384_block_size 104
#define Py_hmac_sha3_384_digest_size 48

#define Py_hmac_sha3_384_compute_func Hacl_HMAC_compute_sha3_384

#if defined(LN_sha3_384)
# define Py_OpenSSL_LN_sha3_384 LN_sha3_384
#else
# define Py_OpenSSL_LN_sha3_384 "sha3_384"
#endif

// HACL_HID = sha3_512
#define Py_hmac_sha3_512_block_size 72
#define Py_hmac_sha3_512_digest_size 64

#define Py_hmac_sha3_512_compute_func Hacl_HMAC_compute_sha3_512

#if defined(LN_sha3_512)
# define Py_OpenSSL_LN_sha3_512 LN_sha3_512
#else
# define Py_OpenSSL_LN_sha3_512 "sha3_512"
#endif

/* Blake family */
// HACL_HID = blake2s_32
#define Py_hmac_blake2s_32_block_size 64
#define Py_hmac_blake2s_32_digest_size 32

#define Py_hmac_blake2s_32_compute_func Hacl_HMAC_compute_blake2s_32

#if defined(LN_blake2s256)
# define Py_OpenSSL_LN_blake2s_32 LN_blake2s256
#else
# define Py_OpenSSL_LN_blake2s_32 "blake2s256"
#endif

// HACL_HID = blake2b_32
#define Py_hmac_blake2b_32_block_size 128
#define Py_hmac_blake2b_32_digest_size 64

#define Py_hmac_blake2b_32_compute_func Hacl_HMAC_compute_blake2b_32

#if defined(LN_blake2b512)
# define Py_OpenSSL_LN_blake2b_32 LN_blake2b512
#else
# define Py_OpenSSL_LN_blake2b_32 "blake2b512"
#endif

/* Enumeration indicating the underlying hash function used by HMAC. */
typedef enum HMAC_Hash_Kind {
Py_hmac_kind_unknown = 0,
Expand Down Expand Up @@ -167,12 +214,13 @@
py_hmac_hacl_api api;

const char *hashlib_name; /* hashlib preferred name (default: name) */
const char *hashlib_altn; /* hashlib alias (default: hashlib_name) */
const char *openssl_name; /* hashlib preferred OpenSSL alias (if any) */

Py_ssize_t refcnt;
} py_hmac_hinfo;

// --- HMAC module state ------------------------------------------------------

typedef struct hmacmodule_state {
_Py_hashtable_t *hinfo_table;
/* imported from _hashlib */
Expand All @@ -197,6 +245,8 @@

#include "clinic/hmacmodule.c.h"

// --- Helpers ----------------------------------------------------------------

static inline int
find_hash_info_by_utf8name(hmacmodule_state *state,
const char *name,
Expand Down Expand Up @@ -230,7 +280,7 @@
}

/*
* Find the corresponding HMAC static information.
* Find the corresponding HMAC hash function static information.
*
* If an error occurs or if nothing can be found, this
* returns -1 or 0 respectively, and sets 'info' to NULL.
Expand Down Expand Up @@ -573,46 +623,6 @@

// --- HMAC static information table ------------------------------------------

#define Py_OpenSSL_LN_md5 LN_md5
#define Py_OpenSSL_LN_sha1 LN_sha1

#define Py_OpenSSL_LN_sha2_224 LN_sha224
#define Py_OpenSSL_LN_sha2_256 LN_sha256
#define Py_OpenSSL_LN_sha2_384 LN_sha384
#define Py_OpenSSL_LN_sha2_512 LN_sha512

#if defined(LN_sha3_224)
# define Py_OpenSSL_LN_sha3_224 LN_sha3_224
#else
# define Py_OpenSSL_LN_sha3_224 "sha3_224"
#endif
#if defined(LN_sha3_256)
# define Py_OpenSSL_LN_sha3_256 LN_sha3_256
#else
# define Py_OpenSSL_LN_sha3_256 "sha3_256"
#endif
#if defined(LN_sha3_384)
# define Py_OpenSSL_LN_sha3_384 LN_sha3_384
#else
# define Py_OpenSSL_LN_sha3_384 "sha3_384"
#endif
#if defined(LN_sha3_512)
# define Py_OpenSSL_LN_sha3_512 LN_sha3_512
#else
# define Py_OpenSSL_LN_sha3_512 "sha3_512"
#endif

#if defined(LN_blake2s256)
# define Py_OpenSSL_LN_blake2s_32 LN_blake2s256
#else
# define Py_OpenSSL_LN_blake2s_32 "blake2s256"
#endif
#if defined(LN_blake2b512)
# define Py_OpenSSL_LN_blake2b_32 LN_blake2b512
#else
# define Py_OpenSSL_LN_blake2b_32 "blake2b512"
#endif

/* Static information used to construct the hash table. */
static const py_hmac_hinfo py_hmac_static_hinfo[] = {
#define Py_HMAC_HINFO_HACL_API(HACL_HID) \
Expand All @@ -621,7 +631,7 @@
.compute_py = &_hmac_compute_## HACL_HID ##_impl, \
}

#define Py_HMAC_HINFO_ENTRY(HACL_HID, HLIB_NAME, HLIB_ALTN) \
#define Py_HMAC_HINFO_ENTRY(HACL_HID, HLIB_NAME) \
{ \
.name = Py_STRINGIFY(HACL_HID), \
.p_name = NULL, \
Expand All @@ -630,35 +640,34 @@
.digest_size = Py_hmac_## HACL_HID ##_digest_size, \
.api = Py_HMAC_HINFO_HACL_API(HACL_HID), \
.hashlib_name = HLIB_NAME, \
.hashlib_altn = HLIB_ALTN, \
.openssl_name = Py_OpenSSL_LN_ ## HACL_HID, \
.refcnt = 0, \
}
/* MD5 */
Py_HMAC_HINFO_ENTRY(md5, "md5", "MD5"),
Py_HMAC_HINFO_ENTRY(md5, "md5"),
/* SHA-1 */
Py_HMAC_HINFO_ENTRY(sha1, "sha1", "SHA1"),
Py_HMAC_HINFO_ENTRY(sha1, "sha1"),
/* SHA-2 family */
Py_HMAC_HINFO_ENTRY(sha2_224, "sha224", "SHA224"),
Py_HMAC_HINFO_ENTRY(sha2_256, "sha256", "SHA256"),
Py_HMAC_HINFO_ENTRY(sha2_384, "sha384", "SHA384"),
Py_HMAC_HINFO_ENTRY(sha2_512, "sha512", "SHA512"),
Py_HMAC_HINFO_ENTRY(sha2_224, "sha224"),
Py_HMAC_HINFO_ENTRY(sha2_256, "sha256"),
Py_HMAC_HINFO_ENTRY(sha2_384, "sha384"),
Py_HMAC_HINFO_ENTRY(sha2_512, "sha512"),
/* SHA-3 family */
Py_HMAC_HINFO_ENTRY(sha3_224, NULL, NULL),
Py_HMAC_HINFO_ENTRY(sha3_256, NULL, NULL),
Py_HMAC_HINFO_ENTRY(sha3_384, NULL, NULL),
Py_HMAC_HINFO_ENTRY(sha3_512, NULL, NULL),
Py_HMAC_HINFO_ENTRY(sha3_224, NULL),
Py_HMAC_HINFO_ENTRY(sha3_256, NULL),
Py_HMAC_HINFO_ENTRY(sha3_384, NULL),
Py_HMAC_HINFO_ENTRY(sha3_512, NULL),
/* Blake family */
Py_HMAC_HINFO_ENTRY(blake2s_32, "blake2s256", NULL),
Py_HMAC_HINFO_ENTRY(blake2b_32, "blake2b512", NULL),
Py_HMAC_HINFO_ENTRY(blake2s_32, "blake2s256"),
Py_HMAC_HINFO_ENTRY(blake2b_32, "blake2b512"),
#undef Py_HMAC_HINFO_ENTRY
#undef Py_HMAC_HINFO_HACL_API
/* sentinel */
{
NULL, NULL,
Py_hmac_kind_unknown, 0, 0,
{NULL, NULL},
NULL, NULL, NULL,
NULL, NULL,
0
},
};
Expand Down Expand Up @@ -734,7 +743,6 @@
} while (0)
Py_HMAC_HINFO_LINK(e->name);
Py_HMAC_HINFO_LINK(e->hashlib_name);
Py_HMAC_HINFO_LINK(e->hashlib_altn);
Py_HMAC_HINFO_LINK(e->openssl_name);
#undef Py_HMAC_HINFO_LINK
assert(value->refcnt > 0);
Expand Down
1 change: 1 addition & 0 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ Modules/cmathmodule.c - sqrt_special_values -
Modules/cmathmodule.c - tanh_special_values -
Modules/config.c - _PyImport_Inittab -
Modules/faulthandler.c - faulthandler_handlers -
Modules/hmacmodule.c - py_hmac_static_hinfo -
Modules/getnameinfo.c - gni_afdl -
Modules/posixmodule.c os_getxattr_impl buffer_sizes -
Modules/posixmodule.c os_listxattr_impl buffer_sizes -
Expand Down
Loading
0