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

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