ยปCore Development>Code coverage>Modules/sha512module.c

Python code coverage for Modules/sha512module.c

#countcontent
1n/a/* SHA512 module */
2n/a
3n/a/* This module provides an interface to NIST's SHA-512 and SHA-384 Algorithms */
4n/a
5n/a/* See below for information about the original code this module was
6n/a based upon. Additional work performed by:
7n/a
8n/a Andrew Kuchling (amk@amk.ca)
9n/a Greg Stein (gstein@lyra.org)
10n/a Trevor Perrin (trevp@trevp.net)
11n/a
12n/a Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
13n/a Licensed to PSF under a Contributor Agreement.
14n/a
15n/a*/
16n/a
17n/a/* SHA objects */
18n/a
19n/a#include "Python.h"
20n/a#include "structmember.h"
21n/a#include "hashlib.h"
22n/a#include "pystrhex.h"
23n/a
24n/a/*[clinic input]
25n/amodule _sha512
26n/aclass SHA512Type "SHAobject *" "&PyType_Type"
27n/a[clinic start generated code]*/
28n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=81a3ccde92bcfe8d]*/
29n/a
30n/a/* Some useful types */
31n/a
32n/atypedef unsigned char SHA_BYTE;
33n/a
34n/a#if SIZEOF_INT == 4
35n/atypedef unsigned int SHA_INT32; /* 32-bit integer */
36n/atypedef unsigned long long SHA_INT64; /* 64-bit integer */
37n/a#else
38n/a/* not defined. compilation will die. */
39n/a#endif
40n/a
41n/a/* The SHA block size and message digest sizes, in bytes */
42n/a
43n/a#define SHA_BLOCKSIZE 128
44n/a#define SHA_DIGESTSIZE 64
45n/a
46n/a/* The structure for storing SHA info */
47n/a
48n/atypedef struct {
49n/a PyObject_HEAD
50n/a SHA_INT64 digest[8]; /* Message digest */
51n/a SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
52n/a SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
53n/a int local; /* unprocessed amount in data */
54n/a int digestsize;
55n/a} SHAobject;
56n/a
57n/a#include "clinic/sha512module.c.h"
58n/a
59n/a/* When run on a little-endian CPU we need to perform byte reversal on an
60n/a array of longwords. */
61n/a
62n/a#if PY_LITTLE_ENDIAN
63n/astatic void longReverse(SHA_INT64 *buffer, int byteCount)
64n/a{
65n/a SHA_INT64 value;
66n/a
67n/a byteCount /= sizeof(*buffer);
68n/a while (byteCount--) {
69n/a value = *buffer;
70n/a
71n/a ((unsigned char*)buffer)[0] = (unsigned char)(value >> 56) & 0xff;
72n/a ((unsigned char*)buffer)[1] = (unsigned char)(value >> 48) & 0xff;
73n/a ((unsigned char*)buffer)[2] = (unsigned char)(value >> 40) & 0xff;
74n/a ((unsigned char*)buffer)[3] = (unsigned char)(value >> 32) & 0xff;
75n/a ((unsigned char*)buffer)[4] = (unsigned char)(value >> 24) & 0xff;
76n/a ((unsigned char*)buffer)[5] = (unsigned char)(value >> 16) & 0xff;
77n/a ((unsigned char*)buffer)[6] = (unsigned char)(value >> 8) & 0xff;
78n/a ((unsigned char*)buffer)[7] = (unsigned char)(value ) & 0xff;
79n/a
80n/a buffer++;
81n/a }
82n/a}
83n/a#endif
84n/a
85n/astatic void SHAcopy(SHAobject *src, SHAobject *dest)
86n/a{
87n/a dest->local = src->local;
88n/a dest->digestsize = src->digestsize;
89n/a dest->count_lo = src->count_lo;
90n/a dest->count_hi = src->count_hi;
91n/a memcpy(dest->digest, src->digest, sizeof(src->digest));
92n/a memcpy(dest->data, src->data, sizeof(src->data));
93n/a}
94n/a
95n/a
96n/a/* ------------------------------------------------------------------------
97n/a *
98n/a * This code for the SHA-512 algorithm was noted as public domain. The
99n/a * original headers are pasted below.
100n/a *
101n/a * Several changes have been made to make it more compatible with the
102n/a * Python environment and desired interface.
103n/a *
104n/a */
105n/a
106n/a/* LibTomCrypt, modular cryptographic library -- Tom St Denis
107n/a *
108n/a * LibTomCrypt is a library that provides various cryptographic
109n/a * algorithms in a highly modular and flexible manner.
110n/a *
111n/a * The library is free for all purposes without any express
112n/a * guarantee it works.
113n/a *
114n/a * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
115n/a */
116n/a
117n/a
118n/a/* SHA512 by Tom St Denis */
119n/a
120n/a/* Various logical functions */
121n/a#define ROR64(x, y) \
122n/a ( ((((x) & 0xFFFFFFFFFFFFFFFFULL)>>((unsigned long long)(y) & 63)) | \
123n/a ((x)<<((unsigned long long)(64-((y) & 63))))) & 0xFFFFFFFFFFFFFFFFULL)
124n/a#define Ch(x,y,z) (z ^ (x & (y ^ z)))
125n/a#define Maj(x,y,z) (((x | y) & z) | (x & y))
126n/a#define S(x, n) ROR64((x),(n))
127n/a#define R(x, n) (((x) & 0xFFFFFFFFFFFFFFFFULL) >> ((unsigned long long)n))
128n/a#define Sigma0(x) (S(x, 28) ^ S(x, 34) ^ S(x, 39))
129n/a#define Sigma1(x) (S(x, 14) ^ S(x, 18) ^ S(x, 41))
130n/a#define Gamma0(x) (S(x, 1) ^ S(x, 8) ^ R(x, 7))
131n/a#define Gamma1(x) (S(x, 19) ^ S(x, 61) ^ R(x, 6))
132n/a
133n/a
134n/astatic void
135n/asha512_transform(SHAobject *sha_info)
136n/a{
137n/a int i;
138n/a SHA_INT64 S[8], W[80], t0, t1;
139n/a
140n/a memcpy(W, sha_info->data, sizeof(sha_info->data));
141n/a#if PY_LITTLE_ENDIAN
142n/a longReverse(W, (int)sizeof(sha_info->data));
143n/a#endif
144n/a
145n/a for (i = 16; i < 80; ++i) {
146n/a W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
147n/a }
148n/a for (i = 0; i < 8; ++i) {
149n/a S[i] = sha_info->digest[i];
150n/a }
151n/a
152n/a /* Compress */
153n/a#define RND(a,b,c,d,e,f,g,h,i,ki) \
154n/a t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
155n/a t1 = Sigma0(a) + Maj(a, b, c); \
156n/a d += t0; \
157n/a h = t0 + t1;
158n/a
159n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98d728ae22ULL);
160n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x7137449123ef65cdULL);
161n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcfec4d3b2fULL);
162n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba58189dbbcULL);
163n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25bf348b538ULL);
164n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1b605d019ULL);
165n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4af194f9bULL);
166n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5da6d8118ULL);
167n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98a3030242ULL);
168n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b0145706fbeULL);
169n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be4ee4b28cULL);
170n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3d5ffb4e2ULL);
171n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74f27b896fULL);
172n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe3b1696b1ULL);
173n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a725c71235ULL);
174n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174cf692694ULL);
175n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c19ef14ad2ULL);
176n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786384f25e3ULL);
177n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc68b8cd5b5ULL);
178n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc77ac9c65ULL);
179n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f592b0275ULL);
180n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa6ea6e483ULL);
181n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dcbd41fbd4ULL);
182n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da831153b5ULL);
183n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152ee66dfabULL);
184n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d2db43210ULL);
185n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c898fb213fULL);
186n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7beef0ee4ULL);
187n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf33da88fc2ULL);
188n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147930aa725ULL);
189n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351e003826fULL);
190n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x142929670a0e6e70ULL);
191n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a8546d22ffcULL);
192n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b21385c26c926ULL);
193n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc5ac42aedULL);
194n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d139d95b3dfULL);
195n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a73548baf63deULL);
196n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb3c77b2a8ULL);
197n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e47edaee6ULL);
198n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c851482353bULL);
199n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a14cf10364ULL);
200n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664bbc423001ULL);
201n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70d0f89791ULL);
202n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a30654be30ULL);
203n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819d6ef5218ULL);
204n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd69906245565a910ULL);
205n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e35855771202aULL);
206n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa07032bbd1b8ULL);
207n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116b8d2d0c8ULL);
208n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c085141ab53ULL);
209n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774cdf8eeb99ULL);
210n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5e19b48a8ULL);
211n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3c5c95a63ULL);
212n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4ae3418acbULL);
213n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f7763e373ULL);
214n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3d6b2b8a3ULL);
215n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee5defb2fcULL);
216n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f43172f60ULL);
217n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814a1f0ab72ULL);
218n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc702081a6439ecULL);
219n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa23631e28ULL);
220n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506cebde82bde9ULL);
221n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7b2c67915ULL);
222n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2e372532bULL);
223n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],64,0xca273eceea26619cULL);
224n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],65,0xd186b8c721c0c207ULL);
225n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],66,0xeada7dd6cde0eb1eULL);
226n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],67,0xf57d4f7fee6ed178ULL);
227n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],68,0x06f067aa72176fbaULL);
228n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],69,0x0a637dc5a2c898a6ULL);
229n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],70,0x113f9804bef90daeULL);
230n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],71,0x1b710b35131c471bULL);
231n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],72,0x28db77f523047d84ULL);
232n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],73,0x32caab7b40c72493ULL);
233n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],74,0x3c9ebe0a15c9bebcULL);
234n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],75,0x431d67c49c100d4cULL);
235n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],76,0x4cc5d4becb3e42b6ULL);
236n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],77,0x597f299cfc657e2aULL);
237n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],78,0x5fcb6fab3ad6faecULL);
238n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],79,0x6c44198c4a475817ULL);
239n/a
240n/a#undef RND
241n/a
242n/a /* feedback */
243n/a for (i = 0; i < 8; i++) {
244n/a sha_info->digest[i] = sha_info->digest[i] + S[i];
245n/a }
246n/a
247n/a}
248n/a
249n/a
250n/a
251n/a/* initialize the SHA digest */
252n/a
253n/astatic void
254n/asha512_init(SHAobject *sha_info)
255n/a{
256n/a sha_info->digest[0] = Py_ULL(0x6a09e667f3bcc908);
257n/a sha_info->digest[1] = Py_ULL(0xbb67ae8584caa73b);
258n/a sha_info->digest[2] = Py_ULL(0x3c6ef372fe94f82b);
259n/a sha_info->digest[3] = Py_ULL(0xa54ff53a5f1d36f1);
260n/a sha_info->digest[4] = Py_ULL(0x510e527fade682d1);
261n/a sha_info->digest[5] = Py_ULL(0x9b05688c2b3e6c1f);
262n/a sha_info->digest[6] = Py_ULL(0x1f83d9abfb41bd6b);
263n/a sha_info->digest[7] = Py_ULL(0x5be0cd19137e2179);
264n/a sha_info->count_lo = 0L;
265n/a sha_info->count_hi = 0L;
266n/a sha_info->local = 0;
267n/a sha_info->digestsize = 64;
268n/a}
269n/a
270n/astatic void
271n/asha384_init(SHAobject *sha_info)
272n/a{
273n/a sha_info->digest[0] = Py_ULL(0xcbbb9d5dc1059ed8);
274n/a sha_info->digest[1] = Py_ULL(0x629a292a367cd507);
275n/a sha_info->digest[2] = Py_ULL(0x9159015a3070dd17);
276n/a sha_info->digest[3] = Py_ULL(0x152fecd8f70e5939);
277n/a sha_info->digest[4] = Py_ULL(0x67332667ffc00b31);
278n/a sha_info->digest[5] = Py_ULL(0x8eb44a8768581511);
279n/a sha_info->digest[6] = Py_ULL(0xdb0c2e0d64f98fa7);
280n/a sha_info->digest[7] = Py_ULL(0x47b5481dbefa4fa4);
281n/a sha_info->count_lo = 0L;
282n/a sha_info->count_hi = 0L;
283n/a sha_info->local = 0;
284n/a sha_info->digestsize = 48;
285n/a}
286n/a
287n/a
288n/a/* update the SHA digest */
289n/a
290n/astatic void
291n/asha512_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
292n/a{
293n/a Py_ssize_t i;
294n/a SHA_INT32 clo;
295n/a
296n/a clo = sha_info->count_lo + ((SHA_INT32) count << 3);
297n/a if (clo < sha_info->count_lo) {
298n/a ++sha_info->count_hi;
299n/a }
300n/a sha_info->count_lo = clo;
301n/a sha_info->count_hi += (SHA_INT32) count >> 29;
302n/a if (sha_info->local) {
303n/a i = SHA_BLOCKSIZE - sha_info->local;
304n/a if (i > count) {
305n/a i = count;
306n/a }
307n/a memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
308n/a count -= i;
309n/a buffer += i;
310n/a sha_info->local += (int)i;
311n/a if (sha_info->local == SHA_BLOCKSIZE) {
312n/a sha512_transform(sha_info);
313n/a }
314n/a else {
315n/a return;
316n/a }
317n/a }
318n/a while (count >= SHA_BLOCKSIZE) {
319n/a memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
320n/a buffer += SHA_BLOCKSIZE;
321n/a count -= SHA_BLOCKSIZE;
322n/a sha512_transform(sha_info);
323n/a }
324n/a memcpy(sha_info->data, buffer, count);
325n/a sha_info->local = (int)count;
326n/a}
327n/a
328n/a/* finish computing the SHA digest */
329n/a
330n/astatic void
331n/asha512_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
332n/a{
333n/a int count;
334n/a SHA_INT32 lo_bit_count, hi_bit_count;
335n/a
336n/a lo_bit_count = sha_info->count_lo;
337n/a hi_bit_count = sha_info->count_hi;
338n/a count = (int) ((lo_bit_count >> 3) & 0x7f);
339n/a ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
340n/a if (count > SHA_BLOCKSIZE - 16) {
341n/a memset(((SHA_BYTE *) sha_info->data) + count, 0,
342n/a SHA_BLOCKSIZE - count);
343n/a sha512_transform(sha_info);
344n/a memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 16);
345n/a }
346n/a else {
347n/a memset(((SHA_BYTE *) sha_info->data) + count, 0,
348n/a SHA_BLOCKSIZE - 16 - count);
349n/a }
350n/a
351n/a /* GJS: note that we add the hi/lo in big-endian. sha512_transform will
352n/a swap these values into host-order. */
353n/a sha_info->data[112] = 0;
354n/a sha_info->data[113] = 0;
355n/a sha_info->data[114] = 0;
356n/a sha_info->data[115] = 0;
357n/a sha_info->data[116] = 0;
358n/a sha_info->data[117] = 0;
359n/a sha_info->data[118] = 0;
360n/a sha_info->data[119] = 0;
361n/a sha_info->data[120] = (hi_bit_count >> 24) & 0xff;
362n/a sha_info->data[121] = (hi_bit_count >> 16) & 0xff;
363n/a sha_info->data[122] = (hi_bit_count >> 8) & 0xff;
364n/a sha_info->data[123] = (hi_bit_count >> 0) & 0xff;
365n/a sha_info->data[124] = (lo_bit_count >> 24) & 0xff;
366n/a sha_info->data[125] = (lo_bit_count >> 16) & 0xff;
367n/a sha_info->data[126] = (lo_bit_count >> 8) & 0xff;
368n/a sha_info->data[127] = (lo_bit_count >> 0) & 0xff;
369n/a sha512_transform(sha_info);
370n/a digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 56) & 0xff);
371n/a digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 48) & 0xff);
372n/a digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 40) & 0xff);
373n/a digest[ 3] = (unsigned char) ((sha_info->digest[0] >> 32) & 0xff);
374n/a digest[ 4] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
375n/a digest[ 5] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
376n/a digest[ 6] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
377n/a digest[ 7] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
378n/a digest[ 8] = (unsigned char) ((sha_info->digest[1] >> 56) & 0xff);
379n/a digest[ 9] = (unsigned char) ((sha_info->digest[1] >> 48) & 0xff);
380n/a digest[10] = (unsigned char) ((sha_info->digest[1] >> 40) & 0xff);
381n/a digest[11] = (unsigned char) ((sha_info->digest[1] >> 32) & 0xff);
382n/a digest[12] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
383n/a digest[13] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
384n/a digest[14] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
385n/a digest[15] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
386n/a digest[16] = (unsigned char) ((sha_info->digest[2] >> 56) & 0xff);
387n/a digest[17] = (unsigned char) ((sha_info->digest[2] >> 48) & 0xff);
388n/a digest[18] = (unsigned char) ((sha_info->digest[2] >> 40) & 0xff);
389n/a digest[19] = (unsigned char) ((sha_info->digest[2] >> 32) & 0xff);
390n/a digest[20] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
391n/a digest[21] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
392n/a digest[22] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
393n/a digest[23] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
394n/a digest[24] = (unsigned char) ((sha_info->digest[3] >> 56) & 0xff);
395n/a digest[25] = (unsigned char) ((sha_info->digest[3] >> 48) & 0xff);
396n/a digest[26] = (unsigned char) ((sha_info->digest[3] >> 40) & 0xff);
397n/a digest[27] = (unsigned char) ((sha_info->digest[3] >> 32) & 0xff);
398n/a digest[28] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
399n/a digest[29] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
400n/a digest[30] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
401n/a digest[31] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
402n/a digest[32] = (unsigned char) ((sha_info->digest[4] >> 56) & 0xff);
403n/a digest[33] = (unsigned char) ((sha_info->digest[4] >> 48) & 0xff);
404n/a digest[34] = (unsigned char) ((sha_info->digest[4] >> 40) & 0xff);
405n/a digest[35] = (unsigned char) ((sha_info->digest[4] >> 32) & 0xff);
406n/a digest[36] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
407n/a digest[37] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
408n/a digest[38] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
409n/a digest[39] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
410n/a digest[40] = (unsigned char) ((sha_info->digest[5] >> 56) & 0xff);
411n/a digest[41] = (unsigned char) ((sha_info->digest[5] >> 48) & 0xff);
412n/a digest[42] = (unsigned char) ((sha_info->digest[5] >> 40) & 0xff);
413n/a digest[43] = (unsigned char) ((sha_info->digest[5] >> 32) & 0xff);
414n/a digest[44] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
415n/a digest[45] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
416n/a digest[46] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
417n/a digest[47] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
418n/a digest[48] = (unsigned char) ((sha_info->digest[6] >> 56) & 0xff);
419n/a digest[49] = (unsigned char) ((sha_info->digest[6] >> 48) & 0xff);
420n/a digest[50] = (unsigned char) ((sha_info->digest[6] >> 40) & 0xff);
421n/a digest[51] = (unsigned char) ((sha_info->digest[6] >> 32) & 0xff);
422n/a digest[52] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
423n/a digest[53] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
424n/a digest[54] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
425n/a digest[55] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
426n/a digest[56] = (unsigned char) ((sha_info->digest[7] >> 56) & 0xff);
427n/a digest[57] = (unsigned char) ((sha_info->digest[7] >> 48) & 0xff);
428n/a digest[58] = (unsigned char) ((sha_info->digest[7] >> 40) & 0xff);
429n/a digest[59] = (unsigned char) ((sha_info->digest[7] >> 32) & 0xff);
430n/a digest[60] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
431n/a digest[61] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
432n/a digest[62] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
433n/a digest[63] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
434n/a}
435n/a
436n/a/*
437n/a * End of copied SHA code.
438n/a *
439n/a * ------------------------------------------------------------------------
440n/a */
441n/a
442n/astatic PyTypeObject SHA384type;
443n/astatic PyTypeObject SHA512type;
444n/a
445n/a
446n/astatic SHAobject *
447n/anewSHA384object(void)
448n/a{
449n/a return (SHAobject *)PyObject_New(SHAobject, &SHA384type);
450n/a}
451n/a
452n/astatic SHAobject *
453n/anewSHA512object(void)
454n/a{
455n/a return (SHAobject *)PyObject_New(SHAobject, &SHA512type);
456n/a}
457n/a
458n/a/* Internal methods for a hash object */
459n/a
460n/astatic void
461n/aSHA512_dealloc(PyObject *ptr)
462n/a{
463n/a PyObject_Del(ptr);
464n/a}
465n/a
466n/a
467n/a/* External methods for a hash object */
468n/a
469n/a/*[clinic input]
470n/aSHA512Type.copy
471n/a
472n/aReturn a copy of the hash object.
473n/a[clinic start generated code]*/
474n/a
475n/astatic PyObject *
476n/aSHA512Type_copy_impl(SHAobject *self)
477n/a/*[clinic end generated code: output=adea896ed3164821 input=9f5f31e6c457776a]*/
478n/a{
479n/a SHAobject *newobj;
480n/a
481n/a if (((PyObject*)self)->ob_type == &SHA512type) {
482n/a if ( (newobj = newSHA512object())==NULL)
483n/a return NULL;
484n/a } else {
485n/a if ( (newobj = newSHA384object())==NULL)
486n/a return NULL;
487n/a }
488n/a
489n/a SHAcopy(self, newobj);
490n/a return (PyObject *)newobj;
491n/a}
492n/a
493n/a/*[clinic input]
494n/aSHA512Type.digest
495n/a
496n/aReturn the digest value as a string of binary data.
497n/a[clinic start generated code]*/
498n/a
499n/astatic PyObject *
500n/aSHA512Type_digest_impl(SHAobject *self)
501n/a/*[clinic end generated code: output=1080bbeeef7dde1b input=60c2cede9e023018]*/
502n/a{
503n/a unsigned char digest[SHA_DIGESTSIZE];
504n/a SHAobject temp;
505n/a
506n/a SHAcopy(self, &temp);
507n/a sha512_final(digest, &temp);
508n/a return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
509n/a}
510n/a
511n/a/*[clinic input]
512n/aSHA512Type.hexdigest
513n/a
514n/aReturn the digest value as a string of hexadecimal digits.
515n/a[clinic start generated code]*/
516n/a
517n/astatic PyObject *
518n/aSHA512Type_hexdigest_impl(SHAobject *self)
519n/a/*[clinic end generated code: output=7373305b8601e18b input=498b877b25cbe0a2]*/
520n/a{
521n/a unsigned char digest[SHA_DIGESTSIZE];
522n/a SHAobject temp;
523n/a
524n/a /* Get the raw (binary) digest value */
525n/a SHAcopy(self, &temp);
526n/a sha512_final(digest, &temp);
527n/a
528n/a return _Py_strhex((const char *)digest, self->digestsize);
529n/a}
530n/a
531n/a/*[clinic input]
532n/aSHA512Type.update
533n/a
534n/a obj: object
535n/a /
536n/a
537n/aUpdate this hash object's state with the provided string.
538n/a[clinic start generated code]*/
539n/a
540n/astatic PyObject *
541n/aSHA512Type_update(SHAobject *self, PyObject *obj)
542n/a/*[clinic end generated code: output=1cf333e73995a79e input=ded2b46656566283]*/
543n/a{
544n/a Py_buffer buf;
545n/a
546n/a GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
547n/a
548n/a sha512_update(self, buf.buf, buf.len);
549n/a
550n/a PyBuffer_Release(&buf);
551n/a Py_RETURN_NONE;
552n/a}
553n/a
554n/astatic PyMethodDef SHA_methods[] = {
555n/a SHA512TYPE_COPY_METHODDEF
556n/a SHA512TYPE_DIGEST_METHODDEF
557n/a SHA512TYPE_HEXDIGEST_METHODDEF
558n/a SHA512TYPE_UPDATE_METHODDEF
559n/a {NULL, NULL} /* sentinel */
560n/a};
561n/a
562n/astatic PyObject *
563n/aSHA512_get_block_size(PyObject *self, void *closure)
564n/a{
565n/a return PyLong_FromLong(SHA_BLOCKSIZE);
566n/a}
567n/a
568n/astatic PyObject *
569n/aSHA512_get_name(PyObject *self, void *closure)
570n/a{
571n/a if (((SHAobject *)self)->digestsize == 64)
572n/a return PyUnicode_FromStringAndSize("sha512", 6);
573n/a else
574n/a return PyUnicode_FromStringAndSize("sha384", 6);
575n/a}
576n/a
577n/astatic PyGetSetDef SHA_getseters[] = {
578n/a {"block_size",
579n/a (getter)SHA512_get_block_size, NULL,
580n/a NULL,
581n/a NULL},
582n/a {"name",
583n/a (getter)SHA512_get_name, NULL,
584n/a NULL,
585n/a NULL},
586n/a {NULL} /* Sentinel */
587n/a};
588n/a
589n/astatic PyMemberDef SHA_members[] = {
590n/a {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
591n/a {NULL} /* Sentinel */
592n/a};
593n/a
594n/astatic PyTypeObject SHA384type = {
595n/a PyVarObject_HEAD_INIT(NULL, 0)
596n/a "_sha512.sha384", /*tp_name*/
597n/a sizeof(SHAobject), /*tp_size*/
598n/a 0, /*tp_itemsize*/
599n/a /* methods */
600n/a SHA512_dealloc, /*tp_dealloc*/
601n/a 0, /*tp_print*/
602n/a 0, /*tp_getattr*/
603n/a 0, /*tp_setattr*/
604n/a 0, /*tp_reserved*/
605n/a 0, /*tp_repr*/
606n/a 0, /*tp_as_number*/
607n/a 0, /*tp_as_sequence*/
608n/a 0, /*tp_as_mapping*/
609n/a 0, /*tp_hash*/
610n/a 0, /*tp_call*/
611n/a 0, /*tp_str*/
612n/a 0, /*tp_getattro*/
613n/a 0, /*tp_setattro*/
614n/a 0, /*tp_as_buffer*/
615n/a Py_TPFLAGS_DEFAULT, /*tp_flags*/
616n/a 0, /*tp_doc*/
617n/a 0, /*tp_traverse*/
618n/a 0, /*tp_clear*/
619n/a 0, /*tp_richcompare*/
620n/a 0, /*tp_weaklistoffset*/
621n/a 0, /*tp_iter*/
622n/a 0, /*tp_iternext*/
623n/a SHA_methods, /* tp_methods */
624n/a SHA_members, /* tp_members */
625n/a SHA_getseters, /* tp_getset */
626n/a};
627n/a
628n/astatic PyTypeObject SHA512type = {
629n/a PyVarObject_HEAD_INIT(NULL, 0)
630n/a "_sha512.sha512", /*tp_name*/
631n/a sizeof(SHAobject), /*tp_size*/
632n/a 0, /*tp_itemsize*/
633n/a /* methods */
634n/a SHA512_dealloc, /*tp_dealloc*/
635n/a 0, /*tp_print*/
636n/a 0, /*tp_getattr*/
637n/a 0, /*tp_setattr*/
638n/a 0, /*tp_reserved*/
639n/a 0, /*tp_repr*/
640n/a 0, /*tp_as_number*/
641n/a 0, /*tp_as_sequence*/
642n/a 0, /*tp_as_mapping*/
643n/a 0, /*tp_hash*/
644n/a 0, /*tp_call*/
645n/a 0, /*tp_str*/
646n/a 0, /*tp_getattro*/
647n/a 0, /*tp_setattro*/
648n/a 0, /*tp_as_buffer*/
649n/a Py_TPFLAGS_DEFAULT, /*tp_flags*/
650n/a 0, /*tp_doc*/
651n/a 0, /*tp_traverse*/
652n/a 0, /*tp_clear*/
653n/a 0, /*tp_richcompare*/
654n/a 0, /*tp_weaklistoffset*/
655n/a 0, /*tp_iter*/
656n/a 0, /*tp_iternext*/
657n/a SHA_methods, /* tp_methods */
658n/a SHA_members, /* tp_members */
659n/a SHA_getseters, /* tp_getset */
660n/a};
661n/a
662n/a
663n/a/* The single module-level function: new() */
664n/a
665n/a/*[clinic input]
666n/a_sha512.sha512
667n/a
668n/a string: object(c_default="NULL") = b''
669n/a
670n/aReturn a new SHA-512 hash object; optionally initialized with a string.
671n/a[clinic start generated code]*/
672n/a
673n/astatic PyObject *
674n/a_sha512_sha512_impl(PyObject *module, PyObject *string)
675n/a/*[clinic end generated code: output=8b865a2df73bd387 input=e69bad9ae9b6a308]*/
676n/a{
677n/a SHAobject *new;
678n/a Py_buffer buf;
679n/a
680n/a if (string)
681n/a GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
682n/a
683n/a if ((new = newSHA512object()) == NULL) {
684n/a if (string)
685n/a PyBuffer_Release(&buf);
686n/a return NULL;
687n/a }
688n/a
689n/a sha512_init(new);
690n/a
691n/a if (PyErr_Occurred()) {
692n/a Py_DECREF(new);
693n/a if (string)
694n/a PyBuffer_Release(&buf);
695n/a return NULL;
696n/a }
697n/a if (string) {
698n/a sha512_update(new, buf.buf, buf.len);
699n/a PyBuffer_Release(&buf);
700n/a }
701n/a
702n/a return (PyObject *)new;
703n/a}
704n/a
705n/a/*[clinic input]
706n/a_sha512.sha384
707n/a
708n/a string: object(c_default="NULL") = b''
709n/a
710n/aReturn a new SHA-384 hash object; optionally initialized with a string.
711n/a[clinic start generated code]*/
712n/a
713n/astatic PyObject *
714n/a_sha512_sha384_impl(PyObject *module, PyObject *string)
715n/a/*[clinic end generated code: output=ae4b2e26decf81e8 input=c9327788d4ea4545]*/
716n/a{
717n/a SHAobject *new;
718n/a Py_buffer buf;
719n/a
720n/a if (string)
721n/a GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
722n/a
723n/a if ((new = newSHA384object()) == NULL) {
724n/a if (string)
725n/a PyBuffer_Release(&buf);
726n/a return NULL;
727n/a }
728n/a
729n/a sha384_init(new);
730n/a
731n/a if (PyErr_Occurred()) {
732n/a Py_DECREF(new);
733n/a if (string)
734n/a PyBuffer_Release(&buf);
735n/a return NULL;
736n/a }
737n/a if (string) {
738n/a sha512_update(new, buf.buf, buf.len);
739n/a PyBuffer_Release(&buf);
740n/a }
741n/a
742n/a return (PyObject *)new;
743n/a}
744n/a
745n/a
746n/a/* List of functions exported by this module */
747n/a
748n/astatic struct PyMethodDef SHA_functions[] = {
749n/a _SHA512_SHA512_METHODDEF
750n/a _SHA512_SHA384_METHODDEF
751n/a {NULL, NULL} /* Sentinel */
752n/a};
753n/a
754n/a
755n/a/* Initialize this module. */
756n/a
757n/a#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
758n/a
759n/a
760n/astatic struct PyModuleDef _sha512module = {
761n/a PyModuleDef_HEAD_INIT,
762n/a "_sha512",
763n/a NULL,
764n/a -1,
765n/a SHA_functions,
766n/a NULL,
767n/a NULL,
768n/a NULL,
769n/a NULL
770n/a};
771n/a
772n/aPyMODINIT_FUNC
773n/aPyInit__sha512(void)
774n/a{
775n/a PyObject *m;
776n/a
777n/a Py_TYPE(&SHA384type) = &PyType_Type;
778n/a if (PyType_Ready(&SHA384type) < 0)
779n/a return NULL;
780n/a Py_TYPE(&SHA512type) = &PyType_Type;
781n/a if (PyType_Ready(&SHA512type) < 0)
782n/a return NULL;
783n/a
784n/a m = PyModule_Create(&_sha512module);
785n/a if (m == NULL)
786n/a return NULL;
787n/a
788n/a Py_INCREF((PyObject *)&SHA384type);
789n/a PyModule_AddObject(m, "SHA384Type", (PyObject *)&SHA384type);
790n/a Py_INCREF((PyObject *)&SHA512type);
791n/a PyModule_AddObject(m, "SHA512Type", (PyObject *)&SHA512type);
792n/a return m;
793n/a}