1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
|
/* SHA2 module */
/* This provides an interface to NIST's SHA2 224, 256, 384, & 512 Algorithms */
/* See below for information about the original code this module was
based upon. Additional work performed by:
Andrew Kuchling (amk@amk.ca)
Greg Stein (gstein@lyra.org)
Trevor Perrin (trevp@trevp.net)
Jonathan Protzenko (jonathan@protzenko.fr)
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 "structmember.h" // PyMemberDef
#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]*/
/* The SHA block sizes and maximum message digest sizes, in bytes */
#define SHA256_BLOCKSIZE 64
#define SHA256_DIGESTSIZE 32
#define SHA512_BLOCKSIZE 128
#define SHA512_DIGESTSIZE 64
/* Our SHA2 implementations defer to the HACL* verified library. */
#include "_hacl/Hacl_Streaming_SHA2.h"
// TODO: Get rid of int digestsize in favor of Hacl state info?
typedef struct {
PyObject_HEAD
int digestsize;
Hacl_Streaming_SHA2_state_sha2_256 *state;
} SHA256object;
typedef struct {
PyObject_HEAD
int digestsize;
Hacl_Streaming_SHA2_state_sha2_512 *state;
} SHA512object;
#include "clinic/sha2module.c.h"
/* We shall use run-time type information in the remainder of this module to
* tell apart SHA2-224 and SHA2-256 */
typedef struct {
PyTypeObject* sha224_type;
PyTypeObject* sha256_type;
PyTypeObject* sha384_type;
PyTypeObject* sha512_type;
} sha2_state;
static inline sha2_state*
sha2_get_state(PyObject *module)
{
void *state = _PyModule_GetState(module);
assert(state != NULL);
return (sha2_state *)state;
}
static void SHA256copy(SHA256object *src, SHA256object *dest)
{
dest->digestsize = src->digestsize;
dest->state = Hacl_Streaming_SHA2_copy_256(src->state);
}
static void SHA512copy(SHA512object *src, SHA512object *dest)
{
dest->digestsize = src->digestsize;
dest->state = Hacl_Streaming_SHA2_copy_512(src->state);
}
static SHA256object *
newSHA224object(sha2_state *state)
{
SHA256object *sha = (SHA256object *)PyObject_GC_New(
SHA256object, state->sha224_type);
if (!sha) {
return NULL;
}
PyObject_GC_Track(sha);
return sha;
}
static SHA256object *
newSHA256object(sha2_state *state)
{
SHA256object *sha = (SHA256object *)PyObject_GC_New(
SHA256object, state->sha256_type);
if (!sha) {
return NULL;
}
PyObject_GC_Track(sha);
return sha;
}
static SHA512object *
newSHA384object(sha2_state *state)
{
SHA512object *sha = (SHA512object *)PyObject_GC_New(
SHA512object, state->sha384_type);
if (!sha) {
return NULL;
}
PyObject_GC_Track(sha);
return sha;
}
static SHA512object *
newSHA512object(sha2_state *state)
{
SHA512object *sha = (SHA512object *)PyObject_GC_New(
SHA512object, state->sha512_type);
if (!sha) {
return NULL;
}
PyObject_GC_Track(sha);
return sha;
}
/* Internal methods for our hash objects. */
static int
SHA2_traverse(PyObject *ptr, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(ptr));
return 0;
}
static void
SHA256_dealloc(SHA256object *ptr)
{
Hacl_Streaming_SHA2_free_256(ptr->state);
PyTypeObject *tp = Py_TYPE(ptr);
PyObject_GC_UnTrack(ptr);
PyObject_GC_Del(ptr);
Py_DECREF(tp);
}
static void
SHA512_dealloc(SHA512object *ptr)
{
Hacl_Streaming_SHA2_free_512(ptr->state);
PyTypeObject *tp = Py_TYPE(ptr);
PyObject_GC_UnTrack(ptr);
PyObject_GC_Del(ptr);
Py_DECREF(tp);
}
/* HACL* takes a uint32_t for the length of its parameter, but Py_ssize_t can be
* 64 bits so we loop in <4gig chunks when needed. */
static void update_256(Hacl_Streaming_SHA2_state_sha2_256 *state, uint8_t *buf, Py_ssize_t len) {
/* Note: we explicitly ignore the error code on the basis that it would take >
* 1 billion years to overflow the maximum admissible length for SHA2-256
* (namely, 2^61-1 bytes). */
#if PY_SSIZE_T_MAX > UINT32_MAX
while (len > UINT32_MAX) {
Hacl_Streaming_SHA2_update_256(state, buf, UINT32_MAX);
len -= UINT32_MAX;
buf += UINT32_MAX;
}
#endif
/* Cast to uint32_t is safe: len <= UINT32_MAX at this point. */
Hacl_Streaming_SHA2_update_256(state, buf, (uint32_t) len);
}
static void update_512(Hacl_Streaming_SHA2_state_sha2_512 *state, uint8_t *buf, Py_ssize_t len) {
/* Note: we explicitly ignore the error code on the basis that it would take >
* 1 billion years to overflow the maximum admissible length for this API
* (namely, 2^64-1 bytes). */
#if PY_SSIZE_T_MAX > UINT32_MAX
while (len > UINT32_MAX) {
Hacl_Streaming_SHA2_update_512(state, buf, UINT32_MAX);
len -= UINT32_MAX;
buf += UINT32_MAX;
}
#endif
/* Cast to uint32_t is safe: len <= UINT32_MAX at this point. */
Hacl_Streaming_SHA2_update_512(state, buf, (uint32_t) len);
}
/* External methods for our hash objects */
/*[clinic input]
SHA256Type.copy
cls:defining_class
Return a copy of the hash object.
[clinic start generated code]*/
static PyObject *
SHA256Type_copy_impl(SHA256object *self, PyTypeObject *cls)
/*[clinic end generated code: output=fabd515577805cd3 input=3137146fcb88e212]*/
{
SHA256object *newobj;
sha2_state *state = _PyType_GetModuleState(cls);
if (Py_IS_TYPE(self, state->sha256_type)) {
if ((newobj = newSHA256object(state)) == NULL) {
return NULL;
}
} else {
if ((newobj = newSHA224object(state)) == NULL) {
return NULL;
}
}
SHA256copy(self, newobj);
return (PyObject *)newobj;
}
/*[clinic input]
SHA512Type.copy
cls: defining_class
Return a copy of the hash object.
[clinic start generated code]*/
static PyObject *
SHA512Type_copy_impl(SHA512object *self, PyTypeObject *cls)
/*[clinic end generated code: output=66d2a8ef20de8302 input=f673a18f66527c90]*/
{
SHA512object *newobj;
sha2_state *state = _PyType_GetModuleState(cls);
if (Py_IS_TYPE((PyObject*)self, state->sha512_type)) {
if ((newobj = newSHA512object(state)) == NULL) {
return NULL;
}
}
else {
if ((newobj = newSHA384object(state)) == NULL) {
return NULL;
}
}
SHA512copy(self, newobj);
return (PyObject *)newobj;
}
/*[clinic input]
SHA256Type.digest
Return the digest value as a bytes object.
[clinic start generated code]*/
static PyObject *
SHA256Type_digest_impl(SHA256object *self)
/*[clinic end generated code: output=3a2e3997a98ee792 input=f1f4cfea5cbde35c]*/
{
uint8_t digest[SHA256_DIGESTSIZE];
assert(self->digestsize <= SHA256_DIGESTSIZE);
// HACL* performs copies under the hood so that self->state remains valid
// after this call.
Hacl_Streaming_SHA2_finish_256(self->state, digest);
return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
}
/*[clinic input]
SHA512Type.digest
Return the digest value as a bytes object.
[clinic start generated code]*/
static PyObject *
SHA512Type_digest_impl(SHA512object *self)
/*[clinic end generated code: output=dd8c6320070458e0 input=f6470dd359071f4b]*/
{
uint8_t digest[SHA512_DIGESTSIZE];
assert(self->digestsize <= SHA512_DIGESTSIZE);
// HACL* performs copies under the hood so that self->state remains valid
// after this call.
Hacl_Streaming_SHA2_finish_512(self->state, digest);
return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
}
/*[clinic input]
SHA256Type.hexdigest
Return the digest value as a string of hexadecimal digits.
[clinic start generated code]*/
static PyObject *
SHA256Type_hexdigest_impl(SHA256object *self)
/*[clinic end generated code: output=96cb68996a780ab3 input=0cc4c714693010d1]*/
{
uint8_t digest[SHA256_DIGESTSIZE];
assert(self->digestsize <= SHA256_DIGESTSIZE);
Hacl_Streaming_SHA2_finish_256(self->state, digest);
return _Py_strhex((const char *)digest, self->digestsize);
}
/*[clinic input]
SHA512Type.hexdigest
Return the digest value as a string of hexadecimal digits.
[clinic start generated code]*/
static PyObject *
SHA512Type_hexdigest_impl(SHA512object *self)
/*[clinic end generated code: output=cbd6f844aba1fe7c input=498b877b25cbe0a2]*/
{
uint8_t digest[SHA512_DIGESTSIZE];
assert(self->digestsize <= SHA512_DIGESTSIZE);
Hacl_Streaming_SHA2_finish_512(self->state, digest);
return _Py_strhex((const char *)digest, self->digestsize);
}
/*[clinic input]
SHA256Type.update
obj: object
/
Update this hash object's state with the provided string.
[clinic start generated code]*/
static PyObject *
SHA256Type_update(SHA256object *self, PyObject *obj)
/*[clinic end generated code: output=1b240f965ddbd8c6 input=b2d449d5b30f0f5a]*/
{
Py_buffer buf;
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
update_256(self->state, buf.buf, buf.len);
PyBuffer_Release(&buf);
Py_RETURN_NONE;
}
/*[clinic input]
SHA512Type.update
obj: object
/
Update this hash object's state with the provided string.
[clinic start generated code]*/
static PyObject *
SHA512Type_update(SHA512object *self, PyObject *obj)
/*[clinic end generated code: output=745f51057a985884 input=ded2b46656566283]*/
{
Py_buffer buf;
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
update_512(self->state, buf.buf, buf.len);
PyBuffer_Release(&buf);
Py_RETURN_NONE;
}
static PyMethodDef SHA256_methods[] = {
SHA256TYPE_COPY_METHODDEF
SHA256TYPE_DIGEST_METHODDEF
SHA256TYPE_HEXDIGEST_METHODDEF
SHA256TYPE_UPDATE_METHODDEF
{NULL, NULL} /* sentinel */
};
static PyMethodDef SHA512_methods[] = {
SHA512TYPE_COPY_METHODDEF
SHA512TYPE_DIGEST_METHODDEF
SHA512TYPE_HEXDIGEST_METHODDEF
SHA512TYPE_UPDATE_METHODDEF
{NULL, NULL} /* sentinel */
};
static PyObject *
SHA256_get_block_size(PyObject *self, void *closure)
{
return PyLong_FromLong(SHA256_BLOCKSIZE);
}
static PyObject *
SHA512_get_block_size(PyObject *self, void *closure)
{
return PyLong_FromLong(SHA512_BLOCKSIZE);
}
static PyObject *
SHA256_get_digest_size(SHA256object *self, void *closure)
{
return PyLong_FromLong(self->digestsize);
}
static PyObject *
SHA512_get_digest_size(SHA512object *self, void *closure)
{
return PyLong_FromLong(self->digestsize);
}
static PyObject *
SHA256_get_name(SHA256object *self, void *closure)
{
if (self->digestsize == 28) {
return PyUnicode_FromStringAndSize("sha224", 6);
}
return PyUnicode_FromStringAndSize("sha256", 6);
}
static PyObject *
SHA512_get_name(SHA512object *self, void *closure)
{
if (self->digestsize == 64) {
return PyUnicode_FromStringAndSize("sha512", 6);
}
return PyUnicode_FromStringAndSize("sha384", 6);
}
static PyGetSetDef SHA256_getseters[] = {
{"block_size",
(getter)SHA256_get_block_size, NULL,
NULL,
NULL},
{"name",
(getter)SHA256_get_name, NULL,
NULL,
NULL},
{"digest_size",
(getter)SHA256_get_digest_size, NULL,
NULL,
NULL},
{NULL} /* Sentinel */
};
static PyGetSetDef SHA512_getseters[] = {
{"block_size",
(getter)SHA512_get_block_size, NULL,
NULL,
NULL},
{"name",
(getter)SHA512_get_name, NULL,
NULL,
NULL},
{"digest_size",
(getter)SHA512_get_digest_size, NULL,
NULL,
NULL},
{NULL} /* Sentinel */
};
static PyType_Slot sha256_types_slots[] = {
{Py_tp_dealloc, SHA256_dealloc},
{Py_tp_methods, SHA256_methods},
{Py_tp_getset, SHA256_getseters},
{Py_tp_traverse, SHA2_traverse},
{0,0}
};
static PyType_Slot sha512_type_slots[] = {
{Py_tp_dealloc, SHA512_dealloc},
{Py_tp_methods, SHA512_methods},
{Py_tp_getset, SHA512_getseters},
{Py_tp_traverse, SHA2_traverse},
{0,0}
};
// Using _PyType_GetModuleState() on these types is safe since they
// cannot be subclassed: they don't have the Py_TPFLAGS_BASETYPE flag.
static PyType_Spec sha224_type_spec = {
.name = "_sha2.SHA224Type",
.basicsize = sizeof(SHA256object),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
.slots = sha256_types_slots
};
static PyType_Spec sha256_type_spec = {
.name = "_sha2.SHA256Type",
.basicsize = sizeof(SHA256object),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
.slots = sha256_types_slots
};
static PyType_Spec sha384_type_spec = {
.name = "_sha2.SHA384Type",
.basicsize = sizeof(SHA512object),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
.slots = sha512_type_slots
};
static PyType_Spec sha512_type_spec = {
.name = "_sha2.SHA512Type",
.basicsize = sizeof(SHA512object),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
.slots = sha512_type_slots
};
/* The module-level constructors. */
/*[clinic input]
_sha2.sha256
string: object(c_default="NULL") = b''
*
usedforsecurity: bool = True
Return a new SHA-256 hash object; optionally initialized with a string.
[clinic start generated code]*/
static PyObject *
_sha2_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity)
/*[clinic end generated code: output=243c9dd289931f87 input=6249da1de607280a]*/
{
Py_buffer buf;
if (string) {
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
}
sha2_state *state = sha2_get_state(module);
SHA256object *new;
if ((new = newSHA256object(state)) == NULL) {
if (string) {
PyBuffer_Release(&buf);
}
return NULL;
}
new->state = Hacl_Streaming_SHA2_create_in_256();
new->digestsize = 32;
if (PyErr_Occurred()) {
Py_DECREF(new);
if (string) {
PyBuffer_Release(&buf);
}
return NULL;
}
if (string) {
update_256(new->state, buf.buf, buf.len);
PyBuffer_Release(&buf);
}
return (PyObject *)new;
}
/*[clinic input]
_sha2.sha224
string: object(c_default="NULL") = b''
*
usedforsecurity: bool = True
Return a new SHA-224 hash object; optionally initialized with a string.
[clinic start generated code]*/
static PyObject *
_sha2_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
/*[clinic end generated code: output=68191f232e4a3843 input=c42bcba47fd7d2b7]*/
{
Py_buffer buf;
if (string) {
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
}
sha2_state *state = sha2_get_state(module);
SHA256object *new;
if ((new = newSHA224object(state)) == NULL) {
if (string) {
PyBuffer_Release(&buf);
}
return NULL;
}
new->state = Hacl_Streaming_SHA2_create_in_224();
new->digestsize = 28;
if (PyErr_Occurred()) {
Py_DECREF(new);
if (string) {
PyBuffer_Release(&buf);
}
return NULL;
}
if (string) {
update_256(new->state, buf.buf, buf.len);
PyBuffer_Release(&buf);
}
return (PyObject *)new;
}
/*[clinic input]
_sha2.sha512
string: object(c_default="NULL") = b''
*
usedforsecurity: bool = True
Return a new SHA-512 hash object; optionally initialized with a string.
[clinic start generated code]*/
static PyObject *
_sha2_sha512_impl(PyObject *module, PyObject *string, int usedforsecurity)
/*[clinic end generated code: output=d55c8996eca214d7 input=0576ae2a6ebfad25]*/
{
SHA512object *new;
Py_buffer buf;
sha2_state *state = sha2_get_state(module);
if (string)
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
if ((new = newSHA512object(state)) == NULL) {
if (string)
PyBuffer_Release(&buf);
return NULL;
}
new->state = Hacl_Streaming_SHA2_create_in_512();
new->digestsize = 64;
if (PyErr_Occurred()) {
Py_DECREF(new);
if (string)
PyBuffer_Release(&buf);
return NULL;
}
if (string) {
update_512(new->state, buf.buf, buf.len);
PyBuffer_Release(&buf);
}
return (PyObject *)new;
}
/*[clinic input]
_sha2.sha384
string: object(c_default="NULL") = b''
*
usedforsecurity: bool = True
Return a new SHA-384 hash object; optionally initialized with a string.
[clinic start generated code]*/
static PyObject *
_sha2_sha384_impl(PyObject *module, PyObject *string, int usedforsecurity)
/*[clinic end generated code: output=b29a0d81d51d1368 input=4e9199d8de0d2f9b]*/
{
SHA512object *new;
Py_buffer buf;
sha2_state *state = sha2_get_state(module);
if (string)
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
if ((new = newSHA384object(state)) == NULL) {
if (string)
PyBuffer_Release(&buf);
return NULL;
}
new->state = Hacl_Streaming_SHA2_create_in_384();
new->digestsize = 48;
if (PyErr_Occurred()) {
Py_DECREF(new);
if (string)
PyBuffer_Release(&buf);
return NULL;
}
if (string) {
update_512(new->state, buf.buf, buf.len);
PyBuffer_Release(&buf);
}
return (PyObject *)new;
}
/* List of functions exported by this module */
static struct PyMethodDef SHA2_functions[] = {
_SHA2_SHA256_METHODDEF
_SHA2_SHA224_METHODDEF
_SHA2_SHA512_METHODDEF
_SHA2_SHA384_METHODDEF
{NULL, NULL} /* Sentinel */
};
static int
_sha2_traverse(PyObject *module, visitproc visit, void *arg)
{
sha2_state *state = sha2_get_state(module);
Py_VISIT(state->sha224_type);
Py_VISIT(state->sha256_type);
Py_VISIT(state->sha384_type);
Py_VISIT(state->sha512_type);
return 0;
}
static int
_sha2_clear(PyObject *module)
{
sha2_state *state = sha2_get_state(module);
Py_CLEAR(state->sha224_type);
Py_CLEAR(state->sha256_type);
Py_CLEAR(state->sha384_type);
Py_CLEAR(state->sha512_type);
return 0;
}
static void
_sha2_free(void *module)
{
_sha2_clear((PyObject *)module);
}
/* Initialize this module. */
static int sha2_exec(PyObject *module)
{
sha2_state *state = sha2_get_state(module);
state->sha224_type = (PyTypeObject *)PyType_FromModuleAndSpec(
module, &sha224_type_spec, NULL);
if (state->sha224_type == NULL) {
return -1;
}
state->sha256_type = (PyTypeObject *)PyType_FromModuleAndSpec(
module, &sha256_type_spec, NULL);
if (state->sha256_type == NULL) {
return -1;
}
state->sha384_type = (PyTypeObject *)PyType_FromModuleAndSpec(
module, &sha384_type_spec, NULL);
if (state->sha384_type == NULL) {
return -1;
}
state->sha512_type = (PyTypeObject *)PyType_FromModuleAndSpec(
module, &sha512_type_spec, NULL);
if (state->sha512_type == NULL) {
return -1;
}
if (PyModule_AddType(module, state->sha224_type) < 0) {
return -1;
}
if (PyModule_AddType(module, state->sha256_type) < 0) {
return -1;
}
if (PyModule_AddType(module, state->sha384_type) < 0) {
return -1;
}
if (PyModule_AddType(module, state->sha512_type) < 0) {
return -1;
}
return 0;
}
static PyModuleDef_Slot _sha2_slots[] = {
{Py_mod_exec, sha2_exec},
{0, NULL}
};
static struct PyModuleDef _sha2module = {
PyModuleDef_HEAD_INIT,
.m_name = "_sha2",
.m_size = sizeof(sha2_state),
.m_methods = SHA2_functions,
.m_slots = _sha2_slots,
.m_traverse = _sha2_traverse,
.m_clear = _sha2_clear,
.m_free = _sha2_free
};
PyMODINIT_FUNC
PyInit__sha2(void)
{
return PyModuleDef_Init(&_sha2module);
}
|