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