blob: fac7073d951d2df931e6b214e1f0fe3e78afbb4c [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{
Miss Islington (bot)44319222021-05-27 01:10:39 -0700385 SHAobject *sha = (SHAobject *)PyObject_GC_New(SHAobject,
386 state->sha224_type);
387 PyObject_GC_Track(sha);
388 return sha;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000389}
390
391static SHAobject *
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500392newSHA256object(_sha256_state *state)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000393{
Miss Islington (bot)44319222021-05-27 01:10:39 -0700394 SHAobject *sha = (SHAobject *)PyObject_GC_New(SHAobject,
395 state->sha256_type);
396 PyObject_GC_Track(sha);
397 return sha;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000398}
399
400/* Internal methods for a hash object */
Miss Islington (bot)44319222021-05-27 01:10:39 -0700401static int
402SHA_traverse(PyObject *ptr, visitproc visit, void *arg)
403{
404 Py_VISIT(Py_TYPE(ptr));
405 return 0;
406}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000407
408static void
409SHA_dealloc(PyObject *ptr)
410{
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500411 PyTypeObject *tp = Py_TYPE(ptr);
Miss Islington (bot)44319222021-05-27 01:10:39 -0700412 PyObject_GC_UnTrack(ptr);
413 PyObject_GC_Del(ptr);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500414 Py_DECREF(tp);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000415}
416
417
418/* External methods for a hash object */
419
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200420/*[clinic input]
421SHA256Type.copy
422
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500423 cls:defining_class
424
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200425Return a copy of the hash object.
426[clinic start generated code]*/
427
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200428static PyObject *
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500429SHA256Type_copy_impl(SHAobject *self, PyTypeObject *cls)
430/*[clinic end generated code: output=9273f92c382be12f input=3137146fcb88e212]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000431{
432 SHAobject *newobj;
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500433 _sha256_state *state = PyType_GetModuleState(cls);
434 if (Py_IS_TYPE(self, state->sha256_type)) {
435 if ( (newobj = newSHA256object(state)) == NULL) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000436 return NULL;
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500437 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000438 } else {
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500439 if ( (newobj = newSHA224object(state))==NULL) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000440 return NULL;
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500441 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000442 }
443
444 SHAcopy(self, newobj);
445 return (PyObject *)newobj;
446}
447
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200448/*[clinic input]
449SHA256Type.digest
450
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530451Return the digest value as a bytes object.
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200452[clinic start generated code]*/
453
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200454static PyObject *
455SHA256Type_digest_impl(SHAobject *self)
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530456/*[clinic end generated code: output=46616a5e909fbc3d input=f1f4cfea5cbde35c]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000457{
458 unsigned char digest[SHA_DIGESTSIZE];
459 SHAobject temp;
460
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000461 SHAcopy(self, &temp);
462 sha_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000463 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000464}
465
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200466/*[clinic input]
467SHA256Type.hexdigest
468
469Return the digest value as a string of hexadecimal digits.
470[clinic start generated code]*/
471
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200472static PyObject *
473SHA256Type_hexdigest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300474/*[clinic end generated code: output=725f8a7041ae97f3 input=0cc4c714693010d1]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000475{
476 unsigned char digest[SHA_DIGESTSIZE];
477 SHAobject temp;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000478
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000479 /* Get the raw (binary) digest value */
480 SHAcopy(self, &temp);
481 sha_final(digest, &temp);
482
Gregory P. Smith8cb65692015-04-25 23:22:26 +0000483 return _Py_strhex((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000484}
485
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200486/*[clinic input]
487SHA256Type.update
488
489 obj: object
490 /
491
492Update this hash object's state with the provided string.
493[clinic start generated code]*/
494
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000495static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200496SHA256Type_update(SHAobject *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300497/*[clinic end generated code: output=0967fb2860c66af7 input=b2d449d5b30f0f5a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000498{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000499 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000500
Gregory P. Smith365a1862009-02-12 07:35:29 +0000501 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000502
Gregory P. Smith365a1862009-02-12 07:35:29 +0000503 sha_update(self, buf.buf, buf.len);
504
505 PyBuffer_Release(&buf);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200506 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000507}
508
509static PyMethodDef SHA_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200510 SHA256TYPE_COPY_METHODDEF
511 SHA256TYPE_DIGEST_METHODDEF
512 SHA256TYPE_HEXDIGEST_METHODDEF
513 SHA256TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000515};
516
517static PyObject *
518SHA256_get_block_size(PyObject *self, void *closure)
519{
Christian Heimes217cfd12007-12-02 14:31:20 +0000520 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000521}
522
523static PyObject *
524SHA256_get_name(PyObject *self, void *closure)
525{
526 if (((SHAobject *)self)->digestsize == 32)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200527 return PyUnicode_FromStringAndSize("sha256", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000528 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200529 return PyUnicode_FromStringAndSize("sha224", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000530}
531
532static 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
544static PyMemberDef SHA_members[] = {
545 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000546 {NULL} /* Sentinel */
547};
548
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500549static 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},
Miss Islington (bot)44319222021-05-27 01:10:39 -0700554 {Py_tp_traverse, SHA_traverse},
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500555 {0,0}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000556};
557
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500558static PyType_Spec sha224_type_spec = {
559 .name = "_sha256.sha224",
560 .basicsize = sizeof(SHAobject),
Miss Islington (bot)44319222021-05-27 01:10:39 -0700561 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
562 Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500563 .slots = sha256_types_slots
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000564};
565
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500566static PyType_Spec sha256_type_spec = {
567 .name = "_sha256.sha256",
568 .basicsize = sizeof(SHAobject),
Miss Islington (bot)44319222021-05-27 01:10:39 -0700569 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
570 Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500571 .slots = sha256_types_slots
572};
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000573
574/* The single module-level function: new() */
575
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200576/*[clinic input]
577_sha256.sha256
578
579 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200580 *
581 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200582
583Return a new SHA-256 hash object; optionally initialized with a string.
584[clinic start generated code]*/
585
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200586static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200587_sha256_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity)
588/*[clinic end generated code: output=a1de327e8e1185cf input=9be86301aeb14ea5]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200589{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000590 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000591
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500592 if (string) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200593 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500594 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000595
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500596 _sha256_state *state = PyModule_GetState(module);
597
598 SHAobject *new;
599 if ((new = newSHA256object(state)) == NULL) {
600 if (string) {
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000601 PyBuffer_Release(&buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500602 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000603 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000604 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000605
606 sha_init(new);
607
608 if (PyErr_Occurred()) {
609 Py_DECREF(new);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500610 if (string) {
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000611 PyBuffer_Release(&buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500612 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000613 return NULL;
614 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200615 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000616 sha_update(new, buf.buf, buf.len);
617 PyBuffer_Release(&buf);
618 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000619
620 return (PyObject *)new;
621}
622
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200623/*[clinic input]
624_sha256.sha224
625
626 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200627 *
628 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200629
630Return a new SHA-224 hash object; optionally initialized with a string.
631[clinic start generated code]*/
632
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200633static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200634_sha256_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
635/*[clinic end generated code: output=08be6b36569bc69c input=9fcfb46e460860ac]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200636{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000637 Py_buffer buf;
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500638 if (string) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200639 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500640 }
Gregory P. Smith365a1862009-02-12 07:35:29 +0000641
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500642 _sha256_state *state = PyModule_GetState(module);
643 SHAobject *new;
644 if ((new = newSHA224object(state)) == NULL) {
645 if (string) {
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000646 PyBuffer_Release(&buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500647 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000648 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000649 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000650
651 sha224_init(new);
652
653 if (PyErr_Occurred()) {
654 Py_DECREF(new);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500655 if (string) {
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000656 PyBuffer_Release(&buf);
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500657 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000658 return NULL;
659 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200660 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000661 sha_update(new, buf.buf, buf.len);
662 PyBuffer_Release(&buf);
663 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000664
665 return (PyObject *)new;
666}
667
668
669/* List of functions exported by this module */
670
671static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200672 _SHA256_SHA256_METHODDEF
673 _SHA256_SHA224_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000675};
676
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500677static int
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
686static int
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
695static void
696_sha256_free(void *module)
697{
698 _sha256_clear((PyObject *)module);
699}
700
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500701static int sha256_exec(PyObject *module)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000702{
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500703 _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) {
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500709 return -1;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100710 }
Christian Heimes327dd732013-10-22 15:05:23 +0200711
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500712 state->sha256_type = (PyTypeObject *)PyType_FromModuleAndSpec(
713 module, &sha256_type_spec, NULL);
714
715 if (state->sha256_type == NULL) {
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500716 return -1;
717 }
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500718
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);
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500727 return -1;
728 }
729 return 0;
730}
Christian Heimes327dd732013-10-22 15:05:23 +0200731
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500732static PyModuleDef_Slot _sha256_slots[] = {
733 {Py_mod_exec, sha256_exec},
734 {0, NULL}
735};
736
737static struct PyModuleDef _sha256module = {
738 PyModuleDef_HEAD_INIT,
739 .m_name = "_sha256",
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500740 .m_size = sizeof(_sha256_state),
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500741 .m_methods = SHA_functions,
742 .m_slots = _sha256_slots,
Mohamed Koubaa52a2df12020-09-08 04:16:14 -0500743 .m_traverse = _sha256_traverse,
744 .m_clear = _sha256_clear,
745 .m_free = _sha256_free
Mohamed Koubaa9d006972020-07-03 03:59:47 -0500746};
747
748/* Initialize this module. */
749PyMODINIT_FUNC
750PyInit__sha256(void)
751{
752 return PyModuleDef_Init(&_sha256module);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000753}