Skip to content

Commit 2e5ee67

Browse files
committed
add HMAC module
1 parent 73caca4 commit 2e5ee67

File tree

10 files changed

+120
-0
lines changed

10 files changed

+120
-0
lines changed

Modules/Setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ PYTHONPATH=$(COREPYTHONPATH)
170170
#_sha1 sha1module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Hash_SHA1.a -D_BSD_SOURCE -D_DEFAULT_SOURCE
171171
#_sha2 sha2module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Hash_SHA2.a -D_BSD_SOURCE -D_DEFAULT_SOURCE
172172
#_sha3 sha3module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Hash_SHA3.a -D_BSD_SOURCE -D_DEFAULT_SOURCE
173+
#_hmac hmacmodule.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_HMAC.a -D_BSD_SOURCE -D_DEFAULT_SOURCE
173174

174175
# text encodings and unicode
175176
#_codecs_cn cjkcodecs/_codecs_cn.c

Modules/Setup.stdlib.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
@MODULE__SHA3_TRUE@_sha3 sha3module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Hash_SHA3.a -D_BSD_SOURCE -D_DEFAULT_SOURCE
8585
@MODULE__BLAKE2_TRUE@_blake2 blake2module.c -I$(srcdir)/Modules/_hacl/include Modules/_hacl/libHacl_Hash_Blake2.a -D_BSD_SOURCE -D_DEFAULT_SOURCE
8686

87+
@MODULE__HMAC_TRUE@_hmac hmacmodule.c
88+
8789
############################################################################
8890
# XML and text
8991

Modules/hmacmodule.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* @author Bénédikt Tran
3+
* @date December 2024
4+
*
5+
* Implement the HMAC algorithm as described by RFC 2104 using HACL*.
6+
*
7+
* Using HACL* implementation implicitly assumes that the caller wants
8+
* a formally verified implementation. In particular, only algorithms
9+
* given by their names will be recognized.
10+
*
11+
* Some algorithms exposed by `_hashlib` such as truncated SHA-2-512-224/256
12+
* are not yet implemented by the HACL* project. Nonetheless, the supported
13+
* HMAC algorithms form a subset of those supported by '_hashlib'.
14+
*/
15+
16+
#ifndef Py_BUILD_CORE_BUILTIN
17+
# define Py_BUILD_CORE_MODULE 1
18+
#endif
19+
20+
#include "Python.h"
21+
22+
// --- HMAC module clinic configuration ---------------------------------------
23+
24+
/*[clinic input]
25+
module _hmac
26+
[clinic start generated code]*/
27+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=799f0f10157d561f]*/
28+
29+
// --- HMAC module methods ----------------------------------------------------
30+
31+
static PyMethodDef hmacmodule_methods[] = {
32+
{NULL, NULL, 0, NULL} /* sentinel */
33+
};
34+
35+
// --- HMAC module initialization and finalization functions ------------------
36+
37+
static struct PyModuleDef_Slot hmacmodule_slots[] = {
38+
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
39+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
40+
{0, NULL} /* sentinel */
41+
};
42+
43+
static struct PyModuleDef _hmacmodule = {
44+
PyModuleDef_HEAD_INIT,
45+
.m_name = "_hmac",
46+
.m_size = sizeof(hmacmodule_state),
47+
.m_methods = hmacmodule_methods,
48+
.m_slots = hmacmodule_slots,
49+
};
50+
51+
PyMODINIT_FUNC
52+
PyInit__hmac(void)
53+
{
54+
return PyModuleDef_Init(&_hmacmodule);
55+
}

PC/config.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern PyObject* PyInit__statistics(void);
2525
extern PyObject* PyInit__sysconfig(void);
2626
extern PyObject* PyInit__typing(void);
2727
extern PyObject* PyInit__blake2(void);
28+
extern PyObject* PyInit__hmac(void);
2829
extern PyObject* PyInit_time(void);
2930
extern PyObject* PyInit__thread(void);
3031
#ifdef WIN32
@@ -102,6 +103,7 @@ struct _inittab _PyImport_Inittab[] = {
102103
{"_sha2", PyInit__sha2},
103104
{"_sha3", PyInit__sha3},
104105
{"_blake2", PyInit__blake2},
106+
{"_hmac", PyInit__hmac},
105107
{"_sysconfig", PyInit__sysconfig},
106108
{"time", PyInit_time},
107109
{"_thread", PyInit__thread},

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@
468468
<ClCompile Include="..\Modules\faulthandler.c" />
469469
<ClCompile Include="..\Modules\gcmodule.c" />
470470
<ClCompile Include="..\Modules\getbuildinfo.c" />
471+
<ClCompile Include="..\Modules\hmacmodule.c" />
471472
<ClCompile Include="..\Modules\itertoolsmodule.c" />
472473
<ClCompile Include="..\Modules\main.c" />
473474
<ClCompile Include="..\Modules\mathmodule.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,9 @@
10101010
<ClCompile Include="..\Modules\gcmodule.c">
10111011
<Filter>Modules</Filter>
10121012
</ClCompile>
1013+
<ClCompile Include="..\Modules\hmacmodule.c">
1014+
<Filter>Modules</Filter>
1015+
</ClCompile>
10131016
<ClCompile Include="..\Modules\itertoolsmodule.c">
10141017
<Filter>Modules</Filter>
10151018
</ClCompile>

Python/stdlib_module_names.h

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/c-analyzer/cpython/_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def clean_lines(text):
126126
Modules/sha2module.c Modules/_hacl/include
127127
Modules/sha3module.c Modules/_hacl/include
128128
Modules/blake2module.c Modules/_hacl/include
129+
Modules/hmacmodule.c Modules/_hacl/include
129130
Objects/stringlib/*.h Objects
130131
131132
# possible system-installed headers, just in case

configure

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7898,6 +7898,13 @@ fi
78987898
AC_SUBST([LIBHACL_SIMD256_FLAGS])
78997899
AC_SUBST([LIBHACL_SIMD256_OBJS])
79007900

7901+
dnl HMAC builtin library does not need OpenSSL for now. In the future
7902+
dnl we might want to rely on OpenSSL EVP/NID interface or implement
7903+
dnl our own for algorithm resolution.
7904+
PY_STDLIB_MOD([_hmac], [], [],
7905+
[$LIBHACL_CFLAGS],
7906+
[$LIBHACL_CFLAGS Modules/_hacl/libHacl_HMAC.a])
7907+
79017908
PY_STDLIB_MOD([_ctypes],
79027909
[], [test "$have_libffi" = yes],
79037910
[$NO_STRICT_OVERFLOW_CFLAGS $LIBFFI_CFLAGS], [$LIBFFI_LIBS])

0 commit comments

Comments
 (0)