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 | { |
Christian Heimes | aa6da32 | 2021-04-18 08:39:39 +0200 | [diff] [blame] | 196 | HashReturn res; |
| 197 | Py_buffer buf = {NULL, NULL}; |
| 198 | SHA3State *state = PyType_GetModuleState(type); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 199 | SHA3object *self = newSHA3object(type); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 200 | if (self == NULL) { |
| 201 | goto error; |
| 202 | } |
| 203 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 204 | assert(state != NULL); |
| 205 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 206 | if (type == state->sha3_224_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 207 | res = Keccak_HashInitialize_SHA3_224(&self->hash_state); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 208 | } else if (type == state->sha3_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 209 | res = Keccak_HashInitialize_SHA3_256(&self->hash_state); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 210 | } else if (type == state->sha3_384_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 211 | res = Keccak_HashInitialize_SHA3_384(&self->hash_state); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 212 | } else if (type == state->sha3_512_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 213 | res = Keccak_HashInitialize_SHA3_512(&self->hash_state); |
| 214 | #ifdef PY_WITH_KECCAK |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 215 | } else if (type == state->keccak_224_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 216 | res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 217 | } else if (type == state->keccak_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 218 | res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 219 | } else if (type == state->keccak_384_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 220 | res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 221 | } else if (type == state->keccak_512_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 222 | res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01); |
| 223 | #endif |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 224 | } else if (type == state->shake_128_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 225 | res = Keccak_HashInitialize_SHAKE128(&self->hash_state); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 226 | } else if (type == state->shake_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 227 | res = Keccak_HashInitialize_SHAKE256(&self->hash_state); |
| 228 | } else { |
| 229 | PyErr_BadInternalCall(); |
| 230 | goto error; |
| 231 | } |
| 232 | |
Christian Heimes | aa6da32 | 2021-04-18 08:39:39 +0200 | [diff] [blame] | 233 | if (res != SUCCESS) { |
| 234 | PyErr_SetString(PyExc_RuntimeError, |
| 235 | "internal error in SHA3 initialize()"); |
| 236 | goto error; |
| 237 | } |
| 238 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 239 | if (data) { |
| 240 | GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 241 | if (buf.len >= HASHLIB_GIL_MINSIZE) { |
| 242 | /* invariant: New objects can't be accessed by other code yet, |
| 243 | * thus it's safe to release the GIL without locking the object. |
| 244 | */ |
| 245 | Py_BEGIN_ALLOW_THREADS |
| 246 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 247 | Py_END_ALLOW_THREADS |
| 248 | } |
| 249 | else { |
| 250 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 251 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 252 | if (res != SUCCESS) { |
| 253 | PyErr_SetString(PyExc_RuntimeError, |
| 254 | "internal error in SHA3 Update()"); |
| 255 | goto error; |
| 256 | } |
| 257 | PyBuffer_Release(&buf); |
| 258 | } |
| 259 | |
| 260 | return (PyObject *)self; |
| 261 | |
| 262 | error: |
| 263 | if (self) { |
| 264 | Py_DECREF(self); |
| 265 | } |
| 266 | if (data && buf.obj) { |
| 267 | PyBuffer_Release(&buf); |
| 268 | } |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* Internal methods for a hash object */ |
| 274 | |
| 275 | static void |
| 276 | SHA3_dealloc(SHA3object *self) |
| 277 | { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 278 | if (self->lock) { |
| 279 | PyThread_free_lock(self->lock); |
| 280 | } |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 281 | |
| 282 | PyTypeObject *tp = Py_TYPE(self); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 283 | PyObject_Free(self); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 284 | Py_DECREF(tp); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
| 288 | /* External methods for a hash object */ |
| 289 | |
| 290 | |
| 291 | /*[clinic input] |
| 292 | _sha3.sha3_224.copy |
| 293 | |
| 294 | Return a copy of the hash object. |
| 295 | [clinic start generated code]*/ |
| 296 | |
| 297 | static PyObject * |
| 298 | _sha3_sha3_224_copy_impl(SHA3object *self) |
| 299 | /*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/ |
| 300 | { |
| 301 | SHA3object *newobj; |
| 302 | |
| 303 | if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) { |
| 304 | return NULL; |
| 305 | } |
| 306 | ENTER_HASHLIB(self); |
| 307 | SHA3_copystate(newobj->hash_state, self->hash_state); |
| 308 | LEAVE_HASHLIB(self); |
| 309 | return (PyObject *)newobj; |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /*[clinic input] |
| 314 | _sha3.sha3_224.digest |
| 315 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 316 | Return the digest value as a bytes object. |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 317 | [clinic start generated code]*/ |
| 318 | |
| 319 | static PyObject * |
| 320 | _sha3_sha3_224_digest_impl(SHA3object *self) |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 321 | /*[clinic end generated code: output=fd531842e20b2d5b input=5b2a659536bbd248]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 322 | { |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 323 | unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE]; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 324 | SHA3_state temp; |
| 325 | HashReturn res; |
| 326 | |
| 327 | ENTER_HASHLIB(self); |
| 328 | SHA3_copystate(temp, self->hash_state); |
| 329 | LEAVE_HASHLIB(self); |
| 330 | res = SHA3_done(&temp, digest); |
| 331 | if (res != SUCCESS) { |
| 332 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); |
| 333 | return NULL; |
| 334 | } |
| 335 | return PyBytes_FromStringAndSize((const char *)digest, |
| 336 | self->hash_state.fixedOutputLength / 8); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /*[clinic input] |
| 341 | _sha3.sha3_224.hexdigest |
| 342 | |
| 343 | Return the digest value as a string of hexadecimal digits. |
| 344 | [clinic start generated code]*/ |
| 345 | |
| 346 | static PyObject * |
| 347 | _sha3_sha3_224_hexdigest_impl(SHA3object *self) |
| 348 | /*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/ |
| 349 | { |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 350 | unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE]; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 351 | SHA3_state temp; |
| 352 | HashReturn res; |
| 353 | |
| 354 | /* Get the raw (binary) digest value */ |
| 355 | ENTER_HASHLIB(self); |
| 356 | SHA3_copystate(temp, self->hash_state); |
| 357 | LEAVE_HASHLIB(self); |
| 358 | res = SHA3_done(&temp, digest); |
| 359 | if (res != SUCCESS) { |
| 360 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); |
| 361 | return NULL; |
| 362 | } |
| 363 | return _Py_strhex((const char *)digest, |
| 364 | self->hash_state.fixedOutputLength / 8); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /*[clinic input] |
| 369 | _sha3.sha3_224.update |
| 370 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 371 | data: object |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 372 | / |
| 373 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 374 | Update this hash object's state with the provided bytes-like object. |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 375 | [clinic start generated code]*/ |
| 376 | |
| 377 | static PyObject * |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 378 | _sha3_sha3_224_update(SHA3object *self, PyObject *data) |
| 379 | /*[clinic end generated code: output=d3223352286ed357 input=a887f54dcc4ae227]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 380 | { |
| 381 | Py_buffer buf; |
| 382 | HashReturn res; |
| 383 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 384 | GET_BUFFER_VIEW_OR_ERROUT(data, &buf); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 385 | |
| 386 | /* add new data, the function takes the length in bits not bytes */ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 387 | if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) { |
| 388 | self->lock = PyThread_allocate_lock(); |
| 389 | } |
| 390 | /* Once a lock exists all code paths must be synchronized. We have to |
| 391 | * release the GIL even for small buffers as acquiring the lock may take |
| 392 | * an unlimited amount of time when another thread updates this object |
| 393 | * with lots of data. */ |
| 394 | if (self->lock) { |
| 395 | Py_BEGIN_ALLOW_THREADS |
| 396 | PyThread_acquire_lock(self->lock, 1); |
| 397 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 398 | PyThread_release_lock(self->lock); |
| 399 | Py_END_ALLOW_THREADS |
| 400 | } |
| 401 | else { |
| 402 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 403 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 404 | |
| 405 | if (res != SUCCESS) { |
| 406 | PyBuffer_Release(&buf); |
| 407 | PyErr_SetString(PyExc_RuntimeError, |
| 408 | "internal error in SHA3 Update()"); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | PyBuffer_Release(&buf); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 413 | Py_RETURN_NONE; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
| 417 | static PyMethodDef SHA3_methods[] = { |
| 418 | _SHA3_SHA3_224_COPY_METHODDEF |
| 419 | _SHA3_SHA3_224_DIGEST_METHODDEF |
| 420 | _SHA3_SHA3_224_HEXDIGEST_METHODDEF |
| 421 | _SHA3_SHA3_224_UPDATE_METHODDEF |
| 422 | {NULL, NULL} /* sentinel */ |
| 423 | }; |
| 424 | |
| 425 | |
| 426 | static PyObject * |
| 427 | SHA3_get_block_size(SHA3object *self, void *closure) |
| 428 | { |
| 429 | int rate = self->hash_state.sponge.rate; |
| 430 | return PyLong_FromLong(rate / 8); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | static PyObject * |
| 435 | SHA3_get_name(SHA3object *self, void *closure) |
| 436 | { |
| 437 | PyTypeObject *type = Py_TYPE(self); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 438 | |
| 439 | SHA3State *state = PyType_GetModuleState(type); |
| 440 | assert(state != NULL); |
| 441 | |
| 442 | if (type == state->sha3_224_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 443 | return PyUnicode_FromString("sha3_224"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 444 | } else if (type == state->sha3_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 445 | return PyUnicode_FromString("sha3_256"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 446 | } else if (type == state->sha3_384_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 447 | return PyUnicode_FromString("sha3_384"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 448 | } else if (type == state->sha3_512_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 449 | return PyUnicode_FromString("sha3_512"); |
| 450 | #ifdef PY_WITH_KECCAK |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 451 | } else if (type == state->keccak_224_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 452 | return PyUnicode_FromString("keccak_224"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 453 | } else if (type == state->keccak_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 454 | return PyUnicode_FromString("keccak_256"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 455 | } else if (type == state->keccak_384_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 456 | return PyUnicode_FromString("keccak_384"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 457 | } else if (type == state->keccak_512_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 458 | return PyUnicode_FromString("keccak_512"); |
| 459 | #endif |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 460 | } else if (type == state->shake_128_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 461 | return PyUnicode_FromString("shake_128"); |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 462 | } else if (type == state->shake_256_type) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 463 | return PyUnicode_FromString("shake_256"); |
| 464 | } else { |
| 465 | PyErr_BadInternalCall(); |
| 466 | return NULL; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | |
| 471 | static PyObject * |
| 472 | SHA3_get_digest_size(SHA3object *self, void *closure) |
| 473 | { |
| 474 | return PyLong_FromLong(self->hash_state.fixedOutputLength / 8); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | static PyObject * |
| 479 | SHA3_get_capacity_bits(SHA3object *self, void *closure) |
| 480 | { |
| 481 | int capacity = 1600 - self->hash_state.sponge.rate; |
| 482 | return PyLong_FromLong(capacity); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | static PyObject * |
| 487 | SHA3_get_rate_bits(SHA3object *self, void *closure) |
| 488 | { |
| 489 | unsigned int rate = self->hash_state.sponge.rate; |
| 490 | return PyLong_FromLong(rate); |
| 491 | } |
| 492 | |
| 493 | static PyObject * |
| 494 | SHA3_get_suffix(SHA3object *self, void *closure) |
| 495 | { |
| 496 | unsigned char suffix[2]; |
| 497 | suffix[0] = self->hash_state.delimitedSuffix; |
| 498 | suffix[1] = 0; |
| 499 | return PyBytes_FromStringAndSize((const char *)suffix, 1); |
| 500 | } |
| 501 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 502 | static PyGetSetDef SHA3_getseters[] = { |
| 503 | {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL}, |
| 504 | {"name", (getter)SHA3_get_name, NULL, NULL, NULL}, |
| 505 | {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL}, |
| 506 | {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL}, |
| 507 | {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL}, |
| 508 | {"_suffix", (getter)SHA3_get_suffix, NULL, NULL, NULL}, |
| 509 | {NULL} /* Sentinel */ |
| 510 | }; |
| 511 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 512 | #define SHA3_TYPE_SLOTS(type_slots_obj, type_doc, type_methods) \ |
| 513 | static PyType_Slot type_slots_obj[] = { \ |
| 514 | {Py_tp_dealloc, SHA3_dealloc}, \ |
| 515 | {Py_tp_doc, (char*)type_doc}, \ |
| 516 | {Py_tp_methods, type_methods}, \ |
| 517 | {Py_tp_getset, SHA3_getseters}, \ |
| 518 | {Py_tp_new, py_sha3_new}, \ |
| 519 | {0,0} \ |
| 520 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 521 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 522 | // Using PyType_GetModuleState() on these types is safe since they |
| 523 | // cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag. |
| 524 | #define SHA3_TYPE_SPEC(type_spec_obj, type_name, type_slots) \ |
| 525 | static PyType_Spec type_spec_obj = { \ |
| 526 | .name = "_sha3." type_name, \ |
| 527 | .basicsize = sizeof(SHA3object), \ |
Christian Heimes | 91554e4 | 2021-05-02 09:47:45 +0200 | [diff] [blame^] | 528 | .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, \ |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 529 | .slots = type_slots \ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 532 | PyDoc_STRVAR(sha3_224__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 533 | "sha3_224([data], *, usedforsecurity=True) -> SHA3 object\n\ |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 534 | \n\ |
| 535 | Return a new SHA3 hash object with a hashbit length of 28 bytes."); |
| 536 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 537 | PyDoc_STRVAR(sha3_256__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 538 | "sha3_256([data], *, usedforsecurity=True) -> SHA3 object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 539 | \n\ |
| 540 | Return a new SHA3 hash object with a hashbit length of 32 bytes."); |
| 541 | |
| 542 | PyDoc_STRVAR(sha3_384__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 543 | "sha3_384([data], *, usedforsecurity=True) -> SHA3 object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 544 | \n\ |
| 545 | Return a new SHA3 hash object with a hashbit length of 48 bytes."); |
| 546 | |
| 547 | PyDoc_STRVAR(sha3_512__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 548 | "sha3_512([data], *, usedforsecurity=True) -> SHA3 object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 549 | \n\ |
| 550 | Return a new SHA3 hash object with a hashbit length of 64 bytes."); |
| 551 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 552 | #ifdef PY_WITH_KECCAK |
| 553 | PyDoc_STRVAR(keccak_224__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 554 | "keccak_224([data], *, usedforsecurity=True) -> Keccak object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 555 | \n\ |
| 556 | Return a new Keccak hash object with a hashbit length of 28 bytes."); |
| 557 | |
| 558 | PyDoc_STRVAR(keccak_256__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 559 | "keccak_256([data], *, usedforsecurity=True) -> Keccak object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 560 | \n\ |
| 561 | Return a new Keccak hash object with a hashbit length of 32 bytes."); |
| 562 | |
| 563 | PyDoc_STRVAR(keccak_384__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 564 | "keccak_384([data], *, usedforsecurity=True) -> Keccak object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 565 | \n\ |
| 566 | Return a new Keccak hash object with a hashbit length of 48 bytes."); |
| 567 | |
| 568 | PyDoc_STRVAR(keccak_512__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 569 | "keccak_512([data], *, usedforsecurity=True) -> Keccak object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 570 | \n\ |
| 571 | Return a new Keccak hash object with a hashbit length of 64 bytes."); |
| 572 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 573 | #endif |
| 574 | |
| 575 | SHA3_TYPE_SLOTS(sha3_224_slots, sha3_224__doc__, SHA3_methods); |
| 576 | SHA3_TYPE_SPEC(sha3_224_spec, "sha3_224", sha3_224_slots); |
| 577 | |
| 578 | SHA3_TYPE_SLOTS(sha3_256_slots, sha3_256__doc__, SHA3_methods); |
| 579 | SHA3_TYPE_SPEC(sha3_256_spec, "sha3_256", sha3_256_slots); |
| 580 | |
| 581 | SHA3_TYPE_SLOTS(sha3_384_slots, sha3_384__doc__, SHA3_methods); |
| 582 | SHA3_TYPE_SPEC(sha3_384_spec, "sha3_384", sha3_384_slots); |
| 583 | |
| 584 | SHA3_TYPE_SLOTS(sha3_512_slots, sha3_512__doc__, SHA3_methods); |
| 585 | SHA3_TYPE_SPEC(sha3_512_spec, "sha3_512", sha3_512_slots); |
| 586 | |
| 587 | #ifdef PY_WITH_KECCAK |
| 588 | SHA3_TYPE_SLOTS(Keccak_224_slots, keccak_224__doc__, SHA3_methods); |
| 589 | SHA3_TYPE_SPEC(Keccak_224_spec, "keccak_224", Keccak_224_slots); |
| 590 | |
| 591 | SHA3_TYPE_SLOTS(Keccak_256_slots, keccak_256__doc__, SHA3_methods); |
| 592 | SHA3_TYPE_SPEC(Keccak_256_spec, "keccak_256", Keccak_256_slots); |
| 593 | |
| 594 | SHA3_TYPE_SLOTS(Keccak_384_slots, keccak_384__doc__, SHA3_methods); |
| 595 | SHA3_TYPE_SPEC(Keccak_384_spec, "keccak_384", Keccak_384_slots); |
| 596 | |
| 597 | SHA3_TYPE_SLOTS(Keccak_512_slots, keccak_512__doc__, SHA3_methods); |
| 598 | SHA3_TYPE_SPEC(Keccak_512_spec, "keccak_512", Keccak_512_slots); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 599 | #endif |
| 600 | |
| 601 | |
| 602 | static PyObject * |
| 603 | _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex) |
| 604 | { |
| 605 | unsigned char *digest = NULL; |
| 606 | SHA3_state temp; |
| 607 | int res; |
| 608 | PyObject *result = NULL; |
| 609 | |
Serhiy Storchaka | 9b8c2e7 | 2018-10-11 07:41:00 +0300 | [diff] [blame] | 610 | if (digestlen >= (1 << 29)) { |
| 611 | PyErr_SetString(PyExc_ValueError, "length is too large"); |
| 612 | return NULL; |
| 613 | } |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 614 | /* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and |
| 615 | * SHA3_LANESIZE extra space. |
| 616 | */ |
Christian Heimes | c71ec8a | 2016-09-08 15:04:38 +0200 | [diff] [blame] | 617 | digest = (unsigned char*)PyMem_Malloc(digestlen + SHA3_LANESIZE); |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 618 | if (digest == NULL) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 619 | return PyErr_NoMemory(); |
| 620 | } |
| 621 | |
| 622 | /* Get the raw (binary) digest value */ |
| 623 | ENTER_HASHLIB(self); |
| 624 | SHA3_copystate(temp, self->hash_state); |
| 625 | LEAVE_HASHLIB(self); |
| 626 | res = SHA3_done(&temp, NULL); |
| 627 | if (res != SUCCESS) { |
| 628 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 done()"); |
| 629 | goto error; |
| 630 | } |
| 631 | res = SHA3_squeeze(&temp, digest, digestlen * 8); |
| 632 | if (res != SUCCESS) { |
| 633 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Squeeze()"); |
| 634 | return NULL; |
| 635 | } |
| 636 | if (hex) { |
| 637 | result = _Py_strhex((const char *)digest, digestlen); |
| 638 | } else { |
| 639 | result = PyBytes_FromStringAndSize((const char *)digest, |
| 640 | digestlen); |
| 641 | } |
| 642 | error: |
| 643 | if (digest != NULL) { |
| 644 | PyMem_Free(digest); |
| 645 | } |
| 646 | return result; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | /*[clinic input] |
| 651 | _sha3.shake_128.digest |
| 652 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 653 | length: unsigned_long |
| 654 | / |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 655 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 656 | Return the digest value as a bytes object. |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 657 | [clinic start generated code]*/ |
| 658 | |
| 659 | static PyObject * |
| 660 | _sha3_shake_128_digest_impl(SHA3object *self, unsigned long length) |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 661 | /*[clinic end generated code: output=2313605e2f87bb8f input=418ef6a36d2e6082]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 662 | { |
| 663 | return _SHAKE_digest(self, length, 0); |
| 664 | } |
| 665 | |
| 666 | |
| 667 | /*[clinic input] |
| 668 | _sha3.shake_128.hexdigest |
| 669 | |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 670 | length: unsigned_long |
| 671 | / |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 672 | |
| 673 | Return the digest value as a string of hexadecimal digits. |
| 674 | [clinic start generated code]*/ |
| 675 | |
| 676 | static PyObject * |
| 677 | _sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length) |
Serhiy Storchaka | f1d36d8 | 2018-07-31 09:50:16 +0300 | [diff] [blame] | 678 | /*[clinic end generated code: output=bf8e2f1e490944a8 input=69fb29b0926ae321]*/ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 679 | { |
| 680 | return _SHAKE_digest(self, length, 1); |
| 681 | } |
| 682 | |
| 683 | |
| 684 | static PyMethodDef SHAKE_methods[] = { |
| 685 | _SHA3_SHA3_224_COPY_METHODDEF |
| 686 | _SHA3_SHAKE_128_DIGEST_METHODDEF |
| 687 | _SHA3_SHAKE_128_HEXDIGEST_METHODDEF |
| 688 | _SHA3_SHA3_224_UPDATE_METHODDEF |
| 689 | {NULL, NULL} /* sentinel */ |
| 690 | }; |
| 691 | |
| 692 | PyDoc_STRVAR(shake_128__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 693 | "shake_128([data], *, usedforsecurity=True) -> SHAKE object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 694 | \n\ |
| 695 | Return a new SHAKE hash object."); |
| 696 | |
| 697 | PyDoc_STRVAR(shake_256__doc__, |
Christian Heimes | 7cad53e | 2019-09-13 02:30:00 +0200 | [diff] [blame] | 698 | "shake_256([data], *, usedforsecurity=True) -> SHAKE object\n\ |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 699 | \n\ |
| 700 | Return a new SHAKE hash object."); |
| 701 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 702 | SHA3_TYPE_SLOTS(SHAKE128slots, shake_128__doc__, SHAKE_methods); |
| 703 | SHA3_TYPE_SPEC(SHAKE128_spec, "shake_128", SHAKE128slots); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 704 | |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 705 | SHA3_TYPE_SLOTS(SHAKE256slots, shake_256__doc__, SHAKE_methods); |
| 706 | SHA3_TYPE_SPEC(SHAKE256_spec, "shake_256", SHAKE256slots); |
| 707 | |
| 708 | |
| 709 | static int |
| 710 | _sha3_traverse(PyObject *module, visitproc visit, void *arg) |
| 711 | { |
| 712 | SHA3State *state = sha3_get_state(module); |
| 713 | Py_VISIT(state->sha3_224_type); |
| 714 | Py_VISIT(state->sha3_256_type); |
| 715 | Py_VISIT(state->sha3_384_type); |
| 716 | Py_VISIT(state->sha3_512_type); |
| 717 | #ifdef PY_WITH_KECCAK |
| 718 | Py_VISIT(state->keccak_224_type); |
| 719 | Py_VISIT(state->keccak_256_type); |
| 720 | Py_VISIT(state->keccak_384_type); |
| 721 | Py_VISIT(state->keccak_512_type); |
| 722 | #endif |
| 723 | Py_VISIT(state->shake_128_type); |
| 724 | Py_VISIT(state->shake_256_type); |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int |
| 729 | _sha3_clear(PyObject *module) |
| 730 | { |
| 731 | SHA3State *state = sha3_get_state(module); |
| 732 | Py_CLEAR(state->sha3_224_type); |
| 733 | Py_CLEAR(state->sha3_256_type); |
| 734 | Py_CLEAR(state->sha3_384_type); |
| 735 | Py_CLEAR(state->sha3_512_type); |
| 736 | #ifdef PY_WITH_KECCAK |
| 737 | Py_CLEAR(state->keccak_224_type); |
| 738 | Py_CLEAR(state->keccak_256_type); |
| 739 | Py_CLEAR(state->keccak_384_type); |
| 740 | Py_CLEAR(state->keccak_512_type); |
| 741 | #endif |
| 742 | Py_CLEAR(state->shake_128_type); |
| 743 | Py_CLEAR(state->shake_256_type); |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | static void |
| 748 | _sha3_free(void *module) |
| 749 | { |
| 750 | _sha3_clear((PyObject *)module); |
| 751 | } |
| 752 | |
| 753 | static int |
| 754 | _sha3_exec(PyObject *m) |
| 755 | { |
| 756 | SHA3State *st = sha3_get_state(m); |
| 757 | |
| 758 | #define init_sha3type(type, typespec) \ |
| 759 | do { \ |
| 760 | st->type = (PyTypeObject *)PyType_FromModuleAndSpec( \ |
| 761 | m, &typespec, NULL); \ |
| 762 | if (st->type == NULL) { \ |
| 763 | return -1; \ |
| 764 | } \ |
| 765 | if (PyModule_AddType(m, st->type) < 0) { \ |
| 766 | return -1; \ |
| 767 | } \ |
| 768 | } while(0) |
| 769 | |
| 770 | init_sha3type(sha3_224_type, sha3_224_spec); |
| 771 | init_sha3type(sha3_256_type, sha3_256_spec); |
| 772 | init_sha3type(sha3_384_type, sha3_384_spec); |
| 773 | init_sha3type(sha3_512_type, sha3_512_spec); |
| 774 | #ifdef PY_WITH_KECCAK |
| 775 | init_sha3type(keccak_224_type, Keccak_224_spec); |
| 776 | init_sha3type(keccak_256_type, Keccak_256_spec); |
| 777 | init_sha3type(keccak_384_type, Keccak_384_spec); |
| 778 | init_sha3type(keccak_512_type, Keccak_512_spec); |
| 779 | #endif |
| 780 | init_sha3type(shake_128_type, SHAKE128_spec); |
| 781 | init_sha3type(shake_256_type, SHAKE256_spec); |
| 782 | #undef init_sha3type |
| 783 | |
| 784 | if (PyModule_AddIntConstant(m, "keccakopt", KeccakOpt) < 0) { |
| 785 | return -1; |
| 786 | } |
| 787 | if (PyModule_AddStringConstant(m, "implementation", |
| 788 | KeccakP1600_implementation) < 0) { |
| 789 | return -1; |
| 790 | } |
| 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | static PyModuleDef_Slot _sha3_slots[] = { |
| 796 | {Py_mod_exec, _sha3_exec}, |
| 797 | {0, NULL} |
| 798 | }; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 799 | |
| 800 | /* Initialize this module. */ |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 801 | static struct PyModuleDef _sha3module = { |
| 802 | PyModuleDef_HEAD_INIT, |
| 803 | .m_name = "_sha3", |
| 804 | .m_size = sizeof(SHA3State), |
| 805 | .m_slots = _sha3_slots, |
| 806 | .m_traverse = _sha3_traverse, |
| 807 | .m_clear = _sha3_clear, |
| 808 | .m_free = _sha3_free, |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 809 | }; |
| 810 | |
| 811 | |
| 812 | PyMODINIT_FUNC |
| 813 | PyInit__sha3(void) |
| 814 | { |
Mohamed Koubaa | 93d50a6 | 2020-09-02 04:55:19 -0500 | [diff] [blame] | 815 | return PyModuleDef_Init(&_sha3module); |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 816 | } |