blob: 8237d867f4aa28b15ef23c106e0f922b49666b40 [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. Smith8cb65692015-04-25 23:22:26 +000022#include "pystrhex.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000023
Martin v. Löwis501b13c2014-07-27 14:20:23 +020024/*[clinic input]
25module _sha512
26class SHA512Type "SHAobject *" "&PyType_Type"
27[clinic start generated code]*/
28/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/
29
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000030#ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
31
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000032/* Some useful types */
33
34typedef unsigned char SHA_BYTE;
35
36#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037typedef unsigned int SHA_INT32; /* 32-bit integer */
38typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000039#else
40/* not defined. compilation will die. */
41#endif
42
43/* The SHA block size and message digest sizes, in bytes */
44
45#define SHA_BLOCKSIZE 128
46#define SHA_DIGESTSIZE 64
47
48/* The structure for storing SHA info */
49
50typedef struct {
51 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 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 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 int local; /* unprocessed amount in data */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000056 int digestsize;
57} SHAobject;
58
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030059#include "clinic/sha512module.c.h"
60
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000061/* When run on a little-endian CPU we need to perform byte reversal on an
62 array of longwords. */
63
Christian Heimes743e0cd2012-10-17 23:52:17 +020064#if PY_LITTLE_ENDIAN
65static void longReverse(SHA_INT64 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000066{
67 SHA_INT64 value;
68
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000069 byteCount /= sizeof(*buffer);
70 while (byteCount--) {
71 value = *buffer;
72
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
74 ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
75 ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
76 ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
77 ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
78 ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
79 ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
80 ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
81
82 buffer++;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000083 }
84}
Christian Heimes743e0cd2012-10-17 23:52:17 +020085#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000086
87static void SHAcopy(SHAobject *src, SHAobject *dest)
88{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000089 dest->local = src->local;
90 dest->digestsize = src->digestsize;
91 dest->count_lo = src->count_lo;
92 dest->count_hi = src->count_hi;
93 memcpy(dest->digest, src->digest, sizeof(src->digest));
94 memcpy(dest->data, src->data, sizeof(src->data));
95}
96
97
98/* ------------------------------------------------------------------------
99 *
100 * This code for the SHA-512 algorithm was noted as public domain. The
101 * original headers are pasted below.
102 *
103 * Several changes have been made to make it more compatible with the
104 * Python environment and desired interface.
105 *
106 */
107
108/* LibTomCrypt, modular cryptographic library -- Tom St Denis
109 *
110 * LibTomCrypt is a library that provides various cryptographic
111 * algorithms in a highly modular and flexible manner.
112 *
113 * The library is free for all purposes without any express
114 * gurantee it works.
115 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000116 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000117 */
118
119
120/* SHA512 by Tom St Denis */
121
122/* Various logical functions */
123#define ROR64(x, y) \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \
125 ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000126#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000128#define S(x, n) ROR64((x),(n))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000129#define R(x, n) (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000130#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
131#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
132#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
133#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
134
135
136static void
137sha512_transform(SHAobject *sha_info)
138{
139 int i;
140 SHA_INT64 S[8], W[80], t0, t1;
141
142 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200143#if PY_LITTLE_ENDIAN
144 longReverse(W, (int)sizeof(sha_info->data));
145#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000146
147 for (i = 16; i < 80; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000149 }
150 for (i = 0; i < 8; ++i) {
151 S[i] = sha_info->digest[i];
152 }
153
154 /* Compress */
155#define RND(a,b,c,d,e,f,g,h,i,ki) \
156 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
157 t1 = Sigma0(a) + Maj(a, b, c); \
158 d += t0; \
159 h = t0 + t1;
160
Thomas Wouters477c8d52006-05-27 19:21:47 +0000161 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22));
162 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd));
163 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f));
164 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc));
165 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538));
166 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019));
167 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b));
168 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118));
169 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242));
170 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe));
171 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c));
172 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2));
173 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f));
174 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1));
175 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235));
176 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694));
177 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2));
178 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3));
179 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5));
180 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65));
181 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275));
182 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483));
183 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4));
184 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5));
185 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab));
186 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210));
187 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f));
188 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4));
189 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2));
190 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725));
191 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f));
192 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70));
193 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc));
194 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926));
195 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed));
196 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df));
197 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de));
198 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8));
199 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6));
200 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b));
201 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364));
202 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001));
203 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791));
204 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30));
205 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218));
206 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910));
207 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a));
208 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8));
209 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8));
210 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53));
211 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99));
212 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8));
213 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63));
214 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb));
215 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373));
216 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3));
217 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc));
218 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60));
219 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72));
220 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec));
221 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28));
222 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9));
223 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915));
224 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b));
225 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c));
226 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207));
227 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e));
228 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178));
229 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba));
230 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6));
231 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae));
232 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b));
233 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84));
234 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493));
235 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc));
236 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c));
237 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6));
238 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a));
239 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec));
240 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 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242#undef RND
243
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000244 /* feedback */
245 for (i = 0; i < 8; i++) {
246 sha_info->digest[i] = sha_info->digest[i] + S[i];
247 }
248
249}
250
251
252
253/* initialize the SHA digest */
254
255static void
256sha512_init(SHAobject *sha_info)
257{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000258 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);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000266 sha_info->count_lo = 0L;
267 sha_info->count_hi = 0L;
268 sha_info->local = 0;
269 sha_info->digestsize = 64;
270}
271
272static void
273sha384_init(SHAobject *sha_info)
274{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000275 sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
276 sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
277 sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
278 sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
279 sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
280 sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
281 sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
282 sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000283 sha_info->count_lo = 0L;
284 sha_info->count_hi = 0L;
285 sha_info->local = 0;
286 sha_info->digestsize = 48;
287}
288
289
290/* update the SHA digest */
291
292static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000293sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000294{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000295 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000296 SHA_INT32 clo;
297
298 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
299 if (clo < sha_info->count_lo) {
300 ++sha_info->count_hi;
301 }
302 sha_info->count_lo = clo;
303 sha_info->count_hi += (SHA_INT32) count >> 29;
304 if (sha_info->local) {
305 i = SHA_BLOCKSIZE - sha_info->local;
306 if (i > count) {
307 i = count;
308 }
309 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
310 count -= i;
311 buffer += i;
Victor Stinner70792d22013-05-08 00:00:44 +0200312 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000313 if (sha_info->local == SHA_BLOCKSIZE) {
314 sha512_transform(sha_info);
315 }
316 else {
317 return;
318 }
319 }
320 while (count >= SHA_BLOCKSIZE) {
321 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
322 buffer += SHA_BLOCKSIZE;
323 count -= SHA_BLOCKSIZE;
324 sha512_transform(sha_info);
325 }
326 memcpy(sha_info->data, buffer, count);
Victor Stinner70792d22013-05-08 00:00:44 +0200327 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000328}
329
330/* finish computing the SHA digest */
331
332static void
333sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
334{
335 int count;
336 SHA_INT32 lo_bit_count, hi_bit_count;
337
338 lo_bit_count = sha_info->count_lo;
339 hi_bit_count = sha_info->count_hi;
340 count = (int) ((lo_bit_count >> 3) & 0x7f);
341 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
342 if (count > SHA_BLOCKSIZE - 16) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 memset(((SHA_BYTE *) sha_info->data) + count, 0,
344 SHA_BLOCKSIZE - count);
345 sha512_transform(sha_info);
346 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000347 }
348 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 memset(((SHA_BYTE *) sha_info->data) + count, 0,
350 SHA_BLOCKSIZE - 16 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000351 }
352
353 /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
354 swap these values into host-order. */
355 sha_info->data[112] = 0;
356 sha_info->data[113] = 0;
357 sha_info->data[114] = 0;
358 sha_info->data[115] = 0;
359 sha_info->data[116] = 0;
360 sha_info->data[117] = 0;
361 sha_info->data[118] = 0;
362 sha_info->data[119] = 0;
363 sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
364 sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
365 sha_info->data[122] = (hi_bit_count >> 8) & 0xff;
366 sha_info->data[123] = (hi_bit_count >> 0) & 0xff;
367 sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
368 sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
369 sha_info->data[126] = (lo_bit_count >> 8) & 0xff;
370 sha_info->data[127] = (lo_bit_count >> 0) & 0xff;
371 sha512_transform(sha_info);
372 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
373 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
374 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
375 digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
376 digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
377 digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
378 digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
379 digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
380 digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
381 digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
382 digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
383 digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
384 digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
385 digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
386 digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
387 digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
388 digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
389 digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
390 digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
391 digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
392 digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
393 digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
394 digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
395 digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
396 digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
397 digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
398 digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
399 digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
400 digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
401 digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
402 digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
403 digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
404 digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
405 digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
406 digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
407 digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
408 digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
409 digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
410 digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
411 digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
412 digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
413 digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
414 digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
415 digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
416 digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
417 digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
418 digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
419 digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
420 digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
421 digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
422 digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
423 digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
424 digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
425 digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
426 digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
427 digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
428 digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
429 digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
430 digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
431 digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
432 digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
433 digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
434 digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
435 digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
436}
437
438/*
439 * End of copied SHA code.
440 *
441 * ------------------------------------------------------------------------
442 */
443
444static PyTypeObject SHA384type;
445static PyTypeObject SHA512type;
446
447
448static SHAobject *
449newSHA384object(void)
450{
451 return (SHAobject *)PyObject_New(SHAobject, &SHA384type);
452}
453
454static SHAobject *
455newSHA512object(void)
456{
457 return (SHAobject *)PyObject_New(SHAobject, &SHA512type);
458}
459
460/* Internal methods for a hash object */
461
462static void
463SHA512_dealloc(PyObject *ptr)
464{
465 PyObject_Del(ptr);
466}
467
468
469/* External methods for a hash object */
470
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200471/*[clinic input]
472SHA512Type.copy
473
474Return a copy of the hash object.
475[clinic start generated code]*/
476
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200477static PyObject *
478SHA512Type_copy_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300479/*[clinic end generated code: output=adea896ed3164821 input=9f5f31e6c457776a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000480{
481 SHAobject *newobj;
482
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000483 if (((PyObject*)self)->ob_type == &SHA512type) {
484 if ( (newobj = newSHA512object())==NULL)
485 return NULL;
486 } else {
487 if ( (newobj = newSHA384object())==NULL)
488 return NULL;
489 }
490
491 SHAcopy(self, newobj);
492 return (PyObject *)newobj;
493}
494
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200495/*[clinic input]
496SHA512Type.digest
497
498Return the digest value as a string of binary data.
499[clinic start generated code]*/
500
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200501static PyObject *
502SHA512Type_digest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300503/*[clinic end generated code: output=1080bbeeef7dde1b input=60c2cede9e023018]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000504{
505 unsigned char digest[SHA_DIGESTSIZE];
506 SHAobject temp;
507
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000508 SHAcopy(self, &temp);
509 sha512_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000510 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000511}
512
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200513/*[clinic input]
514SHA512Type.hexdigest
515
516Return the digest value as a string of hexadecimal digits.
517[clinic start generated code]*/
518
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200519static PyObject *
520SHA512Type_hexdigest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300521/*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000522{
523 unsigned char digest[SHA_DIGESTSIZE];
524 SHAobject temp;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000525
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000526 /* Get the raw (binary) digest value */
527 SHAcopy(self, &temp);
528 sha512_final(digest, &temp);
529
Gregory P. Smith8cb65692015-04-25 23:22:26 +0000530 return _Py_strhex((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000531}
532
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200533/*[clinic input]
534SHA512Type.update
535
536 obj: object
537 /
538
539Update this hash object's state with the provided string.
540[clinic start generated code]*/
541
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000542static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200543SHA512Type_update(SHAobject *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300544/*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000545{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000546 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000547
Gregory P. Smith365a1862009-02-12 07:35:29 +0000548 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000549
Gregory P. Smith365a1862009-02-12 07:35:29 +0000550 sha512_update(self, buf.buf, buf.len);
551
552 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000553 Py_INCREF(Py_None);
554 return Py_None;
555}
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200556/*[clinic input]
557dump buffer
558[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300559/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000560
561static PyMethodDef SHA_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200562 SHA512TYPE_COPY_METHODDEF
563 SHA512TYPE_DIGEST_METHODDEF
564 SHA512TYPE_HEXDIGEST_METHODDEF
565 SHA512TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000567};
568
569static PyObject *
570SHA512_get_block_size(PyObject *self, void *closure)
571{
Christian Heimes217cfd12007-12-02 14:31:20 +0000572 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000573}
574
575static PyObject *
576SHA512_get_name(PyObject *self, void *closure)
577{
578 if (((SHAobject *)self)->digestsize == 64)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200579 return PyUnicode_FromStringAndSize("sha512", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000580 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200581 return PyUnicode_FromStringAndSize("sha384", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000582}
583
584static PyGetSetDef SHA_getseters[] = {
585 {"block_size",
586 (getter)SHA512_get_block_size, NULL,
587 NULL,
588 NULL},
589 {"name",
590 (getter)SHA512_get_name, NULL,
591 NULL,
592 NULL},
593 {NULL} /* Sentinel */
594};
595
596static PyMemberDef SHA_members[] = {
597 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000598 {NULL} /* Sentinel */
599};
600
601static PyTypeObject SHA384type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000602 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 "_sha512.sha384", /*tp_name*/
604 sizeof(SHAobject), /*tp_size*/
605 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000606 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 SHA512_dealloc, /*tp_dealloc*/
608 0, /*tp_print*/
609 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000610 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000611 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000612 0, /*tp_repr*/
613 0, /*tp_as_number*/
614 0, /*tp_as_sequence*/
615 0, /*tp_as_mapping*/
616 0, /*tp_hash*/
617 0, /*tp_call*/
618 0, /*tp_str*/
619 0, /*tp_getattro*/
620 0, /*tp_setattro*/
621 0, /*tp_as_buffer*/
622 Py_TPFLAGS_DEFAULT, /*tp_flags*/
623 0, /*tp_doc*/
624 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 0, /*tp_clear*/
626 0, /*tp_richcompare*/
627 0, /*tp_weaklistoffset*/
628 0, /*tp_iter*/
629 0, /*tp_iternext*/
630 SHA_methods, /* tp_methods */
631 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000632 SHA_getseters, /* tp_getset */
633};
634
635static PyTypeObject SHA512type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000636 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 "_sha512.sha512", /*tp_name*/
638 sizeof(SHAobject), /*tp_size*/
639 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000640 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 SHA512_dealloc, /*tp_dealloc*/
642 0, /*tp_print*/
643 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000644 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000645 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000646 0, /*tp_repr*/
647 0, /*tp_as_number*/
648 0, /*tp_as_sequence*/
649 0, /*tp_as_mapping*/
650 0, /*tp_hash*/
651 0, /*tp_call*/
652 0, /*tp_str*/
653 0, /*tp_getattro*/
654 0, /*tp_setattro*/
655 0, /*tp_as_buffer*/
656 Py_TPFLAGS_DEFAULT, /*tp_flags*/
657 0, /*tp_doc*/
658 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 0, /*tp_clear*/
660 0, /*tp_richcompare*/
661 0, /*tp_weaklistoffset*/
662 0, /*tp_iter*/
663 0, /*tp_iternext*/
664 SHA_methods, /* tp_methods */
665 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000666 SHA_getseters, /* tp_getset */
667};
668
669
670/* The single module-level function: new() */
671
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200672/*[clinic input]
673_sha512.sha512
674
675 string: object(c_default="NULL") = b''
676
677Return a new SHA-512 hash object; optionally initialized with a string.
678[clinic start generated code]*/
679
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200680static PyObject *
681_sha512_sha512_impl(PyModuleDef *module, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300682/*[clinic end generated code: output=da13bc0a94da6de3 input=e69bad9ae9b6a308]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200683{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000684 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000685 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000686
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200687 if (string)
688 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000689
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000690 if ((new = newSHA512object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200691 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000692 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000693 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000694 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000695
696 sha512_init(new);
697
698 if (PyErr_Occurred()) {
699 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200700 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000701 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000702 return NULL;
703 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200704 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000705 sha512_update(new, buf.buf, buf.len);
706 PyBuffer_Release(&buf);
707 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000708
709 return (PyObject *)new;
710}
711
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200712/*[clinic input]
713_sha512.sha384
714
715 string: object(c_default="NULL") = b''
716
717Return a new SHA-384 hash object; optionally initialized with a string.
718[clinic start generated code]*/
719
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200720static PyObject *
721_sha512_sha384_impl(PyModuleDef *module, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300722/*[clinic end generated code: output=ac731aea5509174d input=c9327788d4ea4545]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200723{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000724 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000725 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000726
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200727 if (string)
728 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000729
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000730 if ((new = newSHA384object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200731 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000732 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000733 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000734 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000735
736 sha384_init(new);
737
738 if (PyErr_Occurred()) {
739 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200740 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000741 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000742 return NULL;
743 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200744 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000745 sha512_update(new, buf.buf, buf.len);
746 PyBuffer_Release(&buf);
747 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000748
749 return (PyObject *)new;
750}
751
752
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200753/*[clinic input]
754dump buffer
755[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300756/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200757
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000758/* List of functions exported by this module */
759
760static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200761 _SHA512_SHA512_METHODDEF
762 _SHA512_SHA384_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000764};
765
766
767/* Initialize this module. */
768
769#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
770
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000771
Martin v. Löwis1a214512008-06-11 05:26:20 +0000772static struct PyModuleDef _sha512module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 PyModuleDef_HEAD_INIT,
774 "_sha512",
775 NULL,
776 -1,
777 SHA_functions,
778 NULL,
779 NULL,
780 NULL,
781 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000782};
783
784PyMODINIT_FUNC
785PyInit__sha512(void)
786{
Christian Heimes327dd732013-10-22 15:05:23 +0200787 PyObject *m;
788
Christian Heimes90aa7642007-12-19 02:45:37 +0000789 Py_TYPE(&SHA384type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000790 if (PyType_Ready(&SHA384type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000791 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000792 Py_TYPE(&SHA512type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000793 if (PyType_Ready(&SHA512type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000794 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200795
796 m = PyModule_Create(&_sha512module);
797 if (m == NULL)
798 return NULL;
799
800 Py_INCREF((PyObject *)&SHA384type);
801 PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type);
802 Py_INCREF((PyObject *)&SHA512type);
803 PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type);
804 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000805}
806
807#endif