blob: 6399b752a58cf908a26e2de873462295c4e382f4 [file] [log] [blame]
Guido van Rossum29d2acc1999-03-24 19:03:59 +00001/* SHA module */
2
3/* This module provides an interface to NIST's Secure Hash Algorithm */
4
5/* See below for information about the original code this module was
6 based upon. Additional work performed by:
7
Andrew M. Kuchling3adefcc2002-10-30 21:08:34 +00008 Andrew Kuchling (amk@amk.ca)
Guido van Rossum29d2acc1999-03-24 19:03:59 +00009 Greg Stein (gstein@lyra.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000010
Gregory P. Smithf8057852007-09-09 20:25:00 +000011 Copyright (C) 2005 Gregory P. Smith (greg@krypto.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000012 Licensed to PSF under a Contributor Agreement.
13
Guido van Rossum29d2acc1999-03-24 19:03:59 +000014*/
15
16/* SHA objects */
17
18#include "Python.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000019#include "structmember.h"
Gregory P. Smithea388262009-02-13 03:00:00 +000020#include "hashlib.h"
Guido van Rossum29d2acc1999-03-24 19:03:59 +000021
22
23/* Endianness testing and definitions */
24#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
25 if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
26
27#define PCT_LITTLE_ENDIAN 1
28#define PCT_BIG_ENDIAN 0
29
30/* Some useful types */
31
32typedef unsigned char SHA_BYTE;
33
34#if SIZEOF_INT == 4
35typedef unsigned int SHA_INT32; /* 32-bit integer */
36#else
37/* not defined. compilation will die. */
38#endif
39
40/* The SHA block size and message digest sizes, in bytes */
41
42#define SHA_BLOCKSIZE 64
43#define SHA_DIGESTSIZE 20
44
45/* The structure for storing SHS info */
46
47typedef struct {
Fred Drake2c4a3dc2000-07-08 06:41:03 +000048 PyObject_HEAD
49 SHA_INT32 digest[5]; /* Message digest */
50 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
51 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
52 int Endianness;
53 int local; /* unprocessed amount in data */
Guido van Rossum29d2acc1999-03-24 19:03:59 +000054} SHAobject;
55
56/* When run on a little-endian CPU we need to perform byte reversal on an
57 array of longwords. */
58
Fred Drake2c4a3dc2000-07-08 06:41:03 +000059static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
Guido van Rossum29d2acc1999-03-24 19:03:59 +000060{
61 SHA_INT32 value;
62
63 if ( Endianness == PCT_BIG_ENDIAN )
64 return;
65
66 byteCount /= sizeof(*buffer);
Fred Drake2c4a3dc2000-07-08 06:41:03 +000067 while (byteCount--) {
Guido van Rossum29d2acc1999-03-24 19:03:59 +000068 value = *buffer;
69 value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
70 ( ( value & 0x00FF00FFL ) << 8 );
71 *buffer++ = ( value << 16 ) | ( value >> 16 );
72 }
73}
74
Fred Drake2c4a3dc2000-07-08 06:41:03 +000075static void SHAcopy(SHAobject *src, SHAobject *dest)
Guido van Rossum29d2acc1999-03-24 19:03:59 +000076{
Fred Drake2c4a3dc2000-07-08 06:41:03 +000077 dest->Endianness = src->Endianness;
78 dest->local = src->local;
79 dest->count_lo = src->count_lo;
80 dest->count_hi = src->count_hi;
81 memcpy(dest->digest, src->digest, sizeof(src->digest));
82 memcpy(dest->data, src->data, sizeof(src->data));
Guido van Rossum29d2acc1999-03-24 19:03:59 +000083}
84
85
86/* ------------------------------------------------------------------------
87 *
88 * This code for the SHA algorithm was noted as public domain. The original
89 * headers are pasted below.
90 *
91 * Several changes have been made to make it more compatible with the
92 * Python environment and desired interface.
93 *
94 */
95
96/* NIST Secure Hash Algorithm */
97/* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */
98/* from Peter C. Gutmann's implementation as found in */
99/* Applied Cryptography by Bruce Schneier */
100/* Further modifications to include the "UNRAVEL" stuff, below */
101
102/* This code is in the public domain */
103
104/* UNRAVEL should be fastest & biggest */
105/* UNROLL_LOOPS should be just as big, but slightly slower */
106/* both undefined should be smallest and slowest */
107
108#define UNRAVEL
109/* #define UNROLL_LOOPS */
110
111/* The SHA f()-functions. The f1 and f3 functions can be optimized to
112 save one boolean operation each - thanks to Rich Schroeppel,
113 rcs@cs.arizona.edu for discovering this */
114
115/*#define f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */
116#define f1(x,y,z) (z ^ (x & (y ^ z))) /* Rounds 0-19 */
117#define f2(x,y,z) (x ^ y ^ z) /* Rounds 20-39 */
118/*#define f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */
119#define f3(x,y,z) ((x & y) | (z & (x | y))) /* Rounds 40-59 */
120#define f4(x,y,z) (x ^ y ^ z) /* Rounds 60-79 */
121
122/* SHA constants */
123
124#define CONST1 0x5a827999L /* Rounds 0-19 */
125#define CONST2 0x6ed9eba1L /* Rounds 20-39 */
126#define CONST3 0x8f1bbcdcL /* Rounds 40-59 */
127#define CONST4 0xca62c1d6L /* Rounds 60-79 */
128
129/* 32-bit rotate */
130
131#define R32(x,n) ((x << n) | (x >> (32 - n)))
132
133/* the generic case, for when the overall rotation is not unraveled */
134
135#define FG(n) \
136 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; \
137 E = D; D = C; C = R32(B,30); B = A; A = T
138
139/* specific cases, for when the overall rotation is unraveled */
140
141#define FA(n) \
142 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; B = R32(B,30)
143
144#define FB(n) \
145 E = R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n; A = R32(A,30)
146
147#define FC(n) \
148 D = R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n; T = R32(T,30)
149
150#define FD(n) \
151 C = R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n; E = R32(E,30)
152
153#define FE(n) \
154 B = R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n; D = R32(D,30)
155
156#define FT(n) \
157 A = R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n; C = R32(C,30)
158
159/* do SHA transformation */
160
161static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000162sha_transform(SHAobject *sha_info)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000163{
164 int i;
165 SHA_INT32 T, A, B, C, D, E, W[80], *WP;
166
167 memcpy(W, sha_info->data, sizeof(sha_info->data));
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000168 longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000169
170 for (i = 16; i < 80; ++i) {
171 W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
172
173 /* extra rotation fix */
174 W[i] = R32(W[i], 1);
175 }
176 A = sha_info->digest[0];
177 B = sha_info->digest[1];
178 C = sha_info->digest[2];
179 D = sha_info->digest[3];
180 E = sha_info->digest[4];
181 WP = W;
182#ifdef UNRAVEL
183 FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
184 FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
185 FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
186 FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
187 FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
188 FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
189 FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
190 FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
191 sha_info->digest[0] += E;
192 sha_info->digest[1] += T;
193 sha_info->digest[2] += A;
194 sha_info->digest[3] += B;
195 sha_info->digest[4] += C;
196#else /* !UNRAVEL */
197#ifdef UNROLL_LOOPS
198 FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
199 FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
200 FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
201 FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
202 FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
203 FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
204 FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
205 FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
206#else /* !UNROLL_LOOPS */
207 for (i = 0; i < 20; ++i) { FG(1); }
208 for (i = 20; i < 40; ++i) { FG(2); }
209 for (i = 40; i < 60; ++i) { FG(3); }
210 for (i = 60; i < 80; ++i) { FG(4); }
211#endif /* !UNROLL_LOOPS */
212 sha_info->digest[0] += A;
213 sha_info->digest[1] += B;
214 sha_info->digest[2] += C;
215 sha_info->digest[3] += D;
216 sha_info->digest[4] += E;
217#endif /* !UNRAVEL */
218}
219
220/* initialize the SHA digest */
221
222static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000223sha_init(SHAobject *sha_info)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000224{
225 TestEndianness(sha_info->Endianness)
226
227 sha_info->digest[0] = 0x67452301L;
228 sha_info->digest[1] = 0xefcdab89L;
229 sha_info->digest[2] = 0x98badcfeL;
230 sha_info->digest[3] = 0x10325476L;
231 sha_info->digest[4] = 0xc3d2e1f0L;
232 sha_info->count_lo = 0L;
233 sha_info->count_hi = 0L;
234 sha_info->local = 0;
235}
236
237/* update the SHA digest */
238
239static void
Gregory P. Smithea388262009-02-13 03:00:00 +0000240sha_update(SHAobject *sha_info, SHA_BYTE *buffer, unsigned int count)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000241{
Gregory P. Smithea388262009-02-13 03:00:00 +0000242 unsigned int i;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000243 SHA_INT32 clo;
244
245 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
246 if (clo < sha_info->count_lo) {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000247 ++sha_info->count_hi;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000248 }
249 sha_info->count_lo = clo;
250 sha_info->count_hi += (SHA_INT32) count >> 29;
251 if (sha_info->local) {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000252 i = SHA_BLOCKSIZE - sha_info->local;
253 if (i > count) {
254 i = count;
255 }
256 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
257 count -= i;
258 buffer += i;
259 sha_info->local += i;
260 if (sha_info->local == SHA_BLOCKSIZE) {
261 sha_transform(sha_info);
262 }
263 else {
264 return;
265 }
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000266 }
267 while (count >= SHA_BLOCKSIZE) {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000268 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
269 buffer += SHA_BLOCKSIZE;
270 count -= SHA_BLOCKSIZE;
271 sha_transform(sha_info);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000272 }
273 memcpy(sha_info->data, buffer, count);
274 sha_info->local = count;
275}
276
277/* finish computing the SHA digest */
278
279static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000280sha_final(unsigned char digest[20], SHAobject *sha_info)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000281{
282 int count;
283 SHA_INT32 lo_bit_count, hi_bit_count;
284
285 lo_bit_count = sha_info->count_lo;
286 hi_bit_count = sha_info->count_hi;
287 count = (int) ((lo_bit_count >> 3) & 0x3f);
288 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000289 if (count > SHA_BLOCKSIZE - 8) {
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000290 memset(((SHA_BYTE *) sha_info->data) + count, 0,
291 SHA_BLOCKSIZE - count);
292 sha_transform(sha_info);
293 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
294 }
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000295 else {
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000296 memset(((SHA_BYTE *) sha_info->data) + count, 0,
297 SHA_BLOCKSIZE - 8 - count);
298 }
299
300 /* GJS: note that we add the hi/lo in big-endian. sha_transform will
301 swap these values into host-order. */
302 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
303 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
304 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
305 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
306 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
307 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
308 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
309 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
310 sha_transform(sha_info);
311 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
312 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
313 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
314 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
315 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
316 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
317 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
318 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
319 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
320 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
321 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
322 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
323 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
324 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
325 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
326 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
327 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
328 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
329 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
330 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
331}
332
333/*
334 * End of copied SHA code.
335 *
336 * ------------------------------------------------------------------------
337 */
338
Jeremy Hylton938ace62002-07-17 16:30:39 +0000339static PyTypeObject SHAtype;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000340
341
342static SHAobject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000343newSHAobject(void)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000344{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000345 return (SHAobject *)PyObject_New(SHAobject, &SHAtype);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000346}
347
348/* Internal methods for a hashing object */
349
350static void
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000351SHA_dealloc(PyObject *ptr)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000352{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000353 PyObject_Del(ptr);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000354}
355
356
357/* External methods for a hashing object */
358
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000359PyDoc_STRVAR(SHA_copy__doc__, "Return a copy of the hashing object.");
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000360
361static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000362SHA_copy(SHAobject *self, PyObject *unused)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000363{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000364 SHAobject *newobj;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000365
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000366 if ( (newobj = newSHAobject())==NULL)
367 return NULL;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000368
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000369 SHAcopy(self, newobj);
370 return (PyObject *)newobj;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000371}
372
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000373PyDoc_STRVAR(SHA_digest__doc__,
374"Return the digest value as a string of binary data.");
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000375
376static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000377SHA_digest(SHAobject *self, PyObject *unused)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000378{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000379 unsigned char digest[SHA_DIGESTSIZE];
380 SHAobject temp;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000381
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000382 SHAcopy(self, &temp);
383 sha_final(digest, &temp);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000384 return PyString_FromStringAndSize((const char *)digest, sizeof(digest));
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000385}
386
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000387PyDoc_STRVAR(SHA_hexdigest__doc__,
388"Return the digest value as a string of hexadecimal digits.");
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000389
390static PyObject *
Georg Brandl96a8c392006-05-29 21:04:52 +0000391SHA_hexdigest(SHAobject *self, PyObject *unused)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000392{
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000393 unsigned char digest[SHA_DIGESTSIZE];
394 SHAobject temp;
395 PyObject *retval;
396 char *hex_digest;
397 int i, j;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000398
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000399 /* Get the raw (binary) digest value */
400 SHAcopy(self, &temp);
401 sha_final(digest, &temp);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000402
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000403 /* Create a new string */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000404 retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2);
Barry Warsaw57b808d2000-08-15 06:03:35 +0000405 if (!retval)
406 return NULL;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000407 hex_digest = PyString_AsString(retval);
Barry Warsaw57b808d2000-08-15 06:03:35 +0000408 if (!hex_digest) {
409 Py_DECREF(retval);
410 return NULL;
411 }
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000412
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000413 /* Make hex version of the digest */
414 for(i=j=0; i<sizeof(digest); i++) {
415 char c;
Barry Warsaw57b808d2000-08-15 06:03:35 +0000416 c = (digest[i] >> 4) & 0xf;
417 c = (c>9) ? c+'a'-10 : c + '0';
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000418 hex_digest[j++] = c;
Barry Warsaw57b808d2000-08-15 06:03:35 +0000419 c = (digest[i] & 0xf);
420 c = (c>9) ? c+'a'-10 : c + '0';
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000421 hex_digest[j++] = c;
422 }
423 return retval;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000424}
425
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000426PyDoc_STRVAR(SHA_update__doc__,
427"Update this hashing object's state with the provided string.");
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000428
429static PyObject *
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000430SHA_update(SHAobject *self, PyObject *args)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000431{
Gregory P. Smithea388262009-02-13 03:00:00 +0000432 PyObject *data_obj;
433 Py_buffer view;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000434
Gregory P. Smithea388262009-02-13 03:00:00 +0000435 if (!PyArg_ParseTuple(args, "O:update", &data_obj))
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000436 return NULL;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000437
Gregory P. Smithea388262009-02-13 03:00:00 +0000438 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000439
Gregory P. Smithea388262009-02-13 03:00:00 +0000440 sha_update(self, (unsigned char*)view.buf,
441 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
442
443 PyBuffer_Release(&view);
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000444 Py_INCREF(Py_None);
445 return Py_None;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000446}
447
448static PyMethodDef SHA_methods[] = {
Georg Brandl96a8c392006-05-29 21:04:52 +0000449 {"copy", (PyCFunction)SHA_copy, METH_NOARGS, SHA_copy__doc__},
450 {"digest", (PyCFunction)SHA_digest, METH_NOARGS, SHA_digest__doc__},
451 {"hexdigest", (PyCFunction)SHA_hexdigest, METH_NOARGS, SHA_hexdigest__doc__},
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000452 {"update", (PyCFunction)SHA_update, METH_VARARGS, SHA_update__doc__},
453 {NULL, NULL} /* sentinel */
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000454};
455
456static PyObject *
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000457SHA_get_block_size(PyObject *self, void *closure)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000458{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000459 return PyInt_FromLong(SHA_BLOCKSIZE);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000460}
461
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000462static PyObject *
463SHA_get_digest_size(PyObject *self, void *closure)
464{
465 return PyInt_FromLong(SHA_DIGESTSIZE);
466}
467
468static PyObject *
469SHA_get_name(PyObject *self, void *closure)
470{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000471 return PyString_FromStringAndSize("SHA1", 4);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000472}
473
474static PyGetSetDef SHA_getseters[] = {
475 {"digest_size",
476 (getter)SHA_get_digest_size, NULL,
477 NULL,
478 NULL},
479 {"block_size",
480 (getter)SHA_get_block_size, NULL,
481 NULL,
482 NULL},
483 {"name",
484 (getter)SHA_get_name, NULL,
485 NULL,
486 NULL},
487 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
488 * the old sha module also supported 'digestsize'. ugh. */
489 {"digestsize",
490 (getter)SHA_get_digest_size, NULL,
491 NULL,
492 NULL},
493 {NULL} /* Sentinel */
494};
495
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000496static PyTypeObject SHAtype = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000497 PyVarObject_HEAD_INIT(NULL, 0)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000498 "_sha.sha", /*tp_name*/
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000499 sizeof(SHAobject), /*tp_size*/
500 0, /*tp_itemsize*/
501 /* methods */
502 SHA_dealloc, /*tp_dealloc*/
503 0, /*tp_print*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000504 0, /*tp_getattr*/
505 0, /*tp_setattr*/
506 0, /*tp_compare*/
507 0, /*tp_repr*/
508 0, /*tp_as_number*/
509 0, /*tp_as_sequence*/
510 0, /*tp_as_mapping*/
511 0, /*tp_hash*/
512 0, /*tp_call*/
513 0, /*tp_str*/
514 0, /*tp_getattro*/
515 0, /*tp_setattro*/
516 0, /*tp_as_buffer*/
517 Py_TPFLAGS_DEFAULT, /*tp_flags*/
518 0, /*tp_doc*/
519 0, /*tp_traverse*/
520 0, /*tp_clear*/
521 0, /*tp_richcompare*/
522 0, /*tp_weaklistoffset*/
523 0, /*tp_iter*/
524 0, /*tp_iternext*/
525 SHA_methods, /* tp_methods */
526 0, /* tp_members */
527 SHA_getseters, /* tp_getset */
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000528};
529
530
531/* The single module-level function: new() */
532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000533PyDoc_STRVAR(SHA_new__doc__,
534"Return a new SHA hashing object. An optional string argument\n\
535may be provided; if present, this string will be automatically\n\
536hashed.");
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000537
538static PyObject *
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000539SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000540{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000541 static char *kwlist[] = {"string", NULL};
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000542 SHAobject *new;
Gregory P. Smithea388262009-02-13 03:00:00 +0000543 PyObject *data_obj = NULL;
544 Py_buffer view;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000545
Gregory P. Smithea388262009-02-13 03:00:00 +0000546 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
547 &data_obj)) {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000548 return NULL;
549 }
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000550
Kristján Valur Jónsson7705d0a2009-03-03 03:20:42 +0000551 if (data_obj)
552 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
Gregory P. Smithea388262009-02-13 03:00:00 +0000553
554 if ((new = newSHAobject()) == NULL) {
Kristján Valur Jónsson7705d0a2009-03-03 03:20:42 +0000555 if (data_obj)
556 PyBuffer_Release(&view);
Jeremy Hylton55087f02001-01-29 22:46:35 +0000557 return NULL;
Gregory P. Smithea388262009-02-13 03:00:00 +0000558 }
Jeremy Hylton55087f02001-01-29 22:46:35 +0000559
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000560 sha_init(new);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000561
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000562 if (PyErr_Occurred()) {
563 Py_DECREF(new);
Kristján Valur Jónsson7705d0a2009-03-03 03:20:42 +0000564 if (data_obj)
565 PyBuffer_Release(&view);
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000566 return NULL;
567 }
Gregory P. Smithea388262009-02-13 03:00:00 +0000568 if (data_obj) {
569 sha_update(new, (unsigned char*)view.buf,
570 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
Kristján Valur Jónsson7705d0a2009-03-03 03:20:42 +0000571 PyBuffer_Release(&view);
Gregory P. Smithea388262009-02-13 03:00:00 +0000572 }
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000573
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000574 return (PyObject *)new;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000575}
576
577
578/* List of functions exported by this module */
579
580static struct PyMethodDef SHA_functions[] = {
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000581 {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__},
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000582 {NULL, NULL} /* Sentinel */
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000583};
584
585
586/* Initialize this module. */
587
Fred Drake8b14ac92001-11-02 22:04:17 +0000588#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000589
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000590PyMODINIT_FUNC
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000591init_sha(void)
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000592{
Fred Draked63e5042002-04-01 14:30:50 +0000593 PyObject *m;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000594
Christian Heimese93237d2007-12-19 02:37:44 +0000595 Py_TYPE(&SHAtype) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000596 if (PyType_Ready(&SHAtype) < 0)
597 return;
598 m = Py_InitModule("_sha", SHA_functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000599 if (m == NULL)
600 return;
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000601
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000602 /* Add some symbolic constants to the module */
Fred Drake2c4a3dc2000-07-08 06:41:03 +0000603 insint("blocksize", 1); /* For future use, in case some hash
604 functions require an integral number of
605 blocks */
606 insint("digestsize", 20);
Andrew M. Kuchling75fec2c2001-11-02 21:41:00 +0000607 insint("digest_size", 20);
Guido van Rossum29d2acc1999-03-24 19:03:59 +0000608}