Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 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-2016 Christian Heimes (christian@python.org) |
| 14 | * Licensed to PSF under a Contributor Agreement. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include "Python.h" |
| 19 | #include "pystrhex.h" |
| 20 | #include "../hashlib.h" |
| 21 | |
| 22 | /* ************************************************************************** |
| 23 | * SHA-3 (Keccak) and SHAKE |
| 24 | * |
| 25 | * The code is based on KeccakCodePackage from 2016-04-23 |
| 26 | * commit 647f93079afc4ada3d23737477a6e52511ca41fd |
| 27 | * |
| 28 | * The reference implementation is altered in this points: |
| 29 | * - C++ comments are converted to ANSI C comments. |
| 30 | * - all function names are mangled |
| 31 | * - typedef for UINT64 is commented out. |
| 32 | * - brg_endian.h is removed |
| 33 | * |
| 34 | * *************************************************************************/ |
| 35 | |
| 36 | #ifdef __sparc |
| 37 | /* opt64 uses un-aligned memory access that causes a BUS error with msg |
| 38 | * 'invalid address alignment' on SPARC. */ |
| 39 | #define KeccakOpt 32 |
Christian Heimes | b205fe9 | 2016-09-07 12:42:47 +0200 | [diff] [blame] | 40 | #elif PY_BIG_ENDIAN |
| 41 | /* opt64 is not yet supported on big endian platforms */ |
| 42 | #define KeccakOpt 32 |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 43 | #elif SIZEOF_VOID_P == 8 |
Christian Heimes | b205fe9 | 2016-09-07 12:42:47 +0200 | [diff] [blame] | 44 | /* opt64 works only on little-endian 64bit platforms with unsigned int64 */ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 45 | #define KeccakOpt 64 |
| 46 | #else |
| 47 | /* opt32 is used for the remaining 32 and 64bit platforms */ |
| 48 | #define KeccakOpt 32 |
| 49 | #endif |
| 50 | |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 51 | #if KeccakOpt == 64 |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 52 | /* 64bit platforms with unsigned int64 */ |
Victor Stinner | 1a1bd2e | 2020-04-17 19:13:06 +0200 | [diff] [blame] | 53 | typedef uint64_t UINT64; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 54 | typedef unsigned char UINT8; |
| 55 | #endif |
| 56 | |
| 57 | /* replacement for brg_endian.h */ |
| 58 | #define IS_LITTLE_ENDIAN 1234 |
| 59 | #define IS_BIG_ENDIAN 4321 |
| 60 | #if PY_LITTLE_ENDIAN |
| 61 | #define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN |
| 62 | #endif |
| 63 | #if PY_BIG_ENDIAN |
| 64 | #define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN |
| 65 | #endif |
| 66 | |
| 67 | /* mangle names */ |
| 68 | #define KeccakF1600_FastLoop_Absorb _PySHA3_KeccakF1600_FastLoop_Absorb |
| 69 | #define Keccak_HashFinal _PySHA3_Keccak_HashFinal |
| 70 | #define Keccak_HashInitialize _PySHA3_Keccak_HashInitialize |
| 71 | #define Keccak_HashSqueeze _PySHA3_Keccak_HashSqueeze |
| 72 | #define Keccak_HashUpdate _PySHA3_Keccak_HashUpdate |
| 73 | #define KeccakP1600_AddBytes _PySHA3_KeccakP1600_AddBytes |
| 74 | #define KeccakP1600_AddBytesInLane _PySHA3_KeccakP1600_AddBytesInLane |
| 75 | #define KeccakP1600_AddLanes _PySHA3_KeccakP1600_AddLanes |
| 76 | #define KeccakP1600_ExtractAndAddBytes _PySHA3_KeccakP1600_ExtractAndAddBytes |
| 77 | #define KeccakP1600_ExtractAndAddBytesInLane _PySHA3_KeccakP1600_ExtractAndAddBytesInLane |
| 78 | #define KeccakP1600_ExtractAndAddLanes _PySHA3_KeccakP1600_ExtractAndAddLanes |
| 79 | #define KeccakP1600_ExtractBytes _PySHA3_KeccakP1600_ExtractBytes |
| 80 | #define KeccakP1600_ExtractBytesInLane _PySHA3_KeccakP1600_ExtractBytesInLane |
| 81 | #define KeccakP1600_ExtractLanes _PySHA3_KeccakP1600_ExtractLanes |
| 82 | #define KeccakP1600_Initialize _PySHA3_KeccakP1600_Initialize |
| 83 | #define KeccakP1600_OverwriteBytes _PySHA3_KeccakP1600_OverwriteBytes |
| 84 | #define KeccakP1600_OverwriteBytesInLane _PySHA3_KeccakP1600_OverwriteBytesInLane |
| 85 | #define KeccakP1600_OverwriteLanes _PySHA3_KeccakP1600_OverwriteLanes |
| 86 | #define KeccakP1600_OverwriteWithZeroes _PySHA3_KeccakP1600_OverwriteWithZeroes |
| 87 | #define KeccakP1600_Permute_12rounds _PySHA3_KeccakP1600_Permute_12rounds |
| 88 | #define KeccakP1600_Permute_24rounds _PySHA3_KeccakP1600_Permute_24rounds |
| 89 | #define KeccakWidth1600_Sponge _PySHA3_KeccakWidth1600_Sponge |
| 90 | #define KeccakWidth1600_SpongeAbsorb _PySHA3_KeccakWidth1600_SpongeAbsorb |
| 91 | #define KeccakWidth1600_SpongeAbsorbLastFewBits _PySHA3_KeccakWidth1600_SpongeAbsorbLastFewBits |
| 92 | #define KeccakWidth1600_SpongeInitialize _PySHA3_KeccakWidth1600_SpongeInitialize |
| 93 | #define KeccakWidth1600_SpongeSqueeze _PySHA3_KeccakWidth1600_SpongeSqueeze |
| 94 | #if KeccakOpt == 32 |
| 95 | #define KeccakP1600_AddByte _PySHA3_KeccakP1600_AddByte |
| 96 | #define KeccakP1600_Permute_Nrounds _PySHA3_KeccakP1600_Permute_Nrounds |
| 97 | #define KeccakP1600_SetBytesInLaneToZero _PySHA3_KeccakP1600_SetBytesInLaneToZero |
| 98 | #endif |
| 99 | |
| 100 | /* we are only interested in KeccakP1600 */ |
| 101 | #define KeccakP200_excluded 1 |
| 102 | #define KeccakP400_excluded 1 |
| 103 | #define KeccakP800_excluded 1 |
| 104 | |
| 105 | /* inline all Keccak dependencies */ |
| 106 | #include "kcp/KeccakHash.h" |
| 107 | #include "kcp/KeccakSponge.h" |
| 108 | #include "kcp/KeccakHash.c" |
| 109 | #include "kcp/KeccakSponge.c" |
| 110 | #if KeccakOpt == 64 |
| 111 | #include "kcp/KeccakP-1600-opt64.c" |
| 112 | #elif KeccakOpt == 32 |
| 113 | #include "kcp/KeccakP-1600-inplace32BI.c" |
| 114 | #endif |
| 115 | |
| 116 | #define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */ |
Christian Heimes | c71ec8a | 2016-09-08 15:04:38 +0200 | [diff] [blame] | 117 | #define SHA3_LANESIZE (20 * 8) /* ExtractLane needs max uint64_t[20] extra. */ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 118 | #define SHA3_state Keccak_HashInstance |
| 119 | #define SHA3_init Keccak_HashInitialize |
| 120 | #define SHA3_process Keccak_HashUpdate |
| 121 | #define SHA3_done Keccak_HashFinal |
| 122 | #define SHA3_squeeze Keccak_HashSqueeze |
| 123 | #define SHA3_copystate(dest, src) memcpy(&(dest), &(src), sizeof(SHA3_state)) |
| 124 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 125 | typedef struct { |
| 126 | PyTypeObject *sha3_224_type; |
| 127 | PyTypeObject *sha3_256_type; |
| 128 | PyTypeObject *sha3_384_type; |
| 129 | PyTypeObject *sha3_512_type; |
| 130 | #ifdef PY_WITH_KECCAK |
| 131 | PyTypeObject *keccak_224_type; |
| 132 | PyTypeObject *keccak_256_type; |
| 133 | PyTypeObject *keccak_384_type; |
| 134 | PyTypeObject *keccak_512_type; |
| 135 | #endif |
| 136 | PyTypeObject *shake_128_type; |
| 137 | PyTypeObject *shake_256_type; |
| 138 | } SHA3State; |
| 139 | |
| 140 | static inline SHA3State* |
| 141 | sha3_get_state(PyObject *module) |
| 142 | { |
| 143 | void *state = PyModule_GetState(module); |
| 144 | assert(state != NULL); |
| 145 | return (SHA3State *)state; |
| 146 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 147 | |
| 148 | /*[clinic input] |
| 149 | module _sha3 |
| 150 | class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ" |
| 151 | class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ" |
| 152 | class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ" |
| 153 | class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ" |
| 154 | class _sha3.shake_128 "SHA3object *" "&SHAKE128type" |
| 155 | class _sha3.shake_256 "SHA3object *" "&SHAKE256type" |
| 156 | [clinic start generated code]*/ |
| 157 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/ |
| 158 | |
| 159 | /* The structure for storing SHA3 info */ |
| 160 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 161 | typedef struct { |
| 162 | PyObject_HEAD |
| 163 | SHA3_state hash_state; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 164 | PyThread_type_lock lock; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 165 | } SHA3object; |
| 166 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 167 | #include "clinic/sha3module.c.h" |
| 168 | |
| 169 | static SHA3object * |
| 170 | newSHA3object(PyTypeObject *type) |
| 171 | { |
| 172 | SHA3object *newobj; |
| 173 | newobj = (SHA3object *)PyObject_New(SHA3object, type); |
| 174 | if (newobj == NULL) { |
| 175 | return NULL; |
| 176 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 177 | newobj->lock = NULL; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 178 | return newobj; |
| 179 | } |
| 180 | |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 181 | /*[clinic input] |
| 182 | @classmethod |
| 183 | _sha3.sha3_224.__new__ as py_sha3_new |
| 184 | data: object(c_default="NULL") = b'' |
| 185 | / |
| 186 | * |
| 187 | usedforsecurity: bool = True |
| 188 | |
| 189 | Return a new BLAKE2b hash object. |
| 190 | [clinic start generated code]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 191 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 192 | static PyObject * |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 193 | py_sha3_new_impl(PyTypeObject *type, PyObject *data, int usedforsecurity) |
| 194 | /*[clinic end generated code: output=90409addc5d5e8b0 input=bcfcdf2e4368347a]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 195 | { |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 196 | SHA3object *self = newSHA3object(type); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 197 | if (self == NULL) { |
| 198 | goto error; |
| 199 | } |
| 200 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 201 | SHA3State *state = PyType_GetModuleState(type); |
| 202 | assert(state != NULL); |
| 203 | |
| 204 | HashReturn res; |
| 205 | if (type == state->sha3_224_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 206 | res = Keccak_HashInitialize_SHA3_224(&self->hash_state); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 207 | } else if (type == state->sha3_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 208 | res = Keccak_HashInitialize_SHA3_256(&self->hash_state); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 209 | } else if (type == state->sha3_384_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 210 | res = Keccak_HashInitialize_SHA3_384(&self->hash_state); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 211 | } else if (type == state->sha3_512_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 212 | res = Keccak_HashInitialize_SHA3_512(&self->hash_state); |
| 213 | #ifdef PY_WITH_KECCAK |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 214 | } else if (type == state->keccak_224_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 215 | res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 216 | } else if (type == state->keccak_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 217 | res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 218 | } else if (type == state->keccak_384_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 219 | res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 220 | } else if (type == state->keccak_512_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 221 | res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01); |
| 222 | #endif |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 223 | } else if (type == state->shake_128_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 224 | res = Keccak_HashInitialize_SHAKE128(&self->hash_state); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 225 | } else if (type == state->shake_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 226 | res = Keccak_HashInitialize_SHAKE256(&self->hash_state); |
| 227 | } else { |
| 228 | PyErr_BadInternalCall(); |
| 229 | goto error; |
| 230 | } |
| 231 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 232 | Py_buffer buf = {NULL, NULL}; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 233 | if (data) { |
| 234 | GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 235 | if (buf.len >= HASHLIB_GIL_MINSIZE) { |
| 236 | /* invariant: New objects can't be accessed by other code yet, |
| 237 | * thus it's safe to release the GIL without locking the object. |
| 238 | */ |
| 239 | Py_BEGIN_ALLOW_THREADS |
| 240 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 241 | Py_END_ALLOW_THREADS |
| 242 | } |
| 243 | else { |
| 244 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 245 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 246 | if (res != SUCCESS) { |
| 247 | PyErr_SetString(PyExc_RuntimeError, |
| 248 | "internal error in SHA3 Update()"); |
| 249 | goto error; |
| 250 | } |
| 251 | PyBuffer_Release(&buf); |
| 252 | } |
| 253 | |
| 254 | return (PyObject *)self; |
| 255 | |
| 256 | error: |
| 257 | if (self) { |
| 258 | Py_DECREF(self); |
| 259 | } |
| 260 | if (data && buf.obj) { |
| 261 | PyBuffer_Release(&buf); |
| 262 | } |
| 263 | return NULL; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /* Internal methods for a hash object */ |
| 268 | |
| 269 | static void |
| 270 | SHA3_dealloc(SHA3object *self) |
| 271 | { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 272 | if (self->lock) { |
| 273 | PyThread_free_lock(self->lock); |
| 274 | } |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 275 | |
| 276 | PyTypeObject *tp = Py_TYPE(self); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 277 | PyObject_Free(self); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 278 | Py_DECREF(tp); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | |
| 282 | /* External methods for a hash object */ |
| 283 | |
| 284 | |
| 285 | /*[clinic input] |
| 286 | _sha3.sha3_224.copy |
| 287 | |
| 288 | Return a copy of the hash object. |
| 289 | [clinic start generated code]*/ |
| 290 | |
| 291 | static PyObject * |
| 292 | _sha3_sha3_224_copy_impl(SHA3object *self) |
| 293 | /*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/ |
| 294 | { |
| 295 | SHA3object *newobj; |
| 296 | |
| 297 | if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) { |
| 298 | return NULL; |
| 299 | } |
| 300 | ENTER_HASHLIB(self); |
| 301 | SHA3_copystate(newobj->hash_state, self->hash_state); |
| 302 | LEAVE_HASHLIB(self); |
| 303 | return (PyObject *)newobj; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /*[clinic input] |
| 308 | _sha3.sha3_224.digest |
| 309 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 310 | Return the digest value as a bytes object. |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 311 | [clinic start generated code]*/ |
| 312 | |
| 313 | static PyObject * |
| 314 | _sha3_sha3_224_digest_impl(SHA3object *self) |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 315 | /*[clinic end generated code: output=fd531842e20b2d5b input=5b2a659536bbd248]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 316 | { |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 317 | unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE]; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 318 | SHA3_state temp; |
| 319 | HashReturn res; |
| 320 | |
| 321 | ENTER_HASHLIB(self); |
| 322 | SHA3_copystate(temp, self->hash_state); |
| 323 | LEAVE_HASHLIB(self); |
| 324 | res = SHA3_done(&temp, digest); |
| 325 | if (res != SUCCESS) { |
| 326 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); |
| 327 | return NULL; |
| 328 | } |
| 329 | return PyBytes_FromStringAndSize((const char *)digest, |
| 330 | self->hash_state.fixedOutputLength / 8); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /*[clinic input] |
| 335 | _sha3.sha3_224.hexdigest |
| 336 | |
| 337 | Return the digest value as a string of hexadecimal digits. |
| 338 | [clinic start generated code]*/ |
| 339 | |
| 340 | static PyObject * |
| 341 | _sha3_sha3_224_hexdigest_impl(SHA3object *self) |
| 342 | /*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/ |
| 343 | { |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 344 | unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE]; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 345 | SHA3_state temp; |
| 346 | HashReturn res; |
| 347 | |
| 348 | /* Get the raw (binary) digest value */ |
| 349 | ENTER_HASHLIB(self); |
| 350 | SHA3_copystate(temp, self->hash_state); |
| 351 | LEAVE_HASHLIB(self); |
| 352 | res = SHA3_done(&temp, digest); |
| 353 | if (res != SUCCESS) { |
| 354 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); |
| 355 | return NULL; |
| 356 | } |
| 357 | return _Py_strhex((const char *)digest, |
| 358 | self->hash_state.fixedOutputLength / 8); |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /*[clinic input] |
| 363 | _sha3.sha3_224.update |
| 364 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 365 | data: object |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 366 | / |
| 367 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 368 | Update this hash object's state with the provided bytes-like object. |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 369 | [clinic start generated code]*/ |
| 370 | |
| 371 | static PyObject * |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 372 | _sha3_sha3_224_update(SHA3object *self, PyObject *data) |
| 373 | /*[clinic end generated code: output=d3223352286ed357 input=a887f54dcc4ae227]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 374 | { |
| 375 | Py_buffer buf; |
| 376 | HashReturn res; |
| 377 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 378 | GET_BUFFER_VIEW_OR_ERROUT(data, &buf); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 379 | |
| 380 | /* add new data, the function takes the length in bits not bytes */ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 381 | if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) { |
| 382 | self->lock = PyThread_allocate_lock(); |
| 383 | } |
| 384 | /* Once a lock exists all code paths must be synchronized. We have to |
| 385 | * release the GIL even for small buffers as acquiring the lock may take |
| 386 | * an unlimited amount of time when another thread updates this object |
| 387 | * with lots of data. */ |
| 388 | if (self->lock) { |
| 389 | Py_BEGIN_ALLOW_THREADS |
| 390 | PyThread_acquire_lock(self->lock, 1); |
| 391 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 392 | PyThread_release_lock(self->lock); |
| 393 | Py_END_ALLOW_THREADS |
| 394 | } |
| 395 | else { |
| 396 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 397 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 398 | |
| 399 | if (res != SUCCESS) { |
| 400 | PyBuffer_Release(&buf); |
| 401 | PyErr_SetString(PyExc_RuntimeError, |
| 402 | "internal error in SHA3 Update()"); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | PyBuffer_Release(&buf); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 407 | Py_RETURN_NONE; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | |
| 411 | static PyMethodDef SHA3_methods[] = { |
| 412 | _SHA3_SHA3_224_COPY_METHODDEF |
| 413 | _SHA3_SHA3_224_DIGEST_METHODDEF |
| 414 | _SHA3_SHA3_224_HEXDIGEST_METHODDEF |
| 415 | _SHA3_SHA3_224_UPDATE_METHODDEF |
| 416 | {NULL, NULL} /* sentinel */ |
| 417 | }; |
| 418 | |
| 419 | |
| 420 | static PyObject * |
| 421 | SHA3_get_block_size(SHA3object *self, void *closure) |
| 422 | { |
| 423 | int rate = self->hash_state.sponge.rate; |
| 424 | return PyLong_FromLong(rate / 8); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | static PyObject * |
| 429 | SHA3_get_name(SHA3object *self, void *closure) |
| 430 | { |
| 431 | PyTypeObject *type = Py_TYPE(self); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 432 | |
| 433 | SHA3State *state = PyType_GetModuleState(type); |
| 434 | assert(state != NULL); |
| 435 | |
| 436 | if (type == state->sha3_224_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 437 | return PyUnicode_FromString("sha3_224"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 438 | } else if (type == state->sha3_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 439 | return PyUnicode_FromString("sha3_256"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 440 | } else if (type == state->sha3_384_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 441 | return PyUnicode_FromString("sha3_384"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 442 | } else if (type == state->sha3_512_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 443 | return PyUnicode_FromString("sha3_512"); |
| 444 | #ifdef PY_WITH_KECCAK |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 445 | } else if (type == state->keccak_224_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 446 | return PyUnicode_FromString("keccak_224"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 447 | } else if (type == state->keccak_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 448 | return PyUnicode_FromString("keccak_256"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 449 | } else if (type == state->keccak_384_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 450 | return PyUnicode_FromString("keccak_384"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 451 | } else if (type == state->keccak_512_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 452 | return PyUnicode_FromString("keccak_512"); |
| 453 | #endif |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 454 | } else if (type == state->shake_128_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 455 | return PyUnicode_FromString("shake_128"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 456 | } else if (type == state->shake_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 457 | return PyUnicode_FromString("shake_256"); |
| 458 | } else { |
| 459 | PyErr_BadInternalCall(); |
| 460 | return NULL; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | |
| 465 | static PyObject * |
| 466 | SHA3_get_digest_size(SHA3object *self, void *closure) |
| 467 | { |
| 468 | return PyLong_FromLong(self->hash_state.fixedOutputLength / 8); |
| 469 | } |
| 470 | |
| 471 | |
| 472 | static PyObject * |
| 473 | SHA3_get_capacity_bits(SHA3object *self, void *closure) |
| 474 | { |
| 475 | int capacity = 1600 - self->hash_state.sponge.rate; |
| 476 | return PyLong_FromLong(capacity); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | static PyObject * |
| 481 | SHA3_get_rate_bits(SHA3object *self, void *closure) |
| 482 | { |
| 483 | unsigned int rate = self->hash_state.sponge.rate; |
| 484 | return PyLong_FromLong(rate); |
| 485 | } |
| 486 | |
| 487 | static PyObject * |
| 488 | SHA3_get_suffix(SHA3object *self, void *closure) |
| 489 | { |
| 490 | unsigned char suffix[2]; |
| 491 | suffix[0] = self->hash_state.delimitedSuffix; |
| 492 | suffix[1] = 0; |
| 493 | return PyBytes_FromStringAndSize((const char *)suffix, 1); |
| 494 | } |
| 495 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 496 | static PyGetSetDef SHA3_getseters[] = { |
| 497 | {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL}, |
| 498 | {"name", (getter)SHA3_get_name, NULL, NULL, NULL}, |
| 499 | {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL}, |
| 500 | {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL}, |
| 501 | {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL}, |
| 502 | {"_suffix", (getter)SHA3_get_suffix, NULL, NULL, NULL}, |
| 503 | {NULL} /* Sentinel */ |
| 504 | }; |
| 505 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 506 | #define SHA3_TYPE_SLOTS(type_slots_obj, type_doc, type_methods) \ |
| 507 | static PyType_Slot type_slots_obj[] = { \ |
| 508 | {Py_tp_dealloc, SHA3_dealloc}, \ |
| 509 | {Py_tp_doc, (char*)type_doc}, \ |
| 510 | {Py_tp_methods, type_methods}, \ |
| 511 | {Py_tp_getset, SHA3_getseters}, \ |
| 512 | {Py_tp_new, py_sha3_new}, \ |
| 513 | {0,0} \ |
| 514 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 515 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 516 | // Using PyType_GetModuleState() on these types is safe since they |
| 517 | // cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag. |
| 518 | #define SHA3_TYPE_SPEC(type_spec_obj, type_name, type_slots) \ |
| 519 | static PyType_Spec type_spec_obj = { \ |
| 520 | .name = "_sha3." type_name, \ |
| 521 | .basicsize = sizeof(SHA3object), \ |
| 522 | .flags = Py_TPFLAGS_DEFAULT, \ |
| 523 | .slots = type_slots \ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 526 | PyDoc_STRVAR(sha3_224__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 527 | "sha3_224([data], *, usedforsecurity=True) -> SHA3 object\n\ |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 528 | \n\ |
| 529 | Return a new SHA3 hash object with a hashbit length of 28 bytes."); |
| 530 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 531 | PyDoc_STRVAR(sha3_256__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 532 | "sha3_256([data], *, usedforsecurity=True) -> SHA3 object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 533 | \n\ |
| 534 | Return a new SHA3 hash object with a hashbit length of 32 bytes."); |
| 535 | |
| 536 | PyDoc_STRVAR(sha3_384__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 537 | "sha3_384([data], *, usedforsecurity=True) -> SHA3 object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 538 | \n\ |
| 539 | Return a new SHA3 hash object with a hashbit length of 48 bytes."); |
| 540 | |
| 541 | PyDoc_STRVAR(sha3_512__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 542 | "sha3_512([data], *, usedforsecurity=True) -> SHA3 object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 543 | \n\ |
| 544 | Return a new SHA3 hash object with a hashbit length of 64 bytes."); |
| 545 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 546 | #ifdef PY_WITH_KECCAK |
| 547 | PyDoc_STRVAR(keccak_224__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 548 | "keccak_224([data], *, usedforsecurity=True) -> Keccak object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 549 | \n\ |
| 550 | Return a new Keccak hash object with a hashbit length of 28 bytes."); |
| 551 | |
| 552 | PyDoc_STRVAR(keccak_256__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 553 | "keccak_256([data], *, usedforsecurity=True) -> Keccak object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 554 | \n\ |
| 555 | Return a new Keccak hash object with a hashbit length of 32 bytes."); |
| 556 | |
| 557 | PyDoc_STRVAR(keccak_384__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 558 | "keccak_384([data], *, usedforsecurity=True) -> Keccak object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 559 | \n\ |
| 560 | Return a new Keccak hash object with a hashbit length of 48 bytes."); |
| 561 | |
| 562 | PyDoc_STRVAR(keccak_512__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 563 | "keccak_512([data], *, usedforsecurity=True) -> Keccak object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 564 | \n\ |
| 565 | Return a new Keccak hash object with a hashbit length of 64 bytes."); |
| 566 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 567 | #endif |
| 568 | |
| 569 | SHA3_TYPE_SLOTS(sha3_224_slots, sha3_224__doc__, SHA3_methods); |
| 570 | SHA3_TYPE_SPEC(sha3_224_spec, "sha3_224", sha3_224_slots); |
| 571 | |
| 572 | SHA3_TYPE_SLOTS(sha3_256_slots, sha3_256__doc__, SHA3_methods); |
| 573 | SHA3_TYPE_SPEC(sha3_256_spec, "sha3_256", sha3_256_slots); |
| 574 | |
| 575 | SHA3_TYPE_SLOTS(sha3_384_slots, sha3_384__doc__, SHA3_methods); |
| 576 | SHA3_TYPE_SPEC(sha3_384_spec, "sha3_384", sha3_384_slots); |
| 577 | |
| 578 | SHA3_TYPE_SLOTS(sha3_512_slots, sha3_512__doc__, SHA3_methods); |
| 579 | SHA3_TYPE_SPEC(sha3_512_spec, "sha3_512", sha3_512_slots); |
| 580 | |
| 581 | #ifdef PY_WITH_KECCAK |
| 582 | SHA3_TYPE_SLOTS(Keccak_224_slots, keccak_224__doc__, SHA3_methods); |
| 583 | SHA3_TYPE_SPEC(Keccak_224_spec, "keccak_224", Keccak_224_slots); |
| 584 | |
| 585 | SHA3_TYPE_SLOTS(Keccak_256_slots, keccak_256__doc__, SHA3_methods); |
| 586 | SHA3_TYPE_SPEC(Keccak_256_spec, "keccak_256", Keccak_256_slots); |
| 587 | |
| 588 | SHA3_TYPE_SLOTS(Keccak_384_slots, keccak_384__doc__, SHA3_methods); |
| 589 | SHA3_TYPE_SPEC(Keccak_384_spec, "keccak_384", Keccak_384_slots); |
| 590 | |
| 591 | SHA3_TYPE_SLOTS(Keccak_512_slots, keccak_512__doc__, SHA3_methods); |
| 592 | SHA3_TYPE_SPEC(Keccak_512_spec, "keccak_512", Keccak_512_slots); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 593 | #endif |
| 594 | |
| 595 | |
| 596 | static PyObject * |
| 597 | _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex) |
| 598 | { |
| 599 | unsigned char *digest = NULL; |
| 600 | SHA3_state temp; |
| 601 | int res; |
| 602 | PyObject *result = NULL; |
| 603 | |
Serhiy Storchaka | 9b8c2e7 | 2018-10-11 07:41:00 +0300 | [diff] [blame] | 604 | if (digestlen >= (1 << 29)) { |
| 605 | PyErr_SetString(PyExc_ValueError, "length is too large"); |
| 606 | return NULL; |
| 607 | } |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 608 | /* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and |
| 609 | * SHA3_LANESIZE extra space. |
| 610 | */ |
Christian Heimes | c71ec8a | 2016-09-08 15:04:38 +0200 | [diff] [blame] | 611 | digest = (unsigned char*)PyMem_Malloc(digestlen + SHA3_LANESIZE); |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 612 | if (digest == NULL) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 613 | return PyErr_NoMemory(); |
| 614 | } |
| 615 | |
| 616 | /* Get the raw (binary) digest value */ |
| 617 | ENTER_HASHLIB(self); |
| 618 | SHA3_copystate(temp, self->hash_state); |
| 619 | LEAVE_HASHLIB(self); |
| 620 | res = SHA3_done(&temp, NULL); |
| 621 | if (res != SUCCESS) { |
| 622 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 done()"); |
| 623 | goto error; |
| 624 | } |
| 625 | res = SHA3_squeeze(&temp, digest, digestlen * 8); |
| 626 | if (res != SUCCESS) { |
| 627 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Squeeze()"); |
| 628 | return NULL; |
| 629 | } |
| 630 | if (hex) { |
| 631 | result = _Py_strhex((const char *)digest, digestlen); |
| 632 | } else { |
| 633 | result = PyBytes_FromStringAndSize((const char *)digest, |
| 634 | digestlen); |
| 635 | } |
| 636 | error: |
| 637 | if (digest != NULL) { |
| 638 | PyMem_Free(digest); |
| 639 | } |
| 640 | return result; |
| 641 | } |
| 642 | |
| 643 | |
| 644 | /*[clinic input] |
| 645 | _sha3.shake_128.digest |
| 646 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 647 | length: unsigned_long |
| 648 | / |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 649 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 650 | Return the digest value as a bytes object. |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 651 | [clinic start generated code]*/ |
| 652 | |
| 653 | static PyObject * |
| 654 | _sha3_shake_128_digest_impl(SHA3object *self, unsigned long length) |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 655 | /*[clinic end generated code: output=2313605e2f87bb8f input=418ef6a36d2e6082]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 656 | { |
| 657 | return _SHAKE_digest(self, length, 0); |
| 658 | } |
| 659 | |
| 660 | |
| 661 | /*[clinic input] |
| 662 | _sha3.shake_128.hexdigest |
| 663 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 664 | length: unsigned_long |
| 665 | / |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 666 | |
| 667 | Return the digest value as a string of hexadecimal digits. |
| 668 | [clinic start generated code]*/ |
| 669 | |
| 670 | static PyObject * |
| 671 | _sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length) |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 672 | /*[clinic end generated code: output=bf8e2f1e490944a8 input=69fb29b0926ae321]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 673 | { |
| 674 | return _SHAKE_digest(self, length, 1); |
| 675 | } |
| 676 | |
| 677 | |
| 678 | static PyMethodDef SHAKE_methods[] = { |
| 679 | _SHA3_SHA3_224_COPY_METHODDEF |
| 680 | _SHA3_SHAKE_128_DIGEST_METHODDEF |
| 681 | _SHA3_SHAKE_128_HEXDIGEST_METHODDEF |
| 682 | _SHA3_SHA3_224_UPDATE_METHODDEF |
| 683 | {NULL, NULL} /* sentinel */ |
| 684 | }; |
| 685 | |
| 686 | PyDoc_STRVAR(shake_128__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 687 | "shake_128([data], *, usedforsecurity=True) -> SHAKE object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 688 | \n\ |
| 689 | Return a new SHAKE hash object."); |
| 690 | |
| 691 | PyDoc_STRVAR(shake_256__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 692 | "shake_256([data], *, usedforsecurity=True) -> SHAKE object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 693 | \n\ |
| 694 | Return a new SHAKE hash object."); |
| 695 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 696 | SHA3_TYPE_SLOTS(SHAKE128slots, shake_128__doc__, SHAKE_methods); |
| 697 | SHA3_TYPE_SPEC(SHAKE128_spec, "shake_128", SHAKE128slots); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 698 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 699 | SHA3_TYPE_SLOTS(SHAKE256slots, shake_256__doc__, SHAKE_methods); |
| 700 | SHA3_TYPE_SPEC(SHAKE256_spec, "shake_256", SHAKE256slots); |
| 701 | |
| 702 | |
| 703 | static int |
| 704 | _sha3_traverse(PyObject *module, visitproc visit, void *arg) |
| 705 | { |
| 706 | SHA3State *state = sha3_get_state(module); |
| 707 | Py_VISIT(state->sha3_224_type); |
| 708 | Py_VISIT(state->sha3_256_type); |
| 709 | Py_VISIT(state->sha3_384_type); |
| 710 | Py_VISIT(state->sha3_512_type); |
| 711 | #ifdef PY_WITH_KECCAK |
| 712 | Py_VISIT(state->keccak_224_type); |
| 713 | Py_VISIT(state->keccak_256_type); |
| 714 | Py_VISIT(state->keccak_384_type); |
| 715 | Py_VISIT(state->keccak_512_type); |
| 716 | #endif |
| 717 | Py_VISIT(state->shake_128_type); |
| 718 | Py_VISIT(state->shake_256_type); |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static int |
| 723 | _sha3_clear(PyObject *module) |
| 724 | { |
| 725 | SHA3State *state = sha3_get_state(module); |
| 726 | Py_CLEAR(state->sha3_224_type); |
| 727 | Py_CLEAR(state->sha3_256_type); |
| 728 | Py_CLEAR(state->sha3_384_type); |
| 729 | Py_CLEAR(state->sha3_512_type); |
| 730 | #ifdef PY_WITH_KECCAK |
| 731 | Py_CLEAR(state->keccak_224_type); |
| 732 | Py_CLEAR(state->keccak_256_type); |
| 733 | Py_CLEAR(state->keccak_384_type); |
| 734 | Py_CLEAR(state->keccak_512_type); |
| 735 | #endif |
| 736 | Py_CLEAR(state->shake_128_type); |
| 737 | Py_CLEAR(state->shake_256_type); |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | static void |
| 742 | _sha3_free(void *module) |
| 743 | { |
| 744 | _sha3_clear((PyObject *)module); |
| 745 | } |
| 746 | |
| 747 | static int |
| 748 | _sha3_exec(PyObject *m) |
| 749 | { |
| 750 | SHA3State *st = sha3_get_state(m); |
| 751 | |
| 752 | #define init_sha3type(type, typespec) \ |
| 753 | do { \ |
| 754 | st->type = (PyTypeObject *)PyType_FromModuleAndSpec( \ |
| 755 | m, &typespec, NULL); \ |
| 756 | if (st->type == NULL) { \ |
| 757 | return -1; \ |
| 758 | } \ |
| 759 | if (PyModule_AddType(m, st->type) < 0) { \ |
| 760 | return -1; \ |
| 761 | } \ |
| 762 | } while(0) |
| 763 | |
| 764 | init_sha3type(sha3_224_type, sha3_224_spec); |
| 765 | init_sha3type(sha3_256_type, sha3_256_spec); |
| 766 | init_sha3type(sha3_384_type, sha3_384_spec); |
| 767 | init_sha3type(sha3_512_type, sha3_512_spec); |
| 768 | #ifdef PY_WITH_KECCAK |
| 769 | init_sha3type(keccak_224_type, Keccak_224_spec); |
| 770 | init_sha3type(keccak_256_type, Keccak_256_spec); |
| 771 | init_sha3type(keccak_384_type, Keccak_384_spec); |
| 772 | init_sha3type(keccak_512_type, Keccak_512_spec); |
| 773 | #endif |
| 774 | init_sha3type(shake_128_type, SHAKE128_spec); |
| 775 | init_sha3type(shake_256_type, SHAKE256_spec); |
| 776 | #undef init_sha3type |
| 777 | |
| 778 | if (PyModule_AddIntConstant(m, "keccakopt", KeccakOpt) < 0) { |
| 779 | return -1; |
| 780 | } |
| 781 | if (PyModule_AddStringConstant(m, "implementation", |
| 782 | KeccakP1600_implementation) < 0) { |
| 783 | return -1; |
| 784 | } |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | static PyModuleDef_Slot _sha3_slots[] = { |
| 790 | {Py_mod_exec, _sha3_exec}, |
| 791 | {0, NULL} |
| 792 | }; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 793 | |
| 794 | /* Initialize this module. */ |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 795 | static struct PyModuleDef _sha3module = { |
| 796 | PyModuleDef_HEAD_INIT, |
| 797 | .m_name = "_sha3", |
| 798 | .m_size = sizeof(SHA3State), |
| 799 | .m_slots = _sha3_slots, |
| 800 | .m_traverse = _sha3_traverse, |
| 801 | .m_clear = _sha3_clear, |
| 802 | .m_free = _sha3_free, |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 803 | }; |
| 804 | |
| 805 | |
| 806 | PyMODINIT_FUNC |
| 807 | PyInit__sha3(void) |
| 808 | { |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 809 | return PyModuleDef_Init(&_sha3module); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 810 | } |