GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Unify recognization of message digest names for `_hashlib` and `_hmac` · Issue #131876 · python/cpython

Unify recognization of message digest names for _hashlib and _hmac #131876

New issue
Copy link
New issue
Copy link
Open
Open
Unify recognization of message digest names for _hashlib and _hmac#131876
Copy link
Assignees
picnixz
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement

Description

@picnixz
picnixz
opened on Mar 29, 2025
Issue body actions

Feature or enhancement

When calling hmac.new(key, digestmod=HASH), the HASH can be:

  • a named algorithm recognized by hashlib.new, (e.g., sha256);
  • a digest constructor (e.g., hashlib.openssl_sha256);
  • an object supporting PEP-247.

In #130157, I've only supported named algorithms as HACL* only supports named algorithms. When using OpenSSL HMAC instead of HACL* HMAC, determining which hash function to use from HASH is left to the hashlib C implementation, which itself delegates this task to OpenSSL based on NIDs.

I'm creating this issue so that we can brainstorm and decide how we should make HACL* HMAC able to also possibly detect objects supporting PEP-247. I plan to first upgrade the HMAC documentation:

Return a new hmac object. key is a bytes or bytearray object giving the secret key. If msg is present, the method call update(msg) is made. digestmod is the digest name, digest constructor or module for the HMAC object to use. It may be any name suitable to hashlib.new(). Despite its argument position, it is required.

As you may see, the terms "digest constructor" and "module" are not well-defined. So I first plan to explain these two. In a second phase, I plan to extract the code in _hashopenssl.c responsible for determining whether a name is known or not in a separate module so that it can be shared with the HMAC C implementation later. The idea is to ease the future transition where we would drop OpenSSL (but this is still not planned nor decided, and this would likely require a PEP) and entirely rely on HACL* instead, both for speed and security.

Some questions we need to address before letting HACL* HMAC support non-named algorithms:

  • Should we restrict digest constructors to HACL* ones only or not?
  • If not, should we consider hashlib.openssl_sha256 equivalent to using HACL* SHA-256?
  • If not again, should we fallback to a generic implementation of HMAC which directly calls that callable

Currently, passing a digest constructor to the (OpenSSL) C implementation does not mean that we're using it. We're actually using it to recover the algorithm name, so it can regarded as an alias.

OTOH, in the Python implementation of HMAC, any callable is considered a digest constructor and will be used as is. In some sense, it's a way to have HMAC implemented using an arbitrary hash function (HMAC is designed as such).

Linked PRs

  • gh-131876: extract _hashlib helpers into a separate directory #136995
  • gh-131876: use extern storage for _hashlib helper functions #137301
  • gh-131876: Revert "gh-131876: extract _hashlib helpers into a separate directory (#136995) #137307
  • gh-131876: extract _hashlib helpers into a separate directory #137319

Activity

  1. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Mar 29, 2025
  2. picnixz
    self-assigned this
    on Mar 29, 2025
  3. picnixz
    changed the title [-][RFE] Improve recognization of hashlib-supported algorithms for HMAC[/-] [+]Improve recognization of hashlib-supported algorithms for HMAC[/+] on Jun 10, 2025
  4. picnixz
    added
    type-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)
    and removed
    type-featureA feature request or enhancementA feature request or enhancement
    on Jun 10, 2025
  5. picnixz
    changed the title [-]Improve recognization of hashlib-supported algorithms for HMAC[/-] [+]Unify recognization of message digest names for `_hashlib` and `_hmac`[/+] on Jun 10, 2025
  6. picnixz
    added a commit that references this issue on Jul 28, 2025

    gh-131876: extract `_hashlib` helpers into a separate directory (#136995

    45138d3
  7. picnixz commented on Jul 28, 2025

    @picnixz
    picnixz
    on Jul 28, 2025
    MemberAuthor
    More actions

    Next steps:

    • Find a nice API for identifying digest-like objects.
    • Move more helpers into these new files if possible.
  8. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    and removed
    type-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)
    on Jul 29, 2025
  9. picnixz
    added a commit that references this issue on Aug 1, 2025

    Revert "pythongh-131876: extract `_hashlib` helpers into a separate d…

    4feb4d0
  10. gpshead
    added a commit that references this issue on Aug 1, 2025

    gh-131876: Revert "gh-131876: extract `_hashlib` helpers into a separ…

    fe0e921
  11. picnixz
    added 4 commits that reference this issue on Aug 2, 2025

    pythongh-131876: extract `_hashlib` helpers into a separate directory (…

    688ad54

    pythongh-131876: extract `_hashlib` helpers into a separate directory (…

    a629e42

    pythongh-131876: extract `_hashlib` helpers into a separate directory (…

    c7f7776

    pythongh-131876: extract `_hashlib` helpers into a separate directory (…

    2fb8a6c
  12. Agent-Hellboy
    added 2 commits that reference this issue on Aug 19, 2025

    pythongh-131876: extract `_hashlib` helpers into a separate directory (…

    b707ead

    pythongh-131876: Revert "pythongh-131876: extract `_hashlib` helpers …

    ef4a1fc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @picnixz
    picnixz

Labels

extension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.