blob: b05bfc172f517ac62a46e97ed8fffee9e067a142 [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;
Victor Stinner70792d22013-05-08 00:00:44 +0200277 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000278 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);
Victor Stinner70792d22013-05-08 00:00:44 +0200292 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000293}
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 }
Christian Heimesf402e922013-01-03 09:21:55 +0100459#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200460 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100461#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000462 return retval;
463}
464
465PyDoc_STRVAR(SHA256_update__doc__,
466"Update this hash object's state with the provided string.");
467
468static PyObject *
469SHA256_update(SHAobject *self, PyObject *args)
470{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000471 PyObject *obj;
472 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000473
Gregory P. Smith365a1862009-02-12 07:35:29 +0000474 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000475 return NULL;
476
Gregory P. Smith365a1862009-02-12 07:35:29 +0000477 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000478
Gregory P. Smith365a1862009-02-12 07:35:29 +0000479 sha_update(self, buf.buf, buf.len);
480
481 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000482 Py_INCREF(Py_None);
483 return Py_None;
484}
485
486static PyMethodDef SHA_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 {"copy", (PyCFunction)SHA256_copy, METH_NOARGS, SHA256_copy__doc__},
488 {"digest", (PyCFunction)SHA256_digest, METH_NOARGS, SHA256_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000489 {"hexdigest", (PyCFunction)SHA256_hexdigest, METH_NOARGS, SHA256_hexdigest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 {"update", (PyCFunction)SHA256_update, METH_VARARGS, SHA256_update__doc__},
491 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000492};
493
494static PyObject *
495SHA256_get_block_size(PyObject *self, void *closure)
496{
Christian Heimes217cfd12007-12-02 14:31:20 +0000497 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000498}
499
500static PyObject *
501SHA256_get_name(PyObject *self, void *closure)
502{
503 if (((SHAobject *)self)->digestsize == 32)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200504 return PyUnicode_FromStringAndSize("sha256", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000505 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200506 return PyUnicode_FromStringAndSize("sha224", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000507}
508
509static PyGetSetDef SHA_getseters[] = {
510 {"block_size",
511 (getter)SHA256_get_block_size, NULL,
512 NULL,
513 NULL},
514 {"name",
515 (getter)SHA256_get_name, NULL,
516 NULL,
517 NULL},
518 {NULL} /* Sentinel */
519};
520
521static PyMemberDef SHA_members[] = {
522 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000523 {NULL} /* Sentinel */
524};
525
526static PyTypeObject SHA224type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000527 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 "_sha256.sha224", /*tp_name*/
529 sizeof(SHAobject), /*tp_size*/
530 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000531 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 SHA_dealloc, /*tp_dealloc*/
533 0, /*tp_print*/
534 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000535 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000536 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000537 0, /*tp_repr*/
538 0, /*tp_as_number*/
539 0, /*tp_as_sequence*/
540 0, /*tp_as_mapping*/
541 0, /*tp_hash*/
542 0, /*tp_call*/
543 0, /*tp_str*/
544 0, /*tp_getattro*/
545 0, /*tp_setattro*/
546 0, /*tp_as_buffer*/
547 Py_TPFLAGS_DEFAULT, /*tp_flags*/
548 0, /*tp_doc*/
549 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 0, /*tp_clear*/
551 0, /*tp_richcompare*/
552 0, /*tp_weaklistoffset*/
553 0, /*tp_iter*/
554 0, /*tp_iternext*/
555 SHA_methods, /* tp_methods */
556 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000557 SHA_getseters, /* tp_getset */
558};
559
560static PyTypeObject SHA256type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000561 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 "_sha256.sha256", /*tp_name*/
563 sizeof(SHAobject), /*tp_size*/
564 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000565 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 SHA_dealloc, /*tp_dealloc*/
567 0, /*tp_print*/
568 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000569 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000570 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000571 0, /*tp_repr*/
572 0, /*tp_as_number*/
573 0, /*tp_as_sequence*/
574 0, /*tp_as_mapping*/
575 0, /*tp_hash*/
576 0, /*tp_call*/
577 0, /*tp_str*/
578 0, /*tp_getattro*/
579 0, /*tp_setattro*/
580 0, /*tp_as_buffer*/
581 Py_TPFLAGS_DEFAULT, /*tp_flags*/
582 0, /*tp_doc*/
583 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000584 0, /*tp_clear*/
585 0, /*tp_richcompare*/
586 0, /*tp_weaklistoffset*/
587 0, /*tp_iter*/
588 0, /*tp_iternext*/
589 SHA_methods, /* tp_methods */
590 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000591 SHA_getseters, /* tp_getset */
592};
593
594
595/* The single module-level function: new() */
596
597PyDoc_STRVAR(SHA256_new__doc__,
598"Return a new SHA-256 hash object; optionally initialized with a string.");
599
600static PyObject *
601SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
602{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000603 static char *kwlist[] = {"string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000604 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000605 PyObject *data_obj = NULL;
606 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000607
Gregory P. Smith365a1862009-02-12 07:35:29 +0000608 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
609 &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000610 return NULL;
611 }
612
Gregory P. Smith365a1862009-02-12 07:35:29 +0000613 if (data_obj)
614 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
615
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000616 if ((new = newSHA256object()) == NULL) {
617 if (data_obj)
618 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000619 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000620 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000621
622 sha_init(new);
623
624 if (PyErr_Occurred()) {
625 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000626 if (data_obj)
627 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000628 return NULL;
629 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000630 if (data_obj) {
631 sha_update(new, buf.buf, buf.len);
632 PyBuffer_Release(&buf);
633 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000634
635 return (PyObject *)new;
636}
637
638PyDoc_STRVAR(SHA224_new__doc__,
639"Return a new SHA-224 hash object; optionally initialized with a string.");
640
641static PyObject *
642SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
643{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000644 static char *kwlist[] = {"string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000645 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000646 PyObject *data_obj = NULL;
647 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000648
Gregory P. Smith365a1862009-02-12 07:35:29 +0000649 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
650 &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000651 return NULL;
652 }
653
Gregory P. Smith365a1862009-02-12 07:35:29 +0000654 if (data_obj)
655 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
656
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000657 if ((new = newSHA224object()) == NULL) {
658 if (data_obj)
659 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000660 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000661 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000662
663 sha224_init(new);
664
665 if (PyErr_Occurred()) {
666 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000667 if (data_obj)
668 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000669 return NULL;
670 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000671 if (data_obj) {
672 sha_update(new, buf.buf, buf.len);
673 PyBuffer_Release(&buf);
674 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000675
676 return (PyObject *)new;
677}
678
679
680/* List of functions exported by this module */
681
682static struct PyMethodDef SHA_functions[] = {
683 {"sha256", (PyCFunction)SHA256_new, METH_VARARGS|METH_KEYWORDS, SHA256_new__doc__},
684 {"sha224", (PyCFunction)SHA224_new, METH_VARARGS|METH_KEYWORDS, SHA224_new__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000686};
687
688
689/* Initialize this module. */
690
691#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
692
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000693
Martin v. Löwis1a214512008-06-11 05:26:20 +0000694static struct PyModuleDef _sha256module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 PyModuleDef_HEAD_INIT,
696 "_sha256",
697 NULL,
698 -1,
699 SHA_functions,
700 NULL,
701 NULL,
702 NULL,
703 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000704};
705
706PyMODINIT_FUNC
707PyInit__sha256(void)
708{
Christian Heimes327dd732013-10-22 15:05:23 +0200709 PyObject *m;
710
Christian Heimes90aa7642007-12-19 02:45:37 +0000711 Py_TYPE(&SHA224type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000712 if (PyType_Ready(&SHA224type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000713 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000714 Py_TYPE(&SHA256type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000715 if (PyType_Ready(&SHA256type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000716 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200717
718 m = PyModule_Create(&_sha256module);
719 if (m == NULL)
720 return NULL;
721
722 Py_INCREF((PyObject *)&SHA224type);
723 PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type);
724 Py_INCREF((PyObject *)&SHA256type);
725 PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type);
726 return m;
727
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000728}