Merged revisions 81782 via svnmerge from
[python/dscho.git] / Modules / sha512module.c
blobc403ed6113e9f94da850786524fef856085b3aeb
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-2007 Gregory P. Smith (greg@krypto.org)
13 Licensed to PSF under a Contributor Agreement.
17 /* SHA objects */
19 #include "Python.h"
20 #include "structmember.h"
21 #include "hashlib.h"
23 #ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
25 /* Endianness testing and definitions */
26 #define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\
27 if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;}
29 #define PCT_LITTLE_ENDIAN 1
30 #define PCT_BIG_ENDIAN 0
32 /* Some useful types */
34 typedef unsigned char SHA_BYTE;
36 #if SIZEOF_INT == 4
37 typedef unsigned int SHA_INT32; /* 32-bit integer */
38 typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */
39 #else
40 /* not defined. compilation will die. */
41 #endif
43 /* The SHA block size and message digest sizes, in bytes */
45 #define SHA_BLOCKSIZE 128
46 #define SHA_DIGESTSIZE 64
48 /* The structure for storing SHA info */
50 typedef struct {
51 PyObject_HEAD
52 SHA_INT64 digest[8]; /* Message digest */
53 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
54 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
55 int Endianness;
56 int local; /* unprocessed amount in data */
57 int digestsize;
58 } SHAobject;
60 /* When run on a little-endian CPU we need to perform byte reversal on an
61 array of longwords. */
63 static void longReverse(SHA_INT64 *buffer, int byteCount, int Endianness)
65 SHA_INT64 value;
67 if ( Endianness == PCT_BIG_ENDIAN )
68 return;
70 byteCount /= sizeof(*buffer);
71 while (byteCount--) {
72 value = *buffer;
74 ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
75 ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
76 ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
77 ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
78 ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
79 ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
80 ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
81 ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
83 buffer++;
87 static void SHAcopy(SHAobject *src, SHAobject *dest)
89 dest->Endianness = src->Endianness;
90 dest->local = src->local;
91 dest->digestsize = src->digestsize;
92 dest->count_lo = src->count_lo;
93 dest->count_hi = src->count_hi;
94 memcpy(dest->digest, src->digest, sizeof(src->digest));
95 memcpy(dest->data, src->data, sizeof(src->data));
99 /* ------------------------------------------------------------------------
101 * This code for the SHA-512 algorithm was noted as public domain. The
102 * original headers are pasted below.
104 * Several changes have been made to make it more compatible with the
105 * Python environment and desired interface.
109 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
111 * LibTomCrypt is a library that provides various cryptographic
112 * algorithms in a highly modular and flexible manner.
114 * The library is free for all purposes without any express
115 * gurantee it works.
117 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
121 /* SHA512 by Tom St Denis */
123 /* Various logical functions */
124 #define ROR64(x, y) \
125 ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \
126 ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF))
127 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
128 #define Maj(x,y,z) (((x | y) & z) | (x & y))
129 #define S(x, n) ROR64((x),(n))
130 #define R(x, n) (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n))
131 #define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
132 #define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
133 #define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
134 #define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
137 static void
138 sha512_transform(SHAobject *sha_info)
140 int i;
141 SHA_INT64 S[8], W[80], t0, t1;
143 memcpy(W, sha_info->data, sizeof(sha_info->data));
144 longReverse(W, (int)sizeof(sha_info->data), sha_info->Endianness);
146 for (i = 16; i < 80; ++i) {
147 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
149 for (i = 0; i < 8; ++i) {
150 S[i] = sha_info->digest[i];
153 /* Compress */
154 #define RND(a,b,c,d,e,f,g,h,i,ki) \
155 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
156 t1 = Sigma0(a) + Maj(a, b, c); \
157 d += t0; \
158 h = t0 + t1;
160 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22));
161 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd));
162 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f));
163 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc));
164 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538));
165 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019));
166 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b));
167 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118));
168 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242));
169 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe));
170 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c));
171 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2));
172 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f));
173 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1));
174 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235));
175 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694));
176 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2));
177 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3));
178 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5));
179 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65));
180 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275));
181 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483));
182 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4));
183 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5));
184 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab));
185 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210));
186 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f));
187 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4));
188 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2));
189 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725));
190 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f));
191 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70));
192 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc));
193 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926));
194 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed));
195 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df));
196 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de));
197 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8));
198 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6));
199 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b));
200 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364));
201 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001));
202 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791));
203 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30));
204 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218));
205 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910));
206 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a));
207 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8));
208 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8));
209 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53));
210 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99));
211 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8));
212 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63));
213 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb));
214 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373));
215 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3));
216 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc));
217 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60));
218 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72));
219 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec));
220 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28));
221 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9));
222 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915));
223 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b));
224 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c));
225 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207));
226 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e));
227 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178));
228 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba));
229 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6));
230 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae));
231 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b));
232 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84));
233 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493));
234 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc));
235 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c));
236 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6));
237 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a));
238 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec));
239 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,Py_ULL(0x6c44198c4a475817));
241 #undef RND
243 /* feedback */
244 for (i = 0; i < 8; i++) {
245 sha_info->digest[i] = sha_info->digest[i] + S[i];
252 /* initialize the SHA digest */
254 static void
255 sha512_init(SHAobject *sha_info)
257 TestEndianness(sha_info->Endianness)
258 sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
259 sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
260 sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
261 sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1);
262 sha_info->digest[4] = Py_ULL(0x510e527fade682d1);
263 sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f);
264 sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b);
265 sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179);
266 sha_info->count_lo = 0L;
267 sha_info->count_hi = 0L;
268 sha_info->local = 0;
269 sha_info->digestsize = 64;
272 static void
273 sha384_init(SHAobject *sha_info)
275 TestEndianness(sha_info->Endianness)
276 sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
277 sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
278 sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
279 sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
280 sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
281 sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
282 sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
283 sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
284 sha_info->count_lo = 0L;
285 sha_info->count_hi = 0L;
286 sha_info->local = 0;
287 sha_info->digestsize = 48;
291 /* update the SHA digest */
293 static void
294 sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, int count)
296 int i;
297 SHA_INT32 clo;
299 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
300 if (clo < sha_info->count_lo) {
301 ++sha_info->count_hi;
303 sha_info->count_lo = clo;
304 sha_info->count_hi += (SHA_INT32) count >> 29;
305 if (sha_info->local) {
306 i = SHA_BLOCKSIZE - sha_info->local;
307 if (i > count) {
308 i = count;
310 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
311 count -= i;
312 buffer += i;
313 sha_info->local += i;
314 if (sha_info->local == SHA_BLOCKSIZE) {
315 sha512_transform(sha_info);
317 else {
318 return;
321 while (count >= SHA_BLOCKSIZE) {
322 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
323 buffer += SHA_BLOCKSIZE;
324 count -= SHA_BLOCKSIZE;
325 sha512_transform(sha_info);
327 memcpy(sha_info->data, buffer, count);
328 sha_info->local = count;
331 /* finish computing the SHA digest */
333 static void
334 sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
336 int count;
337 SHA_INT32 lo_bit_count, hi_bit_count;
339 lo_bit_count = sha_info->count_lo;
340 hi_bit_count = sha_info->count_hi;
341 count = (int) ((lo_bit_count >> 3) & 0x7f);
342 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
343 if (count > SHA_BLOCKSIZE - 16) {
344 memset(((SHA_BYTE *) sha_info->data) + count, 0,
345 SHA_BLOCKSIZE - count);
346 sha512_transform(sha_info);
347 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
349 else {
350 memset(((SHA_BYTE *) sha_info->data) + count, 0,
351 SHA_BLOCKSIZE - 16 - count);
354 /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
355 swap these values into host-order. */
356 sha_info->data[112] = 0;
357 sha_info->data[113] = 0;
358 sha_info->data[114] = 0;
359 sha_info->data[115] = 0;
360 sha_info->data[116] = 0;
361 sha_info->data[117] = 0;
362 sha_info->data[118] = 0;
363 sha_info->data[119] = 0;
364 sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
365 sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
366 sha_info->data[122] = (hi_bit_count >> 8) & 0xff;
367 sha_info->data[123] = (hi_bit_count >> 0) & 0xff;
368 sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
369 sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
370 sha_info->data[126] = (lo_bit_count >> 8) & 0xff;
371 sha_info->data[127] = (lo_bit_count >> 0) & 0xff;
372 sha512_transform(sha_info);
373 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
374 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
375 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
376 digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
377 digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
378 digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
379 digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
380 digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
381 digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
382 digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
383 digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
384 digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
385 digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
386 digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
387 digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
388 digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
389 digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
390 digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
391 digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
392 digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
393 digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
394 digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
395 digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
396 digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
397 digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
398 digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
399 digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
400 digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
401 digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
402 digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
403 digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
404 digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
405 digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
406 digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
407 digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
408 digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
409 digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
410 digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
411 digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
412 digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
413 digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
414 digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
415 digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
416 digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
417 digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
418 digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
419 digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
420 digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
421 digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
422 digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
423 digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
424 digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
425 digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
426 digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
427 digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
428 digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
429 digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
430 digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
431 digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
432 digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
433 digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
434 digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
435 digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
436 digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
440 * End of copied SHA code.
442 * ------------------------------------------------------------------------
445 static PyTypeObject SHA384type;
446 static PyTypeObject SHA512type;
449 static SHAobject *
450 newSHA384object(void)
452 return (SHAobject *)PyObject_New(SHAobject, &SHA384type);
455 static SHAobject *
456 newSHA512object(void)
458 return (SHAobject *)PyObject_New(SHAobject, &SHA512type);
461 /* Internal methods for a hash object */
463 static void
464 SHA512_dealloc(PyObject *ptr)
466 PyObject_Del(ptr);
470 /* External methods for a hash object */
472 PyDoc_STRVAR(SHA512_copy__doc__, "Return a copy of the hash object.");
474 static PyObject *
475 SHA512_copy(SHAobject *self, PyObject *unused)
477 SHAobject *newobj;
479 if (((PyObject*)self)->ob_type == &SHA512type) {
480 if ( (newobj = newSHA512object())==NULL)
481 return NULL;
482 } else {
483 if ( (newobj = newSHA384object())==NULL)
484 return NULL;
487 SHAcopy(self, newobj);
488 return (PyObject *)newobj;
491 PyDoc_STRVAR(SHA512_digest__doc__,
492 "Return the digest value as a string of binary data.");
494 static PyObject *
495 SHA512_digest(SHAobject *self, PyObject *unused)
497 unsigned char digest[SHA_DIGESTSIZE];
498 SHAobject temp;
500 SHAcopy(self, &temp);
501 sha512_final(digest, &temp);
502 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
505 PyDoc_STRVAR(SHA512_hexdigest__doc__,
506 "Return the digest value as a string of hexadecimal digits.");
508 static PyObject *
509 SHA512_hexdigest(SHAobject *self, PyObject *unused)
511 unsigned char digest[SHA_DIGESTSIZE];
512 SHAobject temp;
513 PyObject *retval;
514 Py_UNICODE *hex_digest;
515 int i, j;
517 /* Get the raw (binary) digest value */
518 SHAcopy(self, &temp);
519 sha512_final(digest, &temp);
521 /* Create a new string */
522 retval = PyUnicode_FromStringAndSize(NULL, self->digestsize * 2);
523 if (!retval)
524 return NULL;
525 hex_digest = PyUnicode_AS_UNICODE(retval);
526 if (!hex_digest) {
527 Py_DECREF(retval);
528 return NULL;
531 /* Make hex version of the digest */
532 for (i=j=0; i<self->digestsize; i++) {
533 char c;
534 c = (digest[i] >> 4) & 0xf;
535 c = (c>9) ? c+'a'-10 : c + '0';
536 hex_digest[j++] = c;
537 c = (digest[i] & 0xf);
538 c = (c>9) ? c+'a'-10 : c + '0';
539 hex_digest[j++] = c;
541 return retval;
544 PyDoc_STRVAR(SHA512_update__doc__,
545 "Update this hash object's state with the provided string.");
547 static PyObject *
548 SHA512_update(SHAobject *self, PyObject *args)
550 PyObject *obj;
551 Py_buffer buf;
553 if (!PyArg_ParseTuple(args, "O:update", &obj))
554 return NULL;
556 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
558 sha512_update(self, buf.buf, buf.len);
560 PyBuffer_Release(&buf);
561 Py_INCREF(Py_None);
562 return Py_None;
565 static PyMethodDef SHA_methods[] = {
566 {"copy", (PyCFunction)SHA512_copy, METH_NOARGS, SHA512_copy__doc__},
567 {"digest", (PyCFunction)SHA512_digest, METH_NOARGS, SHA512_digest__doc__},
568 {"hexdigest", (PyCFunction)SHA512_hexdigest, METH_NOARGS, SHA512_hexdigest__doc__},
569 {"update", (PyCFunction)SHA512_update, METH_VARARGS, SHA512_update__doc__},
570 {NULL, NULL} /* sentinel */
573 static PyObject *
574 SHA512_get_block_size(PyObject *self, void *closure)
576 return PyLong_FromLong(SHA_BLOCKSIZE);
579 static PyObject *
580 SHA512_get_name(PyObject *self, void *closure)
582 if (((SHAobject *)self)->digestsize == 64)
583 return PyUnicode_FromStringAndSize("SHA512", 6);
584 else
585 return PyUnicode_FromStringAndSize("SHA384", 6);
588 static PyGetSetDef SHA_getseters[] = {
589 {"block_size",
590 (getter)SHA512_get_block_size, NULL,
591 NULL,
592 NULL},
593 {"name",
594 (getter)SHA512_get_name, NULL,
595 NULL,
596 NULL},
597 {NULL} /* Sentinel */
600 static PyMemberDef SHA_members[] = {
601 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
602 {NULL} /* Sentinel */
605 static PyTypeObject SHA384type = {
606 PyVarObject_HEAD_INIT(NULL, 0)
607 "_sha512.sha384", /*tp_name*/
608 sizeof(SHAobject), /*tp_size*/
609 0, /*tp_itemsize*/
610 /* methods */
611 SHA512_dealloc, /*tp_dealloc*/
612 0, /*tp_print*/
613 0, /*tp_getattr*/
614 0, /*tp_setattr*/
615 0, /*tp_reserved*/
616 0, /*tp_repr*/
617 0, /*tp_as_number*/
618 0, /*tp_as_sequence*/
619 0, /*tp_as_mapping*/
620 0, /*tp_hash*/
621 0, /*tp_call*/
622 0, /*tp_str*/
623 0, /*tp_getattro*/
624 0, /*tp_setattro*/
625 0, /*tp_as_buffer*/
626 Py_TPFLAGS_DEFAULT, /*tp_flags*/
627 0, /*tp_doc*/
628 0, /*tp_traverse*/
629 0, /*tp_clear*/
630 0, /*tp_richcompare*/
631 0, /*tp_weaklistoffset*/
632 0, /*tp_iter*/
633 0, /*tp_iternext*/
634 SHA_methods, /* tp_methods */
635 SHA_members, /* tp_members */
636 SHA_getseters, /* tp_getset */
639 static PyTypeObject SHA512type = {
640 PyVarObject_HEAD_INIT(NULL, 0)
641 "_sha512.sha512", /*tp_name*/
642 sizeof(SHAobject), /*tp_size*/
643 0, /*tp_itemsize*/
644 /* methods */
645 SHA512_dealloc, /*tp_dealloc*/
646 0, /*tp_print*/
647 0, /*tp_getattr*/
648 0, /*tp_setattr*/
649 0, /*tp_reserved*/
650 0, /*tp_repr*/
651 0, /*tp_as_number*/
652 0, /*tp_as_sequence*/
653 0, /*tp_as_mapping*/
654 0, /*tp_hash*/
655 0, /*tp_call*/
656 0, /*tp_str*/
657 0, /*tp_getattro*/
658 0, /*tp_setattro*/
659 0, /*tp_as_buffer*/
660 Py_TPFLAGS_DEFAULT, /*tp_flags*/
661 0, /*tp_doc*/
662 0, /*tp_traverse*/
663 0, /*tp_clear*/
664 0, /*tp_richcompare*/
665 0, /*tp_weaklistoffset*/
666 0, /*tp_iter*/
667 0, /*tp_iternext*/
668 SHA_methods, /* tp_methods */
669 SHA_members, /* tp_members */
670 SHA_getseters, /* tp_getset */
674 /* The single module-level function: new() */
676 PyDoc_STRVAR(SHA512_new__doc__,
677 "Return a new SHA-512 hash object; optionally initialized with a string.");
679 static PyObject *
680 SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict)
682 static char *kwlist[] = {"string", NULL};
683 SHAobject *new;
684 PyObject *data_obj = NULL;
685 Py_buffer buf;
687 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
688 &data_obj)) {
689 return NULL;
692 if (data_obj)
693 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
695 if ((new = newSHA512object()) == NULL) {
696 if (data_obj)
697 PyBuffer_Release(&buf);
698 return NULL;
701 sha512_init(new);
703 if (PyErr_Occurred()) {
704 Py_DECREF(new);
705 if (data_obj)
706 PyBuffer_Release(&buf);
707 return NULL;
709 if (data_obj) {
710 sha512_update(new, buf.buf, buf.len);
711 PyBuffer_Release(&buf);
714 return (PyObject *)new;
717 PyDoc_STRVAR(SHA384_new__doc__,
718 "Return a new SHA-384 hash object; optionally initialized with a string.");
720 static PyObject *
721 SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict)
723 static char *kwlist[] = {"string", NULL};
724 SHAobject *new;
725 PyObject *data_obj = NULL;
726 Py_buffer buf;
728 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
729 &data_obj)) {
730 return NULL;
733 if (data_obj)
734 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
736 if ((new = newSHA384object()) == NULL) {
737 if (data_obj)
738 PyBuffer_Release(&buf);
739 return NULL;
742 sha384_init(new);
744 if (PyErr_Occurred()) {
745 Py_DECREF(new);
746 if (data_obj)
747 PyBuffer_Release(&buf);
748 return NULL;
750 if (data_obj) {
751 sha512_update(new, buf.buf, buf.len);
752 PyBuffer_Release(&buf);
755 return (PyObject *)new;
759 /* List of functions exported by this module */
761 static struct PyMethodDef SHA_functions[] = {
762 {"sha512", (PyCFunction)SHA512_new, METH_VARARGS|METH_KEYWORDS, SHA512_new__doc__},
763 {"sha384", (PyCFunction)SHA384_new, METH_VARARGS|METH_KEYWORDS, SHA384_new__doc__},
764 {NULL, NULL} /* Sentinel */
768 /* Initialize this module. */
770 #define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
773 static struct PyModuleDef _sha512module = {
774 PyModuleDef_HEAD_INIT,
775 "_sha512",
776 NULL,
778 SHA_functions,
779 NULL,
780 NULL,
781 NULL,
782 NULL
785 PyMODINIT_FUNC
786 PyInit__sha512(void)
788 Py_TYPE(&SHA384type) = &PyType_Type;
789 if (PyType_Ready(&SHA384type) < 0)
790 return NULL;
791 Py_TYPE(&SHA512type) = &PyType_Type;
792 if (PyType_Ready(&SHA512type) < 0)
793 return NULL;
794 return PyModule_Create(&_sha512module);
797 #endif