blob: 8961de48d8fc29e59b1c64be8d37dc8478e2d6a1 [file] [log] [blame]
Gregory P. Smith2f21eb32007-09-09 06:44:34 +00001/* SHA1 module */
2
3/* This module provides an interface to the SHA1 algorithm */
4
5/* See below for information about the original code this module was
6 based upon. Additional work performed by:
7
8 Andrew Kuchling (amk@amk.ca)
9 Greg Stein (gstein@lyra.org)
10 Trevor Perrin (trevp@trevp.net)
11
12 Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
13 Licensed to PSF under a Contributor Agreement.
14
15*/
16
17/* SHA1 objects */
18
19#include "Python.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000020#include "hashlib.h"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000021
Martin v. Löwis501b13c2014-07-27 14:20:23 +020022/*[clinic input]
23module _sha1
24class SHA1Type "SHA1object *" "&PyType_Type"
25[clinic start generated code]*/
26/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000027
28/* Some useful types */
29
30#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031typedef unsigned int SHA1_INT32; /* 32-bit integer */
32typedef PY_LONG_LONG SHA1_INT64; /* 64-bit integer */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000033#else
34/* not defined. compilation will die. */
35#endif
36
37/* The SHA1 block size and message digest sizes, in bytes */
38
39#define SHA1_BLOCKSIZE 64
40#define SHA1_DIGESTSIZE 20
41
42/* The structure for storing SHA1 info */
43
44struct sha1_state {
45 SHA1_INT64 length;
46 SHA1_INT32 state[5], curlen;
47 unsigned char buf[SHA1_BLOCKSIZE];
48};
49
50typedef struct {
51 PyObject_HEAD
52
53 struct sha1_state hash_state;
54} SHA1object;
55
56
57/* ------------------------------------------------------------------------
58 *
59 * This code for the SHA1 algorithm was noted as public domain. The
60 * original headers are pasted below.
61 *
62 * Several changes have been made to make it more compatible with the
63 * Python environment and desired interface.
64 *
65 */
66
67/* LibTomCrypt, modular cryptographic library -- Tom St Denis
68 *
69 * LibTomCrypt is a library that provides various cryptographic
70 * algorithms in a highly modular and flexible manner.
71 *
72 * The library is free for all purposes without any express
73 * guarantee it works.
74 *
75 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
76 */
77
78/* rotate the hard way (platform optimizations could be done) */
79#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
80#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
81
82/* Endian Neutral macros that work on all platforms */
83
84#define STORE32H(x, y) \
85 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
86 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
87
88#define LOAD32H(x, y) \
89 { x = ((unsigned long)((y)[0] & 255)<<24) | \
90 ((unsigned long)((y)[1] & 255)<<16) | \
91 ((unsigned long)((y)[2] & 255)<<8) | \
92 ((unsigned long)((y)[3] & 255)); }
93
94#define STORE64H(x, y) \
95 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
96 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
97 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
98 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
99
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000100
101/* SHA1 macros */
102
103#define F0(x,y,z) (z ^ (x & (y ^ z)))
104#define F1(x,y,z) (x ^ y ^ z)
105#define F2(x,y,z) ((x & y) | (z & (x | y)))
106#define F3(x,y,z) (x ^ y ^ z)
107
108static void sha1_compress(struct sha1_state *sha1, unsigned char *buf)
109{
110 SHA1_INT32 a,b,c,d,e,W[80],i;
111
112 /* copy the state into 512-bits into W[0..15] */
113 for (i = 0; i < 16; i++) {
114 LOAD32H(W[i], buf + (4*i));
115 }
116
117 /* copy state */
118 a = sha1->state[0];
119 b = sha1->state[1];
120 c = sha1->state[2];
121 d = sha1->state[3];
122 e = sha1->state[4];
123
124 /* expand it */
125 for (i = 16; i < 80; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000127 }
128
129 /* compress */
130 /* round one */
Brett Cannonf079c572010-07-23 15:43:14 +0000131 #define FF_0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
132 #define FF_1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
133 #define FF_2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
134 #define FF_3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000136 for (i = 0; i < 20; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000137 FF_0(a,b,c,d,e,i++);
138 FF_0(e,a,b,c,d,i++);
139 FF_0(d,e,a,b,c,i++);
140 FF_0(c,d,e,a,b,i++);
141 FF_0(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000142 }
143
144 /* round two */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 for (; i < 40; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000146 FF_1(a,b,c,d,e,i++);
147 FF_1(e,a,b,c,d,i++);
148 FF_1(d,e,a,b,c,i++);
149 FF_1(c,d,e,a,b,i++);
150 FF_1(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000151 }
152
153 /* round three */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 for (; i < 60; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000155 FF_2(a,b,c,d,e,i++);
156 FF_2(e,a,b,c,d,i++);
157 FF_2(d,e,a,b,c,i++);
158 FF_2(c,d,e,a,b,i++);
159 FF_2(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000160 }
161
162 /* round four */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 for (; i < 80; ) {
Brett Cannonf079c572010-07-23 15:43:14 +0000164 FF_3(a,b,c,d,e,i++);
165 FF_3(e,a,b,c,d,i++);
166 FF_3(d,e,a,b,c,i++);
167 FF_3(c,d,e,a,b,i++);
168 FF_3(b,c,d,e,a,i++);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000169 }
170
Brett Cannonf079c572010-07-23 15:43:14 +0000171 #undef FF_0
172 #undef FF_1
173 #undef FF_2
174 #undef FF_3
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000175
176 /* store */
177 sha1->state[0] = sha1->state[0] + a;
178 sha1->state[1] = sha1->state[1] + b;
179 sha1->state[2] = sha1->state[2] + c;
180 sha1->state[3] = sha1->state[3] + d;
181 sha1->state[4] = sha1->state[4] + e;
182}
183
184/**
185 Initialize the hash state
186 @param sha1 The hash state you wish to initialize
187*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200188static void
189sha1_init(struct sha1_state *sha1)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000190{
191 assert(sha1 != NULL);
192 sha1->state[0] = 0x67452301UL;
193 sha1->state[1] = 0xefcdab89UL;
194 sha1->state[2] = 0x98badcfeUL;
195 sha1->state[3] = 0x10325476UL;
196 sha1->state[4] = 0xc3d2e1f0UL;
197 sha1->curlen = 0;
198 sha1->length = 0;
199}
200
201/**
202 Process a block of memory though the hash
203 @param sha1 The hash state
204 @param in The data to hash
205 @param inlen The length of the data (octets)
206*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200207static void
208sha1_process(struct sha1_state *sha1,
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000209 const unsigned char *in, Py_ssize_t inlen)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000210{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000211 Py_ssize_t n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000212
213 assert(sha1 != NULL);
214 assert(in != NULL);
215 assert(sha1->curlen <= sizeof(sha1->buf));
216
217 while (inlen > 0) {
218 if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) {
219 sha1_compress(sha1, (unsigned char *)in);
220 sha1->length += SHA1_BLOCKSIZE * 8;
221 in += SHA1_BLOCKSIZE;
222 inlen -= SHA1_BLOCKSIZE;
223 } else {
Victor Stinner640c35c2013-06-04 23:14:37 +0200224 n = Py_MIN(inlen, (Py_ssize_t)(SHA1_BLOCKSIZE - sha1->curlen));
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000225 memcpy(sha1->buf + sha1->curlen, in, (size_t)n);
Victor Stinner56cb1252012-10-31 00:33:57 +0100226 sha1->curlen += (SHA1_INT32)n;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000227 in += n;
228 inlen -= n;
229 if (sha1->curlen == SHA1_BLOCKSIZE) {
230 sha1_compress(sha1, sha1->buf);
231 sha1->length += 8*SHA1_BLOCKSIZE;
232 sha1->curlen = 0;
233 }
234 }
235 }
236}
237
238/**
239 Terminate the hash to get the digest
240 @param sha1 The hash state
241 @param out [out] The destination of the hash (20 bytes)
242*/
doko@ubuntu.com1e5be8c2012-06-21 17:05:50 +0200243static void
244sha1_done(struct sha1_state *sha1, unsigned char *out)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000245{
246 int i;
247
248 assert(sha1 != NULL);
249 assert(out != NULL);
250 assert(sha1->curlen < sizeof(sha1->buf));
251
252 /* increase the length of the message */
253 sha1->length += sha1->curlen * 8;
254
255 /* append the '1' bit */
256 sha1->buf[sha1->curlen++] = (unsigned char)0x80;
257
258 /* if the length is currently above 56 bytes we append zeros
259 * then compress. Then we can fall back to padding zeros and length
260 * encoding like normal.
261 */
262 if (sha1->curlen > 56) {
263 while (sha1->curlen < 64) {
264 sha1->buf[sha1->curlen++] = (unsigned char)0;
265 }
266 sha1_compress(sha1, sha1->buf);
267 sha1->curlen = 0;
268 }
269
270 /* pad upto 56 bytes of zeroes */
271 while (sha1->curlen < 56) {
272 sha1->buf[sha1->curlen++] = (unsigned char)0;
273 }
274
275 /* store length */
276 STORE64H(sha1->length, sha1->buf+56);
277 sha1_compress(sha1, sha1->buf);
278
279 /* copy output */
280 for (i = 0; i < 5; i++) {
281 STORE32H(sha1->state[i], out+(4*i));
282 }
283}
284
285
286/* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */
287/* .Revision: 1.10 $ */
288/* .Date: 2007/05/12 14:25:28 $ */
289
290/*
291 * End of copied SHA1 code.
292 *
293 * ------------------------------------------------------------------------
294 */
295
296static PyTypeObject SHA1type;
297
298
299static SHA1object *
300newSHA1object(void)
301{
302 return (SHA1object *)PyObject_New(SHA1object, &SHA1type);
303}
304
305
306/* Internal methods for a hash object */
307
308static void
309SHA1_dealloc(PyObject *ptr)
310{
311 PyObject_Del(ptr);
312}
313
314
315/* External methods for a hash object */
316
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200317/*[clinic input]
318SHA1Type.copy
319
320Return a copy of the hash object.
321[clinic start generated code]*/
322
323PyDoc_STRVAR(SHA1Type_copy__doc__,
324"copy($self, /)\n"
325"--\n"
326"\n"
327"Return a copy of the hash object.");
328
329#define SHA1TYPE_COPY_METHODDEF \
330 {"copy", (PyCFunction)SHA1Type_copy, METH_NOARGS, SHA1Type_copy__doc__},
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000331
332static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200333SHA1Type_copy_impl(SHA1object *self);
334
335static PyObject *
336SHA1Type_copy(SHA1object *self, PyObject *Py_UNUSED(ignored))
337{
338 return SHA1Type_copy_impl(self);
339}
340
341static PyObject *
342SHA1Type_copy_impl(SHA1object *self)
343/*[clinic end generated code: output=1a320e75a7444098 input=b7eae10df6f89b36]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000344{
345 SHA1object *newobj;
346
Benjamin Peterson51ce29c2013-04-15 21:38:25 -0400347 if ((newobj = newSHA1object()) == NULL)
348 return NULL;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000349
350 newobj->hash_state = self->hash_state;
351 return (PyObject *)newobj;
352}
353
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200354/*[clinic input]
355SHA1Type.digest
356
357Return the digest value as a string of binary data.
358[clinic start generated code]*/
359
360PyDoc_STRVAR(SHA1Type_digest__doc__,
361"digest($self, /)\n"
362"--\n"
363"\n"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000364"Return the digest value as a string of binary data.");
365
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200366#define SHA1TYPE_DIGEST_METHODDEF \
367 {"digest", (PyCFunction)SHA1Type_digest, METH_NOARGS, SHA1Type_digest__doc__},
368
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000369static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200370SHA1Type_digest_impl(SHA1object *self);
371
372static PyObject *
373SHA1Type_digest(SHA1object *self, PyObject *Py_UNUSED(ignored))
374{
375 return SHA1Type_digest_impl(self);
376}
377
378static PyObject *
379SHA1Type_digest_impl(SHA1object *self)
380/*[clinic end generated code: output=c4920f75228bfbfd input=205d47e1927fd009]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000381{
382 unsigned char digest[SHA1_DIGESTSIZE];
383 struct sha1_state temp;
384
385 temp = self->hash_state;
386 sha1_done(&temp, digest);
Christian Heimes72b710a2008-05-26 13:28:38 +0000387 return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000388}
389
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200390/*[clinic input]
391SHA1Type.hexdigest
392
393Return the digest value as a string of hexadecimal digits.
394[clinic start generated code]*/
395
396PyDoc_STRVAR(SHA1Type_hexdigest__doc__,
397"hexdigest($self, /)\n"
398"--\n"
399"\n"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000400"Return the digest value as a string of hexadecimal digits.");
401
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200402#define SHA1TYPE_HEXDIGEST_METHODDEF \
403 {"hexdigest", (PyCFunction)SHA1Type_hexdigest, METH_NOARGS, SHA1Type_hexdigest__doc__},
404
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000405static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200406SHA1Type_hexdigest_impl(SHA1object *self);
407
408static PyObject *
409SHA1Type_hexdigest(SHA1object *self, PyObject *Py_UNUSED(ignored))
410{
411 return SHA1Type_hexdigest_impl(self);
412}
413
414static PyObject *
415SHA1Type_hexdigest_impl(SHA1object *self)
416/*[clinic end generated code: output=6e345aac201887b2 input=97691055c0c74ab0]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000417{
418 unsigned char digest[SHA1_DIGESTSIZE];
419 struct sha1_state temp;
420 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200421 Py_UCS1 *hex_digest;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000422 int i, j;
423
424 /* Get the raw (binary) digest value */
425 temp = self->hash_state;
426 sha1_done(&temp, digest);
427
428 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200429 retval = PyUnicode_New(SHA1_DIGESTSIZE * 2, 127);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000430 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200432 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000433
434 /* Make hex version of the digest */
435 for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200436 unsigned char c;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000437 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200438 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000439 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200440 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000441 }
Christian Heimesf402e922013-01-03 09:21:55 +0100442#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200443 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100444#endif
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000445 return retval;
446}
447
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200448/*[clinic input]
449SHA1Type.update
450
451 obj: object
452 /
453
454Update this hash object's state with the provided string.
455[clinic start generated code]*/
456
457PyDoc_STRVAR(SHA1Type_update__doc__,
458"update($self, obj, /)\n"
459"--\n"
460"\n"
461"Update this hash object\'s state with the provided string.");
462
463#define SHA1TYPE_UPDATE_METHODDEF \
464 {"update", (PyCFunction)SHA1Type_update, METH_O, SHA1Type_update__doc__},
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000465
466static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200467SHA1Type_update(SHA1object *self, PyObject *obj)
468/*[clinic end generated code: output=ab20a86a25e7d255 input=aad8e07812edbba3]*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000469{
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000470 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000471
Gregory P. Smith365a1862009-02-12 07:35:29 +0000472 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
473
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000474 sha1_process(&self->hash_state, buf.buf, buf.len);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000475
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000476 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000477 Py_INCREF(Py_None);
478 return Py_None;
479}
480
481static PyMethodDef SHA1_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200482 SHA1TYPE_COPY_METHODDEF
483 SHA1TYPE_DIGEST_METHODDEF
484 SHA1TYPE_HEXDIGEST_METHODDEF
485 SHA1TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 {NULL, NULL} /* sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000487};
488
489static PyObject *
490SHA1_get_block_size(PyObject *self, void *closure)
491{
Christian Heimes217cfd12007-12-02 14:31:20 +0000492 return PyLong_FromLong(SHA1_BLOCKSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000493}
494
495static PyObject *
496SHA1_get_name(PyObject *self, void *closure)
497{
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200498 return PyUnicode_FromStringAndSize("sha1", 4);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000499}
500
501static PyObject *
502sha1_get_digest_size(PyObject *self, void *closure)
503{
Christian Heimes217cfd12007-12-02 14:31:20 +0000504 return PyLong_FromLong(SHA1_DIGESTSIZE);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000505}
506
507
508static PyGetSetDef SHA1_getseters[] = {
509 {"block_size",
510 (getter)SHA1_get_block_size, NULL,
511 NULL,
512 NULL},
513 {"name",
514 (getter)SHA1_get_name, NULL,
515 NULL,
516 NULL},
517 {"digest_size",
518 (getter)sha1_get_digest_size, NULL,
519 NULL,
520 NULL},
521 {NULL} /* Sentinel */
522};
523
524static PyTypeObject SHA1type = {
525 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 "_sha1.sha1", /*tp_name*/
527 sizeof(SHA1object), /*tp_size*/
528 0, /*tp_itemsize*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000529 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 SHA1_dealloc, /*tp_dealloc*/
531 0, /*tp_print*/
532 0, /*tp_getattr*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000533 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000534 0, /*tp_reserved*/
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000535 0, /*tp_repr*/
536 0, /*tp_as_number*/
537 0, /*tp_as_sequence*/
538 0, /*tp_as_mapping*/
539 0, /*tp_hash*/
540 0, /*tp_call*/
541 0, /*tp_str*/
542 0, /*tp_getattro*/
543 0, /*tp_setattro*/
544 0, /*tp_as_buffer*/
545 Py_TPFLAGS_DEFAULT, /*tp_flags*/
546 0, /*tp_doc*/
547 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 0, /*tp_clear*/
549 0, /*tp_richcompare*/
550 0, /*tp_weaklistoffset*/
551 0, /*tp_iter*/
552 0, /*tp_iternext*/
553 SHA1_methods, /* tp_methods */
554 NULL, /* tp_members */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000555 SHA1_getseters, /* tp_getset */
556};
557
558
559/* The single module-level function: new() */
560
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200561/*[clinic input]
562_sha1.sha1
563
564 string: object(c_default="NULL") = b''
565
566Return a new SHA1 hash object; optionally initialized with a string.
567[clinic start generated code]*/
568
569PyDoc_STRVAR(_sha1_sha1__doc__,
570"sha1($module, /, string=b\'\')\n"
571"--\n"
572"\n"
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000573"Return a new SHA1 hash object; optionally initialized with a string.");
574
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200575#define _SHA1_SHA1_METHODDEF \
576 {"sha1", (PyCFunction)_sha1_sha1, METH_VARARGS|METH_KEYWORDS, _sha1_sha1__doc__},
577
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000578static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200579_sha1_sha1_impl(PyModuleDef *module, PyObject *string);
580
581static PyObject *
582_sha1_sha1(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000583{
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200584 PyObject *return_value = NULL;
585 static char *_keywords[] = {"string", NULL};
586 PyObject *string = NULL;
587
588 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
589 "|O:sha1", _keywords,
590 &string))
591 goto exit;
592 return_value = _sha1_sha1_impl(module, string);
593
594exit:
595 return return_value;
596}
597
598static PyObject *
599_sha1_sha1_impl(PyModuleDef *module, PyObject *string)
600/*[clinic end generated code: output=c9068552f07b8954 input=27ea54281d995ec2]*/
601{
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000602 SHA1object *new;
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000603 Py_buffer buf;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000604
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200605 if (string)
606 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000607
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000608 if ((new = newSHA1object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200609 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000610 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000611 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000612 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000613
614 sha1_init(&new->hash_state);
615
616 if (PyErr_Occurred()) {
617 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200618 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000619 PyBuffer_Release(&buf);
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000620 return NULL;
621 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200622 if (string) {
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000623 sha1_process(&new->hash_state, buf.buf, buf.len);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000624 PyBuffer_Release(&buf);
Martin v. Löwis7b9cb252008-08-14 15:52:23 +0000625 }
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000626
627 return (PyObject *)new;
628}
629
630
631/* List of functions exported by this module */
632
633static struct PyMethodDef SHA1_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200634 _SHA1_SHA1_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 {NULL, NULL} /* Sentinel */
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000636};
637
638
639/* Initialize this module. */
640
641#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
642
Martin v. Löwis1a214512008-06-11 05:26:20 +0000643
644static struct PyModuleDef _sha1module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 PyModuleDef_HEAD_INIT,
646 "_sha1",
647 NULL,
648 -1,
649 SHA1_functions,
650 NULL,
651 NULL,
652 NULL,
653 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000654};
655
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000656PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +0000657PyInit__sha1(void)
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000658{
Christian Heimes327dd732013-10-22 15:05:23 +0200659 PyObject *m;
660
Christian Heimes90aa7642007-12-19 02:45:37 +0000661 Py_TYPE(&SHA1type) = &PyType_Type;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000662 if (PyType_Ready(&SHA1type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000663 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200664
665 m = PyModule_Create(&_sha1module);
666 if (m == NULL)
667 return NULL;
668
669 Py_INCREF((PyObject *)&SHA1type);
670 PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type);
671 return m;
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000672}