blob: 4533c003d599d95806b204a783525f1f35e43cd8 [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
Martin v. Löwis501b13c2014-07-27 14:20:23 +020023/*[clinic input]
24module _sha512
25class SHA512Type "SHAobject *" "&PyType_Type"
26[clinic start generated code]*/
27/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/
28
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000029#ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
30
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000031/* Some useful types */
32
33typedef unsigned char SHA_BYTE;
34
35#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036typedef unsigned int SHA_INT32; /* 32-bit integer */
37typedef unsigned PY_LONG_LONG SHA_INT64; /* 64-bit integer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000038#else
39/* not defined. compilation will die. */
40#endif
41
42/* The SHA block size and message digest sizes, in bytes */
43
44#define SHA_BLOCKSIZE 128
45#define SHA_DIGESTSIZE 64
46
47/* The structure for storing SHA info */
48
49typedef struct {
50 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 SHA_INT64 digest[8]; /* Message digest */
52 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
53 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 int local; /* unprocessed amount in data */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000055 int digestsize;
56} SHAobject;
57
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030058#include "clinic/sha512module.c.h"
59
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000060/* When run on a little-endian CPU we need to perform byte reversal on an
61 array of longwords. */
62
Christian Heimes743e0cd2012-10-17 23:52:17 +020063#if PY_LITTLE_ENDIAN
64static void longReverse(SHA_INT64 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000065{
66 SHA_INT64 value;
67
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000068 byteCount /= sizeof(*buffer);
69 while (byteCount--) {
70 value = *buffer;
71
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
73 ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
74 ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
75 ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
76 ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
77 ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
78 ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
79 ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
80
81 buffer++;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000082 }
83}
Christian Heimes743e0cd2012-10-17 23:52:17 +020084#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000085
86static void SHAcopy(SHAobject *src, SHAobject *dest)
87{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000088 dest->local = src->local;
89 dest->digestsize = src->digestsize;
90 dest->count_lo = src->count_lo;
91 dest->count_hi = src->count_hi;
92 memcpy(dest->digest, src->digest, sizeof(src->digest));
93 memcpy(dest->data, src->data, sizeof(src->data));
94}
95
96
97/* ------------------------------------------------------------------------
98 *
99 * This code for the SHA-512 algorithm was noted as public domain. The
100 * original headers are pasted below.
101 *
102 * Several changes have been made to make it more compatible with the
103 * Python environment and desired interface.
104 *
105 */
106
107/* LibTomCrypt, modular cryptographic library -- Tom St Denis
108 *
109 * LibTomCrypt is a library that provides various cryptographic
110 * algorithms in a highly modular and flexible manner.
111 *
112 * The library is free for all purposes without any express
113 * gurantee it works.
114 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000115 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000116 */
117
118
119/* SHA512 by Tom St Denis */
120
121/* Various logical functions */
122#define ROR64(x, y) \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123 ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \
124 ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000125#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000127#define S(x, n) ROR64((x),(n))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000128#define R(x, n) (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000129#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
130#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
131#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
132#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
133
134
135static void
136sha512_transform(SHAobject *sha_info)
137{
138 int i;
139 SHA_INT64 S[8], W[80], t0, t1;
140
141 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200142#if PY_LITTLE_ENDIAN
143 longReverse(W, (int)sizeof(sha_info->data));
144#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000145
146 for (i = 16; i < 80; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000148 }
149 for (i = 0; i < 8; ++i) {
150 S[i] = sha_info->digest[i];
151 }
152
153 /* Compress */
154#define RND(a,b,c,d,e,f,g,h,i,ki) \
155 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
156 t1 = Sigma0(a) + Maj(a, b, c); \
157 d += t0; \
158 h = t0 + t1;
159
Thomas Wouters477c8d52006-05-27 19:21:47 +0000160 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22));
161 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd));
162 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f));
163 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc));
164 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538));
165 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019));
166 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b));
167 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118));
168 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242));
169 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe));
170 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c));
171 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2));
172 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f));
173 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1));
174 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235));
175 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694));
176 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2));
177 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3));
178 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5));
179 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65));
180 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275));
181 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483));
182 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4));
183 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5));
184 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab));
185 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210));
186 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f));
187 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4));
188 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2));
189 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725));
190 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f));
191 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70));
192 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc));
193 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926));
194 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed));
195 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df));
196 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de));
197 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8));
198 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6));
199 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b));
200 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364));
201 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001));
202 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791));
203 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30));
204 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218));
205 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910));
206 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a));
207 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8));
208 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8));
209 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53));
210 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99));
211 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8));
212 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63));
213 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb));
214 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373));
215 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3));
216 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc));
217 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60));
218 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72));
219 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec));
220 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28));
221 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9));
222 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915));
223 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b));
224 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c));
225 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207));
226 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e));
227 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178));
228 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba));
229 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6));
230 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae));
231 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b));
232 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84));
233 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493));
234 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc));
235 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c));
236 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6));
237 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a));
238 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec));
239 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,Py_ULL(0x6c44198c4a475817));
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241#undef RND
242
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000243 /* feedback */
244 for (i = 0; i < 8; i++) {
245 sha_info->digest[i] = sha_info->digest[i] + S[i];
246 }
247
248}
249
250
251
252/* initialize the SHA digest */
253
254static void
255sha512_init(SHAobject *sha_info)
256{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000257 sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
258 sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
259 sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
260 sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1);
261 sha_info->digest[4] = Py_ULL(0x510e527fade682d1);
262 sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f);
263 sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b);
264 sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000265 sha_info->count_lo = 0L;
266 sha_info->count_hi = 0L;
267 sha_info->local = 0;
268 sha_info->digestsize = 64;
269}
270
271static void
272sha384_init(SHAobject *sha_info)
273{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000274 sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
275 sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
276 sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
277 sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
278 sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
279 sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
280 sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
281 sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000282 sha_info->count_lo = 0L;
283 sha_info->count_hi = 0L;
284 sha_info->local = 0;
285 sha_info->digestsize = 48;
286}
287
288
289/* update the SHA digest */
290
291static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000292sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000293{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000294 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000295 SHA_INT32 clo;
296
297 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
298 if (clo < sha_info->count_lo) {
299 ++sha_info->count_hi;
300 }
301 sha_info->count_lo = clo;
302 sha_info->count_hi += (SHA_INT32) count >> 29;
303 if (sha_info->local) {
304 i = SHA_BLOCKSIZE - sha_info->local;
305 if (i > count) {
306 i = count;
307 }
308 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
309 count -= i;
310 buffer += i;
Victor Stinner70792d22013-05-08 00:00:44 +0200311 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000312 if (sha_info->local == SHA_BLOCKSIZE) {
313 sha512_transform(sha_info);
314 }
315 else {
316 return;
317 }
318 }
319 while (count >= SHA_BLOCKSIZE) {
320 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
321 buffer += SHA_BLOCKSIZE;
322 count -= SHA_BLOCKSIZE;
323 sha512_transform(sha_info);
324 }
325 memcpy(sha_info->data, buffer, count);
Victor Stinner70792d22013-05-08 00:00:44 +0200326 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000327}
328
329/* finish computing the SHA digest */
330
331static void
332sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
333{
334 int count;
335 SHA_INT32 lo_bit_count, hi_bit_count;
336
337 lo_bit_count = sha_info->count_lo;
338 hi_bit_count = sha_info->count_hi;
339 count = (int) ((lo_bit_count >> 3) & 0x7f);
340 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
341 if (count > SHA_BLOCKSIZE - 16) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 memset(((SHA_BYTE *) sha_info->data) + count, 0,
343 SHA_BLOCKSIZE - count);
344 sha512_transform(sha_info);
345 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000346 }
347 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 memset(((SHA_BYTE *) sha_info->data) + count, 0,
349 SHA_BLOCKSIZE - 16 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000350 }
351
352 /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
353 swap these values into host-order. */
354 sha_info->data[112] = 0;
355 sha_info->data[113] = 0;
356 sha_info->data[114] = 0;
357 sha_info->data[115] = 0;
358 sha_info->data[116] = 0;
359 sha_info->data[117] = 0;
360 sha_info->data[118] = 0;
361 sha_info->data[119] = 0;
362 sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
363 sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
364 sha_info->data[122] = (hi_bit_count >> 8) & 0xff;
365 sha_info->data[123] = (hi_bit_count >> 0) & 0xff;
366 sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
367 sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
368 sha_info->data[126] = (lo_bit_count >> 8) & 0xff;
369 sha_info->data[127] = (lo_bit_count >> 0) & 0xff;
370 sha512_transform(sha_info);
371 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
372 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
373 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
374 digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
375 digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
376 digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
377 digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
378 digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
379 digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
380 digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
381 digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
382 digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
383 digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
384 digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
385 digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
386 digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
387 digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
388 digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
389 digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
390 digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
391 digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
392 digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
393 digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
394 digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
395 digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
396 digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
397 digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
398 digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
399 digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
400 digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
401 digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
402 digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
403 digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
404 digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
405 digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
406 digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
407 digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
408 digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
409 digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
410 digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
411 digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
412 digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
413 digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
414 digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
415 digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
416 digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
417 digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
418 digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
419 digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
420 digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
421 digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
422 digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
423 digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
424 digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
425 digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
426 digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
427 digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
428 digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
429 digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
430 digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
431 digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
432 digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
433 digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
434 digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
435}
436
437/*
438 * End of copied SHA code.
439 *
440 * ------------------------------------------------------------------------
441 */
442
443static PyTypeObject SHA384type;
444static PyTypeObject SHA512type;
445
446
447static SHAobject *
448newSHA384object(void)
449{
450 return (SHAobject *)PyObject_New(SHAobject, &SHA384type);
451}
452
453static SHAobject *
454newSHA512object(void)
455{
456 return (SHAobject *)PyObject_New(SHAobject, &SHA512type);
457}
458
459/* Internal methods for a hash object */
460
461static void
462SHA512_dealloc(PyObject *ptr)
463{
464 PyObject_Del(ptr);
465}
466
467
468/* External methods for a hash object */
469
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200470/*[clinic input]
471SHA512Type.copy
472
473Return a copy of the hash object.
474[clinic start generated code]*/
475
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200476static PyObject *
477SHA512Type_copy_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300478/*[clinic end generated code: output=adea896ed3164821 input=9f5f31e6c457776a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000479{
480 SHAobject *newobj;
481
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000482 if (((PyObject*)self)->ob_type == &SHA512type) {
483 if ( (newobj = newSHA512object())==NULL)
484 return NULL;
485 } else {
486 if ( (newobj = newSHA384object())==NULL)
487 return NULL;
488 }
489
490 SHAcopy(self, newobj);
491 return (PyObject *)newobj;
492}
493
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200494/*[clinic input]
495SHA512Type.digest
496
497Return the digest value as a string of binary data.
498[clinic start generated code]*/
499
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200500static PyObject *
501SHA512Type_digest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300502/*[clinic end generated code: output=1080bbeeef7dde1b input=60c2cede9e023018]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000503{
504 unsigned char digest[SHA_DIGESTSIZE];
505 SHAobject temp;
506
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000507 SHAcopy(self, &temp);
508 sha512_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000509 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000510}
511
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200512/*[clinic input]
513SHA512Type.hexdigest
514
515Return the digest value as a string of hexadecimal digits.
516[clinic start generated code]*/
517
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200518static PyObject *
519SHA512Type_hexdigest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300520/*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000521{
522 unsigned char digest[SHA_DIGESTSIZE];
523 SHAobject temp;
524 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200525 Py_UCS1 *hex_digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000526 int i, j;
527
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000528 /* Get the raw (binary) digest value */
529 SHAcopy(self, &temp);
530 sha512_final(digest, &temp);
531
532 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200533 retval = PyUnicode_New(self->digestsize * 2, 127);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000534 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200536 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000537
538 /* Make hex version of the digest */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000539 for (i=j=0; i<self->digestsize; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200540 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000541 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200542 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000543 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200544 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000545 }
Christian Heimesf402e922013-01-03 09:21:55 +0100546#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200547 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100548#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000549 return retval;
550}
551
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200552/*[clinic input]
553SHA512Type.update
554
555 obj: object
556 /
557
558Update this hash object's state with the provided string.
559[clinic start generated code]*/
560
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000561static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200562SHA512Type_update(SHAobject *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300563/*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000564{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000565 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000566
Gregory P. Smith365a1862009-02-12 07:35:29 +0000567 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000568
Gregory P. Smith365a1862009-02-12 07:35:29 +0000569 sha512_update(self, buf.buf, buf.len);
570
571 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000572 Py_INCREF(Py_None);
573 return Py_None;
574}
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200575/*[clinic input]
576dump buffer
577[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300578/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000579
580static PyMethodDef SHA_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200581 SHA512TYPE_COPY_METHODDEF
582 SHA512TYPE_DIGEST_METHODDEF
583 SHA512TYPE_HEXDIGEST_METHODDEF
584 SHA512TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000586};
587
588static PyObject *
589SHA512_get_block_size(PyObject *self, void *closure)
590{
Christian Heimes217cfd12007-12-02 14:31:20 +0000591 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000592}
593
594static PyObject *
595SHA512_get_name(PyObject *self, void *closure)
596{
597 if (((SHAobject *)self)->digestsize == 64)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200598 return PyUnicode_FromStringAndSize("sha512", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000599 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200600 return PyUnicode_FromStringAndSize("sha384", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000601}
602
603static PyGetSetDef SHA_getseters[] = {
604 {"block_size",
605 (getter)SHA512_get_block_size, NULL,
606 NULL,
607 NULL},
608 {"name",
609 (getter)SHA512_get_name, NULL,
610 NULL,
611 NULL},
612 {NULL} /* Sentinel */
613};
614
615static PyMemberDef SHA_members[] = {
616 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000617 {NULL} /* Sentinel */
618};
619
620static PyTypeObject SHA384type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000621 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 "_sha512.sha384", /*tp_name*/
623 sizeof(SHAobject), /*tp_size*/
624 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000625 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 SHA512_dealloc, /*tp_dealloc*/
627 0, /*tp_print*/
628 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000629 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000630 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000631 0, /*tp_repr*/
632 0, /*tp_as_number*/
633 0, /*tp_as_sequence*/
634 0, /*tp_as_mapping*/
635 0, /*tp_hash*/
636 0, /*tp_call*/
637 0, /*tp_str*/
638 0, /*tp_getattro*/
639 0, /*tp_setattro*/
640 0, /*tp_as_buffer*/
641 Py_TPFLAGS_DEFAULT, /*tp_flags*/
642 0, /*tp_doc*/
643 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 0, /*tp_clear*/
645 0, /*tp_richcompare*/
646 0, /*tp_weaklistoffset*/
647 0, /*tp_iter*/
648 0, /*tp_iternext*/
649 SHA_methods, /* tp_methods */
650 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000651 SHA_getseters, /* tp_getset */
652};
653
654static PyTypeObject SHA512type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000655 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 "_sha512.sha512", /*tp_name*/
657 sizeof(SHAobject), /*tp_size*/
658 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000659 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 SHA512_dealloc, /*tp_dealloc*/
661 0, /*tp_print*/
662 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000663 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000664 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000665 0, /*tp_repr*/
666 0, /*tp_as_number*/
667 0, /*tp_as_sequence*/
668 0, /*tp_as_mapping*/
669 0, /*tp_hash*/
670 0, /*tp_call*/
671 0, /*tp_str*/
672 0, /*tp_getattro*/
673 0, /*tp_setattro*/
674 0, /*tp_as_buffer*/
675 Py_TPFLAGS_DEFAULT, /*tp_flags*/
676 0, /*tp_doc*/
677 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 0, /*tp_clear*/
679 0, /*tp_richcompare*/
680 0, /*tp_weaklistoffset*/
681 0, /*tp_iter*/
682 0, /*tp_iternext*/
683 SHA_methods, /* tp_methods */
684 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000685 SHA_getseters, /* tp_getset */
686};
687
688
689/* The single module-level function: new() */
690
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200691/*[clinic input]
692_sha512.sha512
693
694 string: object(c_default="NULL") = b''
695
696Return a new SHA-512 hash object; optionally initialized with a string.
697[clinic start generated code]*/
698
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200699static PyObject *
700_sha512_sha512_impl(PyModuleDef *module, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300701/*[clinic end generated code: output=da13bc0a94da6de3 input=e69bad9ae9b6a308]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200702{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000703 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000704 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000705
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200706 if (string)
707 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000708
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000709 if ((new = newSHA512object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200710 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000711 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000712 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000713 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000714
715 sha512_init(new);
716
717 if (PyErr_Occurred()) {
718 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200719 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000720 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000721 return NULL;
722 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200723 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000724 sha512_update(new, buf.buf, buf.len);
725 PyBuffer_Release(&buf);
726 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000727
728 return (PyObject *)new;
729}
730
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200731/*[clinic input]
732_sha512.sha384
733
734 string: object(c_default="NULL") = b''
735
736Return a new SHA-384 hash object; optionally initialized with a string.
737[clinic start generated code]*/
738
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200739static PyObject *
740_sha512_sha384_impl(PyModuleDef *module, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300741/*[clinic end generated code: output=ac731aea5509174d input=c9327788d4ea4545]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200742{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000743 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000744 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000745
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200746 if (string)
747 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000748
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000749 if ((new = newSHA384object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200750 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000751 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000752 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000753 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000754
755 sha384_init(new);
756
757 if (PyErr_Occurred()) {
758 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200759 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000760 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000761 return NULL;
762 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200763 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000764 sha512_update(new, buf.buf, buf.len);
765 PyBuffer_Release(&buf);
766 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000767
768 return (PyObject *)new;
769}
770
771
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200772/*[clinic input]
773dump buffer
774[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300775/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200776
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000777/* List of functions exported by this module */
778
779static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200780 _SHA512_SHA512_METHODDEF
781 _SHA512_SHA384_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000783};
784
785
786/* Initialize this module. */
787
788#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
789
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000790
Martin v. Löwis1a214512008-06-11 05:26:20 +0000791static struct PyModuleDef _sha512module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyModuleDef_HEAD_INIT,
793 "_sha512",
794 NULL,
795 -1,
796 SHA_functions,
797 NULL,
798 NULL,
799 NULL,
800 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000801};
802
803PyMODINIT_FUNC
804PyInit__sha512(void)
805{
Christian Heimes327dd732013-10-22 15:05:23 +0200806 PyObject *m;
807
Christian Heimes90aa7642007-12-19 02:45:37 +0000808 Py_TYPE(&SHA384type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000809 if (PyType_Ready(&SHA384type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000810 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000811 Py_TYPE(&SHA512type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000812 if (PyType_Ready(&SHA512type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000813 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200814
815 m = PyModule_Create(&_sha512module);
816 if (m == NULL)
817 return NULL;
818
819 Py_INCREF((PyObject *)&SHA384type);
820 PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type);
821 Py_INCREF((PyObject *)&SHA512type);
822 PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type);
823 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000824}
825
826#endif