blob: 7b1488696f490f49c5562cb651def10364253f67 [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"
20#include "structmember.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000021#include "hashlib.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000022
Martin v. Löwis501b13c2014-07-27 14:20:23 +020023/*[clinic input]
24module _sha256
25class SHA256Type "SHAobject *" "&PyType_Type"
26[clinic start generated code]*/
27/*[clinic end generated code: output=da39a3ee5e6b4b0d input=71a39174d4f0a744]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000028
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000029/* Some useful types */
30
31typedef unsigned char SHA_BYTE;
32
33#if SIZEOF_INT == 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034typedef unsigned int SHA_INT32; /* 32-bit integer */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000035#else
36/* not defined. compilation will die. */
37#endif
38
39/* The SHA block size and message digest sizes, in bytes */
40
41#define SHA_BLOCKSIZE 64
42#define SHA_DIGESTSIZE 32
43
44/* The structure for storing SHA info */
45
46typedef struct {
47 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 SHA_INT32 digest[8]; /* Message digest */
49 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
50 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 int local; /* unprocessed amount in data */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000052 int digestsize;
53} SHAobject;
54
55/* When run on a little-endian CPU we need to perform byte reversal on an
56 array of longwords. */
57
Christian Heimes743e0cd2012-10-17 23:52:17 +020058#if PY_LITTLE_ENDIAN
59static void longReverse(SHA_INT32 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000060{
61 SHA_INT32 value;
62
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000063 byteCount /= sizeof(*buffer);
64 while (byteCount--) {
65 value = *buffer;
66 value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
67 ( ( value & 0x00FF00FFL ) << 8 );
68 *buffer++ = ( value << 16 ) | ( value >> 16 );
69 }
70}
Christian Heimes743e0cd2012-10-17 23:52:17 +020071#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000072
73static void SHAcopy(SHAobject *src, SHAobject *dest)
74{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000075 dest->local = src->local;
76 dest->digestsize = src->digestsize;
77 dest->count_lo = src->count_lo;
78 dest->count_hi = src->count_hi;
79 memcpy(dest->digest, src->digest, sizeof(src->digest));
80 memcpy(dest->data, src->data, sizeof(src->data));
81}
82
83
84/* ------------------------------------------------------------------------
85 *
86 * This code for the SHA-256 algorithm was noted as public domain. The
87 * original headers are pasted below.
88 *
89 * Several changes have been made to make it more compatible with the
90 * Python environment and desired interface.
91 *
92 */
93
94/* LibTomCrypt, modular cryptographic library -- Tom St Denis
95 *
96 * LibTomCrypt is a library that provides various cryptographic
97 * algorithms in a highly modular and flexible manner.
98 *
99 * The library is free for all purposes without any express
100 * gurantee it works.
101 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000102 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000103 */
104
105
106/* SHA256 by Tom St Denis */
107
108/* Various logical functions */
109#define ROR(x, y)\
110( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
111((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
112#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000114#define S(x, n) ROR((x),(n))
115#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
116#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
117#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
118#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
119#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
120
121
122static void
123sha_transform(SHAobject *sha_info)
124{
125 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 SHA_INT32 S[8], W[64], t0, t1;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000127
128 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200129#if PY_LITTLE_ENDIAN
130 longReverse(W, (int)sizeof(sha_info->data));
131#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000132
133 for (i = 16; i < 64; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000135 }
136 for (i = 0; i < 8; ++i) {
137 S[i] = sha_info->digest[i];
138 }
139
140 /* Compress */
141#define RND(a,b,c,d,e,f,g,h,i,ki) \
142 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
143 t1 = Sigma0(a) + Maj(a, b, c); \
144 d += t0; \
145 h = t0 + t1;
146
147 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
148 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
149 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
150 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
151 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
152 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
153 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
154 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
155 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
156 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
157 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
158 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
159 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
160 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
161 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
162 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
163 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
164 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
165 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
166 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
167 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
168 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
169 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
170 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
171 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
172 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
173 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
174 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
175 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
176 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
177 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
178 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
179 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
180 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
181 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
182 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
183 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
184 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
185 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
186 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
187 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
188 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
189 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
190 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
191 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
192 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
193 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
194 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
195 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
196 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
197 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
198 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
199 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
200 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
201 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
202 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
203 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
204 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
205 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
206 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
207 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
208 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
209 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
210 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212#undef RND
213
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000214 /* feedback */
215 for (i = 0; i < 8; i++) {
216 sha_info->digest[i] = sha_info->digest[i] + S[i];
217 }
218
219}
220
221
222
223/* initialize the SHA digest */
224
225static void
226sha_init(SHAobject *sha_info)
227{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000228 sha_info->digest[0] = 0x6A09E667L;
229 sha_info->digest[1] = 0xBB67AE85L;
230 sha_info->digest[2] = 0x3C6EF372L;
231 sha_info->digest[3] = 0xA54FF53AL;
232 sha_info->digest[4] = 0x510E527FL;
233 sha_info->digest[5] = 0x9B05688CL;
234 sha_info->digest[6] = 0x1F83D9ABL;
235 sha_info->digest[7] = 0x5BE0CD19L;
236 sha_info->count_lo = 0L;
237 sha_info->count_hi = 0L;
238 sha_info->local = 0;
239 sha_info->digestsize = 32;
240}
241
242static void
243sha224_init(SHAobject *sha_info)
244{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000245 sha_info->digest[0] = 0xc1059ed8L;
246 sha_info->digest[1] = 0x367cd507L;
247 sha_info->digest[2] = 0x3070dd17L;
248 sha_info->digest[3] = 0xf70e5939L;
249 sha_info->digest[4] = 0xffc00b31L;
250 sha_info->digest[5] = 0x68581511L;
251 sha_info->digest[6] = 0x64f98fa7L;
252 sha_info->digest[7] = 0xbefa4fa4L;
253 sha_info->count_lo = 0L;
254 sha_info->count_hi = 0L;
255 sha_info->local = 0;
256 sha_info->digestsize = 28;
257}
258
259
260/* update the SHA digest */
261
262static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000263sha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000264{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000265 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000266 SHA_INT32 clo;
267
268 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
269 if (clo < sha_info->count_lo) {
270 ++sha_info->count_hi;
271 }
272 sha_info->count_lo = clo;
273 sha_info->count_hi += (SHA_INT32) count >> 29;
274 if (sha_info->local) {
275 i = SHA_BLOCKSIZE - sha_info->local;
276 if (i > count) {
277 i = count;
278 }
279 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
280 count -= i;
281 buffer += i;
Victor Stinner70792d22013-05-08 00:00:44 +0200282 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000283 if (sha_info->local == SHA_BLOCKSIZE) {
284 sha_transform(sha_info);
285 }
286 else {
287 return;
288 }
289 }
290 while (count >= SHA_BLOCKSIZE) {
291 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
292 buffer += SHA_BLOCKSIZE;
293 count -= SHA_BLOCKSIZE;
294 sha_transform(sha_info);
295 }
296 memcpy(sha_info->data, buffer, count);
Victor Stinner70792d22013-05-08 00:00:44 +0200297 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000298}
299
300/* finish computing the SHA digest */
301
302static void
303sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
304{
305 int count;
306 SHA_INT32 lo_bit_count, hi_bit_count;
307
308 lo_bit_count = sha_info->count_lo;
309 hi_bit_count = sha_info->count_hi;
310 count = (int) ((lo_bit_count >> 3) & 0x3f);
311 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
312 if (count > SHA_BLOCKSIZE - 8) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 memset(((SHA_BYTE *) sha_info->data) + count, 0,
314 SHA_BLOCKSIZE - count);
315 sha_transform(sha_info);
316 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000317 }
318 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 memset(((SHA_BYTE *) sha_info->data) + count, 0,
320 SHA_BLOCKSIZE - 8 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000321 }
322
323 /* GJS: note that we add the hi/lo in big-endian. sha_transform will
324 swap these values into host-order. */
325 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
326 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
327 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
328 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
329 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
330 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
331 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
332 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
333 sha_transform(sha_info);
334 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
335 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
336 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
337 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
338 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
339 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
340 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
341 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
342 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
343 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
344 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
345 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
346 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
347 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
348 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
349 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
350 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
351 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
352 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
353 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
354 digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
355 digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
356 digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
357 digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
358 digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
359 digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
360 digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
361 digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
362 digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
363 digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
364 digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
365 digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
366}
367
368/*
369 * End of copied SHA code.
370 *
371 * ------------------------------------------------------------------------
372 */
373
374static PyTypeObject SHA224type;
375static PyTypeObject SHA256type;
376
377
378static SHAobject *
379newSHA224object(void)
380{
381 return (SHAobject *)PyObject_New(SHAobject, &SHA224type);
382}
383
384static SHAobject *
385newSHA256object(void)
386{
387 return (SHAobject *)PyObject_New(SHAobject, &SHA256type);
388}
389
390/* Internal methods for a hash object */
391
392static void
393SHA_dealloc(PyObject *ptr)
394{
395 PyObject_Del(ptr);
396}
397
398
399/* External methods for a hash object */
400
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200401/*[clinic input]
402SHA256Type.copy
403
404Return a copy of the hash object.
405[clinic start generated code]*/
406
407PyDoc_STRVAR(SHA256Type_copy__doc__,
408"copy($self, /)\n"
409"--\n"
410"\n"
411"Return a copy of the hash object.");
412
413#define SHA256TYPE_COPY_METHODDEF \
414 {"copy", (PyCFunction)SHA256Type_copy, METH_NOARGS, SHA256Type_copy__doc__},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000415
416static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200417SHA256Type_copy_impl(SHAobject *self);
418
419static PyObject *
420SHA256Type_copy(SHAobject *self, PyObject *Py_UNUSED(ignored))
421{
422 return SHA256Type_copy_impl(self);
423}
424
425static PyObject *
426SHA256Type_copy_impl(SHAobject *self)
427/*[clinic end generated code: output=f716c39d3f81c27c input=f58840a618d4f2a7]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000428{
429 SHAobject *newobj;
430
Christian Heimes90aa7642007-12-19 02:45:37 +0000431 if (Py_TYPE(self) == &SHA256type) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000432 if ( (newobj = newSHA256object())==NULL)
433 return NULL;
434 } else {
435 if ( (newobj = newSHA224object())==NULL)
436 return NULL;
437 }
438
439 SHAcopy(self, newobj);
440 return (PyObject *)newobj;
441}
442
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200443/*[clinic input]
444SHA256Type.digest
445
446Return the digest value as a string of binary data.
447[clinic start generated code]*/
448
449PyDoc_STRVAR(SHA256Type_digest__doc__,
450"digest($self, /)\n"
451"--\n"
452"\n"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000453"Return the digest value as a string of binary data.");
454
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200455#define SHA256TYPE_DIGEST_METHODDEF \
456 {"digest", (PyCFunction)SHA256Type_digest, METH_NOARGS, SHA256Type_digest__doc__},
457
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000458static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200459SHA256Type_digest_impl(SHAobject *self);
460
461static PyObject *
462SHA256Type_digest(SHAobject *self, PyObject *Py_UNUSED(ignored))
463{
464 return SHA256Type_digest_impl(self);
465}
466
467static PyObject *
468SHA256Type_digest_impl(SHAobject *self)
469/*[clinic end generated code: output=72d34723d7bb694c input=1fb752e58954157d]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000470{
471 unsigned char digest[SHA_DIGESTSIZE];
472 SHAobject temp;
473
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000474 SHAcopy(self, &temp);
475 sha_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000476 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000477}
478
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200479/*[clinic input]
480SHA256Type.hexdigest
481
482Return the digest value as a string of hexadecimal digits.
483[clinic start generated code]*/
484
485PyDoc_STRVAR(SHA256Type_hexdigest__doc__,
486"hexdigest($self, /)\n"
487"--\n"
488"\n"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000489"Return the digest value as a string of hexadecimal digits.");
490
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200491#define SHA256TYPE_HEXDIGEST_METHODDEF \
492 {"hexdigest", (PyCFunction)SHA256Type_hexdigest, METH_NOARGS, SHA256Type_hexdigest__doc__},
493
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000494static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200495SHA256Type_hexdigest_impl(SHAobject *self);
496
497static PyObject *
498SHA256Type_hexdigest(SHAobject *self, PyObject *Py_UNUSED(ignored))
499{
500 return SHA256Type_hexdigest_impl(self);
501}
502
503static PyObject *
504SHA256Type_hexdigest_impl(SHAobject *self)
505/*[clinic end generated code: output=3687aa6183c7d27f input=0cc4c714693010d1]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000506{
507 unsigned char digest[SHA_DIGESTSIZE];
508 SHAobject temp;
509 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200510 Py_UCS1 *hex_digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000511 int i, j;
512
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000513 /* Get the raw (binary) digest value */
514 SHAcopy(self, &temp);
515 sha_final(digest, &temp);
516
517 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200518 retval = PyUnicode_New(self->digestsize * 2, 127);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000519 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200521 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000522
523 /* Make hex version of the digest */
524 for(i=j=0; i<self->digestsize; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200525 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000526 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200527 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000528 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200529 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000530 }
Christian Heimesf402e922013-01-03 09:21:55 +0100531#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200532 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100533#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000534 return retval;
535}
536
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200537/*[clinic input]
538SHA256Type.update
539
540 obj: object
541 /
542
543Update this hash object's state with the provided string.
544[clinic start generated code]*/
545
546PyDoc_STRVAR(SHA256Type_update__doc__,
547"update($self, obj, /)\n"
548"--\n"
549"\n"
550"Update this hash object\'s state with the provided string.");
551
552#define SHA256TYPE_UPDATE_METHODDEF \
553 {"update", (PyCFunction)SHA256Type_update, METH_O, SHA256Type_update__doc__},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000554
555static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200556SHA256Type_update(SHAobject *self, PyObject *obj)
557/*[clinic end generated code: output=b47f53d7cbeabee4 input=b2d449d5b30f0f5a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000558{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000559 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000560
Gregory P. Smith365a1862009-02-12 07:35:29 +0000561 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000562
Gregory P. Smith365a1862009-02-12 07:35:29 +0000563 sha_update(self, buf.buf, buf.len);
564
565 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000566 Py_INCREF(Py_None);
567 return Py_None;
568}
569
570static PyMethodDef SHA_methods[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200571 SHA256TYPE_COPY_METHODDEF
572 SHA256TYPE_DIGEST_METHODDEF
573 SHA256TYPE_HEXDIGEST_METHODDEF
574 SHA256TYPE_UPDATE_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000576};
577
578static PyObject *
579SHA256_get_block_size(PyObject *self, void *closure)
580{
Christian Heimes217cfd12007-12-02 14:31:20 +0000581 return PyLong_FromLong(SHA_BLOCKSIZE);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000582}
583
584static PyObject *
585SHA256_get_name(PyObject *self, void *closure)
586{
587 if (((SHAobject *)self)->digestsize == 32)
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200588 return PyUnicode_FromStringAndSize("sha256", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000589 else
Christian Heimes37d5ceb2013-08-15 18:31:48 +0200590 return PyUnicode_FromStringAndSize("sha224", 6);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000591}
592
593static PyGetSetDef SHA_getseters[] = {
594 {"block_size",
595 (getter)SHA256_get_block_size, NULL,
596 NULL,
597 NULL},
598 {"name",
599 (getter)SHA256_get_name, NULL,
600 NULL,
601 NULL},
602 {NULL} /* Sentinel */
603};
604
605static PyMemberDef SHA_members[] = {
606 {"digest_size", T_INT, offsetof(SHAobject, digestsize), READONLY, NULL},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000607 {NULL} /* Sentinel */
608};
609
610static PyTypeObject SHA224type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000611 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 "_sha256.sha224", /*tp_name*/
613 sizeof(SHAobject), /*tp_size*/
614 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000615 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 SHA_dealloc, /*tp_dealloc*/
617 0, /*tp_print*/
618 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000619 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000620 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000621 0, /*tp_repr*/
622 0, /*tp_as_number*/
623 0, /*tp_as_sequence*/
624 0, /*tp_as_mapping*/
625 0, /*tp_hash*/
626 0, /*tp_call*/
627 0, /*tp_str*/
628 0, /*tp_getattro*/
629 0, /*tp_setattro*/
630 0, /*tp_as_buffer*/
631 Py_TPFLAGS_DEFAULT, /*tp_flags*/
632 0, /*tp_doc*/
633 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 0, /*tp_clear*/
635 0, /*tp_richcompare*/
636 0, /*tp_weaklistoffset*/
637 0, /*tp_iter*/
638 0, /*tp_iternext*/
639 SHA_methods, /* tp_methods */
640 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000641 SHA_getseters, /* tp_getset */
642};
643
644static PyTypeObject SHA256type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000645 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 "_sha256.sha256", /*tp_name*/
647 sizeof(SHAobject), /*tp_size*/
648 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000649 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 SHA_dealloc, /*tp_dealloc*/
651 0, /*tp_print*/
652 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000653 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000654 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000655 0, /*tp_repr*/
656 0, /*tp_as_number*/
657 0, /*tp_as_sequence*/
658 0, /*tp_as_mapping*/
659 0, /*tp_hash*/
660 0, /*tp_call*/
661 0, /*tp_str*/
662 0, /*tp_getattro*/
663 0, /*tp_setattro*/
664 0, /*tp_as_buffer*/
665 Py_TPFLAGS_DEFAULT, /*tp_flags*/
666 0, /*tp_doc*/
667 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 0, /*tp_clear*/
669 0, /*tp_richcompare*/
670 0, /*tp_weaklistoffset*/
671 0, /*tp_iter*/
672 0, /*tp_iternext*/
673 SHA_methods, /* tp_methods */
674 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000675 SHA_getseters, /* tp_getset */
676};
677
678
679/* The single module-level function: new() */
680
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200681/*[clinic input]
682_sha256.sha256
683
684 string: object(c_default="NULL") = b''
685
686Return a new SHA-256 hash object; optionally initialized with a string.
687[clinic start generated code]*/
688
689PyDoc_STRVAR(_sha256_sha256__doc__,
690"sha256($module, /, string=b\'\')\n"
691"--\n"
692"\n"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000693"Return a new SHA-256 hash object; optionally initialized with a string.");
694
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200695#define _SHA256_SHA256_METHODDEF \
696 {"sha256", (PyCFunction)_sha256_sha256, METH_VARARGS|METH_KEYWORDS, _sha256_sha256__doc__},
697
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000698static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200699_sha256_sha256_impl(PyModuleDef *module, PyObject *string);
700
701static PyObject *
702_sha256_sha256(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000703{
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200704 PyObject *return_value = NULL;
705 static char *_keywords[] = {"string", NULL};
706 PyObject *string = NULL;
707
708 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
709 "|O:sha256", _keywords,
710 &string))
711 goto exit;
712 return_value = _sha256_sha256_impl(module, string);
713
714exit:
715 return return_value;
716}
717
718static PyObject *
719_sha256_sha256_impl(PyModuleDef *module, PyObject *string)
720/*[clinic end generated code: output=4b1263d1e2fcdb98 input=09cce3fb855056b2]*/
721{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000722 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000723 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000724
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200725 if (string)
726 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000727
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000728 if ((new = newSHA256object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200729 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000730 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000731 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000732 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000733
734 sha_init(new);
735
736 if (PyErr_Occurred()) {
737 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200738 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000739 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000740 return NULL;
741 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200742 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000743 sha_update(new, buf.buf, buf.len);
744 PyBuffer_Release(&buf);
745 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000746
747 return (PyObject *)new;
748}
749
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200750/*[clinic input]
751_sha256.sha224
752
753 string: object(c_default="NULL") = b''
754
755Return a new SHA-224 hash object; optionally initialized with a string.
756[clinic start generated code]*/
757
758PyDoc_STRVAR(_sha256_sha224__doc__,
759"sha224($module, /, string=b\'\')\n"
760"--\n"
761"\n"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000762"Return a new SHA-224 hash object; optionally initialized with a string.");
763
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200764#define _SHA256_SHA224_METHODDEF \
765 {"sha224", (PyCFunction)_sha256_sha224, METH_VARARGS|METH_KEYWORDS, _sha256_sha224__doc__},
766
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000767static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200768_sha256_sha224_impl(PyModuleDef *module, PyObject *string);
769
770static PyObject *
771_sha256_sha224(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000772{
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200773 PyObject *return_value = NULL;
774 static char *_keywords[] = {"string", NULL};
775 PyObject *string = NULL;
776
777 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
778 "|O:sha224", _keywords,
779 &string))
780 goto exit;
781 return_value = _sha256_sha224_impl(module, string);
782
783exit:
784 return return_value;
785}
786
787static PyObject *
788_sha256_sha224_impl(PyModuleDef *module, PyObject *string)
789/*[clinic end generated code: output=4dde0eb1cdaebc06 input=27a04ba24c353a73]*/
790{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000791 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000792 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000793
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200794 if (string)
795 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000796
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000797 if ((new = newSHA224object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200798 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000799 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000800 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000801 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000802
803 sha224_init(new);
804
805 if (PyErr_Occurred()) {
806 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200807 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000808 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000809 return NULL;
810 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200811 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000812 sha_update(new, buf.buf, buf.len);
813 PyBuffer_Release(&buf);
814 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000815
816 return (PyObject *)new;
817}
818
819
820/* List of functions exported by this module */
821
822static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200823 _SHA256_SHA256_METHODDEF
824 _SHA256_SHA224_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000826};
827
828
829/* Initialize this module. */
830
831#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
832
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000833
Martin v. Löwis1a214512008-06-11 05:26:20 +0000834static struct PyModuleDef _sha256module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 PyModuleDef_HEAD_INIT,
836 "_sha256",
837 NULL,
838 -1,
839 SHA_functions,
840 NULL,
841 NULL,
842 NULL,
843 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000844};
845
846PyMODINIT_FUNC
847PyInit__sha256(void)
848{
Christian Heimes327dd732013-10-22 15:05:23 +0200849 PyObject *m;
850
Christian Heimes90aa7642007-12-19 02:45:37 +0000851 Py_TYPE(&SHA224type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000852 if (PyType_Ready(&SHA224type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000853 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000854 Py_TYPE(&SHA256type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000855 if (PyType_Ready(&SHA256type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000856 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200857
858 m = PyModule_Create(&_sha256module);
859 if (m == NULL)
860 return NULL;
861
862 Py_INCREF((PyObject *)&SHA224type);
863 PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type);
864 Py_INCREF((PyObject *)&SHA256type);
865 PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type);
866 return m;
867
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000868}