blob: 47c57e5c428f106f9e99cdf2bf0627bc4f3be995 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* 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
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#ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
24
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000025/* Some useful types */
26
27typedef unsigned char SHA_BYTE;
28
29#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030typedef unsigned int SHA_INT32; /* 32-bit integer */
31typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000032#else
33/* not defined. compilation will die. */
34#endif
35
36/* The SHA block size and message digest sizes, in bytes */
37
38#define SHA_BLOCKSIZE 128
39#define SHA_DIGESTSIZE 64
40
41/* The structure for storing SHA info */
42
43typedef struct {
44 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 SHA_INT64 digest[8]; /* Message digest */
46 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
47 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 int local; /* unprocessed amount in data */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000049 int digestsize;
50} SHAobject;
51
52/* When run on a little-endian CPU we need to perform byte reversal on an
53 array of longwords. */
54
Christian Heimes743e0cd2012-10-17 23:52:17 +020055#if PY_LITTLE_ENDIAN
56static void longReverse(SHA_INT64 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000057{
58 SHA_INT64 value;
59
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000060 byteCount /= sizeof(*buffer);
61 while (byteCount--) {
62 value = *buffer;
63
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
65 ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
66 ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
67 ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
68 ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
69 ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
70 ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
71 ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
72
73 buffer++;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000074 }
75}
Christian Heimes743e0cd2012-10-17 23:52:17 +020076#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000077
78static void SHAcopy(SHAobject *src, SHAobject *dest)
79{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000080 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-512 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/* SHA512 by Tom St Denis */
112
113/* Various logical functions */
114#define ROR64(x, y) \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000115 ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \
116 ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000117#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) ROR64((x),(n))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000120#define R(x, n) (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000121#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
122#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
123#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
124#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
125
126
127static void
128sha512_transform(SHAobject *sha_info)
129{
130 int i;
131 SHA_INT64 S[8], W[80], t0, t1;
132
133 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200134#if PY_LITTLE_ENDIAN
135 longReverse(W, (int)sizeof(sha_info->data));
136#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000137
138 for (i = 16; i < 80; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000140 }
141 for (i = 0; i < 8; ++i) {
142 S[i] = sha_info->digest[i];
143 }
144
145 /* Compress */
146#define RND(a,b,c,d,e,f,g,h,i,ki) \
147 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
148 t1 = Sigma0(a) + Maj(a, b, c); \
149 d += t0; \
150 h = t0 + t1;
151
Thomas Wouters477c8d52006-05-27 19:21:47 +0000152 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22));
153 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd));
154 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f));
155 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc));
156 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538));
157 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019));
158 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b));
159 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118));
160 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242));
161 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe));
162 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c));
163 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2));
164 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f));
165 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1));
166 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235));
167 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694));
168 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2));
169 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3));
170 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5));
171 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65));
172 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275));
173 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483));
174 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4));
175 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5));
176 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab));
177 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210));
178 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f));
179 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4));
180 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2));
181 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725));
182 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f));
183 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70));
184 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc));
185 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926));
186 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed));
187 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df));
188 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de));
189 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8));
190 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6));
191 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b));
192 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364));
193 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001));
194 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791));
195 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30));
196 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218));
197 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910));
198 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a));
199 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8));
200 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8));
201 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53));
202 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99));
203 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8));
204 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63));
205 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb));
206 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373));
207 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3));
208 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc));
209 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60));
210 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72));
211 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec));
212 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28));
213 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9));
214 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915));
215 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b));
216 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c));
217 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207));
218 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e));
219 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178));
220 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba));
221 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6));
222 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae));
223 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b));
224 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84));
225 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493));
226 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc));
227 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c));
228 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6));
229 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a));
230 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec));
231 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,Py_ULL(0x6c44198c4a475817));
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233#undef RND
234
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000235 /* feedback */
236 for (i = 0; i < 8; i++) {
237 sha_info->digest[i] = sha_info->digest[i] + S[i];
238 }
239
240}
241
242
243
244/* initialize the SHA digest */
245
246static void
247sha512_init(SHAobject *sha_info)
248{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249 sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
250 sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
251 sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
252 sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1);
253 sha_info->digest[4] = Py_ULL(0x510e527fade682d1);
254 sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f);
255 sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b);
256 sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000257 sha_info->count_lo = 0L;
258 sha_info->count_hi = 0L;
259 sha_info->local = 0;
260 sha_info->digestsize = 64;
261}
262
263static void
264sha384_init(SHAobject *sha_info)
265{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000266 sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
267 sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
268 sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
269 sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
270 sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
271 sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
272 sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
273 sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000274 sha_info->count_lo = 0L;
275 sha_info->count_hi = 0L;
276 sha_info->local = 0;
277 sha_info->digestsize = 48;
278}
279
280
281/* update the SHA digest */
282
283static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000284sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000285{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000286 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000287 SHA_INT32 clo;
288
289 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
290 if (clo < sha_info->count_lo) {
291 ++sha_info->count_hi;
292 }
293 sha_info->count_lo = clo;
294 sha_info->count_hi += (SHA_INT32) count >> 29;
295 if (sha_info->local) {
296 i = SHA_BLOCKSIZE - sha_info->local;
297 if (i > count) {
298 i = count;
299 }
300 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
301 count -= i;
302 buffer += i;
Victor Stinner70792d22013-05-08 00:00:44 +0200303 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000304 if (sha_info->local == SHA_BLOCKSIZE) {
305 sha512_transform(sha_info);
306 }
307 else {
308 return;
309 }
310 }
311 while (count >= SHA_BLOCKSIZE) {
312 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
313 buffer += SHA_BLOCKSIZE;
314 count -= SHA_BLOCKSIZE;
315 sha512_transform(sha_info);
316 }
317 memcpy(sha_info->data, buffer, count);
Victor Stinner70792d22013-05-08 00:00:44 +0200318 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000319}
320
321/* finish computing the SHA digest */
322
323static void
324sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
325{
326 int count;
327 SHA_INT32 lo_bit_count, hi_bit_count;
328
329 lo_bit_count = sha_info->count_lo;
330 hi_bit_count = sha_info->count_hi;
331 count = (int) ((lo_bit_count >> 3) & 0x7f);
332 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
333 if (count > SHA_BLOCKSIZE - 16) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 memset(((SHA_BYTE *) sha_info->data) + count, 0,
335 SHA_BLOCKSIZE - count);
336 sha512_transform(sha_info);
337 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000338 }
339 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 memset(((SHA_BYTE *) sha_info->data) + count, 0,
341 SHA_BLOCKSIZE - 16 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000342 }
343
344 /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
345 swap these values into host-order. */
346 sha_info->data[112] = 0;
347 sha_info->data[113] = 0;
348 sha_info->data[114] = 0;
349 sha_info->data[115] = 0;
350 sha_info->data[116] = 0;
351 sha_info->data[117] = 0;
352 sha_info->data[118] = 0;
353 sha_info->data[119] = 0;
354 sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
355 sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
356 sha_info->data[122] = (hi_bit_count >> 8) & 0xff;
357 sha_info->data[123] = (hi_bit_count >> 0) & 0xff;
358 sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
359 sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
360 sha_info->data[126] = (lo_bit_count >> 8) & 0xff;
361 sha_info->data[127] = (lo_bit_count >> 0) & 0xff;
362 sha512_transform(sha_info);
363 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
364 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
365 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
366 digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
367 digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
368 digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
369 digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
370 digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
371 digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
372 digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
373 digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
374 digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
375 digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
376 digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
377 digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
378 digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
379 digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
380 digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
381 digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
382 digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
383 digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
384 digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
385 digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
386 digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
387 digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
388 digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
389 digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
390 digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
391 digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
392 digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
393 digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
394 digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
395 digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
396 digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
397 digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
398 digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
399 digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
400 digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
401 digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
402 digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
403 digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
404 digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
405 digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
406 digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
407 digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
408 digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
409 digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
410 digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
411 digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
412 digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
413 digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
414 digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
415 digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
416 digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
417 digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
418 digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
419 digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
420 digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
421 digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
422 digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
423 digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
424 digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
425 digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
426 digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
427}
428
429/*
430 * End of copied SHA code.
431 *
432 * ------------------------------------------------------------------------
433 */
434
435static PyTypeObject SHA384type;
436static PyTypeObject SHA512type;
437
438
439static SHAobject *
440newSHA384object(void)
441{
442 return (SHAobject *)PyObject_New(SHAobject, &SHA384type);
443}
444
445static SHAobject *
446newSHA512object(void)
447{
448 return (SHAobject *)PyObject_New(SHAobject, &SHA512type);
449}
450
451/* Internal methods for a hash object */
452
453static void
454SHA512_dealloc(PyObject *ptr)
455{
456 PyObject_Del(ptr);
457}
458
459
460/* External methods for a hash object */
461
462PyDoc_STRVAR(SHA512_copy__doc__, "Return a copy of the hash object.");
463
464static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000465SHA512_copy(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000466{
467 SHAobject *newobj;
468
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000469 if (((PyObject*)self)->ob_type == &SHA512type) {
470 if ( (newobj = newSHA512object())==NULL)
471 return NULL;
472 } else {
473 if ( (newobj = newSHA384object())==NULL)
474 return NULL;
475 }
476
477 SHAcopy(self, newobj);
478 return (PyObject *)newobj;
479}
480
481PyDoc_STRVAR(SHA512_digest__doc__,
482"Return the digest value as a string of binary data.");
483
484static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000485SHA512_digest(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000486{
487 unsigned char digest[SHA_DIGESTSIZE];
488 SHAobject temp;
489
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000490 SHAcopy(self, &temp);
491 sha512_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000492 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000493}
494
495PyDoc_STRVAR(SHA512_hexdigest__doc__,
496"Return the digest value as a string of hexadecimal digits.");
497
498static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000499SHA512_hexdigest(SHAobject *self, PyObject *unused)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000500{
501 unsigned char digest[SHA_DIGESTSIZE];
502 SHAobject temp;
503 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200504 Py_UCS1 *hex_digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000505 int i, j;
506
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000507 /* Get the raw (binary) digest value */
508 SHAcopy(self, &temp);
509 sha512_final(digest, &temp);
510
511 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200512 retval = PyUnicode_New(self->digestsize * 2, 127);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000513 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200515 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000516
517 /* Make hex version of the digest */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000518 for (i=j=0; i<self->digestsize; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200519 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000520 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200521 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000522 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200523 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000524 }
Christian Heimesf402e922013-01-03 09:21:55 +0100525#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200526 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100527#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000528 return retval;
529}
530
531PyDoc_STRVAR(SHA512_update__doc__,
532"Update this hash object's state with the provided string.");
533
534static PyObject *
535SHA512_update(SHAobject *self, PyObject *args)
536{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000537 PyObject *obj;
538 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000539
Gregory P. Smith365a1862009-02-12 07:35:29 +0000540 if (!PyArg_ParseTuple(args, "O:update", &obj))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000541 return NULL;
542
Gregory P. Smith365a1862009-02-12 07:35:29 +0000543 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000544
Gregory P. Smith365a1862009-02-12 07:35:29 +0000545 sha512_update(self, buf.buf, buf.len);
546
547 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000548 Py_INCREF(Py_None);
549 return Py_None;
550}
551
552static PyMethodDef SHA_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 {"copy", (PyCFunction)SHA512_copy, METH_NOARGS, SHA512_copy__doc__},
554 {"digest", (PyCFunction)SHA512_digest, METH_NOARGS, SHA512_digest__doc__},
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000555 {"hexdigest", (PyCFunction)SHA512_hexdigest, METH_NOARGS, SHA512_hexdigest__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 {"update", (PyCFunction)SHA512_update, METH_VARARGS, SHA512_update__doc__},
557 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000558};
559
560static PyObject *
561SHA512_get_block_size(PyObject *self, void *closure)
562{
Christian Heimes217cfd12007-12-02 14:31:20 +0000563 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000564}
565
566static PyObject *
567SHA512_get_name(PyObject *self, void *closure)
568{
569 if (((SHAobject *)self)->digestsize == 64)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200570 return PyUnicode_FromStringAndSize("sha512", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000571 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200572 return PyUnicode_FromStringAndSize("sha384", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000573}
574
575static PyGetSetDef SHA_getseters[] = {
576 {"block_size",
577 (getter)SHA512_get_block_size, NULL,
578 NULL,
579 NULL},
580 {"name",
581 (getter)SHA512_get_name, NULL,
582 NULL,
583 NULL},
584 {NULL} /* Sentinel */
585};
586
587static PyMemberDef SHA_members[] = {
588 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000589 {NULL} /* Sentinel */
590};
591
592static PyTypeObject SHA384type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000593 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 "_sha512.sha384", /*tp_name*/
595 sizeof(SHAobject), /*tp_size*/
596 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000597 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 SHA512_dealloc, /*tp_dealloc*/
599 0, /*tp_print*/
600 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000601 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000602 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000603 0, /*tp_repr*/
604 0, /*tp_as_number*/
605 0, /*tp_as_sequence*/
606 0, /*tp_as_mapping*/
607 0, /*tp_hash*/
608 0, /*tp_call*/
609 0, /*tp_str*/
610 0, /*tp_getattro*/
611 0, /*tp_setattro*/
612 0, /*tp_as_buffer*/
613 Py_TPFLAGS_DEFAULT, /*tp_flags*/
614 0, /*tp_doc*/
615 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 0, /*tp_clear*/
617 0, /*tp_richcompare*/
618 0, /*tp_weaklistoffset*/
619 0, /*tp_iter*/
620 0, /*tp_iternext*/
621 SHA_methods, /* tp_methods */
622 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000623 SHA_getseters, /* tp_getset */
624};
625
626static PyTypeObject SHA512type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000627 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 "_sha512.sha512", /*tp_name*/
629 sizeof(SHAobject), /*tp_size*/
630 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000631 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 SHA512_dealloc, /*tp_dealloc*/
633 0, /*tp_print*/
634 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000635 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000636 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000637 0, /*tp_repr*/
638 0, /*tp_as_number*/
639 0, /*tp_as_sequence*/
640 0, /*tp_as_mapping*/
641 0, /*tp_hash*/
642 0, /*tp_call*/
643 0, /*tp_str*/
644 0, /*tp_getattro*/
645 0, /*tp_setattro*/
646 0, /*tp_as_buffer*/
647 Py_TPFLAGS_DEFAULT, /*tp_flags*/
648 0, /*tp_doc*/
649 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 0, /*tp_clear*/
651 0, /*tp_richcompare*/
652 0, /*tp_weaklistoffset*/
653 0, /*tp_iter*/
654 0, /*tp_iternext*/
655 SHA_methods, /* tp_methods */
656 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000657 SHA_getseters, /* tp_getset */
658};
659
660
661/* The single module-level function: new() */
662
663PyDoc_STRVAR(SHA512_new__doc__,
664"Return a new SHA-512 hash object; optionally initialized with a string.");
665
666static PyObject *
667SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict)
668{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000669 static char *kwlist[] = {"string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000670 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000671 PyObject *data_obj = NULL;
672 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000673
Gregory P. Smith365a1862009-02-12 07:35:29 +0000674 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
675 &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000676 return NULL;
677 }
678
Gregory P. Smith365a1862009-02-12 07:35:29 +0000679 if (data_obj)
680 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
681
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000682 if ((new = newSHA512object()) == NULL) {
683 if (data_obj)
684 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000685 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000686 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000687
688 sha512_init(new);
689
690 if (PyErr_Occurred()) {
691 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000692 if (data_obj)
693 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000694 return NULL;
695 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000696 if (data_obj) {
697 sha512_update(new, buf.buf, buf.len);
698 PyBuffer_Release(&buf);
699 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000700
701 return (PyObject *)new;
702}
703
704PyDoc_STRVAR(SHA384_new__doc__,
705"Return a new SHA-384 hash object; optionally initialized with a string.");
706
707static PyObject *
708SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict)
709{
Martin v. Löwis15e62742006-02-27 16:46:16 +0000710 static char *kwlist[] = {"string", NULL};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000711 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000712 PyObject *data_obj = NULL;
713 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000714
Gregory P. Smith365a1862009-02-12 07:35:29 +0000715 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
716 &data_obj)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000717 return NULL;
718 }
719
Gregory P. Smith365a1862009-02-12 07:35:29 +0000720 if (data_obj)
721 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
722
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000723 if ((new = newSHA384object()) == NULL) {
724 if (data_obj)
725 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000726 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000727 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000728
729 sha384_init(new);
730
731 if (PyErr_Occurred()) {
732 Py_DECREF(new);
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000733 if (data_obj)
734 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000735 return NULL;
736 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000737 if (data_obj) {
738 sha512_update(new, buf.buf, buf.len);
739 PyBuffer_Release(&buf);
740 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000741
742 return (PyObject *)new;
743}
744
745
746/* List of functions exported by this module */
747
748static struct PyMethodDef SHA_functions[] = {
749 {"sha512", (PyCFunction)SHA512_new, METH_VARARGS|METH_KEYWORDS, SHA512_new__doc__},
750 {"sha384", (PyCFunction)SHA384_new, METH_VARARGS|METH_KEYWORDS, SHA384_new__doc__},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000752};
753
754
755/* Initialize this module. */
756
757#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
758
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000759
Martin v. Löwis1a214512008-06-11 05:26:20 +0000760static struct PyModuleDef _sha512module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 PyModuleDef_HEAD_INIT,
762 "_sha512",
763 NULL,
764 -1,
765 SHA_functions,
766 NULL,
767 NULL,
768 NULL,
769 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000770};
771
772PyMODINIT_FUNC
773PyInit__sha512(void)
774{
Christian Heimes327dd732013-10-22 15:05:23 +0200775 PyObject *m;
776
Christian Heimes90aa7642007-12-19 02:45:37 +0000777 Py_TYPE(&SHA384type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000778 if (PyType_Ready(&SHA384type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000779 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000780 Py_TYPE(&SHA512type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000781 if (PyType_Ready(&SHA512type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000782 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200783
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;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000793}
794
795#endif