Added information on function name added to LogRecord, and the 'extra' keyword parameter.
[python.git] / Modules / sha512module.c
blob3837795964fa9b9c46532b88d695a75efaf42a8b
1 /* SHA512 module */
3 /* This module provides an interface to NIST's SHA-512 and SHA-384 Algorithms */
5 /* See below for information about the original code this module was
6 based upon. Additional work performed by:
8 Andrew Kuchling (amk@amk.ca)
9 Greg Stein (gstein@lyra.org)
10 Trevor Perrin (trevp@trevp.net)
12 Copyright (C) 2005 Gregory P. Smith (greg@electricrain.com)
13 Licensed to PSF under a Contributor Agreement.
17 /* SHA objects */
19 #include "Python.h"
20 #include "structmember.h"
22 #ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
24 /* Endianness testing and definitions */
25 #define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
26 if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
28 #define PCT_LITTLE_ENDIAN 1
29 #define PCT_BIG_ENDIAN 0
31 /* Some useful types */
33 typedef unsigned char SHA_BYTE;
35 #if SIZEOF_INT == 4
36 typedef unsigned int SHA_INT32; /* 32-bit integer */
37 typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */
38 #else
39 /* not defined. compilation will die. */
40 #endif
42 /* The SHA block size and message digest sizes, in bytes */
44 #define SHA_BLOCKSIZE 128
45 #define SHA_DIGESTSIZE 64
47 /* The structure for storing SHA info */
49 typedef struct {
50 PyObject_HEAD
51 SHA_INT64 digest[8]; /* Message digest */
52 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
53 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
54 int Endianness;
55 int local; /* unprocessed amount in data */
56 int digestsize;
57 } SHAobject;
59 /* When run on a little-endian CPU we need to perform byte reversal on an
60 array of longwords. */
62 static void longReverse(SHA_INT64 *buffer, int byteCount, int Endianness)
64 SHA_INT64 value;
66 if ( Endianness == PCT_BIG_ENDIAN )
67 return;
69 byteCount /= sizeof(*buffer);
70 while (byteCount--) {
71 value = *buffer;
73 ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
74 ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
75 ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
76 ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
77 ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
78 ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
79 ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
80 ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
82 buffer++;
86 static void SHAcopy(SHAobject *src, SHAobject *dest)
88 dest->Endianness = src->Endianness;
89 dest->local = src->local;
90 dest->digestsize = src->digestsize;
91 dest->count_lo = src->count_lo;
92 dest->count_hi = src->count_hi;
93 memcpy(dest->digest, src->digest, sizeof(src->digest));
94 memcpy(dest->data, src->data, sizeof(src->data));
98 /* ------------------------------------------------------------------------
100 * This code for the SHA-512 algorithm was noted as public domain. The
101 * original headers are pasted below.
103 * Several changes have been made to make it more compatible with the
104 * Python environment and desired interface.
108 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
110 * LibTomCrypt is a library that provides various cryptographic
111 * algorithms in a highly modular and flexible manner.
113 * The library is free for all purposes without any express
114 * gurantee it works.
116 * Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org
120 /* SHA512 by Tom St Denis */
122 /* Various logical functions */
123 #define ROR64(x, y) \
124 ( ((((x) & 0xFFFFFFFFFFFFFFFFULL)>>((unsigned PY_LONG_LONG)(y) & 63)) | \
125 ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & 0xFFFFFFFFFFFFFFFFULL)
126 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
127 #define Maj(x,y,z) (((x | y) & z) | (x & y))
128 #define S(x, n) ROR64((x),(n))
129 #define R(x, n) (((x) & 0xFFFFFFFFFFFFFFFFULL) >> ((unsigned PY_LONG_LONG)n))
130 #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
131 #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
132 #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
133 #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
136 static void
137 sha512_transform(SHAobject *sha_info)
139 int i;
140 SHA_INT64 S[8], W[80], t0, t1;
142 memcpy(W, sha_info->data, sizeof(sha_info->data));
143 longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
145 for (i = 16; i < 80; ++i) {
146 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
148 for (i = 0; i < 8; ++i) {
149 S[i] = sha_info->digest[i];
152 /* Compress */
153 #define RND(a,b,c,d,e,f,g,h,i,ki) \
154 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
155 t1 = Sigma0(a) + Maj(a, b, c); \
156 d += t0; \
157 h = t0 + t1;
159 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98d728ae22ULL);
160 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x7137449123ef65cdULL);
161 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcfec4d3b2fULL);
162 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba58189dbbcULL);
163 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25bf348b538ULL);
164 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1b605d019ULL);
165 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4af194f9bULL);
166 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5da6d8118ULL);
167 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98a3030242ULL);
168 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b0145706fbeULL);
169 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be4ee4b28cULL);
170 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3d5ffb4e2ULL);
171 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74f27b896fULL);
172 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe3b1696b1ULL);
173 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a725c71235ULL);
174 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174cf692694ULL);
175 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c19ef14ad2ULL);
176 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786384f25e3ULL);
177 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc68b8cd5b5ULL);
178 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc77ac9c65ULL);
179 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f592b0275ULL);
180 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa6ea6e483ULL);
181 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dcbd41fbd4ULL);
182 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da831153b5ULL);
183 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152ee66dfabULL);
184 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d2db43210ULL);
185 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c898fb213fULL);
186 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7beef0ee4ULL);
187 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf33da88fc2ULL);
188 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147930aa725ULL);
189 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351e003826fULL);
190 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x142929670a0e6e70ULL);
191 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a8546d22ffcULL);
192 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b21385c26c926ULL);
193 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc5ac42aedULL);
194 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d139d95b3dfULL);
195 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a73548baf63deULL);
196 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb3c77b2a8ULL);
197 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e47edaee6ULL);
198 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c851482353bULL);
199 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a14cf10364ULL);
200 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664bbc423001ULL);
201 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70d0f89791ULL);
202 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a30654be30ULL);
203 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819d6ef5218ULL);
204 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd69906245565a910ULL);
205 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e35855771202aULL);
206 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa07032bbd1b8ULL);
207 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116b8d2d0c8ULL);
208 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c085141ab53ULL);
209 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774cdf8eeb99ULL);
210 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5e19b48a8ULL);
211 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3c5c95a63ULL);
212 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4ae3418acbULL);
213 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f7763e373ULL);
214 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3d6b2b8a3ULL);
215 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee5defb2fcULL);
216 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f43172f60ULL);
217 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814a1f0ab72ULL);
218 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc702081a6439ecULL);
219 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa23631e28ULL);
220 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506cebde82bde9ULL);
221 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7b2c67915ULL);
222 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2e372532bULL);
223 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,0xca273eceea26619cULL);
224 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,0xd186b8c721c0c207ULL);
225 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,0xeada7dd6cde0eb1eULL);
226 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,0xf57d4f7fee6ed178ULL);
227 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,0x06f067aa72176fbaULL);
228 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,0x0a637dc5a2c898a6ULL);
229 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,0x113f9804bef90daeULL);
230 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,0x1b710b35131c471bULL);
231 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,0x28db77f523047d84ULL);
232 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,0x32caab7b40c72493ULL);
233 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,0x3c9ebe0a15c9bebcULL);
234 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,0x431d67c49c100d4cULL);
235 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,0x4cc5d4becb3e42b6ULL);
236 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,0x597f299cfc657e2aULL);
237 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,0x5fcb6fab3ad6faecULL);
238 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,0x6c44198c4a475817ULL);
240 #undef RND
242 /* feedback */
243 for (i = 0; i < 8; i++) {
244 sha_info->digest[i] = sha_info->digest[i] + S[i];
251 /* initialize the SHA digest */
253 static void
254 sha512_init(SHAobject *sha_info)
256 TestEndianness(sha_info->Endianness)
257 sha_info->digest[0] = 0x6a09e667f3bcc908ULL;
258 sha_info->digest[1] = 0xbb67ae8584caa73bULL;
259 sha_info->digest[2] = 0x3c6ef372fe94f82bULL;
260 sha_info->digest[3] = 0xa54ff53a5f1d36f1ULL;
261 sha_info->digest[4] = 0x510e527fade682d1ULL;
262 sha_info->digest[5] = 0x9b05688c2b3e6c1fULL;
263 sha_info->digest[6] = 0x1f83d9abfb41bd6bULL;
264 sha_info->digest[7] = 0x5be0cd19137e2179ULL;
265 sha_info->count_lo = 0L;
266 sha_info->count_hi = 0L;
267 sha_info->local = 0;
268 sha_info->digestsize = 64;
271 static void
272 sha384_init(SHAobject *sha_info)
274 TestEndianness(sha_info->Endianness)
275 sha_info->digest[0] = 0xcbbb9d5dc1059ed8ULL;
276 sha_info->digest[1] = 0x629a292a367cd507ULL;
277 sha_info->digest[2] = 0x9159015a3070dd17ULL;
278 sha_info->digest[3] = 0x152fecd8f70e5939ULL;
279 sha_info->digest[4] = 0x67332667ffc00b31ULL;
280 sha_info->digest[5] = 0x8eb44a8768581511ULL;
281 sha_info->digest[6] = 0xdb0c2e0d64f98fa7ULL;
282 sha_info->digest[7] = 0x47b5481dbefa4fa4ULL;
283 sha_info->count_lo = 0L;
284 sha_info->count_hi = 0L;
285 sha_info->local = 0;
286 sha_info->digestsize = 48;
290 /* update the SHA digest */
292 static void
293 sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, int count)
295 int i;
296 SHA_INT32 clo;
298 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
299 if (clo < sha_info->count_lo) {
300 ++sha_info->count_hi;
302 sha_info->count_lo = clo;
303 sha_info->count_hi += (SHA_INT32) count >> 29;
304 if (sha_info->local) {
305 i = SHA_BLOCKSIZE - sha_info->local;
306 if (i > count) {
307 i = count;
309 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
310 count -= i;
311 buffer += i;
312 sha_info->local += i;
313 if (sha_info->local == SHA_BLOCKSIZE) {
314 sha512_transform(sha_info);
316 else {
317 return;
320 while (count >= SHA_BLOCKSIZE) {
321 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
322 buffer += SHA_BLOCKSIZE;
323 count -= SHA_BLOCKSIZE;
324 sha512_transform(sha_info);
326 memcpy(sha_info->data, buffer, count);
327 sha_info->local = count;
330 /* finish computing the SHA digest */
332 static void
333 sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
335 int count;
336 SHA_INT32 lo_bit_count, hi_bit_count;
338 lo_bit_count = sha_info->count_lo;
339 hi_bit_count = sha_info->count_hi;
340 count = (int) ((lo_bit_count >> 3) & 0x7f);
341 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
342 if (count > SHA_BLOCKSIZE - 16) {
343 memset(((SHA_BYTE *) sha_info->data) + count, 0,
344 SHA_BLOCKSIZE - count);
345 sha512_transform(sha_info);
346 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
348 else {
349 memset(((SHA_BYTE *) sha_info->data) + count, 0,
350 SHA_BLOCKSIZE - 16 - count);
353 /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
354 swap these values into host-order. */
355 sha_info->data[112] = 0;
356 sha_info->data[113] = 0;
357 sha_info->data[114] = 0;
358 sha_info->data[115] = 0;
359 sha_info->data[116] = 0;
360 sha_info->data[117] = 0;
361 sha_info->data[118] = 0;
362 sha_info->data[119] = 0;
363 sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
364 sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
365 sha_info->data[122] = (hi_bit_count >> 8) & 0xff;
366 sha_info->data[123] = (hi_bit_count >> 0) & 0xff;
367 sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
368 sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
369 sha_info->data[126] = (lo_bit_count >> 8) & 0xff;
370 sha_info->data[127] = (lo_bit_count >> 0) & 0xff;
371 sha512_transform(sha_info);
372 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
373 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
374 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
375 digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
376 digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
377 digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
378 digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
379 digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
380 digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
381 digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
382 digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
383 digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
384 digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
385 digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
386 digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
387 digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
388 digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
389 digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
390 digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
391 digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
392 digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
393 digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
394 digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
395 digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
396 digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
397 digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
398 digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
399 digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
400 digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
401 digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
402 digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
403 digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
404 digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
405 digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
406 digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
407 digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
408 digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
409 digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
410 digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
411 digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
412 digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
413 digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
414 digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
415 digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
416 digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
417 digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
418 digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
419 digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
420 digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
421 digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
422 digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
423 digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
424 digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
425 digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
426 digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
427 digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
428 digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
429 digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
430 digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
431 digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
432 digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
433 digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
434 digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
435 digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
439 * End of copied SHA code.
441 * ------------------------------------------------------------------------
444 static PyTypeObject SHA384type;
445 static PyTypeObject SHA512type;
448 static SHAobject *
449 newSHA384object(void)
451 return (SHAobject *)PyObject_New(SHAobject, &SHA384type);
454 static SHAobject *
455 newSHA512object(void)
457 return (SHAobject *)PyObject_New(SHAobject, &SHA512type);
460 /* Internal methods for a hash object */
462 static void
463 SHA512_dealloc(PyObject *ptr)
465 PyObject_Del(ptr);
469 /* External methods for a hash object */
471 PyDoc_STRVAR(SHA512_copy__doc__, "Return a copy of the hash object.");
473 static PyObject *
474 SHA512_copy(SHAobject *self, PyObject *args)
476 SHAobject *newobj;
478 if (!PyArg_ParseTuple(args, ":copy")) {
479 return NULL;
482 if (((PyObject*)self)->ob_type == &SHA512type) {
483 if ( (newobj = newSHA512object())==NULL)
484 return NULL;
485 } else {
486 if ( (newobj = newSHA384object())==NULL)
487 return NULL;
490 SHAcopy(self, newobj);
491 return (PyObject *)newobj;
494 PyDoc_STRVAR(SHA512_digest__doc__,
495 "Return the digest value as a string of binary data.");
497 static PyObject *
498 SHA512_digest(SHAobject *self, PyObject *args)
500 unsigned char digest[SHA_DIGESTSIZE];
501 SHAobject temp;
503 if (!PyArg_ParseTuple(args, ":digest"))
504 return NULL;
506 SHAcopy(self, &temp);
507 sha512_final(digest, &temp);
508 return PyString_FromStringAndSize((const char *)digest, self->digestsize);
511 PyDoc_STRVAR(SHA512_hexdigest__doc__,
512 "Return the digest value as a string of hexadecimal digits.");
514 static PyObject *
515 SHA512_hexdigest(SHAobject *self, PyObject *args)
517 unsigned char digest[SHA_DIGESTSIZE];
518 SHAobject temp;
519 PyObject *retval;
520 char *hex_digest;
521 int i, j;
523 if (!PyArg_ParseTuple(args, ":hexdigest"))
524 return NULL;
526 /* Get the raw (binary) digest value */
527 SHAcopy(self, &temp);
528 sha512_final(digest, &temp);
530 /* Create a new string */
531 retval = PyString_FromStringAndSize(NULL, self->digestsize * 2);
532 if (!retval)
533 return NULL;
534 hex_digest = PyString_AsString(retval);
535 if (!hex_digest) {
536 Py_DECREF(retval);
537 return NULL;
540 /* Make hex version of the digest */
541 for(i=j=0; i<self->digestsize; i++) {
542 char c;
543 c = (digest[i] >> 4) & 0xf;
544 c = (c>9) ? c+'a'-10 : c + '0';
545 hex_digest[j++] = c;
546 c = (digest[i] & 0xf);
547 c = (c>9) ? c+'a'-10 : c + '0';
548 hex_digest[j++] = c;
550 return retval;
553 PyDoc_STRVAR(SHA512_update__doc__,
554 "Update this hash object's state with the provided string.");
556 static PyObject *
557 SHA512_update(SHAobject *self, PyObject *args)
559 unsigned char *cp;
560 int len;
562 if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
563 return NULL;
565 sha512_update(self, cp, len);
567 Py_INCREF(Py_None);
568 return Py_None;
571 static PyMethodDef SHA_methods[] = {
572 {"copy", (PyCFunction)SHA512_copy, METH_VARARGS, SHA512_copy__doc__},
573 {"digest", (PyCFunction)SHA512_digest, METH_VARARGS, SHA512_digest__doc__},
574 {"hexdigest", (PyCFunction)SHA512_hexdigest, METH_VARARGS, SHA512_hexdigest__doc__},
575 {"update", (PyCFunction)SHA512_update, METH_VARARGS, SHA512_update__doc__},
576 {NULL, NULL} /* sentinel */
579 static PyObject *
580 SHA512_get_block_size(PyObject *self, void *closure)
582 return PyInt_FromLong(SHA_BLOCKSIZE);
585 static PyObject *
586 SHA512_get_name(PyObject *self, void *closure)
588 if (((SHAobject *)self)->digestsize == 64)
589 return PyString_FromStringAndSize("SHA512", 6);
590 else
591 return PyString_FromStringAndSize("SHA384", 6);
594 static PyGetSetDef SHA_getseters[] = {
595 {"block_size",
596 (getter)SHA512_get_block_size, NULL,
597 NULL,
598 NULL},
599 {"name",
600 (getter)SHA512_get_name, NULL,
601 NULL,
602 NULL},
603 {NULL} /* Sentinel */
606 static PyMemberDef SHA_members[] = {
607 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
608 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
609 * the old sha module also supported 'digestsize'. ugh. */
610 {"digestsize", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
611 {NULL} /* Sentinel */
614 static PyTypeObject SHA384type = {
615 PyObject_HEAD_INIT(NULL)
616 0, /*ob_size*/
617 "_sha512.sha384", /*tp_name*/
618 sizeof(SHAobject), /*tp_size*/
619 0, /*tp_itemsize*/
620 /* methods */
621 SHA512_dealloc, /*tp_dealloc*/
622 0, /*tp_print*/
623 0, /*tp_getattr*/
624 0, /*tp_setattr*/
625 0, /*tp_compare*/
626 0, /*tp_repr*/
627 0, /*tp_as_number*/
628 0, /*tp_as_sequence*/
629 0, /*tp_as_mapping*/
630 0, /*tp_hash*/
631 0, /*tp_call*/
632 0, /*tp_str*/
633 0, /*tp_getattro*/
634 0, /*tp_setattro*/
635 0, /*tp_as_buffer*/
636 Py_TPFLAGS_DEFAULT, /*tp_flags*/
637 0, /*tp_doc*/
638 0, /*tp_traverse*/
639 0, /*tp_clear*/
640 0, /*tp_richcompare*/
641 0, /*tp_weaklistoffset*/
642 0, /*tp_iter*/
643 0, /*tp_iternext*/
644 SHA_methods, /* tp_methods */
645 SHA_members, /* tp_members */
646 SHA_getseters, /* tp_getset */
649 static PyTypeObject SHA512type = {
650 PyObject_HEAD_INIT(NULL)
651 0, /*ob_size*/
652 "_sha512.sha512", /*tp_name*/
653 sizeof(SHAobject), /*tp_size*/
654 0, /*tp_itemsize*/
655 /* methods */
656 SHA512_dealloc, /*tp_dealloc*/
657 0, /*tp_print*/
658 0, /*tp_getattr*/
659 0, /*tp_setattr*/
660 0, /*tp_compare*/
661 0, /*tp_repr*/
662 0, /*tp_as_number*/
663 0, /*tp_as_sequence*/
664 0, /*tp_as_mapping*/
665 0, /*tp_hash*/
666 0, /*tp_call*/
667 0, /*tp_str*/
668 0, /*tp_getattro*/
669 0, /*tp_setattro*/
670 0, /*tp_as_buffer*/
671 Py_TPFLAGS_DEFAULT, /*tp_flags*/
672 0, /*tp_doc*/
673 0, /*tp_traverse*/
674 0, /*tp_clear*/
675 0, /*tp_richcompare*/
676 0, /*tp_weaklistoffset*/
677 0, /*tp_iter*/
678 0, /*tp_iternext*/
679 SHA_methods, /* tp_methods */
680 SHA_members, /* tp_members */
681 SHA_getseters, /* tp_getset */
685 /* The single module-level function: new() */
687 PyDoc_STRVAR(SHA512_new__doc__,
688 "Return a new SHA-512 hash object; optionally initialized with a string.");
690 static PyObject *
691 SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict)
693 static const char *kwlist[] = {"string", NULL};
694 SHAobject *new;
695 unsigned char *cp = NULL;
696 int len;
698 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist,
699 &cp, &len)) {
700 return NULL;
703 if ((new = newSHA512object()) == NULL)
704 return NULL;
706 sha512_init(new);
708 if (PyErr_Occurred()) {
709 Py_DECREF(new);
710 return NULL;
712 if (cp)
713 sha512_update(new, cp, len);
715 return (PyObject *)new;
718 PyDoc_STRVAR(SHA384_new__doc__,
719 "Return a new SHA-384 hash object; optionally initialized with a string.");
721 static PyObject *
722 SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict)
724 static const char *kwlist[] = {"string", NULL};
725 SHAobject *new;
726 unsigned char *cp = NULL;
727 int len;
729 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist,
730 &cp, &len)) {
731 return NULL;
734 if ((new = newSHA384object()) == NULL)
735 return NULL;
737 sha384_init(new);
739 if (PyErr_Occurred()) {
740 Py_DECREF(new);
741 return NULL;
743 if (cp)
744 sha512_update(new, cp, len);
746 return (PyObject *)new;
750 /* List of functions exported by this module */
752 static struct PyMethodDef SHA_functions[] = {
753 {"sha512", (PyCFunction)SHA512_new, METH_VARARGS|METH_KEYWORDS, SHA512_new__doc__},
754 {"sha384", (PyCFunction)SHA384_new, METH_VARARGS|METH_KEYWORDS, SHA384_new__doc__},
755 {NULL, NULL} /* Sentinel */
759 /* Initialize this module. */
761 #define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
763 PyMODINIT_FUNC
764 init_sha512(void)
766 PyObject *m;
768 SHA384type.ob_type = &PyType_Type;
769 if (PyType_Ready(&SHA384type) < 0)
770 return;
771 SHA512type.ob_type = &PyType_Type;
772 if (PyType_Ready(&SHA512type) < 0)
773 return;
774 m = Py_InitModule("_sha512", SHA_functions);
775 if (m == NULL)
776 return;
779 #endif