blob: 4abd1cd43a89c7f2a0374f4751609528add78057 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* SHA256 module */
2
3/* This module provides an interface to NIST's SHA-256 and SHA-224 Algorithms */
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
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000012 Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000013 Licensed to PSF under a Contributor Agreement.
14
15*/
16
17/* SHA objects */
18
19#include "Python.h"
20#include "structmember.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000021#include "hashlib.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000022
23
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000024/* Some useful types */
25
26typedef unsigned char SHA_BYTE;
27
28#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029typedef unsigned int SHA_INT32; /* 32-bit integer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000030#else
31/* not defined. compilation will die. */
32#endif
33
34/* The SHA block size and message digest sizes, in bytes */
35
36#define SHA_BLOCKSIZE 64
37#define SHA_DIGESTSIZE 32
38
39/* The structure for storing SHA info */
40
41typedef struct {
42 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 SHA_INT32 digest[8]; /* Message digest */
44 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
45 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 int local; /* unprocessed amount in data */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000047 int digestsize;
48} SHAobject;
49
50/* When run on a little-endian CPU we need to perform byte reversal on an
51 array of longwords. */
52
Christian Heimes743e0cd2012-10-17 23:52:17 +020053#if PY_LITTLE_ENDIAN
54static void longReverse(SHA_INT32 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000055{
56 SHA_INT32 value;
57
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000058 byteCount /= sizeof(*buffer);
59 while (byteCount--) {
60 value = *buffer;
61 value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
62 ( ( value & 0x00FF00FFL ) << 8 );
63 *buffer++ = ( value << 16 ) | ( value >> 16 );
64 }
65}
Christian Heimes743e0cd2012-10-17 23:52:17 +020066#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000067
68static void SHAcopy(SHAobject *src, SHAobject *dest)
69{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000070 dest->local = src->local;
71 dest->digestsize = src->digestsize;
72 dest->count_lo = src->count_lo;
73 dest->count_hi = src->count_hi;
74 memcpy(dest->digest, src->digest, sizeof(src->digest));
75 memcpy(dest->data, src->data, sizeof(src->data));
76}
77
78
79/* ------------------------------------------------------------------------
80 *
81 * This code for the SHA-256 algorithm was noted as public domain. The
82 * original headers are pasted below.
83 *
84 * Several changes have been made to make it more compatible with the
85 * Python environment and desired interface.
86 *
87 */
88
89/* LibTomCrypt, modular cryptographic library -- Tom St Denis
90 *
91 * LibTomCrypt is a library that provides various cryptographic
92 * algorithms in a highly modular and flexible manner.
93 *
94 * The library is free for all purposes without any express
95 * gurantee it works.
96 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000097 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000098 */
99
100
101/* SHA256 by Tom St Denis */
102
103/* Various logical functions */
104#define ROR(x, y)\
105( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
106((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
107#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000109#define S(x, n) ROR((x),(n))
110#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
111#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
112#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
113#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
114#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
115
116
117static void
118sha_transform(SHAobject *sha_info)
119{
120 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 SHA_INT32 S[8], W[64], t0, t1;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000122
123 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200124#if PY_LITTLE_ENDIAN
125 longReverse(W, (int)sizeof(sha_info->data));
126#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000127
128 for (i = 16; i < 64; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000130 }
131 for (i = 0; i < 8; ++i) {
132 S[i] = sha_info->digest[i];
133 }
134
135 /* Compress */
136#define RND(a,b,c,d,e,f,g,h,i,ki) \
137 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
138 t1 = Sigma0(a) + Maj(a, b, c); \
139 d += t0; \
140 h = t0 + t1;
141
142 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
143 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
144 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
145 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
146 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
147 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
148 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
149 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
150 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
151 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
152 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
153 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
154 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
155 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
156 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
157 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
158 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
159 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
160 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
161 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
162 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
163 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
164 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
165 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
166 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
167 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
168 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
169 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
170 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
171 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
172 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
173 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
174 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
175 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
176 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
177 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
178 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
179 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
180 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
181 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
182 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
183 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
184 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
185 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
186 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
187 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
188 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
189 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
190 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
191 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
192 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
193 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
194 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
195 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
196 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
197 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
198 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
199 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
200 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
201 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
202 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
203 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
204 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
205 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207#undef RND
208
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000209 /* feedback */
210 for (i = 0; i < 8; i++) {
211 sha_info->digest[i] = sha_info->digest[i] + S[i];
212 }
213
214}
215
216
217
218/* initialize the SHA digest */
219
220static void
221sha_init(SHAobject *sha_info)
222{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000223 sha_info->digest[0] = 0x6A09E667L;
224 sha_info->digest[1] = 0xBB67AE85L;
225 sha_info->digest[2] = 0x3C6EF372L;
226 sha_info->digest[3] = 0xA54FF53AL;
227 sha_info->digest[4] = 0x510E527FL;
228 sha_info->digest[5] = 0x9B05688CL;
229 sha_info->digest[6] = 0x1F83D9ABL;
230 sha_info->digest[7] = 0x5BE0CD19L;
231 sha_info->count_lo = 0L;
232 sha_info->count_hi = 0L;
233 sha_info->local = 0;
234 sha_info->digestsize = 32;
235}
236
237static void
238sha224_init(SHAobject *sha_info)
239{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000240 sha_info->digest[0] = 0xc1059ed8L;
241 sha_info->digest[1] = 0x367cd507L;
242 sha_info->digest[2] = 0x3070dd17L;
243 sha_info->digest[3] = 0xf70e5939L;
244 sha_info->digest[4] = 0xffc00b31L;
245 sha_info->digest[5] = 0x68581511L;
246 sha_info->digest[6] = 0x64f98fa7L;
247 sha_info->digest[7] = 0xbefa4fa4L;
248 sha_info->count_lo = 0L;
249 sha_info->count_hi = 0L;
250 sha_info->local = 0;
251 sha_info->digestsize = 28;
252}
253
254
255/* update the SHA digest */
256
257static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000258sha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000259{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000260 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000261 SHA_INT32 clo;
262
263 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
264 if (clo < sha_info->count_lo) {
265 ++sha_info->count_hi;
266 }
267 sha_info->count_lo = clo;
268 sha_info->count_hi += (SHA_INT32) count >> 29;
269 if (sha_info->local) {
270 i = SHA_BLOCKSIZE - sha_info->local;
271 if (i > count) {
272 i = count;
273 }
274 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
275 count -= i;
276 buffer += i;
277 sha_info->local += i;
278 if (sha_info->local == SHA_BLOCKSIZE) {
279 sha_transform(sha_info);
280 }
281 else {
282 return;
283 }
284 }
285 while (count >= SHA_BLOCKSIZE) {
286 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
287 buffer += SHA_BLOCKSIZE;
288 count -= SHA_BLOCKSIZE;
289 sha_transform(sha_info);
290 }
291 memcpy(sha_info->data, buffer, count);
292 sha_info->local = count;
293}
294
295/* finish computing the SHA digest */
296
297static void
298sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
299{
300 int count;
301 SHA_INT32 lo_bit_count, hi_bit_count;
302
303 lo_bit_count = sha_info->count_lo;
304 hi_bit_count = sha_info->count_hi;
305 count = (int) ((lo_bit_count >> 3) & 0x3f);
306 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
307 if (count > SHA_BLOCKSIZE - 8) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 memset(((SHA_BYTE *) sha_info->data) + count, 0,
309 SHA_BLOCKSIZE - count);
310 sha_transform(sha_info);
311 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000312 }
313 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 memset(((SHA_BYTE *) sha_info->data) + count, 0,
315 SHA_BLOCKSIZE - 8 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000316 }
317
318 /* GJS: note that we add the hi/lo in big-endian. sha_transform will
319 swap these values into host-order. */
320 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
321 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
322 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
323 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
324 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
325 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
326 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
327 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
328 sha_transform(sha_info);
329 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
330 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
331 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
332 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
333 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
334 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
335 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
336 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
337 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
338 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
339 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
340 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
341 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
342 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
343 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
344 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
345 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
346 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
347 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
348 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
349 digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
350 digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
351 digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
352 digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
353 digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
354 digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
355 digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
356 digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
357 digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
358 digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
359 digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
360 digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
361}
362
363/*
364 * End of copied SHA code.
365 *
366 * ------------------------------------------------------------------------
367 */
368
369static PyTypeObject SHA224type;
370static PyTypeObject SHA256type;
371
372
373static SHAobject *
374newSHA224object(void)
375{
376 return (SHAobject *)PyObject_New(SHAobject, &SHA224type);
377}
378
379static SHAobject *
380newSHA256object(void)
381{
382 return (SHAobject *)PyObject_New(SHAobject, &SHA256type);
383}
384
385/* Internal methods for a hash object */
386
387static void
388SHA_dealloc(PyObject *ptr)
389{
390 PyObject_Del(ptr);
391}
392
393
394/* External methods for a hash object */
395
396PyDoc_STRVAR(SHA256_copy__doc__, "Return a copy of the hash object.");
397
398static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000399SHA256_copy(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000400{
401 SHAobject *newobj;
402
Christian Heimes90aa7642007-12-19 02:45:37 +0000403 if (Py_TYPE(self) == &SHA256type) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000404 if ( (newobj = newSHA256object())==NULL)
405 return NULL;
406 } else {
407 if ( (newobj = newSHA224object())==NULL)
408 return NULL;
409 }
410
411 SHAcopy(self, newobj);
412 return (PyObject *)newobj;
413}
414
415PyDoc_STRVAR(SHA256_digest__doc__,
416"Return the digest value as a string of binary data.");
417
418static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000419SHA256_digest(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000420{
421 unsigned char digest[SHA_DIGESTSIZE];
422 SHAobject temp;
423
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000424 SHAcopy(self, &temp);
425 sha_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000426 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000427}
428
429PyDoc_STRVAR(SHA256_hexdigest__doc__,
430"Return the digest value as a string of hexadecimal digits.");
431
432static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000433SHA256_hexdigest(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000434{
435 unsigned char digest[SHA_DIGESTSIZE];
436 SHAobject temp;
437 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200438 Py_UCS1 *hex_digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000439 int i, j;
440
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000441 /* Get the raw (binary) digest value */
442 SHAcopy(self, &temp);
443 sha_final(digest, &temp);
444
445 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200446 retval = PyUnicode_New(self->digestsize * 2, 127);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000447 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200449 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000450
451 /* Make hex version of the digest */
452 for(i=j=0; i<self->digestsize; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200453 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000454 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200455 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000456 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200457 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000458 }
Victor Stinner8f825062012-04-27 13:55:39 +0200459 assert(_PyUnicode_CheckConsistency(retval, 1));
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000460 return retval;
461}
462
463PyDoc_STRVAR(SHA256_update__doc__,
464"Update this hash object's state with the provided string.");
465
466static PyObject *
467SHA256_update(SHAobject *self, PyObject *args)
468{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000469 PyObject *obj;
470 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000471
Gregory P. Smith365a1862009-02-12 07:35:29 +0000472 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000473 return NULL;
474
Gregory P. Smith365a1862009-02-12 07:35:29 +0000475 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000476
Gregory P. Smith365a1862009-02-12 07:35:29 +0000477 sha_update(self, buf.buf, buf.len);
478
479 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000480 Py_INCREF(Py_None);
481 return Py_None;
482}
483
484static PyMethodDef SHA_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 {"copy", (PyCFunction)SHA256_copy, METH_NOARGS, SHA256_copy__doc__},
486 {"digest", (PyCFunction)SHA256_digest, METH_NOARGS, SHA256_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000487 {"hexdigest", (PyCFunction)SHA256_hexdigest, METH_NOARGS, SHA256_hexdigest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 {"update", (PyCFunction)SHA256_update, METH_VARARGS, SHA256_update__doc__},
489 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000490};
491
492static PyObject *
493SHA256_get_block_size(PyObject *self, void *closure)
494{
Christian Heimes217cfd12007-12-02 14:31:20 +0000495 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000496}
497
498static PyObject *
499SHA256_get_name(PyObject *self, void *closure)
500{
501 if (((SHAobject *)self)->digestsize == 32)
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000502 return PyUnicode_FromStringAndSize("SHA256", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000503 else
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000504 return PyUnicode_FromStringAndSize("SHA224", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000505}
506
507static PyGetSetDef SHA_getseters[] = {
508 {"block_size",
509 (getter)SHA256_get_block_size, NULL,
510 NULL,
511 NULL},
512 {"name",
513 (getter)SHA256_get_name, NULL,
514 NULL,
515 NULL},
516 {NULL} /* Sentinel */
517};
518
519static PyMemberDef SHA_members[] = {
520 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000521 {NULL} /* Sentinel */
522};
523
524static PyTypeObject SHA224type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000525 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 "_sha256.sha224", /*tp_name*/
527 sizeof(SHAobject), /*tp_size*/
528 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000529 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 SHA_dealloc, /*tp_dealloc*/
531 0, /*tp_print*/
532 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000533 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000534 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +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 SHA_methods, /* tp_methods */
554 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000555 SHA_getseters, /* tp_getset */
556};
557
558static PyTypeObject SHA256type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000559 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 "_sha256.sha256", /*tp_name*/
561 sizeof(SHAobject), /*tp_size*/
562 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000563 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 SHA_dealloc, /*tp_dealloc*/
565 0, /*tp_print*/
566 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000567 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000568 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000569 0, /*tp_repr*/
570 0, /*tp_as_number*/
571 0, /*tp_as_sequence*/
572 0, /*tp_as_mapping*/
573 0, /*tp_hash*/
574 0, /*tp_call*/
575 0, /*tp_str*/
576 0, /*tp_getattro*/
577 0, /*tp_setattro*/
578 0, /*tp_as_buffer*/
579 Py_TPFLAGS_DEFAULT, /*tp_flags*/
580 0, /*tp_doc*/
581 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 0, /*tp_clear*/
583 0, /*tp_richcompare*/
584 0, /*tp_weaklistoffset*/
585 0, /*tp_iter*/
586 0, /*tp_iternext*/
587 SHA_methods, /* tp_methods */
588 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000589 SHA_getseters, /* tp_getset */
590};
591
592
593/* The single module-level function: new() */
594
595PyDoc_STRVAR(SHA256_new__doc__,
596"Return a new SHA-256 hash object; optionally initialized with a string.");
597
598static PyObject *
599SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
600{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000601 static char *kwlist[] = {"string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000602 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000603 PyObject *data_obj = NULL;
604 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000605
Gregory P. Smith365a1862009-02-12 07:35:29 +0000606 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
607 &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000608 return NULL;
609 }
610
Gregory P. Smith365a1862009-02-12 07:35:29 +0000611 if (data_obj)
612 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
613
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000614 if ((new = newSHA256object()) == NULL) {
615 if (data_obj)
616 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000617 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000618 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000619
620 sha_init(new);
621
622 if (PyErr_Occurred()) {
623 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000624 if (data_obj)
625 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000626 return NULL;
627 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000628 if (data_obj) {
629 sha_update(new, buf.buf, buf.len);
630 PyBuffer_Release(&buf);
631 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000632
633 return (PyObject *)new;
634}
635
636PyDoc_STRVAR(SHA224_new__doc__,
637"Return a new SHA-224 hash object; optionally initialized with a string.");
638
639static PyObject *
640SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
641{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000642 static char *kwlist[] = {"string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000643 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000644 PyObject *data_obj = NULL;
645 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000646
Gregory P. Smith365a1862009-02-12 07:35:29 +0000647 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
648 &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000649 return NULL;
650 }
651
Gregory P. Smith365a1862009-02-12 07:35:29 +0000652 if (data_obj)
653 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
654
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000655 if ((new = newSHA224object()) == NULL) {
656 if (data_obj)
657 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000658 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000659 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000660
661 sha224_init(new);
662
663 if (PyErr_Occurred()) {
664 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000665 if (data_obj)
666 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000667 return NULL;
668 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000669 if (data_obj) {
670 sha_update(new, buf.buf, buf.len);
671 PyBuffer_Release(&buf);
672 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000673
674 return (PyObject *)new;
675}
676
677
678/* List of functions exported by this module */
679
680static struct PyMethodDef SHA_functions[] = {
681 {"sha256", (PyCFunction)SHA256_new, METH_VARARGS|METH_KEYWORDS, SHA256_new__doc__},
682 {"sha224", (PyCFunction)SHA224_new, METH_VARARGS|METH_KEYWORDS, SHA224_new__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000684};
685
686
687/* Initialize this module. */
688
689#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
690
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000691
Martin v. Löwis1a214512008-06-11 05:26:20 +0000692static struct PyModuleDef _sha256module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 PyModuleDef_HEAD_INIT,
694 "_sha256",
695 NULL,
696 -1,
697 SHA_functions,
698 NULL,
699 NULL,
700 NULL,
701 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000702};
703
704PyMODINIT_FUNC
705PyInit__sha256(void)
706{
Christian Heimes90aa7642007-12-19 02:45:37 +0000707 Py_TYPE(&SHA224type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000708 if (PyType_Ready(&SHA224type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000709 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000710 Py_TYPE(&SHA256type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000711 if (PyType_Ready(&SHA256type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000712 return NULL;
713 return PyModule_Create(&_sha256module);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000714}