Line data Source code
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]
29 : module _sha256
30 : class SHA256Type "SHAobject *" "&PyType_Type"
31 : [clinic start generated code]*/
32 : /*[clinic end generated code: output=da39a3ee5e6b4b0d input=71a39174d4f0a744]*/
33 :
34 : /* Some useful types */
35 :
36 : typedef unsigned char SHA_BYTE;
37 : typedef 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 :
46 : typedef 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 :
57 : typedef struct {
58 : PyTypeObject* sha224_type;
59 : PyTypeObject* sha256_type;
60 : } _sha256_state;
61 :
62 : static inline _sha256_state*
63 63 : _sha256_get_state(PyObject *module)
64 : {
65 63 : void *state = PyModule_GetState(module);
66 63 : assert(state != NULL);
67 63 : 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
74 94266 : static void longReverse(SHA_INT32 *buffer, int byteCount)
75 : {
76 94266 : byteCount /= sizeof(*buffer);
77 1602520 : for (; byteCount--; buffer++) {
78 1508260 : *buffer = _Py_bswap32(*buffer);
79 : }
80 94266 : }
81 : #endif
82 :
83 67 : static void SHAcopy(SHAobject *src, SHAobject *dest)
84 : {
85 67 : dest->local = src->local;
86 67 : dest->digestsize = src->digestsize;
87 67 : dest->count_lo = src->count_lo;
88 67 : dest->count_hi = src->count_hi;
89 67 : memcpy(dest->digest, src->digest, sizeof(src->digest));
90 67 : memcpy(dest->data, src->data, sizeof(src->data));
91 67 : }
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 :
132 : static void
133 94266 : sha_transform(SHAobject *sha_info)
134 : {
135 : int i;
136 : SHA_INT32 S[8], W[64], t0, t1;
137 :
138 94266 : memcpy(W, sha_info->data, sizeof(sha_info->data));
139 : #if PY_LITTLE_ENDIAN
140 94266 : longReverse(W, (int)sizeof(sha_info->data));
141 : #endif
142 :
143 4619030 : for (i = 16; i < 64; ++i) {
144 4524770 : W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
145 : }
146 848394 : for (i = 0; i < 8; ++i) {
147 754128 : 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 94266 : RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
158 94266 : RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
159 94266 : RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
160 94266 : RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
161 94266 : RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
162 94266 : RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
163 94266 : RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
164 94266 : RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
165 94266 : RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
166 94266 : RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
167 94266 : RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
168 94266 : RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
169 94266 : RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
170 94266 : RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
171 94266 : RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
172 94266 : RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
173 94266 : RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
174 94266 : RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
175 94266 : RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
176 94266 : RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
177 94266 : RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
178 94266 : RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
179 94266 : RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
180 94266 : RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
181 94266 : RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
182 94266 : RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
183 94266 : RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
184 94266 : RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
185 94266 : RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
186 94266 : RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
187 94266 : RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
188 94266 : RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
189 94266 : RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
190 94266 : RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
191 94266 : RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
192 94266 : RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
193 94266 : RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
194 94266 : RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
195 94266 : RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
196 94266 : RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
197 94266 : RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
198 94266 : RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
199 94266 : RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
200 94266 : RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
201 94266 : RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
202 94266 : RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
203 94266 : RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
204 94266 : RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
205 94266 : RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
206 94266 : RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
207 94266 : RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
208 94266 : RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
209 94266 : RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
210 94266 : RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
211 94266 : RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
212 94266 : RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
213 94266 : RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
214 94266 : RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
215 94266 : RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
216 94266 : RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
217 94266 : RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
218 94266 : RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
219 94266 : RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
220 94266 : 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 848394 : for (i = 0; i < 8; i++) {
226 754128 : sha_info->digest[i] = sha_info->digest[i] + S[i];
227 : }
228 :
229 94266 : }
230 :
231 :
232 :
233 : /* initialize the SHA digest */
234 :
235 : static void
236 33 : sha_init(SHAobject *sha_info)
237 : {
238 33 : sha_info->digest[0] = 0x6A09E667L;
239 33 : sha_info->digest[1] = 0xBB67AE85L;
240 33 : sha_info->digest[2] = 0x3C6EF372L;
241 33 : sha_info->digest[3] = 0xA54FF53AL;
242 33 : sha_info->digest[4] = 0x510E527FL;
243 33 : sha_info->digest[5] = 0x9B05688CL;
244 33 : sha_info->digest[6] = 0x1F83D9ABL;
245 33 : sha_info->digest[7] = 0x5BE0CD19L;
246 33 : sha_info->count_lo = 0L;
247 33 : sha_info->count_hi = 0L;
248 33 : sha_info->local = 0;
249 33 : sha_info->digestsize = 32;
250 33 : }
251 :
252 : static void
253 29 : sha224_init(SHAobject *sha_info)
254 : {
255 29 : sha_info->digest[0] = 0xc1059ed8L;
256 29 : sha_info->digest[1] = 0x367cd507L;
257 29 : sha_info->digest[2] = 0x3070dd17L;
258 29 : sha_info->digest[3] = 0xf70e5939L;
259 29 : sha_info->digest[4] = 0xffc00b31L;
260 29 : sha_info->digest[5] = 0x68581511L;
261 29 : sha_info->digest[6] = 0x64f98fa7L;
262 29 : sha_info->digest[7] = 0xbefa4fa4L;
263 29 : sha_info->count_lo = 0L;
264 29 : sha_info->count_hi = 0L;
265 29 : sha_info->local = 0;
266 29 : sha_info->digestsize = 28;
267 29 : }
268 :
269 :
270 : /* update the SHA digest */
271 :
272 : static void
273 68 : sha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
274 : {
275 : Py_ssize_t i;
276 : SHA_INT32 clo;
277 :
278 68 : clo = sha_info->count_lo + ((SHA_INT32) count << 3);
279 68 : if (clo < sha_info->count_lo) {
280 0 : ++sha_info->count_hi;
281 : }
282 68 : sha_info->count_lo = clo;
283 68 : sha_info->count_hi += (SHA_INT32) count >> 29;
284 68 : if (sha_info->local) {
285 10 : i = SHA_BLOCKSIZE - sha_info->local;
286 10 : if (i > count) {
287 2 : i = count;
288 : }
289 10 : memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
290 10 : count -= i;
291 10 : buffer += i;
292 10 : sha_info->local += (int)i;
293 10 : if (sha_info->local == SHA_BLOCKSIZE) {
294 8 : sha_transform(sha_info);
295 : }
296 : else {
297 2 : return;
298 : }
299 : }
300 94236 : while (count >= SHA_BLOCKSIZE) {
301 94170 : memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
302 94170 : buffer += SHA_BLOCKSIZE;
303 94170 : count -= SHA_BLOCKSIZE;
304 94170 : sha_transform(sha_info);
305 : }
306 66 : memcpy(sha_info->data, buffer, count);
307 66 : sha_info->local = (int)count;
308 : }
309 :
310 : /* finish computing the SHA digest */
311 :
312 : static void
313 64 : sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
314 : {
315 : int count;
316 : SHA_INT32 lo_bit_count, hi_bit_count;
317 :
318 64 : lo_bit_count = sha_info->count_lo;
319 64 : hi_bit_count = sha_info->count_hi;
320 64 : count = (int) ((lo_bit_count >> 3) & 0x3f);
321 64 : ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
322 64 : if (count > SHA_BLOCKSIZE - 8) {
323 24 : memset(((SHA_BYTE *) sha_info->data) + count, 0,
324 24 : SHA_BLOCKSIZE - count);
325 24 : sha_transform(sha_info);
326 24 : memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
327 : }
328 : else {
329 40 : memset(((SHA_BYTE *) sha_info->data) + count, 0,
330 40 : 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 64 : sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
336 64 : sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
337 64 : sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
338 64 : sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
339 64 : sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
340 64 : sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
341 64 : sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
342 64 : sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
343 64 : sha_transform(sha_info);
344 64 : digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
345 64 : digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
346 64 : digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
347 64 : digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
348 64 : digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
349 64 : digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
350 64 : digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
351 64 : digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
352 64 : digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
353 64 : digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
354 64 : digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
355 64 : digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
356 64 : digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
357 64 : digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
358 64 : digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
359 64 : digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
360 64 : digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
361 64 : digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
362 64 : digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
363 64 : digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
364 64 : digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
365 64 : digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
366 64 : digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
367 64 : digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
368 64 : digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
369 64 : digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
370 64 : digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
371 64 : digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
372 64 : digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
373 64 : digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
374 64 : digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
375 64 : digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
376 64 : }
377 :
378 : /*
379 : * End of copied SHA code.
380 : *
381 : * ------------------------------------------------------------------------
382 : */
383 :
384 :
385 : static SHAobject *
386 30 : newSHA224object(_sha256_state *state)
387 : {
388 30 : SHAobject *sha = (SHAobject *)PyObject_GC_New(SHAobject,
389 : state->sha224_type);
390 30 : PyObject_GC_Track(sha);
391 30 : return sha;
392 : }
393 :
394 : static SHAobject *
395 35 : newSHA256object(_sha256_state *state)
396 : {
397 35 : SHAobject *sha = (SHAobject *)PyObject_GC_New(SHAobject,
398 : state->sha256_type);
399 35 : PyObject_GC_Track(sha);
400 35 : return sha;
401 : }
402 :
403 : /* Internal methods for a hash object */
404 : static int
405 65 : SHA_traverse(PyObject *ptr, visitproc visit, void *arg)
406 : {
407 65 : Py_VISIT(Py_TYPE(ptr));
408 65 : return 0;
409 : }
410 :
411 : static void
412 65 : SHA_dealloc(PyObject *ptr)
413 : {
414 65 : PyTypeObject *tp = Py_TYPE(ptr);
415 65 : PyObject_GC_UnTrack(ptr);
416 65 : PyObject_GC_Del(ptr);
417 65 : Py_DECREF(tp);
418 65 : }
419 :
420 :
421 : /* External methods for a hash object */
422 :
423 : /*[clinic input]
424 : SHA256Type.copy
425 :
426 : cls:defining_class
427 :
428 : Return a copy of the hash object.
429 : [clinic start generated code]*/
430 :
431 : static PyObject *
432 3 : SHA256Type_copy_impl(SHAobject *self, PyTypeObject *cls)
433 : /*[clinic end generated code: output=9273f92c382be12f input=3137146fcb88e212]*/
434 : {
435 : SHAobject *newobj;
436 3 : _sha256_state *state = PyType_GetModuleState(cls);
437 3 : if (Py_IS_TYPE(self, state->sha256_type)) {
438 2 : if ( (newobj = newSHA256object(state)) == NULL) {
439 0 : return NULL;
440 : }
441 : } else {
442 1 : if ( (newobj = newSHA224object(state))==NULL) {
443 0 : return NULL;
444 : }
445 : }
446 :
447 3 : SHAcopy(self, newobj);
448 3 : return (PyObject *)newobj;
449 : }
450 :
451 : /*[clinic input]
452 : SHA256Type.digest
453 :
454 : Return the digest value as a bytes object.
455 : [clinic start generated code]*/
456 :
457 : static PyObject *
458 33 : SHA256Type_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 33 : SHAcopy(self, &temp);
465 33 : sha_final(digest, &temp);
466 33 : return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
467 : }
468 :
469 : /*[clinic input]
470 : SHA256Type.hexdigest
471 :
472 : Return the digest value as a string of hexadecimal digits.
473 : [clinic start generated code]*/
474 :
475 : static PyObject *
476 31 : SHA256Type_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 31 : SHAcopy(self, &temp);
484 31 : sha_final(digest, &temp);
485 :
486 31 : return _Py_strhex((const char *)digest, self->digestsize);
487 : }
488 :
489 : /*[clinic input]
490 : SHA256Type.update
491 :
492 : obj: object
493 : /
494 :
495 : Update this hash object's state with the provided string.
496 : [clinic start generated code]*/
497 :
498 : static PyObject *
499 48 : SHA256Type_update(SHAobject *self, PyObject *obj)
500 : /*[clinic end generated code: output=0967fb2860c66af7 input=b2d449d5b30f0f5a]*/
501 : {
502 : Py_buffer buf;
503 :
504 48 : GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
505 :
506 48 : sha_update(self, buf.buf, buf.len);
507 :
508 48 : PyBuffer_Release(&buf);
509 48 : Py_RETURN_NONE;
510 : }
511 :
512 : static 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 :
520 : static PyObject *
521 5 : SHA256_get_block_size(PyObject *self, void *closure)
522 : {
523 5 : return PyLong_FromLong(SHA_BLOCKSIZE);
524 : }
525 :
526 : static PyObject *
527 21 : SHA256_get_name(PyObject *self, void *closure)
528 : {
529 21 : if (((SHAobject *)self)->digestsize == 32)
530 11 : return PyUnicode_FromStringAndSize("sha256", 6);
531 : else
532 10 : return PyUnicode_FromStringAndSize("sha224", 6);
533 : }
534 :
535 : static 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 :
547 : static PyMemberDef SHA_members[] = {
548 : {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
549 : {NULL} /* Sentinel */
550 : };
551 :
552 : static 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 :
561 : static 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 :
569 : static 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 :
586 : Return a new SHA-256 hash object; optionally initialized with a string.
587 : [clinic start generated code]*/
588 :
589 : static PyObject *
590 34 : _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 34 : if (string) {
596 11 : GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
597 : }
598 :
599 33 : _sha256_state *state = PyModule_GetState(module);
600 :
601 : SHAobject *new;
602 33 : if ((new = newSHA256object(state)) == NULL) {
603 0 : if (string) {
604 0 : PyBuffer_Release(&buf);
605 : }
606 0 : return NULL;
607 : }
608 :
609 33 : sha_init(new);
610 :
611 33 : if (PyErr_Occurred()) {
612 0 : Py_DECREF(new);
613 0 : if (string) {
614 0 : PyBuffer_Release(&buf);
615 : }
616 0 : return NULL;
617 : }
618 33 : if (string) {
619 10 : sha_update(new, buf.buf, buf.len);
620 10 : PyBuffer_Release(&buf);
621 : }
622 :
623 33 : 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 :
633 : Return a new SHA-224 hash object; optionally initialized with a string.
634 : [clinic start generated code]*/
635 :
636 : static PyObject *
637 30 : _sha256_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
638 : /*[clinic end generated code: output=08be6b36569bc69c input=9fcfb46e460860ac]*/
639 : {
640 : Py_buffer buf;
641 30 : if (string) {
642 11 : GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
643 : }
644 :
645 29 : _sha256_state *state = PyModule_GetState(module);
646 : SHAobject *new;
647 29 : if ((new = newSHA224object(state)) == NULL) {
648 0 : if (string) {
649 0 : PyBuffer_Release(&buf);
650 : }
651 0 : return NULL;
652 : }
653 :
654 29 : sha224_init(new);
655 :
656 29 : if (PyErr_Occurred()) {
657 0 : Py_DECREF(new);
658 0 : if (string) {
659 0 : PyBuffer_Release(&buf);
660 : }
661 0 : return NULL;
662 : }
663 29 : if (string) {
664 10 : sha_update(new, buf.buf, buf.len);
665 10 : PyBuffer_Release(&buf);
666 : }
667 :
668 29 : return (PyObject *)new;
669 : }
670 :
671 :
672 : /* List of functions exported by this module */
673 :
674 : static struct PyMethodDef SHA_functions[] = {
675 : _SHA256_SHA256_METHODDEF
676 : _SHA256_SHA224_METHODDEF
677 : {NULL, NULL} /* Sentinel */
678 : };
679 :
680 : static int
681 54 : _sha256_traverse(PyObject *module, visitproc visit, void *arg)
682 : {
683 54 : _sha256_state *state = _sha256_get_state(module);
684 54 : Py_VISIT(state->sha224_type);
685 54 : Py_VISIT(state->sha256_type);
686 54 : return 0;
687 : }
688 :
689 : static int
690 6 : _sha256_clear(PyObject *module)
691 : {
692 6 : _sha256_state *state = _sha256_get_state(module);
693 6 : Py_CLEAR(state->sha224_type);
694 6 : Py_CLEAR(state->sha256_type);
695 6 : return 0;
696 : }
697 :
698 : static void
699 3 : _sha256_free(void *module)
700 : {
701 3 : _sha256_clear((PyObject *)module);
702 3 : }
703 :
704 3 : static int sha256_exec(PyObject *module)
705 : {
706 3 : _sha256_state *state = _sha256_get_state(module);
707 :
708 3 : state->sha224_type = (PyTypeObject *)PyType_FromModuleAndSpec(
709 : module, &sha224_type_spec, NULL);
710 :
711 3 : if (state->sha224_type == NULL) {
712 0 : return -1;
713 : }
714 :
715 3 : state->sha256_type = (PyTypeObject *)PyType_FromModuleAndSpec(
716 : module, &sha256_type_spec, NULL);
717 :
718 3 : if (state->sha256_type == NULL) {
719 0 : return -1;
720 : }
721 :
722 3 : Py_INCREF((PyObject *)state->sha224_type);
723 3 : if (PyModule_AddObject(module, "SHA224Type", (PyObject *)state->sha224_type) < 0) {
724 0 : Py_DECREF((PyObject *)state->sha224_type);
725 0 : return -1;
726 : }
727 3 : Py_INCREF((PyObject *)state->sha256_type);
728 3 : if (PyModule_AddObject(module, "SHA256Type", (PyObject *)state->sha256_type) < 0) {
729 0 : Py_DECREF((PyObject *)state->sha256_type);
730 0 : return -1;
731 : }
732 3 : return 0;
733 : }
734 :
735 : static PyModuleDef_Slot _sha256_slots[] = {
736 : {Py_mod_exec, sha256_exec},
737 : {0, NULL}
738 : };
739 :
740 : static 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. */
752 : PyMODINIT_FUNC
753 3 : PyInit__sha256(void)
754 : {
755 3 : return PyModuleDef_Init(&_sha256module);
756 : }
|