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 |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 43 | #elif SIZEOF_VOID_P == 8 && defined(PY_UINT64_T) |
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 | |
| 51 | #if KeccakOpt == 64 && defined(PY_UINT64_T) |
| 52 | /* 64bit platforms with unsigned int64 */ |
| 53 | typedef PY_UINT64_T UINT64; |
| 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 | |
| 125 | |
| 126 | /*[clinic input] |
| 127 | module _sha3 |
| 128 | class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ" |
| 129 | class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ" |
| 130 | class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ" |
| 131 | class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ" |
| 132 | class _sha3.shake_128 "SHA3object *" "&SHAKE128type" |
| 133 | class _sha3.shake_256 "SHA3object *" "&SHAKE256type" |
| 134 | [clinic start generated code]*/ |
| 135 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/ |
| 136 | |
| 137 | /* The structure for storing SHA3 info */ |
| 138 | |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 139 | typedef struct { |
| 140 | PyObject_HEAD |
| 141 | SHA3_state hash_state; |
| 142 | #ifdef WITH_THREAD |
| 143 | PyThread_type_lock lock; |
| 144 | #endif |
| 145 | } SHA3object; |
| 146 | |
| 147 | static PyTypeObject SHA3_224type; |
| 148 | static PyTypeObject SHA3_256type; |
| 149 | static PyTypeObject SHA3_384type; |
| 150 | static PyTypeObject SHA3_512type; |
| 151 | #ifdef PY_WITH_KECCAK |
| 152 | static PyTypeObject Keccak_224type; |
| 153 | static PyTypeObject Keccak_256type; |
| 154 | static PyTypeObject Keccak_384type; |
| 155 | static PyTypeObject Keccak_512type; |
| 156 | #endif |
| 157 | static PyTypeObject SHAKE128type; |
| 158 | static PyTypeObject SHAKE256type; |
| 159 | |
| 160 | #include "clinic/sha3module.c.h" |
| 161 | |
| 162 | static SHA3object * |
| 163 | newSHA3object(PyTypeObject *type) |
| 164 | { |
| 165 | SHA3object *newobj; |
| 166 | newobj = (SHA3object *)PyObject_New(SHA3object, type); |
| 167 | if (newobj == NULL) { |
| 168 | return NULL; |
| 169 | } |
| 170 | #ifdef WITH_THREAD |
| 171 | newobj->lock = NULL; |
| 172 | #endif |
| 173 | return newobj; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /*[clinic input] |
| 178 | @classmethod |
| 179 | _sha3.sha3_224.__new__ as py_sha3_new |
| 180 | string as data: object = NULL |
| 181 | |
| 182 | Return a new SHA3 hash object with a hashbit length of 28 bytes. |
| 183 | [clinic start generated code]*/ |
| 184 | |
| 185 | static PyObject * |
| 186 | py_sha3_new_impl(PyTypeObject *type, PyObject *data) |
| 187 | /*[clinic end generated code: output=8d5c34279e69bf09 input=d7c582b950a858b6]*/ |
| 188 | { |
| 189 | SHA3object *self = NULL; |
| 190 | Py_buffer buf = {NULL, NULL}; |
| 191 | HashReturn res; |
| 192 | |
| 193 | self = newSHA3object(type); |
| 194 | if (self == NULL) { |
| 195 | goto error; |
| 196 | } |
| 197 | |
| 198 | if (type == &SHA3_224type) { |
| 199 | res = Keccak_HashInitialize_SHA3_224(&self->hash_state); |
| 200 | } else if (type == &SHA3_256type) { |
| 201 | res = Keccak_HashInitialize_SHA3_256(&self->hash_state); |
| 202 | } else if (type == &SHA3_384type) { |
| 203 | res = Keccak_HashInitialize_SHA3_384(&self->hash_state); |
| 204 | } else if (type == &SHA3_512type) { |
| 205 | res = Keccak_HashInitialize_SHA3_512(&self->hash_state); |
| 206 | #ifdef PY_WITH_KECCAK |
| 207 | } else if (type == &Keccak_224type) { |
| 208 | res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01); |
| 209 | } else if (type == &Keccak_256type) { |
| 210 | res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01); |
| 211 | } else if (type == &Keccak_384type) { |
| 212 | res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01); |
| 213 | } else if (type == &Keccak_512type) { |
| 214 | res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01); |
| 215 | #endif |
| 216 | } else if (type == &SHAKE128type) { |
| 217 | res = Keccak_HashInitialize_SHAKE128(&self->hash_state); |
| 218 | } else if (type == &SHAKE256type) { |
| 219 | res = Keccak_HashInitialize_SHAKE256(&self->hash_state); |
| 220 | } else { |
| 221 | PyErr_BadInternalCall(); |
| 222 | goto error; |
| 223 | } |
| 224 | |
| 225 | if (data) { |
| 226 | GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error); |
| 227 | #ifdef WITH_THREAD |
| 228 | if (buf.len >= HASHLIB_GIL_MINSIZE) { |
| 229 | /* invariant: New objects can't be accessed by other code yet, |
| 230 | * thus it's safe to release the GIL without locking the object. |
| 231 | */ |
| 232 | Py_BEGIN_ALLOW_THREADS |
| 233 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 234 | Py_END_ALLOW_THREADS |
| 235 | } |
| 236 | else { |
| 237 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 238 | } |
| 239 | #else |
| 240 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 241 | #endif |
| 242 | if (res != SUCCESS) { |
| 243 | PyErr_SetString(PyExc_RuntimeError, |
| 244 | "internal error in SHA3 Update()"); |
| 245 | goto error; |
| 246 | } |
| 247 | PyBuffer_Release(&buf); |
| 248 | } |
| 249 | |
| 250 | return (PyObject *)self; |
| 251 | |
| 252 | error: |
| 253 | if (self) { |
| 254 | Py_DECREF(self); |
| 255 | } |
| 256 | if (data && buf.obj) { |
| 257 | PyBuffer_Release(&buf); |
| 258 | } |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /* Internal methods for a hash object */ |
| 264 | |
| 265 | static void |
| 266 | SHA3_dealloc(SHA3object *self) |
| 267 | { |
| 268 | #ifdef WITH_THREAD |
| 269 | if (self->lock) { |
| 270 | PyThread_free_lock(self->lock); |
| 271 | } |
| 272 | #endif |
| 273 | PyObject_Del(self); |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /* External methods for a hash object */ |
| 278 | |
| 279 | |
| 280 | /*[clinic input] |
| 281 | _sha3.sha3_224.copy |
| 282 | |
| 283 | Return a copy of the hash object. |
| 284 | [clinic start generated code]*/ |
| 285 | |
| 286 | static PyObject * |
| 287 | _sha3_sha3_224_copy_impl(SHA3object *self) |
| 288 | /*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/ |
| 289 | { |
| 290 | SHA3object *newobj; |
| 291 | |
| 292 | if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) { |
| 293 | return NULL; |
| 294 | } |
| 295 | ENTER_HASHLIB(self); |
| 296 | SHA3_copystate(newobj->hash_state, self->hash_state); |
| 297 | LEAVE_HASHLIB(self); |
| 298 | return (PyObject *)newobj; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /*[clinic input] |
| 303 | _sha3.sha3_224.digest |
| 304 | |
| 305 | Return the digest value as a string of binary data. |
| 306 | [clinic start generated code]*/ |
| 307 | |
| 308 | static PyObject * |
| 309 | _sha3_sha3_224_digest_impl(SHA3object *self) |
| 310 | /*[clinic end generated code: output=fd531842e20b2d5b input=a5807917d219b30e]*/ |
| 311 | { |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 312 | unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE]; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 313 | SHA3_state temp; |
| 314 | HashReturn res; |
| 315 | |
| 316 | ENTER_HASHLIB(self); |
| 317 | SHA3_copystate(temp, self->hash_state); |
| 318 | LEAVE_HASHLIB(self); |
| 319 | res = SHA3_done(&temp, digest); |
| 320 | if (res != SUCCESS) { |
| 321 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); |
| 322 | return NULL; |
| 323 | } |
| 324 | return PyBytes_FromStringAndSize((const char *)digest, |
| 325 | self->hash_state.fixedOutputLength / 8); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | /*[clinic input] |
| 330 | _sha3.sha3_224.hexdigest |
| 331 | |
| 332 | Return the digest value as a string of hexadecimal digits. |
| 333 | [clinic start generated code]*/ |
| 334 | |
| 335 | static PyObject * |
| 336 | _sha3_sha3_224_hexdigest_impl(SHA3object *self) |
| 337 | /*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/ |
| 338 | { |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 339 | unsigned char digest[SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE]; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 340 | SHA3_state temp; |
| 341 | HashReturn res; |
| 342 | |
| 343 | /* Get the raw (binary) digest value */ |
| 344 | ENTER_HASHLIB(self); |
| 345 | SHA3_copystate(temp, self->hash_state); |
| 346 | LEAVE_HASHLIB(self); |
| 347 | res = SHA3_done(&temp, digest); |
| 348 | if (res != SUCCESS) { |
| 349 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); |
| 350 | return NULL; |
| 351 | } |
| 352 | return _Py_strhex((const char *)digest, |
| 353 | self->hash_state.fixedOutputLength / 8); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /*[clinic input] |
| 358 | _sha3.sha3_224.update |
| 359 | |
| 360 | obj: object |
| 361 | / |
| 362 | |
| 363 | Update this hash object's state with the provided string. |
| 364 | [clinic start generated code]*/ |
| 365 | |
| 366 | static PyObject * |
| 367 | _sha3_sha3_224_update(SHA3object *self, PyObject *obj) |
| 368 | /*[clinic end generated code: output=06721d55b483e0af input=be44bf0d1c279791]*/ |
| 369 | { |
| 370 | Py_buffer buf; |
| 371 | HashReturn res; |
| 372 | |
| 373 | GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); |
| 374 | |
| 375 | /* add new data, the function takes the length in bits not bytes */ |
| 376 | #ifdef WITH_THREAD |
| 377 | if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) { |
| 378 | self->lock = PyThread_allocate_lock(); |
| 379 | } |
| 380 | /* Once a lock exists all code paths must be synchronized. We have to |
| 381 | * release the GIL even for small buffers as acquiring the lock may take |
| 382 | * an unlimited amount of time when another thread updates this object |
| 383 | * with lots of data. */ |
| 384 | if (self->lock) { |
| 385 | Py_BEGIN_ALLOW_THREADS |
| 386 | PyThread_acquire_lock(self->lock, 1); |
| 387 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 388 | PyThread_release_lock(self->lock); |
| 389 | Py_END_ALLOW_THREADS |
| 390 | } |
| 391 | else { |
| 392 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 393 | } |
| 394 | #else |
| 395 | res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); |
| 396 | #endif |
| 397 | |
| 398 | if (res != SUCCESS) { |
| 399 | PyBuffer_Release(&buf); |
| 400 | PyErr_SetString(PyExc_RuntimeError, |
| 401 | "internal error in SHA3 Update()"); |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | PyBuffer_Release(&buf); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 406 | Py_RETURN_NONE; |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | |
| 410 | static PyMethodDef SHA3_methods[] = { |
| 411 | _SHA3_SHA3_224_COPY_METHODDEF |
| 412 | _SHA3_SHA3_224_DIGEST_METHODDEF |
| 413 | _SHA3_SHA3_224_HEXDIGEST_METHODDEF |
| 414 | _SHA3_SHA3_224_UPDATE_METHODDEF |
| 415 | {NULL, NULL} /* sentinel */ |
| 416 | }; |
| 417 | |
| 418 | |
| 419 | static PyObject * |
| 420 | SHA3_get_block_size(SHA3object *self, void *closure) |
| 421 | { |
| 422 | int rate = self->hash_state.sponge.rate; |
| 423 | return PyLong_FromLong(rate / 8); |
| 424 | } |
| 425 | |
| 426 | |
| 427 | static PyObject * |
| 428 | SHA3_get_name(SHA3object *self, void *closure) |
| 429 | { |
| 430 | PyTypeObject *type = Py_TYPE(self); |
| 431 | if (type == &SHA3_224type) { |
| 432 | return PyUnicode_FromString("sha3_224"); |
| 433 | } else if (type == &SHA3_256type) { |
| 434 | return PyUnicode_FromString("sha3_256"); |
| 435 | } else if (type == &SHA3_384type) { |
| 436 | return PyUnicode_FromString("sha3_384"); |
| 437 | } else if (type == &SHA3_512type) { |
| 438 | return PyUnicode_FromString("sha3_512"); |
| 439 | #ifdef PY_WITH_KECCAK |
| 440 | } else if (type == &Keccak_224type) { |
| 441 | return PyUnicode_FromString("keccak_224"); |
| 442 | } else if (type == &Keccak_256type) { |
| 443 | return PyUnicode_FromString("keccak_256"); |
| 444 | } else if (type == &Keccak_384type) { |
| 445 | return PyUnicode_FromString("keccak_384"); |
| 446 | } else if (type == &Keccak_512type) { |
| 447 | return PyUnicode_FromString("keccak_512"); |
| 448 | #endif |
| 449 | } else if (type == &SHAKE128type) { |
| 450 | return PyUnicode_FromString("shake_128"); |
| 451 | } else if (type == &SHAKE256type) { |
| 452 | return PyUnicode_FromString("shake_256"); |
| 453 | } else { |
| 454 | PyErr_BadInternalCall(); |
| 455 | return NULL; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | |
| 460 | static PyObject * |
| 461 | SHA3_get_digest_size(SHA3object *self, void *closure) |
| 462 | { |
| 463 | return PyLong_FromLong(self->hash_state.fixedOutputLength / 8); |
| 464 | } |
| 465 | |
| 466 | |
| 467 | static PyObject * |
| 468 | SHA3_get_capacity_bits(SHA3object *self, void *closure) |
| 469 | { |
| 470 | int capacity = 1600 - self->hash_state.sponge.rate; |
| 471 | return PyLong_FromLong(capacity); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | static PyObject * |
| 476 | SHA3_get_rate_bits(SHA3object *self, void *closure) |
| 477 | { |
| 478 | unsigned int rate = self->hash_state.sponge.rate; |
| 479 | return PyLong_FromLong(rate); |
| 480 | } |
| 481 | |
| 482 | static PyObject * |
| 483 | SHA3_get_suffix(SHA3object *self, void *closure) |
| 484 | { |
| 485 | unsigned char suffix[2]; |
| 486 | suffix[0] = self->hash_state.delimitedSuffix; |
| 487 | suffix[1] = 0; |
| 488 | return PyBytes_FromStringAndSize((const char *)suffix, 1); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | static PyGetSetDef SHA3_getseters[] = { |
| 493 | {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL}, |
| 494 | {"name", (getter)SHA3_get_name, NULL, NULL, NULL}, |
| 495 | {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL}, |
| 496 | {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL}, |
| 497 | {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL}, |
| 498 | {"_suffix", (getter)SHA3_get_suffix, NULL, NULL, NULL}, |
| 499 | {NULL} /* Sentinel */ |
| 500 | }; |
| 501 | |
| 502 | |
| 503 | #define SHA3_TYPE(type_obj, type_name, type_doc, type_methods) \ |
| 504 | static PyTypeObject type_obj = { \ |
| 505 | PyVarObject_HEAD_INIT(NULL, 0) \ |
| 506 | type_name, /* tp_name */ \ |
| 507 | sizeof(SHA3object), /* tp_size */ \ |
| 508 | 0, /* tp_itemsize */ \ |
| 509 | /* methods */ \ |
| 510 | (destructor)SHA3_dealloc, /* tp_dealloc */ \ |
| 511 | 0, /* tp_print */ \ |
| 512 | 0, /* tp_getattr */ \ |
| 513 | 0, /* tp_setattr */ \ |
| 514 | 0, /* tp_reserved */ \ |
| 515 | 0, /* tp_repr */ \ |
| 516 | 0, /* tp_as_number */ \ |
| 517 | 0, /* tp_as_sequence */ \ |
| 518 | 0, /* tp_as_mapping */ \ |
| 519 | 0, /* tp_hash */ \ |
| 520 | 0, /* tp_call */ \ |
| 521 | 0, /* tp_str */ \ |
| 522 | 0, /* tp_getattro */ \ |
| 523 | 0, /* tp_setattro */ \ |
| 524 | 0, /* tp_as_buffer */ \ |
| 525 | Py_TPFLAGS_DEFAULT, /* tp_flags */ \ |
| 526 | type_doc, /* tp_doc */ \ |
| 527 | 0, /* tp_traverse */ \ |
| 528 | 0, /* tp_clear */ \ |
| 529 | 0, /* tp_richcompare */ \ |
| 530 | 0, /* tp_weaklistoffset */ \ |
| 531 | 0, /* tp_iter */ \ |
| 532 | 0, /* tp_iternext */ \ |
| 533 | type_methods, /* tp_methods */ \ |
| 534 | NULL, /* tp_members */ \ |
| 535 | SHA3_getseters, /* tp_getset */ \ |
| 536 | 0, /* tp_base */ \ |
| 537 | 0, /* tp_dict */ \ |
| 538 | 0, /* tp_descr_get */ \ |
| 539 | 0, /* tp_descr_set */ \ |
| 540 | 0, /* tp_dictoffset */ \ |
| 541 | 0, /* tp_init */ \ |
| 542 | 0, /* tp_alloc */ \ |
| 543 | py_sha3_new, /* tp_new */ \ |
| 544 | } |
| 545 | |
| 546 | PyDoc_STRVAR(sha3_256__doc__, |
| 547 | "sha3_256([string]) -> SHA3 object\n\ |
| 548 | \n\ |
| 549 | Return a new SHA3 hash object with a hashbit length of 32 bytes."); |
| 550 | |
| 551 | PyDoc_STRVAR(sha3_384__doc__, |
| 552 | "sha3_384([string]) -> SHA3 object\n\ |
| 553 | \n\ |
| 554 | Return a new SHA3 hash object with a hashbit length of 48 bytes."); |
| 555 | |
| 556 | PyDoc_STRVAR(sha3_512__doc__, |
| 557 | "sha3_512([string]) -> SHA3 object\n\ |
| 558 | \n\ |
| 559 | Return a new SHA3 hash object with a hashbit length of 64 bytes."); |
| 560 | |
| 561 | SHA3_TYPE(SHA3_224type, "_sha3.sha3_224", py_sha3_new__doc__, SHA3_methods); |
| 562 | SHA3_TYPE(SHA3_256type, "_sha3.sha3_256", sha3_256__doc__, SHA3_methods); |
| 563 | SHA3_TYPE(SHA3_384type, "_sha3.sha3_384", sha3_384__doc__, SHA3_methods); |
| 564 | SHA3_TYPE(SHA3_512type, "_sha3.sha3_512", sha3_512__doc__, SHA3_methods); |
| 565 | |
| 566 | #ifdef PY_WITH_KECCAK |
| 567 | PyDoc_STRVAR(keccak_224__doc__, |
| 568 | "keccak_224([string]) -> Keccak object\n\ |
| 569 | \n\ |
| 570 | Return a new Keccak hash object with a hashbit length of 28 bytes."); |
| 571 | |
| 572 | PyDoc_STRVAR(keccak_256__doc__, |
| 573 | "keccak_256([string]) -> Keccak object\n\ |
| 574 | \n\ |
| 575 | Return a new Keccak hash object with a hashbit length of 32 bytes."); |
| 576 | |
| 577 | PyDoc_STRVAR(keccak_384__doc__, |
| 578 | "keccak_384([string]) -> Keccak object\n\ |
| 579 | \n\ |
| 580 | Return a new Keccak hash object with a hashbit length of 48 bytes."); |
| 581 | |
| 582 | PyDoc_STRVAR(keccak_512__doc__, |
| 583 | "keccak_512([string]) -> Keccak object\n\ |
| 584 | \n\ |
| 585 | Return a new Keccak hash object with a hashbit length of 64 bytes."); |
| 586 | |
| 587 | SHA3_TYPE(Keccak_224type, "_sha3.keccak_224", keccak_224__doc__, SHA3_methods); |
| 588 | SHA3_TYPE(Keccak_256type, "_sha3.keccak_256", keccak_256__doc__, SHA3_methods); |
| 589 | SHA3_TYPE(Keccak_384type, "_sha3.keccak_384", keccak_384__doc__, SHA3_methods); |
| 590 | SHA3_TYPE(Keccak_512type, "_sha3.keccak_512", keccak_512__doc__, SHA3_methods); |
| 591 | #endif |
| 592 | |
| 593 | |
| 594 | static PyObject * |
| 595 | _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex) |
| 596 | { |
| 597 | unsigned char *digest = NULL; |
| 598 | SHA3_state temp; |
| 599 | int res; |
| 600 | PyObject *result = NULL; |
| 601 | |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 602 | /* ExtractLane needs at least SHA3_MAX_DIGESTSIZE + SHA3_LANESIZE and |
| 603 | * SHA3_LANESIZE extra space. |
| 604 | */ |
Christian Heimes | c71ec8a | 2016-09-08 15:04:38 +0200 | [diff] [blame] | 605 | digest = (unsigned char*)PyMem_Malloc(digestlen + SHA3_LANESIZE); |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 606 | if (digest == NULL) { |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 607 | return PyErr_NoMemory(); |
| 608 | } |
| 609 | |
| 610 | /* Get the raw (binary) digest value */ |
| 611 | ENTER_HASHLIB(self); |
| 612 | SHA3_copystate(temp, self->hash_state); |
| 613 | LEAVE_HASHLIB(self); |
| 614 | res = SHA3_done(&temp, NULL); |
| 615 | if (res != SUCCESS) { |
| 616 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 done()"); |
| 617 | goto error; |
| 618 | } |
| 619 | res = SHA3_squeeze(&temp, digest, digestlen * 8); |
| 620 | if (res != SUCCESS) { |
| 621 | PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Squeeze()"); |
| 622 | return NULL; |
| 623 | } |
| 624 | if (hex) { |
| 625 | result = _Py_strhex((const char *)digest, digestlen); |
| 626 | } else { |
| 627 | result = PyBytes_FromStringAndSize((const char *)digest, |
| 628 | digestlen); |
| 629 | } |
| 630 | error: |
| 631 | if (digest != NULL) { |
| 632 | PyMem_Free(digest); |
| 633 | } |
| 634 | return result; |
| 635 | } |
| 636 | |
| 637 | |
| 638 | /*[clinic input] |
| 639 | _sha3.shake_128.digest |
| 640 | |
| 641 | length: unsigned_long(bitwise=True) |
| 642 | \ |
| 643 | |
| 644 | Return the digest value as a string of binary data. |
| 645 | [clinic start generated code]*/ |
| 646 | |
| 647 | static PyObject * |
| 648 | _sha3_shake_128_digest_impl(SHA3object *self, unsigned long length) |
| 649 | /*[clinic end generated code: output=2313605e2f87bb8f input=608c8ca80ae9d115]*/ |
| 650 | { |
| 651 | return _SHAKE_digest(self, length, 0); |
| 652 | } |
| 653 | |
| 654 | |
| 655 | /*[clinic input] |
| 656 | _sha3.shake_128.hexdigest |
| 657 | |
| 658 | length: unsigned_long(bitwise=True) |
| 659 | \ |
| 660 | |
| 661 | Return the digest value as a string of hexadecimal digits. |
| 662 | [clinic start generated code]*/ |
| 663 | |
| 664 | static PyObject * |
| 665 | _sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length) |
| 666 | /*[clinic end generated code: output=bf8e2f1e490944a8 input=64e56b4760db4573]*/ |
| 667 | { |
| 668 | return _SHAKE_digest(self, length, 1); |
| 669 | } |
| 670 | |
| 671 | |
| 672 | static PyMethodDef SHAKE_methods[] = { |
| 673 | _SHA3_SHA3_224_COPY_METHODDEF |
| 674 | _SHA3_SHAKE_128_DIGEST_METHODDEF |
| 675 | _SHA3_SHAKE_128_HEXDIGEST_METHODDEF |
| 676 | _SHA3_SHA3_224_UPDATE_METHODDEF |
| 677 | {NULL, NULL} /* sentinel */ |
| 678 | }; |
| 679 | |
| 680 | PyDoc_STRVAR(shake_128__doc__, |
| 681 | "shake_128([string]) -> SHAKE object\n\ |
| 682 | \n\ |
| 683 | Return a new SHAKE hash object."); |
| 684 | |
| 685 | PyDoc_STRVAR(shake_256__doc__, |
| 686 | "shake_256([string]) -> SHAKE object\n\ |
| 687 | \n\ |
| 688 | Return a new SHAKE hash object."); |
| 689 | |
| 690 | SHA3_TYPE(SHAKE128type, "_sha3.shake_128", shake_128__doc__, SHAKE_methods); |
| 691 | SHA3_TYPE(SHAKE256type, "_sha3.shake_256", shake_256__doc__, SHAKE_methods); |
| 692 | |
| 693 | |
| 694 | /* Initialize this module. */ |
| 695 | static struct PyModuleDef _SHA3module = { |
| 696 | PyModuleDef_HEAD_INIT, |
| 697 | "_sha3", |
| 698 | NULL, |
| 699 | -1, |
| 700 | NULL, |
| 701 | NULL, |
| 702 | NULL, |
| 703 | NULL, |
| 704 | NULL |
| 705 | }; |
| 706 | |
| 707 | |
| 708 | PyMODINIT_FUNC |
| 709 | PyInit__sha3(void) |
| 710 | { |
| 711 | PyObject *m = NULL; |
| 712 | |
Christian Heimes | cf45ee1 | 2016-09-08 13:35:00 +0200 | [diff] [blame] | 713 | if ((m = PyModule_Create(&_SHA3module)) == NULL) { |
| 714 | return NULL; |
| 715 | } |
Christian Heimes | 6fe2a75 | 2016-09-07 11:58:24 +0200 | [diff] [blame] | 716 | |
| 717 | #define init_sha3type(name, type) \ |
| 718 | do { \ |
| 719 | Py_TYPE(type) = &PyType_Type; \ |
| 720 | if (PyType_Ready(type) < 0) { \ |
| 721 | goto error; \ |
| 722 | } \ |
| 723 | Py_INCREF((PyObject *)type); \ |
| 724 | if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \ |
| 725 | goto error; \ |
| 726 | } \ |
| 727 | } while(0) |
| 728 | |
| 729 | init_sha3type("sha3_224", &SHA3_224type); |
| 730 | init_sha3type("sha3_256", &SHA3_256type); |
| 731 | init_sha3type("sha3_384", &SHA3_384type); |
| 732 | init_sha3type("sha3_512", &SHA3_512type); |
| 733 | #ifdef PY_WITH_KECCAK |
| 734 | init_sha3type("keccak_224", &Keccak_224type); |
| 735 | init_sha3type("keccak_256", &Keccak_256type); |
| 736 | init_sha3type("keccak_384", &Keccak_384type); |
| 737 | init_sha3type("keccak_512", &Keccak_512type); |
| 738 | #endif |
| 739 | init_sha3type("shake_128", &SHAKE128type); |
| 740 | init_sha3type("shake_256", &SHAKE256type); |
| 741 | |
| 742 | #undef init_sha3type |
| 743 | |
| 744 | if (PyModule_AddIntConstant(m, "keccakopt", KeccakOpt) < 0) { |
| 745 | goto error; |
| 746 | } |
| 747 | if (PyModule_AddStringConstant(m, "implementation", |
| 748 | KeccakP1600_implementation) < 0) { |
| 749 | goto error; |
| 750 | } |
| 751 | |
| 752 | return m; |
| 753 | error: |
| 754 | Py_DECREF(m); |
| 755 | return NULL; |
| 756 | } |