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