1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
/* SHA3 module
*
* This module provides an interface to the SHA3 algorithm
*
* See below for information about the original code this module was
* based upon. Additional work performed by:
*
* Andrew Kuchling (amk@amk.ca)
* Greg Stein (gstein@lyra.org)
* Trevor Perrin (trevp@trevp.net)
* Gregory P. Smith (greg@krypto.org)
*
* Copyright (C) 2012 Christian Heimes (christian@python.org)
* Licensed to PSF under a Contributor Agreement.
*
*/
#include "Python.h"
#include "../hashlib.h"
/* **************************************************************************
* SHA-3 (Keccak)
*
* The code is based on KeccakReferenceAndOptimized-3.2.zip from 29 May 2012.
*
* The reference implementation is altered in this points:
* - C++ comments are converted to ANSI C comments.
* - All functions and globals are declared static.
* - The typedef for UINT64 is commented out.
* - KeccakF-1600-opt[32|64]-settings.h are commented out
* - Some unused functions are commented out to silence compiler warnings.
*
* In order to avoid name clashes with other software I have to declare all
* Keccak functions and global data as static. The C code is directly
* included into this file in order to access the static functions.
*
* Keccak can be tuned with several paramenters. I try to explain all options
* as far as I understand them. The reference implementation also contains
* assembler code for ARM platforms (NEON instructions).
*
* Common
* ======
*
* Options:
* UseBebigokimisa, Unrolling
*
* - Unrolling: loop unrolling (24, 12, 8, 6, 4, 3, 2, 1)
* - UseBebigokimisa: lane complementing
*
* 64bit platforms
* ===============
*
* Additional options:
* UseSSE, UseOnlySIMD64, UseMMX, UseXOP, UseSHLD
*
* Optimized instructions (disabled by default):
* - UseSSE: use Stream SIMD extensions
* o UseOnlySIMD64: limit to 64bit instructions, otherwise 128bit
* o w/o UseOnlySIMD64: requires compiler agument -mssse3 or -mtune
* - UseMMX: use 64bit MMX instructions
* - UseXOP: use AMD's eXtended Operations (128bit SSE extension)
*
* Other:
* - Unrolling: default 24
* - UseBebigokimisa: default 1
*
* When neither UseSSE, UseMMX nor UseXOP is configured, ROL64 (rotate left
* 64) is implemented as:
* - Windows: _rotl64()
* - UseSHLD: use shld (shift left) asm optimization
* - otherwise: shift and xor
*
* UseBebigokimisa can't be used in combination with UseSSE, UseMMX or
* UseXOP. UseOnlySIMD64 has no effect unless UseSSE is specified.
*
* Tests have shown that UseSSE + UseOnlySIMD64 is about three to four
* times SLOWER than UseBebigokimisa. UseSSE and UseMMX are about two times
* slower. (tested by CH and AP)
*
* 32bit platforms
* ===============
*
* Additional options:
* UseInterleaveTables, UseSchedule
*
* - Unrolling: default 2
* - UseBebigokimisa: default n/a
* - UseSchedule: ???, (1, 2, 3; default 3)
* - UseInterleaveTables: use two 64k lookup tables for (de)interleaving
* default: n/a
*
* schedules:
* - 3: no UseBebigokimisa, Unrolling must be 2
* - 2 + 1: ???
*
* *************************************************************************/
#ifdef __sparc
/* On SPARC with Solaris CC opt64 fails with 'invalid address alignment' */
#define KeccakOpt 32
#elif SIZEOF_VOID_P == 8
#define KeccakOpt 64
#elif SIZEOF_VOID_P == 4
#define KeccakOpt 32
#endif
#if KeccakOpt == 64 && defined(PY_UINT64_T)
/* 64bit platforms with unsigned int64 */
#define Unrolling 24
#define UseBebigokimisa
typedef PY_UINT64_T UINT64;
#elif KeccakOpt == 32 && defined(PY_UINT64_T)
/* 32bit platforms with unsigned int64 */
#define Unrolling 2
#define UseSchedule 3
typedef PY_UINT64_T UINT64;
#else
/* 32 or 64bit platforms without unsigned int64 */
#define Unrolling 2
#define UseSchedule 3
#define UseInterleaveTables
#endif
/* replacement for brg_endian.h
#define IS_BIG_ENDIAN BIG_ENDIAN
#define IS_LITTLE_ENDIAN LITTLE_ENDIAN
#define PLATFORM_BYTE_ORDER BYTE_ORDER
*/
/* inline all Keccak dependencies */
#include "keccak/KeccakNISTInterface.h"
#include "keccak/KeccakNISTInterface.c"
#include "keccak/KeccakSponge.c"
#if KeccakOpt == 64
#include "keccak/KeccakF-1600-opt64.c"
#elif KeccakOpt == 32
#include "keccak/KeccakF-1600-opt32.c"
#endif
#define SHA3_BLOCKSIZE 200 /* 1600 bits */
#define SHA3_MAX_DIGESTSIZE 64 /* 512 bits */
#define SHA3_state hashState
#define SHA3_init Init
#define SHA3_process Update
#define SHA3_done Final
#define SHA3_copystate(dest, src) memcpy(&(dest), &(src), sizeof(SHA3_state))
#define SHA3_clearstate(state) memset(&(state), 0, sizeof(SHA3_state))
/* The structure for storing SHA3 info */
typedef struct {
PyObject_HEAD
int hashbitlen;
SHA3_state hash_state;
#ifdef WITH_THREAD
PyThread_type_lock lock;
#endif
} SHA3object;
static PyTypeObject SHA3type;
static SHA3object *
newSHA3object(int hashbitlen)
{
SHA3object *newobj;
/* check hashbitlen */
switch(hashbitlen) {
/* supported hash length */
case 224:
break;
case 256:
break;
case 384:
break;
case 512:
break;
case 0:
/* arbitrarily-long output isn't supported by this module */
default:
/* everything else is an error */
PyErr_SetString(PyExc_ValueError,
"hashbitlen must be one of 224, 256, 384 or 512.");
return NULL;
}
newobj = (SHA3object *)PyObject_New(SHA3object, &SHA3type);
if (newobj == NULL) {
return NULL;
}
newobj->hashbitlen = hashbitlen;
#ifdef WITH_THREAD
newobj->lock = NULL;
#endif
return newobj;
}
/* Internal methods for a hash object */
static void
SHA3_dealloc(SHA3object *self)
{
SHA3_clearstate(self->hash_state);
#ifdef WITH_THREAD
if (self->lock) {
PyThread_free_lock(self->lock);
}
#endif
PyObject_Del(self);
}
/* External methods for a hash object */
PyDoc_STRVAR(SHA3_copy__doc__, "Return a copy of the hash object.");
static PyObject *
SHA3_copy(SHA3object *self, PyObject *unused)
{
SHA3object *newobj;
if ((newobj = newSHA3object(self->hashbitlen)) == NULL) {
return NULL;
}
ENTER_HASHLIB(self);
SHA3_copystate(newobj->hash_state, self->hash_state);
LEAVE_HASHLIB(self);
return (PyObject *)newobj;
}
PyDoc_STRVAR(SHA3_digest__doc__,
"Return the digest value as a string of binary data.");
static PyObject *
SHA3_digest(SHA3object *self, PyObject *unused)
{
unsigned char digest[SHA3_MAX_DIGESTSIZE];
SHA3_state temp;
HashReturn res;
ENTER_HASHLIB(self);
SHA3_copystate(temp, self->hash_state);
LEAVE_HASHLIB(self);
res = SHA3_done(&temp, digest);
SHA3_clearstate(temp);
if (res != SUCCESS) {
PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
return NULL;
}
return PyBytes_FromStringAndSize((const char *)digest,
self->hashbitlen / 8);
}
PyDoc_STRVAR(SHA3_hexdigest__doc__,
"Return the digest value as a string of hexadecimal digits.");
static PyObject *
SHA3_hexdigest(SHA3object *self, PyObject *unused)
{
unsigned char digest[SHA3_MAX_DIGESTSIZE];
SHA3_state temp;
HashReturn res;
PyObject *retval;
Py_UCS1 *hex_digest;
int digestlen, i, j;
/* Get the raw (binary) digest value */
ENTER_HASHLIB(self);
SHA3_copystate(temp, self->hash_state);
LEAVE_HASHLIB(self);
res = SHA3_done(&temp, digest);
SHA3_clearstate(temp);
if (res != SUCCESS) {
PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
return NULL;
}
/* Create a new string */
digestlen = self->hashbitlen / 8;
retval = PyUnicode_New(digestlen * 2, 127);
if (!retval)
return NULL;
hex_digest = PyUnicode_1BYTE_DATA(retval);
/* Make hex version of the digest */
for(i=j=0; i < digestlen; i++) {
unsigned char c;
c = (digest[i] >> 4) & 0xf;
hex_digest[j++] = Py_hexdigits[c];
c = (digest[i] & 0xf);
hex_digest[j++] = Py_hexdigits[c];
}
assert(_PyUnicode_CheckConsistency(retval, 1));
return retval;
}
PyDoc_STRVAR(SHA3_update__doc__,
"Update this hash object's state with the provided string.");
static PyObject *
SHA3_update(SHA3object *self, PyObject *args)
{
PyObject *obj;
Py_buffer buf;
HashReturn res;
if (!PyArg_ParseTuple(args, "O:update", &obj))
return NULL;
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
/* add new data, the function takes the length in bits not bytes */
#ifdef WITH_THREADS
if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
self->lock = PyThread_allocate_lock();
}
/* Once a lock exists all code paths must be synchronized. We have to
* release the GIL even for small buffers as acquiring the lock may take
* an unlimited amount of time when another thread updates this object
* with lots of data. */
if (self->lock) {
Py_BEGIN_ALLOW_THREADS
PyThread_acquire_lock(self->lock, 1);
res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
PyThread_release_lock(self->lock);
Py_END_ALLOW_THREADS
}
else {
res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
}
#else
res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
#endif
LEAVE_HASHLIB(self);
if (res != SUCCESS) {
PyBuffer_Release(&buf);
PyErr_SetString(PyExc_RuntimeError,
"internal error in SHA3 Update()");
return NULL;
}
PyBuffer_Release(&buf);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef SHA3_methods[] = {
{"copy", (PyCFunction)SHA3_copy, METH_NOARGS,
SHA3_copy__doc__},
{"digest", (PyCFunction)SHA3_digest, METH_NOARGS,
SHA3_digest__doc__},
{"hexdigest", (PyCFunction)SHA3_hexdigest, METH_NOARGS,
SHA3_hexdigest__doc__},
{"update", (PyCFunction)SHA3_update, METH_VARARGS,
SHA3_update__doc__},
{NULL, NULL} /* sentinel */
};
static PyObject *
SHA3_get_block_size(SHA3object *self, void *closure)
{
return PyLong_FromLong(SHA3_BLOCKSIZE);
}
static PyObject *
SHA3_get_name(SHA3object *self, void *closure)
{
return PyUnicode_FromFormat("sha3_%i", self->hashbitlen);
}
static PyObject *
SHA3_get_digest_size(SHA3object *self, void *closure)
{
return PyLong_FromLong(self->hashbitlen / 8);
}
static PyGetSetDef SHA3_getseters[] = {
{"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
{"name", (getter)SHA3_get_name, NULL, NULL, NULL},
{"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL},
{NULL} /* Sentinel */
};
static PyTypeObject SHA3type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_sha3.SHA3", /* tp_name */
sizeof(SHA3object), /* tp_size */
0, /* tp_itemsize */
/* methods */
(destructor)SHA3_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
0, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
SHA3_methods, /* tp_methods */
NULL, /* tp_members */
SHA3_getseters, /* tp_getset */
};
/* constructor helper */
static PyObject *
SHA3_factory(PyObject *args, PyObject *kwdict, const char *fmt,
int hashbitlen)
{
SHA3object *newobj = NULL;
static char *kwlist[] = {"string", NULL};
PyObject *data_obj = NULL;
Py_buffer buf;
HashReturn res;
if (!PyArg_ParseTupleAndKeywords(args, kwdict, fmt, kwlist,
&data_obj)) {
return NULL;
}
if (data_obj)
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
if ((newobj = newSHA3object(hashbitlen)) == NULL) {
goto error;
}
if (SHA3_init(&newobj->hash_state, hashbitlen) != SUCCESS) {
PyErr_SetString(PyExc_RuntimeError,
"internal error in SHA3 Update()");
goto error;
}
if (data_obj) {
#ifdef WITH_THREADS
if (buf.len >= HASHLIB_GIL_MINSIZE) {
/* invariant: New objects can't be accessed by other code yet,
* thus it's safe to release the GIL without locking the object.
*/
Py_BEGIN_ALLOW_THREADS
res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
Py_END_ALLOW_THREADS
}
else {
res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
}
#else
res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
#endif
if (res != SUCCESS) {
PyErr_SetString(PyExc_RuntimeError,
"internal error in SHA3 Update()");
goto error;
}
PyBuffer_Release(&buf);
}
return (PyObject *)newobj;
error:
if (newobj) {
SHA3_dealloc(newobj);
}
if (data_obj) {
PyBuffer_Release(&buf);
}
return NULL;
}
PyDoc_STRVAR(sha3_224__doc__,
"sha3_224([string]) -> SHA3 object\n\
\n\
Return a new SHA3 hash object with a hashbit length of 28 bytes.");
static PyObject *
sha3_224(PyObject *self, PyObject *args, PyObject *kwdict)
{
return SHA3_factory(args, kwdict, "|O:sha3_224", 224);
}
PyDoc_STRVAR(sha3_256__doc__,
"sha3_256([string]) -> SHA3 object\n\
\n\
Return a new SHA3 hash object with a hashbit length of 32 bytes.");
static PyObject *
sha3_256(PyObject *self, PyObject *args, PyObject *kwdict)
{
return SHA3_factory(args, kwdict, "|O:sha3_256", 256);
}
PyDoc_STRVAR(sha3_384__doc__,
"sha3_384([string]) -> SHA3 object\n\
\n\
Return a new SHA3 hash object with a hashbit length of 48 bytes.");
static PyObject *
sha3_384(PyObject *self, PyObject *args, PyObject *kwdict)
{
return SHA3_factory(args, kwdict, "|O:sha3_384", 384);
}
PyDoc_STRVAR(sha3_512__doc__,
"sha3_512([string]) -> SHA3 object\n\
\n\
Return a new SHA3 hash object with a hashbit length of 64 bytes.");
static PyObject *
sha3_512(PyObject *self, PyObject *args, PyObject *kwdict)
{
return SHA3_factory(args, kwdict, "|O:sha3_512", 512);
}
/* List of functions exported by this module */
static struct PyMethodDef SHA3_functions[] = {
{"sha3_224", (PyCFunction)sha3_224, METH_VARARGS|METH_KEYWORDS,
sha3_224__doc__},
{"sha3_256", (PyCFunction)sha3_256, METH_VARARGS|METH_KEYWORDS,
sha3_256__doc__},
{"sha3_384", (PyCFunction)sha3_384, METH_VARARGS|METH_KEYWORDS,
sha3_384__doc__},
{"sha3_512", (PyCFunction)sha3_512, METH_VARARGS|METH_KEYWORDS,
sha3_512__doc__},
{NULL, NULL} /* Sentinel */
};
/* Initialize this module. */
static struct PyModuleDef _SHA3module = {
PyModuleDef_HEAD_INIT,
"_sha3",
NULL,
-1,
SHA3_functions,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit__sha3(void)
{
Py_TYPE(&SHA3type) = &PyType_Type;
if (PyType_Ready(&SHA3type) < 0) {
return NULL;
}
return PyModule_Create(&_SHA3module);
}
|