Skip to content

Commit ad5ee2b

Browse files
committed
add HMAC module
1 parent f7c04c5 commit ad5ee2b

File tree

10 files changed

+119
-0
lines changed

10 files changed

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

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
@@ -469,6 +469,7 @@
469469
<ClCompile Include="..\Modules\faulthandler.c" />
470470
<ClCompile Include="..\Modules\gcmodule.c" />
471471
<ClCompile Include="..\Modules\getbuildinfo.c" />
472+
<ClCompile Include="..\Modules\hmacmodule.c" />
472473
<ClCompile Include="..\Modules\itertoolsmodule.c" />
473474
<ClCompile Include="..\Modules\main.c" />
474475
<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)