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

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 21
22 /* overwrite Keccak typedefs and defines with sane values from Python.h */ 22 /* **************************************************************************
23 #ifdef PY_UINT64_T 23 * SHA-3 (Keccak) and SHAKE
24 *
25 * The code is based on KeccakCodePackage from 2016-04-23
26 * commit 647f93079afc4ada3d23737477a6e52511ca41fd
27 *
28 * The reference implementation is altered in this points:
29 * - C++ comments are converted to ANSI C comments.
30 * - all function names are mangled
31 * - typedef for UINT64 is commented out.
32 * - brg_endian.h is removed
33 *
34 * *************************************************************************/
35
36 #ifdef __sparc
37 /* opt64 uses un-aligned memory access that causes a BUS error with msg
38 * 'invalid address alignment' on SPARC. */
39 #define KeccakOpt 32
40 #elif SIZEOF_VOID_P == 8 && defined(PY_UINT64_T)
41 /* opt64 works only for 64bit platforms with unsigned int64 */
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 */
24 typedef PY_UINT64_T UINT64; 50 typedef PY_UINT64_T UINT64;
25 #else 51 typedef unsigned char UINT8;
26 typedef unsigned long long int UINT64; 52 #endif
27 #endif 53
28 54 /* replacement for brg_endian.h */
29 #define IS_BIG_ENDIAN BIG_ENDIAN 55 #define IS_LITTLE_ENDIAN 1234
30 #define IS_LITTLE_ENDIAN LITTLE_ENDIAN 56 #define IS_BIG_ENDIAN 4321
31 #define PLATFORM_BYTE_ORDER BYTE_ORDER 57 #if PY_LITTLE_ENDIAN
32 58 #define PLATFORM_BYTE_ORDER IS_LITTLE_ENDIAN
33 /* In order to avoid name clashes with other software I had to declare all 59 #endif
34 * Keccak functions and global data as static. The code is directly included 60 #if PY_BIG_ENDIAN
35 * here to access the static functions. 61 #define PLATFORM_BYTE_ORDER IS_BIG_ENDIAN
36 */ 62 #endif
37 #include "keccak/KeccakNISTInterface.h" 63
38 #include "keccak/KeccakNISTInterface.c" 64 /* mangle names */
39 #include "keccak/KeccakSponge.c" 65 #define KeccakF1600_FastLoop_Absorb _PySHA3_KeccakF1600_FastLoop_Absorb
40 #if SIZEOF_VOID_P == 4 66 #define Keccak_HashFinal _PySHA3_Keccak_HashFinal
41 #include "keccak/KeccakF-1600-opt32.c" 67 #define Keccak_HashInitialize _PySHA3_Keccak_HashInitialize
42 #elif SIZEOF_VOID_P == 8 68 #define Keccak_HashSqueeze _PySHA3_Keccak_HashSqueeze
43 #include "keccak/KeccakF-1600-opt64.c" 69 #define Keccak_HashUpdate _PySHA3_Keccak_HashUpdate
44 #endif 70 #define KeccakP1600_AddBytes _PySHA3_KeccakP1600_AddBytes
45 71 #define KeccakP1600_AddBytesInLane _PySHA3_KeccakP1600_AddBytesInLane
46 #define SHA3_BLOCKSIZE 200 /* 1600 bits */ 72 #define KeccakP1600_AddLanes _PySHA3_KeccakP1600_AddLanes
47 #define SHA3_MAX_DIGESTSIZE 64 /* 512 bits */ 73 #define KeccakP1600_ExtractAndAddBytes _PySHA3_KeccakP1600_ExtractAndAddBytes
48 #define SHA3_state hashState 74 #define KeccakP1600_ExtractAndAddBytesInLane _PySHA3_KeccakP1600_ExtractAndAddBy tesInLane
49 #define SHA3_init Init 75 #define KeccakP1600_ExtractAndAddLanes _PySHA3_KeccakP1600_ExtractAndAddLanes
50 #define SHA3_process Update 76 #define KeccakP1600_ExtractBytes _PySHA3_KeccakP1600_ExtractBytes
51 #define SHA3_done Final 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
52 #define SHA3_copystate(dest, src) memcpy(&(dest), &(src), sizeof(SHA3_state)) 119 #define SHA3_copystate(dest, src) memcpy(&(dest), &(src), sizeof(SHA3_state))
53 #define SHA3_clearstate(state) memset(&(state), 0, 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]*/
54 132
55 /* The structure for storing SHA3 info */ 133 /* The structure for storing SHA3 info */
134
135 #define PY_WITH_KECCAK 0
56 136
57 typedef struct { 137 typedef struct {
58 PyObject_HEAD 138 PyObject_HEAD
59 int hashbitlen;
60 SHA3_state hash_state; 139 SHA3_state hash_state;
61 #ifdef WITH_THREAD 140 #ifdef WITH_THREAD
62 PyThread_type_lock lock; 141 PyThread_type_lock lock;
63 #endif 142 #endif
64
65 } SHA3object; 143 } SHA3object;
66 144
67 static PyTypeObject SHA3type; 145 static PyTypeObject SHA3_224type;
68 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"
69 159
70 static SHA3object * 160 static SHA3object *
71 newSHA3object(int hashbitlen) 161 newSHA3object(PyTypeObject *type)
72 { 162 {
73 SHA3object *newobj; 163 SHA3object *newobj;
74 164 newobj = (SHA3object *)PyObject_New(SHA3object, type);
75 /* check hashbitlen */
76 switch(hashbitlen) {
77 /* supported hash length */
78 case 224:
79 break;
80 case 256:
81 break;
82 case 384:
83 break;
84 case 512:
85 break;
86 case 0:
87 /* arbitrarily-long output isn't supported by this module */
88 default:
89 /* everything else is an error */
90 PyErr_SetString(PyExc_ValueError,
91 "hashbitlen must be one of 224, 256, 384 or 512.");
92 return NULL;
93 }
94 newobj = (SHA3object *)PyObject_New(SHA3object, &SHA3type);
95 if (newobj == NULL) { 165 if (newobj == NULL) {
96 return NULL; 166 return NULL;
97 } 167 }
98 newobj->hashbitlen = hashbitlen;
99 #ifdef WITH_THREAD 168 #ifdef WITH_THREAD
100 newobj->lock = NULL; 169 newobj->lock = NULL;
101 #endif 170 #endif
102 return newobj; 171 return newobj;
103 } 172 }
104 173
105 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
106 /* Internal methods for a hash object */ 261 /* Internal methods for a hash object */
107 262
108 static void 263 static void
109 SHA3_dealloc(SHA3object *self) 264 SHA3_dealloc(SHA3object *self)
110 { 265 {
111 SHA3_clearstate(self->hash_state);
112 #ifdef WITH_THREAD 266 #ifdef WITH_THREAD
113 if (self->lock) { 267 if (self->lock) {
114 PyThread_free_lock(self->lock); 268 PyThread_free_lock(self->lock);
115 } 269 }
116 #endif 270 #endif
117 PyObject_Del(self); 271 PyObject_Del(self);
118 } 272 }
119 273
120 274
121 /* External methods for a hash object */ 275 /* External methods for a hash object */
122 276
123 PyDoc_STRVAR(SHA3_copy__doc__, "Return a copy of the hash object."); 277
124 278 /*[clinic input]
125 static PyObject * 279 _sha3.sha3_224.copy
126 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]*/
127 { 287 {
128 SHA3object *newobj; 288 SHA3object *newobj;
129 289
130 if ((newobj = newSHA3object(self->hashbitlen)) == NULL) { 290 if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
131 return NULL; 291 return NULL;
132 } 292 }
133 ENTER_HASHLIB(self); 293 ENTER_HASHLIB(self);
134 SHA3_copystate(newobj->hash_state, self->hash_state); 294 SHA3_copystate(newobj->hash_state, self->hash_state);
135 LEAVE_HASHLIB(self); 295 LEAVE_HASHLIB(self);
136 return (PyObject *)newobj; 296 return (PyObject *)newobj;
137 } 297 }
138 298
139 299
140 PyDoc_STRVAR(SHA3_digest__doc__, 300 /*[clinic input]
141 "Return the digest value as a string of binary data."); 301 _sha3.sha3_224.digest
142 302
143 static PyObject * 303 Return the digest value as a string of binary data.
144 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]*/
145 { 309 {
146 unsigned char digest[SHA3_MAX_DIGESTSIZE]; 310 unsigned char digest[SHA3_MAX_DIGESTSIZE];
147 SHA3_state temp; 311 SHA3_state temp;
148 HashReturn res; 312 HashReturn res;
149 313
150 ENTER_HASHLIB(self); 314 ENTER_HASHLIB(self);
151 SHA3_copystate(temp, self->hash_state); 315 SHA3_copystate(temp, self->hash_state);
152 LEAVE_HASHLIB(self); 316 LEAVE_HASHLIB(self);
153 res = SHA3_done((hashState*)&temp, digest); 317 res = SHA3_done(&temp, digest);
154 SHA3_clearstate(temp);
155 if (res != SUCCESS) { 318 if (res != SUCCESS) {
156 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); 319 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
157 return NULL; 320 return NULL;
158 } 321 }
159 return PyBytes_FromStringAndSize((const char *)digest, 322 return PyBytes_FromStringAndSize((const char *)digest,
160 self->hashbitlen / 8); 323 self->hash_state.fixedOutputLength / 8);
161 } 324 }
162 325
163 326
164 PyDoc_STRVAR(SHA3_hexdigest__doc__, 327 /*[clinic input]
165 "Return the digest value as a string of hexadecimal digits."); 328 _sha3.sha3_224.hexdigest
166 329
167 static PyObject * 330 Return the digest value as a string of hexadecimal digits.
168 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]*/
169 { 336 {
170 unsigned char digest[SHA3_MAX_DIGESTSIZE]; 337 unsigned char digest[SHA3_MAX_DIGESTSIZE];
171 SHA3_state temp; 338 SHA3_state temp;
172 HashReturn res; 339 HashReturn res;
173 PyObject *retval;
174 Py_UCS1 *hex_digest;
175 int digestlen, i, j;
176 340
177 /* Get the raw (binary) digest value */ 341 /* Get the raw (binary) digest value */
178 ENTER_HASHLIB(self); 342 ENTER_HASHLIB(self);
179 SHA3_copystate(temp, self->hash_state); 343 SHA3_copystate(temp, self->hash_state);
180 LEAVE_HASHLIB(self); 344 LEAVE_HASHLIB(self);
181 res = SHA3_done((hashState*)&temp, digest); 345 res = SHA3_done(&temp, digest);
182 SHA3_clearstate(temp);
183 if (res != SUCCESS) { 346 if (res != SUCCESS) {
184 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()"); 347 PyErr_SetString(PyExc_RuntimeError, "internal error in SHA3 Final()");
185 return NULL; 348 return NULL;
186 } 349 }
187 350 return _Py_strhex((const char *)digest,
188 /* Create a new string */ 351 self->hash_state.fixedOutputLength / 8);
189 digestlen = self->hashbitlen / 8; 352 }
190 retval = PyUnicode_New(digestlen * 2, 127); 353
191 if (!retval) 354
192 return NULL; 355 /*[clinic input]
193 hex_digest = PyUnicode_1BYTE_DATA(retval); 356 _sha3.sha3_224.update
194 357
195 /* Make hex version of the digest */ 358 obj: object
196 for(i=j=0; i < digestlen; i++) { 359 /
197 unsigned char c; 360
198 c = (digest[i] >> 4) & 0xf; 361 Update this hash object's state with the provided string.
199 hex_digest[j++] = Py_hexdigits[c]; 362 [clinic start generated code]*/
200 c = (digest[i] & 0xf); 363
201 hex_digest[j++] = Py_hexdigits[c]; 364 static PyObject *
202 } 365 _sha3_sha3_224_update(SHA3object *self, PyObject *obj)
203 assert(_PyUnicode_CheckConsistency(retval, 1)); 366 /*[clinic end generated code: output=06721d55b483e0af input=be44bf0d1c279791]*/
204 return retval; 367 {
205 }
206
207 PyDoc_STRVAR(SHA3_update__doc__,
208 "Update this hash object's state with the provided string.");
209
210 static PyObject *
211 SHA3_update(SHA3object *self, PyObject *args)
212 {
213 PyObject *obj;
214 Py_buffer buf; 368 Py_buffer buf;
215 HashReturn res; 369 HashReturn res;
216 370
217 if (!PyArg_ParseTuple(args, "O:update", &obj))
218 return NULL;
219
220 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); 371 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
221 372
222 /* 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 */
223 #ifdef WITH_THREADS 374 #ifdef WITH_THREAD
224 if (self->lock == NULL && buf.len >= GIL_RELEASE_MINSIZE) { 375 if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
225 self->lock = PyThread_allocate_lock(); 376 self->lock = PyThread_allocate_lock();
226 } 377 }
227 /* Once a lock exists all code paths must be syncronized. We have to 378 /* Once a lock exists all code paths must be synchronized. We have to
228 * release the GIL even for small buffers as acquiring the lock may take 379 * release the GIL even for small buffers as acquiring the lock may take
229 * an unlimited amount of time when another thread updates this object 380 * an unlimited amount of time when another thread updates this object
230 * with lots of data. */ 381 * with lots of data. */
231 if (self->lock) { 382 if (self->lock) {
232 Py_BEGIN_ALLOW_THREADS 383 Py_BEGIN_ALLOW_THREADS
233 PyThread_acquire_lock(self->lock, 1); 384 PyThread_acquire_lock(self->lock, 1);
234 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); 385 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
235 PyThread_release_lock(self->lock); 386 PyThread_release_lock(self->lock);
236 Py_END_ALLOW_THREADS 387 Py_END_ALLOW_THREADS
237 } 388 }
238 else { 389 else {
239 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); 390 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
240 } 391 }
241 #else 392 #else
242 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8); 393 res = SHA3_process(&self->hash_state, buf.buf, buf.len * 8);
243 #endif 394 #endif
244 LEAVE_HASHLIB(self);
245 395
246 if (res != SUCCESS) { 396 if (res != SUCCESS) {
247 PyBuffer_Release(&buf); 397 PyBuffer_Release(&buf);
248 PyErr_SetString(PyExc_RuntimeError, 398 PyErr_SetString(PyExc_RuntimeError,
249 "internal error in SHA3 Update()"); 399 "internal error in SHA3 Update()");
250 return NULL; 400 return NULL;
251 } 401 }
252 402
253 PyBuffer_Release(&buf); 403 PyBuffer_Release(&buf);
254 Py_INCREF(Py_None); 404 Py_INCREF(Py_None);
255 return Py_None; 405 return Py_None;
256 } 406 }
257 407
408
258 static PyMethodDef SHA3_methods[] = { 409 static PyMethodDef SHA3_methods[] = {
259 {"copy", (PyCFunction)SHA3_copy, METH_NOARGS, 410 _SHA3_SHA3_224_COPY_METHODDEF
260 SHA3_copy__doc__}, 411 _SHA3_SHA3_224_DIGEST_METHODDEF
261 {"digest", (PyCFunction)SHA3_digest, METH_NOARGS, 412 _SHA3_SHA3_224_HEXDIGEST_METHODDEF
262 SHA3_digest__doc__}, 413 _SHA3_SHA3_224_UPDATE_METHODDEF
263 {"hexdigest", (PyCFunction)SHA3_hexdigest, METH_NOARGS,
264 SHA3_hexdigest__doc__},
265 {"update", (PyCFunction)SHA3_update, METH_VARARGS,
266 SHA3_update__doc__},
267 {NULL, NULL} /* sentinel */ 414 {NULL, NULL} /* sentinel */
268 }; 415 };
269 416
417
270 static PyObject * 418 static PyObject *
271 SHA3_get_block_size(SHA3object *self, void *closure) 419 SHA3_get_block_size(SHA3object *self, void *closure)
272 { 420 {
273 return PyLong_FromLong(SHA3_BLOCKSIZE); 421 int rate = self->hash_state.sponge.rate;
274 } 422 return PyLong_FromLong(rate / 8);
423 }
424
275 425
276 static PyObject * 426 static PyObject *
277 SHA3_get_name(SHA3object *self, void *closure) 427 SHA3_get_name(SHA3object *self, void *closure)
278 { 428 {
279 return PyUnicode_FromFormat("sha3_%i", self->hashbitlen); 429 PyTypeObject *type = Py_TYPE(self);
280 } 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
281 458
282 static PyObject * 459 static PyObject *
283 SHA3_get_digest_size(SHA3object *self, void *closure) 460 SHA3_get_digest_size(SHA3object *self, void *closure)
284 { 461 {
285 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);
286 } 488 }
287 489
288 490
289 static PyGetSetDef SHA3_getseters[] = { 491 static PyGetSetDef SHA3_getseters[] = {
290 {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL}, 492 {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
291 {"name", (getter)SHA3_get_name, NULL, NULL, NULL}, 493 {"name", (getter)SHA3_get_name, NULL, NULL, NULL},
292 {"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},
293 {NULL} /* Sentinel */ 498 {NULL} /* Sentinel */
294 }; 499 };
295 500
296 static PyTypeObject SHA3type = { 501
297 PyVarObject_HEAD_INIT(NULL, 0) 502 #define SHA3_TYPE(type_obj, type_name, type_doc, type_methods) \
298 "_sha3.SHA3", /* tp_name */ 503 static PyTypeObject type_obj = { \
299 sizeof(SHA3object), /* tp_size */ 504 PyVarObject_HEAD_INIT(NULL, 0) \
300 0, /* tp_itemsize */ 505 type_name, /* tp_name */ \
301 /* methods */ 506 sizeof(SHA3object), /* tp_size */ \
302 (destructor)SHA3_dealloc, /* tp_dealloc */ 507 0, /* tp_itemsize */ \
303 0, /* tp_print */ 508 /* methods */ \
304 0, /* tp_getattr */ 509 (destructor)SHA3_dealloc, /* tp_dealloc */ \
305 0, /* tp_setattr */ 510 0, /* tp_print */ \
306 0, /* tp_reserved */ 511 0, /* tp_getattr */ \
307 0, /* tp_repr */ 512 0, /* tp_setattr */ \
308 0, /* tp_as_number */ 513 0, /* tp_reserved */ \
309 0, /* tp_as_sequence */ 514 0, /* tp_repr */ \
310 0, /* tp_as_mapping */ 515 0, /* tp_as_number */ \
311 0, /* tp_hash */ 516 0, /* tp_as_sequence */ \
312 0, /* tp_call */ 517 0, /* tp_as_mapping */ \
313 0, /* tp_str */ 518 0, /* tp_hash */ \
314 0, /* tp_getattro */ 519 0, /* tp_call */ \
315 0, /* tp_setattro */ 520 0, /* tp_str */ \
316 0, /* tp_as_buffer */ 521 0, /* tp_getattro */ \
317 Py_TPFLAGS_DEFAULT, /* tp_flags */ 522 0, /* tp_setattro */ \
318 0, /* tp_doc */ 523 0, /* tp_as_buffer */ \
319 0, /* tp_traverse */ 524 Py_TPFLAGS_DEFAULT, /* tp_flags */ \
320 0, /* tp_clear */ 525 type_doc, /* tp_doc */ \
321 0, /* tp_richcompare */ 526 0, /* tp_traverse */ \
322 0, /* tp_weaklistoffset */ 527 0, /* tp_clear */ \
323 0, /* tp_iter */ 528 0, /* tp_richcompare */ \
324 0, /* tp_iternext */ 529 0, /* tp_weaklistoffset */ \
325 SHA3_methods, /* tp_methods */ 530 0, /* tp_iter */ \
326 NULL, /* tp_members */ 531 0, /* tp_iternext */ \
327 SHA3_getseters, /* tp_getset */ 532 type_methods, /* tp_methods */ \
328 }; 533 NULL, /* tp_members */ \
329 534 SHA3_getseters, /* tp_getset */ \
330 535 0, /* tp_base */ \
331 /* constructor helper */ 536 0, /* tp_dict */ \
332 static PyObject * 537 0, /* tp_descr_get */ \
333 SHA3_factory(PyObject *args, PyObject *kwdict, const char *fmt, 538 0, /* tp_descr_set */ \
334 int hashbitlen) 539 0, /* tp_dictoffset */ \
335 { 540 0, /* tp_init */ \
336 SHA3object *newobj = NULL; 541 0, /* tp_alloc */ \
337 static char *kwlist[] = {"string", NULL}; 542 py_sha3_new, /* tp_new */ \
338 PyObject *data_obj = NULL; 543 }
339 Py_buffer buf;
340 HashReturn res;
341
342 if (!PyArg_ParseTupleAndKeywords(args, kwdict, fmt, kwlist,
343 &data_obj)) {
344 return NULL;
345 }
346
347 if (data_obj)
348 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
349
350 if ((newobj = newSHA3object(hashbitlen)) == NULL) {
351 goto error;
352 }
353
354 if (SHA3_init((hashState*)&newobj->hash_state, hashbitlen) != SUCCESS) {
355 PyErr_SetString(PyExc_RuntimeError,
356 "internal error in SHA3 Update()");
357 goto error;
358 }
359
360 if (data_obj) {
361 #ifdef WITH_THREADS
362 if (buf.len >= GIL_RELEASE_MINSIZE) {
363 /* invariant: New objects can't be accessed by other code yet,
364 * thus it's safe to release the GIL without locking the object.
365 */
366 Py_BEGIN_ALLOW_THREADS
367 res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
368 Py_END_ALLOW_THREADS
369 }
370 else {
371 res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
372 }
373 #else
374 res = SHA3_process(&newobj->hash_state, buf.buf, buf.len * 8);
375 #endif
376 if (res != SUCCESS) {
377 PyErr_SetString(PyExc_RuntimeError,
378 "internal error in SHA3 Update()");
379 goto error;
380 }
381 PyBuffer_Release(&buf);
382 }
383
384 return (PyObject *)newobj;
385
386 error:
387 if (newobj) {
388 SHA3_clearstate(newobj->hash_state);
389 /* self->lock is always NULL */
390 }
391 if (data_obj) {
392 PyBuffer_Release(&buf);
393 }
394 return NULL;
395
396 }
397
398 PyDoc_STRVAR(sha3_224__doc__,
399 "sha3_224([string]) -> SHA3 object\n\
400 \n\
401 Return a new SHA3 hash object with a hashbit length of 28 bytes.");
402
403 static PyObject *
404 sha3_224(PyObject *self, PyObject *args, PyObject *kwdict)
405 {
406 return SHA3_factory(args, kwdict, "|O:sha3_224", 224);
407 }
408
409 544
410 PyDoc_STRVAR(sha3_256__doc__, 545 PyDoc_STRVAR(sha3_256__doc__,
411 "sha3_256([string]) -> SHA3 object\n\ 546 "sha3_256([string]) -> SHA3 object\n\
412 \n\ 547 \n\
413 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.");
414 549
415 static PyObject *
416 sha3_256(PyObject *self, PyObject *args, PyObject *kwdict)
417 {
418 return SHA3_factory(args, kwdict, "|O:sha3_256", 256);
419 }
420
421 PyDoc_STRVAR(sha3_384__doc__, 550 PyDoc_STRVAR(sha3_384__doc__,
422 "sha3_384([string]) -> SHA3 object\n\ 551 "sha3_384([string]) -> SHA3 object\n\
423 \n\ 552 \n\
424 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.");
425 554
426 static PyObject *
427 sha3_384(PyObject *self, PyObject *args, PyObject *kwdict)
428 {
429 return SHA3_factory(args, kwdict, "|O:sha3_384", 384);
430 }
431
432 PyDoc_STRVAR(sha3_512__doc__, 555 PyDoc_STRVAR(sha3_512__doc__,
433 "sha3_512([string]) -> SHA3 object\n\ 556 "sha3_512([string]) -> SHA3 object\n\
434 \n\ 557 \n\
435 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.");
436 559
437 static PyObject * 560 SHA3_TYPE(SHA3_224type, "_sha3.sha3_224", py_sha3_new__doc__, SHA3_methods);
438 sha3_512(PyObject *self, PyObject *args, PyObject *kwdict) 561 SHA3_TYPE(SHA3_256type, "_sha3.sha3_256", sha3_256__doc__, SHA3_methods);
439 { 562 SHA3_TYPE(SHA3_384type, "_sha3.sha3_384", sha3_384__doc__, SHA3_methods);
440 return SHA3_factory(args, kwdict, "|O:sha3_512", 512); 563 SHA3_TYPE(SHA3_512type, "_sha3.sha3_512", sha3_512__doc__, SHA3_methods);
441 } 564
442 565 #ifdef PY_WITH_KECCAK
443 566 PyDoc_STRVAR(keccak_224__doc__,
444 /* List of functions exported by this module */ 567 "keccak_224([string]) -> Keccak object\n\
445 static struct PyMethodDef SHA3_functions[] = { 568 \n\
446 {"sha3_224", (PyCFunction)sha3_224, METH_VARARGS|METH_KEYWORDS, 569 Return a new Keccak hash object with a hashbit length of 28 bytes.");
447 sha3_224__doc__}, 570
448 {"sha3_256", (PyCFunction)sha3_256, METH_VARARGS|METH_KEYWORDS, 571 PyDoc_STRVAR(keccak_256__doc__,
449 sha3_256__doc__}, 572 "keccak_256([string]) -> Keccak object\n\
450 {"sha3_384", (PyCFunction)sha3_384, METH_VARARGS|METH_KEYWORDS, 573 \n\
451 sha3_384__doc__}, 574 Return a new Keccak hash object with a hashbit length of 32 bytes.");
452 {"sha3_512", (PyCFunction)sha3_512, METH_VARARGS|METH_KEYWORDS, 575
453 sha3_512__doc__}, 576 PyDoc_STRVAR(keccak_384__doc__,
454 {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 */
455 }; 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);
456 687
457 688
458 /* Initialize this module. */ 689 /* Initialize this module. */
459 static struct PyModuleDef _SHA3module = { 690 static struct PyModuleDef _SHA3module = {
460 PyModuleDef_HEAD_INIT, 691 PyModuleDef_HEAD_INIT,
461 "_sha3", 692 "_sha3",
462 NULL, 693 NULL,
463 -1, 694 -1,
464 SHA3_functions, 695 NULL,
465 NULL, 696 NULL,
466 NULL, 697 NULL,
467 NULL, 698 NULL,
468 NULL 699 NULL
469 }; 700 };
470 701
702
471 PyMODINIT_FUNC 703 PyMODINIT_FUNC
472 PyInit__sha3(void) 704 PyInit__sha3(void)
473 { 705 {
474 Py_TYPE(&SHA3type) = &PyType_Type; 706 PyObject *m = NULL;
475 if (PyType_Ready(&SHA3type) < 0) { 707
476 return NULL; 708 m = PyModule_Create(&_SHA3module);
477 } 709
478 710 #define init_sha3type(name, type) \
479 return PyModule_Create(&_SHA3module); 711 do { \
480 } 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+