Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(48745)

Delta Between Two Patch Sets: Modules/_sha3/sha3module.c

Issue 16113: Add SHA-3 (Keccak) support
Left Patch Set: Created 6 years, 11 months ago
Right Patch Set: Created 3 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Modules/_sha3/README.txt ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* SHA3 module 1 /* SHA3 module
2 * 2 *
3 * This module provides an interface to the SHA3 algorithm 3 * This module provides an interface to the SHA3 algorithm
4 * 4 *
5 * See below for information about the original code this module was 5 * See below for information about the original code this module was
6 * based upon. Additional work performed by: 6 * based upon. Additional work performed by:
7 * 7 *
8 * Andrew Kuchling (amk@amk.ca) 8 * Andrew Kuchling (amk@amk.ca)
9 * Greg Stein (gstein@lyra.org) 9 * Greg Stein (gstein@lyra.org)
10 * Trevor Perrin (trevp@trevp.net) 10 * Trevor Perrin (trevp@trevp.net)
11 * Gregory P. Smith (greg@krypto.org)
11 * 12 *
12 * Copyright (C) 2012 Christian Heimes (christian@python.org) 13 * Copyright (C) 2012-2016 Christian Heimes (christian@python.org)
13 * Licensed to PSF under a Contributor Agreement. 14 * Licensed to PSF under a Contributor Agreement.
14 * 15 *
15 */ 16 */
16 17
17 /* SHA3 objects */
18
19 #include "Python.h" 18 #include "Python.h"
19 #include "pystrhex.h"
20 #include "../hashlib.h" 20 #include "../hashlib.h"
21 #include "keccak/KeccakNISTInterface.h" 21
22 22 /* **************************************************************************
23 /* In order to avoid name clashes with other software I had to declare all 23 * SHA-3 (Keccak) and SHAKE
24 * Kebbak functions and global data as static. The code is directly included 24 *
gregory.p.smith 2012/10/05 08:29:56 Kebbak or Keccak? be consistent in our ASCII spel
christian.heimes 2012/10/05 11:16:27 It's a typo. The SHA-3 algorithm is called Keccak.
25 * here to access the static functions. 25 * The code is based on KeccakCodePackage from 2016-04-23
26 */ 26 * commit 647f93079afc4ada3d23737477a6e52511ca41fd
27 #include "keccak/KeccakNISTInterface.c" 27 *
28 #include "keccak/KeccakSponge.c" 28 * The reference implementation is altered in this points:
29 #if SIZEOF_VOID_P == 4 29 * - C++ comments are converted to ANSI C comments.
30 #include "keccak/KeccakF-1600-opt32.c" 30 * - all function names are mangled
31 #elif SIZEOF_VOID_P == 8 31 * - typedef for UINT64 is commented out.
32 #include "keccak/KeccakF-1600-opt64.c" 32 * - brg_endian.h is removed
33 #endif 33 *
34 34 * *************************************************************************/
35 #define SHA3_BLOCKSIZE 200 /* 1600 bits */ 35
36 #define SHA3_MAX_DIGESTSIZE 64 /* 512 bits */ 36 #ifdef __sparc
37 #define SHA3_state hashState 37 /* opt64 uses un-aligned memory access that causes a BUS error with msg
38 #define SHA3_init Init 38 * 'invalid address alignment' on SPARC. */
39 #define SHA3_process Update 39 #define KeccakOpt 32
40 #define SHA3_done Final 40 #elif SIZEOF_VOID_P == 8 && defined(PY_UINT64_T)
41 #define SHA3_copystate(dest, src) memcpy(&dest, &src, sizeof(SHA3_state)) 41 /* opt64 works only for 64bit platforms with unsigned int64 */
42 #define SHA3_clearstate(state) memset(&state, 0, sizeof(SHA3_state)) 42 #define KeccakOpt 64
43 #else
44 /* opt32 is used for the remaining 32 and 64bit platforms */
45 #define KeccakOpt 32
46 #endif
47
48 #if KeccakOpt == 64 && defined(PY_UINT64_T)
49 /* 64bit platforms with unsigned int64 */
50 typedef PY_UINT64_T UINT64;
51 typedef unsigned char UINT8;
52 #endif
53
54 /* replacement for brg_endian.h */
55 #define IS_LITTLE_ENDIAN 1234
56 #define IS_BIG_ENDIAN 4321
57 #if PY_LITTLE_ENDIAN
58 #define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
59 #endif
60 #if PY_BIG_ENDIAN
61 #define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
62 #endif
63
64 /* mangle names */
65 #define KeccakF1600_FastLoop_Absorb _PySHA3_KeccakF1600_FastLoop_Absorb
66 #define Keccak_HashFinal _PySHA3_Keccak_HashFinal
67 #define Keccak_HashInitialize _PySHA3_Keccak_HashInitialize
68 #define Keccak_HashSqueeze _PySHA3_Keccak_HashSqueeze
69 #define Keccak_HashUpdate _PySHA3_Keccak_HashUpdate
70 #define KeccakP1600_AddBytes _PySHA3_KeccakP1600_AddBytes
71 #define KeccakP1600_AddBytesInLane _PySHA3_KeccakP1600_AddBytesInLane
72 #define KeccakP1600_AddLanes _PySHA3_KeccakP1600_AddLanes
73 #define KeccakP1600_ExtractAndAddBytes _PySHA3_KeccakP1600_ExtractAndAddBytes
74 #define KeccakP1600_ExtractAndAddBytesInLane _PySHA3_KeccakP1600_ExtractAndAddBy tesInLane
75 #define KeccakP1600_ExtractAndAddLanes _PySHA3_KeccakP1600_ExtractAndAddLanes
76 #define KeccakP1600_ExtractBytes _PySHA3_KeccakP1600_ExtractBytes
77 #define KeccakP1600_ExtractBytesInLane _PySHA3_KeccakP1600_ExtractBytesInLane
78 #define KeccakP1600_ExtractLanes _PySHA3_KeccakP1600_ExtractLanes
79 #define KeccakP1600_Initialize _PySHA3_KeccakP1600_Initialize
80 #define KeccakP1600_OverwriteBytes _PySHA3_KeccakP1600_OverwriteBytes
81 #define KeccakP1600_OverwriteBytesInLane _PySHA3_KeccakP1600_OverwriteBytesInLan e
82 #define KeccakP1600_OverwriteLanes _PySHA3_KeccakP1600_OverwriteLanes
83 #define KeccakP1600_OverwriteWithZeroes _PySHA3_KeccakP1600_OverwriteWithZeroes
84 #define KeccakP1600_Permute_12rounds _PySHA3_KeccakP1600_Permute_12rounds
85 #define KeccakP1600_Permute_24rounds _PySHA3_KeccakP1600_Permute_24rounds
86 #define KeccakWidth1600_Sponge _PySHA3_KeccakWidth1600_Sponge
87 #define KeccakWidth1600_SpongeAbsorb _PySHA3_KeccakWidth1600_SpongeAbsorb
88 #define KeccakWidth1600_SpongeAbsorbLastFewBits _PySHA3_KeccakWidth1600_SpongeAb sorbLastFewBits
89 #define KeccakWidth1600_SpongeInitialize _PySHA3_KeccakWidth1600_SpongeInitializ e
90 #define KeccakWidth1600_SpongeSqueeze _PySHA3_KeccakWidth1600_SpongeSqueeze
91 #if KeccakOpt == 32
92 #define KeccakP1600_AddByte _PySHA3_KeccakP1600_AddByte
93 #define KeccakP1600_Permute_Nrounds _PySHA3_KeccakP1600_Permute_Nrounds
94 #define KeccakP1600_SetBytesInLaneToZero _PySHA3_KeccakP1600_SetBytesInLaneToZer o
95 #endif
96
97 /* we are only interested in KeccakP1600 */
98 #define KeccakP200_excluded 1
99 #define KeccakP400_excluded 1
100 #define KeccakP800_excluded 1
101
102 /* inline all Keccak dependencies */
103 #include "kcp/KeccakHash.h"
104 #include "kcp/KeccakSponge.h"
105 #include "kcp/KeccakHash.c"
106 #include "kcp/KeccakSponge.c"
107 #if KeccakOpt == 64
108 #include "kcp/KeccakP-1600-opt64.c"
109 #elif KeccakOpt == 32
110 #include "kcp/KeccakP-1600-inplace32BI.c"
111 #endif
112
113 #define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */
114 #define SHA3_state Keccak_HashInstance
115 #define SHA3_init Keccak_HashInitialize
116 #define SHA3_process Keccak_HashUpdate
117 #define SHA3_done Keccak_HashFinal
118 #define SHA3_squeeze Keccak_HashSqueeze
119 #define SHA3_copystate(dest, src) memcpy(&(dest), &(src), sizeof(SHA3_state))
120
121
122 /*[clinic input]
123 module _sha3
124 class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ"
125 class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ"
126 class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ"
127 class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ"
128 class _sha3.shake_128 "SHA3object *" "&SHAKE128type"
129 class _sha3.shake_256 "SHA3object *" "&SHAKE256type"
130 [clinic start generated code]*/
131 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/
43 132
44 /* The structure for storing SHA3 info */ 133 /* The structure for storing SHA3 info */
134
135 #define PY_WITH_KECCAK 0
45 136
46 typedef struct { 137 typedef struct {
47 PyObject_HEAD 138 PyObject_HEAD
48 int hashbitlen;
49 SHA3_state hash_state; 139 SHA3_state hash_state;
140 #ifdef WITH_THREAD
141 PyThread_type_lock lock;
142 #endif
50 } SHA3object; 143 } SHA3object;
51 144
52 static PyTypeObject SHA3type; 145 static PyTypeObject SHA3_224type;
53 146 static PyTypeObject SHA3_256type;
147 static PyTypeObject SHA3_384type;
148 static PyTypeObject SHA3_512type;
149 #ifdef PY_WITH_KECCAK
150 static PyTypeObject Keccak_224type;
151 static PyTypeObject Keccak_256type;
152 static PyTypeObject Keccak_384type;
153 static PyTypeObject Keccak_512type;
154 #endif
155 static PyTypeObject SHAKE128type;
156 static PyTypeObject SHAKE256type;
157
158 #include "clinic/sha3module.c.h"
54 159
55 static SHA3object * 160 static SHA3object *
56 newSHA3object(int hashbitlen) 161 newSHA3object(PyTypeObject *type)
57 { 162 {
58 SHA3object *newobj; 163 SHA3object *newobj;
59 164 newobj = (SHA3object *)PyObject_New(SHA3object, type);
60 /* check hashbitlen */ 165 if (newobj == NULL) {
61 switch(hashbitlen) { 166 return NULL;
62 /* supported hash length */ 167 }
63 case 224: 168 #ifdef WITH_THREAD
64 break; 169 newobj->lock = NULL;
65 case 256: 170 #endif
66 break;
67 case 384:
68 break;
69 case 512:
70 break;
71 case 0:
72 /* arbitrarily-long output isn't supported by this module */
73 default:
74 /* everything else is an error */
75 PyErr_SetString(PyExc_ValueError,
76 "hashbitlen must be one of 224, 256, 384 or 512.");
77 return NULL;
78 }
79 newobj = (SHA3object *)PyObject_New(SHA3object, &SHA3type);
80 newobj->hashbitlen = hashbitlen;
81 return newobj; 171 return newobj;
82 } 172 }
83 173
84 174
175 /*[clinic input]
176 @classmethod
177 _sha3.sha3_224.__new__ as py_sha3_new
178 string as data: object = NULL
179
180 Return a new SHA3 hash object with a hashbit length of 28 bytes.
181 [clinic start generated code]*/
182
183 static PyObject *
184 py_sha3_new_impl(PyTypeObject *type, PyObject *data)
185 /*[clinic end generated code: output=8d5c34279e69bf09 input=d7c582b950a858b6]*/
186 {
187 SHA3object *self = NULL;
188 Py_buffer buf = {NULL, NULL};
189 HashReturn res;
190
191 self = newSHA3object(type);
192 if (self == NULL) {
193 goto error;
194 }
195
196 if (type == &SHA3_224type) {
197 res = Keccak_HashInitialize_SHA3_224(&self->hash_state);
198 } else if (type == &SHA3_256type) {
199 res = Keccak_HashInitialize_SHA3_256(&self->hash_state);
200 } else if (type == &SHA3_384type) {
201 res = Keccak_HashInitialize_SHA3_384(&self->hash_state);
202 } else if (type == &SHA3_512type) {
203 res = Keccak_HashInitialize_SHA3_512(&self->hash_state);
204 #ifdef PY_WITH_KECCAK
205 } else if (type == &Keccak_224type) {
206 res = Keccak_HashInitialize(&self->hash_state, 1152, 448, 224, 0x01);
207 } else if (type == &Keccak_256type) {
208 res = Keccak_HashInitialize(&self->hash_state, 1088, 512, 256, 0x01);
209 } else if (type == &Keccak_384type) {
210 res = Keccak_HashInitialize(&self->hash_state, 832, 768, 384, 0x01);
211 } else if (type == &Keccak_512type) {
212 res = Keccak_HashInitialize(&self->hash_state, 576, 1024, 512, 0x01);
213 #endif
214 } else if (type == &SHAKE128type) {
215 res = Keccak_HashInitialize_SHAKE128(&self->hash_state);
216 } else if (type == &SHAKE256type) {
217 res = Keccak_HashInitialize_SHAKE256(&self->hash_state);
218 } else {
219 PyErr_BadInternalCall();
220 goto error;
221 }
222
223 if (data) {
224 GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
225 #ifdef WITH_THREAD
226 if (buf.len >= HASHLIB_GIL_MINSIZE) {
227 /* invariant: New objects can't be accessed by other code yet,
228 * thus it's safe to release the GIL without locking the object.
229 */
230 Py_BEGIN_ALLOW_THREADS
231 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
232 Py_END_ALLOW_THREADS
233 }
234 else {
235 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
236 }
237 #else
238 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
239 #endif
240 if (res != SUCCESS) {
241 PyErr_SetString(PyExc_RuntimeError,
242 "internal error in SHA3 Update()");
243 goto error;
244 }
245 PyBuffer_Release(&buf);
246 }
247
248 return (PyObject *)self;
249
250 error:
251 if (self) {
252 Py_DECREF(self);
253 }
254 if (data && buf.obj) {
255 PyBuffer_Release(&buf);
256 }
257 return NULL;
258 }
259
260
85 /* Internal methods for a hash object */ 261 /* Internal methods for a hash object */
86 262
87 static void 263 static void
88 SHA3_dealloc(PyObject *ptr) 264 SHA3_dealloc(SHA3object *self)
89 { 265 {
90 PyObject_Del(ptr); 266 #ifdef WITH_THREAD
91 } 267 if (self->lock) {
92 268 PyThread_free_lock(self->lock);
93 static int 269 }
94 SHA3_clear(SHA3object *self) 270 #endif
95 { 271 PyObject_Del(self);
96 SHA3_clearstate(self->hash_state);
97 return 0;
98 } 272 }
99 273
100 274
101 /* External methods for a hash object */ 275 /* External methods for a hash object */
102 276
103 PyDoc_STRVAR(SHA3_copy__doc__, "Return a copy of the hash object."); 277
104 278 /*[clinic input]
105 static PyObject * 279 _sha3.sha3_224.copy
106 SHA3_copy(SHA3object *self, PyObject *unused) 280
281 Return a copy of the hash object.
282 [clinic start generated code]*/
283
284 static PyObject *
285 _sha3_sha3_224_copy_impl(SHA3object *self)
286 /*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/
107 { 287 {
108 SHA3object *newobj; 288 SHA3object *newobj;
109 289
110 if ((newobj = newSHA3object(self->hashbitlen)) == NULL) { 290 if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
111 return NULL; 291 return NULL;
112 } 292 }
293 ENTER_HASHLIB(self);
113 SHA3_copystate(newobj->hash_state, self->hash_state); 294 SHA3_copystate(newobj->hash_state, self->hash_state);
295 LEAVE_HASHLIB(self);
114 return (PyObject *)newobj; 296 return (PyObject *)newobj;
115 } 297 }
116 298
117 299
118 PyDoc_STRVAR(SHA3_digest__doc__, 300 /*[clinic input]
119 "Return the digest value as a string of binary data."); 301 _sha3.sha3_224.digest
120 302
121 static PyObject * 303 Return the digest value as a string of binary data.
122 SHA3_digest(SHA3object *self, PyObject *unused) 304 [clinic start generated code]*/
305
306 static PyObject *
307 _sha3_sha3_224_digest_impl(SHA3object *self)
308 /*[clinic end generated code: output=fd531842e20b2d5b input=a5807917d219b30e]*/
123 { 309 {
124 unsigned char digest[SHA3_MAX_DIGESTSIZE]; 310 unsigned char digest[SHA3_MAX_DIGESTSIZE];
125 SHA3_state temp; 311 SHA3_state temp;
126 HashReturn res; 312 HashReturn res;
127 313
314 ENTER_HASHLIB(self);
128 SHA3_copystate(temp, self->hash_state); 315 SHA3_copystate(temp, self->hash_state);
129 res = SHA3_done((hashState*)&temp, digest); 316 LEAVE_HASHLIB(self);
130 SHA3_clearstate(temp); 317 res = SHA3_done(&temp, digest);
131 if (res != SUCCESS) { 318 if (res != SUCCESS) {
132 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); 319 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
133 return NULL; 320 return NULL;
134 } 321 }
135 return PyBytes_FromStringAndSize((const char *)digest, 322 return PyBytes_FromStringAndSize((const char *)digest,
136 self->hashbitlen / 8); 323 self->hash_state.fixedOutputLength / 8);
137 } 324 }
138 325
139 326
140 PyDoc_STRVAR(SHA3_hexdigest__doc__, 327 /*[clinic input]
141 "Return the digest value as a string of hexadecimal digits."); 328 _sha3.sha3_224.hexdigest
142 329
143 static PyObject * 330 Return the digest value as a string of hexadecimal digits.
144 SHA3_hexdigest(SHA3object *self, PyObject *unused) 331 [clinic start generated code]*/
332
333 static PyObject *
334 _sha3_sha3_224_hexdigest_impl(SHA3object *self)
335 /*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/
145 { 336 {
146 unsigned char digest[SHA3_MAX_DIGESTSIZE]; 337 unsigned char digest[SHA3_MAX_DIGESTSIZE];
147 SHA3_state temp; 338 SHA3_state temp;
148 HashReturn res; 339 HashReturn res;
149 PyObject *retval;
150 Py_UCS1 *hex_digest;
151 int digestlen, i, j;
152 340
153 /* Get the raw (binary) digest value */ 341 /* Get the raw (binary) digest value */
342 ENTER_HASHLIB(self);
154 SHA3_copystate(temp, self->hash_state); 343 SHA3_copystate(temp, self->hash_state);
155 res = SHA3_done((hashState*)&temp, digest); 344 LEAVE_HASHLIB(self);
156 SHA3_clearstate(temp); 345 res = SHA3_done(&temp, digest);
157 if (res != SUCCESS) { 346 if (res != SUCCESS) {
158 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); 347 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
159 return NULL; 348 return NULL;
160 } 349 }
161 350 return _Py_strhex((const char *)digest,
162 /* Create a new string */ 351 self->hash_state.fixedOutputLength / 8);
163 digestlen = self->hashbitlen / 8; 352 }
164 retval = PyUnicode_New(digestlen * 2, 127); 353
165 if (!retval) 354
166 return NULL; 355 /*[clinic input]
167 hex_digest = PyUnicode_1BYTE_DATA(retval); 356 _sha3.sha3_224.update
168 357
169 /* Make hex version of the digest */ 358 obj: object
gregory.p.smith 2012/10/05 08:29:56 fyi - this hexdigest loop is copied in all of our
christian.heimes 2012/10/05 11:16:27 Agreed! http://bugs.python.org/issue4821
170 for(i=j=0; i < digestlen; i++) { 359 /
171 unsigned char c; 360
172 c = (digest[i] >> 4) & 0xf; 361 Update this hash object's state with the provided string.
173 hex_digest[j++] = Py_hexdigits[c]; 362 [clinic start generated code]*/
174 c = (digest[i] & 0xf); 363
175 hex_digest[j++] = Py_hexdigits[c]; 364 static PyObject *
176 } 365 _sha3_sha3_224_update(SHA3object *self, PyObject *obj)
177 assert(_PyUnicode_CheckConsistency(retval, 1)); 366 /*[clinic end generated code: output=06721d55b483e0af input=be44bf0d1c279791]*/
178 return retval; 367 {
179 }
180
181 PyDoc_STRVAR(SHA3_update__doc__,
182 "Update this hash object's state with the provided string.");
183
184 static PyObject *
185 SHA3_update(SHA3object *self, PyObject *args)
186 {
187 PyObject *obj;
188 Py_buffer buf; 368 Py_buffer buf;
189 HashReturn res; 369 HashReturn res;
190 370
191 if (!PyArg_ParseTuple(args, "O:update", &obj))
192 return NULL;
193
194 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); 371 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
195 372
196 /* add new data, the function takes the length in bits not bytes */ 373 /* add new data, the function takes the length in bits not bytes */
197 res = SHA3_process((hashState*)&self->hash_state, buf.buf, buf.len * 8); 374 #ifdef WITH_THREAD
AntoinePitrou 2012/10/04 23:39:50 You may want to release the GIL when the buffer is
christian.heimes 2012/10/05 11:16:27 The next patch will release the GIL.
375 if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
376 self->lock = PyThread_allocate_lock();
377 }
378 /* Once a lock exists all code paths must be synchronized. We have to
379 * release the GIL even for small buffers as acquiring the lock may take
380 * an unlimited amount of time when another thread updates this object
381 * with lots of data. */
382 if (self->lock) {
383 Py_BEGIN_ALLOW_THREADS
384 PyThread_acquire_lock(self->lock, 1);
385 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
386 PyThread_release_lock(self->lock);
387 Py_END_ALLOW_THREADS
388 }
389 else {
390 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
391 }
392 #else
393 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
394 #endif
395
198 if (res != SUCCESS) { 396 if (res != SUCCESS) {
199 PyBuffer_Release(&buf); 397 PyBuffer_Release(&buf);
200 PyErr_SetString(PyExc_RuntimeError, 398 PyErr_SetString(PyExc_RuntimeError,
201 "internal error in SHA3 Update()"); 399 "internal error in SHA3 Update()");
202 return NULL; 400 return NULL;
203 } 401 }
204 402
205 PyBuffer_Release(&buf); 403 PyBuffer_Release(&buf);
206 Py_INCREF(Py_None); 404 Py_INCREF(Py_None);
207 return Py_None; 405 return Py_None;
208 } 406 }
209 407
408
210 static PyMethodDef SHA3_methods[] = { 409 static PyMethodDef SHA3_methods[] = {
211 {"copy", (PyCFunction)SHA3_copy, METH_NOARGS, 410 _SHA3_SHA3_224_COPY_METHODDEF
212 SHA3_copy__doc__}, 411 _SHA3_SHA3_224_DIGEST_METHODDEF
213 {"digest", (PyCFunction)SHA3_digest, METH_NOARGS, 412 _SHA3_SHA3_224_HEXDIGEST_METHODDEF
214 SHA3_digest__doc__}, 413 _SHA3_SHA3_224_UPDATE_METHODDEF
215 {"hexdigest", (PyCFunction)SHA3_hexdigest, METH_NOARGS,
216 SHA3_hexdigest__doc__},
217 {"update", (PyCFunction)SHA3_update, METH_VARARGS,
218 SHA3_update__doc__},
219 {NULL, NULL} /* sentinel */ 414 {NULL, NULL} /* sentinel */
220 }; 415 };
221 416
417
222 static PyObject * 418 static PyObject *
223 SHA3_get_block_size(SHA3object *self, void *closure) 419 SHA3_get_block_size(SHA3object *self, void *closure)
224 { 420 {
225 return PyLong_FromLong(SHA3_BLOCKSIZE); 421 int rate = self->hash_state.sponge.rate;
226 } 422 return PyLong_FromLong(rate / 8);
423 }
424
227 425
228 static PyObject * 426 static PyObject *
229 SHA3_get_name(SHA3object *self, void *closure) 427 SHA3_get_name(SHA3object *self, void *closure)
230 { 428 {
231 return PyUnicode_FromFormat("sha3_%i", self->hashbitlen); 429 PyTypeObject *type = Py_TYPE(self);
232 } 430 if (type == &SHA3_224type) {
431 return PyUnicode_FromString("sha3_224");
432 } else if (type == &SHA3_256type) {
433 return PyUnicode_FromString("sha3_256");
434 } else if (type == &SHA3_384type) {
435 return PyUnicode_FromString("sha3_384");
436 } else if (type == &SHA3_512type) {
437 return PyUnicode_FromString("sha3_512");
438 #ifdef PY_WITH_KECCAK
439 } else if (type == &Keccak_224type) {
440 return PyUnicode_FromString("keccak_224");
441 } else if (type == &Keccak_256type) {
442 return PyUnicode_FromString("keccak_256");
443 } else if (type == &Keccak_384type) {
444 return PyUnicode_FromString("keccak_384");
445 } else if (type == &Keccak_512type) {
446 return PyUnicode_FromString("keccak_512");
447 #endif
448 } else if (type == &SHAKE128type) {
449 return PyUnicode_FromString("shake_128");
450 } else if (type == &SHAKE256type) {
451 return PyUnicode_FromString("shake_256");
452 } else {
453 PyErr_BadInternalCall();
454 return NULL;
455 }
456 }
457
233 458
234 static PyObject * 459 static PyObject *
235 SHA3_get_digest_size(SHA3object *self, void *closure) 460 SHA3_get_digest_size(SHA3object *self, void *closure)
236 { 461 {
237 return PyLong_FromLong(self->hashbitlen / 8); 462 return PyLong_FromLong(self->hash_state.fixedOutputLength / 8);
463 }
464
465
466 static PyObject *
467 SHA3_get_capacity_bits(SHA3object *self, void *closure)
468 {
469 int capacity = 1600 - self->hash_state.sponge.rate;
470 return PyLong_FromLong(capacity);
471 }
472
473
474 static PyObject *
475 SHA3_get_rate_bits(SHA3object *self, void *closure)
476 {
477 unsigned int rate = self->hash_state.sponge.rate;
478 return PyLong_FromLong(rate);
479 }
480
481 static PyObject *
482 SHA3_get_suffix(SHA3object *self, void *closure)
483 {
484 unsigned char suffix[2];
485 suffix[0] = self->hash_state.delimitedSuffix;
486 suffix[1] = 0;
487 return PyBytes_FromStringAndSize((const char *)suffix, 1);
238 } 488 }
239 489
240 490
241 static PyGetSetDef SHA3_getseters[] = { 491 static PyGetSetDef SHA3_getseters[] = {
242 {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL}, 492 {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
243 {"name", (getter)SHA3_get_name, NULL, NULL, NULL}, 493 {"name", (getter)SHA3_get_name, NULL, NULL, NULL},
244 {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL}, 494 {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL},
495 {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL},
496 {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL},
497 {"_suffix", (getter)SHA3_get_suffix, NULL, NULL, NULL},
245 {NULL} /* Sentinel */ 498 {NULL} /* Sentinel */
246 }; 499 };
247 500
248 static PyTypeObject SHA3type = { 501
249 PyVarObject_HEAD_INIT(NULL, 0) 502 #define SHA3_TYPE(type_obj, type_name, type_doc, type_methods) \
250 "_sha3.SHA3", /* tp_name */ 503 static PyTypeObject type_obj = { \
251 sizeof(SHA3object), /* tp_size */ 504 PyVarObject_HEAD_INIT(NULL, 0) \
252 0, /* tp_itemsize */ 505 type_name, /* tp_name */ \
253 /* methods */ 506 sizeof(SHA3object), /* tp_size */ \
254 SHA3_dealloc, /* tp_dealloc */ 507 0, /* tp_itemsize */ \
255 0, /* tp_print */ 508 /* methods */ \
256 0, /* tp_getattr */ 509 (destructor)SHA3_dealloc, /* tp_dealloc */ \
257 0, /* tp_setattr */ 510 0, /* tp_print */ \
258 0, /* tp_reserved */ 511 0, /* tp_getattr */ \
259 0, /* tp_repr */ 512 0, /* tp_setattr */ \
260 0, /* tp_as_number */ 513 0, /* tp_reserved */ \
261 0, /* tp_as_sequence */ 514 0, /* tp_repr */ \
262 0, /* tp_as_mapping */ 515 0, /* tp_as_number */ \
263 0, /* tp_hash */ 516 0, /* tp_as_sequence */ \
264 0, /* tp_call */ 517 0, /* tp_as_mapping */ \
265 0, /* tp_str */ 518 0, /* tp_hash */ \
266 0, /* tp_getattro */ 519 0, /* tp_call */ \
267 0, /* tp_setattro */ 520 0, /* tp_str */ \
268 0, /* tp_as_buffer */ 521 0, /* tp_getattro */ \
269 Py_TPFLAGS_DEFAULT, /* tp_flags */ 522 0, /* tp_setattro */ \
270 0, /* tp_doc */ 523 0, /* tp_as_buffer */ \
271 0, /* tp_traverse */ 524 Py_TPFLAGS_DEFAULT, /* tp_flags */ \
272 (inquiry)SHA3_clear, /* tp_clear */ 525 type_doc, /* tp_doc */ \
AntoinePitrou 2012/10/04 23:39:50 tp_clear is only used if the type is GC-enabled, w
christian.heimes 2012/10/05 11:16:27 Thanks, I've moved the SHA3_clearstate() call to t
273 0, /* tp_richcompare */ 526 0, /* tp_traverse */ \
274 0, /* tp_weaklistoffset */ 527 0, /* tp_clear */ \
275 0, /* tp_iter */ 528 0, /* tp_richcompare */ \
276 0, /* tp_iternext */ 529 0, /* tp_weaklistoffset */ \
277 SHA3_methods, /* tp_methods */ 530 0, /* tp_iter */ \
278 NULL, /* tp_members */ 531 0, /* tp_iternext */ \
279 SHA3_getseters, /* tp_getset */ 532 type_methods, /* tp_methods */ \
280 }; 533 NULL, /* tp_members */ \
281 534 SHA3_getseters, /* tp_getset */ \
282 535 0, /* tp_base */ \
283 /* constructor helper */ 536 0, /* tp_dict */ \
284 static PyObject * 537 0, /* tp_descr_get */ \
285 SHA3_factory(PyObject *args, PyObject *kwdict, const char *fmt, 538 0, /* tp_descr_set */ \
286 int hashbitlen) 539 0, /* tp_dictoffset */ \
287 { 540 0, /* tp_init */ \
288 SHA3object *newobj = NULL; 541 0, /* tp_alloc */ \
289 static char *kwlist[] = {"string", NULL}; 542 py_sha3_new, /* tp_new */ \
290 PyObject *data_obj = NULL; 543 }
291 Py_buffer buf;
292
293 if (!PyArg_ParseTupleAndKeywords(args, kwdict, fmt, kwlist,
294 &data_obj)) {
295 return NULL;
296 }
297
298 if (data_obj)
299 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
300
301 if ((newobj = newSHA3object(hashbitlen)) == NULL) {
302 goto error;
303 }
304
305 if (SHA3_init((hashState*)&newobj->hash_state, hashbitlen) != SUCCESS) {
306 PyErr_SetString(PyExc_RuntimeError,
307 "internal error in SHA3 Update()");
308 goto error;
309 }
310
311 if (data_obj) {
312 if (SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8) != SUCCESS) {
AntoinePitrou 2012/10/04 23:39:50 Same remark as in update() (about the GIL).
313 PyErr_SetString(PyExc_RuntimeError,
314 "internal error in SHA3 Update()");
315 goto error;
316 }
317 PyBuffer_Release(&buf);
318 }
319
320 return (PyObject *)newobj;
321
322 error:
323 if (newobj) {
324 SHA3_clearstate(newobj->hash_state);
325 }
326 if (data_obj) {
327 PyBuffer_Release(&buf);
328 }
329 return NULL;
330
331 }
332
333 PyDoc_STRVAR(sha3_224__doc__,
334 "sha3_224([string]) -> SHA3 object\n\
335 \n\
336 Return a new SHA3 hash object with a hashbit length of 28 bytes.");
337
338 static PyObject *
339 sha3_224(PyObject *self, PyObject *args, PyObject *kwdict)
340 {
341 return SHA3_factory(args, kwdict, "|O:sha3_224", 224);
342 }
343
344 544
345 PyDoc_STRVAR(sha3_256__doc__, 545 PyDoc_STRVAR(sha3_256__doc__,
346 "sha3_256([string]) -> SHA3 object\n\ 546 "sha3_256([string]) -> SHA3 object\n\
347 \n\ 547 \n\
348 Return a new SHA3 hash object with a hashbit length of 32 bytes."); 548 Return a new SHA3 hash object with a hashbit length of 32 bytes.");
349 549
350 static PyObject *
351 sha3_256(PyObject *self, PyObject *args, PyObject *kwdict)
352 {
353 return SHA3_factory(args, kwdict, "|O:sha3_256", 256);
354 }
355
356 PyDoc_STRVAR(sha3_384__doc__, 550 PyDoc_STRVAR(sha3_384__doc__,
357 "sha3_384([string]) -> SHA3 object\n\ 551 "sha3_384([string]) -> SHA3 object\n\
358 \n\ 552 \n\
359 Return a new SHA3 hash object with a hashbit length of 48 bytes."); 553 Return a new SHA3 hash object with a hashbit length of 48 bytes.");
360 554
361 static PyObject *
362 sha3_384(PyObject *self, PyObject *args, PyObject *kwdict)
363 {
364 return SHA3_factory(args, kwdict, "|O:sha3_384", 384);
365 }
366
367 PyDoc_STRVAR(sha3_512__doc__, 555 PyDoc_STRVAR(sha3_512__doc__,
368 "sha3_512([string]) -> SHA3 object\n\ 556 "sha3_512([string]) -> SHA3 object\n\
369 \n\ 557 \n\
370 Return a new SHA3 hash object with a hashbit length of 64 bytes."); 558 Return a new SHA3 hash object with a hashbit length of 64 bytes.");
371 559
372 static PyObject * 560 SHA3_TYPE(SHA3_224type, "_sha3.sha3_224", py_sha3_new__doc__, SHA3_methods);
373 sha3_512(PyObject *self, PyObject *args, PyObject *kwdict) 561 SHA3_TYPE(SHA3_256type, "_sha3.sha3_256", sha3_256__doc__, SHA3_methods);
374 { 562 SHA3_TYPE(SHA3_384type, "_sha3.sha3_384", sha3_384__doc__, SHA3_methods);
375 return SHA3_factory(args, kwdict, "|O:sha3_512", 512); 563 SHA3_TYPE(SHA3_512type, "_sha3.sha3_512", sha3_512__doc__, SHA3_methods);
376 } 564
377 565 #ifdef PY_WITH_KECCAK
378 566 PyDoc_STRVAR(keccak_224__doc__,
379 /* List of functions exported by this module */ 567 "keccak_224([string]) -> Keccak object\n\
380 static struct PyMethodDef SHA3_functions[] = { 568 \n\
381 {"sha3_224", (PyCFunction)sha3_224, METH_VARARGS|METH_KEYWORDS, 569 Return a new Keccak hash object with a hashbit length of 28 bytes.");
382 sha3_224__doc__}, 570
383 {"sha3_256", (PyCFunction)sha3_256, METH_VARARGS|METH_KEYWORDS, 571 PyDoc_STRVAR(keccak_256__doc__,
384 sha3_256__doc__}, 572 "keccak_256([string]) -> Keccak object\n\
385 {"sha3_384", (PyCFunction)sha3_384, METH_VARARGS|METH_KEYWORDS, 573 \n\
386 sha3_384__doc__}, 574 Return a new Keccak hash object with a hashbit length of 32 bytes.");
387 {"sha3_512", (PyCFunction)sha3_512, METH_VARARGS|METH_KEYWORDS, 575
388 sha3_512__doc__}, 576 PyDoc_STRVAR(keccak_384__doc__,
389 {NULL, NULL} /* Sentinel */ 577 "keccak_384([string]) -> Keccak object\n\
578 \n\
579 Return a new Keccak hash object with a hashbit length of 48 bytes.");
580
581 PyDoc_STRVAR(keccak_512__doc__,
582 "keccak_512([string]) -> Keccak object\n\
583 \n\
584 Return a new Keccak hash object with a hashbit length of 64 bytes.");
585
586 SHA3_TYPE(Keccak_224type, "_sha3.keccak_224", keccak_224__doc__, SHA3_methods);
587 SHA3_TYPE(Keccak_256type, "_sha3.keccak_256", keccak_256__doc__, SHA3_methods);
588 SHA3_TYPE(Keccak_384type, "_sha3.keccak_384", keccak_384__doc__, SHA3_methods);
589 SHA3_TYPE(Keccak_512type, "_sha3.keccak_512", keccak_512__doc__, SHA3_methods);
590 #endif
591
592
593 static PyObject *
594 _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
595 {
596 unsigned char *digest = NULL;
597 SHA3_state temp;
598 int res;
599 PyObject *result = NULL;
600
601 if ((digest = (unsigned char*)PyMem_Malloc(digestlen)) == NULL) {
602 return PyErr_NoMemory();
603 }
604
605 /* Get the raw (binary) digest value */
606 ENTER_HASHLIB(self);
607 SHA3_copystate(temp, self->hash_state);
608 LEAVE_HASHLIB(self);
609 res = SHA3_done(&temp, NULL);
610 if (res != SUCCESS) {
611 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 done()");
612 goto error;
613 }
614 res = SHA3_squeeze(&temp, digest, digestlen * 8);
615 if (res != SUCCESS) {
616 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Squeeze()");
617 return NULL;
618 }
619 if (hex) {
620 result = _Py_strhex((const char *)digest, digestlen);
621 } else {
622 result = PyBytes_FromStringAndSize((const char *)digest,
623 digestlen);
624 }
625 error:
626 if (digest != NULL) {
627 PyMem_Free(digest);
628 }
629 return result;
630 }
631
632
633 /*[clinic input]
634 _sha3.shake_128.digest
635
636 length: unsigned_long(bitwise=True)
637 \
638
639 Return the digest value as a string of binary data.
640 [clinic start generated code]*/
641
642 static PyObject *
643 _sha3_shake_128_digest_impl(SHA3object *self, unsigned long length)
644 /*[clinic end generated code: output=2313605e2f87bb8f input=608c8ca80ae9d115]*/
645 {
646 return _SHAKE_digest(self, length, 0);
647 }
648
649
650 /*[clinic input]
651 _sha3.shake_128.hexdigest
652
653 length: unsigned_long(bitwise=True)
654 \
655
656 Return the digest value as a string of hexadecimal digits.
657 [clinic start generated code]*/
658
659 static PyObject *
660 _sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length)
661 /*[clinic end generated code: output=bf8e2f1e490944a8 input=64e56b4760db4573]*/
662 {
663 return _SHAKE_digest(self, length, 1);
664 }
665
666
667 static PyMethodDef SHAKE_methods[] = {
668 _SHA3_SHA3_224_COPY_METHODDEF
669 _SHA3_SHAKE_128_DIGEST_METHODDEF
670 _SHA3_SHAKE_128_HEXDIGEST_METHODDEF
671 _SHA3_SHA3_224_UPDATE_METHODDEF
672 {NULL, NULL} /* sentinel */
390 }; 673 };
674
675 PyDoc_STRVAR(shake_128__doc__,
676 "shake_128([string]) -> SHAKE object\n\
677 \n\
678 Return a new SHAKE hash object.");
679
680 PyDoc_STRVAR(shake_256__doc__,
681 "shake_256([string]) -> SHAKE object\n\
682 \n\
683 Return a new SHAKE hash object.");
684
685 SHA3_TYPE(SHAKE128type, "_sha3.shake_128", shake_128__doc__, SHAKE_methods);
686 SHA3_TYPE(SHAKE256type, "_sha3.shake_256", shake_256__doc__, SHAKE_methods);
391 687
392 688
393 /* Initialize this module. */ 689 /* Initialize this module. */
394 static struct PyModuleDef _SHA3module = { 690 static struct PyModuleDef _SHA3module = {
395 PyModuleDef_HEAD_INIT, 691 PyModuleDef_HEAD_INIT,
396 "_sha3", 692 "_sha3",
397 NULL, 693 NULL,
398 -1, 694 -1,
399 SHA3_functions, 695 NULL,
400 NULL, 696 NULL,
401 NULL, 697 NULL,
402 NULL, 698 NULL,
403 NULL 699 NULL
404 }; 700 };
405 701
702
406 PyMODINIT_FUNC 703 PyMODINIT_FUNC
407 PyInit__sha3(void) 704 PyInit__sha3(void)
408 { 705 {
409 Py_TYPE(&SHA3type) = &PyType_Type; 706 PyObject *m = NULL;
410 if (PyType_Ready(&SHA3type) < 0) { 707
411 return NULL; 708 m = PyModule_Create(&_SHA3module);
412 } 709
413 710 #define init_sha3type(name, type) \
414 return PyModule_Create(&_SHA3module); 711 do { \
415 } 712 Py_TYPE(type) = &PyType_Type; \
713 if (PyType_Ready(type) < 0) { \
714 goto error; \
715 } \
716 Py_INCREF((PyObject *)type); \
717 if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \
718 goto error; \
719 } \
720 } while(0)
721
722 init_sha3type("sha3_224", &SHA3_224type);
723 init_sha3type("sha3_256", &SHA3_256type);
724 init_sha3type("sha3_384", &SHA3_384type);
725 init_sha3type("sha3_512", &SHA3_512type);
726 #ifdef PY_WITH_KECCAK
727 init_sha3type("keccak_224", &Keccak_224type);
728 init_sha3type("keccak_256", &Keccak_256type);
729 init_sha3type("keccak_384", &Keccak_384type);
730 init_sha3type("keccak_512", &Keccak_512type);
731 #endif
732 init_sha3type("shake_128", &SHAKE128type);
733 init_sha3type("shake_256", &SHAKE256type);
734
735 #undef init_sha3type
736
737 if (PyModule_AddIntConstant(m, "keccakopt", KeccakOpt) < 0) {
738 goto error;
739 }
740 if (PyModule_AddStringConstant(m, "implementation",
741 KeccakP1600_implementation) < 0) {
742 goto error;
743 }
744
745 return m;
746 error:
747 Py_DECREF(m);
748 return NULL;
749 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+