Skip to content

Commit 621a8bd

Browse files
authored
gh-135532: cleanup clinic module directives for cryptographic modules (#135822)
1 parent b57b619 commit 621a8bd

File tree

6 files changed

+89
-52
lines changed

6 files changed

+89
-52
lines changed

Modules/_hashopenssl.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ py_hashentry_table_new(void) {
255255
return NULL;
256256
}
257257

258-
/* Module state */
258+
// --- Module state -----------------------------------------------------------
259+
259260
static PyModuleDef _hashlibmodule;
260261

261262
typedef struct {
@@ -277,6 +278,8 @@ get_hashlib_state(PyObject *module)
277278
return (_hashlibstate *)state;
278279
}
279280

281+
// --- Module objects ---------------------------------------------------------
282+
280283
typedef struct {
281284
HASHLIB_OBJECT_HEAD
282285
EVP_MD_CTX *ctx; /* OpenSSL message digest context */
@@ -291,15 +294,17 @@ typedef struct {
291294

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

294-
#include "clinic/_hashopenssl.c.h"
297+
// --- Module clinic configuration --------------------------------------------
298+
295299
/*[clinic input]
296300
module _hashlib
297-
class _hashlib.HASH "HASHobject *" "((_hashlibstate *)PyModule_GetState(module))->HASH_type"
298-
class _hashlib.HASHXOF "HASHobject *" "((_hashlibstate *)PyModule_GetState(module))->HASHXOF_type"
299-
class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))->HMAC_type"
301+
class _hashlib.HASH "HASHobject *" "&PyType_Type"
302+
class _hashlib.HASHXOF "HASHobject *" "&PyType_Type"
303+
class _hashlib.HMAC "HMACobject *" "&PyType_Type"
300304
[clinic start generated code]*/
301-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=eb805ce4b90b1b31]*/
305+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6b5c9ce5c28bdc58]*/
302306

307+
#include "clinic/_hashopenssl.c.h"
303308

304309
/* LCOV_EXCL_START */
305310

Modules/blake2module.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Written in 2013 by Dmitry Chestnykh <dmitry@codingrobots.com>
33
* Modified for CPython by Christian Heimes <christian@python.org>
44
* Updated to use HACL* by Jonathan Protzenko <jonathan@protzenko.fr>
5+
* Additional work by Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
56
*
67
* To the extent possible under law, the author have dedicated all
78
* copyright and related and neighboring rights to this software to
@@ -368,15 +369,18 @@ typedef struct {
368369

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

371-
#include "clinic/blake2module.c.h"
372+
// --- Module clinic configuration --------------------------------------------
372373

373374
/*[clinic input]
374375
module _blake2
375-
class _blake2.blake2b "Blake2Object *" "&PyBlake2_BLAKE2bType"
376-
class _blake2.blake2s "Blake2Object *" "&PyBlake2_BLAKE2sType"
376+
class _blake2.blake2b "Blake2Object *" "&PyType_Type"
377+
class _blake2.blake2s "Blake2Object *" "&PyType_Type"
377378
[clinic start generated code]*/
378-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b7526666bd18af83]*/
379+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=86b0972b0c41b3d0]*/
380+
381+
#include "clinic/blake2module.c.h"
379382

383+
// --- BLAKE-2 object interface -----------------------------------------------
380384

381385
static Blake2Object *
382386
new_Blake2Object(PyTypeObject *type)

Modules/md5module.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
Andrew Kuchling (amk@amk.ca)
99
Greg Stein (gstein@lyra.org)
1010
Trevor Perrin (trevp@trevp.net)
11+
Bénédikt Tran (10796600+picnixz@users.noreply.github.com)
1112
1213
Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
1314
Licensed to PSF under a Contributor Agreement.
@@ -25,18 +26,14 @@
2526

2627
#include "hashlib.h"
2728

28-
/*[clinic input]
29-
module _md5
30-
class MD5Type "MD5object *" "&PyType_Type"
31-
[clinic start generated code]*/
32-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
29+
#include "_hacl/Hacl_Hash_MD5.h"
3330

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

3633
#define MD5_BLOCKSIZE 64
3734
#define MD5_DIGESTSIZE 16
3835

