blob: b90e5df7826740690c8971fa1234d14e2c975933 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* 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
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000012 Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000013 Licensed to PSF under a Contributor Agreement.
14
15*/
16
17/* SHA objects */
18
19#include "Python.h"
Victor Stinnerc6b292c2020-06-08 16:30:33 +020020#include "pycore_bitutils.h" // _Py_bswap32()
Victor Stinner4a21e572020-04-15 02:35:41 +020021#include "structmember.h" // PyMemberDef
Gregory P. Smith365a1862009-02-12 07:35:29 +000022#include "hashlib.h"
Gregory P. Smith8cb65692015-04-25 23:22:26 +000023#include "pystrhex.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000024
Martin v. Löwis501b13c2014-07-27 14:20:23 +020025/*[clinic input]
26module _sha256
27class SHA256Type "SHAobject *" "&PyType_Type"
28[clinic start generated code]*/
29/*[clinic end generated code: output=da39a3ee5e6b4b0d input=71a39174d4f0a744]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000030
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000031/* Some useful types */
32
33typedef unsigned char SHA_BYTE;
Victor Stinner1ae035b2020-04-17 17:47:20 +020034typedef uint32_t SHA_INT32; /* 32-bit integer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000035
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
43typedef struct {
44 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 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 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 int local; /* unprocessed amount in data */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000049 int digestsize;
50} SHAobject;
51
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030052#include "clinic/sha256module.c.h"
53
Mohamed Koubaa52a2df12020-09-08 04:16:14 -050054typedef struct {
55 PyTypeObject* sha224_type;
56 PyTypeObject* sha256_type;
57} _sha256_state;
58
59static inline _sha256_state*
60_sha256_get_state(PyObject *module)
61{
62 void *state = PyModule_GetState(module);
63 assert(state != NULL);
64 return (_sha256_state *)state;
65}
66
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000067/* When run on a little-endian CPU we need to perform byte reversal on an
68 array of longwords. */
69
Christian Heimes743e0cd2012-10-17 23:52:17 +020070#if PY_LITTLE_ENDIAN
71static void longReverse(SHA_INT32 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000072{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000073 byteCount /= sizeof(*buffer);
Victor Stinner1ae035b2020-04-17 17:47:20 +020074 for (; byteCount--; buffer++) {
75 *buffer = _Py_bswap32(*buffer);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000076 }
77}
Christian Heimes743e0cd2012-10-17 23:52:17 +020078#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000079
80static void SHAcopy(SHAobject *src, SHAobject *dest)
81{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000082 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
Martin Panter46f50722016-05-26 05:35:26 +0000107 * guarantee it works.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000108 *
Erlend Egeberg Aasland5ec7d532021-02-12 11:34:11 +0100109 * Tom St Denis, tomstdenis@iahu.ca, https://www.libtom.net
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000110 */
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)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000121#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
129static void
130sha_transform(SHAobject *sha_info)
131{
132 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 SHA_INT32 S[8], W[64], t0, t1;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000134
135 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200136#if PY_LITTLE_ENDIAN
137 longReverse(W, (int)sizeof(sha_info->data));
138#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000139
140 for (i = 16; i < 64; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000142 }
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219#undef RND
220
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000221 /* 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
232static void
233sha_init(SHAobject *sha_info)
234{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000235 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
249static void
250sha224_init(SHAobject *sha_info)
251{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000252 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
269static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000270sha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000271{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000272 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000273 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;
Victor Stinner70792d22013-05-08 00:00:44 +0200289 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000290 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);
Victor Stinner70792d22013-05-08 00:00:44 +0200304 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000305}
306
307/* finish computing the SHA digest */
308
309static void
310sha_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) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 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);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000324 }
325 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 memset(((SHA_BYTE *) sha_info->data) + count, 0,
327 SHA_BLOCKSIZE - 8 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000328 }
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
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000381
382static SHAobject *
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500383newSHA224object(_sha256_state *state)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000384{
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500385 return (SHAobject *)PyObject_New(SHAobject, state->sha224_type);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000386}
387
388static SHAobject *
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500389newSHA256object(_sha256_state *state)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000390{
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500391 return (SHAobject *)PyObject_New(SHAobject, state->sha256_type);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000392}
393
394/* Internal methods for a hash object */
395
396static void
397SHA_dealloc(PyObject *ptr)
398{
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500399 PyTypeObject *tp = Py_TYPE(ptr);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100400 PyObject_Free(ptr);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500401 Py_DECREF(tp);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000402}
403
404
405/* External methods for a hash object */
406
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200407/*[clinic input]
408SHA256Type.copy
409
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500410 cls:defining_class
411
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200412Return a copy of the hash object.
413[clinic start generated code]*/
414
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200415static PyObject *
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500416SHA256Type_copy_impl(SHAobject *self, PyTypeObject *cls)
417/*[clinic end generated code: output=9273f92c382be12f input=3137146fcb88e212]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000418{
419 SHAobject *newobj;
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500420 _sha256_state *state = PyType_GetModuleState(cls);
421 if (Py_IS_TYPE(self, state->sha256_type)) {
422 if ( (newobj = newSHA256object(state)) == NULL) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000423 return NULL;
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500424 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000425 } else {
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500426 if ( (newobj = newSHA224object(state))==NULL) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000427 return NULL;
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500428 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000429 }
430
431 SHAcopy(self, newobj);
432 return (PyObject *)newobj;
433}
434
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200435/*[clinic input]
436SHA256Type.digest
437
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530438Return the digest value as a bytes object.
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200439[clinic start generated code]*/
440
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200441static PyObject *
442SHA256Type_digest_impl(SHAobject *self)
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530443/*[clinic end generated code: output=46616a5e909fbc3d input=f1f4cfea5cbde35c]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000444{
445 unsigned char digest[SHA_DIGESTSIZE];
446 SHAobject temp;
447
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000448 SHAcopy(self, &temp);
449 sha_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000450 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000451}
452
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200453/*[clinic input]
454SHA256Type.hexdigest
455
456Return the digest value as a string of hexadecimal digits.
457[clinic start generated code]*/
458
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200459static PyObject *
460SHA256Type_hexdigest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300461/*[clinic end generated code: output=725f8a7041ae97f3 input=0cc4c714693010d1]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000462{
463 unsigned char digest[SHA_DIGESTSIZE];
464 SHAobject temp;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000465
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000466 /* Get the raw (binary) digest value */
467 SHAcopy(self, &temp);
468 sha_final(digest, &temp);
469
Gregory P. Smith8cb65692015-04-25 23:22:26 +0000470 return _Py_strhex((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000471}
472
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200473/*[clinic input]
474SHA256Type.update
475
476 obj: object
477 /
478
479Update this hash object's state with the provided string.
480[clinic start generated code]*/
481
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000482static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200483SHA256Type_update(SHAobject *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300484/*[clinic end generated code: output=0967fb2860c66af7 input=b2d449d5b30f0f5a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000485{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000486 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000487
Gregory P. Smith365a1862009-02-12 07:35:29 +0000488 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000489
Gregory P. Smith365a1862009-02-12 07:35:29 +0000490 sha_update(self, buf.buf, buf.len);
491
492 PyBuffer_Release(&buf);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200493 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000494}
495
496static PyMethodDef SHA_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200497 SHA256TYPE_COPY_METHODDEF
498 SHA256TYPE_DIGEST_METHODDEF
499 SHA256TYPE_HEXDIGEST_METHODDEF
500 SHA256TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000502};
503
504static PyObject *
505SHA256_get_block_size(PyObject *self, void *closure)
506{
Christian Heimes217cfd12007-12-02 14:31:20 +0000507 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000508}
509
510static PyObject *
511SHA256_get_name(PyObject *self, void *closure)
512{
513 if (((SHAobject *)self)->digestsize == 32)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200514 return PyUnicode_FromStringAndSize("sha256", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000515 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200516 return PyUnicode_FromStringAndSize("sha224", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000517}
518
519static PyGetSetDef SHA_getseters[] = {
520 {"block_size",
521 (getter)SHA256_get_block_size, NULL,
522 NULL,
523 NULL},
524 {"name",
525 (getter)SHA256_get_name, NULL,
526 NULL,
527 NULL},
528 {NULL} /* Sentinel */
529};
530
531static PyMemberDef SHA_members[] = {
532 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000533 {NULL} /* Sentinel */
534};
535
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500536static PyType_Slot sha256_types_slots[] = {
537 {Py_tp_dealloc, SHA_dealloc},
538 {Py_tp_methods, SHA_methods},
539 {Py_tp_members, SHA_members},
540 {Py_tp_getset, SHA_getseters},
541 {0,0}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000542};
543
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500544static PyType_Spec sha224_type_spec = {
545 .name = "_sha256.sha224",
546 .basicsize = sizeof(SHAobject),
547 .flags = Py_TPFLAGS_DEFAULT,
548 .slots = sha256_types_slots
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000549};
550
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500551static PyType_Spec sha256_type_spec = {
552 .name = "_sha256.sha256",
553 .basicsize = sizeof(SHAobject),
554 .flags = Py_TPFLAGS_DEFAULT,
555 .slots = sha256_types_slots
556};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000557
558/* The single module-level function: new() */
559
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200560/*[clinic input]
561_sha256.sha256
562
563 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200564 *
565 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200566
567Return a new SHA-256 hash object; optionally initialized with a string.
568[clinic start generated code]*/
569
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200570static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200571_sha256_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity)
572/*[clinic end generated code: output=a1de327e8e1185cf input=9be86301aeb14ea5]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200573{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000574 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000575
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500576 if (string) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200577 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500578 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000579
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500580 _sha256_state *state = PyModule_GetState(module);
581
582 SHAobject *new;
583 if ((new = newSHA256object(state)) == NULL) {
584 if (string) {
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000585 PyBuffer_Release(&buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500586 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000587 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000588 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000589
590 sha_init(new);
591
592 if (PyErr_Occurred()) {
593 Py_DECREF(new);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500594 if (string) {
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000595 PyBuffer_Release(&buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500596 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000597 return NULL;
598 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200599 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000600 sha_update(new, buf.buf, buf.len);
601 PyBuffer_Release(&buf);
602 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000603
604 return (PyObject *)new;
605}
606
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200607/*[clinic input]
608_sha256.sha224
609
610 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200611 *
612 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200613
614Return a new SHA-224 hash object; optionally initialized with a string.
615[clinic start generated code]*/
616
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200617static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200618_sha256_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
619/*[clinic end generated code: output=08be6b36569bc69c input=9fcfb46e460860ac]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200620{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000621 Py_buffer buf;
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500622 if (string) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200623 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500624 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000625
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500626 _sha256_state *state = PyModule_GetState(module);
627 SHAobject *new;
628 if ((new = newSHA224object(state)) == NULL) {
629 if (string) {
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000630 PyBuffer_Release(&buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500631 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000632 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000633 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000634
635 sha224_init(new);
636
637 if (PyErr_Occurred()) {
638 Py_DECREF(new);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500639 if (string) {
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000640 PyBuffer_Release(&buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500641 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000642 return NULL;
643 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200644 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000645 sha_update(new, buf.buf, buf.len);
646 PyBuffer_Release(&buf);
647 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000648
649 return (PyObject *)new;
650}
651
652
653/* List of functions exported by this module */
654
655static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200656 _SHA256_SHA256_METHODDEF
657 _SHA256_SHA224_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000659};
660
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500661static int
662_sha256_traverse(PyObject *module, visitproc visit, void *arg)
663{
664 _sha256_state *state = _sha256_get_state(module);
665 Py_VISIT(state->sha224_type);
666 Py_VISIT(state->sha256_type);
667 return 0;
668}
669
670static int
671_sha256_clear(PyObject *module)
672{
673 _sha256_state *state = _sha256_get_state(module);
674 Py_CLEAR(state->sha224_type);
675 Py_CLEAR(state->sha256_type);
676 return 0;
677}
678
679static void
680_sha256_free(void *module)
681{
682 _sha256_clear((PyObject *)module);
683}
684
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500685static int sha256_exec(PyObject *module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000686{
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500687 _sha256_state *state = _sha256_get_state(module);
688
689 state->sha224_type = (PyTypeObject *)PyType_FromModuleAndSpec(
690 module, &sha224_type_spec, NULL);
691
692 if (state->sha224_type == NULL) {
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500693 return -1;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100694 }
Christian Heimes327dd732013-10-22 15:05:23 +0200695
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500696 state->sha256_type = (PyTypeObject *)PyType_FromModuleAndSpec(
697 module, &sha256_type_spec, NULL);
698
699 if (state->sha256_type == NULL) {
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500700 return -1;
701 }
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500702
703 Py_INCREF((PyObject *)state->sha224_type);
704 if (PyModule_AddObject(module, "SHA224Type", (PyObject *)state->sha224_type) < 0) {
705 Py_DECREF((PyObject *)state->sha224_type);
706 return -1;
707 }
708 Py_INCREF((PyObject *)state->sha256_type);
709 if (PyModule_AddObject(module, "SHA256Type", (PyObject *)state->sha256_type) < 0) {
710 Py_DECREF((PyObject *)state->sha256_type);
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500711 return -1;
712 }
713 return 0;
714}
Christian Heimes327dd732013-10-22 15:05:23 +0200715
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500716static PyModuleDef_Slot _sha256_slots[] = {
717 {Py_mod_exec, sha256_exec},
718 {0, NULL}
719};
720
721static struct PyModuleDef _sha256module = {
722 PyModuleDef_HEAD_INIT,
723 .m_name = "_sha256",
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500724 .m_size = sizeof(_sha256_state),
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500725 .m_methods = SHA_functions,
726 .m_slots = _sha256_slots,
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500727 .m_traverse = _sha256_traverse,
728 .m_clear = _sha256_clear,
729 .m_free = _sha256_free
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500730};
731
732/* Initialize this module. */
733PyMODINIT_FUNC
734PyInit__sha256(void)
735{
736 return PyModuleDef_Init(&_sha256module);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000737}