gh-135532: cleanup clinic module directives for cryptographic modules (#135822)
Some checks are pending
Tests / (push) Blocked by required conditions
Tests / Windows MSI (push) Blocked by required conditions
Tests / Change detection (push) Waiting to run
Tests / Docs (push) Blocked by required conditions
Tests / Check if Autoconf files are up to date (push) Blocked by required conditions
Tests / Check if generated files are up to date (push) Blocked by required conditions
Tests / Ubuntu SSL tests with OpenSSL (push) Blocked by required conditions
Tests / WASI (push) Blocked by required conditions
Tests / Hypothesis tests on Ubuntu (push) Blocked by required conditions
Tests / Address sanitizer (push) Blocked by required conditions
Tests / Undefined behavior sanitizer (push) Blocked by required conditions
Tests / Cross build Linux (push) Blocked by required conditions
Tests / CIFuzz (push) Blocked by required conditions
Tests / All required checks pass (push) Blocked by required conditions
Lint / lint (push) Waiting to run
mypy / Run mypy on Lib/_pyrepl (push) Waiting to run
mypy / Run mypy on Lib/test/libregrtest (push) Waiting to run
mypy / Run mypy on Lib/tomllib (push) Waiting to run
mypy / Run mypy on Tools/build (push) Waiting to run
mypy / Run mypy on Tools/cases_generator (push) Waiting to run
mypy / Run mypy on Tools/clinic (push) Waiting to run
mypy / Run mypy on Tools/jit (push) Waiting to run
mypy / Run mypy on Tools/peg_generator (push) Waiting to run

This commit is contained in:
Bénédikt Tran 2025-06-22 22:04:38 +02:00 committed by GitHub
parent b57b619e34
commit 621a8bd6a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 89 additions and 52 deletions

View file

@ -255,7 +255,8 @@ py_hashentry_table_new(void) {
return NULL;
}
/* Module state */
// --- Module state -----------------------------------------------------------
static PyModuleDef _hashlibmodule;
typedef struct {
@ -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 */
@ -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 */

View file

@ -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
@ -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)

View file

@ -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.
@ -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
@ -45,8 +42,7 @@ typedef struct {
#define _MD5object_CAST(op) ((MD5object *)(op))
#include "clinic/md5module.c.h"
// --- Module state -----------------------------------------------------------
typedef struct {
PyTypeObject* md5_type;
@ -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)
{

View file

@ -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
@ -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
@ -44,8 +40,7 @@ typedef struct {
#define _SHA1object_CAST(op) ((SHA1object *)(op))
#include "clinic/sha1module.c.h"
// --- Module state -----------------------------------------------------------
typedef struct {
PyTypeObject* sha1_type;
@ -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)
{

View file

@ -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 */
@ -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?
@ -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 */
@ -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)
{

View file

@ -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.
@ -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.
*
@ -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;
@ -54,21 +59,10 @@ 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;
@ -76,8 +70,23 @@ typedef struct {
#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)
{