1f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* SHA256 module */
2f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
3f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* This module provides an interface to NIST's SHA-256 and SHA-224 Algorithms */
4f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
5f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* See below for information about the original code this module was
6f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith   based upon. Additional work performed by:
7f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
8f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith   Andrew Kuchling (amk@amk.ca)
9f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith   Greg Stein (gstein@lyra.org)
10f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith   Trevor Perrin (trevp@trevp.net)
11f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
122f21eb3a153ee09e333300a837be86a37cd22d28Gregory P. Smith   Copyright (C) 2005-2007   Gregory P. Smith (greg@krypto.org)
13f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith   Licensed to PSF under a Contributor Agreement.
14f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
15f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith*/
16f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
17f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* SHA objects */
18f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
19f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#include "Python.h"
20f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#include "structmember.h"
21365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith#include "hashlib.h"
228cb6569fe14ba8e57ab1a2bea68594747852a9d1Gregory P. Smith#include "pystrhex.h"
23f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
24501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis/*[clinic input]
25501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwismodule _sha256
26501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwisclass SHA256Type "SHAobject *" "&PyType_Type"
27501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis[clinic start generated code]*/
28501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis/*[clinic end generated code: output=da39a3ee5e6b4b0d input=71a39174d4f0a744]*/
29f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
30f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* Some useful types */
31f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
32f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithtypedef unsigned char SHA_BYTE;
33f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
34f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#if SIZEOF_INT == 4
35f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitroutypedef unsigned int SHA_INT32; /* 32-bit integer */
36f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#else
37f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* not defined. compilation will die. */
38f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#endif
39f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
40f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* The SHA block size and message digest sizes, in bytes */
41f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
42f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define SHA_BLOCKSIZE    64
43f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define SHA_DIGESTSIZE  32
44f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
45f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* The structure for storing SHA info */
46f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
47f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithtypedef struct {
48f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    PyObject_HEAD
49f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_INT32 digest[8];                /* Message digest */
50f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_INT32 count_lo, count_hi;       /* 64-bit bit count */
51f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_BYTE data[SHA_BLOCKSIZE];       /* SHA data buffer */
52f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    int local;                          /* unprocessed amount in data */
53f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    int digestsize;
54f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith} SHAobject;
55f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
561009bf18b38a8d36298575191dd8fdf43f8f9097Serhiy Storchaka#include "clinic/sha256module.c.h"
571009bf18b38a8d36298575191dd8fdf43f8f9097Serhiy Storchaka
58f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* When run on a little-endian CPU we need to perform byte reversal on an
59f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith   array of longwords. */
60f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
61743e0cd6b5d59767aae2524700857f188ca1e80eChristian Heimes#if PY_LITTLE_ENDIAN
62743e0cd6b5d59767aae2524700857f188ca1e80eChristian Heimesstatic void longReverse(SHA_INT32 *buffer, int byteCount)
63f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
64f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHA_INT32 value;
65f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
66f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    byteCount /= sizeof(*buffer);
67f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    while (byteCount--) {
68f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        value = *buffer;
69f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        value = ( ( value & 0xFF00FF00L ) >> 8  ) | \
70f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith                ( ( value & 0x00FF00FFL ) << 8 );
71f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        *buffer++ = ( value << 16 ) | ( value >> 16 );
72f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
73f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
74743e0cd6b5d59767aae2524700857f188ca1e80eChristian Heimes#endif
75f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
76f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic void SHAcopy(SHAobject *src, SHAobject *dest)
77f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
78f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    dest->local = src->local;
79f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    dest->digestsize = src->digestsize;
80f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    dest->count_lo = src->count_lo;
81f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    dest->count_hi = src->count_hi;
82f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    memcpy(dest->digest, src->digest, sizeof(src->digest));
83f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    memcpy(dest->data, src->data, sizeof(src->data));
84f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
85f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
86f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
87f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* ------------------------------------------------------------------------
88f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith *
89f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * This code for the SHA-256 algorithm was noted as public domain. The
90f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * original headers are pasted below.
91f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith *
92f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * Several changes have been made to make it more compatible with the
93f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * Python environment and desired interface.
94f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith *
95f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith */
96f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
97f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* LibTomCrypt, modular cryptographic library -- Tom St Denis
98f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith *
99f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * LibTomCrypt is a library that provides various cryptographic
100f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * algorithms in a highly modular and flexible manner.
101f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith *
102f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * The library is free for all purposes without any express
10346f50726a0047ae81d478c3a206f587b8f35ed08Martin Panter * guarantee it works.
104f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith *
1052f21eb3a153ee09e333300a837be86a37cd22d28Gregory P. Smith * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
106f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith */
107f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
108f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
109f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* SHA256 by Tom St Denis */
110f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
111f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* Various logical functions */
112f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define ROR(x, y)\
113f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
114f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
115f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define Ch(x,y,z)       (z ^ (x & (y ^ z)))
116f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou#define Maj(x,y,z)      (((x | y) & z) | (x & y))
117f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define S(x, n)         ROR((x),(n))
118f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define R(x, n)         (((x)&0xFFFFFFFFUL)>>(n))
119f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define Sigma0(x)       (S(x, 2) ^ S(x, 13) ^ S(x, 22))
120f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define Sigma1(x)       (S(x, 6) ^ S(x, 11) ^ S(x, 25))
121f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define Gamma0(x)       (S(x, 7) ^ S(x, 18) ^ R(x, 3))
122f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define Gamma1(x)       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
123f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
124f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
125f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic void
126f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithsha_transform(SHAobject *sha_info)
127f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
128f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    int i;
129f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        SHA_INT32 S[8], W[64], t0, t1;
130f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
131f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    memcpy(W, sha_info->data, sizeof(sha_info->data));
132743e0cd6b5d59767aae2524700857f188ca1e80eChristian Heimes#if PY_LITTLE_ENDIAN
133743e0cd6b5d59767aae2524700857f188ca1e80eChristian Heimes    longReverse(W, (int)sizeof(sha_info->data));
134743e0cd6b5d59767aae2524700857f188ca1e80eChristian Heimes#endif
135f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
136f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    for (i = 16; i < 64; ++i) {
137f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou                W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
138f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
139f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    for (i = 0; i < 8; ++i) {
140f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        S[i] = sha_info->digest[i];
141f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
142f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
143f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    /* Compress */
144f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define RND(a,b,c,d,e,f,g,h,i,ki)                    \
145f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i];   \
146f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     t1 = Sigma0(a) + Maj(a, b, c);                  \
147f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     d += t0;                                        \
148f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     h  = t0 + t1;
149f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
150f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
151f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
152f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
153f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
154f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
155f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
156f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
157f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
158f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
159f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
160f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
161f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
162f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
163f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
164f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
165f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
166f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
167f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
168f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
169f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
170f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
171f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
172f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
173f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
174f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
175f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
176f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
177f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
178f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
179f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
180f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
181f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
182f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
183f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
184f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
185f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
186f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
187f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
188f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
189f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
190f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
191f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
192f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
193f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
194f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
195f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
196f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
197f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
198f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
199f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
200f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
201f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
202f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
203f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
204f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
205f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
206f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
207f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
208f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
209f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
210f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
211f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
212f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
213f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
214f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
215f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou#undef RND
216f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou
217f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    /* feedback */
218f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    for (i = 0; i < 8; i++) {
219f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        sha_info->digest[i] = sha_info->digest[i] + S[i];
220f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
221f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
222f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
223f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
224f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
225f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
226f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* initialize the SHA digest */
227f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
228f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic void
229f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithsha_init(SHAobject *sha_info)
230f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
231f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[0] = 0x6A09E667L;
232f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[1] = 0xBB67AE85L;
233f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[2] = 0x3C6EF372L;
234f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[3] = 0xA54FF53AL;
235f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[4] = 0x510E527FL;
236f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[5] = 0x9B05688CL;
237f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[6] = 0x1F83D9ABL;
238f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[7] = 0x5BE0CD19L;
239f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->count_lo = 0L;
240f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->count_hi = 0L;
241f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->local = 0;
242f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digestsize = 32;
243f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
244f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
245f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic void
246f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithsha224_init(SHAobject *sha_info)
247f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
248f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[0] = 0xc1059ed8L;
249f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[1] = 0x367cd507L;
250f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[2] = 0x3070dd17L;
251f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[3] = 0xf70e5939L;
252f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[4] = 0xffc00b31L;
253f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[5] = 0x68581511L;
254f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[6] = 0x64f98fa7L;
255f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digest[7] = 0xbefa4fa4L;
256f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->count_lo = 0L;
257f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->count_hi = 0L;
258f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->local = 0;
259f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->digestsize = 28;
260f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
261f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
262f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
263f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* update the SHA digest */
264f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
265f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic void
2660fcab4a3ed5e39769609b60d6179c4c801e45985Victor Stinnersha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
267f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
2680fcab4a3ed5e39769609b60d6179c4c801e45985Victor Stinner    Py_ssize_t i;
269f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHA_INT32 clo;
270f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
271f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    clo = sha_info->count_lo + ((SHA_INT32) count << 3);
272f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    if (clo < sha_info->count_lo) {
273f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        ++sha_info->count_hi;
274f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
275f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->count_lo = clo;
276f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->count_hi += (SHA_INT32) count >> 29;
277f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    if (sha_info->local) {
278f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        i = SHA_BLOCKSIZE - sha_info->local;
279f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        if (i > count) {
280f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith            i = count;
281f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        }
282f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
283f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        count -= i;
284f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        buffer += i;
28570792d268eaa8f460ac32d8084171a939f19f14bVictor Stinner        sha_info->local += (int)i;
286f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        if (sha_info->local == SHA_BLOCKSIZE) {
287f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith            sha_transform(sha_info);
288f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        }
289f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        else {
290f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith            return;
291f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        }
292f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
293f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    while (count >= SHA_BLOCKSIZE) {
294f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
295f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        buffer += SHA_BLOCKSIZE;
296f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        count -= SHA_BLOCKSIZE;
297f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        sha_transform(sha_info);
298f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
299f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    memcpy(sha_info->data, buffer, count);
30070792d268eaa8f460ac32d8084171a939f19f14bVictor Stinner    sha_info->local = (int)count;
301f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
302f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
303f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* finish computing the SHA digest */
304f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
305f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic void
306f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithsha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
307f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
308f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    int count;
309f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHA_INT32 lo_bit_count, hi_bit_count;
310f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
311f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    lo_bit_count = sha_info->count_lo;
312f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    hi_bit_count = sha_info->count_hi;
313f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    count = (int) ((lo_bit_count >> 3) & 0x3f);
314f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
315f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    if (count > SHA_BLOCKSIZE - 8) {
316f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        memset(((SHA_BYTE *) sha_info->data) + count, 0,
317f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou               SHA_BLOCKSIZE - count);
318f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        sha_transform(sha_info);
319f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
320f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
321f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    else {
322f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        memset(((SHA_BYTE *) sha_info->data) + count, 0,
323f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou               SHA_BLOCKSIZE - 8 - count);
324f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
325f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
326f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    /* GJS: note that we add the hi/lo in big-endian. sha_transform will
327f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith       swap these values into host-order. */
328f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
329f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
330f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->data[58] = (hi_bit_count >>  8) & 0xff;
331f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->data[59] = (hi_bit_count >>  0) & 0xff;
332f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
333f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
334f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->data[62] = (lo_bit_count >>  8) & 0xff;
335f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_info->data[63] = (lo_bit_count >>  0) & 0xff;
336f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_transform(sha_info);
337f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
338f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
339f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 2] = (unsigned char) ((sha_info->digest[0] >>  8) & 0xff);
340f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 3] = (unsigned char) ((sha_info->digest[0]      ) & 0xff);
341f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
342f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
343f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 6] = (unsigned char) ((sha_info->digest[1] >>  8) & 0xff);
344f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 7] = (unsigned char) ((sha_info->digest[1]      ) & 0xff);
345f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
346f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
347f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[10] = (unsigned char) ((sha_info->digest[2] >>  8) & 0xff);
348f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[11] = (unsigned char) ((sha_info->digest[2]      ) & 0xff);
349f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
350f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
351f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[14] = (unsigned char) ((sha_info->digest[3] >>  8) & 0xff);
352f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[15] = (unsigned char) ((sha_info->digest[3]      ) & 0xff);
353f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
354f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
355f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[18] = (unsigned char) ((sha_info->digest[4] >>  8) & 0xff);
356f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[19] = (unsigned char) ((sha_info->digest[4]      ) & 0xff);
357f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
358f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
359f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[22] = (unsigned char) ((sha_info->digest[5] >>  8) & 0xff);
360f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[23] = (unsigned char) ((sha_info->digest[5]      ) & 0xff);
361f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
362f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
363f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[26] = (unsigned char) ((sha_info->digest[6] >>  8) & 0xff);
364f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[27] = (unsigned char) ((sha_info->digest[6]      ) & 0xff);
365f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
366f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
367f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[30] = (unsigned char) ((sha_info->digest[7] >>  8) & 0xff);
368f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    digest[31] = (unsigned char) ((sha_info->digest[7]      ) & 0xff);
369f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
370f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
371f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/*
372f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * End of copied SHA code.
373f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith *
374f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith * ------------------------------------------------------------------------
375f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith */
376f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
377f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyTypeObject SHA224type;
378f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyTypeObject SHA256type;
379f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
380f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
381f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic SHAobject *
382f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. SmithnewSHA224object(void)
383f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
384f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    return (SHAobject *)PyObject_New(SHAobject, &SHA224type);
385f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
386f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
387f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic SHAobject *
388f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. SmithnewSHA256object(void)
389f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
390f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    return (SHAobject *)PyObject_New(SHAobject, &SHA256type);
391f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
392f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
393f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* Internal methods for a hash object */
394f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
395f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic void
396f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. SmithSHA_dealloc(PyObject *ptr)
397f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
398f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    PyObject_Del(ptr);
399f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
400f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
401f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
402f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* External methods for a hash object */
403f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
404501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis/*[clinic input]
405501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisSHA256Type.copy
406501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
407501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisReturn a copy of the hash object.
408501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis[clinic start generated code]*/
409501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
410501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwisstatic PyObject *
411501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisSHA256Type_copy_impl(SHAobject *self)
4121009bf18b38a8d36298575191dd8fdf43f8f9097Serhiy Storchaka/*[clinic end generated code: output=1a8bbd66a0c9c168 input=f58840a618d4f2a7]*/
413f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
414f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHAobject *newobj;
415f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
41690aa7646affbaee9628ca6ea6a702aec17b3b550Christian Heimes    if (Py_TYPE(self) == &SHA256type) {
417f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        if ( (newobj = newSHA256object())==NULL)
418f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith            return NULL;
419f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    } else {
420f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        if ( (newobj = newSHA224object())==NULL)
421f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith            return NULL;
422f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
423f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
424f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHAcopy(self, newobj);
425f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    return (PyObject *)newobj;
426f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
427f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
428501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis/*[clinic input]
429501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisSHA256Type.digest
430501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
431501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisReturn the digest value as a string of binary data.
432501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis[clinic start generated code]*/
433501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
434f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyObject *
435501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisSHA256Type_digest_impl(SHAobject *self)
4361009bf18b38a8d36298575191dd8fdf43f8f9097Serhiy Storchaka/*[clinic end generated code: output=46616a5e909fbc3d input=1fb752e58954157d]*/
437f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
438f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    unsigned char digest[SHA_DIGESTSIZE];
439f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHAobject temp;
440f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
441f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHAcopy(self, &temp);
442f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_final(digest, &temp);
44372b710a59617ebe6dd1c41613d2c7eb81702efd9Christian Heimes    return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
444f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
445f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
446501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis/*[clinic input]
447501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisSHA256Type.hexdigest
448501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
449501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisReturn the digest value as a string of hexadecimal digits.
450501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis[clinic start generated code]*/
451501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
452501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwisstatic PyObject *
453501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisSHA256Type_hexdigest_impl(SHAobject *self)
4541009bf18b38a8d36298575191dd8fdf43f8f9097Serhiy Storchaka/*[clinic end generated code: output=725f8a7041ae97f3 input=0cc4c714693010d1]*/
455f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
456f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    unsigned char digest[SHA_DIGESTSIZE];
457f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHAobject temp;
458f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
459f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    /* Get the raw (binary) digest value */
460f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHAcopy(self, &temp);
461f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_final(digest, &temp);
462f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
4638cb6569fe14ba8e57ab1a2bea68594747852a9d1Gregory P. Smith    return _Py_strhex((const char *)digest, self->digestsize);
464f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
465f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
466501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis/*[clinic input]
467501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisSHA256Type.update
468501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
469501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    obj: object
470501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    /
471501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
472501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisUpdate this hash object's state with the provided string.
473501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis[clinic start generated code]*/
474501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
475f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyObject *
476501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisSHA256Type_update(SHAobject *self, PyObject *obj)
4771009bf18b38a8d36298575191dd8fdf43f8f9097Serhiy Storchaka/*[clinic end generated code: output=0967fb2860c66af7 input=b2d449d5b30f0f5a]*/
478f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
479365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith    Py_buffer buf;
480f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
481365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith    GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
482f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
483365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith    sha_update(self, buf.buf, buf.len);
484365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith
485365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith    PyBuffer_Release(&buf);
486f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    Py_INCREF(Py_None);
487f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    return Py_None;
488f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
489f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
490f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyMethodDef SHA_methods[] = {
491501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    SHA256TYPE_COPY_METHODDEF
492501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    SHA256TYPE_DIGEST_METHODDEF
493501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    SHA256TYPE_HEXDIGEST_METHODDEF
494501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    SHA256TYPE_UPDATE_METHODDEF
495f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    {NULL,        NULL}         /* sentinel */
496f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith};
497f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
498f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyObject *
499f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. SmithSHA256_get_block_size(PyObject *self, void *closure)
500f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
501217cfd1c86c59ed8a55ce6d6b88bbe37309e7ba2Christian Heimes    return PyLong_FromLong(SHA_BLOCKSIZE);
502f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
503f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
504f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyObject *
505f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. SmithSHA256_get_name(PyObject *self, void *closure)
506f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
507f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    if (((SHAobject *)self)->digestsize == 32)
50837d5cebb4816a96785a85db9dcfd8519f30df18bChristian Heimes        return PyUnicode_FromStringAndSize("sha256", 6);
509f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    else
51037d5cebb4816a96785a85db9dcfd8519f30df18bChristian Heimes        return PyUnicode_FromStringAndSize("sha224", 6);
511f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
512f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
513f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyGetSetDef SHA_getseters[] = {
514f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    {"block_size",
515f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     (getter)SHA256_get_block_size, NULL,
516f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     NULL,
517f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     NULL},
518f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    {"name",
519f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     (getter)SHA256_get_name, NULL,
520f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     NULL,
521f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith     NULL},
522f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    {NULL}  /* Sentinel */
523f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith};
524f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
525f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyMemberDef SHA_members[] = {
526f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
527f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    {NULL}  /* Sentinel */
528f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith};
529f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
530f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyTypeObject SHA224type = {
5319f2e346911988cda95fec7c901e8d10d34fa9563Martin v. Löwis    PyVarObject_HEAD_INIT(NULL, 0)
532f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    "_sha256.sha224",   /*tp_name*/
533f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    sizeof(SHAobject),  /*tp_size*/
534f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_itemsize*/
535f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    /* methods */
536f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_dealloc,        /*tp_dealloc*/
537f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_print*/
538f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_getattr*/
539f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_setattr*/
540e94c679df0b632bc929936ca54f0de006e1a6dc2Mark Dickinson    0,                  /*tp_reserved*/
541f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_repr*/
542f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_as_number*/
543f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_as_sequence*/
544f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_as_mapping*/
545f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_hash*/
546f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_call*/
547f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_str*/
548f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_getattro*/
549f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_setattro*/
550f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_as_buffer*/
551f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    Py_TPFLAGS_DEFAULT, /*tp_flags*/
552f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_doc*/
553f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_traverse*/
554f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_clear*/
555f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_richcompare*/
556f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_weaklistoffset*/
557f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_iter*/
558f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_iternext*/
559f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_methods,        /* tp_methods */
560f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_members,        /* tp_members */
561f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHA_getseters,      /* tp_getset */
562f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith};
563f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
564f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic PyTypeObject SHA256type = {
5659f2e346911988cda95fec7c901e8d10d34fa9563Martin v. Löwis    PyVarObject_HEAD_INIT(NULL, 0)
566f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    "_sha256.sha256",   /*tp_name*/
567f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    sizeof(SHAobject),  /*tp_size*/
568f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_itemsize*/
569f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    /* methods */
570f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_dealloc,        /*tp_dealloc*/
571f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_print*/
572f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_getattr*/
573f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_setattr*/
574e94c679df0b632bc929936ca54f0de006e1a6dc2Mark Dickinson    0,                  /*tp_reserved*/
575f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_repr*/
576f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_as_number*/
577f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_as_sequence*/
578f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_as_mapping*/
579f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_hash*/
580f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_call*/
581f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_str*/
582f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_getattro*/
583f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_setattro*/
584f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_as_buffer*/
585f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    Py_TPFLAGS_DEFAULT, /*tp_flags*/
586f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_doc*/
587f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    0,                  /*tp_traverse*/
588f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_clear*/
589f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_richcompare*/
590f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_weaklistoffset*/
591f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_iter*/
592f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    0,                  /*tp_iternext*/
593f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_methods,        /* tp_methods */
594f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    SHA_members,        /* tp_members */
595f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHA_getseters,      /* tp_getset */
596f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith};
597f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
598f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
599f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* The single module-level function: new() */
600f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
601501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis/*[clinic input]
602501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis_sha256.sha256
603501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
604501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    string: object(c_default="NULL") = b''
605501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
606501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisReturn a new SHA-256 hash object; optionally initialized with a string.
607501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis[clinic start generated code]*/
608501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
609501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwisstatic PyObject *
6101a2b24f02dfd4eb3383f6ae2b59e5a4eb66fd5bbSerhiy Storchaka_sha256_sha256_impl(PyObject *module, PyObject *string)
6111a2b24f02dfd4eb3383f6ae2b59e5a4eb66fd5bbSerhiy Storchaka/*[clinic end generated code: output=fa644436dcea5c31 input=09cce3fb855056b2]*/
612f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
613f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHAobject *new;
614365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith    Py_buffer buf;
615f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
616501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    if (string)
617501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis        GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
618365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith
6198404749e4b65f23a7d84c3940ea061eee0b4b344Hirokazu Yamamoto    if ((new = newSHA256object()) == NULL) {
620501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis        if (string)
6218404749e4b65f23a7d84c3940ea061eee0b4b344Hirokazu Yamamoto            PyBuffer_Release(&buf);
622f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        return NULL;
6238404749e4b65f23a7d84c3940ea061eee0b4b344Hirokazu Yamamoto    }
624f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
625f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha_init(new);
626f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
627f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    if (PyErr_Occurred()) {
628f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        Py_DECREF(new);
629501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis        if (string)
6308404749e4b65f23a7d84c3940ea061eee0b4b344Hirokazu Yamamoto            PyBuffer_Release(&buf);
631f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        return NULL;
632f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
633501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    if (string) {
634365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith        sha_update(new, buf.buf, buf.len);
635365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith        PyBuffer_Release(&buf);
636365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith    }
637f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
638f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    return (PyObject *)new;
639f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
640f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
641501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis/*[clinic input]
642501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis_sha256.sha224
643501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
644501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    string: object(c_default="NULL") = b''
645501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
646501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. LöwisReturn a new SHA-224 hash object; optionally initialized with a string.
647501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis[clinic start generated code]*/
648501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis
649501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwisstatic PyObject *
6501a2b24f02dfd4eb3383f6ae2b59e5a4eb66fd5bbSerhiy Storchaka_sha256_sha224_impl(PyObject *module, PyObject *string)
6511a2b24f02dfd4eb3383f6ae2b59e5a4eb66fd5bbSerhiy Storchaka/*[clinic end generated code: output=21e3ba22c3404f93 input=27a04ba24c353a73]*/
652f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
653f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    SHAobject *new;
654365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith    Py_buffer buf;
655f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
656501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    if (string)
657501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis        GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
658365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith
6598404749e4b65f23a7d84c3940ea061eee0b4b344Hirokazu Yamamoto    if ((new = newSHA224object()) == NULL) {
660501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis        if (string)
6618404749e4b65f23a7d84c3940ea061eee0b4b344Hirokazu Yamamoto            PyBuffer_Release(&buf);
662f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        return NULL;
6638404749e4b65f23a7d84c3940ea061eee0b4b344Hirokazu Yamamoto    }
664f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
665f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    sha224_init(new);
666f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
667f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    if (PyErr_Occurred()) {
668f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        Py_DECREF(new);
669501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis        if (string)
6708404749e4b65f23a7d84c3940ea061eee0b4b344Hirokazu Yamamoto            PyBuffer_Release(&buf);
671f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith        return NULL;
672f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    }
673501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    if (string) {
674365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith        sha_update(new, buf.buf, buf.len);
675365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith        PyBuffer_Release(&buf);
676365a1864fd285fc6ee9d9fa1f8770b39d4dab830Gregory P. Smith    }
677f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
678f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    return (PyObject *)new;
679f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
680f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
681f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
682f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* List of functions exported by this module */
683f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
684f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smithstatic struct PyMethodDef SHA_functions[] = {
685501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    _SHA256_SHA256_METHODDEF
686501b13c6229f740dd92a0067fdb5486bf2ff1a57Martin v. Löwis    _SHA256_SHA224_METHODDEF
687f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou    {NULL,      NULL}            /* Sentinel */
688f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith};
689f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
690f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
691f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith/* Initialize this module. */
692f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
693f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
694f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith
6951a21451b1d73b65af949193208372e86bf308411Martin v. Löwis
6961a21451b1d73b65af949193208372e86bf308411Martin v. Löwisstatic struct PyModuleDef _sha256module = {
697f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        PyModuleDef_HEAD_INIT,
698f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        "_sha256",
699f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        NULL,
700f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        -1,
701f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        SHA_functions,
702f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        NULL,
703f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        NULL,
704f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        NULL,
705f95a1b3c53bdd678b64aa608d4375660033460c3Antoine Pitrou        NULL
7061a21451b1d73b65af949193208372e86bf308411Martin v. Löwis};
7071a21451b1d73b65af949193208372e86bf308411Martin v. Löwis
708f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. SmithPyMODINIT_FUNC
7091a21451b1d73b65af949193208372e86bf308411Martin v. LöwisPyInit__sha256(void)
710f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith{
711327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes    PyObject *m;
712327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes
71390aa7646affbaee9628ca6ea6a702aec17b3b550Christian Heimes    Py_TYPE(&SHA224type) = &PyType_Type;
714f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    if (PyType_Ready(&SHA224type) < 0)
7151a21451b1d73b65af949193208372e86bf308411Martin v. Löwis        return NULL;
71690aa7646affbaee9628ca6ea6a702aec17b3b550Christian Heimes    Py_TYPE(&SHA256type) = &PyType_Type;
717f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith    if (PyType_Ready(&SHA256type) < 0)
7181a21451b1d73b65af949193208372e86bf308411Martin v. Löwis        return NULL;
719327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes
720327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes    m = PyModule_Create(&_sha256module);
721327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes    if (m == NULL)
722327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes        return NULL;
723327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes
724327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes    Py_INCREF((PyObject *)&SHA224type);
725327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes    PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type);
726327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes    Py_INCREF((PyObject *)&SHA256type);
727327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes    PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type);
728327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes    return m;
729327dd732ce2b7df001b0a052e3464c85f0c85128Christian Heimes
730f21a5f773964d34c7b6deb7e3d753fae2b9c70e2Gregory P. Smith}
731