39-
#include "_hacl/Hacl_Hash_MD5.h"
36+
// --- Module objects ---------------------------------------------------------
4037

4138
typedef struct {
4239
HASHLIB_OBJECT_HEAD
@@ -45,8 +42,7 @@ typedef struct {
4542

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

48-
#include "clinic/md5module.c.h"
49-
45+
// --- Module state -----------------------------------------------------------
5046

5147
typedef struct {
5248
PyTypeObject* md5_type;
@@ -60,6 +56,18 @@ md5_get_state(PyObject *module)
6056
return (MD5State *)state;
6157
}
6258

59+
// --- Module clinic configuration --------------------------------------------
60+
61+
/*[clinic input]
62+
module _md5
63+
class MD5Type "MD5object *" "&PyType_Type"
64+
[clinic start generated code]*/
65+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6e5261719957a912]*/
66+
67+
#include "clinic/md5module.c.h"
68+
69+
// --- MD5 object interface ---------------------------------------------------
70+
6371
static MD5object *
6472
newMD5object(MD5State * st)
6573
{

Modules/sha1module.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
Andrew Kuchling (amk@amk.ca)
99
Greg Stein (gstein@lyra.org)
1010
Trevor Perrin (trevp@trevp.net)
11+
Bénédikt Tran (10796600+picnixz@users.noreply.github.com)
1112
1213
Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
1314
Licensed to PSF under a Contributor Agreement.
1415
1516
*/
1617

17-
/* SHA1 objects */
1818
#ifndef Py_BUILD_CORE_BUILTIN
1919
# define Py_BUILD_CORE_MODULE 1
2020
#endif
@@ -24,18 +24,14 @@
2424
#include "pycore_strhex.h" // _Py_strhex()
2525
#include "pycore_typeobject.h" // _PyType_GetModuleState()
2626

27-
/*[clinic input]
28-
module _sha1
29-
class SHA1Type "SHA1object *" "&PyType_Type"
30-
[clinic start generated code]*/
31-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
27+
#include "_hacl/Hacl_Hash_SHA1.h"
3228

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

3531
#define SHA1_BLOCKSIZE 64
3632
#define SHA1_DIGESTSIZE 20
3733

38-
#include "_hacl/Hacl_Hash_SHA1.h"
34+
// --- Module objects ---------------------------------------------------------
3935

4036
typedef struct {
4137
HASHLIB_OBJECT_HEAD
@@ -44,8 +40,7 @@ typedef struct {
4440

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

47-
#include "clinic/sha1module.c.h"
48-
43+
// --- Module state -----------------------------------------------------------
4944

5045
typedef struct {
5146
PyTypeObject* sha1_type;
@@ -59,6 +54,18 @@ sha1_get_state(PyObject *module)
5954
return (SHA1State *)state;
6055
}
6156

57+
// --- Module clinic configuration --------------------------------------------
58+
59+
/*[clinic input]
60+
module _sha1
61+
class SHA1Type "SHA1object *" "&PyType_Type"
62+
[clinic start generated code]*/
63+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
64+
65+
#include "clinic/sha1module.c.h"
66+
67+
// --- SHA-1 object interface configuration -----------------------------------
68+
6269
static SHA1object *
6370
newSHA1object(SHA1State *st)
6471
{

Modules/sha2module.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,25 @@
99
Greg Stein (gstein@lyra.org)
1010
Trevor Perrin (trevp@trevp.net)
1111
Jonathan Protzenko (jonathan@protzenko.fr)
12+
Bénédikt Tran (10796600+picnixz@users.noreply.github.com)
1213
1314
Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
1415
Licensed to PSF under a Contributor Agreement.
1516
1617
*/
1718

18-
/* SHA objects */
1919
#ifndef Py_BUILD_CORE_BUILTIN
2020
# define Py_BUILD_CORE_MODULE 1
2121
#endif
2222

2323
#include "Python.h"
24-
#include "pycore_bitutils.h" // _Py_bswap32()
2524
#include "pycore_moduleobject.h" // _PyModule_GetState()
2625
#include "pycore_typeobject.h" // _PyType_GetModuleState()
2726
#include "pycore_strhex.h" // _Py_strhex()
2827

2928
#include "hashlib.h"
3029

31-
/*[clinic input]
32-
module _sha2
33-
class SHA256Type "SHA256object *" "&PyType_Type"
34-
class SHA512Type "SHA512object *" "&PyType_Type"
35-
[clinic start generated code]*/
36-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5315a7b611c9afc]*/
37-
30+
#include "_hacl/Hacl_Hash_SHA2.h"
3831

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

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

46-
/* Our SHA2 implementations defer to the HACL* verified library. */
47-
48-
#include "_hacl/Hacl_Hash_SHA2.h"
39+
// --- Module objects ---------------------------------------------------------
4940

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

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

67-
#include "clinic/sha2module.c.h"
58+
// --- Module state -----------------------------------------------------------
6859

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

77+
// --- Module clinic configuration --------------------------------------------
78+
79+
/*[clinic input]
80+
module _sha2
81+
class SHA256Type "SHA256object *" "&PyType_Type"
82+
class SHA512Type "SHA512object *" "&PyType_Type"
83+
[clinic start generated code]*/
84+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5315a7b611c9afc]*/
85+
86+
#include "clinic/sha2module.c.h"
87+
88+
// --- SHA-2 object interface -------------------------------------------------
89+
8690
static int
8791
SHA256copy(SHA256object *src, SHA256object *dest)
8892
{

Modules/sha3module.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Greg Stein (gstein@lyra.org)
1010
* Trevor Perrin (trevp@trevp.net)
1111
* Gregory P. Smith (greg@krypto.org)
12+
* Bénédikt Tran (10796600+picnixz@users.noreply.github.com)
1213
*
1314
* Copyright (C) 2012-2022 Christian Heimes (christian@python.org)
1415
* Licensed to PSF under a Contributor Agreement.
@@ -24,6 +25,8 @@
2425
#include "pycore_typeobject.h" // _PyType_GetModuleState()
2526
#include "hashlib.h"
2627

28+
#include "_hacl/Hacl_Hash_SHA3.h"
29+
2730
/*
2831
* Assert that 'LEN' can be safely casted to uint32_t.
2932
*
@@ -37,6 +40,8 @@
3740

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

43+
// --- Module state -----------------------------------------------------------
44+
4045
typedef struct {
4146
PyTypeObject *sha3_224_type;
4247
PyTypeObject *sha3_256_type;
@@ -54,30 +59,34 @@ sha3_get_state(PyObject *module)
5459
return (SHA3State *)state;
5560
}
5661

57-
/*[clinic input]
58-
module _sha3
59-
class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ"
60-
class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ"
61-
class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ"
62-
class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ"
63-
class _sha3.shake_128 "SHA3object *" "&SHAKE128type"
64-
class _sha3.shake_256 "SHA3object *" "&SHAKE256type"
65-
[clinic start generated code]*/
66-
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/
62+
// --- Module objects ---------------------------------------------------------
6763

6864
/* The structure for storing SHA3 info */
6965

70-
#include "_hacl/Hacl_Hash_SHA3.h"
71-
7266
typedef struct {
7367
HASHLIB_OBJECT_HEAD
7468
Hacl_Hash_SHA3_state_t *hash_state;
7569
} SHA3object;
7670

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

73+
// --- Module clinic configuration --------------------------------------------
74+
75+
/*[clinic input]
76+
module _sha3
77+
class _sha3.sha3_224 "SHA3object *" "&PyType_Type"
78+
class _sha3.sha3_256 "SHA3object *" "&PyType_Type"
79+
class _sha3.sha3_384 "SHA3object *" "&PyType_Type"
80+
class _sha3.sha3_512 "SHA3object *" "&PyType_Type"
81+
class _sha3.shake_128 "SHA3object *" "&PyType_Type"
82+
class _sha3.shake_256 "SHA3object *" "&PyType_Type"
83+
[clinic start generated code]*/
84+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ccd22550c7fb99bf]*/
85+
7986
#include "clinic/sha3module.c.h"
8087

88+
// --- SHA-3 object interface -------------------------------------------------
89+
8190
static SHA3object *
8291
newSHA3object(PyTypeObject *type)
8392
{

0 commit comments

Comments
 (0)