Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 1 | /* SHA1 module */ |
| 2 | |
| 3 | /* This module provides an interface to the SHA1 algorithm */ |
| 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 | |
| 12 | Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) |
| 13 | Licensed to PSF under a Contributor Agreement. |
| 14 | |
| 15 | */ |
| 16 | |
| 17 | /* SHA1 objects */ |
| 18 | |
| 19 | #include "Python.h" |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 20 | #include "hashlib.h" |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 21 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 22 | /*[clinic input] |
| 23 | module _sha1 |
| 24 | class SHA1Type "SHA1object *" "&PyType_Type" |
| 25 | [clinic start generated code]*/ |
| 26 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 27 | |
| 28 | /* Some useful types */ |
| 29 | |
| 30 | #if SIZEOF_INT == 4 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | typedef unsigned int SHA1_INT32; /* 32-bit integer */ |
| 32 | typedef PY_LONG_LONG SHA1_INT64; /* 64-bit integer */ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 33 | #else |
| 34 | /* not defined. compilation will die. */ |
| 35 | #endif |
| 36 | |
| 37 | /* The SHA1 block size and message digest sizes, in bytes */ |
| 38 | |
| 39 | #define SHA1_BLOCKSIZE 64 |
| 40 | #define SHA1_DIGESTSIZE 20 |
| 41 | |
| 42 | /* The structure for storing SHA1 info */ |
| 43 | |
| 44 | struct sha1_state { |
| 45 | SHA1_INT64 length; |
| 46 | SHA1_INT32 state[5], curlen; |
| 47 | unsigned char buf[SHA1_BLOCKSIZE]; |
| 48 | }; |
| 49 | |
| 50 | typedef struct { |
| 51 | PyObject_HEAD |
| 52 | |
| 53 | struct sha1_state hash_state; |
| 54 | } SHA1object; |
| 55 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 56 | #include "clinic/sha1module.c.h" |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 57 | |
| 58 | /* ------------------------------------------------------------------------ |
| 59 | * |
| 60 | * This code for the SHA1 algorithm was noted as public domain. The |
| 61 | * original headers are pasted below. |
| 62 | * |
| 63 | * Several changes have been made to make it more compatible with the |
| 64 | * Python environment and desired interface. |
| 65 | * |
| 66 | */ |
| 67 | |
| 68 | /* LibTomCrypt, modular cryptographic library -- Tom St Denis |
| 69 | * |
| 70 | * LibTomCrypt is a library that provides various cryptographic |
| 71 | * algorithms in a highly modular and flexible manner. |
| 72 | * |
| 73 | * The library is free for all purposes without any express |
| 74 | * guarantee it works. |
| 75 | * |
| 76 | * Tom St Denis, tomstdenis@gmail.com, http://libtom.org |
| 77 | */ |
| 78 | |
| 79 | /* rotate the hard way (platform optimizations could be done) */ |
| 80 | #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
| 81 | #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
| 82 | |
| 83 | /* Endian Neutral macros that work on all platforms */ |
| 84 | |
| 85 | #define STORE32H(x, y) \ |
| 86 | { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ |
| 87 | (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } |
| 88 | |
| 89 | #define LOAD32H(x, y) \ |
| 90 | { x = ((unsigned long)((y)[0] & 255)<<24) | \ |
| 91 | ((unsigned long)((y)[1] & 255)<<16) | \ |
| 92 | ((unsigned long)((y)[2] & 255)<<8) | \ |
| 93 | ((unsigned long)((y)[3] & 255)); } |
| 94 | |
| 95 | #define STORE64H(x, y) \ |
| 96 | { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ |
| 97 | (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ |
| 98 | (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ |
| 99 | (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } |
| 100 | |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 101 | |
| 102 | /* SHA1 macros */ |
| 103 | |
| 104 | #define F0(x,y,z) (z ^ (x & (y ^ z))) |
| 105 | #define F1(x,y,z) (x ^ y ^ z) |
| 106 | #define F2(x,y,z) ((x & y) | (z & (x | y))) |
| 107 | #define F3(x,y,z) (x ^ y ^ z) |
| 108 | |
| 109 | static void sha1_compress(struct sha1_state *sha1, unsigned char *buf) |
| 110 | { |
| 111 | SHA1_INT32 a,b,c,d,e,W[80],i; |
| 112 | |
| 113 | /* copy the state into 512-bits into W[0..15] */ |
| 114 | for (i = 0; i < 16; i++) { |
| 115 | LOAD32H(W[i], buf + (4*i)); |
| 116 | } |
| 117 | |
| 118 | /* copy state */ |
| 119 | a = sha1->state[0]; |
| 120 | b = sha1->state[1]; |
| 121 | c = sha1->state[2]; |
| 122 | d = sha1->state[3]; |
| 123 | e = sha1->state[4]; |
| 124 | |
| 125 | /* expand it */ |
| 126 | for (i = 16; i < 80; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* compress */ |
| 131 | /* round one */ |
Brett Cannon | f079c57 | 2010-07-23 15:43:14 +0000 | [diff] [blame] | 132 | #define FF_0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30); |
| 133 | #define FF_1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30); |
| 134 | #define FF_2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30); |
| 135 | #define FF_3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 137 | for (i = 0; i < 20; ) { |
Brett Cannon | f079c57 | 2010-07-23 15:43:14 +0000 | [diff] [blame] | 138 | FF_0(a,b,c,d,e,i++); |
| 139 | FF_0(e,a,b,c,d,i++); |
| 140 | FF_0(d,e,a,b,c,i++); |
| 141 | FF_0(c,d,e,a,b,i++); |
| 142 | FF_0(b,c,d,e,a,i++); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* round two */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | for (; i < 40; ) { |
Brett Cannon | f079c57 | 2010-07-23 15:43:14 +0000 | [diff] [blame] | 147 | FF_1(a,b,c,d,e,i++); |
| 148 | FF_1(e,a,b,c,d,i++); |
| 149 | FF_1(d,e,a,b,c,i++); |
| 150 | FF_1(c,d,e,a,b,i++); |
| 151 | FF_1(b,c,d,e,a,i++); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* round three */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | for (; i < 60; ) { |
Brett Cannon | f079c57 | 2010-07-23 15:43:14 +0000 | [diff] [blame] | 156 | FF_2(a,b,c,d,e,i++); |
| 157 | FF_2(e,a,b,c,d,i++); |
| 158 | FF_2(d,e,a,b,c,i++); |
| 159 | FF_2(c,d,e,a,b,i++); |
| 160 | FF_2(b,c,d,e,a,i++); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* round four */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | for (; i < 80; ) { |
Brett Cannon | f079c57 | 2010-07-23 15:43:14 +0000 | [diff] [blame] | 165 | FF_3(a,b,c,d,e,i++); |
| 166 | FF_3(e,a,b,c,d,i++); |
| 167 | FF_3(d,e,a,b,c,i++); |
| 168 | FF_3(c,d,e,a,b,i++); |
| 169 | FF_3(b,c,d,e,a,i++); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Brett Cannon | f079c57 | 2010-07-23 15:43:14 +0000 | [diff] [blame] | 172 | #undef FF_0 |
| 173 | #undef FF_1 |
| 174 | #undef FF_2 |
| 175 | #undef FF_3 |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 176 | |
| 177 | /* store */ |
| 178 | sha1->state[0] = sha1->state[0] + a; |
| 179 | sha1->state[1] = sha1->state[1] + b; |
| 180 | sha1->state[2] = sha1->state[2] + c; |
| 181 | sha1->state[3] = sha1->state[3] + d; |
| 182 | sha1->state[4] = sha1->state[4] + e; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | Initialize the hash state |
| 187 | @param sha1 The hash state you wish to initialize |
| 188 | */ |
doko@ubuntu.com | 1e5be8c | 2012-06-21 17:05:50 +0200 | [diff] [blame] | 189 | static void |
| 190 | sha1_init(struct sha1_state *sha1) |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 191 | { |
| 192 | assert(sha1 != NULL); |
| 193 | sha1->state[0] = 0x67452301UL; |
| 194 | sha1->state[1] = 0xefcdab89UL; |
| 195 | sha1->state[2] = 0x98badcfeUL; |
| 196 | sha1->state[3] = 0x10325476UL; |
| 197 | sha1->state[4] = 0xc3d2e1f0UL; |
| 198 | sha1->curlen = 0; |
| 199 | sha1->length = 0; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | Process a block of memory though the hash |
| 204 | @param sha1 The hash state |
| 205 | @param in The data to hash |
| 206 | @param inlen The length of the data (octets) |
| 207 | */ |
doko@ubuntu.com | 1e5be8c | 2012-06-21 17:05:50 +0200 | [diff] [blame] | 208 | static void |
| 209 | sha1_process(struct sha1_state *sha1, |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 210 | const unsigned char *in, Py_ssize_t inlen) |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 211 | { |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 212 | Py_ssize_t n; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 213 | |
| 214 | assert(sha1 != NULL); |
| 215 | assert(in != NULL); |
| 216 | assert(sha1->curlen <= sizeof(sha1->buf)); |
| 217 | |
| 218 | while (inlen > 0) { |
| 219 | if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) { |
| 220 | sha1_compress(sha1, (unsigned char *)in); |
| 221 | sha1->length += SHA1_BLOCKSIZE * 8; |
| 222 | in += SHA1_BLOCKSIZE; |
| 223 | inlen -= SHA1_BLOCKSIZE; |
| 224 | } else { |
Victor Stinner | 640c35c | 2013-06-04 23:14:37 +0200 | [diff] [blame] | 225 | n = Py_MIN(inlen, (Py_ssize_t)(SHA1_BLOCKSIZE - sha1->curlen)); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 226 | memcpy(sha1->buf + sha1->curlen, in, (size_t)n); |
Victor Stinner | 56cb125 | 2012-10-31 00:33:57 +0100 | [diff] [blame] | 227 | sha1->curlen += (SHA1_INT32)n; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 228 | in += n; |
| 229 | inlen -= n; |
| 230 | if (sha1->curlen == SHA1_BLOCKSIZE) { |
| 231 | sha1_compress(sha1, sha1->buf); |
| 232 | sha1->length += 8*SHA1_BLOCKSIZE; |
| 233 | sha1->curlen = 0; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | Terminate the hash to get the digest |
| 241 | @param sha1 The hash state |
| 242 | @param out [out] The destination of the hash (20 bytes) |
| 243 | */ |
doko@ubuntu.com | 1e5be8c | 2012-06-21 17:05:50 +0200 | [diff] [blame] | 244 | static void |
| 245 | sha1_done(struct sha1_state *sha1, unsigned char *out) |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 246 | { |
| 247 | int i; |
| 248 | |
| 249 | assert(sha1 != NULL); |
| 250 | assert(out != NULL); |
| 251 | assert(sha1->curlen < sizeof(sha1->buf)); |
| 252 | |
| 253 | /* increase the length of the message */ |
| 254 | sha1->length += sha1->curlen * 8; |
| 255 | |
| 256 | /* append the '1' bit */ |
| 257 | sha1->buf[sha1->curlen++] = (unsigned char)0x80; |
| 258 | |
| 259 | /* if the length is currently above 56 bytes we append zeros |
| 260 | * then compress. Then we can fall back to padding zeros and length |
| 261 | * encoding like normal. |
| 262 | */ |
| 263 | if (sha1->curlen > 56) { |
| 264 | while (sha1->curlen < 64) { |
| 265 | sha1->buf[sha1->curlen++] = (unsigned char)0; |
| 266 | } |
| 267 | sha1_compress(sha1, sha1->buf); |
| 268 | sha1->curlen = 0; |
| 269 | } |
| 270 | |
| 271 | /* pad upto 56 bytes of zeroes */ |
| 272 | while (sha1->curlen < 56) { |
| 273 | sha1->buf[sha1->curlen++] = (unsigned char)0; |
| 274 | } |
| 275 | |
| 276 | /* store length */ |
| 277 | STORE64H(sha1->length, sha1->buf+56); |
| 278 | sha1_compress(sha1, sha1->buf); |
| 279 | |
| 280 | /* copy output */ |
| 281 | for (i = 0; i < 5; i++) { |
| 282 | STORE32H(sha1->state[i], out+(4*i)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | |
| 287 | /* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */ |
| 288 | /* .Revision: 1.10 $ */ |
| 289 | /* .Date: 2007/05/12 14:25:28 $ */ |
| 290 | |
| 291 | /* |
| 292 | * End of copied SHA1 code. |
| 293 | * |
| 294 | * ------------------------------------------------------------------------ |
| 295 | */ |
| 296 | |
| 297 | static PyTypeObject SHA1type; |
| 298 | |
| 299 | |
| 300 | static SHA1object * |
| 301 | newSHA1object(void) |
| 302 | { |
| 303 | return (SHA1object *)PyObject_New(SHA1object, &SHA1type); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /* Internal methods for a hash object */ |
| 308 | |
| 309 | static void |
| 310 | SHA1_dealloc(PyObject *ptr) |
| 311 | { |
| 312 | PyObject_Del(ptr); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | /* External methods for a hash object */ |
| 317 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 318 | /*[clinic input] |
| 319 | SHA1Type.copy |
| 320 | |
| 321 | Return a copy of the hash object. |
| 322 | [clinic start generated code]*/ |
| 323 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 324 | static PyObject * |
| 325 | SHA1Type_copy_impl(SHA1object *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 326 | /*[clinic end generated code: output=b4e001264620f02a input=b7eae10df6f89b36]*/ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 327 | { |
| 328 | SHA1object *newobj; |
| 329 | |
Benjamin Peterson | 51ce29c | 2013-04-15 21:38:25 -0400 | [diff] [blame] | 330 | if ((newobj = newSHA1object()) == NULL) |
| 331 | return NULL; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 332 | |
| 333 | newobj->hash_state = self->hash_state; |
| 334 | return (PyObject *)newobj; |
| 335 | } |
| 336 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 337 | /*[clinic input] |
| 338 | SHA1Type.digest |
| 339 | |
| 340 | Return the digest value as a string of binary data. |
| 341 | [clinic start generated code]*/ |
| 342 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 343 | static PyObject * |
| 344 | SHA1Type_digest_impl(SHA1object *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 345 | /*[clinic end generated code: output=2f05302a7aa2b5cb input=205d47e1927fd009]*/ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 346 | { |
| 347 | unsigned char digest[SHA1_DIGESTSIZE]; |
| 348 | struct sha1_state temp; |
| 349 | |
| 350 | temp = self->hash_state; |
| 351 | sha1_done(&temp, digest); |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 352 | return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 355 | /*[clinic input] |
| 356 | SHA1Type.hexdigest |
| 357 | |
| 358 | Return the digest value as a string of hexadecimal digits. |
| 359 | [clinic start generated code]*/ |
| 360 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 361 | static PyObject * |
| 362 | SHA1Type_hexdigest_impl(SHA1object *self) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 363 | /*[clinic end generated code: output=4161fd71e68c6659 input=97691055c0c74ab0]*/ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 364 | { |
| 365 | unsigned char digest[SHA1_DIGESTSIZE]; |
| 366 | struct sha1_state temp; |
| 367 | PyObject *retval; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 368 | Py_UCS1 *hex_digest; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 369 | int i, j; |
| 370 | |
| 371 | /* Get the raw (binary) digest value */ |
| 372 | temp = self->hash_state; |
| 373 | sha1_done(&temp, digest); |
| 374 | |
| 375 | /* Create a new string */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 376 | retval = PyUnicode_New(SHA1_DIGESTSIZE * 2, 127); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 377 | if (!retval) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 379 | hex_digest = PyUnicode_1BYTE_DATA(retval); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 380 | |
| 381 | /* Make hex version of the digest */ |
| 382 | for(i=j=0; i<SHA1_DIGESTSIZE; i++) { |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 383 | unsigned char c; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 384 | c = (digest[i] >> 4) & 0xf; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 385 | hex_digest[j++] = Py_hexdigits[c]; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 386 | c = (digest[i] & 0xf); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 387 | hex_digest[j++] = Py_hexdigits[c]; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 388 | } |
Christian Heimes | f402e92 | 2013-01-03 09:21:55 +0100 | [diff] [blame] | 389 | #ifdef Py_DEBUG |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 390 | assert(_PyUnicode_CheckConsistency(retval, 1)); |
Christian Heimes | f402e92 | 2013-01-03 09:21:55 +0100 | [diff] [blame] | 391 | #endif |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 392 | return retval; |
| 393 | } |
| 394 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 395 | /*[clinic input] |
| 396 | SHA1Type.update |
| 397 | |
| 398 | obj: object |
| 399 | / |
| 400 | |
| 401 | Update this hash object's state with the provided string. |
| 402 | [clinic start generated code]*/ |
| 403 | |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 404 | static PyObject * |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 405 | SHA1Type_update(SHA1object *self, PyObject *obj) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 406 | /*[clinic end generated code: output=d9902f0e5015e9ae input=aad8e07812edbba3]*/ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 407 | { |
Martin v. Löwis | 7b9cb25 | 2008-08-14 15:52:23 +0000 | [diff] [blame] | 408 | Py_buffer buf; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 409 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 410 | GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); |
| 411 | |
Martin v. Löwis | 7b9cb25 | 2008-08-14 15:52:23 +0000 | [diff] [blame] | 412 | sha1_process(&self->hash_state, buf.buf, buf.len); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 413 | |
Martin v. Löwis | 7b9cb25 | 2008-08-14 15:52:23 +0000 | [diff] [blame] | 414 | PyBuffer_Release(&buf); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 415 | Py_INCREF(Py_None); |
| 416 | return Py_None; |
| 417 | } |
| 418 | |
| 419 | static PyMethodDef SHA1_methods[] = { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 420 | SHA1TYPE_COPY_METHODDEF |
| 421 | SHA1TYPE_DIGEST_METHODDEF |
| 422 | SHA1TYPE_HEXDIGEST_METHODDEF |
| 423 | SHA1TYPE_UPDATE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | {NULL, NULL} /* sentinel */ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 425 | }; |
| 426 | |
| 427 | static PyObject * |
| 428 | SHA1_get_block_size(PyObject *self, void *closure) |
| 429 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 430 | return PyLong_FromLong(SHA1_BLOCKSIZE); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static PyObject * |
| 434 | SHA1_get_name(PyObject *self, void *closure) |
| 435 | { |
Christian Heimes | 37d5ceb | 2013-08-15 18:31:48 +0200 | [diff] [blame] | 436 | return PyUnicode_FromStringAndSize("sha1", 4); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static PyObject * |
| 440 | sha1_get_digest_size(PyObject *self, void *closure) |
| 441 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 442 | return PyLong_FromLong(SHA1_DIGESTSIZE); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | |
| 446 | static PyGetSetDef SHA1_getseters[] = { |
| 447 | {"block_size", |
| 448 | (getter)SHA1_get_block_size, NULL, |
| 449 | NULL, |
| 450 | NULL}, |
| 451 | {"name", |
| 452 | (getter)SHA1_get_name, NULL, |
| 453 | NULL, |
| 454 | NULL}, |
| 455 | {"digest_size", |
| 456 | (getter)sha1_get_digest_size, NULL, |
| 457 | NULL, |
| 458 | NULL}, |
| 459 | {NULL} /* Sentinel */ |
| 460 | }; |
| 461 | |
| 462 | static PyTypeObject SHA1type = { |
| 463 | PyVarObject_HEAD_INIT(NULL, 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | "_sha1.sha1", /*tp_name*/ |
| 465 | sizeof(SHA1object), /*tp_size*/ |
| 466 | 0, /*tp_itemsize*/ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 467 | /* methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | SHA1_dealloc, /*tp_dealloc*/ |
| 469 | 0, /*tp_print*/ |
| 470 | 0, /*tp_getattr*/ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 471 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 472 | 0, /*tp_reserved*/ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 473 | 0, /*tp_repr*/ |
| 474 | 0, /*tp_as_number*/ |
| 475 | 0, /*tp_as_sequence*/ |
| 476 | 0, /*tp_as_mapping*/ |
| 477 | 0, /*tp_hash*/ |
| 478 | 0, /*tp_call*/ |
| 479 | 0, /*tp_str*/ |
| 480 | 0, /*tp_getattro*/ |
| 481 | 0, /*tp_setattro*/ |
| 482 | 0, /*tp_as_buffer*/ |
| 483 | Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 484 | 0, /*tp_doc*/ |
| 485 | 0, /*tp_traverse*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | 0, /*tp_clear*/ |
| 487 | 0, /*tp_richcompare*/ |
| 488 | 0, /*tp_weaklistoffset*/ |
| 489 | 0, /*tp_iter*/ |
| 490 | 0, /*tp_iternext*/ |
| 491 | SHA1_methods, /* tp_methods */ |
| 492 | NULL, /* tp_members */ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 493 | SHA1_getseters, /* tp_getset */ |
| 494 | }; |
| 495 | |
| 496 | |
| 497 | /* The single module-level function: new() */ |
| 498 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 499 | /*[clinic input] |
| 500 | _sha1.sha1 |
| 501 | |
| 502 | string: object(c_default="NULL") = b'' |
| 503 | |
| 504 | Return a new SHA1 hash object; optionally initialized with a string. |
| 505 | [clinic start generated code]*/ |
| 506 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 507 | static PyObject * |
| 508 | _sha1_sha1_impl(PyModuleDef *module, PyObject *string) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 509 | /*[clinic end generated code: output=3e4e841386b9e8db input=27ea54281d995ec2]*/ |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 510 | { |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 511 | SHA1object *new; |
Martin v. Löwis | 7b9cb25 | 2008-08-14 15:52:23 +0000 | [diff] [blame] | 512 | Py_buffer buf; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 513 | |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 514 | if (string) |
| 515 | GET_BUFFER_VIEW_OR_ERROUT(string, &buf); |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 516 | |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 517 | if ((new = newSHA1object()) == NULL) { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 518 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 519 | PyBuffer_Release(&buf); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 520 | return NULL; |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 521 | } |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 522 | |
| 523 | sha1_init(&new->hash_state); |
| 524 | |
| 525 | if (PyErr_Occurred()) { |
| 526 | Py_DECREF(new); |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 527 | if (string) |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 528 | PyBuffer_Release(&buf); |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 529 | return NULL; |
| 530 | } |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 531 | if (string) { |
Martin v. Löwis | 7b9cb25 | 2008-08-14 15:52:23 +0000 | [diff] [blame] | 532 | sha1_process(&new->hash_state, buf.buf, buf.len); |
Hirokazu Yamamoto | 8404749 | 2009-03-03 07:49:01 +0000 | [diff] [blame] | 533 | PyBuffer_Release(&buf); |
Martin v. Löwis | 7b9cb25 | 2008-08-14 15:52:23 +0000 | [diff] [blame] | 534 | } |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 535 | |
| 536 | return (PyObject *)new; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /* List of functions exported by this module */ |
| 541 | |
| 542 | static struct PyMethodDef SHA1_functions[] = { |
Martin v. Löwis | 501b13c | 2014-07-27 14:20:23 +0200 | [diff] [blame] | 543 | _SHA1_SHA1_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | {NULL, NULL} /* Sentinel */ |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 545 | }; |
| 546 | |
| 547 | |
| 548 | /* Initialize this module. */ |
| 549 | |
| 550 | #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } |
| 551 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 552 | |
| 553 | static struct PyModuleDef _sha1module = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | PyModuleDef_HEAD_INIT, |
| 555 | "_sha1", |
| 556 | NULL, |
| 557 | -1, |
| 558 | SHA1_functions, |
| 559 | NULL, |
| 560 | NULL, |
| 561 | NULL, |
| 562 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 563 | }; |
| 564 | |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 565 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 566 | PyInit__sha1(void) |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 567 | { |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 568 | PyObject *m; |
| 569 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 570 | Py_TYPE(&SHA1type) = &PyType_Type; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 571 | if (PyType_Ready(&SHA1type) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 572 | return NULL; |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 573 | |
| 574 | m = PyModule_Create(&_sha1module); |
| 575 | if (m == NULL) |
| 576 | return NULL; |
| 577 | |
| 578 | Py_INCREF((PyObject *)&SHA1type); |
| 579 | PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type); |
| 580 | return m; |
Gregory P. Smith | 2f21eb3 | 2007-09-09 06:44:34 +0000 | [diff] [blame] | 581 | } |