| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | /* SHA3 module
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This module provides an interface to the SHA3 algorithm | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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) | 
					
						
							|  |  |  |  *  Gregory P. Smith (greg@krypto.org) | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  |  * Copyright (C) 2012-2022  Christian Heimes (christian@python.org) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |  * Licensed to PSF under a Contributor Agreement. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-22 16:36:28 +03:00
										 |  |  | #ifndef Py_BUILD_CORE_BUILTIN
 | 
					
						
							|  |  |  | #  define Py_BUILD_CORE_MODULE 1
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2021-10-13 15:22:35 +02:00
										 |  |  | #include "pycore_strhex.h"        // _Py_strhex()
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:16:29 +01:00
										 |  |  | #include "pycore_typeobject.h"    // _PyType_GetModuleState()
 | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  | #include "hashlib.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     PyTypeObject *sha3_224_type; | 
					
						
							|  |  |  |     PyTypeObject *sha3_256_type; | 
					
						
							|  |  |  |     PyTypeObject *sha3_384_type; | 
					
						
							|  |  |  |     PyTypeObject *sha3_512_type; | 
					
						
							|  |  |  |     PyTypeObject *shake_128_type; | 
					
						
							|  |  |  |     PyTypeObject *shake_256_type; | 
					
						
							|  |  |  | } SHA3State; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline SHA3State* | 
					
						
							|  |  |  | sha3_get_state(PyObject *module) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     void *state = PyModule_GetState(module); | 
					
						
							|  |  |  |     assert(state != NULL); | 
					
						
							|  |  |  |     return (SHA3State *)state; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | module _sha3 | 
					
						
							|  |  |  | class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ" | 
					
						
							|  |  |  | class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ" | 
					
						
							|  |  |  | class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ" | 
					
						
							|  |  |  | class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ" | 
					
						
							|  |  |  | class _sha3.shake_128 "SHA3object *" "&SHAKE128type" | 
					
						
							|  |  |  | class _sha3.shake_256 "SHA3object *" "&SHAKE256type" | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* The structure for storing SHA3 info */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  | #include "_hacl/Hacl_Hash_SHA3.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | typedef struct { | 
					
						
							|  |  |  |     PyObject_HEAD | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     // Prevents undefined behavior via multiple threads entering the C API.
 | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |     bool use_mutex; | 
					
						
							|  |  |  |     PyMutex mutex; | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     Hacl_Hash_SHA3_state_t *hash_state; | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } SHA3object; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | #define _SHA3object_CAST(op)    ((SHA3object *)(op))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | #include "clinic/sha3module.c.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static SHA3object * | 
					
						
							|  |  |  | newSHA3object(PyTypeObject *type) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |     SHA3object *newobj = PyObject_GC_New(SHA3object, type); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     if (newobj == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |     HASHLIB_INIT_MUTEX(newobj); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |     PyObject_GC_Track(newobj); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return newobj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  | static void | 
					
						
							|  |  |  | sha3_update(Hacl_Hash_SHA3_state_t *state, uint8_t *buf, Py_ssize_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /*
 | 
					
						
							|  |  |  |    * Note: we explicitly ignore the error code on the basis that it would | 
					
						
							|  |  |  |    * take more than 1 billion years to overflow the maximum admissible length | 
					
						
							|  |  |  |    * for SHA-3 (2^64 - 1). | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  | #if PY_SSIZE_T_MAX > UINT32_MAX
 | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     while (len > UINT32_MAX) { | 
					
						
							|  |  |  |         (void)Hacl_Hash_SHA3_update(state, buf, UINT32_MAX); | 
					
						
							|  |  |  |         len -= UINT32_MAX; | 
					
						
							|  |  |  |         buf += UINT32_MAX; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     /* cast to uint32_t is now safe */ | 
					
						
							|  |  |  |     (void)Hacl_Hash_SHA3_update(state, buf, (uint32_t)len); | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | @classmethod | 
					
						
							|  |  |  | _sha3.sha3_224.__new__ as py_sha3_new | 
					
						
							|  |  |  |     data: object(c_default="NULL") = b'' | 
					
						
							|  |  |  |     / | 
					
						
							|  |  |  |     * | 
					
						
							|  |  |  |     usedforsecurity: bool = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  | Return a new SHA3 hash object. | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | [clinic start generated code]*/ | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | py_sha3_new_impl(PyTypeObject *type, PyObject *data, int usedforsecurity) | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  | /*[clinic end generated code: output=90409addc5d5e8b0 input=637e5f8f6a93982a]*/ | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-18 08:39:39 +02:00
										 |  |  |     Py_buffer buf = {NULL, NULL}; | 
					
						
							| 
									
										
										
										
											2023-02-24 21:16:29 +01:00
										 |  |  |     SHA3State *state = _PyType_GetModuleState(type); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     SHA3object *self = newSHA3object(type); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     if (self == NULL) { | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     assert(state != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (type == state->sha3_224_type) { | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |         self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_224); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (type == state->sha3_256_type) { | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |         self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_256); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (type == state->sha3_384_type) { | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |         self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_384); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (type == state->sha3_512_type) { | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |         self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_512); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (type == state->shake_128_type) { | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |         self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_Shake128); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (type == state->shake_256_type) { | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |         self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_Shake256); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |         PyErr_BadInternalCall(); | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     if (self->hash_state == NULL) { | 
					
						
							|  |  |  |         (void)PyErr_NoMemory(); | 
					
						
							|  |  |  |         goto error; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     if (data) { | 
					
						
							|  |  |  |         GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |         if (buf.len >= HASHLIB_GIL_MINSIZE) { | 
					
						
							|  |  |  |             /* We do not initialize self->lock here as this is the constructor
 | 
					
						
							|  |  |  |              * where it is not yet possible to have concurrent access. */ | 
					
						
							|  |  |  |             Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |             sha3_update(self->hash_state, buf.buf, buf.len); | 
					
						
							|  |  |  |             Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |             sha3_update(self->hash_state, buf.buf, buf.len); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     PyBuffer_Release(&buf); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return (PyObject *)self; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  | error: | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     if (self) { | 
					
						
							|  |  |  |         Py_DECREF(self); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (data && buf.obj) { | 
					
						
							|  |  |  |         PyBuffer_Release(&buf); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Internal methods for a hash object */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHA3_clear(PyObject *op) | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     SHA3object *self = _SHA3object_CAST(op); | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |     if (self->hash_state != NULL) { | 
					
						
							|  |  |  |         Hacl_Hash_SHA3_free(self->hash_state); | 
					
						
							|  |  |  |         self->hash_state = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHA3_dealloc(PyObject *self) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     PyTypeObject *tp = Py_TYPE(self); | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |     PyObject_GC_UnTrack(self); | 
					
						
							|  |  |  |     (void)SHA3_clear(self); | 
					
						
							|  |  |  |     tp->tp_free(self); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     Py_DECREF(tp); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | SHA3_traverse(PyObject *self, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_VISIT(Py_TYPE(self)); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* External methods for a hash object */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _sha3.sha3_224.copy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return a copy of the hash object. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _sha3_sha3_224_copy_impl(SHA3object *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/ | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SHA3object *newobj; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     ENTER_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     newobj->hash_state = Hacl_Hash_SHA3_copy(self->hash_state); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     LEAVE_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     if (newobj->hash_state == NULL) { | 
					
						
							|  |  |  |         Py_DECREF(newobj); | 
					
						
							|  |  |  |         return PyErr_NoMemory(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return (PyObject *)newobj; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _sha3.sha3_224.digest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  | Return the digest value as a bytes object. | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _sha3_sha3_224_digest_impl(SHA3object *self) | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  | /*[clinic end generated code: output=fd531842e20b2d5b input=5b2a659536bbd248]*/ | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     unsigned char digest[SHA3_MAX_DIGESTSIZE]; | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     // This function errors out if the algorithm is SHAKE. Here, we know this
 | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     // not to be the case, and therefore do not perform error checking.
 | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     ENTER_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     (void)Hacl_Hash_SHA3_digest(self->hash_state, digest); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     LEAVE_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return PyBytes_FromStringAndSize((const char *)digest, | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |         Hacl_Hash_SHA3_hash_len(self->hash_state)); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _sha3.sha3_224.hexdigest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Return the digest value as a string of hexadecimal digits. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _sha3_sha3_224_hexdigest_impl(SHA3object *self) | 
					
						
							|  |  |  | /*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/ | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     unsigned char digest[SHA3_MAX_DIGESTSIZE]; | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     ENTER_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     (void)Hacl_Hash_SHA3_digest(self->hash_state, digest); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     LEAVE_HASHLIB(self); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return _Py_strhex((const char *)digest, | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |         Hacl_Hash_SHA3_hash_len(self->hash_state)); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _sha3.sha3_224.update | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  |     data: object | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     / | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  | Update this hash object's state with the provided bytes-like object. | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-03-11 16:33:36 +01:00
										 |  |  | _sha3_sha3_224_update_impl(SHA3object *self, PyObject *data) | 
					
						
							|  |  |  | /*[clinic end generated code: output=390b7abf7c9795a5 input=a887f54dcc4ae227]*/ | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     Py_buffer buf; | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  |     GET_BUFFER_VIEW_OR_ERROUT(data, &buf); | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!self->use_mutex && buf.len >= HASHLIB_GIL_MINSIZE) { | 
					
						
							|  |  |  |         self->use_mutex = true; | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |     if (self->use_mutex) { | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |         Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |         PyMutex_Lock(&self->mutex); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |         sha3_update(self->hash_state, buf.buf, buf.len); | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  |         PyMutex_Unlock(&self->mutex); | 
					
						
							| 
									
										
										
										
											2023-05-22 17:06:41 -07:00
										 |  |  |         Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         sha3_update(self->hash_state, buf.buf, buf.len); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-11-16 00:53:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     PyBuffer_Release(&buf); | 
					
						
							| 
									
										
										
										
											2017-01-23 09:47:21 +02:00
										 |  |  |     Py_RETURN_NONE; | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef SHA3_methods[] = { | 
					
						
							|  |  |  |     _SHA3_SHA3_224_COPY_METHODDEF | 
					
						
							|  |  |  |     _SHA3_SHA3_224_DIGEST_METHODDEF | 
					
						
							|  |  |  |     _SHA3_SHA3_224_HEXDIGEST_METHODDEF | 
					
						
							|  |  |  |     _SHA3_SHA3_224_UPDATE_METHODDEF | 
					
						
							|  |  |  |     {NULL,        NULL}         /* sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHA3_get_block_size(PyObject *op, void *Py_UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     SHA3object *self = _SHA3object_CAST(op); | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state); | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  |     return PyLong_FromLong(rate); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHA3_get_name(PyObject *self, void *Py_UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     PyTypeObject *type = Py_TYPE(self); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:16:29 +01:00
										 |  |  |     SHA3State *state = _PyType_GetModuleState(type); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     assert(state != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (type == state->sha3_224_type) { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |         return PyUnicode_FromString("sha3_224"); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     } else if (type == state->sha3_256_type) { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |         return PyUnicode_FromString("sha3_256"); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     } else if (type == state->sha3_384_type) { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |         return PyUnicode_FromString("sha3_384"); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     } else if (type == state->sha3_512_type) { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |         return PyUnicode_FromString("sha3_512"); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     } else if (type == state->shake_128_type) { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |         return PyUnicode_FromString("shake_128"); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     } else if (type == state->shake_256_type) { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |         return PyUnicode_FromString("shake_256"); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         PyErr_BadInternalCall(); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHA3_get_digest_size(PyObject *op, void *Py_UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     // Preserving previous behavior: variable-length algorithms return 0
 | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     SHA3object *self = _SHA3object_CAST(op); | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     if (Hacl_Hash_SHA3_is_shake(self->hash_state)) | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |       return PyLong_FromLong(0); | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |       return PyLong_FromLong(Hacl_Hash_SHA3_hash_len(self->hash_state)); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHA3_get_capacity_bits(PyObject *op, void *Py_UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     SHA3object *self = _SHA3object_CAST(op); | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state) * 8; | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |     assert(rate <= 1600); | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     int capacity = 1600 - rate; | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return PyLong_FromLong(capacity); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHA3_get_rate_bits(PyObject *op, void *Py_UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     SHA3object *self = _SHA3object_CAST(op); | 
					
						
							| 
									
										
										
										
											2024-03-25 17:35:26 -07:00
										 |  |  |     uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state) * 8; | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return PyLong_FromLong(rate); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHA3_get_suffix(PyObject *Py_UNUSED(self), void *Py_UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  |     unsigned char suffix[2] = {0x06, 0}; | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return PyBytes_FromStringAndSize((const char *)suffix, 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyGetSetDef SHA3_getseters[] = { | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     {"block_size", SHA3_get_block_size, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"name", SHA3_get_name, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"digest_size", SHA3_get_digest_size, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"_capacity_bits", SHA3_get_capacity_bits, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"_rate_bits", SHA3_get_rate_bits, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"_suffix", SHA3_get_suffix, NULL, NULL, NULL}, | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     {NULL}  /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | #define SHA3_TYPE_SLOTS(type_slots_obj, type_doc, type_methods, type_getseters) \
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     static PyType_Slot type_slots_obj[] = { \ | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |         {Py_tp_clear, SHA3_clear}, \ | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |         {Py_tp_dealloc, SHA3_dealloc}, \ | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |         {Py_tp_traverse, SHA3_traverse}, \ | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |         {Py_tp_doc, (char*)type_doc}, \ | 
					
						
							|  |  |  |         {Py_tp_methods, type_methods}, \ | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  |         {Py_tp_getset, type_getseters}, \ | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |         {Py_tp_new, py_sha3_new}, \ | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |         {0, NULL} \ | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-24 21:16:29 +01:00
										 |  |  | // Using _PyType_GetModuleState() on these types is safe since they
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | // cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag.
 | 
					
						
							|  |  |  | #define SHA3_TYPE_SPEC(type_spec_obj, type_name, type_slots) \
 | 
					
						
							|  |  |  |     static PyType_Spec type_spec_obj = { \ | 
					
						
							|  |  |  |         .name = "_sha3." type_name, \ | 
					
						
							|  |  |  |         .basicsize = sizeof(SHA3object), \ | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |         .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE \ | 
					
						
							|  |  |  |                  | Py_TPFLAGS_HAVE_GC, \ | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |         .slots = type_slots \ | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  | PyDoc_STRVAR(sha3_224__doc__, | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | "sha3_224([data], *, usedforsecurity=True) -> SHA3 object\n\
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  | \n\ | 
					
						
							|  |  |  | Return a new SHA3 hash object with a hashbit length of 28 bytes."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | PyDoc_STRVAR(sha3_256__doc__, | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | "sha3_256([data], *, usedforsecurity=True) -> SHA3 object\n\
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | \n\ | 
					
						
							|  |  |  | Return a new SHA3 hash object with a hashbit length of 32 bytes."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(sha3_384__doc__, | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | "sha3_384([data], *, usedforsecurity=True) -> SHA3 object\n\
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | \n\ | 
					
						
							|  |  |  | Return a new SHA3 hash object with a hashbit length of 48 bytes."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(sha3_512__doc__, | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | "sha3_512([data], *, usedforsecurity=True) -> SHA3 object\n\
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | \n\ | 
					
						
							|  |  |  | Return a new SHA3 hash object with a hashbit length of 64 bytes."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | SHA3_TYPE_SLOTS(sha3_224_slots, sha3_224__doc__, SHA3_methods, SHA3_getseters); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | SHA3_TYPE_SPEC(sha3_224_spec, "sha3_224", sha3_224_slots); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | SHA3_TYPE_SLOTS(sha3_256_slots, sha3_256__doc__, SHA3_methods, SHA3_getseters); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | SHA3_TYPE_SPEC(sha3_256_spec, "sha3_256", sha3_256_slots); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | SHA3_TYPE_SLOTS(sha3_384_slots, sha3_384__doc__, SHA3_methods, SHA3_getseters); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | SHA3_TYPE_SPEC(sha3_384_spec, "sha3_384", sha3_384_slots); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | SHA3_TYPE_SLOTS(sha3_512_slots, sha3_512__doc__, SHA3_methods, SHA3_getseters); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | SHA3_TYPE_SPEC(sha3_512_spec, "sha3_512", sha3_512_slots); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | _SHAKE_digest(PyObject *op, unsigned long digestlen, int hex) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char *digest = NULL; | 
					
						
							|  |  |  |     PyObject *result = NULL; | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     SHA3object *self = _SHA3object_CAST(op); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 07:41:00 +03:00
										 |  |  |     if (digestlen >= (1 << 29)) { | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_ValueError, "length is too large"); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     digest = (unsigned char*)PyMem_Malloc(digestlen); | 
					
						
							| 
									
										
										
										
											2016-09-08 13:35:00 +02:00
										 |  |  |     if (digest == NULL) { | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |         return PyErr_NoMemory(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     /* Get the raw (binary) digest value. The HACL functions errors out if:
 | 
					
						
							| 
									
										
										
										
											2024-08-28 19:41:04 +08:00
										 |  |  |      * - the algorithm is not shake -- not the case here | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |      * - the output length is zero -- we follow the existing behavior and return | 
					
						
							|  |  |  |      *   an empty digest, without raising an error */ | 
					
						
							|  |  |  |     if (digestlen > 0) { | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |         (void)Hacl_Hash_SHA3_squeeze(self->hash_state, digest, digestlen); | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     if (hex) { | 
					
						
							| 
									
										
										
										
											2023-05-07 20:50:04 -07:00
										 |  |  |         result = _Py_strhex((const char *)digest, digestlen); | 
					
						
							| 
									
										
										
										
											2025-03-17 11:10:39 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         result = PyBytes_FromStringAndSize((const char *)digest, digestlen); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |     PyMem_Free(digest); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _sha3.shake_128.digest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  |     length: unsigned_long | 
					
						
							|  |  |  |     / | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  | Return the digest value as a bytes object. | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _sha3_shake_128_digest_impl(SHA3object *self, unsigned long length) | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  | /*[clinic end generated code: output=2313605e2f87bb8f input=418ef6a36d2e6082]*/ | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     return _SHAKE_digest((PyObject *)self, length, 0); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*[clinic input]
 | 
					
						
							|  |  |  | _sha3.shake_128.hexdigest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  |     length: unsigned_long | 
					
						
							|  |  |  |     / | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | Return the digest value as a string of hexadecimal digits. | 
					
						
							|  |  |  | [clinic start generated code]*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | _sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length) | 
					
						
							| 
									
										
										
										
											2018-07-31 09:50:16 +03:00
										 |  |  | /*[clinic end generated code: output=bf8e2f1e490944a8 input=69fb29b0926ae321]*/ | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     return _SHAKE_digest((PyObject *)self, length, 1); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHAKE_get_digest_size(PyObject *Py_UNUSED(self), void *Py_UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return PyLong_FromLong(0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  | SHAKE_get_suffix(PyObject *Py_UNUSED(self), void *Py_UNUSED(closure)) | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char suffix[2] = {0x1f, 0}; | 
					
						
							|  |  |  |     return PyBytes_FromStringAndSize((const char *)suffix, 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyGetSetDef SHAKE_getseters[] = { | 
					
						
							| 
									
										
										
										
											2025-01-27 14:50:58 +01:00
										 |  |  |     {"block_size", SHA3_get_block_size, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"name", SHA3_get_name, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"digest_size", SHAKE_get_digest_size, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"_capacity_bits", SHA3_get_capacity_bits, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"_rate_bits", SHA3_get_rate_bits, NULL, NULL, NULL}, | 
					
						
							|  |  |  |     {"_suffix", SHAKE_get_suffix, NULL, NULL, NULL}, | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  |     {NULL}  /* Sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef SHAKE_methods[] = { | 
					
						
							|  |  |  |     _SHA3_SHA3_224_COPY_METHODDEF | 
					
						
							|  |  |  |     _SHA3_SHAKE_128_DIGEST_METHODDEF | 
					
						
							|  |  |  |     _SHA3_SHAKE_128_HEXDIGEST_METHODDEF | 
					
						
							|  |  |  |     _SHA3_SHA3_224_UPDATE_METHODDEF | 
					
						
							|  |  |  |     {NULL,        NULL}         /* sentinel */ | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(shake_128__doc__, | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | "shake_128([data], *, usedforsecurity=True) -> SHAKE object\n\
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | \n\ | 
					
						
							|  |  |  | Return a new SHAKE hash object."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(shake_256__doc__, | 
					
						
							| 
									
										
										
										
											2019-09-13 02:30:00 +02:00
										 |  |  | "shake_256([data], *, usedforsecurity=True) -> SHAKE object\n\
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | \n\ | 
					
						
							|  |  |  | Return a new SHAKE hash object."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | SHA3_TYPE_SLOTS(SHAKE128slots, shake_128__doc__, SHAKE_methods, SHAKE_getseters); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | SHA3_TYPE_SPEC(SHAKE128_spec, "shake_128", SHAKE128slots); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-26 22:36:08 +02:00
										 |  |  | SHA3_TYPE_SLOTS(SHAKE256slots, shake_256__doc__, SHAKE_methods, SHAKE_getseters); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | SHA3_TYPE_SPEC(SHAKE256_spec, "shake_256", SHAKE256slots); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | static int | 
					
						
							|  |  |  | _sha3_traverse(PyObject *module, visitproc visit, void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SHA3State *state = sha3_get_state(module); | 
					
						
							|  |  |  |     Py_VISIT(state->sha3_224_type); | 
					
						
							|  |  |  |     Py_VISIT(state->sha3_256_type); | 
					
						
							|  |  |  |     Py_VISIT(state->sha3_384_type); | 
					
						
							|  |  |  |     Py_VISIT(state->sha3_512_type); | 
					
						
							|  |  |  |     Py_VISIT(state->shake_128_type); | 
					
						
							|  |  |  |     Py_VISIT(state->shake_256_type); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | static int | 
					
						
							|  |  |  | _sha3_clear(PyObject *module) | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     SHA3State *state = sha3_get_state(module); | 
					
						
							|  |  |  |     Py_CLEAR(state->sha3_224_type); | 
					
						
							|  |  |  |     Py_CLEAR(state->sha3_256_type); | 
					
						
							|  |  |  |     Py_CLEAR(state->sha3_384_type); | 
					
						
							|  |  |  |     Py_CLEAR(state->sha3_512_type); | 
					
						
							|  |  |  |     Py_CLEAR(state->shake_128_type); | 
					
						
							|  |  |  |     Py_CLEAR(state->shake_256_type); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | static void | 
					
						
							|  |  |  | _sha3_free(void *module) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-12-08 18:31:10 +01:00
										 |  |  |     (void)_sha3_clear((PyObject *)module); | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  | static int | 
					
						
							|  |  |  | _sha3_exec(PyObject *m) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     SHA3State *st = sha3_get_state(m); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define init_sha3type(type, typespec)                           \
 | 
					
						
							|  |  |  |     do {                                                        \ | 
					
						
							|  |  |  |         st->type = (PyTypeObject *)PyType_FromModuleAndSpec(    \ | 
					
						
							|  |  |  |             m, &typespec, NULL);                                \ | 
					
						
							|  |  |  |         if (st->type == NULL) {                                 \ | 
					
						
							|  |  |  |             return -1;                                          \ | 
					
						
							|  |  |  |         }                                                       \ | 
					
						
							|  |  |  |         if (PyModule_AddType(m, st->type) < 0) {                \ | 
					
						
							|  |  |  |             return -1;                                          \ | 
					
						
							|  |  |  |         }                                                       \ | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     } while(0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     init_sha3type(sha3_224_type, sha3_224_spec); | 
					
						
							|  |  |  |     init_sha3type(sha3_256_type, sha3_256_spec); | 
					
						
							|  |  |  |     init_sha3type(sha3_384_type, sha3_384_spec); | 
					
						
							|  |  |  |     init_sha3type(sha3_512_type, sha3_512_spec); | 
					
						
							|  |  |  |     init_sha3type(shake_128_type, SHAKE128_spec); | 
					
						
							|  |  |  |     init_sha3type(shake_256_type, SHAKE256_spec); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | #undef init_sha3type
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-28 00:20:15 +02:00
										 |  |  |     if (PyModule_AddStringConstant(m, "implementation", "HACL") < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PyModule_AddIntConstant(m, "_GIL_MINSIZE", HASHLIB_GIL_MINSIZE) < 0) { | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |         return -1; | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyModuleDef_Slot _sha3_slots[] = { | 
					
						
							|  |  |  |     {Py_mod_exec, _sha3_exec}, | 
					
						
							| 
									
										
										
										
											2023-05-05 15:11:27 -06:00
										 |  |  |     {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, | 
					
						
							| 
									
										
										
										
											2024-05-03 08:30:55 -07:00
										 |  |  |     {Py_mod_gil, Py_MOD_GIL_NOT_USED}, | 
					
						
							| 
									
										
										
										
											2020-09-02 04:55:19 -05:00
										 |  |  |     {0, NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Initialize this module. */ | 
					
						
							|  |  |  | static struct PyModuleDef _sha3module = { | 
					
						
							|  |  |  |     PyModuleDef_HEAD_INIT, | 
					
						
							|  |  |  |     .m_name = "_sha3", | 
					
						
							|  |  |  |     .m_size = sizeof(SHA3State), | 
					
						
							|  |  |  |     .m_slots = _sha3_slots, | 
					
						
							|  |  |  |     .m_traverse = _sha3_traverse, | 
					
						
							|  |  |  |     .m_clear = _sha3_clear, | 
					
						
							|  |  |  |     .m_free = _sha3_free, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyMODINIT_FUNC | 
					
						
							|  |  |  | PyInit__sha3(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return PyModuleDef_Init(&_sha3module); | 
					
						
							| 
									
										
										
										
											2016-09-07 11:58:24 +02:00
										 |  |  | } |