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