Skip to content

gh-135532: cleanup clinic module directives for cryptographic modules #135822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ py_hashentry_table_new(void) {
return NULL;
}

/* Module state */
// --- Module state -----------------------------------------------------------

static PyModuleDef _hashlibmodule;

typedef struct {
Expand All @@ -277,6 +278,8 @@ get_hashlib_state(PyObject *module)
return (_hashlibstate *)state;
}

// --- Module objects ---------------------------------------------------------

typedef struct {
HASHLIB_OBJECT_HEAD
EVP_MD_CTX *ctx; /* OpenSSL message digest context */
Expand All @@ -291,15 +294,17 @@ typedef struct {

#define HMACobject_CAST(op) ((HMACobject *)(op))

#include "clinic/_hashopenssl.c.h"
// --- Module clinic configuration --------------------------------------------

/*[clinic input]
module _hashlib
class _hashlib.HASH "HASHobject *" "((_hashlibstate *)PyModule_GetState(module))->HASH_type"
class _hashlib.HASHXOF "HASHobject *" "((_hashlibstate *)PyModule_GetState(module))->HASHXOF_type"
class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))->HMAC_type"
class _hashlib.HASH "HASHobject *" "&PyType_Type"
class _hashlib.HASHXOF "HASHobject *" "&PyType_Type"
class _hashlib.HMAC "HMACobject *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=eb805ce4b90b1b31]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6b5c9ce5c28bdc58]*/

#include "clinic/_hashopenssl.c.h"

/* LCOV_EXCL_START */

Expand Down
12 changes: 8 additions & 4 deletions Modules/blake2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Written in 2013 by Dmitry Chestnykh <dmitry@codingrobots.com>
* Modified for CPython by Christian Heimes <christian@python.org>
* Updated to use HACL* by Jonathan Protzenko <jonathan@protzenko.fr>
* Additional work by Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
*
* To the extent possible under law, the author have dedicated all
* copyright and related and neighboring rights to this software to
Expand Down Expand Up @@ -368,15 +369,18 @@ typedef struct {

#define _Blake2Object_CAST(op) ((Blake2Object *)(op))

#include "clinic/blake2module.c.h"
// --- Module clinic configuration --------------------------------------------

/*[clinic input]
module _blake2
class _blake2.blake2b "Blake2Object *" "&PyBlake2_BLAKE2bType"
class _blake2.blake2s "Blake2Object *" "&PyBlake2_BLAKE2sType"
class _blake2.blake2b "Blake2Object *" "&PyType_Type"
class _blake2.blake2s "Blake2Object *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b7526666bd18af83]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=86b0972b0c41b3d0]*/

#include "clinic/blake2module.c.h"

// --- BLAKE-2 object interface -----------------------------------------------

static Blake2Object *
new_Blake2Object(PyTypeObject *type)
Expand Down
24 changes: 16 additions & 8 deletions Modules/md5module.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Andrew Kuchling (amk@amk.ca)
Greg Stein (gstein@lyra.org)
Trevor Perrin (trevp@trevp.net)
Bénédikt Tran (10796600+picnixz@users.noreply.github.com)

Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
Licensed to PSF under a Contributor Agreement.
Expand All @@ -25,18 +26,14 @@

#include "hashlib.h"

/*[clinic input]
module _md5
class MD5Type "MD5object *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
#include "_hacl/Hacl_Hash_MD5.h"

/* The MD5 block size and message digest sizes, in bytes */

#define MD5_BLOCKSIZE 64
#define MD5_DIGESTSIZE 16

#include "_hacl/Hacl_Hash_MD5.h"
// --- Module objects ---------------------------------------------------------

typedef struct {
HASHLIB_OBJECT_HEAD
Expand All @@ -45,8 +42,7 @@ typedef struct {

#define _MD5object_CAST(op) ((MD5object *)(op))

#include "clinic/md5module.c.h"

// --- Module state -----------------------------------------------------------

typedef struct {
PyTypeObject* md5_type;
Expand All @@ -60,6 +56,18 @@ md5_get_state(PyObject *module)
return (MD5State *)state;
}

// --- Module clinic configuration --------------------------------------------

/*[clinic input]
module _md5
class MD5Type "MD5object *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/

#include "clinic/md5module.c.h"

// --- MD5 object interface ---------------------------------------------------

static MD5object *
newMD5object(MD5State * st)
{
Expand Down
25 changes: 16 additions & 9 deletions Modules/sha1module.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
Andrew Kuchling (amk@amk.ca)
Greg Stein (gstein@lyra.org)
Trevor Perrin (trevp@trevp.net)
Bénédikt Tran (10796600+picnixz@users.noreply.github.com)

Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
Licensed to PSF under a Contributor Agreement.

*/

/* SHA1 objects */
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
Expand All @@ -24,18 +24,14 @@
#include "pycore_strhex.h" // _Py_strhex()
#include "pycore_typeobject.h" // _PyType_GetModuleState()

/*[clinic input]
module _sha1
class SHA1Type "SHA1object *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
#include "_hacl/Hacl_Hash_SHA1.h"

/* The SHA1 block size and message digest sizes, in bytes */

#define SHA1_BLOCKSIZE 64
#define SHA1_DIGESTSIZE 20

#include "_hacl/Hacl_Hash_SHA1.h"
// --- Module objects ---------------------------------------------------------

typedef struct {
HASHLIB_OBJECT_HEAD
Expand All @@ -44,8 +40,7 @@ typedef struct {

#define _SHA1object_CAST(op) ((SHA1object *)(op))

#include "clinic/sha1module.c.h"

// --- Module state -----------------------------------------------------------

typedef struct {
PyTypeObject* sha1_type;
Expand All @@ -59,6 +54,18 @@ sha1_get_state(PyObject *module)
return (SHA1State *)state;
}

// --- Module clinic configuration --------------------------------------------

/*[clinic input]
module _sha1
class SHA1Type "SHA1object *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/

#include "clinic/sha1module.c.h"

// --- SHA-1 object interface configuration -----------------------------------

static SHA1object *
newSHA1object(SHA1State *st)
{
Expand Down
30 changes: 17 additions & 13 deletions Modules/sha2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,25 @@
Greg Stein (gstein@lyra.org)
Trevor Perrin (trevp@trevp.net)
Jonathan Protzenko (jonathan@protzenko.fr)
Bénédikt Tran (10796600+picnixz@users.noreply.github.com)

Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
Licensed to PSF under a Contributor Agreement.

*/

/* SHA objects */
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif

#include "Python.h"
#include "pycore_bitutils.h" // _Py_bswap32()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_typeobject.h" // _PyType_GetModuleState()
#include "pycore_strhex.h" // _Py_strhex()

#include "hashlib.h"

/*[clinic input]
module _sha2
class SHA256Type "SHA256object *" "&PyType_Type"
class SHA512Type "SHA512object *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5315a7b611c9afc]*/

#include "_hacl/Hacl_Hash_SHA2.h"

/* The SHA block sizes and maximum message digest sizes, in bytes */

Expand All @@ -43,9 +36,7 @@ class SHA512Type "SHA512object *" "&PyType_Type"
#define SHA512_BLOCKSIZE 128
#define SHA512_DIGESTSIZE 64

/* Our SHA2 implementations defer to the HACL* verified library. */

#include "_hacl/Hacl_Hash_SHA2.h"
// --- Module objects ---------------------------------------------------------

// TODO: Get rid of int digestsize in favor of Hacl state info?

Expand All @@ -64,7 +55,7 @@ typedef struct {
#define _SHA256object_CAST(op) ((SHA256object *)(op))
#define _SHA512object_CAST(op) ((SHA512object *)(op))

#include "clinic/sha2module.c.h"
// --- Module state -----------------------------------------------------------

/* We shall use run-time type information in the remainder of this module to
* tell apart SHA2-224 and SHA2-256 */
Expand All @@ -83,6 +74,19 @@ sha2_get_state(PyObject *module)
return (sha2_state *)state;
}

// --- Module clinic configuration --------------------------------------------

/*[clinic input]
module _sha2
class SHA256Type "SHA256object *" "&PyType_Type"
class SHA512Type "SHA512object *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5315a7b611c9afc]*/

#include "clinic/sha2module.c.h"

// --- SHA-2 object interface -------------------------------------------------

static int
SHA256copy(SHA256object *src, SHA256object *dest)
{
Expand Down
33 changes: 21 additions & 12 deletions Modules/sha3module.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Greg Stein (gstein@lyra.org)
* Trevor Perrin (trevp@trevp.net)
* Gregory P. Smith (greg@krypto.org)
* Bénédikt Tran (10796600+picnixz@users.noreply.github.com)
*
* Copyright (C) 2012-2022 Christian Heimes (christian@python.org)
* Licensed to PSF under a Contributor Agreement.
Expand All @@ -24,6 +25,8 @@
#include "pycore_typeobject.h" // _PyType_GetModuleState()
#include "hashlib.h"

#include "_hacl/Hacl_Hash_SHA3.h"

/*
* Assert that 'LEN' can be safely casted to uint32_t.
*
Expand All @@ -37,6 +40,8 @@

#define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */

// --- Module state -----------------------------------------------------------

typedef struct {
PyTypeObject *sha3_224_type;
PyTypeObject *sha3_256_type;
Expand All @@ -54,30 +59,34 @@ sha3_get_state(PyObject *module)
return (SHA3State *)state;
}

/*[clinic input]
module _sha3
class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ"
class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ"
class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ"
class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ"
class _sha3.shake_128 "SHA3object *" "&SHAKE128type"
class _sha3.shake_256 "SHA3object *" "&SHAKE256type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/
// --- Module objects ---------------------------------------------------------

/* The structure for storing SHA3 info */

#include "_hacl/Hacl_Hash_SHA3.h"

typedef struct {
HASHLIB_OBJECT_HEAD
Hacl_Hash_SHA3_state_t *hash_state;
} SHA3object;

#define _SHA3object_CAST(op) ((SHA3object *)(op))

// --- Module clinic configuration --------------------------------------------

/*[clinic input]
module _sha3
class _sha3.sha3_224 "SHA3object *" "&PyType_Type"
class _sha3.sha3_256 "SHA3object *" "&PyType_Type"
class _sha3.sha3_384 "SHA3object *" "&PyType_Type"
class _sha3.sha3_512 "SHA3object *" "&PyType_Type"
class _sha3.shake_128 "SHA3object *" "&PyType_Type"
class _sha3.shake_256 "SHA3object *" "&PyType_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ccd22550c7fb99bf]*/

#include "clinic/sha3module.c.h"

// --- SHA-3 object interface -------------------------------------------------

static SHA3object *
newSHA3object(PyTypeObject *type)
{
Expand Down
Loading