blob: 4c3c6db1322abf1eeaa61b5db5fb2c45df26297d [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 }
303 assert(_PyUnicode_CheckConsistency(retval, 1));
304 return retval;
305}
306
307PyDoc_STRVAR(SHA3_update__doc__,
308"Update this hash object's state with the provided string.");
309
310static PyObject *
311SHA3_update(SHA3object *self, PyObject *args)
312{
313 PyObject *obj;
314 Py_buffer buf;
315 HashReturn res;
316
317 if (!PyArg_ParseTuple(args, "O:update", &obj))
318 return NULL;
319
320 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
321
322 /* add new data, the function takes the length in bits not bytes */
323#ifdef WITH_THREADS
324 if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
325 self->lock = PyThread_allocate_lock();
326 }
327 /* Once a lock exists all code paths must be synchronized. We have to
328 * release the GIL even for small buffers as acquiring the lock may take
329 * an unlimited amount of time when another thread updates this object
330 * with lots of data. */
331 if (self->lock) {
332 Py_BEGIN_ALLOW_THREADS
333 PyThread_acquire_lock(self->lock, 1);
334 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
335 PyThread_release_lock(self->lock);
336 Py_END_ALLOW_THREADS
337 }
338 else {
339 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
340 }
341#else
342 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
343#endif
344 LEAVE_HASHLIB(self);
345
346 if (res != SUCCESS) {
347 PyBuffer_Release(&buf);
348 PyErr_SetString(PyExc_RuntimeError,
349 "internal error in SHA3 Update()");
350 return NULL;
351 }
352
353 PyBuffer_Release(&buf);
354 Py_INCREF(Py_None);
355 return Py_None;
356}
357
358static PyMethodDef SHA3_methods[] = {
359 {"copy", (PyCFunction)SHA3_copy, METH_NOARGS,
360 SHA3_copy__doc__},
361 {"digest", (PyCFunction)SHA3_digest, METH_NOARGS,
362 SHA3_digest__doc__},
363 {"hexdigest", (PyCFunction)SHA3_hexdigest, METH_NOARGS,
364 SHA3_hexdigest__doc__},
365 {"update", (PyCFunction)SHA3_update, METH_VARARGS,
366 SHA3_update__doc__},
367 {NULL, NULL} /* sentinel */
368};
369
370static PyObject *
371SHA3_get_block_size(SHA3object *self, void *closure)
372{
Christian Heimes7707f6f2012-10-14 22:16:27 +0200373 /* HMAC-SHA3 hasn't been specified yet and no official test vectors are
374 * available. Thus block_size returns NotImplemented to prevent people
375 * from using SHA3 with the hmac module.
376 */
377 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes4a0270d2012-10-06 02:23:36 +0200378}
379
380static PyObject *
381SHA3_get_name(SHA3object *self, void *closure)
382{
383 return PyUnicode_FromFormat("sha3_%i", self->hashbitlen);
384}
385
386static PyObject *
387SHA3_get_digest_size(SHA3object *self, void *closure)
388{
389 return PyLong_FromLong(self->hashbitlen / 8);
390}
391
392
393static PyGetSetDef SHA3_getseters[] = {
394 {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
395 {"name", (getter)SHA3_get_name, NULL, NULL, NULL},
396 {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL},
397 {NULL} /* Sentinel */
398};
399
400static PyTypeObject SHA3type = {
401 PyVarObject_HEAD_INIT(NULL, 0)
402 "_sha3.SHA3", /* tp_name */
403 sizeof(SHA3object), /* tp_size */
404 0, /* tp_itemsize */
405 /* methods */
406 (destructor)SHA3_dealloc, /* tp_dealloc */
407 0, /* tp_print */
408 0, /* tp_getattr */
409 0, /* tp_setattr */
410 0, /* tp_reserved */
411 0, /* tp_repr */
412 0, /* tp_as_number */
413 0, /* tp_as_sequence */
414 0, /* tp_as_mapping */
415 0, /* tp_hash */
416 0, /* tp_call */
417 0, /* tp_str */
418 0, /* tp_getattro */
419 0, /* tp_setattro */
420 0, /* tp_as_buffer */
421 Py_TPFLAGS_DEFAULT, /* tp_flags */
422 0, /* tp_doc */
423 0, /* tp_traverse */
424 0, /* tp_clear */
425 0, /* tp_richcompare */
426 0, /* tp_weaklistoffset */
427 0, /* tp_iter */
428 0, /* tp_iternext */
429 SHA3_methods, /* tp_methods */
430 NULL, /* tp_members */
431 SHA3_getseters, /* tp_getset */
432};
433
434
435/* constructor helper */
436static PyObject *
437SHA3_factory(PyObject *args, PyObject *kwdict, const char *fmt,
438 int hashbitlen)
439{
440 SHA3object *newobj = NULL;
441 static char *kwlist[] = {"string", NULL};
442 PyObject *data_obj = NULL;
443 Py_buffer buf;
444 HashReturn res;
445
446 if (!PyArg_ParseTupleAndKeywords(args, kwdict, fmt, kwlist,
447 &data_obj)) {
448 return NULL;
449 }
450
451 if (data_obj)
452 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
453
454 if ((newobj = newSHA3object(hashbitlen)) == NULL) {
455 goto error;
456 }
457
458 if (SHA3_init(&newobj->hash_state, hashbitlen) != SUCCESS) {
459 PyErr_SetString(PyExc_RuntimeError,
460 "internal error in SHA3 Update()");
461 goto error;
462 }
463
464 if (data_obj) {
465#ifdef WITH_THREADS
466 if (buf.len >= HASHLIB_GIL_MINSIZE) {
467 /* invariant: New objects can't be accessed by other code yet,
468 * thus it's safe to release the GIL without locking the object.
469 */
470 Py_BEGIN_ALLOW_THREADS
471 res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
472 Py_END_ALLOW_THREADS
473 }
474 else {
475 res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
476 }
477#else
478 res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
479#endif
480 if (res != SUCCESS) {
481 PyErr_SetString(PyExc_RuntimeError,
482 "internal error in SHA3 Update()");
483 goto error;
484 }
485 PyBuffer_Release(&buf);
486 }
487
488 return (PyObject *)newobj;
489
490 error:
491 if (newobj) {
Christian Heimesc018f572012-10-06 15:09:06 +0200492 SHA3_dealloc(newobj);
Christian Heimes4a0270d2012-10-06 02:23:36 +0200493 }
494 if (data_obj) {
495 PyBuffer_Release(&buf);
496 }
497 return NULL;
498
499}
500
501PyDoc_STRVAR(sha3_224__doc__,
502"sha3_224([string]) -> SHA3 object\n\
503\n\
504Return a new SHA3 hash object with a hashbit length of 28 bytes.");
505
506static PyObject *
507sha3_224(PyObject *self, PyObject *args, PyObject *kwdict)
508{
509 return SHA3_factory(args, kwdict, "|O:sha3_224", 224);
510}
511
512
513PyDoc_STRVAR(sha3_256__doc__,
514"sha3_256([string]) -> SHA3 object\n\
515\n\
516Return a new SHA3 hash object with a hashbit length of 32 bytes.");
517
518static PyObject *
519sha3_256(PyObject *self, PyObject *args, PyObject *kwdict)
520{
521 return SHA3_factory(args, kwdict, "|O:sha3_256", 256);
522}
523
524PyDoc_STRVAR(sha3_384__doc__,
525"sha3_384([string]) -> SHA3 object\n\
526\n\
527Return a new SHA3 hash object with a hashbit length of 48 bytes.");
528
529static PyObject *
530sha3_384(PyObject *self, PyObject *args, PyObject *kwdict)
531{
532 return SHA3_factory(args, kwdict, "|O:sha3_384", 384);
533}
534
535PyDoc_STRVAR(sha3_512__doc__,
536"sha3_512([string]) -> SHA3 object\n\
537\n\
538Return a new SHA3 hash object with a hashbit length of 64 bytes.");
539
540static PyObject *
541sha3_512(PyObject *self, PyObject *args, PyObject *kwdict)
542{
543 return SHA3_factory(args, kwdict, "|O:sha3_512", 512);
544}
545
546
547/* List of functions exported by this module */
548static struct PyMethodDef SHA3_functions[] = {
549 {"sha3_224", (PyCFunction)sha3_224, METH_VARARGS|METH_KEYWORDS,
550 sha3_224__doc__},
551 {"sha3_256", (PyCFunction)sha3_256, METH_VARARGS|METH_KEYWORDS,
552 sha3_256__doc__},
553 {"sha3_384", (PyCFunction)sha3_384, METH_VARARGS|METH_KEYWORDS,
554 sha3_384__doc__},
555 {"sha3_512", (PyCFunction)sha3_512, METH_VARARGS|METH_KEYWORDS,
556 sha3_512__doc__},
557 {NULL, NULL} /* Sentinel */
558};
559
560
561/* Initialize this module. */
562static struct PyModuleDef _SHA3module = {
563 PyModuleDef_HEAD_INIT,
564 "_sha3",
565 NULL,
566 -1,
567 SHA3_functions,
568 NULL,
569 NULL,
570 NULL,
571 NULL
572};
573
574PyMODINIT_FUNC
575PyInit__sha3(void)
576{
577 Py_TYPE(&SHA3type) = &PyType_Type;
578 if (PyType_Ready(&SHA3type) < 0) {
579 return NULL;
580 }
581
582 return PyModule_Create(&_SHA3module);
583}