Commit graph

176 commits

Author SHA1 Message Date
Victor Stinner
a68efdf09c
gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl (#138922)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public
PyBytesWriter API.
2025-09-15 18:41:43 +02:00
Victor Stinner
eec8c98d06
gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl (#138835)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public
PyBytesWriter API.
2025-09-12 15:39:16 +02:00
Bénédikt Tran
b9c50b4988
gh-134531: use EVP_MAC API for _hashlib.HMAC when possible (#135235)
Use `EVP_MAC` API for HMAC on builds with OpenSSL 3.0 or later.
2025-09-10 10:35:01 +02:00
Adam Turner
98b4cd6fe9
GH-135763: AC: Use `Py_ssize_t(allow_negative=False)` (#138394) 2025-09-02 21:29:05 +01:00
Bénédikt Tran
c9b252c2c0
gh-116946: Revert GC protocol for immutable empty heap types (GH-138266, GH-138288, GH-138289) (#138338)
* Revert "gh-116946: fully implement GC protocol for `bz2` objects (#138266)"

This reverts commit 9be91f6a20.

* Revert "gh-116946: fully implement GC protocol for `lzma` objects (#138288)"

This reverts commit 3ea16f990f.

* Revert "gh-116946: fully implement GC protocol for `_hashlib` objects (#138289)"

This reverts commit 6f1dd9551a.
2025-09-01 21:15:11 +05:30
Bénédikt Tran
6f1dd9551a
gh-116946: fully implement GC protocol for _hashlib objects (#138289) 2025-09-01 10:23:01 +02:00
Bénédikt Tran
e545bae55a
gh-134531: simplify code for computing HMAC digests (#138046) 2025-08-22 09:19:04 +00:00
Adam Turner
918e3ba6c0
GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
Bénédikt Tran
fe0e921817
gh-131876: Revert "gh-131876: extract _hashlib helpers into a separate directory (#136995) (#137307)
Revert "gh-131876: extract `_hashlib` helpers into a separate directory (#136995)"

This reverts commit 45138d3584.
2025-08-01 10:45:40 -07:00
Bénédikt Tran
45138d3584
gh-131876: extract _hashlib helpers into a separate directory (#136995)
The `Modules/hashlib.h` helper file is now removed and split into multiple files:

* `Modules/_hashlib/hashlib_buffer.[ch]` -- Utilities for getting a buffer view and handling buffer inputs.
* `Modules/_hashlib/hashlib_fetch.h` -- Utilities used when fetching a message digest from a digest-like identifier.
  Currently, this file only contains common error messages as the fetching API is not yet implemented.
* `Modules/_hashlib/hashlib_mutex.h` -- Utilities for managing the lock on cryptographic hash objects.
2025-07-28 11:28:48 +02:00
Bénédikt Tran
eefd70f0ed
gh-136968: fortify macro usage in cryptographic modules (#136973)
Macros used in cryptographic extension modules are partially rewritten
to use `static inline` functions when possible to help code completion.
2025-07-28 07:36:15 +00:00
Bénédikt Tran
4a151cae33
gh-134531: prefer using _hashlib module state instead of module object (#136865)
Some internal helper functions taking the module object to retrieve its state
under some conditions now directly take the module's state instead as those
conditions hold most of the time.
2025-07-20 13:10:36 +00:00
Bénédikt Tran
6be49ee517
gh-136787: improve exception messages for invalid hash algorithms (#136802) 2025-07-20 08:49:34 +00:00
Bénédikt Tran
a68ddea3bf
gh-90733: improve hashlib.scrypt interface (#136100)
* add `scrypt` to `hashlib.__all__`
* improve `hashlib.scrypt` exception messages
2025-07-14 12:49:34 +02:00
Bénédikt Tran
9be3649f5e
gh-136591: avoid using deprecated features for OpenSSL 3.0+ (#136592)
Since OpenSSL 3.0, `ERR_func_error_string()` always returns NULL and
`EVP_MD_CTX_get0_md()` should be preferred over `EVP_MD_CTX_md()`.
2025-07-12 16:33:07 +00:00
Bénédikt Tran
621a8bd6a8
gh-135532: cleanup clinic module directives for cryptographic modules (#135822) 2025-06-22 20:04:38 +00:00
Bénédikt Tran
e7295a89b8
gh-135239: simpler use of mutexes in cryptographic modules (#135267) 2025-06-22 16:59:57 +02:00
Bénédikt Tran
d08b4b2333
gh-135532: optimize calls to PyMem_Malloc in SHAKE digest computation (#135744)
- Add a fast path when the digest length is 0 to avoid calling useless functions.
- Directly allocate via `PyBytes_FromStringAndSize(NULL, length)` when possible.
2025-06-21 14:32:00 +02:00
Bénédikt Tran
7c4361564c
gh-135759: consistently reject negative sizes in SHAKE digests (#135767)
Passing a negative digest length to `_hashilb.HASHXOF.[hex]digest()` now
raises a ValueError instead of a MemoryError or a SystemError. This makes
the behavior consistent with that of `_sha3.shake_{128,256}.[hex]digest`.
2025-06-21 09:43:30 +00:00
Bénédikt Tran
83b94e856e
gh-135234: improve _hashlib exceptions when reporting an OpenSSL function failure (#135250)
- Refactor `get_openssl_evp_md_by_utf8name` error branches.
- Refactor `HASH.{digest,hexdigest}` computations.
- Refactor `_hashlib_HASH_copy_locked` and `locked_HMAC_CTX_copy`.
2025-06-09 09:13:29 +02:00
Bénédikt Tran
aee45fd03f
gh-134531: refactor _hashlib logic for handling NIDs and EVP_MDs (#135254) 2025-06-08 12:34:57 +00:00
Bénédikt Tran
4372011928
gh-134531: fix _hashlib clinic directive post GH-134626 (#135249) 2025-06-08 07:43:32 +00:00
Bénédikt Tran
c6e63d9d35
gh-134696: align OpenSSL and HACL*-based hash functions constructors AC signatures (#134713)
OpenSSL and HACL*-based hash functions constructors now support both `data` and `string` parameters.
Previously these constructor functions inconsistently supported sometimes `data` and sometimes `string`,
while the documentation expected `data` to be given in all cases.
2025-05-31 09:37:47 +02:00
Bénédikt Tran
cb8045e86c
gh-134531: cleanup _hashopenssl.c to support EVP_MAC (#134626)
Rename components related to `_hashlib.{HASH,HASHXOF}` objects.

- The `EVPobject` structure is renamed `HASHobject`.
- Non-clinic `HASH` methods are now prefixed by `_hashlib_HASH_*`.
  A similar change is made for non-clinic `HASHXOF` methods.
- Functions extracting information from `EVP_MD` objects and functions
  constructing `EVP_MD` objects now include `openssl_evp_md` in their name.

This change allows us to avoid future ambiguities between the `EVP_MD`
and the `EVP_MAC` APIs (currently, we only use `EVP_MD` for hash functions
and rely on the legacy interface for HMAC instead of using `EVP_MAC`).
2025-05-26 10:12:32 +00:00
Serhiy Storchaka
d6da6803a1
gh-132987: Support __index__() in hashlib.scrypt() (GH-133100)
Even if such signature is not supported by PyArg_ParseTupleAndKeywords(),
Argument Clinic supports it with inlined converters.
2025-04-29 16:16:05 +03:00
Bénédikt Tran
3695ba93d5
gh-132993: expose HASHLIB_GIL_MINSIZE to private extension modules (#132999) 2025-04-27 22:20:15 +00:00
Bénédikt Tran
2df0f88047
gh-132674: fix _hashopenssl.c compiler warnings on free-threaded build (#132675) 2025-04-18 13:52:17 +02:00
Bénédikt Tran
ac50ece6ce
gh-127667: refactor and improve _hashopenssl.c error branches (#131145)
Refactor `_setException()` into different helpers that can be used separately:

- set_ssl_exception_from_errcode(): set an exception from an explicit SSL error code.
- raise_ssl_error(): set an exception from the last SSL error code or use a user-defined message.
- notify_ssl_error_occurred(): same as raise_ssl_error() but with a generic default message.
2025-03-17 11:12:55 +01:00
Victor Stinner
9d759b63d8
gh-111178: Change Argument Clinic signature for METH_O (#130682)
Use "PyObject*" for METH_O functions to fix an undefined behavior.
2025-03-11 16:33:36 +01:00
Bénédikt Tran
097846502b
gh-127667: fix memory leaks in hashlib (#127668)
- Correctly handle `NULL` values returned by `EVP_MD_CTX_md`.
- Correctly free resources in error branches.
- Consistently suppress `_setException()` return value when needed.
- Collapse `_setException() + return NULL` into a single statement.
2025-03-03 09:20:33 +01:00
Hugo Beauzée-Luyssen
214562ed4d
gh-130740: Move some stdbool.h includes after Python.h (#130738)
Move some `#include <stdbool.h>` after `#include "Python.h"` when `pyconfig.h` is not
included first and when we are in a platform-agnostic context. This is to avoid having
features defined by `stdbool.h` before those decided by `Python.h`.
2025-03-02 09:56:49 +00:00
Bénédikt Tran
f976892b7d
gh-111178: fix UBSan failures in Modules/_hashopenssl.c (GH-129802)
Fix UBSan failures for `EVPobject`, `HMACobject`
2025-02-26 09:24:44 +00:00
Bénédikt Tran
071820113f
gh-130151: Fix reference leaks in _hashlib.hmac_{new,digest} (GH-130152)
* fix leak in `_hashlib.hmac_new`
* fix leak in `hmac_digest`
* fix exception type in `_hashlib.HMAC.copy`
2025-02-24 00:09:14 +00:00
Tomasz Pytel
6c67904e79
gh-128657: fix _hashopenssl ref/data race (GH-128886) 2025-02-07 18:47:45 -08:00
Bénédikt Tran
7303f06846
gh-126742: Add _PyErr_SetLocaleString, use it for gdbm & dlerror messages (GH-126746)
- Add a helper to set an error from locale-encoded `char*`
- Use the helper for gdbm & dlerror messages

Co-authored-by: Victor Stinner <vstinner@python.org>
2024-12-17 12:12:45 +01:00
Victor Stinner
d8e69b2c1b
gh-122854: Add Py_HashBuffer() function (#122855) 2024-08-30 15:42:27 +00:00
Brett Simmers
c2627d6eea
gh-116322: Add Py_mod_gil module slot (#116882)
This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).
2024-05-03 11:30:55 -04:00
Will Childs-Klein
b8eaad3009
gh-117233: Detect support for several hashes at hashlib build time (GH-117234)
Detect libcrypto BLAKE2, Shake, SHA3, and Truncated-SHA512 support at hashlib build time

## BLAKE2

While OpenSSL supports both "b" and "s" variants of the BLAKE2 hash
function, other cryptographic libraries may lack support for one or both
of the variants. This commit modifies `hashlib`'s C code to detect
whether or not the linked libcrypto supports each BLAKE2 variant, and
elides references to each variant's NID accordingly. In cases where the
underlying libcrypto doesn't fully support BLAKE2, CPython's
`./configure` script can be given the following flag to use CPython's
interned BLAKE2 implementation: `--with-builtin-hashlib-hashes=blake2`.

## SHA3, Shake, & truncated SHA512.

Detect BLAKE2, SHA3, Shake, & truncated SHA512 support in the
OpenSSL-ish libcrypto library at build time.  This helps allow hashlib's
`_hashopenssl` to be used with libraries that do not to support every
algorithm that upstream OpenSSL does.  Such as AWS-LC & BoringSSL.

Co-authored-by: Gregory P. Smith [Google LLC] <greg@krypto.org>
2024-04-11 16:49:41 +02:00
Tomas R
a6465605c1
gh-111916: Make hashlib related modules thread-safe without the GIL (#111981)
Always use an individual lock on hash objects when in free-threaded builds.

Fixes #111916
2023-11-15 23:53:38 +00:00
Serhiy Storchaka
329e4a1a3f
gh-86493: Modernize modules initialization code (GH-106858)
Use PyModule_Add() or PyModule_AddObjectRef() instead of soft deprecated
PyModule_AddObject().
2023-07-25 14:34:49 +03:00
Victor Stinner
89f9875448
gh-106320: Move private _PyHash API to the internal C API (#107026)
* No longer export most private _PyHash symbols, only export the ones
  which are needed by shared extensions.
* Modules/_xxtestfuzz/fuzzer.c now uses the internal C API.
2023-07-22 13:49:37 +00:00
Serhiy Storchaka
a293fa5915
gh-86493: Use PyModule_Add() instead of PyModule_AddObjectRef() (GH-106860) 2023-07-18 23:59:53 +03:00
Inada Naoki
d5bd32fb48
gh-104922: remove PY_SSIZE_T_CLEAN (#106315) 2023-07-02 15:07:46 +09:00
Serhiy Storchaka
1d33d53780
gh-106033: Get rid of new occurrences of PyDict_GetItem and PyObject_HasAttr (GH-106034)
These functions are broken by design because they discard any exceptions raised
inside, including MemoryError and KeyboardInterrupt.  They should not be
used in new code.
2023-06-23 20:10:32 +03:00
Victor Stinner
ef300937c2
gh-92536: Remove PyUnicode_READY() calls (#105210)
Since Python 3.12, PyUnicode_READY() does nothing and always
returns 0.
2023-06-02 01:33:17 +02:00
Gregory P. Smith
2e5d8a90aa
gh-99108: Release the GIL around hashlib built-in computation (#104675)
This matches the GIL releasing behavior of our existing `_hashopenssl`
module, extending it to the HACL* built-ins.

Includes adding comments to better describe the ENTER/LEAVE macros
purpose and explain the lock strategy in both existing and new code.
2023-05-23 00:06:41 +00:00
Eric Snow
a9c6e0618f
gh-99113: Add Py_MOD_PER_INTERPRETER_GIL_SUPPORTED (gh-104205)
Here we are doing no more than adding the value for Py_mod_multiple_interpreters and using it for stdlib modules.  We will start checking for it in gh-104206 (once PyInterpreterState.ceval.own_gil is added in gh-104204).
2023-05-05 21:11:27 +00:00
Dong-hee Na
efb0a2cf3a
gh-103256: Fix hmac algorithm to support fallback implementation (gh-103286)
Co-authored-by: Gregory P. Smith <greg@krypto.org>
2023-04-07 10:51:29 +09:00
chgnrdv
85ba8a3e03
gh-102650: Remove duplicate include directives from multiple source files (#102651)
Remove duplicate include directives from multiple source files
2023-03-13 13:25:17 -06:00
Thomas A Caswell
0709586744
DOC: correct bytesarray -> bytearray in comments (GH-92410) 2022-06-21 22:04:03 +02:00