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