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