Changeset 391 for python/trunk/Modules/shamodule.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Modules/shamodule.c
r2 r391 22 22 /* Endianness testing and definitions */ 23 23 #define TestEndianness(variable) {int i=1; variable=PCT_BIG_ENDIAN;\ 24 24 if (*((char*)&i)==1) variable=PCT_LITTLE_ENDIAN;} 25 25 26 26 #define PCT_LITTLE_ENDIAN 1 … … 32 32 33 33 #if SIZEOF_INT == 4 34 typedef unsigned int SHA_INT32; 34 typedef unsigned int SHA_INT32; /* 32-bit integer */ 35 35 #else 36 36 /* not defined. compilation will die. */ … … 46 46 typedef struct { 47 47 PyObject_HEAD 48 SHA_INT32 digest[5]; 49 SHA_INT32 count_lo, count_hi; 50 SHA_BYTE data[SHA_BLOCKSIZE]; 48 SHA_INT32 digest[5]; /* Message digest */ 49 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */ 50 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */ 51 51 int Endianness; 52 int local; 52 int local; /* unprocessed amount in data */ 53 53 } SHAobject; 54 54 … … 61 61 62 62 if ( Endianness == PCT_BIG_ENDIAN ) 63 63 return; 64 64 65 65 byteCount /= sizeof(*buffer); … … 112 112 rcs@cs.arizona.edu for discovering this */ 113 113 114 /*#define f1(x,y,z) ((x & y) | (~x & z))// Rounds 0-19 */115 #define f1(x,y,z) (z ^ (x & (y ^ z)))/* Rounds 0-19 */116 #define f2(x,y,z) (x ^ y ^ z)/* Rounds 20-39 */117 /*#define f3(x,y,z) ((x & y) | (x & z) | (y & z))// Rounds 40-59 */118 #define f3(x,y,z) ((x & y) | (z & (x | y)))/* Rounds 40-59 */119 #define f4(x,y,z) (x ^ y ^ z)/* Rounds 60-79 */114 /*#define f1(x,y,z) ((x & y) | (~x & z)) // Rounds 0-19 */ 115 #define f1(x,y,z) (z ^ (x & (y ^ z))) /* Rounds 0-19 */ 116 #define f2(x,y,z) (x ^ y ^ z) /* Rounds 20-39 */ 117 /*#define f3(x,y,z) ((x & y) | (x & z) | (y & z)) // Rounds 40-59 */ 118 #define f3(x,y,z) ((x & y) | (z & (x | y))) /* Rounds 40-59 */ 119 #define f4(x,y,z) (x ^ y ^ z) /* Rounds 60-79 */ 120 120 121 121 /* SHA constants */ 122 122 123 #define CONST1 0x5a827999L/* Rounds 0-19 */124 #define CONST2 0x6ed9eba1L/* Rounds 20-39 */125 #define CONST3 0x8f1bbcdcL/* Rounds 40-59 */126 #define CONST4 0xca62c1d6L/* Rounds 60-79 */123 #define CONST1 0x5a827999L /* Rounds 0-19 */ 124 #define CONST2 0x6ed9eba1L /* Rounds 20-39 */ 125 #define CONST3 0x8f1bbcdcL /* Rounds 40-59 */ 126 #define CONST4 0xca62c1d6L /* Rounds 60-79 */ 127 127 128 128 /* 32-bit rotate */ 129 129 130 #define R32(x,n) 130 #define R32(x,n) ((x << n) | (x >> (32 - n))) 131 131 132 132 /* the generic case, for when the overall rotation is not unraveled */ 133 133 134 #define FG(n) 135 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; 134 #define FG(n) \ 135 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; \ 136 136 E = D; D = C; C = R32(B,30); B = A; A = T 137 137 138 138 /* specific cases, for when the overall rotation is unraveled */ 139 139 140 #define FA(n) 140 #define FA(n) \ 141 141 T = R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n; B = R32(B,30) 142 142 143 #define FB(n) 143 #define FB(n) \ 144 144 E = R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n; A = R32(A,30) 145 145 146 #define FC(n) 146 #define FC(n) \ 147 147 D = R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n; T = R32(T,30) 148 148 149 #define FD(n) 149 #define FD(n) \ 150 150 C = R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n; E = R32(E,30) 151 151 152 #define FE(n) 152 #define FE(n) \ 153 153 B = R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n; D = R32(D,30) 154 154 155 #define FT(n) 155 #define FT(n) \ 156 156 A = R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n; C = R32(C,30) 157 157 … … 168 168 169 169 for (i = 16; i < 80; ++i) { 170 171 172 173 170 W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16]; 171 172 /* extra rotation fix */ 173 W[i] = R32(W[i], 1); 174 174 } 175 175 A = sha_info->digest[0]; … … 237 237 238 238 static void 239 sha_update(SHAobject *sha_info, SHA_BYTE *buffer, int count)240 { 241 int i;239 sha_update(SHAobject *sha_info, SHA_BYTE *buffer, unsigned int count) 240 { 241 unsigned int i; 242 242 SHA_INT32 clo; 243 243 … … 287 287 ((SHA_BYTE *) sha_info->data)[count++] = 0x80; 288 288 if (count > SHA_BLOCKSIZE - 8) { 289 290 291 292 289 memset(((SHA_BYTE *) sha_info->data) + count, 0, 290 SHA_BLOCKSIZE - count); 291 sha_transform(sha_info); 292 memset((SHA_BYTE *) sha_info->data, 0, SHA_BLOCKSIZE - 8); 293 293 } 294 294 else { 295 296 295 memset(((SHA_BYTE *) sha_info->data) + count, 0, 296 SHA_BLOCKSIZE - 8 - count); 297 297 } 298 298 … … 403 403 retval = PyString_FromStringAndSize(NULL, sizeof(digest) * 2); 404 404 if (!retval) 405 405 return NULL; 406 406 hex_digest = PyString_AsString(retval); 407 407 if (!hex_digest) { 408 409 408 Py_DECREF(retval); 409 return NULL; 410 410 } 411 411 … … 414 414 char c; 415 415 c = (digest[i] >> 4) & 0xf; 416 416 c = (c>9) ? c+'a'-10 : c + '0'; 417 417 hex_digest[j++] = c; 418 418 c = (digest[i] & 0xf); 419 419 c = (c>9) ? c+'a'-10 : c + '0'; 420 420 hex_digest[j++] = c; 421 421 } … … 429 429 SHA_update(SHAobject *self, PyObject *args) 430 430 { 431 unsigned char *cp; 432 int len; 433 434 if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) 431 Py_buffer view; 432 Py_ssize_t n; 433 unsigned char *buf; 434 435 if (!PyArg_ParseTuple(args, "s*:update", &view)) 435 436 return NULL; 436 437 437 sha_update(self, cp, len); 438 439 Py_INCREF(Py_None); 440 return Py_None; 438 n = view.len; 439 buf = (unsigned char *) view.buf; 440 while (n > 0) { 441 Py_ssize_t nbytes; 442 if (n > INT_MAX) 443 nbytes = INT_MAX; 444 else 445 nbytes = n; 446 sha_update(self, buf, 447 Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int)); 448 buf += nbytes; 449 n -= nbytes; 450 } 451 452 PyBuffer_Release(&view); 453 Py_RETURN_NONE; 441 454 } 442 455 443 456 static PyMethodDef SHA_methods[] = { 444 {"copy", 445 {"digest", 457 {"copy", (PyCFunction)SHA_copy, METH_NOARGS, SHA_copy__doc__}, 458 {"digest", (PyCFunction)SHA_digest, METH_NOARGS, SHA_digest__doc__}, 446 459 {"hexdigest", (PyCFunction)SHA_hexdigest, METH_NOARGS, SHA_hexdigest__doc__}, 447 {"update", 448 {NULL, NULL}/* sentinel */460 {"update", (PyCFunction)SHA_update, METH_VARARGS, SHA_update__doc__}, 461 {NULL, NULL} /* sentinel */ 449 462 }; 450 463 … … 491 504 static PyTypeObject SHAtype = { 492 505 PyVarObject_HEAD_INIT(NULL, 0) 493 "_sha.sha", 494 sizeof(SHAobject), 495 0, 506 "_sha.sha", /*tp_name*/ 507 sizeof(SHAobject), /*tp_size*/ 508 0, /*tp_itemsize*/ 496 509 /* methods */ 497 SHA_dealloc, 498 0, 510 SHA_dealloc, /*tp_dealloc*/ 511 0, /*tp_print*/ 499 512 0, /*tp_getattr*/ 500 513 0, /*tp_setattr*/ … … 513 526 0, /*tp_doc*/ 514 527 0, /*tp_traverse*/ 515 0, 516 0, 517 0, 518 0, 519 0, 520 SHA_methods, 528 0, /*tp_clear*/ 529 0, /*tp_richcompare*/ 530 0, /*tp_weaklistoffset*/ 531 0, /*tp_iter*/ 532 0, /*tp_iternext*/ 533 SHA_methods, /* tp_methods */ 521 534 0, /* tp_members */ 522 535 SHA_getseters, /* tp_getset */ … … 536 549 static char *kwlist[] = {"string", NULL}; 537 550 SHAobject *new; 538 unsigned char *cp = NULL; 539 int len; 540 541 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist, 542 &cp, &len)) { 551 Py_buffer view = { 0 }; 552 Py_ssize_t n; 553 unsigned char *buf; 554 555 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist, 556 &view)) { 543 557 return NULL; 544 558 } 545 559 546 if ((new = newSHAobject()) == NULL) 560 if ((new = newSHAobject()) == NULL) { 561 PyBuffer_Release(&view); 547 562 return NULL; 563 } 548 564 549 565 sha_init(new); … … 551 567 if (PyErr_Occurred()) { 552 568 Py_DECREF(new); 569 PyBuffer_Release(&view); 553 570 return NULL; 554 571 } 555 if (cp) 556 sha_update(new, cp, len); 572 573 n = view.len; 574 buf = (unsigned char *) view.buf; 575 while (n > 0) { 576 Py_ssize_t nbytes; 577 if (n > INT_MAX) 578 nbytes = INT_MAX; 579 else 580 nbytes = n; 581 sha_update(new, buf, 582 Py_SAFE_DOWNCAST(nbytes, Py_ssize_t, unsigned int)); 583 buf += nbytes; 584 n -= nbytes; 585 } 586 587 PyBuffer_Release(&view); 557 588 558 589 return (PyObject *)new; … … 564 595 static struct PyMethodDef SHA_functions[] = { 565 596 {"new", (PyCFunction)SHA_new, METH_VARARGS|METH_KEYWORDS, SHA_new__doc__}, 566 {NULL, NULL}/* Sentinel */597 {NULL, NULL} /* Sentinel */ 567 598 }; 568 599 … … 582 613 m = Py_InitModule("_sha", SHA_functions); 583 614 if (m == NULL) 584 615 return; 585 616 586 617 /* Add some symbolic constants to the module */ 587 618 insint("blocksize", 1); /* For future use, in case some hash 588 619 functions require an integral number of 589 blocks */ 620 blocks */ 590 621 insint("digestsize", 20); 591 622 insint("digest_size", 20);
Note:
See TracChangeset
for help on using the changeset viewer.