diff --git a/Doc/c-api/hash.rst b/Doc/c-api/hash.rst index 1ad712b0ce4f2b..5b36df6123c4fb 100644 --- a/Doc/c-api/hash.rst +++ b/Doc/c-api/hash.rst @@ -44,6 +44,29 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`. Add :c:macro:`!Py_HASH_SIPHASH13`. +.. c:macro:: Py_HASH_EXTERNAL + + If :c:macro:`Py_HASH_ALGORITHM` is set to that value, the hash function + definition ``PyHash_Func`` must be provided by embedders at compile time. + For instance, to use SipHash-4-8 for conservative security purposes + + .. code-block:: c + + static Py_hash_t + siphash48(const void *src, Py_ssize_t src_sz) { ... } + + PyHash_FuncDef PyHash_Func = { + .hash = siphash48, + .name = "siphash48", + .hash_bits = 64, + .seed_bits = 128, + }; + + .. availability:: Unix + + .. versionadded:: 3.4 + + .. c:macro:: Py_HASH_CUTOFF Buffers of length in range ``[1, Py_HASH_CUTOFF)`` are hashed using DJBX33A diff --git a/Doc/using/configure.rst b/Doc/using/configure.rst index cdadbe51417499..72b53a66b5b80c 100644 --- a/Doc/using/configure.rst +++ b/Doc/using/configure.rst @@ -1024,13 +1024,14 @@ Libraries options Security Options ---------------- -.. option:: --with-hash-algorithm=[fnv|siphash13|siphash24] +.. option:: --with-hash-algorithm=[fnv|siphash13|siphash24|external] Select hash algorithm for use in ``Python/pyhash.c``: * ``siphash13`` (default); * ``siphash24``; - * ``fnv``. + * ``fnv``; + * ``external``. .. versionadded:: 3.4 diff --git a/configure b/configure index 8463b5b5e4a9d0..31338b6cd36d2f 100755 --- a/configure +++ b/configure @@ -1902,7 +1902,7 @@ Optional Packages: behaviour detector, 'ubsan' (default is no) --with-thread-sanitizer enable ThreadSanitizer data race detector, 'tsan' (default is no) - --with-hash-algorithm=[fnv|siphash13|siphash24] + --with-hash-algorithm=[fnv|siphash13|siphash24|external] select hash algorithm for use in Python/pyhash.c (default is SipHash13) --with-tzpath= @@ -14934,6 +14934,10 @@ then : { printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $withval" >&5 printf "%s\n" "$withval" >&6; } case "$withval" in + external) + printf "%s\n" "#define Py_HASH_ALGORITHM 0" >>confdefs.h + + ;; siphash13) printf "%s\n" "#define Py_HASH_ALGORITHM 3" >>confdefs.h diff --git a/configure.ac b/configure.ac index df94ae25e63561..44c2dffde1fadb 100644 --- a/configure.ac +++ b/configure.ac @@ -3966,12 +3966,15 @@ dnl quadrigraphs "@<:@" and "@:>@" produce "[" and "]" in the output AC_ARG_WITH( [hash_algorithm], [AS_HELP_STRING( - [--with-hash-algorithm=@<:@fnv|siphash13|siphash24@:>@], + [--with-hash-algorithm=@<:@fnv|siphash13|siphash24|external@:>@], [select hash algorithm for use in Python/pyhash.c (default is SipHash13)] )], [ AC_MSG_RESULT([$withval]) case "$withval" in + external) + AC_DEFINE([Py_HASH_ALGORITHM], [0]) + ;; siphash13) AC_DEFINE([Py_HASH_ALGORITHM], [3]) ;;