ยปCore Development>Code coverage>Modules/_sha3/sha3module.c

Python code coverage for Modules/_sha3/sha3module.c

#countcontent
1n/a/* SHA3 module
2n/a *
3n/a * This module provides an interface to the SHA3 algorithm
4n/a *
5n/a * See below for information about the original code this module was
6n/a * based upon. Additional work performed by:
7n/a *
8n/a * Andrew Kuchling (amk@amk.ca)
9n/a * Greg Stein (gstein@lyra.org)
10n/a * Trevor Perrin (trevp@trevp.net)
11n/a * Gregory P. Smith (greg@krypto.org)
12n/a *
13n/a * Copyright (C) 2012-2016 Christian Heimes (christian@python.org)
14n/a * Licensed to PSF under a Contributor Agreement.
15n/a *
16n/a */
17n/a
18n/a#include "Python.h"
19n/a#include "pystrhex.h"
20n/a#include "../hashlib.h"
21n/a
22n/a/* **************************************************************************
23n/a * SHA-3 (Keccak) and SHAKE
24n/a *
25n/a * The code is based on KeccakCodePackage from 2016-04-23
26n/a * commit 647f93079afc4ada3d23737477a6e52511ca41fd
27n/a *
28n/a * The reference implementation is altered in this points:
29n/a * - C++ comments are converted to ANSI C comments.
30n/a * - all function names are mangled
31n/a * - typedef for UINT64 is commented out.
32n/a * - brg_endian.h is removed
33n/a *
34n/a * *************************************************************************/
35n/a
36n/a#ifdef __sparc
37n/a /* opt64 uses un-aligned memory access that causes a BUS error with msg
38n/a * 'invalid address alignment' on SPARC. */
39n/a #define KeccakOpt 32
40n/a#elif PY_BIG_ENDIAN
41n/a /* opt64 is not yet supported on big endian platforms */
42n/a #define KeccakOpt 32
43n/a#elif SIZEOF_VOID_P == 8 && defined(PY_UINT64_T)
44n/a /* opt64 works only on little-endian 64bit platforms with unsigned int64 */
45n/a #define KeccakOpt 64
46n/a#else
47n/a /* opt32 is used for the remaining 32 and 64bit platforms */
48n/a #define KeccakOpt 32
49n/a#endif
50n/a
51n/a#if KeccakOpt == 64 && defined(PY_UINT64_T)
52n/a /* 64bit platforms with unsigned int64 */
53n/a typedef PY_UINT64_T UINT64;
54n/a typedef unsigned char UINT8;
55n/a#endif
56n/a
57n/a/* replacement for brg_endian.h */
58n/a#define IS_LITTLE_ENDIAN 1234
59n/a#define IS_BIG_ENDIAN 4321
60n/a#if PY_LITTLE_ENDIAN
61n/a#define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
62n/a#endif
63n/a#if PY_BIG_ENDIAN
64n/a#define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
65n/a#endif
66n/a
67n/a/* mangle names */
68n/a#define KeccakF1600_FastLoop_Absorb _PySHA3_KeccakF1600_FastLoop_Absorb
69n/a#define Keccak_HashFinal _PySHA3_Keccak_HashFinal
70n/a#define Keccak_HashInitialize _PySHA3_Keccak_HashInitialize
71n/a#define Keccak_HashSqueeze _PySHA3_Keccak_HashSqueeze
72n/a#define Keccak_HashUpdate _PySHA3_Keccak_HashUpdate
73n/a#define KeccakP1600_AddBytes _PySHA3_KeccakP1600_AddBytes
74n/a#define KeccakP1600_AddBytesInLane _PySHA3_KeccakP1600_AddBytesInLane
75n/a#define KeccakP1600_AddLanes _PySHA3_KeccakP1600_AddLanes
76n/a#define KeccakP1600_ExtractAndAddBytes _PySHA3_KeccakP1600_ExtractAndAddBytes
77n/a#define KeccakP1600_ExtractAndAddBytesInLane _PySHA3_KeccakP1600_ExtractAndAddBytesInLane
78n/a#define KeccakP1600_ExtractAndAddLanes _PySHA3_KeccakP1600_ExtractAndAddLanes
79n/a#define KeccakP1600_ExtractBytes _PySHA3_KeccakP1600_ExtractBytes
80n/a#define KeccakP1600_ExtractBytesInLane _PySHA3_KeccakP1600_ExtractBytesInLane
81n/a#define KeccakP1600_ExtractLanes _PySHA3_KeccakP1600_ExtractLanes
82n/a#define KeccakP1600_Initialize _PySHA3_KeccakP1600_Initialize
83n/a#define KeccakP1600_OverwriteBytes _PySHA3_KeccakP1600_OverwriteBytes
84n/a#define KeccakP1600_OverwriteBytesInLane _PySHA3_KeccakP1600_OverwriteBytesInLane
85n/a#define KeccakP1600_OverwriteLanes _PySHA3_KeccakP1600_OverwriteLanes
86n/a#define KeccakP1600_OverwriteWithZeroes _PySHA3_KeccakP1600_OverwriteWithZeroes
87n/a#define KeccakP1600_Permute_12rounds _PySHA3_KeccakP1600_Permute_12rounds
88n/a#define KeccakP1600_Permute_24rounds _PySHA3_KeccakP1600_Permute_24rounds
89n/a#define KeccakWidth1600_Sponge _PySHA3_KeccakWidth1600_Sponge
90n/a#define KeccakWidth1600_SpongeAbsorb _PySHA3_KeccakWidth1600_SpongeAbsorb
91n/a#define KeccakWidth1600_SpongeAbsorbLastFewBits _PySHA3_KeccakWidth1600_SpongeAbsorbLastFewBits
92n/a#define KeccakWidth1600_SpongeInitialize _PySHA3_KeccakWidth1600_SpongeInitialize
93n/a#define KeccakWidth1600_SpongeSqueeze _PySHA3_KeccakWidth1600_SpongeSqueeze
94n/a#if KeccakOpt == 32
95n/a#define KeccakP1600_AddByte _PySHA3_KeccakP1600_AddByte
96n/a#define KeccakP1600_Permute_Nrounds _PySHA3_KeccakP1600_Permute_Nrounds
97n/a#define KeccakP1600_SetBytesInLaneToZero _PySHA3_KeccakP1600_SetBytesInLaneToZero
98n/a#endif
99n/a
100n/a/* we are only interested in KeccakP1600 */
101n/a#define KeccakP200_excluded 1
102n/a#define KeccakP400_excluded 1
103n/a#define KeccakP800_excluded 1
104n/a
105n/a/* inline all Keccak dependencies */
106n/a#include "kcp/KeccakHash.h"
107n/a#include "kcp/KeccakSponge.h"
108n/a#include "kcp/KeccakHash.c"
109n/a#include "kcp/KeccakSponge.c"
110n/a#if KeccakOpt == 64
111n/a #include "kcp/KeccakP-1600-opt64.c"
112n/a#elif KeccakOpt == 32
113n/a #include "kcp/KeccakP-1600-inplace32BI.c"
114n/a#endif
115n/a
116n/a#define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */
117n/a#define SHA3_LANESIZE (20 * 8) /* ExtractLane needs max uint64_t[20] extra. */
118n/a#define SHA3_state Keccak_HashInstance
119n/a#define SHA3_init Keccak_HashInitialize
120n/a#define SHA3_process Keccak_HashUpdate
121n/a#define SHA3_done Keccak_HashFinal
122n/a#define SHA3_squeeze Keccak_HashSqueeze
123n/a#define SHA3_copystate(dest, src) memcpy(&(dest), &(src), sizeof(SHA3_state))
124n/a
125n/a
126n/a/*[clinic input]
127n/amodule _sha3
128n/aclass _sha3.sha3_224 "SHA3object *" "&SHA3_224typ"
129n/aclass _sha3.sha3_256 "SHA3object *" "&SHA3_256typ"
130n/aclass _sha3.sha3_384 "SHA3object *" "&SHA3_384typ"
131n/aclass _sha3.sha3_512 "SHA3object *" "&SHA3_512typ"
132n/aclass _sha3.shake_128 "SHA3object *" "&SHAKE128type"
133n/aclass _sha3.shake_256 "SHA3object *" "&SHAKE256type"
134n/a[clinic start generated code]*/
135n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/
136n/a
137n/a/* The structure for storing SHA3 info */
138n/a
139n/atypedef struct {
140n/a PyObject_HEAD
141n/a SHA3_state hash_state;
142n/a#ifdef WITH_THREAD
143n/a PyThread_type_lock lock;
144n/a#endif
145n/a} SHA3object;
146n/a
147n/astatic PyTypeObject SHA3_224type;
148n/astatic PyTypeObject SHA3_256type;
149n/astatic PyTypeObject SHA3_384type;
150n/astatic PyTypeObject SHA3_512type;
151n/a#ifdef PY_WITH_KECCAK
152n/astatic PyTypeObject Keccak_224type;
153n/astatic PyTypeObject Keccak_256type;
154n/astatic PyTypeObject Keccak_384type;
155n/astatic PyTypeObject Keccak_512type;
156n/a#endif
157n/astatic PyTypeObject SHAKE128type;
158n/astatic PyTypeObject SHAKE256type;
159n/a
160n/a#include "clinic/sha3module.c.h"
161n/a
162n/astatic SHA3object *
163n/anewSHA3object(PyTypeObject *type)
164n/a{
165n/a SHA3object *newobj;
166n/a newobj = (SHA3object *)PyObject_New(SHA3object, type);
167n/a if (newobj == NULL) {
168n/a return NULL;
169n/a }
170n/a#ifdef WITH_THREAD
171n/a newobj->lock = NULL;
172n/a#endif
173n/a return newobj;
174n/a}
175n/a
176n/a
177n/a/*[clinic input]
178n/a@classmethod
179n/a_sha3.sha3_224.__new__ as py_sha3_new
180n/a string as data: object = NULL
181n/a
182n/aReturn a new SHA3 hash object with a hashbit length of 28 bytes.
183n/a[clinic start generated code]*/
184n/a
185n/astatic PyObject *
186n/apy_sha3_new_impl(PyTypeObject *type, PyObject *data)
187n/a/*[clinic end generated code: output=8d5c34279e69bf09 input=d7c582b950a858b6]*/
188n/a{
189n/a SHA3object *self = NULL;
190n/a Py_buffer buf = {NULL, NULL};
191n/a HashReturn res;
192n/a
193n/a self = newSHA3object(type);
194n/a if (self == NULL) {
195n/a goto error;
196n/a }
197n/a
198n/a if (type == &SHA3_224type) {
199n/a res = Keccak_HashInitialize_SHA3_224(&self->hash_state);
200n/a } else if (type == &SHA3_256type) {
201n/a res = Keccak_HashInitialize_SHA3_256(&self->hash_state);
202n/a } else if (type == &SHA3_384type) {
203n/a res = Keccak_HashInitialize_SHA3_384(&self->hash_state);
204n/a } else if (type == &SHA3_512type) {
205n/a res = Keccak_HashInitialize_SHA3_512(&self->hash_state);
206n/a#ifdef PY_WITH_KECCAK
207n/a } else if (type == &Keccak_224type) {
208n/a res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01);
209n/a } else if (type == &Keccak_256type) {
210n/a res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01);
211n/a } else if (type == &Keccak_384type) {
212n/a res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01);
213n/a } else if (type == &Keccak_512type) {
214n/a res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01);
215n/a#endif
216n/a } else if (type == &SHAKE128type) {
217n/a res = Keccak_HashInitialize_SHAKE128(&self->hash_state);
218n/a } else if (type == &SHAKE256type) {
219n/a res = Keccak_HashInitialize_SHAKE256(&self->hash_state);
220n/a } else {
221n/a PyErr_BadInternalCall();
222n/a goto error;
223n/a }
224n/a
225n/a if (data) {
226n/a GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
227n/a#ifdef WITH_THREAD
228n/a if (buf.len >= HASHLIB_GIL_MINSIZE) {
229n/a /* invariant: New objects can't be accessed by other code yet,
230n/a * thus it's safe to release the GIL without locking the object.
231n/a */
232n/a Py_BEGIN_ALLOW_THREADS
233n/a res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
234n/a Py_END_ALLOW_THREADS
235n/a }
236n/a else {
237n/a res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
238n/a }
239n/a#else
240n/a res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
241n/a#endif
242n/a if (res != SUCCESS) {
243n/a PyErr_SetString(PyExc_RuntimeError,
244n/a "internal error in SHA3 Update()");
245n/a goto error;
246n/a }
247n/a PyBuffer_Release(&buf);
248n/a }
249n/a
250n/a return (PyObject *)self;
251n/a
252n/a error:
253n/a if (self) {
254n/a Py_DECREF(self);
255n/a }
256n/a if (data && buf.obj) {
257n/a PyBuffer_Release(&buf);
258n/a }
259n/a return NULL;
260n/a}
261n/a
262n/a
263n/a/* Internal methods for a hash object */
264n/a
265n/astatic void
266n/aSHA3_dealloc(SHA3object *self)
267n/a{
268n/a#ifdef WITH_THREAD
269n/a if (self->lock) {
270n/a PyThread_free_lock(self->lock);
271n/a }
272n/a#endif
273n/a PyObject_Del(self);
274n/a}
275n/a
276n/a
277n/a/* External methods for a hash object */
278n/a
279n/a
280n/a/*[clinic input]
281n/a_sha3.sha3_224.copy
282n/a
283n/aReturn a copy of the hash object.
284n/a[clinic start generated code]*/
285n/a
286n/astatic PyObject *
287n/a_sha3_sha3_224_copy_impl(SHA3object *self)
288n/a/*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/
289n/a{
290n/a SHA3object *newobj;
291n/a
292n/a if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
293n/a return NULL;
294n/a }
295n/a ENTER_HASHLIB(self);
296n/a SHA3_copystate(newobj->hash_state, self->hash_state);
297n/a LEAVE_HASHLIB(self);
298n/a return (PyObject *)newobj;
299n/a}
300n/a
301n/a
302n/a/*[clinic input]
303n/a_sha3.sha3_224.digest
304n/a
305n/aReturn the digest value as a string of binary data.
306n/a[clinic start generated code]*/
307n/a
308n/astatic PyObject *
309n/a_sha3_sha3_224_digest_impl(SHA3object *self)
310n/a/*[clinic end generated code: output=fd531842e20b2d5b input=a5807917d219b30e]*/
311n/a{
312n/a unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE];
313n/a SHA3_state temp;
314n/a HashReturn res;
315n/a
316n/a ENTER_HASHLIB(self);
317n/a SHA3_copystate(temp, self->hash_state);
318n/a LEAVE_HASHLIB(self);
319n/a res = SHA3_done(&temp, digest);
320n/a if (res != SUCCESS) {
321n/a PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
322n/a return NULL;
323n/a }
324n/a return PyBytes_FromStringAndSize((const char *)digest,
325n/a self->hash_state.fixedOutputLength / 8);
326n/a}
327n/a
328n/a
329n/a/*[clinic input]
330n/a_sha3.sha3_224.hexdigest
331n/a
332n/aReturn the digest value as a string of hexadecimal digits.
333n/a[clinic start generated code]*/
334n/a
335n/astatic PyObject *
336n/a_sha3_sha3_224_hexdigest_impl(SHA3object *self)
337n/a/*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/
338n/a{
339n/a unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE];
340n/a SHA3_state temp;
341n/a HashReturn res;
342n/a
343n/a /* Get the raw (binary) digest value */
344n/a ENTER_HASHLIB(self);
345n/a SHA3_copystate(temp, self->hash_state);
346n/a LEAVE_HASHLIB(self);
347n/a res = SHA3_done(&temp, digest);
348n/a if (res != SUCCESS) {
349n/a PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
350n/a return NULL;
351n/a }
352n/a return _Py_strhex((const char *)digest,
353n/a self->hash_state.fixedOutputLength / 8);
354n/a}
355n/a
356n/a
357n/a/*[clinic input]
358n/a_sha3.sha3_224.update
359n/a
360n/a obj: object
361n/a /
362n/a
363n/aUpdate this hash object's state with the provided string.
364n/a[clinic start generated code]*/
365n/a
366n/astatic PyObject *
367n/a_sha3_sha3_224_update(SHA3object *self, PyObject *obj)
368n/a/*[clinic end generated code: output=06721d55b483e0af input=be44bf0d1c279791]*/
369n/a{
370n/a Py_buffer buf;
371n/a HashReturn res;
372n/a
373n/a GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
374n/a
375n/a /* add new data, the function takes the length in bits not bytes */
376n/a#ifdef WITH_THREAD
377n/a if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
378n/a self->lock = PyThread_allocate_lock();
379n/a }
380n/a /* Once a lock exists all code paths must be synchronized. We have to
381n/a * release the GIL even for small buffers as acquiring the lock may take
382n/a * an unlimited amount of time when another thread updates this object
383n/a * with lots of data. */
384n/a if (self->lock) {
385n/a Py_BEGIN_ALLOW_THREADS
386n/a PyThread_acquire_lock(self->lock, 1);
387n/a res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
388n/a PyThread_release_lock(self->lock);
389n/a Py_END_ALLOW_THREADS
390n/a }
391n/a else {
392n/a res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
393n/a }
394n/a#else
395n/a res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
396n/a#endif
397n/a
398n/a if (res != SUCCESS) {
399n/a PyBuffer_Release(&buf);
400n/a PyErr_SetString(PyExc_RuntimeError,
401n/a "internal error in SHA3 Update()");
402n/a return NULL;
403n/a }
404n/a
405n/a PyBuffer_Release(&buf);
406n/a Py_RETURN_NONE;
407n/a}
408n/a
409n/a
410n/astatic PyMethodDef SHA3_methods[] = {
411n/a _SHA3_SHA3_224_COPY_METHODDEF
412n/a _SHA3_SHA3_224_DIGEST_METHODDEF
413n/a _SHA3_SHA3_224_HEXDIGEST_METHODDEF
414n/a _SHA3_SHA3_224_UPDATE_METHODDEF
415n/a {NULL, NULL} /* sentinel */
416n/a};
417n/a
418n/a
419n/astatic PyObject *
420n/aSHA3_get_block_size(SHA3object *self, void *closure)
421n/a{
422n/a int rate = self->hash_state.sponge.rate;
423n/a return PyLong_FromLong(rate / 8);
424n/a}
425n/a
426n/a
427n/astatic PyObject *
428n/aSHA3_get_name(SHA3object *self, void *closure)
429n/a{
430n/a PyTypeObject *type = Py_TYPE(self);
431n/a if (type == &SHA3_224type) {
432n/a return PyUnicode_FromString("sha3_224");
433n/a } else if (type == &SHA3_256type) {
434n/a return PyUnicode_FromString("sha3_256");
435n/a } else if (type == &SHA3_384type) {
436n/a return PyUnicode_FromString("sha3_384");
437n/a } else if (type == &SHA3_512type) {
438n/a return PyUnicode_FromString("sha3_512");
439n/a#ifdef PY_WITH_KECCAK
440n/a } else if (type == &Keccak_224type) {
441n/a return PyUnicode_FromString("keccak_224");
442n/a } else if (type == &Keccak_256type) {
443n/a return PyUnicode_FromString("keccak_256");
444n/a } else if (type == &Keccak_384type) {
445n/a return PyUnicode_FromString("keccak_384");
446n/a } else if (type == &Keccak_512type) {
447n/a return PyUnicode_FromString("keccak_512");
448n/a#endif
449n/a } else if (type == &SHAKE128type) {
450n/a return PyUnicode_FromString("shake_128");
451n/a } else if (type == &SHAKE256type) {
452n/a return PyUnicode_FromString("shake_256");
453n/a } else {
454n/a PyErr_BadInternalCall();
455n/a return NULL;
456n/a }
457n/a}
458n/a
459n/a
460n/astatic PyObject *
461n/aSHA3_get_digest_size(SHA3object *self, void *closure)
462n/a{
463n/a return PyLong_FromLong(self->hash_state.fixedOutputLength / 8);
464n/a}
465n/a
466n/a
467n/astatic PyObject *
468n/aSHA3_get_capacity_bits(SHA3object *self, void *closure)
469n/a{
470n/a int capacity = 1600 - self->hash_state.sponge.rate;
471n/a return PyLong_FromLong(capacity);
472n/a}
473n/a
474n/a
475n/astatic PyObject *
476n/aSHA3_get_rate_bits(SHA3object *self, void *closure)
477n/a{
478n/a unsigned int rate = self->hash_state.sponge.rate;
479n/a return PyLong_FromLong(rate);
480n/a}
481n/a
482n/astatic PyObject *
483n/aSHA3_get_suffix(SHA3object *self, void *closure)
484n/a{
485n/a unsigned char suffix[2];
486n/a suffix[0] = self->hash_state.delimitedSuffix;
487n/a suffix[1] = 0;
488n/a return PyBytes_FromStringAndSize((const char *)suffix, 1);
489n/a}
490n/a
491n/a
492n/astatic PyGetSetDef SHA3_getseters[] = {
493n/a {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
494n/a {"name", (getter)SHA3_get_name, NULL, NULL, NULL},
495n/a {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL},
496n/a {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL},
497n/a {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL},
498n/a {"_suffix", (getter)SHA3_get_suffix, NULL, NULL, NULL},
499n/a {NULL} /* Sentinel */
500n/a};
501n/a
502n/a
503n/a#define SHA3_TYPE(type_obj, type_name, type_doc, type_methods) \
504n/a static PyTypeObject type_obj = { \
505n/a PyVarObject_HEAD_INIT(NULL, 0) \
506n/a type_name, /* tp_name */ \
507n/a sizeof(SHA3object), /* tp_size */ \
508n/a 0, /* tp_itemsize */ \
509n/a /* methods */ \
510n/a (destructor)SHA3_dealloc, /* tp_dealloc */ \
511n/a 0, /* tp_print */ \
512n/a 0, /* tp_getattr */ \
513n/a 0, /* tp_setattr */ \
514n/a 0, /* tp_reserved */ \
515n/a 0, /* tp_repr */ \
516n/a 0, /* tp_as_number */ \
517n/a 0, /* tp_as_sequence */ \
518n/a 0, /* tp_as_mapping */ \
519n/a 0, /* tp_hash */ \
520n/a 0, /* tp_call */ \
521n/a 0, /* tp_str */ \
522n/a 0, /* tp_getattro */ \
523n/a 0, /* tp_setattro */ \
524n/a 0, /* tp_as_buffer */ \
525n/a Py_TPFLAGS_DEFAULT, /* tp_flags */ \
526n/a type_doc, /* tp_doc */ \
527n/a 0, /* tp_traverse */ \
528n/a 0, /* tp_clear */ \
529n/a 0, /* tp_richcompare */ \
530n/a 0, /* tp_weaklistoffset */ \
531n/a 0, /* tp_iter */ \
532n/a 0, /* tp_iternext */ \
533n/a type_methods, /* tp_methods */ \
534n/a NULL, /* tp_members */ \
535n/a SHA3_getseters, /* tp_getset */ \
536n/a 0, /* tp_base */ \
537n/a 0, /* tp_dict */ \
538n/a 0, /* tp_descr_get */ \
539n/a 0, /* tp_descr_set */ \
540n/a 0, /* tp_dictoffset */ \
541n/a 0, /* tp_init */ \
542n/a 0, /* tp_alloc */ \
543n/a py_sha3_new, /* tp_new */ \
544n/a }
545n/a
546n/aPyDoc_STRVAR(sha3_256__doc__,
547n/a"sha3_256([string]) -> SHA3 object\n\
548n/a\n\
549n/aReturn a new SHA3 hash object with a hashbit length of 32 bytes.");
550n/a
551n/aPyDoc_STRVAR(sha3_384__doc__,
552n/a"sha3_384([string]) -> SHA3 object\n\
553n/a\n\
554n/aReturn a new SHA3 hash object with a hashbit length of 48 bytes.");
555n/a
556n/aPyDoc_STRVAR(sha3_512__doc__,
557n/a"sha3_512([string]) -> SHA3 object\n\
558n/a\n\
559n/aReturn a new SHA3 hash object with a hashbit length of 64 bytes.");
560n/a
561n/aSHA3_TYPE(SHA3_224type, "_sha3.sha3_224", py_sha3_new__doc__, SHA3_methods);
562n/aSHA3_TYPE(SHA3_256type, "_sha3.sha3_256", sha3_256__doc__, SHA3_methods);
563n/aSHA3_TYPE(SHA3_384type, "_sha3.sha3_384", sha3_384__doc__, SHA3_methods);
564n/aSHA3_TYPE(SHA3_512type, "_sha3.sha3_512", sha3_512__doc__, SHA3_methods);
565n/a
566n/a#ifdef PY_WITH_KECCAK
567n/aPyDoc_STRVAR(keccak_224__doc__,
568n/a"keccak_224([string]) -> Keccak object\n\
569n/a\n\
570n/aReturn a new Keccak hash object with a hashbit length of 28 bytes.");
571n/a
572n/aPyDoc_STRVAR(keccak_256__doc__,
573n/a"keccak_256([string]) -> Keccak object\n\
574n/a\n\
575n/aReturn a new Keccak hash object with a hashbit length of 32 bytes.");
576n/a
577n/aPyDoc_STRVAR(keccak_384__doc__,
578n/a"keccak_384([string]) -> Keccak object\n\
579n/a\n\
580n/aReturn a new Keccak hash object with a hashbit length of 48 bytes.");
581n/a
582n/aPyDoc_STRVAR(keccak_512__doc__,
583n/a"keccak_512([string]) -> Keccak object\n\
584n/a\n\
585n/aReturn a new Keccak hash object with a hashbit length of 64 bytes.");
586n/a
587n/aSHA3_TYPE(Keccak_224type, "_sha3.keccak_224", keccak_224__doc__, SHA3_methods);
588n/aSHA3_TYPE(Keccak_256type, "_sha3.keccak_256", keccak_256__doc__, SHA3_methods);
589n/aSHA3_TYPE(Keccak_384type, "_sha3.keccak_384", keccak_384__doc__, SHA3_methods);
590n/aSHA3_TYPE(Keccak_512type, "_sha3.keccak_512", keccak_512__doc__, SHA3_methods);
591n/a#endif
592n/a
593n/a
594n/astatic PyObject *
595n/a_SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
596n/a{
597n/a unsigned char *digest = NULL;
598n/a SHA3_state temp;
599n/a int res;
600n/a PyObject *result = NULL;
601n/a
602n/a /* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and
603n/a * SHA3_LANESIZE extra space.
604n/a */
605n/a digest = (unsigned char*)PyMem_Malloc(digestlen + SHA3_LANESIZE);
606n/a if (digest == NULL) {
607n/a return PyErr_NoMemory();
608n/a }
609n/a
610n/a /* Get the raw (binary) digest value */
611n/a ENTER_HASHLIB(self);
612n/a SHA3_copystate(temp, self->hash_state);
613n/a LEAVE_HASHLIB(self);
614n/a res = SHA3_done(&temp, NULL);
615n/a if (res != SUCCESS) {
616n/a PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 done()");
617n/a goto error;
618n/a }
619n/a res = SHA3_squeeze(&temp, digest, digestlen * 8);
620n/a if (res != SUCCESS) {
621n/a PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Squeeze()");
622n/a return NULL;
623n/a }
624n/a if (hex) {
625n/a result = _Py_strhex((const char *)digest, digestlen);
626n/a } else {
627n/a result = PyBytes_FromStringAndSize((const char *)digest,
628n/a digestlen);
629n/a }
630n/a error:
631n/a if (digest != NULL) {
632n/a PyMem_Free(digest);
633n/a }
634n/a return result;
635n/a}
636n/a
637n/a
638n/a/*[clinic input]
639n/a_sha3.shake_128.digest
640n/a
641n/a length: unsigned_long(bitwise=True)
642n/a \
643n/a
644n/aReturn the digest value as a string of binary data.
645n/a[clinic start generated code]*/
646n/a
647n/astatic PyObject *
648n/a_sha3_shake_128_digest_impl(SHA3object *self, unsigned long length)
649n/a/*[clinic end generated code: output=2313605e2f87bb8f input=608c8ca80ae9d115]*/
650n/a{
651n/a return _SHAKE_digest(self, length, 0);
652n/a}
653n/a
654n/a
655n/a/*[clinic input]
656n/a_sha3.shake_128.hexdigest
657n/a
658n/a length: unsigned_long(bitwise=True)
659n/a \
660n/a
661n/aReturn the digest value as a string of hexadecimal digits.
662n/a[clinic start generated code]*/
663n/a
664n/astatic PyObject *
665n/a_sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length)
666n/a/*[clinic end generated code: output=bf8e2f1e490944a8 input=64e56b4760db4573]*/
667n/a{
668n/a return _SHAKE_digest(self, length, 1);
669n/a}
670n/a
671n/a
672n/astatic PyMethodDef SHAKE_methods[] = {
673n/a _SHA3_SHA3_224_COPY_METHODDEF
674n/a _SHA3_SHAKE_128_DIGEST_METHODDEF
675n/a _SHA3_SHAKE_128_HEXDIGEST_METHODDEF
676n/a _SHA3_SHA3_224_UPDATE_METHODDEF
677n/a {NULL, NULL} /* sentinel */
678n/a};
679n/a
680n/aPyDoc_STRVAR(shake_128__doc__,
681n/a"shake_128([string]) -> SHAKE object\n\
682n/a\n\
683n/aReturn a new SHAKE hash object.");
684n/a
685n/aPyDoc_STRVAR(shake_256__doc__,
686n/a"shake_256([string]) -> SHAKE object\n\
687n/a\n\
688n/aReturn a new SHAKE hash object.");
689n/a
690n/aSHA3_TYPE(SHAKE128type, "_sha3.shake_128", shake_128__doc__, SHAKE_methods);
691n/aSHA3_TYPE(SHAKE256type, "_sha3.shake_256", shake_256__doc__, SHAKE_methods);
692n/a
693n/a
694n/a/* Initialize this module. */
695n/astatic struct PyModuleDef _SHA3module = {
696n/a PyModuleDef_HEAD_INIT,
697n/a "_sha3",
698n/a NULL,
699n/a -1,
700n/a NULL,
701n/a NULL,
702n/a NULL,
703n/a NULL,
704n/a NULL
705n/a};
706n/a
707n/a
708n/aPyMODINIT_FUNC
709n/aPyInit__sha3(void)
710n/a{
711n/a PyObject *m = NULL;
712n/a
713n/a if ((m = PyModule_Create(&_SHA3module)) == NULL) {
714n/a return NULL;
715n/a }
716n/a
717n/a#define init_sha3type(name, type) \
718n/a do { \
719n/a Py_TYPE(type) = &PyType_Type; \
720n/a if (PyType_Ready(type) < 0) { \
721n/a goto error; \
722n/a } \
723n/a Py_INCREF((PyObject *)type); \
724n/a if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \
725n/a goto error; \
726n/a } \
727n/a } while(0)
728n/a
729n/a init_sha3type("sha3_224", &SHA3_224type);
730n/a init_sha3type("sha3_256", &SHA3_256type);
731n/a init_sha3type("sha3_384", &SHA3_384type);
732n/a init_sha3type("sha3_512", &SHA3_512type);
733n/a#ifdef PY_WITH_KECCAK
734n/a init_sha3type("keccak_224", &Keccak_224type);
735n/a init_sha3type("keccak_256", &Keccak_256type);
736n/a init_sha3type("keccak_384", &Keccak_384type);
737n/a init_sha3type("keccak_512", &Keccak_512type);
738n/a#endif
739n/a init_sha3type("shake_128", &SHAKE128type);
740n/a init_sha3type("shake_256", &SHAKE256type);
741n/a
742n/a#undef init_sha3type
743n/a
744n/a if (PyModule_AddIntConstant(m, "keccakopt", KeccakOpt) < 0) {
745n/a goto error;
746n/a }
747n/a if (PyModule_AddStringConstant(m, "implementation",
748n/a KeccakP1600_implementation) < 0) {
749n/a goto error;
750n/a }
751n/a
752n/a return m;
753n/a error:
754n/a Py_DECREF(m);
755n/a return NULL;
756n/a}