blob: 843f95d5c615a577a19c6c6331e1030b3c0006aa [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
58/* When run on a little-endian CPU we need to perform byte reversal on an
59 array of longwords. */
60
Christian Heimes743e0cd2012-10-17 23:52:17 +020061#if PY_LITTLE_ENDIAN
62static void longReverse(SHA_INT64 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000063{
64 SHA_INT64 value;
65
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000066 byteCount /= sizeof(*buffer);
67 while (byteCount--) {
68 value = *buffer;
69
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
71 ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
72 ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
73 ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
74 ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
75 ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
76 ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
77 ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
78
79 buffer++;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000080 }
81}
Christian Heimes743e0cd2012-10-17 23:52:17 +020082#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000083
84static void SHAcopy(SHAobject *src, SHAobject *dest)
85{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000086 dest->local = src->local;
87 dest->digestsize = src->digestsize;
88 dest->count_lo = src->count_lo;
89 dest->count_hi = src->count_hi;
90 memcpy(dest->digest, src->digest, sizeof(src->digest));
91 memcpy(dest->data, src->data, sizeof(src->data));
92}
93
94
95/* ------------------------------------------------------------------------
96 *
97 * This code for the SHA-512 algorithm was noted as public domain. The
98 * original headers are pasted below.
99 *
100 * Several changes have been made to make it more compatible with the
101 * Python environment and desired interface.
102 *
103 */
104
105/* LibTomCrypt, modular cryptographic library -- Tom St Denis
106 *
107 * LibTomCrypt is a library that provides various cryptographic
108 * algorithms in a highly modular and flexible manner.
109 *
110 * The library is free for all purposes without any express
111 * gurantee it works.
112 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000113 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000114 */
115
116
117/* SHA512 by Tom St Denis */
118
119/* Various logical functions */
120#define ROR64(x, y) \
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121 ( ((((x) & Py_ULL(0xFFFFFFFFFFFFFFFF))>>((unsigned PY_LONG_LONG)(y) & 63)) | \
122 ((x)<<((unsigned PY_LONG_LONG)(64-((y) & 63))))) & Py_ULL(0xFFFFFFFFFFFFFFFF))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000123#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000125#define S(x, n) ROR64((x),(n))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000126#define R(x, n) (((x) & Py_ULL(0xFFFFFFFFFFFFFFFF)) >> ((unsigned PY_LONG_LONG)n))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000127#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
128#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
129#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
130#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
131
132
133static void
134sha512_transform(SHAobject *sha_info)
135{
136 int i;
137 SHA_INT64 S[8], W[80], t0, t1;
138
139 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200140#if PY_LITTLE_ENDIAN
141 longReverse(W, (int)sizeof(sha_info->data));
142#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000143
144 for (i = 16; i < 80; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000146 }
147 for (i = 0; i < 8; ++i) {
148 S[i] = sha_info->digest[i];
149 }
150
151 /* Compress */
152#define RND(a,b,c,d,e,f,g,h,i,ki) \
153 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
154 t1 = Sigma0(a) + Maj(a, b, c); \
155 d += t0; \
156 h = t0 + t1;
157
Thomas Wouters477c8d52006-05-27 19:21:47 +0000158 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,Py_ULL(0x428a2f98d728ae22));
159 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,Py_ULL(0x7137449123ef65cd));
160 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,Py_ULL(0xb5c0fbcfec4d3b2f));
161 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,Py_ULL(0xe9b5dba58189dbbc));
162 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,Py_ULL(0x3956c25bf348b538));
163 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,Py_ULL(0x59f111f1b605d019));
164 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,Py_ULL(0x923f82a4af194f9b));
165 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,Py_ULL(0xab1c5ed5da6d8118));
166 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,Py_ULL(0xd807aa98a3030242));
167 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,Py_ULL(0x12835b0145706fbe));
168 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,Py_ULL(0x243185be4ee4b28c));
169 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,Py_ULL(0x550c7dc3d5ffb4e2));
170 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,Py_ULL(0x72be5d74f27b896f));
171 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,Py_ULL(0x80deb1fe3b1696b1));
172 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,Py_ULL(0x9bdc06a725c71235));
173 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,Py_ULL(0xc19bf174cf692694));
174 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,Py_ULL(0xe49b69c19ef14ad2));
175 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,Py_ULL(0xefbe4786384f25e3));
176 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,Py_ULL(0x0fc19dc68b8cd5b5));
177 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,Py_ULL(0x240ca1cc77ac9c65));
178 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,Py_ULL(0x2de92c6f592b0275));
179 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,Py_ULL(0x4a7484aa6ea6e483));
180 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,Py_ULL(0x5cb0a9dcbd41fbd4));
181 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,Py_ULL(0x76f988da831153b5));
182 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,Py_ULL(0x983e5152ee66dfab));
183 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,Py_ULL(0xa831c66d2db43210));
184 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,Py_ULL(0xb00327c898fb213f));
185 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,Py_ULL(0xbf597fc7beef0ee4));
186 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,Py_ULL(0xc6e00bf33da88fc2));
187 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,Py_ULL(0xd5a79147930aa725));
188 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,Py_ULL(0x06ca6351e003826f));
189 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,Py_ULL(0x142929670a0e6e70));
190 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,Py_ULL(0x27b70a8546d22ffc));
191 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,Py_ULL(0x2e1b21385c26c926));
192 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,Py_ULL(0x4d2c6dfc5ac42aed));
193 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,Py_ULL(0x53380d139d95b3df));
194 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,Py_ULL(0x650a73548baf63de));
195 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,Py_ULL(0x766a0abb3c77b2a8));
196 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,Py_ULL(0x81c2c92e47edaee6));
197 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,Py_ULL(0x92722c851482353b));
198 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,Py_ULL(0xa2bfe8a14cf10364));
199 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,Py_ULL(0xa81a664bbc423001));
200 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,Py_ULL(0xc24b8b70d0f89791));
201 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,Py_ULL(0xc76c51a30654be30));
202 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,Py_ULL(0xd192e819d6ef5218));
203 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,Py_ULL(0xd69906245565a910));
204 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,Py_ULL(0xf40e35855771202a));
205 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,Py_ULL(0x106aa07032bbd1b8));
206 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,Py_ULL(0x19a4c116b8d2d0c8));
207 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,Py_ULL(0x1e376c085141ab53));
208 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,Py_ULL(0x2748774cdf8eeb99));
209 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,Py_ULL(0x34b0bcb5e19b48a8));
210 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,Py_ULL(0x391c0cb3c5c95a63));
211 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,Py_ULL(0x4ed8aa4ae3418acb));
212 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,Py_ULL(0x5b9cca4f7763e373));
213 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,Py_ULL(0x682e6ff3d6b2b8a3));
214 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,Py_ULL(0x748f82ee5defb2fc));
215 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,Py_ULL(0x78a5636f43172f60));
216 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,Py_ULL(0x84c87814a1f0ab72));
217 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,Py_ULL(0x8cc702081a6439ec));
218 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,Py_ULL(0x90befffa23631e28));
219 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,Py_ULL(0xa4506cebde82bde9));
220 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,Py_ULL(0xbef9a3f7b2c67915));
221 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,Py_ULL(0xc67178f2e372532b));
222 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,Py_ULL(0xca273eceea26619c));
223 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,Py_ULL(0xd186b8c721c0c207));
224 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,Py_ULL(0xeada7dd6cde0eb1e));
225 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,Py_ULL(0xf57d4f7fee6ed178));
226 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,Py_ULL(0x06f067aa72176fba));
227 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,Py_ULL(0x0a637dc5a2c898a6));
228 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,Py_ULL(0x113f9804bef90dae));
229 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,Py_ULL(0x1b710b35131c471b));
230 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,Py_ULL(0x28db77f523047d84));
231 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,Py_ULL(0x32caab7b40c72493));
232 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,Py_ULL(0x3c9ebe0a15c9bebc));
233 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,Py_ULL(0x431d67c49c100d4c));
234 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,Py_ULL(0x4cc5d4becb3e42b6));
235 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,Py_ULL(0x597f299cfc657e2a));
236 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,Py_ULL(0x5fcb6fab3ad6faec));
237 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 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239#undef RND
240
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000241 /* feedback */
242 for (i = 0; i < 8; i++) {
243 sha_info->digest[i] = sha_info->digest[i] + S[i];
244 }
245
246}
247
248
249
250/* initialize the SHA digest */
251
252static void
253sha512_init(SHAobject *sha_info)
254{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000255 sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
256 sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
257 sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
258 sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1);
259 sha_info->digest[4] = Py_ULL(0x510e527fade682d1);
260 sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f);
261 sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b);
262 sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000263 sha_info->count_lo = 0L;
264 sha_info->count_hi = 0L;
265 sha_info->local = 0;
266 sha_info->digestsize = 64;
267}
268
269static void
270sha384_init(SHAobject *sha_info)
271{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000272 sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
273 sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
274 sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
275 sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
276 sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
277 sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
278 sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
279 sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000280 sha_info->count_lo = 0L;
281 sha_info->count_hi = 0L;
282 sha_info->local = 0;
283 sha_info->digestsize = 48;
284}
285
286
287/* update the SHA digest */
288
289static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000290sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000291{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000292 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000293 SHA_INT32 clo;
294
295 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
296 if (clo < sha_info->count_lo) {
297 ++sha_info->count_hi;
298 }
299 sha_info->count_lo = clo;
300 sha_info->count_hi += (SHA_INT32) count >> 29;
301 if (sha_info->local) {
302 i = SHA_BLOCKSIZE - sha_info->local;
303 if (i > count) {
304 i = count;
305 }
306 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
307 count -= i;
308 buffer += i;
Victor Stinner70792d22013-05-08 00:00:44 +0200309 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000310 if (sha_info->local == SHA_BLOCKSIZE) {
311 sha512_transform(sha_info);
312 }
313 else {
314 return;
315 }
316 }
317 while (count >= SHA_BLOCKSIZE) {
318 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
319 buffer += SHA_BLOCKSIZE;
320 count -= SHA_BLOCKSIZE;
321 sha512_transform(sha_info);
322 }
323 memcpy(sha_info->data, buffer, count);
Victor Stinner70792d22013-05-08 00:00:44 +0200324 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000325}
326
327/* finish computing the SHA digest */
328
329static void
330sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
331{
332 int count;
333 SHA_INT32 lo_bit_count, hi_bit_count;
334
335 lo_bit_count = sha_info->count_lo;
336 hi_bit_count = sha_info->count_hi;
337 count = (int) ((lo_bit_count >> 3) & 0x7f);
338 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
339 if (count > SHA_BLOCKSIZE - 16) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 memset(((SHA_BYTE *) sha_info->data) + count, 0,
341 SHA_BLOCKSIZE - count);
342 sha512_transform(sha_info);
343 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000344 }
345 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 memset(((SHA_BYTE *) sha_info->data) + count, 0,
347 SHA_BLOCKSIZE - 16 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000348 }
349
350 /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
351 swap these values into host-order. */
352 sha_info->data[112] = 0;
353 sha_info->data[113] = 0;
354 sha_info->data[114] = 0;
355 sha_info->data[115] = 0;
356 sha_info->data[116] = 0;
357 sha_info->data[117] = 0;
358 sha_info->data[118] = 0;
359 sha_info->data[119] = 0;
360 sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
361 sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
362 sha_info->data[122] = (hi_bit_count >> 8) & 0xff;
363 sha_info->data[123] = (hi_bit_count >> 0) & 0xff;
364 sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
365 sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
366 sha_info->data[126] = (lo_bit_count >> 8) & 0xff;
367 sha_info->data[127] = (lo_bit_count >> 0) & 0xff;
368 sha512_transform(sha_info);
369 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
370 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
371 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
372 digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
373 digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
374 digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
375 digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
376 digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
377 digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
378 digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
379 digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
380 digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
381 digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
382 digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
383 digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
384 digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
385 digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
386 digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
387 digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
388 digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
389 digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
390 digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
391 digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
392 digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
393 digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
394 digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
395 digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
396 digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
397 digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
398 digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
399 digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
400 digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
401 digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
402 digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
403 digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
404 digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
405 digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
406 digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
407 digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
408 digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
409 digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
410 digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
411 digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
412 digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
413 digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
414 digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
415 digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
416 digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
417 digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
418 digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
419 digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
420 digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
421 digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
422 digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
423 digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
424 digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
425 digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
426 digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
427 digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
428 digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
429 digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
430 digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
431 digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
432 digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
433}
434
435/*
436 * End of copied SHA code.
437 *
438 * ------------------------------------------------------------------------
439 */
440
441static PyTypeObject SHA384type;
442static PyTypeObject SHA512type;
443
444
445static SHAobject *
446newSHA384object(void)
447{
448 return (SHAobject *)PyObject_New(SHAobject, &SHA384type);
449}
450
451static SHAobject *
452newSHA512object(void)
453{
454 return (SHAobject *)PyObject_New(SHAobject, &SHA512type);
455}
456
457/* Internal methods for a hash object */
458
459static void
460SHA512_dealloc(PyObject *ptr)
461{
462 PyObject_Del(ptr);
463}
464
465
466/* External methods for a hash object */
467
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200468/*[clinic input]
469SHA512Type.copy
470
471Return a copy of the hash object.
472[clinic start generated code]*/
473
474PyDoc_STRVAR(SHA512Type_copy__doc__,
475"copy($self, /)\n"
476"--\n"
477"\n"
478"Return a copy of the hash object.");
479
480#define SHA512TYPE_COPY_METHODDEF \
481 {"copy", (PyCFunction)SHA512Type_copy, METH_NOARGS, SHA512Type_copy__doc__},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000482
483static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200484SHA512Type_copy_impl(SHAobject *self);
485
486static PyObject *
487SHA512Type_copy(SHAobject *self, PyObject *Py_UNUSED(ignored))
488{
489 return SHA512Type_copy_impl(self);
490}
491
492static PyObject *
493SHA512Type_copy_impl(SHAobject *self)
494/*[clinic end generated code: output=14f8e6ce9c61ece0 input=9f5f31e6c457776a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000495{
496 SHAobject *newobj;
497
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000498 if (((PyObject*)self)->ob_type == &SHA512type) {
499 if ( (newobj = newSHA512object())==NULL)
500 return NULL;
501 } else {
502 if ( (newobj = newSHA384object())==NULL)
503 return NULL;
504 }
505
506 SHAcopy(self, newobj);
507 return (PyObject *)newobj;
508}
509
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200510/*[clinic input]
511SHA512Type.digest
512
513Return the digest value as a string of binary data.
514[clinic start generated code]*/
515
516PyDoc_STRVAR(SHA512Type_digest__doc__,
517"digest($self, /)\n"
518"--\n"
519"\n"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000520"Return the digest value as a string of binary data.");
521
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200522#define SHA512TYPE_DIGEST_METHODDEF \
523 {"digest", (PyCFunction)SHA512Type_digest, METH_NOARGS, SHA512Type_digest__doc__},
524
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000525static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200526SHA512Type_digest_impl(SHAobject *self);
527
528static PyObject *
529SHA512Type_digest(SHAobject *self, PyObject *Py_UNUSED(ignored))
530{
531 return SHA512Type_digest_impl(self);
532}
533
534static PyObject *
535SHA512Type_digest_impl(SHAobject *self)
536/*[clinic end generated code: output=be8de024b232977e input=60c2cede9e023018]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000537{
538 unsigned char digest[SHA_DIGESTSIZE];
539 SHAobject temp;
540
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000541 SHAcopy(self, &temp);
542 sha512_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000543 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000544}
545
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200546/*[clinic input]
547SHA512Type.hexdigest
548
549Return the digest value as a string of hexadecimal digits.
550[clinic start generated code]*/
551
552PyDoc_STRVAR(SHA512Type_hexdigest__doc__,
553"hexdigest($self, /)\n"
554"--\n"
555"\n"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000556"Return the digest value as a string of hexadecimal digits.");
557
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200558#define SHA512TYPE_HEXDIGEST_METHODDEF \
559 {"hexdigest", (PyCFunction)SHA512Type_hexdigest, METH_NOARGS, SHA512Type_hexdigest__doc__},
560
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000561static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200562SHA512Type_hexdigest_impl(SHAobject *self);
563
564static PyObject *
565SHA512Type_hexdigest(SHAobject *self, PyObject *Py_UNUSED(ignored))
566{
567 return SHA512Type_hexdigest_impl(self);
568}
569
570static PyObject *
571SHA512Type_hexdigest_impl(SHAobject *self)
572/*[clinic end generated code: output=28a4ab2f9a1781b8 input=498b877b25cbe0a2]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000573{
574 unsigned char digest[SHA_DIGESTSIZE];
575 SHAobject temp;
576 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200577 Py_UCS1 *hex_digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000578 int i, j;
579
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000580 /* Get the raw (binary) digest value */
581 SHAcopy(self, &temp);
582 sha512_final(digest, &temp);
583
584 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200585 retval = PyUnicode_New(self->digestsize * 2, 127);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000586 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200588 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000589
590 /* Make hex version of the digest */
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000591 for (i=j=0; i<self->digestsize; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200592 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000593 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200594 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000595 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200596 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000597 }
Christian Heimesf402e922013-01-03 09:21:55 +0100598#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200599 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100600#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000601 return retval;
602}
603
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200604/*[clinic input]
605SHA512Type.update
606
607 obj: object
608 /
609
610Update this hash object's state with the provided string.
611[clinic start generated code]*/
612
613PyDoc_STRVAR(SHA512Type_update__doc__,
614"update($self, obj, /)\n"
615"--\n"
616"\n"
617"Update this hash object\'s state with the provided string.");
618
619#define SHA512TYPE_UPDATE_METHODDEF \
620 {"update", (PyCFunction)SHA512Type_update, METH_O, SHA512Type_update__doc__},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000621
622static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200623SHA512Type_update(SHAobject *self, PyObject *obj)
624/*[clinic end generated code: output=6be574cdc3a9c52d input=ded2b46656566283]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000625{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000626 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000627
Gregory P. Smith365a1862009-02-12 07:35:29 +0000628 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000629
Gregory P. Smith365a1862009-02-12 07:35:29 +0000630 sha512_update(self, buf.buf, buf.len);
631
632 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000633 Py_INCREF(Py_None);
634 return Py_None;
635}
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200636/*[clinic input]
637dump buffer
638[clinic start generated code]*/
639
640#ifndef SHA512TYPE_COPY_METHODDEF
641 #define SHA512TYPE_COPY_METHODDEF
642#endif /* !defined(SHA512TYPE_COPY_METHODDEF) */
643
644#ifndef SHA512TYPE_DIGEST_METHODDEF
645 #define SHA512TYPE_DIGEST_METHODDEF
646#endif /* !defined(SHA512TYPE_DIGEST_METHODDEF) */
647
648#ifndef SHA512TYPE_HEXDIGEST_METHODDEF
649 #define SHA512TYPE_HEXDIGEST_METHODDEF
650#endif /* !defined(SHA512TYPE_HEXDIGEST_METHODDEF) */
651
652#ifndef SHA512TYPE_UPDATE_METHODDEF
653 #define SHA512TYPE_UPDATE_METHODDEF
654#endif /* !defined(SHA512TYPE_UPDATE_METHODDEF) */
655/*[clinic end generated code: output=de713947d31130e9 input=524ce2e021e4eba6]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000656
657static PyMethodDef SHA_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200658 SHA512TYPE_COPY_METHODDEF
659 SHA512TYPE_DIGEST_METHODDEF
660 SHA512TYPE_HEXDIGEST_METHODDEF
661 SHA512TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000663};
664
665static PyObject *
666SHA512_get_block_size(PyObject *self, void *closure)
667{
Christian Heimes217cfd12007-12-02 14:31:20 +0000668 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000669}
670
671static PyObject *
672SHA512_get_name(PyObject *self, void *closure)
673{
674 if (((SHAobject *)self)->digestsize == 64)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200675 return PyUnicode_FromStringAndSize("sha512", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000676 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200677 return PyUnicode_FromStringAndSize("sha384", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000678}
679
680static PyGetSetDef SHA_getseters[] = {
681 {"block_size",
682 (getter)SHA512_get_block_size, NULL,
683 NULL,
684 NULL},
685 {"name",
686 (getter)SHA512_get_name, NULL,
687 NULL,
688 NULL},
689 {NULL} /* Sentinel */
690};
691
692static PyMemberDef SHA_members[] = {
693 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000694 {NULL} /* Sentinel */
695};
696
697static PyTypeObject SHA384type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000698 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 "_sha512.sha384", /*tp_name*/
700 sizeof(SHAobject), /*tp_size*/
701 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000702 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 SHA512_dealloc, /*tp_dealloc*/
704 0, /*tp_print*/
705 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000706 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000707 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000708 0, /*tp_repr*/
709 0, /*tp_as_number*/
710 0, /*tp_as_sequence*/
711 0, /*tp_as_mapping*/
712 0, /*tp_hash*/
713 0, /*tp_call*/
714 0, /*tp_str*/
715 0, /*tp_getattro*/
716 0, /*tp_setattro*/
717 0, /*tp_as_buffer*/
718 Py_TPFLAGS_DEFAULT, /*tp_flags*/
719 0, /*tp_doc*/
720 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 0, /*tp_clear*/
722 0, /*tp_richcompare*/
723 0, /*tp_weaklistoffset*/
724 0, /*tp_iter*/
725 0, /*tp_iternext*/
726 SHA_methods, /* tp_methods */
727 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000728 SHA_getseters, /* tp_getset */
729};
730
731static PyTypeObject SHA512type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000732 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733 "_sha512.sha512", /*tp_name*/
734 sizeof(SHAobject), /*tp_size*/
735 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000736 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 SHA512_dealloc, /*tp_dealloc*/
738 0, /*tp_print*/
739 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000740 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000741 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000742 0, /*tp_repr*/
743 0, /*tp_as_number*/
744 0, /*tp_as_sequence*/
745 0, /*tp_as_mapping*/
746 0, /*tp_hash*/
747 0, /*tp_call*/
748 0, /*tp_str*/
749 0, /*tp_getattro*/
750 0, /*tp_setattro*/
751 0, /*tp_as_buffer*/
752 Py_TPFLAGS_DEFAULT, /*tp_flags*/
753 0, /*tp_doc*/
754 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 0, /*tp_clear*/
756 0, /*tp_richcompare*/
757 0, /*tp_weaklistoffset*/
758 0, /*tp_iter*/
759 0, /*tp_iternext*/
760 SHA_methods, /* tp_methods */
761 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000762 SHA_getseters, /* tp_getset */
763};
764
765
766/* The single module-level function: new() */
767
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200768/*[clinic input]
769_sha512.sha512
770
771 string: object(c_default="NULL") = b''
772
773Return a new SHA-512 hash object; optionally initialized with a string.
774[clinic start generated code]*/
775
776PyDoc_STRVAR(_sha512_sha512__doc__,
777"sha512($module, /, string=b\'\')\n"
778"--\n"
779"\n"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000780"Return a new SHA-512 hash object; optionally initialized with a string.");
781
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200782#define _SHA512_SHA512_METHODDEF \
783 {"sha512", (PyCFunction)_sha512_sha512, METH_VARARGS|METH_KEYWORDS, _sha512_sha512__doc__},
784
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000785static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200786_sha512_sha512_impl(PyModuleDef *module, PyObject *string);
787
788static PyObject *
789_sha512_sha512(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000790{
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200791 PyObject *return_value = NULL;
792 static char *_keywords[] = {"string", NULL};
793 PyObject *string = NULL;
794
795 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
796 "|O:sha512", _keywords,
797 &string))
798 goto exit;
799 return_value = _sha512_sha512_impl(module, string);
800
801exit:
802 return return_value;
803}
804
805static PyObject *
806_sha512_sha512_impl(PyModuleDef *module, PyObject *string)
807/*[clinic end generated code: output=9e39b11115f36878 input=e69bad9ae9b6a308]*/
808{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000809 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000810 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000811
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200812 if (string)
813 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000814
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000815 if ((new = newSHA512object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200816 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000817 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000818 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000819 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000820
821 sha512_init(new);
822
823 if (PyErr_Occurred()) {
824 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200825 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000826 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000827 return NULL;
828 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200829 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000830 sha512_update(new, buf.buf, buf.len);
831 PyBuffer_Release(&buf);
832 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000833
834 return (PyObject *)new;
835}
836
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200837/*[clinic input]
838_sha512.sha384
839
840 string: object(c_default="NULL") = b''
841
842Return a new SHA-384 hash object; optionally initialized with a string.
843[clinic start generated code]*/
844
845PyDoc_STRVAR(_sha512_sha384__doc__,
846"sha384($module, /, string=b\'\')\n"
847"--\n"
848"\n"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000849"Return a new SHA-384 hash object; optionally initialized with a string.");
850
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200851#define _SHA512_SHA384_METHODDEF \
852 {"sha384", (PyCFunction)_sha512_sha384, METH_VARARGS|METH_KEYWORDS, _sha512_sha384__doc__},
853
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000854static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200855_sha512_sha384_impl(PyModuleDef *module, PyObject *string);
856
857static PyObject *
858_sha512_sha384(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000859{
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200860 PyObject *return_value = NULL;
861 static char *_keywords[] = {"string", NULL};
862 PyObject *string = NULL;
863
864 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
865 "|O:sha384", _keywords,
866 &string))
867 goto exit;
868 return_value = _sha512_sha384_impl(module, string);
869
870exit:
871 return return_value;
872}
873
874static PyObject *
875_sha512_sha384_impl(PyModuleDef *module, PyObject *string)
876/*[clinic end generated code: output=397c6fba3525b93a input=c9327788d4ea4545]*/
877{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000878 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000879 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000880
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200881 if (string)
882 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000883
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000884 if ((new = newSHA384object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200885 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000886 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000887 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000888 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000889
890 sha384_init(new);
891
892 if (PyErr_Occurred()) {
893 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200894 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000895 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000896 return NULL;
897 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200898 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000899 sha512_update(new, buf.buf, buf.len);
900 PyBuffer_Release(&buf);
901 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000902
903 return (PyObject *)new;
904}
905
906
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200907/*[clinic input]
908dump buffer
909[clinic start generated code]*/
910
911#ifndef _SHA512_SHA512_METHODDEF
912 #define _SHA512_SHA512_METHODDEF
913#endif /* !defined(_SHA512_SHA512_METHODDEF) */
914
915#ifndef _SHA512_SHA384_METHODDEF
916 #define _SHA512_SHA384_METHODDEF
917#endif /* !defined(_SHA512_SHA384_METHODDEF) */
918/*[clinic end generated code: output=69d84aa9445b01d8 input=524ce2e021e4eba6]*/
919
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000920/* List of functions exported by this module */
921
922static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200923 _SHA512_SHA512_METHODDEF
924 _SHA512_SHA384_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000926};
927
928
929/* Initialize this module. */
930
931#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
932
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000933
Martin v. Löwis1a214512008-06-11 05:26:20 +0000934static struct PyModuleDef _sha512module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 PyModuleDef_HEAD_INIT,
936 "_sha512",
937 NULL,
938 -1,
939 SHA_functions,
940 NULL,
941 NULL,
942 NULL,
943 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000944};
945
946PyMODINIT_FUNC
947PyInit__sha512(void)
948{
Christian Heimes327dd732013-10-22 15:05:23 +0200949 PyObject *m;
950
Christian Heimes90aa7642007-12-19 02:45:37 +0000951 Py_TYPE(&SHA384type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000952 if (PyType_Ready(&SHA384type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000953 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000954 Py_TYPE(&SHA512type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000955 if (PyType_Ready(&SHA512type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000956 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200957
958 m = PyModule_Create(&_sha512module);
959 if (m == NULL)
960 return NULL;
961
962 Py_INCREF((PyObject *)&SHA384type);
963 PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type);
964 Py_INCREF((PyObject *)&SHA512type);
965 PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type);
966 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000967}
968
969#endif