Branch data Line data Source code
1 : : /* SHA3 module
2 : : *
3 : : * This module provides an interface to the SHA3 algorithm
4 : : *
5 : : * See below for information about the original code this module was
6 : : * based upon. Additional work performed by:
7 : : *
8 : : * Andrew Kuchling (amk@amk.ca)
9 : : * Greg Stein (gstein@lyra.org)
10 : : * Trevor Perrin (trevp@trevp.net)
11 : : * Gregory P. Smith (greg@krypto.org)
12 : : *
13 : : * Copyright (C) 2012-2022 Christian Heimes (christian@python.org)
14 : : * Licensed to PSF under a Contributor Agreement.
15 : : *
16 : : */
17 : :
18 : : #ifndef Py_BUILD_CORE_BUILTIN
19 : : # define Py_BUILD_CORE_MODULE 1
20 : : #endif
21 : :
22 : : #include "Python.h"
23 : : #include "pycore_strhex.h" // _Py_strhex()
24 : : #include "pycore_typeobject.h" // _PyType_GetModuleState()
25 : : #include "../hashlib.h"
26 : :
27 : : #include "sha3.c"
28 : :
29 : : #define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */
30 : : #define SHA3_LANESIZE 0
31 : : #define SHA3_state sha3_ctx_t
32 : : #define SHA3_init sha3_init
33 : : #define SHA3_process sha3_update
34 : : #define SHA3_done(state, digest) sha3_final(digest, state)
35 : : #define SHA3_squeeze(state, out, len) shake_xof(state), shake_out(state, out, len)
36 : : #define SHA3_copystate(dest, src) memcpy(&(dest), &(src), sizeof(SHA3_state))
37 : :
38 : : // no optimization
39 : : #define KeccakOpt 0
40 : :
41 : : typedef enum { SUCCESS = 1, FAIL = 0, BAD_HASHLEN = 2 } HashReturn;
42 : :
43 : : typedef struct {
44 : : PyTypeObject *sha3_224_type;
45 : : PyTypeObject *sha3_256_type;
46 : : PyTypeObject *sha3_384_type;
47 : : PyTypeObject *sha3_512_type;
48 : : PyTypeObject *shake_128_type;
49 : : PyTypeObject *shake_256_type;
50 : : } SHA3State;
51 : :
52 : : static inline SHA3State*
53 : 11 : sha3_get_state(PyObject *module)
54 : : {
55 : 11 : void *state = PyModule_GetState(module);
56 : : assert(state != NULL);
57 : 11 : return (SHA3State *)state;
58 : : }
59 : :
60 : : /*[clinic input]
61 : : module _sha3
62 : : class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ"
63 : : class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ"
64 : : class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ"
65 : : class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ"
66 : : class _sha3.shake_128 "SHA3object *" "&SHAKE128type"
67 : : class _sha3.shake_256 "SHA3object *" "&SHAKE256type"
68 : : [clinic start generated code]*/
69 : : /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/
70 : :
71 : : /* The structure for storing SHA3 info */
72 : :
73 : : typedef struct {
74 : : PyObject_HEAD
75 : : SHA3_state hash_state;
76 : : PyThread_type_lock lock;
77 : : } SHA3object;
78 : :
79 : : #include "clinic/sha3module.c.h"
80 : :
81 : : static SHA3object *
82 : 0 : newSHA3object(PyTypeObject *type)
83 : : {
84 : : SHA3object *newobj;
85 : 0 : newobj = (SHA3object *)PyObject_New(SHA3object, type);
86 [ # # ]: 0 : if (newobj == NULL) {
87 : 0 : return NULL;
88 : : }
89 : 0 : newobj->lock = NULL;
90 : 0 : return newobj;
91 : : }
92 : :
93 : : /*[clinic input]
94 : : @classmethod
95 : : _sha3.sha3_224.__new__ as py_sha3_new
96 : : data: object(c_default="NULL") = b''
97 : : /
98 : : *
99 : : usedforsecurity: bool = True
100 : :
101 : : Return a new BLAKE2b hash object.
102 : : [clinic start generated code]*/
103 : :
104 : : static PyObject *
105 : 0 : py_sha3_new_impl(PyTypeObject *type, PyObject *data, int usedforsecurity)
106 : : /*[clinic end generated code: output=90409addc5d5e8b0 input=bcfcdf2e4368347a]*/
107 : : {
108 : : HashReturn res;
109 : 0 : Py_buffer buf = {NULL, NULL};
110 : 0 : SHA3State *state = _PyType_GetModuleState(type);
111 : 0 : SHA3object *self = newSHA3object(type);
112 [ # # ]: 0 : if (self == NULL) {
113 : 0 : goto error;
114 : : }
115 : :
116 : : assert(state != NULL);
117 : :
118 [ # # ]: 0 : if (type == state->sha3_224_type) {
119 : 0 : res = sha3_init(&self->hash_state, 28);
120 [ # # ]: 0 : } else if (type == state->sha3_256_type) {
121 : 0 : res = sha3_init(&self->hash_state, 32);
122 [ # # ]: 0 : } else if (type == state->sha3_384_type) {
123 : 0 : res = sha3_init(&self->hash_state, 48);
124 [ # # ]: 0 : } else if (type == state->sha3_512_type) {
125 : 0 : res = sha3_init(&self->hash_state, 64);
126 [ # # ]: 0 : } else if (type == state->shake_128_type) {
127 : 0 : res = sha3_init(&self->hash_state, 16);
128 [ # # ]: 0 : } else if (type == state->shake_256_type) {
129 : 0 : res = sha3_init(&self->hash_state, 32);
130 : : } else {
131 : 0 : PyErr_BadInternalCall();
132 : 0 : goto error;
133 : : }
134 : :
135 [ # # ]: 0 : if (res != SUCCESS) {
136 : 0 : PyErr_SetString(PyExc_RuntimeError,
137 : : "internal error in SHA3 initialize()");
138 : 0 : goto error;
139 : : }
140 : :
141 [ # # ]: 0 : if (data) {
142 [ # # # # : 0 : GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
# # # # ]
143 [ # # ]: 0 : if (buf.len >= HASHLIB_GIL_MINSIZE) {
144 : : /* invariant: New objects can't be accessed by other code yet,
145 : : * thus it's safe to release the GIL without locking the object.
146 : : */
147 : 0 : Py_BEGIN_ALLOW_THREADS
148 : 0 : res = SHA3_process(&self->hash_state, buf.buf, buf.len);
149 : 0 : Py_END_ALLOW_THREADS
150 : : }
151 : : else {
152 : 0 : res = SHA3_process(&self->hash_state, buf.buf, buf.len);
153 : : }
154 [ # # ]: 0 : if (res != SUCCESS) {
155 : 0 : PyErr_SetString(PyExc_RuntimeError,
156 : : "internal error in SHA3 Update()");
157 : 0 : goto error;
158 : : }
159 : 0 : PyBuffer_Release(&buf);
160 : : }
161 : :
162 : 0 : return (PyObject *)self;
163 : :
164 : 0 : error:
165 [ # # ]: 0 : if (self) {
166 : 0 : Py_DECREF(self);
167 : : }
168 [ # # # # ]: 0 : if (data && buf.obj) {
169 : 0 : PyBuffer_Release(&buf);
170 : : }
171 : 0 : return NULL;
172 : : }
173 : :
174 : :
175 : : /* Internal methods for a hash object */
176 : :
177 : : static void
178 : 0 : SHA3_dealloc(SHA3object *self)
179 : : {
180 [ # # ]: 0 : if (self->lock) {
181 : 0 : PyThread_free_lock(self->lock);
182 : : }
183 : :
184 : 0 : PyTypeObject *tp = Py_TYPE(self);
185 : 0 : PyObject_Free(self);
186 : 0 : Py_DECREF(tp);
187 : 0 : }
188 : :
189 : :
190 : : /* External methods for a hash object */
191 : :
192 : :
193 : : /*[clinic input]
194 : : _sha3.sha3_224.copy
195 : :
196 : : Return a copy of the hash object.
197 : : [clinic start generated code]*/
198 : :
199 : : static PyObject *
200 : 0 : _sha3_sha3_224_copy_impl(SHA3object *self)
201 : : /*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/
202 : : {
203 : : SHA3object *newobj;
204 : :
205 [ # # ]: 0 : if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
206 : 0 : return NULL;
207 : : }
208 [ # # # # ]: 0 : ENTER_HASHLIB(self);
209 : 0 : SHA3_copystate(newobj->hash_state, self->hash_state);
210 [ # # ]: 0 : LEAVE_HASHLIB(self);
211 : 0 : return (PyObject *)newobj;
212 : : }
213 : :
214 : :
215 : : /*[clinic input]
216 : : _sha3.sha3_224.digest
217 : :
218 : : Return the digest value as a bytes object.
219 : : [clinic start generated code]*/
220 : :
221 : : static PyObject *
222 : 0 : _sha3_sha3_224_digest_impl(SHA3object *self)
223 : : /*[clinic end generated code: output=fd531842e20b2d5b input=5b2a659536bbd248]*/
224 : : {
225 : : unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE];
226 : : SHA3_state temp;
227 : : HashReturn res;
228 : :
229 [ # # # # ]: 0 : ENTER_HASHLIB(self);
230 : 0 : SHA3_copystate(temp, self->hash_state);
231 [ # # ]: 0 : LEAVE_HASHLIB(self);
232 : 0 : res = SHA3_done(&temp, digest);
233 [ # # ]: 0 : if (res != SUCCESS) {
234 : 0 : PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
235 : 0 : return NULL;
236 : : }
237 : 0 : return PyBytes_FromStringAndSize((const char *)digest,
238 : 0 : self->hash_state.mdlen);
239 : : }
240 : :
241 : :
242 : : /*[clinic input]
243 : : _sha3.sha3_224.hexdigest
244 : :
245 : : Return the digest value as a string of hexadecimal digits.
246 : : [clinic start generated code]*/
247 : :
248 : : static PyObject *
249 : 0 : _sha3_sha3_224_hexdigest_impl(SHA3object *self)
250 : : /*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/
251 : : {
252 : : unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE];
253 : : SHA3_state temp;
254 : : HashReturn res;
255 : :
256 : : /* Get the raw (binary) digest value */
257 [ # # # # ]: 0 : ENTER_HASHLIB(self);
258 : 0 : SHA3_copystate(temp, self->hash_state);
259 [ # # ]: 0 : LEAVE_HASHLIB(self);
260 : 0 : res = SHA3_done(&temp, digest);
261 [ # # ]: 0 : if (res != SUCCESS) {
262 : 0 : PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
263 : 0 : return NULL;
264 : : }
265 : 0 : return _Py_strhex((const char *)digest,
266 : 0 : self->hash_state.mdlen);
267 : : }
268 : :
269 : :
270 : : /*[clinic input]
271 : : _sha3.sha3_224.update
272 : :
273 : : data: object
274 : : /
275 : :
276 : : Update this hash object's state with the provided bytes-like object.
277 : : [clinic start generated code]*/
278 : :
279 : : static PyObject *
280 : 0 : _sha3_sha3_224_update(SHA3object *self, PyObject *data)
281 : : /*[clinic end generated code: output=d3223352286ed357 input=a887f54dcc4ae227]*/
282 : : {
283 : : Py_buffer buf;
284 : : HashReturn res;
285 : :
286 [ # # # # : 0 : GET_BUFFER_VIEW_OR_ERROUT(data, &buf);
# # # # ]
287 : :
288 : : /* add new data, the function takes the length in bits not bytes */
289 [ # # # # ]: 0 : if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
290 : 0 : self->lock = PyThread_allocate_lock();
291 : : }
292 : : /* Once a lock exists all code paths must be synchronized. We have to
293 : : * release the GIL even for small buffers as acquiring the lock may take
294 : : * an unlimited amount of time when another thread updates this object
295 : : * with lots of data. */
296 [ # # ]: 0 : if (self->lock) {
297 : 0 : Py_BEGIN_ALLOW_THREADS
298 : 0 : PyThread_acquire_lock(self->lock, 1);
299 : 0 : res = SHA3_process(&self->hash_state, buf.buf, buf.len);
300 : 0 : PyThread_release_lock(self->lock);
301 : 0 : Py_END_ALLOW_THREADS
302 : : }
303 : : else {
304 : 0 : res = SHA3_process(&self->hash_state, buf.buf, buf.len);
305 : : }
306 : :
307 [ # # ]: 0 : if (res != SUCCESS) {
308 : 0 : PyBuffer_Release(&buf);
309 : 0 : PyErr_SetString(PyExc_RuntimeError,
310 : : "internal error in SHA3 Update()");
311 : 0 : return NULL;
312 : : }
313 : :
314 : 0 : PyBuffer_Release(&buf);
315 : 0 : Py_RETURN_NONE;
316 : : }
317 : :
318 : :
319 : : static PyMethodDef SHA3_methods[] = {
320 : : _SHA3_SHA3_224_COPY_METHODDEF
321 : : _SHA3_SHA3_224_DIGEST_METHODDEF
322 : : _SHA3_SHA3_224_HEXDIGEST_METHODDEF
323 : : _SHA3_SHA3_224_UPDATE_METHODDEF
324 : : {NULL, NULL} /* sentinel */
325 : : };
326 : :
327 : :
328 : : static PyObject *
329 : 0 : SHA3_get_block_size(SHA3object *self, void *closure)
330 : : {
331 : 0 : int rate = self->hash_state.rsiz;
332 : 0 : return PyLong_FromLong(rate);
333 : : }
334 : :
335 : :
336 : : static PyObject *
337 : 0 : SHA3_get_name(SHA3object *self, void *closure)
338 : : {
339 : 0 : PyTypeObject *type = Py_TYPE(self);
340 : :
341 : 0 : SHA3State *state = _PyType_GetModuleState(type);
342 : : assert(state != NULL);
343 : :
344 [ # # ]: 0 : if (type == state->sha3_224_type) {
345 : 0 : return PyUnicode_FromString("sha3_224");
346 [ # # ]: 0 : } else if (type == state->sha3_256_type) {
347 : 0 : return PyUnicode_FromString("sha3_256");
348 [ # # ]: 0 : } else if (type == state->sha3_384_type) {
349 : 0 : return PyUnicode_FromString("sha3_384");
350 [ # # ]: 0 : } else if (type == state->sha3_512_type) {
351 : 0 : return PyUnicode_FromString("sha3_512");
352 [ # # ]: 0 : } else if (type == state->shake_128_type) {
353 : 0 : return PyUnicode_FromString("shake_128");
354 [ # # ]: 0 : } else if (type == state->shake_256_type) {
355 : 0 : return PyUnicode_FromString("shake_256");
356 : : } else {
357 : 0 : PyErr_BadInternalCall();
358 : 0 : return NULL;
359 : : }
360 : : }
361 : :
362 : :
363 : : static PyObject *
364 : 0 : SHA3_get_digest_size(SHA3object *self, void *closure)
365 : : {
366 : 0 : return PyLong_FromLong(self->hash_state.mdlen);
367 : : }
368 : :
369 : :
370 : : static PyObject *
371 : 0 : SHA3_get_capacity_bits(SHA3object *self, void *closure)
372 : : {
373 : 0 : int capacity = 1600 - self->hash_state.rsiz * 8;
374 : 0 : return PyLong_FromLong(capacity);
375 : : }
376 : :
377 : :
378 : : static PyObject *
379 : 0 : SHA3_get_rate_bits(SHA3object *self, void *closure)
380 : : {
381 : 0 : unsigned int rate = self->hash_state.rsiz * 8;
382 : 0 : return PyLong_FromLong(rate);
383 : : }
384 : :
385 : : static PyObject *
386 : 0 : SHA3_get_suffix(SHA3object *self, void *closure)
387 : : {
388 : 0 : unsigned char suffix[2] = {0x06, 0};
389 : 0 : return PyBytes_FromStringAndSize((const char *)suffix, 1);
390 : : }
391 : :
392 : : static PyGetSetDef SHA3_getseters[] = {
393 : : {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
394 : : {"name", (getter)SHA3_get_name, NULL, NULL, NULL},
395 : : {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL},
396 : : {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL},
397 : : {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL},
398 : : {"_suffix", (getter)SHA3_get_suffix, NULL, NULL, NULL},
399 : : {NULL} /* Sentinel */
400 : : };
401 : :
402 : : #define SHA3_TYPE_SLOTS(type_slots_obj, type_doc, type_methods, type_getseters) \
403 : : static PyType_Slot type_slots_obj[] = { \
404 : : {Py_tp_dealloc, SHA3_dealloc}, \
405 : : {Py_tp_doc, (char*)type_doc}, \
406 : : {Py_tp_methods, type_methods}, \
407 : : {Py_tp_getset, type_getseters}, \
408 : : {Py_tp_new, py_sha3_new}, \
409 : : {0,0} \
410 : : }
411 : :
412 : : // Using _PyType_GetModuleState() on these types is safe since they
413 : : // cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag.
414 : : #define SHA3_TYPE_SPEC(type_spec_obj, type_name, type_slots) \
415 : : static PyType_Spec type_spec_obj = { \
416 : : .name = "_sha3." type_name, \
417 : : .basicsize = sizeof(SHA3object), \
418 : : .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, \
419 : : .slots = type_slots \
420 : : }
421 : :
422 : : PyDoc_STRVAR(sha3_224__doc__,
423 : : "sha3_224([data], *, usedforsecurity=True) -> SHA3 object\n\
424 : : \n\
425 : : Return a new SHA3 hash object with a hashbit length of 28 bytes.");
426 : :
427 : : PyDoc_STRVAR(sha3_256__doc__,
428 : : "sha3_256([data], *, usedforsecurity=True) -> SHA3 object\n\
429 : : \n\
430 : : Return a new SHA3 hash object with a hashbit length of 32 bytes.");
431 : :
432 : : PyDoc_STRVAR(sha3_384__doc__,
433 : : "sha3_384([data], *, usedforsecurity=True) -> SHA3 object\n\
434 : : \n\
435 : : Return a new SHA3 hash object with a hashbit length of 48 bytes.");
436 : :
437 : : PyDoc_STRVAR(sha3_512__doc__,
438 : : "sha3_512([data], *, usedforsecurity=True) -> SHA3 object\n\
439 : : \n\
440 : : Return a new SHA3 hash object with a hashbit length of 64 bytes.");
441 : :
442 : : SHA3_TYPE_SLOTS(sha3_224_slots, sha3_224__doc__, SHA3_methods, SHA3_getseters);
443 : : SHA3_TYPE_SPEC(sha3_224_spec, "sha3_224", sha3_224_slots);
444 : :
445 : : SHA3_TYPE_SLOTS(sha3_256_slots, sha3_256__doc__, SHA3_methods, SHA3_getseters);
446 : : SHA3_TYPE_SPEC(sha3_256_spec, "sha3_256", sha3_256_slots);
447 : :
448 : : SHA3_TYPE_SLOTS(sha3_384_slots, sha3_384__doc__, SHA3_methods, SHA3_getseters);
449 : : SHA3_TYPE_SPEC(sha3_384_spec, "sha3_384", sha3_384_slots);
450 : :
451 : : SHA3_TYPE_SLOTS(sha3_512_slots, sha3_512__doc__, SHA3_methods, SHA3_getseters);
452 : : SHA3_TYPE_SPEC(sha3_512_spec, "sha3_512", sha3_512_slots);
453 : :
454 : : static PyObject *
455 : 0 : _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
456 : : {
457 : 0 : unsigned char *digest = NULL;
458 : : SHA3_state temp;
459 : 0 : PyObject *result = NULL;
460 : :
461 [ # # ]: 0 : if (digestlen >= (1 << 29)) {
462 : 0 : PyErr_SetString(PyExc_ValueError, "length is too large");
463 : 0 : return NULL;
464 : : }
465 : : /* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and
466 : : * SHA3_LANESIZE extra space.
467 : : */
468 : 0 : digest = (unsigned char*)PyMem_Malloc(digestlen + SHA3_LANESIZE);
469 [ # # ]: 0 : if (digest == NULL) {
470 : 0 : return PyErr_NoMemory();
471 : : }
472 : :
473 : : /* Get the raw (binary) digest value */
474 [ # # # # ]: 0 : ENTER_HASHLIB(self);
475 : 0 : SHA3_copystate(temp, self->hash_state);
476 [ # # ]: 0 : LEAVE_HASHLIB(self);
477 : 0 : SHA3_squeeze(&temp, digest, digestlen);
478 [ # # ]: 0 : if (hex) {
479 : 0 : result = _Py_strhex((const char *)digest, digestlen);
480 : : } else {
481 : 0 : result = PyBytes_FromStringAndSize((const char *)digest,
482 : : digestlen);
483 : : }
484 [ # # ]: 0 : if (digest != NULL) {
485 : 0 : PyMem_Free(digest);
486 : : }
487 : 0 : return result;
488 : : }
489 : :
490 : :
491 : : /*[clinic input]
492 : : _sha3.shake_128.digest
493 : :
494 : : length: unsigned_long
495 : : /
496 : :
497 : : Return the digest value as a bytes object.
498 : : [clinic start generated code]*/
499 : :
500 : : static PyObject *
501 : 0 : _sha3_shake_128_digest_impl(SHA3object *self, unsigned long length)
502 : : /*[clinic end generated code: output=2313605e2f87bb8f input=418ef6a36d2e6082]*/
503 : : {
504 : 0 : return _SHAKE_digest(self, length, 0);
505 : : }
506 : :
507 : :
508 : : /*[clinic input]
509 : : _sha3.shake_128.hexdigest
510 : :
511 : : length: unsigned_long
512 : : /
513 : :
514 : : Return the digest value as a string of hexadecimal digits.
515 : : [clinic start generated code]*/
516 : :
517 : : static PyObject *
518 : 0 : _sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length)
519 : : /*[clinic end generated code: output=bf8e2f1e490944a8 input=69fb29b0926ae321]*/
520 : : {
521 : 0 : return _SHAKE_digest(self, length, 1);
522 : : }
523 : :
524 : : static PyObject *
525 : 0 : SHAKE_get_digest_size(SHA3object *self, void *closure)
526 : : {
527 : 0 : return PyLong_FromLong(0);
528 : : }
529 : :
530 : : static PyObject *
531 : 0 : SHAKE_get_suffix(SHA3object *self, void *closure)
532 : : {
533 : 0 : unsigned char suffix[2] = {0x1f, 0};
534 : 0 : return PyBytes_FromStringAndSize((const char *)suffix, 1);
535 : : }
536 : :
537 : :
538 : : static PyGetSetDef SHAKE_getseters[] = {
539 : : {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
540 : : {"name", (getter)SHA3_get_name, NULL, NULL, NULL},
541 : : {"digest_size", (getter)SHAKE_get_digest_size, NULL, NULL, NULL},
542 : : {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL},
543 : : {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL},
544 : : {"_suffix", (getter)SHAKE_get_suffix, NULL, NULL, NULL},
545 : : {NULL} /* Sentinel */
546 : : };
547 : :
548 : :
549 : : static PyMethodDef SHAKE_methods[] = {
550 : : _SHA3_SHA3_224_COPY_METHODDEF
551 : : _SHA3_SHAKE_128_DIGEST_METHODDEF
552 : : _SHA3_SHAKE_128_HEXDIGEST_METHODDEF
553 : : _SHA3_SHA3_224_UPDATE_METHODDEF
554 : : {NULL, NULL} /* sentinel */
555 : : };
556 : :
557 : : PyDoc_STRVAR(shake_128__doc__,
558 : : "shake_128([data], *, usedforsecurity=True) -> SHAKE object\n\
559 : : \n\
560 : : Return a new SHAKE hash object.");
561 : :
562 : : PyDoc_STRVAR(shake_256__doc__,
563 : : "shake_256([data], *, usedforsecurity=True) -> SHAKE object\n\
564 : : \n\
565 : : Return a new SHAKE hash object.");
566 : :
567 : : SHA3_TYPE_SLOTS(SHAKE128slots, shake_128__doc__, SHAKE_methods, SHAKE_getseters);
568 : : SHA3_TYPE_SPEC(SHAKE128_spec, "shake_128", SHAKE128slots);
569 : :
570 : : SHA3_TYPE_SLOTS(SHAKE256slots, shake_256__doc__, SHAKE_methods, SHAKE_getseters);
571 : : SHA3_TYPE_SPEC(SHAKE256_spec, "shake_256", SHAKE256slots);
572 : :
573 : :
574 : : static int
575 : 8 : _sha3_traverse(PyObject *module, visitproc visit, void *arg)
576 : : {
577 : 8 : SHA3State *state = sha3_get_state(module);
578 [ + - - + ]: 8 : Py_VISIT(state->sha3_224_type);
579 [ + - - + ]: 8 : Py_VISIT(state->sha3_256_type);
580 [ + - - + ]: 8 : Py_VISIT(state->sha3_384_type);
581 [ + - - + ]: 8 : Py_VISIT(state->sha3_512_type);
582 [ + - - + ]: 8 : Py_VISIT(state->shake_128_type);
583 [ + - - + ]: 8 : Py_VISIT(state->shake_256_type);
584 : 8 : return 0;
585 : : }
586 : :
587 : : static int
588 : 2 : _sha3_clear(PyObject *module)
589 : : {
590 : 2 : SHA3State *state = sha3_get_state(module);
591 [ + + ]: 2 : Py_CLEAR(state->sha3_224_type);
592 [ + + ]: 2 : Py_CLEAR(state->sha3_256_type);
593 [ + + ]: 2 : Py_CLEAR(state->sha3_384_type);
594 [ + + ]: 2 : Py_CLEAR(state->sha3_512_type);
595 [ + + ]: 2 : Py_CLEAR(state->shake_128_type);
596 [ + + ]: 2 : Py_CLEAR(state->shake_256_type);
597 : 2 : return 0;
598 : : }
599 : :
600 : : static void
601 : 1 : _sha3_free(void *module)
602 : : {
603 : 1 : _sha3_clear((PyObject *)module);
604 : 1 : }
605 : :
606 : : static int
607 : 1 : _sha3_exec(PyObject *m)
608 : : {
609 : 1 : SHA3State *st = sha3_get_state(m);
610 : :
611 : : #define init_sha3type(type, typespec) \
612 : : do { \
613 : : st->type = (PyTypeObject *)PyType_FromModuleAndSpec( \
614 : : m, &typespec, NULL); \
615 : : if (st->type == NULL) { \
616 : : return -1; \
617 : : } \
618 : : if (PyModule_AddType(m, st->type) < 0) { \
619 : : return -1; \
620 : : } \
621 : : } while(0)
622 : :
623 [ - + - + ]: 1 : init_sha3type(sha3_224_type, sha3_224_spec);
624 [ - + - + ]: 1 : init_sha3type(sha3_256_type, sha3_256_spec);
625 [ - + - + ]: 1 : init_sha3type(sha3_384_type, sha3_384_spec);
626 [ - + - + ]: 1 : init_sha3type(sha3_512_type, sha3_512_spec);
627 [ - + - + ]: 1 : init_sha3type(shake_128_type, SHAKE128_spec);
628 [ - + - + ]: 1 : init_sha3type(shake_256_type, SHAKE256_spec);
629 : : #undef init_sha3type
630 : :
631 [ - + ]: 1 : if (PyModule_AddIntConstant(m, "keccakopt", KeccakOpt) < 0) {
632 : 0 : return -1;
633 : : }
634 [ - + ]: 1 : if (PyModule_AddStringConstant(m, "implementation",
635 : : "tiny_sha3") < 0) {
636 : 0 : return -1;
637 : : }
638 : :
639 : 1 : return 0;
640 : : }
641 : :
642 : : static PyModuleDef_Slot _sha3_slots[] = {
643 : : {Py_mod_exec, _sha3_exec},
644 : : {0, NULL}
645 : : };
646 : :
647 : : /* Initialize this module. */
648 : : static struct PyModuleDef _sha3module = {
649 : : PyModuleDef_HEAD_INIT,
650 : : .m_name = "_sha3",
651 : : .m_size = sizeof(SHA3State),
652 : : .m_slots = _sha3_slots,
653 : : .m_traverse = _sha3_traverse,
654 : : .m_clear = _sha3_clear,
655 : : .m_free = _sha3_free,
656 : : };
657 : :
658 : :
659 : : PyMODINIT_FUNC
660 : 1 : PyInit__sha3(void)
661 : : {
662 : 1 : return PyModuleDef_Init(&_sha3module);
663 : : }
|