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

Python code coverage for Modules/sha256module.c

#countcontent
1n/a/* SHA256 module */
2n/a
3n/a/* This module provides an interface to NIST's SHA-256 and SHA-224 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 _sha256
26n/aclass SHA256Type "SHAobject *" "&PyType_Type"
27n/a[clinic start generated code]*/
28n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=71a39174d4f0a744]*/
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/a#else
37n/a/* not defined. compilation will die. */
38n/a#endif
39n/a
40n/a/* The SHA block size and message digest sizes, in bytes */
41n/a
42n/a#define SHA_BLOCKSIZE 64
43n/a#define SHA_DIGESTSIZE 32
44n/a
45n/a/* The structure for storing SHA info */
46n/a
47n/atypedef struct {
48n/a PyObject_HEAD
49n/a SHA_INT32 digest[8]; /* Message digest */
50n/a SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
51n/a SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
52n/a int local; /* unprocessed amount in data */
53n/a int digestsize;
54n/a} SHAobject;
55n/a
56n/a#include "clinic/sha256module.c.h"
57n/a
58n/a/* When run on a little-endian CPU we need to perform byte reversal on an
59n/a array of longwords. */
60n/a
61n/a#if PY_LITTLE_ENDIAN
62n/astatic void longReverse(SHA_INT32 *buffer, int byteCount)
63n/a{
64n/a SHA_INT32 value;
65n/a
66n/a byteCount /= sizeof(*buffer);
67n/a while (byteCount--) {
68n/a value = *buffer;
69n/a value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
70n/a ( ( value & 0x00FF00FFL ) << 8 );
71n/a *buffer++ = ( value << 16 ) | ( value >> 16 );
72n/a }
73n/a}
74n/a#endif
75n/a
76n/astatic void SHAcopy(SHAobject *src, SHAobject *dest)
77n/a{
78n/a dest->local = src->local;
79n/a dest->digestsize = src->digestsize;
80n/a dest->count_lo = src->count_lo;
81n/a dest->count_hi = src->count_hi;
82n/a memcpy(dest->digest, src->digest, sizeof(src->digest));
83n/a memcpy(dest->data, src->data, sizeof(src->data));
84n/a}
85n/a
86n/a
87n/a/* ------------------------------------------------------------------------
88n/a *
89n/a * This code for the SHA-256 algorithm was noted as public domain. The
90n/a * original headers are pasted below.
91n/a *
92n/a * Several changes have been made to make it more compatible with the
93n/a * Python environment and desired interface.
94n/a *
95n/a */
96n/a
97n/a/* LibTomCrypt, modular cryptographic library -- Tom St Denis
98n/a *
99n/a * LibTomCrypt is a library that provides various cryptographic
100n/a * algorithms in a highly modular and flexible manner.
101n/a *
102n/a * The library is free for all purposes without any express
103n/a * guarantee it works.
104n/a *
105n/a * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
106n/a */
107n/a
108n/a
109n/a/* SHA256 by Tom St Denis */
110n/a
111n/a/* Various logical functions */
112n/a#define ROR(x, y)\
113n/a( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
114n/a((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
115n/a#define Ch(x,y,z) (z ^ (x & (y ^ z)))
116n/a#define Maj(x,y,z) (((x | y) & z) | (x & y))
117n/a#define S(x, n) ROR((x),(n))
118n/a#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
119n/a#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
120n/a#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
121n/a#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
122n/a#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
123n/a
124n/a
125n/astatic void
126n/asha_transform(SHAobject *sha_info)
127n/a{
128n/a int i;
129n/a SHA_INT32 S[8], W[64], t0, t1;
130n/a
131n/a memcpy(W, sha_info->data, sizeof(sha_info->data));
132n/a#if PY_LITTLE_ENDIAN
133n/a longReverse(W, (int)sizeof(sha_info->data));
134n/a#endif
135n/a
136n/a for (i = 16; i < 64; ++i) {
137n/a W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
138n/a }
139n/a for (i = 0; i < 8; ++i) {
140n/a S[i] = sha_info->digest[i];
141n/a }
142n/a
143n/a /* Compress */
144n/a#define RND(a,b,c,d,e,f,g,h,i,ki) \
145n/a t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
146n/a t1 = Sigma0(a) + Maj(a, b, c); \
147n/a d += t0; \
148n/a h = t0 + t1;
149n/a
150n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
151n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
152n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
153n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
154n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
155n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
156n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
157n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
158n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
159n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
160n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
161n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
162n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
163n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
164n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
165n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
166n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
167n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
168n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
169n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
170n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
171n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
172n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
173n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
174n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
175n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
176n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
177n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
178n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
179n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
180n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
181n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
182n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
183n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
184n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
185n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
186n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
187n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
188n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
189n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
190n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
191n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
192n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
193n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
194n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
195n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
196n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
197n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
198n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
199n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
200n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
201n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
202n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
203n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
204n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
205n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
206n/a RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
207n/a RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
208n/a RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
209n/a RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
210n/a RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
211n/a RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
212n/a RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
213n/a RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
214n/a
215n/a#undef RND
216n/a
217n/a /* feedback */
218n/a for (i = 0; i < 8; i++) {
219n/a sha_info->digest[i] = sha_info->digest[i] + S[i];
220n/a }
221n/a
222n/a}
223n/a
224n/a
225n/a
226n/a/* initialize the SHA digest */
227n/a
228n/astatic void
229n/asha_init(SHAobject *sha_info)
230n/a{
231n/a sha_info->digest[0] = 0x6A09E667L;
232n/a sha_info->digest[1] = 0xBB67AE85L;
233n/a sha_info->digest[2] = 0x3C6EF372L;
234n/a sha_info->digest[3] = 0xA54FF53AL;
235n/a sha_info->digest[4] = 0x510E527FL;
236n/a sha_info->digest[5] = 0x9B05688CL;
237n/a sha_info->digest[6] = 0x1F83D9ABL;
238n/a sha_info->digest[7] = 0x5BE0CD19L;
239n/a sha_info->count_lo = 0L;
240n/a sha_info->count_hi = 0L;
241n/a sha_info->local = 0;
242n/a sha_info->digestsize = 32;
243n/a}
244n/a
245n/astatic void
246n/asha224_init(SHAobject *sha_info)
247n/a{
248n/a sha_info->digest[0] = 0xc1059ed8L;
249n/a sha_info->digest[1] = 0x367cd507L;
250n/a sha_info->digest[2] = 0x3070dd17L;
251n/a sha_info->digest[3] = 0xf70e5939L;
252n/a sha_info->digest[4] = 0xffc00b31L;
253n/a sha_info->digest[5] = 0x68581511L;
254n/a sha_info->digest[6] = 0x64f98fa7L;
255n/a sha_info->digest[7] = 0xbefa4fa4L;
256n/a sha_info->count_lo = 0L;
257n/a sha_info->count_hi = 0L;
258n/a sha_info->local = 0;
259n/a sha_info->digestsize = 28;
260n/a}
261n/a
262n/a
263n/a/* update the SHA digest */
264n/a
265n/astatic void
266n/asha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
267n/a{
268n/a Py_ssize_t i;
269n/a SHA_INT32 clo;
270n/a
271n/a clo = sha_info->count_lo + ((SHA_INT32) count << 3);
272n/a if (clo < sha_info->count_lo) {
273n/a ++sha_info->count_hi;
274n/a }
275n/a sha_info->count_lo = clo;
276n/a sha_info->count_hi += (SHA_INT32) count >> 29;
277n/a if (sha_info->local) {
278n/a i = SHA_BLOCKSIZE - sha_info->local;
279n/a if (i > count) {
280n/a i = count;
281n/a }
282n/a memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
283n/a count -= i;
284n/a buffer += i;
285n/a sha_info->local += (int)i;
286n/a if (sha_info->local == SHA_BLOCKSIZE) {
287n/a sha_transform(sha_info);
288n/a }
289n/a else {
290n/a return;
291n/a }
292n/a }
293n/a while (count >= SHA_BLOCKSIZE) {
294n/a memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
295n/a buffer += SHA_BLOCKSIZE;
296n/a count -= SHA_BLOCKSIZE;
297n/a sha_transform(sha_info);
298n/a }
299n/a memcpy(sha_info->data, buffer, count);
300n/a sha_info->local = (int)count;
301n/a}
302n/a
303n/a/* finish computing the SHA digest */
304n/a
305n/astatic void
306n/asha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
307n/a{
308n/a int count;
309n/a SHA_INT32 lo_bit_count, hi_bit_count;
310n/a
311n/a lo_bit_count = sha_info->count_lo;
312n/a hi_bit_count = sha_info->count_hi;
313n/a count = (int) ((lo_bit_count >> 3) & 0x3f);
314n/a ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
315n/a if (count > SHA_BLOCKSIZE - 8) {
316n/a memset(((SHA_BYTE *) sha_info->data) + count, 0,
317n/a SHA_BLOCKSIZE - count);
318n/a sha_transform(sha_info);
319n/a memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
320n/a }
321n/a else {
322n/a memset(((SHA_BYTE *) sha_info->data) + count, 0,
323n/a SHA_BLOCKSIZE - 8 - count);
324n/a }
325n/a
326n/a /* GJS: note that we add the hi/lo in big-endian. sha_transform will
327n/a swap these values into host-order. */
328n/a sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
329n/a sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
330n/a sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
331n/a sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
332n/a sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
333n/a sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
334n/a sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
335n/a sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
336n/a sha_transform(sha_info);
337n/a digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
338n/a digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
339n/a digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
340n/a digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
341n/a digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
342n/a digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
343n/a digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
344n/a digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
345n/a digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
346n/a digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
347n/a digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
348n/a digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
349n/a digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
350n/a digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
351n/a digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
352n/a digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
353n/a digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
354n/a digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
355n/a digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
356n/a digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
357n/a digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
358n/a digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
359n/a digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
360n/a digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
361n/a digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
362n/a digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
363n/a digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
364n/a digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
365n/a digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
366n/a digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
367n/a digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
368n/a digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
369n/a}
370n/a
371n/a/*
372n/a * End of copied SHA code.
373n/a *
374n/a * ------------------------------------------------------------------------
375n/a */
376n/a
377n/astatic PyTypeObject SHA224type;
378n/astatic PyTypeObject SHA256type;
379n/a
380n/a
381n/astatic SHAobject *
382n/anewSHA224object(void)
383n/a{
384n/a return (SHAobject *)PyObject_New(SHAobject, &SHA224type);
385n/a}
386n/a
387n/astatic SHAobject *
388n/anewSHA256object(void)
389n/a{
390n/a return (SHAobject *)PyObject_New(SHAobject, &SHA256type);
391n/a}
392n/a
393n/a/* Internal methods for a hash object */
394n/a
395n/astatic void
396n/aSHA_dealloc(PyObject *ptr)
397n/a{
398n/a PyObject_Del(ptr);
399n/a}
400n/a
401n/a
402n/a/* External methods for a hash object */
403n/a
404n/a/*[clinic input]
405n/aSHA256Type.copy
406n/a
407n/aReturn a copy of the hash object.
408n/a[clinic start generated code]*/
409n/a
410n/astatic PyObject *
411n/aSHA256Type_copy_impl(SHAobject *self)
412n/a/*[clinic end generated code: output=1a8bbd66a0c9c168 input=f58840a618d4f2a7]*/
413n/a{
414n/a SHAobject *newobj;
415n/a
416n/a if (Py_TYPE(self) == &SHA256type) {
417n/a if ( (newobj = newSHA256object())==NULL)
418n/a return NULL;
419n/a } else {
420n/a if ( (newobj = newSHA224object())==NULL)
421n/a return NULL;
422n/a }
423n/a
424n/a SHAcopy(self, newobj);
425n/a return (PyObject *)newobj;
426n/a}
427n/a
428n/a/*[clinic input]
429n/aSHA256Type.digest
430n/a
431n/aReturn the digest value as a string of binary data.
432n/a[clinic start generated code]*/
433n/a
434n/astatic PyObject *
435n/aSHA256Type_digest_impl(SHAobject *self)
436n/a/*[clinic end generated code: output=46616a5e909fbc3d input=1fb752e58954157d]*/
437n/a{
438n/a unsigned char digest[SHA_DIGESTSIZE];
439n/a SHAobject temp;
440n/a
441n/a SHAcopy(self, &temp);
442n/a sha_final(digest, &temp);
443n/a return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
444n/a}
445n/a
446n/a/*[clinic input]
447n/aSHA256Type.hexdigest
448n/a
449n/aReturn the digest value as a string of hexadecimal digits.
450n/a[clinic start generated code]*/
451n/a
452n/astatic PyObject *
453n/aSHA256Type_hexdigest_impl(SHAobject *self)
454n/a/*[clinic end generated code: output=725f8a7041ae97f3 input=0cc4c714693010d1]*/
455n/a{
456n/a unsigned char digest[SHA_DIGESTSIZE];
457n/a SHAobject temp;
458n/a
459n/a /* Get the raw (binary) digest value */
460n/a SHAcopy(self, &temp);
461n/a sha_final(digest, &temp);
462n/a
463n/a return _Py_strhex((const char *)digest, self->digestsize);
464n/a}
465n/a
466n/a/*[clinic input]
467n/aSHA256Type.update
468n/a
469n/a obj: object
470n/a /
471n/a
472n/aUpdate this hash object's state with the provided string.
473n/a[clinic start generated code]*/
474n/a
475n/astatic PyObject *
476n/aSHA256Type_update(SHAobject *self, PyObject *obj)
477n/a/*[clinic end generated code: output=0967fb2860c66af7 input=b2d449d5b30f0f5a]*/
478n/a{
479n/a Py_buffer buf;
480n/a
481n/a GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
482n/a
483n/a sha_update(self, buf.buf, buf.len);
484n/a
485n/a PyBuffer_Release(&buf);
486n/a Py_RETURN_NONE;
487n/a}
488n/a
489n/astatic PyMethodDef SHA_methods[] = {
490n/a SHA256TYPE_COPY_METHODDEF
491n/a SHA256TYPE_DIGEST_METHODDEF
492n/a SHA256TYPE_HEXDIGEST_METHODDEF
493n/a SHA256TYPE_UPDATE_METHODDEF
494n/a {NULL, NULL} /* sentinel */
495n/a};
496n/a
497n/astatic PyObject *
498n/aSHA256_get_block_size(PyObject *self, void *closure)
499n/a{
500n/a return PyLong_FromLong(SHA_BLOCKSIZE);
501n/a}
502n/a
503n/astatic PyObject *
504n/aSHA256_get_name(PyObject *self, void *closure)
505n/a{
506n/a if (((SHAobject *)self)->digestsize == 32)
507n/a return PyUnicode_FromStringAndSize("sha256", 6);
508n/a else
509n/a return PyUnicode_FromStringAndSize("sha224", 6);
510n/a}
511n/a
512n/astatic PyGetSetDef SHA_getseters[] = {
513n/a {"block_size",
514n/a (getter)SHA256_get_block_size, NULL,
515n/a NULL,
516n/a NULL},
517n/a {"name",
518n/a (getter)SHA256_get_name, NULL,
519n/a NULL,
520n/a NULL},
521n/a {NULL} /* Sentinel */
522n/a};
523n/a
524n/astatic PyMemberDef SHA_members[] = {
525n/a {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
526n/a {NULL} /* Sentinel */
527n/a};
528n/a
529n/astatic PyTypeObject SHA224type = {
530n/a PyVarObject_HEAD_INIT(NULL, 0)
531n/a "_sha256.sha224", /*tp_name*/
532n/a sizeof(SHAobject), /*tp_size*/
533n/a 0, /*tp_itemsize*/
534n/a /* methods */
535n/a SHA_dealloc, /*tp_dealloc*/
536n/a 0, /*tp_print*/
537n/a 0, /*tp_getattr*/
538n/a 0, /*tp_setattr*/
539n/a 0, /*tp_reserved*/
540n/a 0, /*tp_repr*/
541n/a 0, /*tp_as_number*/
542n/a 0, /*tp_as_sequence*/
543n/a 0, /*tp_as_mapping*/
544n/a 0, /*tp_hash*/
545n/a 0, /*tp_call*/
546n/a 0, /*tp_str*/
547n/a 0, /*tp_getattro*/
548n/a 0, /*tp_setattro*/
549n/a 0, /*tp_as_buffer*/
550n/a Py_TPFLAGS_DEFAULT, /*tp_flags*/
551n/a 0, /*tp_doc*/
552n/a 0, /*tp_traverse*/
553n/a 0, /*tp_clear*/
554n/a 0, /*tp_richcompare*/
555n/a 0, /*tp_weaklistoffset*/
556n/a 0, /*tp_iter*/
557n/a 0, /*tp_iternext*/
558n/a SHA_methods, /* tp_methods */
559n/a SHA_members, /* tp_members */
560n/a SHA_getseters, /* tp_getset */
561n/a};
562n/a
563n/astatic PyTypeObject SHA256type = {
564n/a PyVarObject_HEAD_INIT(NULL, 0)
565n/a "_sha256.sha256", /*tp_name*/
566n/a sizeof(SHAobject), /*tp_size*/
567n/a 0, /*tp_itemsize*/
568n/a /* methods */
569n/a SHA_dealloc, /*tp_dealloc*/
570n/a 0, /*tp_print*/
571n/a 0, /*tp_getattr*/
572n/a 0, /*tp_setattr*/
573n/a 0, /*tp_reserved*/
574n/a 0, /*tp_repr*/
575n/a 0, /*tp_as_number*/
576n/a 0, /*tp_as_sequence*/
577n/a 0, /*tp_as_mapping*/
578n/a 0, /*tp_hash*/
579n/a 0, /*tp_call*/
580n/a 0, /*tp_str*/
581n/a 0, /*tp_getattro*/
582n/a 0, /*tp_setattro*/
583n/a 0, /*tp_as_buffer*/
584n/a Py_TPFLAGS_DEFAULT, /*tp_flags*/
585n/a 0, /*tp_doc*/
586n/a 0, /*tp_traverse*/
587n/a 0, /*tp_clear*/
588n/a 0, /*tp_richcompare*/
589n/a 0, /*tp_weaklistoffset*/
590n/a 0, /*tp_iter*/
591n/a 0, /*tp_iternext*/
592n/a SHA_methods, /* tp_methods */
593n/a SHA_members, /* tp_members */
594n/a SHA_getseters, /* tp_getset */
595n/a};
596n/a
597n/a
598n/a/* The single module-level function: new() */
599n/a
600n/a/*[clinic input]
601n/a_sha256.sha256
602n/a
603n/a string: object(c_default="NULL") = b''
604n/a
605n/aReturn a new SHA-256 hash object; optionally initialized with a string.
606n/a[clinic start generated code]*/
607n/a
608n/astatic PyObject *
609n/a_sha256_sha256_impl(PyObject *module, PyObject *string)
610n/a/*[clinic end generated code: output=fa644436dcea5c31 input=09cce3fb855056b2]*/
611n/a{
612n/a SHAobject *new;
613n/a Py_buffer buf;
614n/a
615n/a if (string)
616n/a GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
617n/a
618n/a if ((new = newSHA256object()) == NULL) {
619n/a if (string)
620n/a PyBuffer_Release(&buf);
621n/a return NULL;
622n/a }
623n/a
624n/a sha_init(new);
625n/a
626n/a if (PyErr_Occurred()) {
627n/a Py_DECREF(new);
628n/a if (string)
629n/a PyBuffer_Release(&buf);
630n/a return NULL;
631n/a }
632n/a if (string) {
633n/a sha_update(new, buf.buf, buf.len);
634n/a PyBuffer_Release(&buf);
635n/a }
636n/a
637n/a return (PyObject *)new;
638n/a}
639n/a
640n/a/*[clinic input]
641n/a_sha256.sha224
642n/a
643n/a string: object(c_default="NULL") = b''
644n/a
645n/aReturn a new SHA-224 hash object; optionally initialized with a string.
646n/a[clinic start generated code]*/
647n/a
648n/astatic PyObject *
649n/a_sha256_sha224_impl(PyObject *module, PyObject *string)
650n/a/*[clinic end generated code: output=21e3ba22c3404f93 input=27a04ba24c353a73]*/
651n/a{
652n/a SHAobject *new;
653n/a Py_buffer buf;
654n/a
655n/a if (string)
656n/a GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
657n/a
658n/a if ((new = newSHA224object()) == NULL) {
659n/a if (string)
660n/a PyBuffer_Release(&buf);
661n/a return NULL;
662n/a }
663n/a
664n/a sha224_init(new);
665n/a
666n/a if (PyErr_Occurred()) {
667n/a Py_DECREF(new);
668n/a if (string)
669n/a PyBuffer_Release(&buf);
670n/a return NULL;
671n/a }
672n/a if (string) {
673n/a sha_update(new, buf.buf, buf.len);
674n/a PyBuffer_Release(&buf);
675n/a }
676n/a
677n/a return (PyObject *)new;
678n/a}
679n/a
680n/a
681n/a/* List of functions exported by this module */
682n/a
683n/astatic struct PyMethodDef SHA_functions[] = {
684n/a _SHA256_SHA256_METHODDEF
685n/a _SHA256_SHA224_METHODDEF
686n/a {NULL, NULL} /* Sentinel */
687n/a};
688n/a
689n/a
690n/a/* Initialize this module. */
691n/a
692n/a#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
693n/a
694n/a
695n/astatic struct PyModuleDef _sha256module = {
696n/a PyModuleDef_HEAD_INIT,
697n/a "_sha256",
698n/a NULL,
699n/a -1,
700n/a SHA_functions,
701n/a NULL,
702n/a NULL,
703n/a NULL,
704n/a NULL
705n/a};
706n/a
707n/aPyMODINIT_FUNC
708n/aPyInit__sha256(void)
709n/a{
710n/a PyObject *m;
711n/a
712n/a Py_TYPE(&SHA224type) = &PyType_Type;
713n/a if (PyType_Ready(&SHA224type) < 0)
714n/a return NULL;
715n/a Py_TYPE(&SHA256type) = &PyType_Type;
716n/a if (PyType_Ready(&SHA256type) < 0)
717n/a return NULL;
718n/a
719n/a m = PyModule_Create(&_sha256module);
720n/a if (m == NULL)
721n/a return NULL;
722n/a
723n/a Py_INCREF((PyObject *)&SHA224type);
724n/a PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type);
725n/a Py_INCREF((PyObject *)&SHA256type);
726n/a PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type);
727n/a return m;
728n/a
729n/a}