blob: 8edb1d53828835ee19cb40dc1019760b8c7f3c43 [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 Stinner1ae035b2020-04-17 17:47:20 +020020#include "pycore_byteswap.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
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000054/* When run on a little-endian CPU we need to perform byte reversal on an
55 array of longwords. */
56
Christian Heimes743e0cd2012-10-17 23:52:17 +020057#if PY_LITTLE_ENDIAN
58static void longReverse(SHA_INT32 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000059{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000060 byteCount /= sizeof(*buffer);
Victor Stinner1ae035b2020-04-17 17:47:20 +020061 for (; byteCount--; buffer++) {
62 *buffer = _Py_bswap32(*buffer);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000063 }
64}
Christian Heimes743e0cd2012-10-17 23:52:17 +020065#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000066
67static void SHAcopy(SHAobject *src, SHAobject *dest)
68{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000069 dest->local = src->local;
70 dest->digestsize = src->digestsize;
71 dest->count_lo = src->count_lo;
72 dest->count_hi = src->count_hi;
73 memcpy(dest->digest, src->digest, sizeof(src->digest));
74 memcpy(dest->data, src->data, sizeof(src->data));
75}
76
77
78/* ------------------------------------------------------------------------
79 *
80 * This code for the SHA-256 algorithm was noted as public domain. The
81 * original headers are pasted below.
82 *
83 * Several changes have been made to make it more compatible with the
84 * Python environment and desired interface.
85 *
86 */
87
88/* LibTomCrypt, modular cryptographic library -- Tom St Denis
89 *
90 * LibTomCrypt is a library that provides various cryptographic
91 * algorithms in a highly modular and flexible manner.
92 *
93 * The library is free for all purposes without any express
Martin Panter46f50722016-05-26 05:35:26 +000094 * guarantee it works.
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000095 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +000096 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000097 */
98
99
100/* SHA256 by Tom St Denis */
101
102/* Various logical functions */
103#define ROR(x, y)\
104( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
105((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
106#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000108#define S(x, n) ROR((x),(n))
109#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
110#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
111#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
112#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
113#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
114
115
116static void
117sha_transform(SHAobject *sha_info)
118{
119 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 SHA_INT32 S[8], W[64], t0, t1;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000121
122 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200123#if PY_LITTLE_ENDIAN
124 longReverse(W, (int)sizeof(sha_info->data));
125#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000126
127 for (i = 16; i < 64; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000129 }
130 for (i = 0; i < 8; ++i) {
131 S[i] = sha_info->digest[i];
132 }
133
134 /* Compress */
135#define RND(a,b,c,d,e,f,g,h,i,ki) \
136 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
137 t1 = Sigma0(a) + Maj(a, b, c); \
138 d += t0; \
139 h = t0 + t1;
140
141 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
142 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
143 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
144 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
145 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
146 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
147 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
148 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
149 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
150 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
151 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
152 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
153 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
154 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
155 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
156 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
157 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
158 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
159 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
160 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
161 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
162 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
163 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
164 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
165 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
166 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
167 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
168 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
169 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
170 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
171 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
172 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
173 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
174 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
175 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
176 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
177 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
178 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
179 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
180 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
181 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
182 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
183 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
184 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
185 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
186 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
187 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
188 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
189 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
190 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
191 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
192 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
193 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
194 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
195 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
196 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
197 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
198 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
199 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
200 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
201 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
202 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
203 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
204 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206#undef RND
207
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000208 /* feedback */
209 for (i = 0; i < 8; i++) {
210 sha_info->digest[i] = sha_info->digest[i] + S[i];
211 }
212
213}
214
215
216
217/* initialize the SHA digest */
218
219static void
220sha_init(SHAobject *sha_info)
221{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000222 sha_info->digest[0] = 0x6A09E667L;
223 sha_info->digest[1] = 0xBB67AE85L;
224 sha_info->digest[2] = 0x3C6EF372L;
225 sha_info->digest[3] = 0xA54FF53AL;
226 sha_info->digest[4] = 0x510E527FL;
227 sha_info->digest[5] = 0x9B05688CL;
228 sha_info->digest[6] = 0x1F83D9ABL;
229 sha_info->digest[7] = 0x5BE0CD19L;
230 sha_info->count_lo = 0L;
231 sha_info->count_hi = 0L;
232 sha_info->local = 0;
233 sha_info->digestsize = 32;
234}
235
236static void
237sha224_init(SHAobject *sha_info)
238{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000239 sha_info->digest[0] = 0xc1059ed8L;
240 sha_info->digest[1] = 0x367cd507L;
241 sha_info->digest[2] = 0x3070dd17L;
242 sha_info->digest[3] = 0xf70e5939L;
243 sha_info->digest[4] = 0xffc00b31L;
244 sha_info->digest[5] = 0x68581511L;
245 sha_info->digest[6] = 0x64f98fa7L;
246 sha_info->digest[7] = 0xbefa4fa4L;
247 sha_info->count_lo = 0L;
248 sha_info->count_hi = 0L;
249 sha_info->local = 0;
250 sha_info->digestsize = 28;
251}
252
253
254/* update the SHA digest */
255
256static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000257sha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000258{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000259 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000260 SHA_INT32 clo;
261
262 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
263 if (clo < sha_info->count_lo) {
264 ++sha_info->count_hi;
265 }
266 sha_info->count_lo = clo;
267 sha_info->count_hi += (SHA_INT32) count >> 29;
268 if (sha_info->local) {
269 i = SHA_BLOCKSIZE - sha_info->local;
270 if (i > count) {
271 i = count;
272 }
273 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
274 count -= i;
275 buffer += i;
Victor Stinner70792d22013-05-08 00:00:44 +0200276 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000277 if (sha_info->local == SHA_BLOCKSIZE) {
278 sha_transform(sha_info);
279 }
280 else {
281 return;
282 }
283 }
284 while (count >= SHA_BLOCKSIZE) {
285 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
286 buffer += SHA_BLOCKSIZE;
287 count -= SHA_BLOCKSIZE;
288 sha_transform(sha_info);
289 }
290 memcpy(sha_info->data, buffer, count);
Victor Stinner70792d22013-05-08 00:00:44 +0200291 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000292}
293
294/* finish computing the SHA digest */
295
296static void
297sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
298{
299 int count;
300 SHA_INT32 lo_bit_count, hi_bit_count;
301
302 lo_bit_count = sha_info->count_lo;
303 hi_bit_count = sha_info->count_hi;
304 count = (int) ((lo_bit_count >> 3) & 0x3f);
305 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
306 if (count > SHA_BLOCKSIZE - 8) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 memset(((SHA_BYTE *) sha_info->data) + count, 0,
308 SHA_BLOCKSIZE - count);
309 sha_transform(sha_info);
310 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000311 }
312 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 memset(((SHA_BYTE *) sha_info->data) + count, 0,
314 SHA_BLOCKSIZE - 8 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000315 }
316
317 /* GJS: note that we add the hi/lo in big-endian. sha_transform will
318 swap these values into host-order. */
319 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
320 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
321 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
322 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
323 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
324 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
325 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
326 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
327 sha_transform(sha_info);
328 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
329 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
330 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
331 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
332 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
333 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
334 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
335 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
336 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
337 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
338 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
339 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
340 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
341 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
342 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
343 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
344 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
345 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
346 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
347 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
348 digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
349 digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
350 digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
351 digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
352 digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
353 digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
354 digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
355 digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
356 digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
357 digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
358 digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
359 digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
360}
361
362/*
363 * End of copied SHA code.
364 *
365 * ------------------------------------------------------------------------
366 */
367
368static PyTypeObject SHA224type;
369static PyTypeObject SHA256type;
370
371
372static SHAobject *
373newSHA224object(void)
374{
375 return (SHAobject *)PyObject_New(SHAobject, &SHA224type);
376}
377
378static SHAobject *
379newSHA256object(void)
380{
381 return (SHAobject *)PyObject_New(SHAobject, &SHA256type);
382}
383
384/* Internal methods for a hash object */
385
386static void
387SHA_dealloc(PyObject *ptr)
388{
389 PyObject_Del(ptr);
390}
391
392
393/* External methods for a hash object */
394
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200395/*[clinic input]
396SHA256Type.copy
397
398Return a copy of the hash object.
399[clinic start generated code]*/
400
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200401static PyObject *
402SHA256Type_copy_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300403/*[clinic end generated code: output=1a8bbd66a0c9c168 input=f58840a618d4f2a7]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000404{
405 SHAobject *newobj;
406
Dong-hee Na1b55b652020-02-17 19:09:15 +0900407 if (Py_IS_TYPE(self, &SHA256type)) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000408 if ( (newobj = newSHA256object())==NULL)
409 return NULL;
410 } else {
411 if ( (newobj = newSHA224object())==NULL)
412 return NULL;
413 }
414
415 SHAcopy(self, newobj);
416 return (PyObject *)newobj;
417}
418
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200419/*[clinic input]
420SHA256Type.digest
421
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530422Return the digest value as a bytes object.
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200423[clinic start generated code]*/
424
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200425static PyObject *
426SHA256Type_digest_impl(SHAobject *self)
Srinivas Thatiparthy (శ్రీనివాస్ తాటిపర్తి)f192aeb2018-10-19 23:12:53 +0530427/*[clinic end generated code: output=46616a5e909fbc3d input=f1f4cfea5cbde35c]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000428{
429 unsigned char digest[SHA_DIGESTSIZE];
430 SHAobject temp;
431
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000432 SHAcopy(self, &temp);
433 sha_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000434 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000435}
436
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200437/*[clinic input]
438SHA256Type.hexdigest
439
440Return the digest value as a string of hexadecimal digits.
441[clinic start generated code]*/
442
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200443static PyObject *
444SHA256Type_hexdigest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300445/*[clinic end generated code: output=725f8a7041ae97f3 input=0cc4c714693010d1]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000446{
447 unsigned char digest[SHA_DIGESTSIZE];
448 SHAobject temp;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000449
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000450 /* Get the raw (binary) digest value */
451 SHAcopy(self, &temp);
452 sha_final(digest, &temp);
453
Gregory P. Smith8cb65692015-04-25 23:22:26 +0000454 return _Py_strhex((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000455}
456
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200457/*[clinic input]
458SHA256Type.update
459
460 obj: object
461 /
462
463Update this hash object's state with the provided string.
464[clinic start generated code]*/
465
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000466static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200467SHA256Type_update(SHAobject *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300468/*[clinic end generated code: output=0967fb2860c66af7 input=b2d449d5b30f0f5a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000469{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000470 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000471
Gregory P. Smith365a1862009-02-12 07:35:29 +0000472 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000473
Gregory P. Smith365a1862009-02-12 07:35:29 +0000474 sha_update(self, buf.buf, buf.len);
475
476 PyBuffer_Release(&buf);
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200477 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000478}
479
480static PyMethodDef SHA_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200481 SHA256TYPE_COPY_METHODDEF
482 SHA256TYPE_DIGEST_METHODDEF
483 SHA256TYPE_HEXDIGEST_METHODDEF
484 SHA256TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000486};
487
488static PyObject *
489SHA256_get_block_size(PyObject *self, void *closure)
490{
Christian Heimes217cfd12007-12-02 14:31:20 +0000491 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000492}
493
494static PyObject *
495SHA256_get_name(PyObject *self, void *closure)
496{
497 if (((SHAobject *)self)->digestsize == 32)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200498 return PyUnicode_FromStringAndSize("sha256", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000499 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200500 return PyUnicode_FromStringAndSize("sha224", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000501}
502
503static PyGetSetDef SHA_getseters[] = {
504 {"block_size",
505 (getter)SHA256_get_block_size, NULL,
506 NULL,
507 NULL},
508 {"name",
509 (getter)SHA256_get_name, NULL,
510 NULL,
511 NULL},
512 {NULL} /* Sentinel */
513};
514
515static PyMemberDef SHA_members[] = {
516 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000517 {NULL} /* Sentinel */
518};
519
520static PyTypeObject SHA224type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000521 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 "_sha256.sha224", /*tp_name*/
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200523 sizeof(SHAobject), /*tp_basicsize*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000525 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 SHA_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200527 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000529 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200530 0, /*tp_as_async*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000531 0, /*tp_repr*/
532 0, /*tp_as_number*/
533 0, /*tp_as_sequence*/
534 0, /*tp_as_mapping*/
535 0, /*tp_hash*/
536 0, /*tp_call*/
537 0, /*tp_str*/
538 0, /*tp_getattro*/
539 0, /*tp_setattro*/
540 0, /*tp_as_buffer*/
541 Py_TPFLAGS_DEFAULT, /*tp_flags*/
542 0, /*tp_doc*/
543 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 0, /*tp_clear*/
545 0, /*tp_richcompare*/
546 0, /*tp_weaklistoffset*/
547 0, /*tp_iter*/
548 0, /*tp_iternext*/
549 SHA_methods, /* tp_methods */
550 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000551 SHA_getseters, /* tp_getset */
552};
553
554static PyTypeObject SHA256type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000555 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 "_sha256.sha256", /*tp_name*/
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200557 sizeof(SHAobject), /*tp_basicsize*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000559 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 SHA_dealloc, /*tp_dealloc*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200561 0, /*tp_vectorcall_offset*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000563 0, /*tp_setattr*/
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200564 0, /*tp_as_async*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000565 0, /*tp_repr*/
566 0, /*tp_as_number*/
567 0, /*tp_as_sequence*/
568 0, /*tp_as_mapping*/
569 0, /*tp_hash*/
570 0, /*tp_call*/
571 0, /*tp_str*/
572 0, /*tp_getattro*/
573 0, /*tp_setattro*/
574 0, /*tp_as_buffer*/
575 Py_TPFLAGS_DEFAULT, /*tp_flags*/
576 0, /*tp_doc*/
577 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 0, /*tp_clear*/
579 0, /*tp_richcompare*/
580 0, /*tp_weaklistoffset*/
581 0, /*tp_iter*/
582 0, /*tp_iternext*/
583 SHA_methods, /* tp_methods */
584 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000585 SHA_getseters, /* tp_getset */
586};
587
588
589/* The single module-level function: new() */
590
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200591/*[clinic input]
592_sha256.sha256
593
594 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200595 *
596 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200597
598Return a new SHA-256 hash object; optionally initialized with a string.
599[clinic start generated code]*/
600
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200601static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200602_sha256_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity)
603/*[clinic end generated code: output=a1de327e8e1185cf input=9be86301aeb14ea5]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200604{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000605 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000606 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000607
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200608 if (string)
609 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000610
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000611 if ((new = newSHA256object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200612 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000613 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000614 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000615 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000616
617 sha_init(new);
618
619 if (PyErr_Occurred()) {
620 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200621 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000622 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000623 return NULL;
624 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200625 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000626 sha_update(new, buf.buf, buf.len);
627 PyBuffer_Release(&buf);
628 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000629
630 return (PyObject *)new;
631}
632
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200633/*[clinic input]
634_sha256.sha224
635
636 string: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200637 *
638 usedforsecurity: bool = True
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200639
640Return a new SHA-224 hash object; optionally initialized with a string.
641[clinic start generated code]*/
642
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200643static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200644_sha256_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
645/*[clinic end generated code: output=08be6b36569bc69c input=9fcfb46e460860ac]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200646{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000647 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000648 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000649
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200650 if (string)
651 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000652
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000653 if ((new = newSHA224object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200654 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000655 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000656 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000657 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000658
659 sha224_init(new);
660
661 if (PyErr_Occurred()) {
662 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200663 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000664 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000665 return NULL;
666 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200667 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000668 sha_update(new, buf.buf, buf.len);
669 PyBuffer_Release(&buf);
670 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000671
672 return (PyObject *)new;
673}
674
675
676/* List of functions exported by this module */
677
678static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200679 _SHA256_SHA256_METHODDEF
680 _SHA256_SHA224_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000682};
683
684
685/* Initialize this module. */
686
Martin v. Löwis1a214512008-06-11 05:26:20 +0000687static struct PyModuleDef _sha256module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyModuleDef_HEAD_INIT,
689 "_sha256",
690 NULL,
691 -1,
692 SHA_functions,
693 NULL,
694 NULL,
695 NULL,
696 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000697};
698
699PyMODINIT_FUNC
700PyInit__sha256(void)
701{
Christian Heimes327dd732013-10-22 15:05:23 +0200702 PyObject *m;
703
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100704 Py_SET_TYPE(&SHA224type, &PyType_Type);
705 if (PyType_Ready(&SHA224type) < 0) {
Martin v. Löwis1a214512008-06-11 05:26:20 +0000706 return NULL;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100707 }
708 Py_SET_TYPE(&SHA256type, &PyType_Type);
709 if (PyType_Ready(&SHA256type) < 0) {
Martin v. Löwis1a214512008-06-11 05:26:20 +0000710 return NULL;
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100711 }
Christian Heimes327dd732013-10-22 15:05:23 +0200712
713 m = PyModule_Create(&_sha256module);
714 if (m == NULL)
715 return NULL;
716
717 Py_INCREF((PyObject *)&SHA224type);
718 PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type);
719 Py_INCREF((PyObject *)&SHA256type);
720 PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type);
721 return m;
722
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000723}