blob: 957fd2b8f9b35e0726470c95ad297c1708a99ac8 [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
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030055#include "clinic/sha256module.c.h"
56
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000057/* When run on a little-endian CPU we need to perform byte reversal on an
58 array of longwords. */
59
Christian Heimes743e0cd2012-10-17 23:52:17 +020060#if PY_LITTLE_ENDIAN
61static void longReverse(SHA_INT32 *buffer, int byteCount)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000062{
63 SHA_INT32 value;
64
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000065 byteCount /= sizeof(*buffer);
66 while (byteCount--) {
67 value = *buffer;
68 value = ( ( value & 0xFF00FF00L ) >> 8 ) | \
69 ( ( value & 0x00FF00FFL ) << 8 );
70 *buffer++ = ( value << 16 ) | ( value >> 16 );
71 }
72}
Christian Heimes743e0cd2012-10-17 23:52:17 +020073#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000074
75static void SHAcopy(SHAobject *src, SHAobject *dest)
76{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000077 dest->local = src->local;
78 dest->digestsize = src->digestsize;
79 dest->count_lo = src->count_lo;
80 dest->count_hi = src->count_hi;
81 memcpy(dest->digest, src->digest, sizeof(src->digest));
82 memcpy(dest->data, src->data, sizeof(src->data));
83}
84
85
86/* ------------------------------------------------------------------------
87 *
88 * This code for the SHA-256 algorithm was noted as public domain. The
89 * original headers are pasted below.
90 *
91 * Several changes have been made to make it more compatible with the
92 * Python environment and desired interface.
93 *
94 */
95
96/* LibTomCrypt, modular cryptographic library -- Tom St Denis
97 *
98 * LibTomCrypt is a library that provides various cryptographic
99 * algorithms in a highly modular and flexible manner.
100 *
101 * The library is free for all purposes without any express
102 * gurantee it works.
103 *
Gregory P. Smith2f21eb32007-09-09 06:44:34 +0000104 * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000105 */
106
107
108/* SHA256 by Tom St Denis */
109
110/* Various logical functions */
111#define ROR(x, y)\
112( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | \
113((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
114#define Ch(x,y,z) (z ^ (x & (y ^ z)))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115#define Maj(x,y,z) (((x | y) & z) | (x & y))
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000116#define S(x, n) ROR((x),(n))
117#define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
118#define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
119#define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
120#define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
121#define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
122
123
124static void
125sha_transform(SHAobject *sha_info)
126{
127 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 SHA_INT32 S[8], W[64], t0, t1;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000129
130 memcpy(W, sha_info->data, sizeof(sha_info->data));
Christian Heimes743e0cd2012-10-17 23:52:17 +0200131#if PY_LITTLE_ENDIAN
132 longReverse(W, (int)sizeof(sha_info->data));
133#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000134
135 for (i = 16; i < 64; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000137 }
138 for (i = 0; i < 8; ++i) {
139 S[i] = sha_info->digest[i];
140 }
141
142 /* Compress */
143#define RND(a,b,c,d,e,f,g,h,i,ki) \
144 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
145 t1 = Sigma0(a) + Maj(a, b, c); \
146 d += t0; \
147 h = t0 + t1;
148
149 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
150 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
151 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
152 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
153 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
154 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
155 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
156 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
157 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
158 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
159 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
160 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
161 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
162 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
163 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
164 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
165 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
166 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
167 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
168 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
169 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
170 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
171 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
172 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
173 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
174 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
175 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
176 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
177 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
178 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
179 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
180 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
181 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
182 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
183 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
184 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
185 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
186 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
187 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
188 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
189 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
190 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
191 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
192 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
193 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
194 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
195 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
196 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
197 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
198 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
199 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
200 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
201 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
202 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
203 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
204 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
205 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
206 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
207 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
208 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
209 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
210 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
211 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
212 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214#undef RND
215
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000216 /* feedback */
217 for (i = 0; i < 8; i++) {
218 sha_info->digest[i] = sha_info->digest[i] + S[i];
219 }
220
221}
222
223
224
225/* initialize the SHA digest */
226
227static void
228sha_init(SHAobject *sha_info)
229{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000230 sha_info->digest[0] = 0x6A09E667L;
231 sha_info->digest[1] = 0xBB67AE85L;
232 sha_info->digest[2] = 0x3C6EF372L;
233 sha_info->digest[3] = 0xA54FF53AL;
234 sha_info->digest[4] = 0x510E527FL;
235 sha_info->digest[5] = 0x9B05688CL;
236 sha_info->digest[6] = 0x1F83D9ABL;
237 sha_info->digest[7] = 0x5BE0CD19L;
238 sha_info->count_lo = 0L;
239 sha_info->count_hi = 0L;
240 sha_info->local = 0;
241 sha_info->digestsize = 32;
242}
243
244static void
245sha224_init(SHAobject *sha_info)
246{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000247 sha_info->digest[0] = 0xc1059ed8L;
248 sha_info->digest[1] = 0x367cd507L;
249 sha_info->digest[2] = 0x3070dd17L;
250 sha_info->digest[3] = 0xf70e5939L;
251 sha_info->digest[4] = 0xffc00b31L;
252 sha_info->digest[5] = 0x68581511L;
253 sha_info->digest[6] = 0x64f98fa7L;
254 sha_info->digest[7] = 0xbefa4fa4L;
255 sha_info->count_lo = 0L;
256 sha_info->count_hi = 0L;
257 sha_info->local = 0;
258 sha_info->digestsize = 28;
259}
260
261
262/* update the SHA digest */
263
264static void
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000265sha_update(SHAobject *sha_info, SHA_BYTE *buffer, Py_ssize_t count)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000266{
Victor Stinner0fcab4a2011-01-04 12:59:15 +0000267 Py_ssize_t i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000268 SHA_INT32 clo;
269
270 clo = sha_info->count_lo + ((SHA_INT32) count << 3);
271 if (clo < sha_info->count_lo) {
272 ++sha_info->count_hi;
273 }
274 sha_info->count_lo = clo;
275 sha_info->count_hi += (SHA_INT32) count >> 29;
276 if (sha_info->local) {
277 i = SHA_BLOCKSIZE - sha_info->local;
278 if (i > count) {
279 i = count;
280 }
281 memcpy(((SHA_BYTE *) sha_info->data) + sha_info->local, buffer, i);
282 count -= i;
283 buffer += i;
Victor Stinner70792d22013-05-08 00:00:44 +0200284 sha_info->local += (int)i;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000285 if (sha_info->local == SHA_BLOCKSIZE) {
286 sha_transform(sha_info);
287 }
288 else {
289 return;
290 }
291 }
292 while (count >= SHA_BLOCKSIZE) {
293 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
294 buffer += SHA_BLOCKSIZE;
295 count -= SHA_BLOCKSIZE;
296 sha_transform(sha_info);
297 }
298 memcpy(sha_info->data, buffer, count);
Victor Stinner70792d22013-05-08 00:00:44 +0200299 sha_info->local = (int)count;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000300}
301
302/* finish computing the SHA digest */
303
304static void
305sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
306{
307 int count;
308 SHA_INT32 lo_bit_count, hi_bit_count;
309
310 lo_bit_count = sha_info->count_lo;
311 hi_bit_count = sha_info->count_hi;
312 count = (int) ((lo_bit_count >> 3) & 0x3f);
313 ((SHA_BYTE *) sha_info->data)[count++] = 0x80;
314 if (count > SHA_BLOCKSIZE - 8) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 memset(((SHA_BYTE *) sha_info->data) + count, 0,
316 SHA_BLOCKSIZE - count);
317 sha_transform(sha_info);
318 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000319 }
320 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 memset(((SHA_BYTE *) sha_info->data) + count, 0,
322 SHA_BLOCKSIZE - 8 - count);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000323 }
324
325 /* GJS: note that we add the hi/lo in big-endian. sha_transform will
326 swap these values into host-order. */
327 sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
328 sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
329 sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
330 sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
331 sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
332 sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
333 sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
334 sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
335 sha_transform(sha_info);
336 digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
337 digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
338 digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
339 digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
340 digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
341 digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
342 digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
343 digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
344 digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
345 digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
346 digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
347 digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
348 digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
349 digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
350 digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
351 digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
352 digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
353 digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
354 digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
355 digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
356 digest[20] = (unsigned char) ((sha_info->digest[5] >> 24) & 0xff);
357 digest[21] = (unsigned char) ((sha_info->digest[5] >> 16) & 0xff);
358 digest[22] = (unsigned char) ((sha_info->digest[5] >> 8) & 0xff);
359 digest[23] = (unsigned char) ((sha_info->digest[5] ) & 0xff);
360 digest[24] = (unsigned char) ((sha_info->digest[6] >> 24) & 0xff);
361 digest[25] = (unsigned char) ((sha_info->digest[6] >> 16) & 0xff);
362 digest[26] = (unsigned char) ((sha_info->digest[6] >> 8) & 0xff);
363 digest[27] = (unsigned char) ((sha_info->digest[6] ) & 0xff);
364 digest[28] = (unsigned char) ((sha_info->digest[7] >> 24) & 0xff);
365 digest[29] = (unsigned char) ((sha_info->digest[7] >> 16) & 0xff);
366 digest[30] = (unsigned char) ((sha_info->digest[7] >> 8) & 0xff);
367 digest[31] = (unsigned char) ((sha_info->digest[7] ) & 0xff);
368}
369
370/*
371 * End of copied SHA code.
372 *
373 * ------------------------------------------------------------------------
374 */
375
376static PyTypeObject SHA224type;
377static PyTypeObject SHA256type;
378
379
380static SHAobject *
381newSHA224object(void)
382{
383 return (SHAobject *)PyObject_New(SHAobject, &SHA224type);
384}
385
386static SHAobject *
387newSHA256object(void)
388{
389 return (SHAobject *)PyObject_New(SHAobject, &SHA256type);
390}
391
392/* Internal methods for a hash object */
393
394static void
395SHA_dealloc(PyObject *ptr)
396{
397 PyObject_Del(ptr);
398}
399
400
401/* External methods for a hash object */
402
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200403/*[clinic input]
404SHA256Type.copy
405
406Return a copy of the hash object.
407[clinic start generated code]*/
408
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200409static PyObject *
410SHA256Type_copy_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300411/*[clinic end generated code: output=1a8bbd66a0c9c168 input=f58840a618d4f2a7]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000412{
413 SHAobject *newobj;
414
Christian Heimes90aa7642007-12-19 02:45:37 +0000415 if (Py_TYPE(self) == &SHA256type) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000416 if ( (newobj = newSHA256object())==NULL)
417 return NULL;
418 } else {
419 if ( (newobj = newSHA224object())==NULL)
420 return NULL;
421 }
422
423 SHAcopy(self, newobj);
424 return (PyObject *)newobj;
425}
426
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200427/*[clinic input]
428SHA256Type.digest
429
430Return the digest value as a string of binary data.
431[clinic start generated code]*/
432
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200433static PyObject *
434SHA256Type_digest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300435/*[clinic end generated code: output=46616a5e909fbc3d input=1fb752e58954157d]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000436{
437 unsigned char digest[SHA_DIGESTSIZE];
438 SHAobject temp;
439
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000440 SHAcopy(self, &temp);
441 sha_final(digest, &temp);
Christian Heimes72b710a2008-05-26 13:28:38 +0000442 return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000443}
444
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200445/*[clinic input]
446SHA256Type.hexdigest
447
448Return the digest value as a string of hexadecimal digits.
449[clinic start generated code]*/
450
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200451static PyObject *
452SHA256Type_hexdigest_impl(SHAobject *self)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300453/*[clinic end generated code: output=725f8a7041ae97f3 input=0cc4c714693010d1]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000454{
455 unsigned char digest[SHA_DIGESTSIZE];
456 SHAobject temp;
457 PyObject *retval;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200458 Py_UCS1 *hex_digest;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000459 int i, j;
460
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000461 /* Get the raw (binary) digest value */
462 SHAcopy(self, &temp);
463 sha_final(digest, &temp);
464
465 /* Create a new string */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200466 retval = PyUnicode_New(self->digestsize * 2, 127);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000467 if (!retval)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200469 hex_digest = PyUnicode_1BYTE_DATA(retval);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000470
471 /* Make hex version of the digest */
472 for(i=j=0; i<self->digestsize; i++) {
Victor Stinnerf5cff562011-10-14 02:13:11 +0200473 unsigned char c;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000474 c = (digest[i] >> 4) & 0xf;
Victor Stinnerf5cff562011-10-14 02:13:11 +0200475 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000476 c = (digest[i] & 0xf);
Victor Stinnerf5cff562011-10-14 02:13:11 +0200477 hex_digest[j++] = Py_hexdigits[c];
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000478 }
Christian Heimesf402e922013-01-03 09:21:55 +0100479#ifdef Py_DEBUG
Victor Stinner8f825062012-04-27 13:55:39 +0200480 assert(_PyUnicode_CheckConsistency(retval, 1));
Christian Heimesf402e922013-01-03 09:21:55 +0100481#endif
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000482 return retval;
483}
484
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200485/*[clinic input]
486SHA256Type.update
487
488 obj: object
489 /
490
491Update this hash object's state with the provided string.
492[clinic start generated code]*/
493
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000494static PyObject *
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200495SHA256Type_update(SHAobject *self, PyObject *obj)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300496/*[clinic end generated code: output=0967fb2860c66af7 input=b2d449d5b30f0f5a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000497{
Gregory P. Smith365a1862009-02-12 07:35:29 +0000498 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000499
Gregory P. Smith365a1862009-02-12 07:35:29 +0000500 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000501
Gregory P. Smith365a1862009-02-12 07:35:29 +0000502 sha_update(self, buf.buf, buf.len);
503
504 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000505 Py_INCREF(Py_None);
506 return Py_None;
507}
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
549static PyTypeObject SHA224type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000550 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 "_sha256.sha224", /*tp_name*/
552 sizeof(SHAobject), /*tp_size*/
553 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000554 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 SHA_dealloc, /*tp_dealloc*/
556 0, /*tp_print*/
557 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000558 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000559 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000560 0, /*tp_repr*/
561 0, /*tp_as_number*/
562 0, /*tp_as_sequence*/
563 0, /*tp_as_mapping*/
564 0, /*tp_hash*/
565 0, /*tp_call*/
566 0, /*tp_str*/
567 0, /*tp_getattro*/
568 0, /*tp_setattro*/
569 0, /*tp_as_buffer*/
570 Py_TPFLAGS_DEFAULT, /*tp_flags*/
571 0, /*tp_doc*/
572 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 0, /*tp_clear*/
574 0, /*tp_richcompare*/
575 0, /*tp_weaklistoffset*/
576 0, /*tp_iter*/
577 0, /*tp_iternext*/
578 SHA_methods, /* tp_methods */
579 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000580 SHA_getseters, /* tp_getset */
581};
582
583static PyTypeObject SHA256type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000584 PyVarObject_HEAD_INIT(NULL, 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 "_sha256.sha256", /*tp_name*/
586 sizeof(SHAobject), /*tp_size*/
587 0, /*tp_itemsize*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000588 /* methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 SHA_dealloc, /*tp_dealloc*/
590 0, /*tp_print*/
591 0, /*tp_getattr*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000592 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +0000593 0, /*tp_reserved*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000594 0, /*tp_repr*/
595 0, /*tp_as_number*/
596 0, /*tp_as_sequence*/
597 0, /*tp_as_mapping*/
598 0, /*tp_hash*/
599 0, /*tp_call*/
600 0, /*tp_str*/
601 0, /*tp_getattro*/
602 0, /*tp_setattro*/
603 0, /*tp_as_buffer*/
604 Py_TPFLAGS_DEFAULT, /*tp_flags*/
605 0, /*tp_doc*/
606 0, /*tp_traverse*/
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 0, /*tp_clear*/
608 0, /*tp_richcompare*/
609 0, /*tp_weaklistoffset*/
610 0, /*tp_iter*/
611 0, /*tp_iternext*/
612 SHA_methods, /* tp_methods */
613 SHA_members, /* tp_members */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000614 SHA_getseters, /* tp_getset */
615};
616
617
618/* The single module-level function: new() */
619
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200620/*[clinic input]
621_sha256.sha256
622
623 string: object(c_default="NULL") = b''
624
625Return a new SHA-256 hash object; optionally initialized with a string.
626[clinic start generated code]*/
627
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200628static PyObject *
629_sha256_sha256_impl(PyModuleDef *module, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300630/*[clinic end generated code: output=d70e6e2d97112844 input=09cce3fb855056b2]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200631{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000632 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000633 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000634
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200635 if (string)
636 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000637
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000638 if ((new = newSHA256object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200639 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000640 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000641 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000642 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000643
644 sha_init(new);
645
646 if (PyErr_Occurred()) {
647 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200648 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000649 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000650 return NULL;
651 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200652 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000653 sha_update(new, buf.buf, buf.len);
654 PyBuffer_Release(&buf);
655 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000656
657 return (PyObject *)new;
658}
659
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200660/*[clinic input]
661_sha256.sha224
662
663 string: object(c_default="NULL") = b''
664
665Return a new SHA-224 hash object; optionally initialized with a string.
666[clinic start generated code]*/
667
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200668static PyObject *
669_sha256_sha224_impl(PyModuleDef *module, PyObject *string)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300670/*[clinic end generated code: output=f2822bf28416b42a input=27a04ba24c353a73]*/
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200671{
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000672 SHAobject *new;
Gregory P. Smith365a1862009-02-12 07:35:29 +0000673 Py_buffer buf;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000674
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200675 if (string)
676 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
Gregory P. Smith365a1862009-02-12 07:35:29 +0000677
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000678 if ((new = newSHA224object()) == NULL) {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200679 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000680 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000681 return NULL;
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000682 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000683
684 sha224_init(new);
685
686 if (PyErr_Occurred()) {
687 Py_DECREF(new);
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200688 if (string)
Hirokazu Yamamoto84047492009-03-03 07:49:01 +0000689 PyBuffer_Release(&buf);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000690 return NULL;
691 }
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200692 if (string) {
Gregory P. Smith365a1862009-02-12 07:35:29 +0000693 sha_update(new, buf.buf, buf.len);
694 PyBuffer_Release(&buf);
695 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000696
697 return (PyObject *)new;
698}
699
700
701/* List of functions exported by this module */
702
703static struct PyMethodDef SHA_functions[] = {
Martin v. Löwis501b13c2014-07-27 14:20:23 +0200704 _SHA256_SHA256_METHODDEF
705 _SHA256_SHA224_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000707};
708
709
710/* Initialize this module. */
711
712#define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
713
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000714
Martin v. Löwis1a214512008-06-11 05:26:20 +0000715static struct PyModuleDef _sha256module = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 PyModuleDef_HEAD_INIT,
717 "_sha256",
718 NULL,
719 -1,
720 SHA_functions,
721 NULL,
722 NULL,
723 NULL,
724 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +0000725};
726
727PyMODINIT_FUNC
728PyInit__sha256(void)
729{
Christian Heimes327dd732013-10-22 15:05:23 +0200730 PyObject *m;
731
Christian Heimes90aa7642007-12-19 02:45:37 +0000732 Py_TYPE(&SHA224type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000733 if (PyType_Ready(&SHA224type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000734 return NULL;
Christian Heimes90aa7642007-12-19 02:45:37 +0000735 Py_TYPE(&SHA256type) = &PyType_Type;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000736 if (PyType_Ready(&SHA256type) < 0)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000737 return NULL;
Christian Heimes327dd732013-10-22 15:05:23 +0200738
739 m = PyModule_Create(&_sha256module);
740 if (m == NULL)
741 return NULL;
742
743 Py_INCREF((PyObject *)&SHA224type);
744 PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type);
745 Py_INCREF((PyObject *)&SHA256type);
746 PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type);
747 return m;
748
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000749}