gh-135532: cleanup clinic `module` directives for cryptographic modul… · python/cpython@621a8bd · GitHub
Skip to content

Commit 621a8bd

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

6 files changed

Lines changed: 89 additions & 52 deletions

File tree

Modules/_hashopenssl.c

Lines changed: 11 additions & 6 deletions

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

0 commit comments

Comments
 (0)