blob: 725098def4d062312267db9921b0e4ac26d59205 [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"
Victor Stinnerc6b292c2020-06-08 16:30:33 +020020#include "pycore_bitutils.h" // _Py_bswap32()
Victor Stinner4a21e572020-04-15 02:35:41 +020021#include "structmember.h" // PyMemberDef
Gregory P. Smith365a1862009-02-12 07:35:29 +000022#include "hashlib.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000023#include "pystrhex.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000024
Martin v. Löwis501b13c2014-07-27 14:20:23 +020025/*[clinic input]
26module _sha512
27class SHA512Type "SHAobject *" "&PyType_Type"
28[clinic start generated code]*/
29/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/
30
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000031/* Some useful types */
32
33typedef unsigned char SHA_BYTE;
Victor Stinner1ae035b2020-04-17 17:47:20 +020034typedef uint32_t SHA_INT32; /* 32-bit integer */
35typedef uint64_t SHA_INT64; /* 64-bit integer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000036
37/* The SHA block size and message digest sizes, in bytes */
38
39#define SHA_BLOCKSIZE 128
40#define SHA_DIGESTSIZE 64
41
42/* The structure for storing SHA info */
43
44typedef struct {
45 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 SHA_INT64 digest[8]; /* Message digest */
47 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
48 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int local; /* unprocessed amount in data */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000050 int digestsize;
51} SHAobject;
52
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030053#include "clinic/sha512module.c.h"
54
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000055/* When run on a little-endian CPU we need to perform byte reversal on an
56 array of longwords. */
57
Christian Heimes743e0cd2012-10-17 23:52:17 +020058#if PY_LITTLE_ENDIAN
59static void longReverse(SHA_INT64 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000060{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000061 byteCount /= sizeof(*buffer);
Victor Stinner1ae035b2020-04-17 17:47:20 +020062 for (; byteCount--; buffer++) {
63 *buffer = _Py_bswap64(*buffer);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000064 }
65}
Christian Heimes743e0cd2012-10-17 23:52:17 +020066#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000067
68static void SHAcopy(SHAobject *src, SHAobject *dest)
69{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000070 dest->local = src->local;
71 dest->digestsize = src->digestsize;
72 dest->count_lo = src->count_lo;
73 dest->count_hi = src->count_hi;
74 memcpy(dest->digest, src->digest, sizeof(src->digest));
75 memcpy(dest->data, src->data, sizeof(src->data));
76}
77
78
79/* ------------------------------------------------------------------------
80 *
81 * This code for the SHA-512 algorithm was noted as public domain. The
82 * original headers are pasted below.
83 *
84 * Several changes have been made to make it more compatible with the
85 * Python environment and desired interface.
86 *
87 */
88
89/* LibTomCrypt, modular cryptographic library -- Tom St Denis
90 *
91 * LibTomCrypt is a library that provides various cryptographic
92 * algorithms in a highly modular and flexible manner.
93 *
94 * The library is free for all purposes without any express
Martin Panter46f50722016-05-26 05:35:26 +000095 * guarantee it works.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000096 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000097 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000098 */
99
100
101/* SHA512 by Tom St Denis */
102
103/* Various logical functions */
104#define ROR64(x, y) \
Benjamin Petersonac965ca2016-09-18 18:12:21 -0700105 ( ((((x) & 0xFFFFFFFFFFFFFFFFULL)>>((unsigned long long)(y) & 63)) | \
106 ((x)<<((unsigned long long)(64-((y) & 63))))) & 0xFFFFFFFFFFFFFFFFULL)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000107#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000109#define S(x, n) ROR64((x),(n))
Benjamin Petersonac965ca2016-09-18 18:12:21 -0700110#define R(x, n) (((x) & 0xFFFFFFFFFFFFFFFFULL) >> ((unsigned long long)n))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000111#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
112#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
113#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
114#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
115
116
117static void
118sha512_transform(SHAobject *sha_info)
119{
120 int i;
121 SHA_INT64 S[8], W[80], t0, t1;
122
123 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200124#if PY_LITTLE_ENDIAN
125 longReverse(W, (int)sizeof(sha_info->data));
126#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000127
128 for (i = 16; i < 80; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000130 }
131 for (i = 0; i < 8; ++i) {
132 S[i] = sha_info->digest[i];
133 }
134
135 /* Compress */
136#define RND(a,b,c,d,e,f,g,h,i,ki) \
137 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
138 t1 = Sigma0(a) + Maj(a, b, c); \
139 d += t0; \
140 h = t0 + t1;
141
Benjamin Petersonac965ca2016-09-18 18:12:21 -0700142 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98d728ae22ULL);
143 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x7137449123ef65cdULL);
144 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcfec4d3b2fULL);
145 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba58189dbbcULL);
146 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25bf348b538ULL);
147 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1b605d019ULL);
148 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4af194f9bULL);
149 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5da6d8118ULL);
150 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98a3030242ULL);
151 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b0145706fbeULL);
152 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be4ee4b28cULL);
153 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3d5ffb4e2ULL);
154 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74f27b896fULL);
155 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe3b1696b1ULL);
156 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a725c71235ULL);
157 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174cf692694ULL);
158 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c19ef14ad2ULL);
159 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786384f25e3ULL);
160 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc68b8cd5b5ULL);
161 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc77ac9c65ULL);
162 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f592b0275ULL);
163 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa6ea6e483ULL);
164 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dcbd41fbd4ULL);
165 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da831153b5ULL);
166 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152ee66dfabULL);
167 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d2db43210ULL);
168 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c898fb213fULL);
169 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7beef0ee4ULL);
170 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf33da88fc2ULL);
171 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147930aa725ULL);
172 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351e003826fULL);
173 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x142929670a0e6e70ULL);
174 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a8546d22ffcULL);
175 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b21385c26c926ULL);
176 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc5ac42aedULL);
177 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d139d95b3dfULL);
178 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a73548baf63deULL);
179 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb3c77b2a8ULL);
180 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e47edaee6ULL);
181 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c851482353bULL);
182 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a14cf10364ULL);
183 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664bbc423001ULL);
184 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70d0f89791ULL);
185 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a30654be30ULL);
186 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819d6ef5218ULL);
187 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd69906245565a910ULL);
188 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e35855771202aULL);
189 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa07032bbd1b8ULL);
190 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116b8d2d0c8ULL);
191 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c085141ab53ULL);
192 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774cdf8eeb99ULL);
193 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5e19b48a8ULL);
194 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3c5c95a63ULL);
195 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4ae3418acbULL);
196 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f7763e373ULL);
197 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3d6b2b8a3ULL);
198 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee5defb2fcULL);
199 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f43172f60ULL);
200 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814a1f0ab72ULL);
201 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc702081a6439ecULL);
202 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa23631e28ULL);
203 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506cebde82bde9ULL);
204 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7b2c67915ULL);
205 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2e372532bULL);
206 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,0xca273eceea26619cULL);
207 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,0xd186b8c721c0c207ULL);
208 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,0xeada7dd6cde0eb1eULL);
209 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,0xf57d4f7fee6ed178ULL);
210 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,0x06f067aa72176fbaULL);
211 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,0x0a637dc5a2c898a6ULL);
212 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,0x113f9804bef90daeULL);
213 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,0x1b710b35131c471bULL);
214 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,0x28db77f523047d84ULL);
215 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,0x32caab7b40c72493ULL);
216 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,0x3c9ebe0a15c9bebcULL);
217 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,0x431d67c49c100d4cULL);
218 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,0x4cc5d4becb3e42b6ULL);
219 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,0x597f299cfc657e2aULL);
220 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,0x5fcb6fab3ad6faecULL);
221 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,0x6c44198c4a475817ULL);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223#undef RND
224
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000225 /* feedback */
226 for (i = 0; i < 8; i++) {
227 sha_info->digest[i] = sha_info->digest[i] + S[i];
228 }
229
230}
231
232
233
234/* initialize the SHA digest */
235
236static void
237sha512_init(SHAobject *sha_info)
238{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000239 sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
240 sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
241 sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
242 sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1);
243 sha_info->digest[4] = Py_ULL(0x510e527fade682d1);
244 sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f);
245 sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b);
246 sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000247 sha_info->count_lo = 0L;
248 sha_info->count_hi = 0L;
249 sha_info->local = 0;
250 sha_info->digestsize = 64;
251}
252
253static void
254sha384_init(SHAobject *sha_info)
255{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000256 sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
257 sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
258 sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
259 sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
260 sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
261 sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
262 sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
263 sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000264 sha_info->count_lo = 0L;
265 sha_info->count_hi = 0L;
266 sha_info->local = 0;
267 sha_info->digestsize = 48;
268}
269
270
271/* update the SHA digest */
272
273static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000274sha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000275{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000276 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000277 SHA_INT32 clo;
278
279 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
280 if (clo < sha_info->count_lo) {
281 ++sha_info->count_hi;
282 }
283 sha_info->count_lo = clo;
284 sha_info->count_hi += (SHA_INT32) count >> 29;
285 if (sha_info->local) {
286 i = SHA_BLOCKSIZE - sha_info->local;
287 if (i > count) {
288 i = count;
289 }
290 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
291 count -= i;
292 buffer += i;
Victor Stinner70792d22013-05-08 00:00:44 +0200293 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000294 if (sha_info->local == SHA_BLOCKSIZE) {
295 sha512_transform(sha_info);
296 }
297 else {
298 return;
299 }
300 }
301 while (count >= SHA_BLOCKSIZE) {
302 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
303 buffer += SHA_BLOCKSIZE;
304 count -= SHA_BLOCKSIZE;
305 sha512_transform(sha_info);
306 }
307 memcpy(sha_info->data, buffer, count);
Victor Stinner70792d22013-05-08 00:00:44 +0200308 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000309}
310
311/* finish computing the SHA digest */
312
313static void
314sha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
315{
316 int count;
317 SHA_INT32 lo_bit_count, hi_bit_count;
318
319 lo_bit_count = sha_info->count_lo;
320 hi_bit_count = sha_info->count_hi;
321 count = (int) ((lo_bit_count >> 3) & 0x7f);
322 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
323 if (count > SHA_BLOCKSIZE - 16) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 memset(((SHA_BYTE *) sha_info->data) + count, 0,
325 SHA_BLOCKSIZE - count);
326 sha512_transform(sha_info);
327 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000328 }
329 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 memset(((SHA_BYTE *) sha_info->data) + count, 0,
331 SHA_BLOCKSIZE - 16 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000332 }
333
334 /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
335 swap these values into host-order. */
336 sha_info->data[112] = 0;
337 sha_info->data[113] = 0;
338 sha_info->data[114] = 0;
339 sha_info->data[115] = 0;
340 sha_info->data[116] = 0;
341 sha_info->data[117] = 0;
342 sha_info->data[118] = 0;
343 sha_info->data[119] = 0;
344 sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
345 sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
346 sha_info->data[122] = (hi_bit_count >> 8) & 0xff;
347 sha_info->data[123] = (hi_bit_count >> 0) & 0xff;
348 sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
349 sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
350 sha_info->data[126] = (lo_bit_count >> 8) & 0xff;
351 sha_info->data[127] = (lo_bit_count >> 0) & 0xff;
352 sha512_transform(sha_info);
353 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
354 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
355 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
356 digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
357 digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
358 digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
359 digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
360 digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
361 digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
362 digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
363 digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
364 digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
365 digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
366 digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
367 digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
368 digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
369 digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
370 digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
371 digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
372 digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
373 digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
374 digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
375 digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
376 digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
377 digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
378 digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
379 digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
380 digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
381 digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
382 digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
383 digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
384 digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
385 digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
386 digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
387 digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
388 digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
389 digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
390 digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
391 digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
392 digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
393 digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
394 digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
395 digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
396 digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
397 digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
398 digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
399 digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
400 digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
401 digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
402 digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
403 digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
404 digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
405 digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
406 digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
407 digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
408 digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
409 digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
410 digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
411 digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
412 digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
413 digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
414 digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
415 digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
416 digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
417}
418
419/*
420 * End of copied SHA code.
421 *
422 * ------------------------------------------------------------------------
423 */
424
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500425typedef struct {
426 PyTypeObject* sha384_type;
427 PyTypeObject* sha512_type;
428} SHA512State;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000429
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500430static inline SHA512State*
431sha512_get_state(PyObject *module)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000432{
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500433 void *state = PyModule_GetState(module);
434 assert(state != NULL);
435 return (SHA512State *)state;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000436}
437
438static SHAobject *
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500439newSHA384object(SHA512State *st)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000440{
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500441 return (SHAobject *)PyObject_New(SHAobject, st->sha384_type);
442}
443
444static SHAobject *
445newSHA512object(SHA512State *st)
446{
447 return (SHAobject *)PyObject_New(SHAobject, st->sha512_type);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000448}
449
450/* Internal methods for a hash object */
451
452static void
453SHA512_dealloc(PyObject *ptr)
454{
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500455 PyTypeObject *tp = Py_TYPE(ptr);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000456 PyObject_Del(ptr);
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500457 Py_DECREF(tp);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000458}
459
460
461/* External methods for a hash object */
462
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200463/*[clinic input]
464SHA512Type.copy
465
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500466 cls: defining_class
467
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200468Return a copy of the hash object.
469[clinic start generated code]*/
470
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200471static PyObject *
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500472SHA512Type_copy_impl(SHAobject *self, PyTypeObject *cls)
473/*[clinic end generated code: output=85ea5b47837a08e6 input=f673a18f66527c90]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000474{
475 SHAobject *newobj;
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500476 SHA512State *st = PyType_GetModuleState(cls);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000477
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500478 if (Py_IS_TYPE((PyObject*)self, st->sha512_type)) {
479 if ( (newobj = newSHA512object(st))==NULL) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000480 return NULL;
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500481 }
482 }
483 else {
484 if ( (newobj = newSHA384object(st))==NULL) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000485 return NULL;
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500486 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000487 }
488
489 SHAcopy(self, newobj);
490 return (PyObject *)newobj;
491}
492
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200493/*[clinic input]
494SHA512Type.digest
495
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530496Return the digest value as a bytes object.
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200497[clinic start generated code]*/
498
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200499static PyObject *
500SHA512Type_digest_impl(SHAobject *self)
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530501/*[clinic end generated code: output=1080bbeeef7dde1b input=f6470dd359071f4b]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000502{
503 unsigned char digest[SHA_DIGESTSIZE];
504 SHAobject temp;
505
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000506 SHAcopy(self, &temp);
507 sha512_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000508 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000509}
510
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200511/*[clinic input]
512SHA512Type.hexdigest
513
514Return the digest value as a string of hexadecimal digits.
515[clinic start generated code]*/
516
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200517static PyObject *
518SHA512Type_hexdigest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300519/*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000520{
521 unsigned char digest[SHA_DIGESTSIZE];
522 SHAobject temp;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000523
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000524 /* Get the raw (binary) digest value */
525 SHAcopy(self, &temp);
526 sha512_final(digest, &temp);
527
Gregory P. Smith8cb65692015-04-25 23:22:26 +0000528 return _Py_strhex((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000529}
530
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200531/*[clinic input]
532SHA512Type.update
533
534 obj: object
535 /
536
537Update this hash object's state with the provided string.
538[clinic start generated code]*/
539
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000540static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200541SHA512Type_update(SHAobject *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300542/*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000543{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000544 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000545
Gregory P. Smith365a1862009-02-12 07:35:29 +0000546 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000547
Gregory P. Smith365a1862009-02-12 07:35:29 +0000548 sha512_update(self, buf.buf, buf.len);
549
550 PyBuffer_Release(&buf);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200551 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000552}
553
554static PyMethodDef SHA_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200555 SHA512TYPE_COPY_METHODDEF
556 SHA512TYPE_DIGEST_METHODDEF
557 SHA512TYPE_HEXDIGEST_METHODDEF
558 SHA512TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000560};
561
562static PyObject *
563SHA512_get_block_size(PyObject *self, void *closure)
564{
Christian Heimes217cfd12007-12-02 14:31:20 +0000565 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000566}
567
568static PyObject *
569SHA512_get_name(PyObject *self, void *closure)
570{
571 if (((SHAobject *)self)->digestsize == 64)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200572 return PyUnicode_FromStringAndSize("sha512", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000573 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200574 return PyUnicode_FromStringAndSize("sha384", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000575}
576
577static PyGetSetDef SHA_getseters[] = {
578 {"block_size",
579 (getter)SHA512_get_block_size, NULL,
580 NULL,
581 NULL},
582 {"name",
583 (getter)SHA512_get_name, NULL,
584 NULL,
585 NULL},
586 {NULL} /* Sentinel */
587};
588
589static PyMemberDef SHA_members[] = {
590 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000591 {NULL} /* Sentinel */
592};
593
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500594static PyType_Slot sha512_sha384_type_slots[] = {
595 {Py_tp_dealloc, SHA512_dealloc},
596 {Py_tp_methods, SHA_methods},
597 {Py_tp_members, SHA_members},
598 {Py_tp_getset, SHA_getseters},
599 {0,0}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000600};
601
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500602static PyType_Spec sha512_sha384_type_spec = {
603 .name = "_sha512.sha384",
604 .basicsize = sizeof(SHAobject),
605 .flags = Py_TPFLAGS_DEFAULT,
606 .slots = sha512_sha384_type_slots
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000607};
608
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500609static PyType_Slot sha512_sha512_type_slots[] = {
610 {Py_tp_dealloc, SHA512_dealloc},
611 {Py_tp_methods, SHA_methods},
612 {Py_tp_members, SHA_members},
613 {Py_tp_getset, SHA_getseters},
614 {0,0}
615};
616
617// Using PyType_GetModuleState() on this type is safe since
618// it cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag.
619static PyType_Spec sha512_sha512_type_spec = {
620 .name = "_sha512.sha512",
621 .basicsize = sizeof(SHAobject),
622 .flags = Py_TPFLAGS_DEFAULT,
623 .slots = sha512_sha512_type_slots
624};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000625
626/* The single module-level function: new() */
627
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200628/*[clinic input]
629_sha512.sha512
630
631 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200632 *
633 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200634
635Return a new SHA-512 hash object; optionally initialized with a string.
636[clinic start generated code]*/
637
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200638static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200639_sha512_sha512_impl(PyObject *module, PyObject *string, int usedforsecurity)
640/*[clinic end generated code: output=a8d9e5f9e6a0831c input=23b4daebc2ebb9c9]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200641{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000642 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000643 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000644
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500645 SHA512State *st = sha512_get_state(module);
646
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200647 if (string)
648 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000649
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500650 if ((new = newSHA512object(st)) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200651 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000652 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000653 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000654 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000655
656 sha512_init(new);
657
658 if (PyErr_Occurred()) {
659 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200660 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000661 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000662 return NULL;
663 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200664 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000665 sha512_update(new, buf.buf, buf.len);
666 PyBuffer_Release(&buf);
667 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000668
669 return (PyObject *)new;
670}
671
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200672/*[clinic input]
673_sha512.sha384
674
675 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200676 *
677 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200678
679Return a new SHA-384 hash object; optionally initialized with a string.
680[clinic start generated code]*/
681
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200682static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200683_sha512_sha384_impl(PyObject *module, PyObject *string, int usedforsecurity)
684/*[clinic end generated code: output=da7d594a08027ac3 input=59ef72f039a6b431]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200685{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000686 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000687 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000688
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500689 SHA512State *st = sha512_get_state(module);
690
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200691 if (string)
692 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000693
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500694 if ((new = newSHA384object(st)) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200695 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000696 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000697 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000698 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000699
700 sha384_init(new);
701
702 if (PyErr_Occurred()) {
703 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200704 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000705 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000706 return NULL;
707 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200708 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000709 sha512_update(new, buf.buf, buf.len);
710 PyBuffer_Release(&buf);
711 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000712
713 return (PyObject *)new;
714}
715
716
717/* List of functions exported by this module */
718
719static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200720 _SHA512_SHA512_METHODDEF
721 _SHA512_SHA384_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000723};
724
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500725static int
726_sha512_traverse(PyObject *module, visitproc visit, void *arg)
727{
728 SHA512State *state = sha512_get_state(module);
729 Py_VISIT(state->sha384_type);
730 Py_VISIT(state->sha512_type);
731 return 0;
732}
733
734static int
735_sha512_clear(PyObject *module)
736{
737 SHA512State *state = sha512_get_state(module);
738 Py_CLEAR(state->sha384_type);
739 Py_CLEAR(state->sha512_type);
740 return 0;
741}
742
743static void
744_sha512_free(void *module)
745{
746 _sha512_clear((PyObject *)module);
747}
748
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000749
750/* Initialize this module. */
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500751static int
752_sha512_exec(PyObject *m)
753{
754 SHA512State* st = sha512_get_state(m);
755
756 st->sha384_type = (PyTypeObject *)PyType_FromModuleAndSpec(
757 m, &sha512_sha384_type_spec, NULL);
758
759 st->sha512_type = (PyTypeObject *)PyType_FromModuleAndSpec(
760 m, &sha512_sha512_type_spec, NULL);
761
762 if (st->sha384_type == NULL || st->sha512_type == NULL) {
763 return -1;
764 }
765
766 Py_INCREF(st->sha384_type);
767 if (PyModule_AddObject(m, "SHA384Type", (PyObject *)st->sha384_type) < 0) {
768 Py_DECREF(st->sha384_type);
769 return -1;
770 }
771
772 Py_INCREF(st->sha512_type);
773 if (PyModule_AddObject(m, "SHA384Type", (PyObject *)st->sha512_type) < 0) {
774 Py_DECREF(st->sha512_type);
775 return -1;
776 }
777
778 return 0;
779}
780
781static PyModuleDef_Slot _sha512_slots[] = {
782 {Py_mod_exec, _sha512_exec},
783 {0, NULL}
784};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000785
Martin v. Löwis1a214512008-06-11 05:26:20 +0000786static struct PyModuleDef _sha512module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 PyModuleDef_HEAD_INIT,
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500788 .m_name = "_sha512",
789 .m_size = sizeof(SHA512State),
790 .m_methods = SHA_functions,
791 .m_slots = _sha512_slots,
792 .m_traverse = _sha512_traverse,
793 .m_clear = _sha512_clear,
794 .m_free = _sha512_free
Martin v. Löwis1a214512008-06-11 05:26:20 +0000795};
796
797PyMODINIT_FUNC
798PyInit__sha512(void)
799{
Mohamed Koubaa63f102f2020-09-06 05:09:51 -0500800 return PyModuleDef_Init(&_sha512module);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000801}