• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SHA512 module */
2 
3 /* This module provides an interface to NIST's SHA-512 and SHA-384 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 
12    Copyright (C) 2005-2007   Gregory P. Smith (greg@krypto.org)
13    Licensed to PSF under a Contributor Agreement.
14 
15 */
16 
17 /* SHA objects */
18 
19 #include "Python.h"
20 #include "structmember.h"
21 #include "hashlib.h"
22 #include "pystrhex.h"
23 
24 /*[clinic input]
25 module _sha512
26 class SHA512Type "SHAobject *" "&PyType_Type"
27 [clinic start generated code]*/
28 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/
29 
30 /* Some useful types */
31 
32 typedef unsigned char SHA_BYTE;
33 
34 #if SIZEOF_INT == 4
35 typedef unsigned int SHA_INT32; /* 32-bit integer */
36 typedef unsigned long long SHA_INT64;        /* 64-bit integer */
37 #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   128
44 #define SHA_DIGESTSIZE  64
45 
46 /* The structure for storing SHA info */
47 
48 typedef struct {
49     PyObject_HEAD
50     SHA_INT64 digest[8];                /* Message digest */
51     SHA_INT32 count_lo, count_hi;       /* 64-bit bit count */
52     SHA_BYTE data[SHA_BLOCKSIZE];       /* SHA data buffer */
53     int local;                          /* unprocessed amount in data */
54     int digestsize;
55 } SHAobject;
56 
57 #include "clinic/sha512module.c.h"
58 
59 /* When run on a little-endian CPU we need to perform byte reversal on an
60    array of longwords. */
61 
62 #if PY_LITTLE_ENDIAN
longReverse(SHA_INT64 * buffer,int byteCount)63 static void longReverse(SHA_INT64 *buffer, int byteCount)
64 {
65     SHA_INT64 value;
66 
67     byteCount /= sizeof(*buffer);
68     while (byteCount--) {
69         value = *buffer;
70 
71                 ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
72                 ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
73                 ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
74                 ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
75                 ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
76                 ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
77                 ((unsigned char*)buffer)[6] = (unsigned char)(value >>  8) & 0xff;
78                 ((unsigned char*)buffer)[7] = (unsigned char)(value      ) & 0xff;
79 
80                 buffer++;
81     }
82 }
83 #endif
84 
SHAcopy(SHAobject * src,SHAobject * dest)85 static void SHAcopy(SHAobject *src, SHAobject *dest)
86 {
87     dest->local = src->local;
88     dest->digestsize = src->digestsize;
89     dest->count_lo = src->count_lo;
90     dest->count_hi = src->count_hi;
91     memcpy(dest->digest, src->digest, sizeof(src->digest));
92     memcpy(dest->data, src->data, sizeof(src->data));
93 }
94 
95 
96 /* ------------------------------------------------------------------------
97  *
98  * This code for the SHA-512 algorithm was noted as public domain. The
99  * original headers are pasted below.
100  *
101  * Several changes have been made to make it more compatible with the
102  * Python environment and desired interface.
103  *
104  */
105 
106 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
107  *
108  * LibTomCrypt is a library that provides various cryptographic
109  * algorithms in a highly modular and flexible manner.
110  *
111  * The library is free for all purposes without any express
112  * guarantee it works.
113  *
114  * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
115  */
116 
117 
118 /* SHA512 by Tom St Denis */
119 
120 /* Various logical functions */
121 #define ROR64(x, y) \
122     ( ((((x) & 0xFFFFFFFFFFFFFFFFULL)>>((unsigned long long)(y) & 63)) | \
123       ((x)<<((unsigned long long)(64-((y) & 63))))) & 0xFFFFFFFFFFFFFFFFULL)
124 #define Ch(x,y,z)       (z ^ (x & (y ^ z)))
125 #define Maj(x,y,z)      (((x | y) & z) | (x & y))
126 #define S(x, n)         ROR64((x),(n))
127 #define R(x, n)         (((x) & 0xFFFFFFFFFFFFFFFFULL) >> ((unsigned long long)n))
128 #define Sigma0(x)       (S(x, 28) ^ S(x, 34) ^ S(x, 39))
129 #define Sigma1(x)       (S(x, 14) ^ S(x, 18) ^ S(x, 41))
130 #define Gamma0(x)       (S(x, 1) ^ S(x, 8) ^ R(x, 7))
131 #define Gamma1(x)       (S(x, 19) ^ S(x, 61) ^ R(x, 6))
132 
133 
134 static void
sha512_transform(SHAobject * sha_info)135 sha512_transform(SHAobject *sha_info)
136 {
137     int i;
138     SHA_INT64 S[8], W[80], t0, t1;
139 
140     memcpy(W, sha_info->data, sizeof(sha_info->data));
141 #if PY_LITTLE_ENDIAN
142     longReverse(W, (int)sizeof(sha_info->data));
143 #endif
144 
145     for (i = 16; i < 80; ++i) {
146                 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
147     }
148     for (i = 0; i < 8; ++i) {
149         S[i] = sha_info->digest[i];
150     }
151 
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;
158 
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);
239 
240 #undef RND
241 
242     /* feedback */
243     for (i = 0; i < 8; i++) {
244         sha_info->digest[i] = sha_info->digest[i] + S[i];
245     }
246 
247 }
248 
249 
250 
251 /* initialize the SHA digest */
252 
253 static void
sha512_init(SHAobject * sha_info)254 sha512_init(SHAobject *sha_info)
255 {
256     sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
257     sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
258     sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
259     sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1);
260     sha_info->digest[4] = Py_ULL(0x510e527fade682d1);
261     sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f);
262     sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b);
263     sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179);
264     sha_info->count_lo = 0L;
265     sha_info->count_hi = 0L;
266     sha_info->local = 0;
267     sha_info->digestsize = 64;
268 }
269 
270 static void
sha384_init(SHAobject * sha_info)271 sha384_init(SHAobject *sha_info)
272 {
273     sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
274     sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
275     sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
276     sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
277     sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
278     sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
279     sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
280     sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
281     sha_info->count_lo = 0L;
282     sha_info->count_hi = 0L;
283     sha_info->local = 0;
284     sha_info->digestsize = 48;
285 }
286 
287 
288 /* update the SHA digest */
289 
290 static void
sha512_update(SHAobject * sha_info,SHA_BYTE * buffer,Py_ssize_t count)291 sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
292 {
293     Py_ssize_t i;
294     SHA_INT32 clo;
295 
296     clo = sha_info->count_lo + ((SHA_INT32) count << 3);
297     if (clo < sha_info->count_lo) {
298         ++sha_info->count_hi;
299     }
300     sha_info->count_lo = clo;
301     sha_info->count_hi += (SHA_INT32) count >> 29;
302     if (sha_info->local) {
303         i = SHA_BLOCKSIZE - sha_info->local;
304         if (i > count) {
305             i = count;
306         }
307         memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
308         count -= i;
309         buffer += i;
310         sha_info->local += (int)i;
311         if (sha_info->local == SHA_BLOCKSIZE) {
312             sha512_transform(sha_info);
313         }
314         else {
315             return;
316         }
317     }
318     while (count >= SHA_BLOCKSIZE) {
319         memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
320         buffer += SHA_BLOCKSIZE;
321         count -= SHA_BLOCKSIZE;
322         sha512_transform(sha_info);
323     }
324     memcpy(sha_info->data, buffer, count);
325     sha_info->local = (int)count;
326 }
327 
328 /* finish computing the SHA digest */
329 
330 static void
sha512_final(unsigned char digest[SHA_DIGESTSIZE],SHAobject * sha_info)331 sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
332 {
333     int count;
334     SHA_INT32 lo_bit_count, hi_bit_count;
335 
336     lo_bit_count = sha_info->count_lo;
337     hi_bit_count = sha_info->count_hi;
338     count = (int) ((lo_bit_count >> 3) & 0x7f);
339     ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
340     if (count > SHA_BLOCKSIZE - 16) {
341         memset(((SHA_BYTE *) sha_info->data) + count, 0,
342                SHA_BLOCKSIZE - count);
343         sha512_transform(sha_info);
344         memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
345     }
346     else {
347         memset(((SHA_BYTE *) sha_info->data) + count, 0,
348                SHA_BLOCKSIZE - 16 - count);
349     }
350 
351     /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
352        swap these values into host-order. */
353     sha_info->data[112] = 0;
354     sha_info->data[113] = 0;
355     sha_info->data[114] = 0;
356     sha_info->data[115] = 0;
357     sha_info->data[116] = 0;
358     sha_info->data[117] = 0;
359     sha_info->data[118] = 0;
360     sha_info->data[119] = 0;
361     sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
362     sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
363     sha_info->data[122] = (hi_bit_count >>  8) & 0xff;
364     sha_info->data[123] = (hi_bit_count >>  0) & 0xff;
365     sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
366     sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
367     sha_info->data[126] = (lo_bit_count >>  8) & 0xff;
368     sha_info->data[127] = (lo_bit_count >>  0) & 0xff;
369     sha512_transform(sha_info);
370     digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
371     digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
372     digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
373     digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
374     digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
375     digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
376     digest[ 6] = (unsigned char) ((sha_info->digest[0] >>  8) & 0xff);
377     digest[ 7] = (unsigned char) ((sha_info->digest[0]      ) & 0xff);
378     digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
379     digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
380     digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
381     digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
382     digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
383     digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
384     digest[14] = (unsigned char) ((sha_info->digest[1] >>  8) & 0xff);
385     digest[15] = (unsigned char) ((sha_info->digest[1]      ) & 0xff);
386     digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
387     digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
388     digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
389     digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
390     digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
391     digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
392     digest[22] = (unsigned char) ((sha_info->digest[2] >>  8) & 0xff);
393     digest[23] = (unsigned char) ((sha_info->digest[2]      ) & 0xff);
394     digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
395     digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
396     digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
397     digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
398     digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
399     digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
400     digest[30] = (unsigned char) ((sha_info->digest[3] >>  8) & 0xff);
401     digest[31] = (unsigned char) ((sha_info->digest[3]      ) & 0xff);
402     digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
403     digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
404     digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
405     digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
406     digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
407     digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
408     digest[38] = (unsigned char) ((sha_info->digest[4] >>  8) & 0xff);
409     digest[39] = (unsigned char) ((sha_info->digest[4]      ) & 0xff);
410     digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
411     digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
412     digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
413     digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
414     digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
415     digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
416     digest[46] = (unsigned char) ((sha_info->digest[5] >>  8) & 0xff);
417     digest[47] = (unsigned char) ((sha_info->digest[5]      ) & 0xff);
418     digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
419     digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
420     digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
421     digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
422     digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
423     digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
424     digest[54] = (unsigned char) ((sha_info->digest[6] >>  8) & 0xff);
425     digest[55] = (unsigned char) ((sha_info->digest[6]      ) & 0xff);
426     digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
427     digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
428     digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
429     digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
430     digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
431     digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
432     digest[62] = (unsigned char) ((sha_info->digest[7] >>  8) & 0xff);
433     digest[63] = (unsigned char) ((sha_info->digest[7]      ) & 0xff);
434 }
435 
436 /*
437  * End of copied SHA code.
438  *
439  * ------------------------------------------------------------------------
440  */
441 
442 static PyTypeObject SHA384type;
443 static PyTypeObject SHA512type;
444 
445 
446 static SHAobject *
newSHA384object(void)447 newSHA384object(void)
448 {
449     return (SHAobject *)PyObject_New(SHAobject, &SHA384type);
450 }
451 
452 static SHAobject *
newSHA512object(void)453 newSHA512object(void)
454 {
455     return (SHAobject *)PyObject_New(SHAobject, &SHA512type);
456 }
457 
458 /* Internal methods for a hash object */
459 
460 static void
SHA512_dealloc(PyObject * ptr)461 SHA512_dealloc(PyObject *ptr)
462 {
463     PyObject_Del(ptr);
464 }
465 
466 
467 /* External methods for a hash object */
468 
469 /*[clinic input]
470 SHA512Type.copy
471 
472 Return a copy of the hash object.
473 [clinic start generated code]*/
474 
475 static PyObject *
SHA512Type_copy_impl(SHAobject * self)476 SHA512Type_copy_impl(SHAobject *self)
477 /*[clinic end generated code: output=adea896ed3164821 input=9f5f31e6c457776a]*/
478 {
479     SHAobject *newobj;
480 
481     if (((PyObject*)self)->ob_type == &SHA512type) {
482         if ( (newobj = newSHA512object())==NULL)
483             return NULL;
484     } else {
485         if ( (newobj = newSHA384object())==NULL)
486             return NULL;
487     }
488 
489     SHAcopy(self, newobj);
490     return (PyObject *)newobj;
491 }
492 
493 /*[clinic input]
494 SHA512Type.digest
495 
496 Return the digest value as a bytes object.
497 [clinic start generated code]*/
498 
499 static PyObject *
SHA512Type_digest_impl(SHAobject * self)500 SHA512Type_digest_impl(SHAobject *self)
501 /*[clinic end generated code: output=1080bbeeef7dde1b input=f6470dd359071f4b]*/
502 {
503     unsigned char digest[SHA_DIGESTSIZE];
504     SHAobject temp;
505 
506     SHAcopy(self, &temp);
507     sha512_final(digest, &temp);
508     return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
509 }
510 
511 /*[clinic input]
512 SHA512Type.hexdigest
513 
514 Return the digest value as a string of hexadecimal digits.
515 [clinic start generated code]*/
516 
517 static PyObject *
SHA512Type_hexdigest_impl(SHAobject * self)518 SHA512Type_hexdigest_impl(SHAobject *self)
519 /*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/
520 {
521     unsigned char digest[SHA_DIGESTSIZE];
522     SHAobject temp;
523 
524     /* Get the raw (binary) digest value */
525     SHAcopy(self, &temp);
526     sha512_final(digest, &temp);
527 
528     return _Py_strhex((const char *)digest, self->digestsize);
529 }
530 
531 /*[clinic input]
532 SHA512Type.update
533 
534     obj: object
535     /
536 
537 Update this hash object's state with the provided string.
538 [clinic start generated code]*/
539 
540 static PyObject *
SHA512Type_update(SHAobject * self,PyObject * obj)541 SHA512Type_update(SHAobject *self, PyObject *obj)
542 /*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/
543 {
544     Py_buffer buf;
545 
546     GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
547 
548     sha512_update(self, buf.buf, buf.len);
549 
550     PyBuffer_Release(&buf);
551     Py_RETURN_NONE;
552 }
553 
554 static PyMethodDef SHA_methods[] = {
555     SHA512TYPE_COPY_METHODDEF
556     SHA512TYPE_DIGEST_METHODDEF
557     SHA512TYPE_HEXDIGEST_METHODDEF
558     SHA512TYPE_UPDATE_METHODDEF
559     {NULL,        NULL}         /* sentinel */
560 };
561 
562 static PyObject *
SHA512_get_block_size(PyObject * self,void * closure)563 SHA512_get_block_size(PyObject *self, void *closure)
564 {
565     return PyLong_FromLong(SHA_BLOCKSIZE);
566 }
567 
568 static PyObject *
SHA512_get_name(PyObject * self,void * closure)569 SHA512_get_name(PyObject *self, void *closure)
570 {
571     if (((SHAobject *)self)->digestsize == 64)
572         return PyUnicode_FromStringAndSize("sha512", 6);
573     else
574         return PyUnicode_FromStringAndSize("sha384", 6);
575 }
576 
577 static PyGetSetDef SHA_getseters[] = {
578     {"block_size",
579      (getter)SHA512_get_block_size, NULL,
580      NULL,
581      NULL},
582     {"name",
583      (getter)SHA512_get_name, NULL,
584      NULL,
585      NULL},
586     {NULL}  /* Sentinel */
587 };
588 
589 static PyMemberDef SHA_members[] = {
590     {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
591     {NULL}  /* Sentinel */
592 };
593 
594 static PyTypeObject SHA384type = {
595     PyVarObject_HEAD_INIT(NULL, 0)
596     "_sha512.sha384",   /*tp_name*/
597     sizeof(SHAobject),  /*tp_basicsize*/
598     0,                  /*tp_itemsize*/
599     /* methods */
600     SHA512_dealloc,     /*tp_dealloc*/
601     0,                  /*tp_print*/
602     0,                  /*tp_getattr*/
603     0,                  /*tp_setattr*/
604     0,                  /*tp_reserved*/
605     0,                  /*tp_repr*/
606     0,                  /*tp_as_number*/
607     0,                  /*tp_as_sequence*/
608     0,                  /*tp_as_mapping*/
609     0,                  /*tp_hash*/
610     0,                  /*tp_call*/
611     0,                  /*tp_str*/
612     0,                  /*tp_getattro*/
613     0,                  /*tp_setattro*/
614     0,                  /*tp_as_buffer*/
615     Py_TPFLAGS_DEFAULT, /*tp_flags*/
616     0,                  /*tp_doc*/
617     0,                  /*tp_traverse*/
618     0,                  /*tp_clear*/
619     0,                  /*tp_richcompare*/
620     0,                  /*tp_weaklistoffset*/
621     0,                  /*tp_iter*/
622     0,                  /*tp_iternext*/
623     SHA_methods,        /* tp_methods */
624     SHA_members,        /* tp_members */
625     SHA_getseters,      /* tp_getset */
626 };
627 
628 static PyTypeObject SHA512type = {
629     PyVarObject_HEAD_INIT(NULL, 0)
630     "_sha512.sha512",   /*tp_name*/
631     sizeof(SHAobject),  /*tp_basicsize*/
632     0,                  /*tp_itemsize*/
633     /* methods */
634     SHA512_dealloc,     /*tp_dealloc*/
635     0,                  /*tp_print*/
636     0,                  /*tp_getattr*/
637     0,                  /*tp_setattr*/
638     0,                  /*tp_reserved*/
639     0,                  /*tp_repr*/
640     0,                  /*tp_as_number*/
641     0,                  /*tp_as_sequence*/
642     0,                  /*tp_as_mapping*/
643     0,                  /*tp_hash*/
644     0,                  /*tp_call*/
645     0,                  /*tp_str*/
646     0,                  /*tp_getattro*/
647     0,                  /*tp_setattro*/
648     0,                  /*tp_as_buffer*/
649     Py_TPFLAGS_DEFAULT, /*tp_flags*/
650     0,                  /*tp_doc*/
651     0,                  /*tp_traverse*/
652     0,                  /*tp_clear*/
653     0,                  /*tp_richcompare*/
654     0,                  /*tp_weaklistoffset*/
655     0,                  /*tp_iter*/
656     0,                  /*tp_iternext*/
657     SHA_methods,        /* tp_methods */
658     SHA_members,        /* tp_members */
659     SHA_getseters,      /* tp_getset */
660 };
661 
662 
663 /* The single module-level function: new() */
664 
665 /*[clinic input]
666 _sha512.sha512
667 
668     string: object(c_default="NULL") = b''
669 
670 Return a new SHA-512 hash object; optionally initialized with a string.
671 [clinic start generated code]*/
672 
673 static PyObject *
_sha512_sha512_impl(PyObject * module,PyObject * string)674 _sha512_sha512_impl(PyObject *module, PyObject *string)
675 /*[clinic end generated code: output=8b865a2df73bd387 input=e69bad9ae9b6a308]*/
676 {
677     SHAobject *new;
678     Py_buffer buf;
679 
680     if (string)
681         GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
682 
683     if ((new = newSHA512object()) == NULL) {
684         if (string)
685             PyBuffer_Release(&buf);
686         return NULL;
687     }
688 
689     sha512_init(new);
690 
691     if (PyErr_Occurred()) {
692         Py_DECREF(new);
693         if (string)
694             PyBuffer_Release(&buf);
695         return NULL;
696     }
697     if (string) {
698         sha512_update(new, buf.buf, buf.len);
699         PyBuffer_Release(&buf);
700     }
701 
702     return (PyObject *)new;
703 }
704 
705 /*[clinic input]
706 _sha512.sha384
707 
708     string: object(c_default="NULL") = b''
709 
710 Return a new SHA-384 hash object; optionally initialized with a string.
711 [clinic start generated code]*/
712 
713 static PyObject *
_sha512_sha384_impl(PyObject * module,PyObject * string)714 _sha512_sha384_impl(PyObject *module, PyObject *string)
715 /*[clinic end generated code: output=ae4b2e26decf81e8 input=c9327788d4ea4545]*/
716 {
717     SHAobject *new;
718     Py_buffer buf;
719 
720     if (string)
721         GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
722 
723     if ((new = newSHA384object()) == NULL) {
724         if (string)
725             PyBuffer_Release(&buf);
726         return NULL;
727     }
728 
729     sha384_init(new);
730 
731     if (PyErr_Occurred()) {
732         Py_DECREF(new);
733         if (string)
734             PyBuffer_Release(&buf);
735         return NULL;
736     }
737     if (string) {
738         sha512_update(new, buf.buf, buf.len);
739         PyBuffer_Release(&buf);
740     }
741 
742     return (PyObject *)new;
743 }
744 
745 
746 /* List of functions exported by this module */
747 
748 static struct PyMethodDef SHA_functions[] = {
749     _SHA512_SHA512_METHODDEF
750     _SHA512_SHA384_METHODDEF
751     {NULL,      NULL}            /* Sentinel */
752 };
753 
754 
755 /* Initialize this module. */
756 
757 #define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
758 
759 
760 static struct PyModuleDef _sha512module = {
761         PyModuleDef_HEAD_INIT,
762         "_sha512",
763         NULL,
764         -1,
765         SHA_functions,
766         NULL,
767         NULL,
768         NULL,
769         NULL
770 };
771 
772 PyMODINIT_FUNC
PyInit__sha512(void)773 PyInit__sha512(void)
774 {
775     PyObject *m;
776 
777     Py_TYPE(&SHA384type) = &PyType_Type;
778     if (PyType_Ready(&SHA384type) < 0)
779         return NULL;
780     Py_TYPE(&SHA512type) = &PyType_Type;
781     if (PyType_Ready(&SHA512type) < 0)
782         return NULL;
783 
784     m = PyModule_Create(&_sha512module);
785     if (m == NULL)
786         return NULL;
787 
788     Py_INCREF((PyObject *)&SHA384type);
789     PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type);
790     Py_INCREF((PyObject *)&SHA512type);
791     PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type);
792     return m;
793 }
794