blob: 9f6b41639c1ee1f8d42de2b94808f34cdb647541 [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
24/* Endianness testing and definitions */
25#define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000027
28#define PCT_LITTLE_ENDIAN 1
29#define PCT_BIG_ENDIAN 0
30
31/* Some useful types */
32
33typedef unsigned char SHA_BYTE;
34
35#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036typedef unsigned int SHA_INT32; /* 32-bit integer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000037#else
38/* not defined. compilation will die. */
39#endif
40
41/* The SHA block size and message digest sizes, in bytes */
42
43#define SHA_BLOCKSIZE 64
44#define SHA_DIGESTSIZE 32
45
46/* The structure for storing SHA info */
47
48typedef struct {
49 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 SHA_INT32 digest[8]; /* Message digest */
51 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
52 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000053 int Endianness;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 int local; /* unprocessed amount in data */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000055 int digestsize;
56} SHAobject;
57
58/* When run on a little-endian CPU we need to perform byte reversal on an
59 array of longwords. */
60
61static void longReverse(SHA_INT32 *buffer, int byteCount, int Endianness)
62{
63 SHA_INT32 value;
64
65 if ( Endianness == PCT_BIG_ENDIAN )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 return;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000067
68 byteCount /= sizeof(*buffer);
69 while (byteCount--) {
70 value = *buffer;
71 value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
72 ( ( value & 0x00FF00FFL ) << 8 );
73 *buffer++ = ( value << 16 ) | ( value >> 16 );
74 }
75}
76
77static void SHAcopy(SHAobject *src, SHAobject *dest)
78{
79 dest->Endianness = src->Endianness;
80 dest->local = src->local;
81 dest->digestsize = src->digestsize;
82 dest->count_lo = src->count_lo;
83 dest->count_hi = src->count_hi;
84 memcpy(dest->digest, src->digest, sizeof(src->digest));
85 memcpy(dest->data, src->data, sizeof(src->data));
86}
87
88
89/* ------------------------------------------------------------------------
90 *
91 * This code for the SHA-256 algorithm was noted as public domain. The
92 * original headers are pasted below.
93 *
94 * Several changes have been made to make it more compatible with the
95 * Python environment and desired interface.
96 *
97 */
98
99/* LibTomCrypt, modular cryptographic library -- Tom St Denis
100 *
101 * LibTomCrypt is a library that provides various cryptographic
102 * algorithms in a highly modular and flexible manner.
103 *
104 * The library is free for all purposes without any express
105 * gurantee it works.
106 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000107 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000108 */
109
110
111/* SHA256 by Tom St Denis */
112
113/* Various logical functions */
114#define ROR(x, y)\
115( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
116((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
117#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000119#define S(x, n) ROR((x),(n))
120#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
121#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
122#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
123#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
124#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
125
126
127static void
128sha_transform(SHAobject *sha_info)
129{
130 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 SHA_INT32 S[8], W[64], t0, t1;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000132
133 memcpy(W, sha_info->data, sizeof(sha_info->data));
134 longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
135
136 for (i = 16; i < 64; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000138 }
139 for (i = 0; i < 8; ++i) {
140 S[i] = sha_info->digest[i];
141 }
142
143 /* Compress */
144#define RND(a,b,c,d,e,f,g,h,i,ki) \
145 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
146 t1 = Sigma0(a) + Maj(a, b, c); \
147 d += t0; \
148 h = t0 + t1;
149
150 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
151 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
152 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
153 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
154 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
155 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
156 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
157 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
158 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
159 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
160 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
161 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
162 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
163 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
164 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
165 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
166 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
167 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
168 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
169 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
170 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
171 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
172 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
173 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
174 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
175 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
176 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
177 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
178 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
179 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
180 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
181 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
182 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
183 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
184 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
185 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
186 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
187 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
188 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
189 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
190 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
191 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
192 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
193 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
194 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
195 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
196 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
197 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
198 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
199 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
200 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
201 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
202 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
203 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
204 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
205 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
206 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
207 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
208 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
209 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
210 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
211 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
212 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
213 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215#undef RND
216
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000217 /* feedback */
218 for (i = 0; i < 8; i++) {
219 sha_info->digest[i] = sha_info->digest[i] + S[i];
220 }
221
222}
223
224
225
226/* initialize the SHA digest */
227
228static void
229sha_init(SHAobject *sha_info)
230{
231 TestEndianness(sha_info->Endianness)
232 sha_info->digest[0] = 0x6A09E667L;
233 sha_info->digest[1] = 0xBB67AE85L;
234 sha_info->digest[2] = 0x3C6EF372L;
235 sha_info->digest[3] = 0xA54FF53AL;
236 sha_info->digest[4] = 0x510E527FL;
237 sha_info->digest[5] = 0x9B05688CL;
238 sha_info->digest[6] = 0x1F83D9ABL;
239 sha_info->digest[7] = 0x5BE0CD19L;
240 sha_info->count_lo = 0L;
241 sha_info->count_hi = 0L;
242 sha_info->local = 0;
243 sha_info->digestsize = 32;
244}
245
246static void
247sha224_init(SHAobject *sha_info)
248{
249 TestEndianness(sha_info->Endianness)
250 sha_info->digest[0] = 0xc1059ed8L;
251 sha_info->digest[1] = 0x367cd507L;
252 sha_info->digest[2] = 0x3070dd17L;
253 sha_info->digest[3] = 0xf70e5939L;
254 sha_info->digest[4] = 0xffc00b31L;
255 sha_info->digest[5] = 0x68581511L;
256 sha_info->digest[6] = 0x64f98fa7L;
257 sha_info->digest[7] = 0xbefa4fa4L;
258 sha_info->count_lo = 0L;
259 sha_info->count_hi = 0L;
260 sha_info->local = 0;
261 sha_info->digestsize = 28;
262}
263
264
265/* update the SHA digest */
266
267static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000268sha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000269{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000270 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000271 SHA_INT32 clo;
272
273 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
274 if (clo < sha_info->count_lo) {
275 ++sha_info->count_hi;
276 }
277 sha_info->count_lo = clo;
278 sha_info->count_hi += (SHA_INT32) count >> 29;
279 if (sha_info->local) {
280 i = SHA_BLOCKSIZE - sha_info->local;
281 if (i > count) {
282 i = count;
283 }
284 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
285 count -= i;
286 buffer += i;
287 sha_info->local += i;
288 if (sha_info->local == SHA_BLOCKSIZE) {
289 sha_transform(sha_info);
290 }
291 else {
292 return;
293 }
294 }
295 while (count >= SHA_BLOCKSIZE) {
296 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
297 buffer += SHA_BLOCKSIZE;
298 count -= SHA_BLOCKSIZE;
299 sha_transform(sha_info);
300 }
301 memcpy(sha_info->data, buffer, count);
302 sha_info->local = count;
303}
304
305/* finish computing the SHA digest */
306
307static void
308sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
309{
310 int count;
311 SHA_INT32 lo_bit_count, hi_bit_count;
312
313 lo_bit_count = sha_info->count_lo;
314 hi_bit_count = sha_info->count_hi;
315 count = (int) ((lo_bit_count >> 3) & 0x3f);
316 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
317 if (count > SHA_BLOCKSIZE - 8) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 memset(((SHA_BYTE *) sha_info->data) + count, 0,
319 SHA_BLOCKSIZE - count);
320 sha_transform(sha_info);
321 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000322 }
323 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 memset(((SHA_BYTE *) sha_info->data) + count, 0,
325 SHA_BLOCKSIZE - 8 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000326 }
327
328 /* GJS: note that we add the hi/lo in big-endian. sha_transform will
329 swap these values into host-order. */
330 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
331 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
332 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
333 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
334 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
335 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
336 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
337 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
338 sha_transform(sha_info);
339 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
340 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
341 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
342 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
343 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
344 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
345 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
346 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
347 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
348 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
349 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
350 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
351 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
352 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
353 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
354 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
355 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
356 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
357 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
358 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
359 digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
360 digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
361 digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
362 digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
363 digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
364 digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
365 digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
366 digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
367 digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
368 digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
369 digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
370 digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
371}
372
373/*
374 * End of copied SHA code.
375 *
376 * ------------------------------------------------------------------------
377 */
378
379static PyTypeObject SHA224type;
380static PyTypeObject SHA256type;
381
382
383static SHAobject *
384newSHA224object(void)
385{
386 return (SHAobject *)PyObject_New(SHAobject, &SHA224type);
387}
388
389static SHAobject *
390newSHA256object(void)
391{
392 return (SHAobject *)PyObject_New(SHAobject, &SHA256type);
393}
394
395/* Internal methods for a hash object */
396
397static void
398SHA_dealloc(PyObject *ptr)
399{
400 PyObject_Del(ptr);
401}
402
403
404/* External methods for a hash object */
405
406PyDoc_STRVAR(SHA256_copy__doc__, "Return a copy of the hash object.");
407
408static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000409SHA256_copy(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000410{
411 SHAobject *newobj;
412
Christian Heimes90aa7642007-12-19 02:45:37 +0000413 if (Py_TYPE(self) == &SHA256type) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000414 if ( (newobj = newSHA256object())==NULL)
415 return NULL;
416 } else {
417 if ( (newobj = newSHA224object())==NULL)
418 return NULL;
419 }
420
421 SHAcopy(self, newobj);
422 return (PyObject *)newobj;
423}
424
425PyDoc_STRVAR(SHA256_digest__doc__,
426"Return the digest value as a string of binary data.");
427
428static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000429SHA256_digest(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000430{
431 unsigned char digest[SHA_DIGESTSIZE];
432 SHAobject temp;
433
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000434 SHAcopy(self, &temp);
435 sha_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000436 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000437}
438
439PyDoc_STRVAR(SHA256_hexdigest__doc__,
440"Return the digest value as a string of hexadecimal digits.");
441
442static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000443SHA256_hexdigest(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000444{
445 unsigned char digest[SHA_DIGESTSIZE];
446 SHAobject temp;
447 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200448 Py_UCS1 *hex_digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000449 int i, j;
450
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000451 /* Get the raw (binary) digest value */
452 SHAcopy(self, &temp);
453 sha_final(digest, &temp);
454
455 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200456 retval = PyUnicode_New(self->digestsize * 2, 127);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000457 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200459 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000460
461 /* Make hex version of the digest */
462 for(i=j=0; i<self->digestsize; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200463 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000464 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200465 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000466 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200467 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000468 }
Christian Heimesf402e922013-01-03 09:21:55 +0100469#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200470 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100471#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000472 return retval;
473}
474
475PyDoc_STRVAR(SHA256_update__doc__,
476"Update this hash object's state with the provided string.");
477
478static PyObject *
479SHA256_update(SHAobject *self, PyObject *args)
480{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000481 PyObject *obj;
482 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000483
Gregory P. Smith365a1862009-02-12 07:35:29 +0000484 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000485 return NULL;
486
Gregory P. Smith365a1862009-02-12 07:35:29 +0000487 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000488
Gregory P. Smith365a1862009-02-12 07:35:29 +0000489 sha_update(self, buf.buf, buf.len);
490
491 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000492 Py_INCREF(Py_None);
493 return Py_None;
494}
495
496static PyMethodDef SHA_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 {"copy", (PyCFunction)SHA256_copy, METH_NOARGS, SHA256_copy__doc__},
498 {"digest", (PyCFunction)SHA256_digest, METH_NOARGS, SHA256_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000499 {"hexdigest", (PyCFunction)SHA256_hexdigest, METH_NOARGS, SHA256_hexdigest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 {"update", (PyCFunction)SHA256_update, METH_VARARGS, SHA256_update__doc__},
501 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000502};
503
504static PyObject *
505SHA256_get_block_size(PyObject *self, void *closure)
506{
Christian Heimes217cfd12007-12-02 14:31:20 +0000507 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000508}
509
510static PyObject *
511SHA256_get_name(PyObject *self, void *closure)
512{
513 if (((SHAobject *)self)->digestsize == 32)
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000514 return PyUnicode_FromStringAndSize("SHA256", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000515 else
Guido van Rossum5ed033b2007-07-09 14:29:40 +0000516 return PyUnicode_FromStringAndSize("SHA224", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000517}
518
519static PyGetSetDef SHA_getseters[] = {
520 {"block_size",
521 (getter)SHA256_get_block_size, NULL,
522 NULL,
523 NULL},
524 {"name",
525 (getter)SHA256_get_name, NULL,
526 NULL,
527 NULL},
528 {NULL} /* Sentinel */
529};
530
531static PyMemberDef SHA_members[] = {
532 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000533 {NULL} /* Sentinel */
534};
535
536static PyTypeObject SHA224type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000537 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 "_sha256.sha224", /*tp_name*/
539 sizeof(SHAobject), /*tp_size*/
540 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000541 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 SHA_dealloc, /*tp_dealloc*/
543 0, /*tp_print*/
544 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000545 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000546 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000547 0, /*tp_repr*/
548 0, /*tp_as_number*/
549 0, /*tp_as_sequence*/
550 0, /*tp_as_mapping*/
551 0, /*tp_hash*/
552 0, /*tp_call*/
553 0, /*tp_str*/
554 0, /*tp_getattro*/
555 0, /*tp_setattro*/
556 0, /*tp_as_buffer*/
557 Py_TPFLAGS_DEFAULT, /*tp_flags*/
558 0, /*tp_doc*/
559 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 0, /*tp_clear*/
561 0, /*tp_richcompare*/
562 0, /*tp_weaklistoffset*/
563 0, /*tp_iter*/
564 0, /*tp_iternext*/
565 SHA_methods, /* tp_methods */
566 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000567 SHA_getseters, /* tp_getset */
568};
569
570static PyTypeObject SHA256type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000571 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 "_sha256.sha256", /*tp_name*/
573 sizeof(SHAobject), /*tp_size*/
574 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000575 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 SHA_dealloc, /*tp_dealloc*/
577 0, /*tp_print*/
578 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000579 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000580 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000581 0, /*tp_repr*/
582 0, /*tp_as_number*/
583 0, /*tp_as_sequence*/
584 0, /*tp_as_mapping*/
585 0, /*tp_hash*/
586 0, /*tp_call*/
587 0, /*tp_str*/
588 0, /*tp_getattro*/
589 0, /*tp_setattro*/
590 0, /*tp_as_buffer*/
591 Py_TPFLAGS_DEFAULT, /*tp_flags*/
592 0, /*tp_doc*/
593 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 0, /*tp_clear*/
595 0, /*tp_richcompare*/
596 0, /*tp_weaklistoffset*/
597 0, /*tp_iter*/
598 0, /*tp_iternext*/
599 SHA_methods, /* tp_methods */
600 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000601 SHA_getseters, /* tp_getset */
602};
603
604
605/* The single module-level function: new() */
606
607PyDoc_STRVAR(SHA256_new__doc__,
608"Return a new SHA-256 hash object; optionally initialized with a string.");
609
610static PyObject *
611SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
612{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000613 static char *kwlist[] = {"string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000614 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000615 PyObject *data_obj = NULL;
616 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000617
Gregory P. Smith365a1862009-02-12 07:35:29 +0000618 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
619 &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000620 return NULL;
621 }
622
Gregory P. Smith365a1862009-02-12 07:35:29 +0000623 if (data_obj)
624 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
625
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000626 if ((new = newSHA256object()) == NULL) {
627 if (data_obj)
628 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000629 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000630 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000631
632 sha_init(new);
633
634 if (PyErr_Occurred()) {
635 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000636 if (data_obj)
637 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000638 return NULL;
639 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000640 if (data_obj) {
641 sha_update(new, buf.buf, buf.len);
642 PyBuffer_Release(&buf);
643 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000644
645 return (PyObject *)new;
646}
647
648PyDoc_STRVAR(SHA224_new__doc__,
649"Return a new SHA-224 hash object; optionally initialized with a string.");
650
651static PyObject *
652SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
653{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000654 static char *kwlist[] = {"string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000655 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000656 PyObject *data_obj = NULL;
657 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000658
Gregory P. Smith365a1862009-02-12 07:35:29 +0000659 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
660 &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000661 return NULL;
662 }
663
Gregory P. Smith365a1862009-02-12 07:35:29 +0000664 if (data_obj)
665 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
666
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000667 if ((new = newSHA224object()) == NULL) {
668 if (data_obj)
669 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000670 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000671 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000672
673 sha224_init(new);
674
675 if (PyErr_Occurred()) {
676 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000677 if (data_obj)
678 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000679 return NULL;
680 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000681 if (data_obj) {
682 sha_update(new, buf.buf, buf.len);
683 PyBuffer_Release(&buf);
684 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000685
686 return (PyObject *)new;
687}
688
689
690/* List of functions exported by this module */
691
692static struct PyMethodDef SHA_functions[] = {
693 {"sha256", (PyCFunction)SHA256_new, METH_VARARGS|METH_KEYWORDS, SHA256_new__doc__},
694 {"sha224", (PyCFunction)SHA224_new, METH_VARARGS|METH_KEYWORDS, SHA224_new__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000696};
697
698
699/* Initialize this module. */
700
701#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
702
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000703
Martin v. Löwis1a214512008-06-11 05:26:20 +0000704static struct PyModuleDef _sha256module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 PyModuleDef_HEAD_INIT,
706 "_sha256",
707 NULL,
708 -1,
709 SHA_functions,
710 NULL,
711 NULL,
712 NULL,
713 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000714};
715
716PyMODINIT_FUNC
717PyInit__sha256(void)
718{
Christian Heimes90aa7642007-12-19 02:45:37 +0000719 Py_TYPE(&SHA224type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000720 if (PyType_Ready(&SHA224type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000721 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000722 Py_TYPE(&SHA256type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000723 if (PyType_Ready(&SHA256type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000724 return NULL;
725 return PyModule_Create(&_sha256module);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000726}