-
Notifications
You must be signed in to change notification settings - Fork 18
/
sha3.c
345 lines (261 loc) · 7.6 KB
/
sha3.c
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
/* SHA3 module -- This module provides an interface to SHA-3, also
known as Keccak.
This code is based on sha256module.c in the standard Python 2
distribution, which has the following header:
---
Additional work performed by:
Andrew Kuchling ([email protected])
Greg Stein ([email protected])
Trevor Perrin ([email protected])
Copyright (C) 2005 Gregory P. Smith ([email protected])
Licensed to PSF under a Contributor Agreement.
---
This file, adapter for SHA-3, is written by Björn Edström
<[email protected]> 2012, 2015.
It is placed under the same license as the original module:
Licensed to PSF under a Contributor Agreement.
*/
#define _VERSION "0.2.0"
#include "Python.h"
#include "structmember.h"
#include "src/KeccakHash.h"
struct module_state {
};
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif
#define MAX_DIGEST_SIZE 2060
typedef struct {
PyObject_HEAD
int hashbitlen;
int outputlen;
Keccak_HashInstance state;
} SHAobject;
static void SHAcopy(SHAobject *src, SHAobject *dest)
{
dest->hashbitlen = src->hashbitlen;
dest->outputlen = src->outputlen;
memcpy(&dest->state, &src->state, sizeof(src->state));
}
static PyTypeObject SHA3type;
static SHAobject *
newSHA3object(void)
{
return (SHAobject *)PyObject_New(SHAobject, &SHA3type);
}
static void
SHA_dealloc(PyObject *ptr)
{
PyObject_Del(ptr);
}
PyDoc_STRVAR(SHA3_copy__doc__, "Return a copy of the hash object.");
static PyObject *
SHA3_copy(SHAobject *self, PyObject *unused)
{
SHAobject *newobj = NULL;
if ((newobj = newSHA3object()) == NULL) {
return NULL;
}
SHAcopy(self, newobj);
return (PyObject *)newobj;
}
PyDoc_STRVAR(SHA3_digest__doc__,
"Return the digest value as a string of binary data.");
static PyObject *
SHA3_digest(SHAobject *self, PyObject *unused)
{
unsigned char *digest = malloc(self->outputlen / 8 + 1); //[MAX_DIGEST_SIZE];
SHAobject temp;
SHAcopy(self, &temp);
Keccak_HashFinal(&temp.state, digest);
if (self->hashbitlen > 512) { // SHAKE
Keccak_HashSqueeze(&temp.state, digest, self->outputlen);
}
PyObject *ret = PyBytes_FromStringAndSize((const char *)digest, self->outputlen / 8);
free(digest);
return ret;
}
PyDoc_STRVAR(SHA3_squeeze__doc__,
"Squeeze the digest and return the digest value as a string of binary data.");
static PyObject *
SHA3_squeeze(SHAobject *self, PyObject *args)
{
int outputlen;
if (!PyArg_ParseTuple(args, "i", &outputlen))
return NULL;
self->outputlen = outputlen;
return SHA3_digest(self, NULL);
}
PyDoc_STRVAR(SHA3_update__doc__,
"Update this hash object's state with the provided string.");
static PyObject *
SHA3_update(SHAobject *self, PyObject *args)
{
unsigned char *cp;
int len;
if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
return NULL;
Keccak_HashUpdate(&self->state, cp, len * 8);
Py_INCREF(Py_None);
return Py_None;
}
PyDoc_STRVAR(SHA3_init__doc__,
"Init this hash object's state.");
static PyObject *
SHA3_init(SHAobject *self, PyObject *args)
{
int hashbitlen;
int outputlen;
if (!PyArg_ParseTuple(args, "ii", &hashbitlen, &outputlen))
return NULL;
self->hashbitlen = hashbitlen;
self->outputlen = outputlen;
switch (hashbitlen) {
case 224:
Keccak_HashInitialize_SHA3_224(&self->state);
break;
case 256:
Keccak_HashInitialize_SHA3_256(&self->state);
break;
case 384:
Keccak_HashInitialize_SHA3_384(&self->state);
break;
case 512:
Keccak_HashInitialize_SHA3_512(&self->state);
break;
// HACK: This is pretty ugly :-)
case 10128:
Keccak_HashInitialize_SHAKE128(&self->state);
break;
case 10256:
Keccak_HashInitialize_SHAKE256(&self->state);
break;
};
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef SHA_methods[] = {
{"copy", (PyCFunction)SHA3_copy, METH_NOARGS, SHA3_copy__doc__},
{"digest", (PyCFunction)SHA3_digest, METH_NOARGS, SHA3_digest__doc__},
{"squeeze", (PyCFunction)SHA3_squeeze, METH_VARARGS, SHA3_squeeze__doc__},
{"update", (PyCFunction)SHA3_update, METH_VARARGS, SHA3_update__doc__},
{"init" , (PyCFunction)SHA3_init, METH_VARARGS, SHA3_init__doc__},
{NULL, NULL} /* sentinel */
};
static PyGetSetDef SHA_getseters[] = {
{NULL} /* Sentinel */
};
static PyMemberDef SHA_members[] = {
{NULL} /* Sentinel */
};
static PyTypeObject SHA3type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_sha3.sha3", /*tp_name*/
sizeof(SHAobject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
SHA_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
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*/
SHA_methods, /* tp_methods */
SHA_members, /* tp_members */
SHA_getseters, /* tp_getset */
};
PyDoc_STRVAR(SHA3_new__doc__,
"Return a new SHA-3 hash object.");
static PyObject *
SHA3_new(PyObject *self, PyObject *args, PyObject *kwdict)
{
SHAobject *new;
if ((new = newSHA3object()) == NULL)
return NULL;
if (PyErr_Occurred()) {
Py_DECREF(new);
return NULL;
}
return (PyObject *)new;
}
static struct PyMethodDef SHA_functions[] = {
{"sha3", (PyCFunction)SHA3_new, METH_VARARGS|METH_KEYWORDS, SHA3_new__doc__},
{NULL, NULL} /* Sentinel */
};
//PyMODINIT_FUNC
//init_sha3(void)
#if PY_MAJOR_VERSION >= 3
static int sha3_traverse(PyObject *m, visitproc visit, void *arg) {
//Py_VISIT(GETSTATE(m)->AuthenticationError);
return 0;
}
static int sha3_clear(PyObject *m) {
//Py_CLEAR(GETSTATE(m)->AuthenticationError);
return 0;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"_sha3",
NULL,
sizeof(struct module_state),
SHA_functions,
NULL,
sha3_traverse,
sha3_clear,
NULL
};
#define INITERROR return NULL
PyObject *
PyInit__sha3(void)
#else
#define INITERROR return
void
init_sha3(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule3("_sha3", SHA_functions, "Module for FIPS202 SHA3/SHAKE");
#endif
if (module == NULL)
INITERROR;
struct module_state *st = GETSTATE(module);
Py_TYPE(&SHA3type) = &PyType_Type;
if (PyType_Ready(&SHA3type) < 0) {
INITERROR;
}
{
#if PY_MAJOR_VERSION < 3
PyObject *s = PyString_FromString(_VERSION);
#else
PyObject *s = PyUnicode_FromString(_VERSION);
#endif
PyObject *dict = PyModule_GetDict(module);
PyDict_SetItemString(dict, "__version__", s);
Py_DECREF(s);
}
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}