diff options
-rw-r--r-- | Lib/hashlib.py | 23 | ||||
-rw-r--r-- | Modules/md5module.c | 561 | ||||
-rw-r--r-- | Modules/sha1module.c | 537 | ||||
-rw-r--r-- | Modules/sha256module.c | 4 | ||||
-rw-r--r-- | Modules/sha512module.c | 4 | ||||
-rw-r--r-- | setup.py | 5 |
6 files changed, 1118 insertions, 16 deletions
diff --git a/Lib/hashlib.py b/Lib/hashlib.py index e24d5a1..a39cb57 100644 --- a/Lib/hashlib.py +++ b/Lib/hashlib.py @@ -10,8 +10,8 @@ new(name, data=b'') - returns a new hash object implementing the given hash function; initializing the hash using the given binary data. -Named constructor functions are also available, these are much faster -than using new(): +Named constructor functions are also available, these are faster +than using new(name): md5(), sha1(), sha224(), sha256(), sha384(), and sha512() @@ -22,14 +22,13 @@ Choose your hash function wisely. Some have known collision weaknesses. sha384 and sha512 will be slow on 32 bit platforms. Hash objects have these methods: - - update(arg): Update the hash object with the string arg. Repeated calls + - update(arg): Update the hash object with the bytes in arg. Repeated calls are equivalent to a single call with the concatenation of all the arguments. - - digest(): Return the digest of the strings passed to the update() method - so far. This may contain non-ASCII characters, including - NUL bytes. - - hexdigest(): Like digest() except the digest is returned as a string of - double length, containing only hexadecimal digits. + - digest(): Return the digest of the bytes passed to the update() method + so far. + - hexdigest(): Like digest() except the digest is returned as a unicode + object of double length, containing only hexadecimal digits. - copy(): Return a copy (clone) of the hash object. This can be used to efficiently compute the digests of strings that share a common initial substring. @@ -54,11 +53,11 @@ More condensed: def __get_builtin_constructor(name): if name in ('SHA1', 'sha1'): - import _sha - return _sha.new + import _sha1 + return _sha1.sha1 elif name in ('MD5', 'md5'): import _md5 - return _md5.new + return _md5.md5 elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): import _sha256 bs = name[3:] @@ -78,7 +77,7 @@ def __get_builtin_constructor(name): def __py_new(name, data=b''): - """new(name, data='') - Return a new hashing object using the named algorithm; + """new(name, data=b'') - Return a new hashing object using the named algorithm; optionally initialized with data (which must be bytes). """ return __get_builtin_constructor(name)(data) diff --git a/Modules/md5module.c b/Modules/md5module.c new file mode 100644 index 0000000..26f0e4c --- /dev/null +++ b/Modules/md5module.c @@ -0,0 +1,561 @@ +/* MD5 module */ + +/* This module provides an interface to the MD5 algorithm */ + +/* See below for information about the original code this module was + based upon. Additional work performed by: + + Andrew Kuchling (amk@amk.ca) + Greg Stein (gstein@lyra.org) + Trevor Perrin (trevp@trevp.net) + + Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) + Licensed to PSF under a Contributor Agreement. + +*/ + +/* MD5 objects */ + +#include "Python.h" + + +/* Some useful types */ + +#if SIZEOF_INT == 4 +typedef unsigned int MD5_INT32; /* 32-bit integer */ +typedef PY_LONG_LONG MD5_INT64; /* 64-bit integer */ +#else +/* not defined. compilation will die. */ +#endif + +/* The MD5 block size and message digest sizes, in bytes */ + +#define MD5_BLOCKSIZE 64 +#define MD5_DIGESTSIZE 16 + +/* The structure for storing MD5 info */ + +struct md5_state { + MD5_INT64 length; + MD5_INT32 state[4], curlen; + unsigned char buf[MD5_BLOCKSIZE]; +}; + +typedef struct { + PyObject_HEAD + + struct md5_state hash_state; +} MD5object; + + +/* ------------------------------------------------------------------------ + * + * This code for the MD5 algorithm was noted as public domain. The + * original headers are pasted below. + * + * Several changes have been made to make it more compatible with the + * Python environment and desired interface. + * + */ + +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ + +/* rotate the hard way (platform optimizations could be done) */ +#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) + +/* Endian Neutral macros that work on all platforms */ + +#define STORE32L(x, y) \ + { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ + (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } + +#define LOAD32L(x, y) \ + { x = ((unsigned long)((y)[3] & 255)<<24) | \ + ((unsigned long)((y)[2] & 255)<<16) | \ + ((unsigned long)((y)[1] & 255)<<8) | \ + ((unsigned long)((y)[0] & 255)); } + +#define STORE64L(x, y) \ + { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ + (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ + (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ + (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } + +#ifndef MIN + #define MIN(x, y) ( ((x)<(y))?(x):(y) ) +#endif + + +/* MD5 macros */ + +#define F(x,y,z) (z ^ (x & (y ^ z))) +#define G(x,y,z) (y ^ (z & (y ^ x))) +#define H(x,y,z) (x^y^z) +#define I(x,y,z) (y^(x|(~z))) + +#define FF(a,b,c,d,M,s,t) \ + a = (a + F(b,c,d) + M + t); a = ROLc(a, s) + b; + +#define GG(a,b,c,d,M,s,t) \ + a = (a + G(b,c,d) + M + t); a = ROLc(a, s) + b; + +#define HH(a,b,c,d,M,s,t) \ + a = (a + H(b,c,d) + M + t); a = ROLc(a, s) + b; + +#define II(a,b,c,d,M,s,t) \ + a = (a + I(b,c,d) + M + t); a = ROLc(a, s) + b; + + +static void md5_compress(struct md5_state *md5, unsigned char *buf) +{ + MD5_INT32 i, W[16], a, b, c, d; + + assert(md5 != NULL); + assert(buf != NULL); + + /* copy the state into 512-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD32L(W[i], buf + (4*i)); + } + + /* copy state */ + a = md5->state[0]; + b = md5->state[1]; + c = md5->state[2]; + d = md5->state[3]; + + FF(a,b,c,d,W[0],7,0xd76aa478UL) + FF(d,a,b,c,W[1],12,0xe8c7b756UL) + FF(c,d,a,b,W[2],17,0x242070dbUL) + FF(b,c,d,a,W[3],22,0xc1bdceeeUL) + FF(a,b,c,d,W[4],7,0xf57c0fafUL) + FF(d,a,b,c,W[5],12,0x4787c62aUL) + FF(c,d,a,b,W[6],17,0xa8304613UL) + FF(b,c,d,a,W[7],22,0xfd469501UL) + FF(a,b,c,d,W[8],7,0x698098d8UL) + FF(d,a,b,c,W[9],12,0x8b44f7afUL) + FF(c,d,a,b,W[10],17,0xffff5bb1UL) + FF(b,c,d,a,W[11],22,0x895cd7beUL) + FF(a,b,c,d,W[12],7,0x6b901122UL) + FF(d,a,b,c,W[13],12,0xfd987193UL) + FF(c,d,a,b,W[14],17,0xa679438eUL) + FF(b,c,d,a,W[15],22,0x49b40821UL) + GG(a,b,c,d,W[1],5,0xf61e2562UL) + GG(d,a,b,c,W[6],9,0xc040b340UL) + GG(c,d,a,b,W[11],14,0x265e5a51UL) + GG(b,c,d,a,W[0],20,0xe9b6c7aaUL) + GG(a,b,c,d,W[5],5,0xd62f105dUL) + GG(d,a,b,c,W[10],9,0x02441453UL) + GG(c,d,a,b,W[15],14,0xd8a1e681UL) + GG(b,c,d,a,W[4],20,0xe7d3fbc8UL) + GG(a,b,c,d,W[9],5,0x21e1cde6UL) + GG(d,a,b,c,W[14],9,0xc33707d6UL) + GG(c,d,a,b,W[3],14,0xf4d50d87UL) + GG(b,c,d,a,W[8],20,0x455a14edUL) + GG(a,b,c,d,W[13],5,0xa9e3e905UL) + GG(d,a,b,c,W[2],9,0xfcefa3f8UL) + GG(c,d,a,b,W[7],14,0x676f02d9UL) + GG(b,c,d,a,W[12],20,0x8d2a4c8aUL) + HH(a,b,c,d,W[5],4,0xfffa3942UL) + HH(d,a,b,c,W[8],11,0x8771f681UL) + HH(c,d,a,b,W[11],16,0x6d9d6122UL) + HH(b,c,d,a,W[14],23,0xfde5380cUL) + HH(a,b,c,d,W[1],4,0xa4beea44UL) + HH(d,a,b,c,W[4],11,0x4bdecfa9UL) + HH(c,d,a,b,W[7],16,0xf6bb4b60UL) + HH(b,c,d,a,W[10],23,0xbebfbc70UL) + HH(a,b,c,d,W[13],4,0x289b7ec6UL) + HH(d,a,b,c,W[0],11,0xeaa127faUL) + HH(c,d,a,b,W[3],16,0xd4ef3085UL) + HH(b,c,d,a,W[6],23,0x04881d05UL) + HH(a,b,c,d,W[9],4,0xd9d4d039UL) + HH(d,a,b,c,W[12],11,0xe6db99e5UL) + HH(c,d,a,b,W[15],16,0x1fa27cf8UL) + HH(b,c,d,a,W[2],23,0xc4ac5665UL) + II(a,b,c,d,W[0],6,0xf4292244UL) + II(d,a,b,c,W[7],10,0x432aff97UL) + II(c,d,a,b,W[14],15,0xab9423a7UL) + II(b,c,d,a,W[5],21,0xfc93a039UL) + II(a,b,c,d,W[12],6,0x655b59c3UL) + II(d,a,b,c,W[3],10,0x8f0ccc92UL) + II(c,d,a,b,W[10],15,0xffeff47dUL) + II(b,c,d,a,W[1],21,0x85845dd1UL) + II(a,b,c,d,W[8],6,0x6fa87e4fUL) + II(d,a,b,c,W[15],10,0xfe2ce6e0UL) + II(c,d,a,b,W[6],15,0xa3014314UL) + II(b,c,d,a,W[13],21,0x4e0811a1UL) + II(a,b,c,d,W[4],6,0xf7537e82UL) + II(d,a,b,c,W[11],10,0xbd3af235UL) + II(c,d,a,b,W[2],15,0x2ad7d2bbUL) + II(b,c,d,a,W[9],21,0xeb86d391UL) + + md5->state[0] = md5->state[0] + a; + md5->state[1] = md5->state[1] + b; + md5->state[2] = md5->state[2] + c; + md5->state[3] = md5->state[3] + d; +} + + +/** + Initialize the hash state + @param sha1 The hash state you wish to initialize +*/ +void md5_init(struct md5_state *md5) +{ + assert(md5 != NULL); + md5->state[0] = 0x67452301UL; + md5->state[1] = 0xefcdab89UL; + md5->state[2] = 0x98badcfeUL; + md5->state[3] = 0x10325476UL; + md5->curlen = 0; + md5->length = 0; +} + +/** + Process a block of memory though the hash + @param sha1 The hash state + @param in The data to hash + @param inlen The length of the data (octets) +*/ +void md5_process(struct md5_state *md5, + const unsigned char *in, unsigned long inlen) +{ + unsigned long n; + + assert(md5 != NULL); + assert(in != NULL); + assert(md5->curlen <= sizeof(md5->buf)); + + while (inlen > 0) { + if (md5->curlen == 0 && inlen >= MD5_BLOCKSIZE) { + md5_compress(md5, (unsigned char *)in); + md5->length += MD5_BLOCKSIZE * 8; + in += MD5_BLOCKSIZE; + inlen -= MD5_BLOCKSIZE; + } else { + n = MIN(inlen, (MD5_BLOCKSIZE - md5->curlen)); + memcpy(md5->buf + md5->curlen, in, (size_t)n); + md5->curlen += n; + in += n; + inlen -= n; + if (md5->curlen == MD5_BLOCKSIZE) { + md5_compress(md5, md5->buf); + md5->length += 8*MD5_BLOCKSIZE; + md5->curlen = 0; + } + } + } +} + +/** + Terminate the hash to get the digest + @param sha1 The hash state + @param out [out] The destination of the hash (16 bytes) +*/ +void md5_done(struct md5_state *md5, unsigned char *out) +{ + int i; + + assert(md5 != NULL); + assert(out != NULL); + assert(md5->curlen < sizeof(md5->buf)); + + /* increase the length of the message */ + md5->length += md5->curlen * 8; + + /* append the '1' bit */ + md5->buf[md5->curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (md5->curlen > 56) { + while (md5->curlen < 64) { + md5->buf[md5->curlen++] = (unsigned char)0; + } + md5_compress(md5, md5->buf); + md5->curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (md5->curlen < 56) { + md5->buf[md5->curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64L(md5->length, md5->buf+56); + md5_compress(md5, md5->buf); + + /* copy output */ + for (i = 0; i < 4; i++) { + STORE32L(md5->state[i], out+(4*i)); + } +} + +/* .Source: /cvs/libtom/libtomcrypt/src/hashes/md5.c,v $ */ +/* .Revision: 1.10 $ */ +/* .Date: 2007/05/12 14:25:28 $ */ + +/* + * End of copied MD5 code. + * + * ------------------------------------------------------------------------ + */ + +static PyTypeObject MD5type; + + +static MD5object * +newMD5object(void) +{ + return (MD5object *)PyObject_New(MD5object, &MD5type); +} + + +/* Internal methods for a hash object */ + +static void +MD5_dealloc(PyObject *ptr) +{ + PyObject_Del(ptr); +} + + +/* External methods for a hash object */ + +PyDoc_STRVAR(MD5_copy__doc__, "Return a copy of the hash object."); + +static PyObject * +MD5_copy(MD5object *self, PyObject *unused) +{ + MD5object *newobj; + + if (Py_Type(self) == &MD5type) { + if ( (newobj = newMD5object())==NULL) + return NULL; + } else { + if ( (newobj = newMD5object())==NULL) + return NULL; + } + + newobj->hash_state = self->hash_state; + return (PyObject *)newobj; +} + +PyDoc_STRVAR(MD5_digest__doc__, +"Return the digest value as a string of binary data."); + +static PyObject * +MD5_digest(MD5object *self, PyObject *unused) +{ + unsigned char digest[MD5_DIGESTSIZE]; + struct md5_state temp; + + temp = self->hash_state; + md5_done(&temp, digest); + return PyBytes_FromStringAndSize((const char *)digest, MD5_DIGESTSIZE); +} + +PyDoc_STRVAR(MD5_hexdigest__doc__, +"Return the digest value as a string of hexadecimal digits."); + +static PyObject * +MD5_hexdigest(MD5object *self, PyObject *unused) +{ + unsigned char digest[MD5_DIGESTSIZE]; + struct md5_state temp; + PyObject *retval; + Py_UNICODE *hex_digest; + int i, j; + + /* Get the raw (binary) digest value */ + temp = self->hash_state; + md5_done(&temp, digest); + + /* Create a new string */ + retval = PyUnicode_FromStringAndSize(NULL, MD5_DIGESTSIZE * 2); + if (!retval) + return NULL; + hex_digest = PyUnicode_AS_UNICODE(retval); + if (!hex_digest) { + Py_DECREF(retval); + return NULL; + } + + /* Make hex version of the digest */ + for(i=j=0; i<MD5_DIGESTSIZE; i++) { + char c; + c = (digest[i] >> 4) & 0xf; + c = (c>9) ? c+'a'-10 : c + '0'; + hex_digest[j++] = c; + c = (digest[i] & 0xf); + c = (c>9) ? c+'a'-10 : c + '0'; + hex_digest[j++] = c; + } + return retval; +} + +PyDoc_STRVAR(MD5_update__doc__, +"Update this hash object's state with the provided string."); + +static PyObject * +MD5_update(MD5object *self, PyObject *args) +{ + unsigned char *cp; + int len; + + if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) + return NULL; + + md5_process(&self->hash_state, cp, len); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyMethodDef MD5_methods[] = { + {"copy", (PyCFunction)MD5_copy, METH_NOARGS, MD5_copy__doc__}, + {"digest", (PyCFunction)MD5_digest, METH_NOARGS, MD5_digest__doc__}, + {"hexdigest", (PyCFunction)MD5_hexdigest, METH_NOARGS, MD5_hexdigest__doc__}, + {"update", (PyCFunction)MD5_update, METH_VARARGS, MD5_update__doc__}, + {NULL, NULL} /* sentinel */ +}; + +static PyObject * +MD5_get_block_size(PyObject *self, void *closure) +{ + return PyInt_FromLong(MD5_BLOCKSIZE); +} + +static PyObject * +MD5_get_name(PyObject *self, void *closure) +{ + return PyUnicode_FromStringAndSize("MD5", 3); +} + +static PyObject * +md5_get_digest_size(PyObject *self, void *closure) +{ + return PyInt_FromLong(MD5_DIGESTSIZE); +} + + +static PyGetSetDef MD5_getseters[] = { + {"block_size", + (getter)MD5_get_block_size, NULL, + NULL, + NULL}, + {"name", + (getter)MD5_get_name, NULL, + NULL, + NULL}, + {"digest_size", + (getter)md5_get_digest_size, NULL, + NULL, + NULL}, + {NULL} /* Sentinel */ +}; + +static PyTypeObject MD5type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_md5.md5", /*tp_name*/ + sizeof(MD5object), /*tp_size*/ + 0, /*tp_itemsize*/ + /* methods */ + MD5_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + MD5_methods, /* tp_methods */ + NULL, /* tp_members */ + MD5_getseters, /* tp_getset */ +}; + + +/* The single module-level function: new() */ + +PyDoc_STRVAR(MD5_new__doc__, +"Return a new MD5 hash object; optionally initialized with a string."); + +static PyObject * +MD5_new(PyObject *self, PyObject *args, PyObject *kwdict) +{ + static char *kwlist[] = {"string", NULL}; + MD5object *new; + unsigned char *cp = NULL; + int len; + + if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist, + &cp, &len)) { + return NULL; + } + + if ((new = newMD5object()) == NULL) + return NULL; + + md5_init(&new->hash_state); + + if (PyErr_Occurred()) { + Py_DECREF(new); + return NULL; + } + if (cp) + md5_process(&new->hash_state, cp, len); + + return (PyObject *)new; +} + + +/* List of functions exported by this module */ + +static struct PyMethodDef MD5_functions[] = { + {"md5", (PyCFunction)MD5_new, METH_VARARGS|METH_KEYWORDS, MD5_new__doc__}, + {NULL, NULL} /* Sentinel */ +}; + + +/* Initialize this module. */ + +#define insint(n,v) { PyModule_AddIntConstant(m,n,v); } + +PyMODINIT_FUNC +init_md5(void) +{ + PyObject *m; + + Py_Type(&MD5type) = &PyType_Type; + if (PyType_Ready(&MD5type) < 0) + return; + m = Py_InitModule("_md5", MD5_functions); + if (m == NULL) + return; +} diff --git a/Modules/sha1module.c b/Modules/sha1module.c new file mode 100644 index 0000000..d96fe35 --- /dev/null +++ b/Modules/sha1module.c @@ -0,0 +1,537 @@ +/* SHA1 module */ + +/* This module provides an interface to the SHA1 algorithm */ + +/* See below for information about the original code this module was + based upon. Additional work performed by: + + Andrew Kuchling (amk@amk.ca) + Greg Stein (gstein@lyra.org) + Trevor Perrin (trevp@trevp.net) + + Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) + Licensed to PSF under a Contributor Agreement. + +*/ + +/* SHA1 objects */ + +#include "Python.h" + + +/* Some useful types */ + +#if SIZEOF_INT == 4 +typedef unsigned int SHA1_INT32; /* 32-bit integer */ +typedef PY_LONG_LONG SHA1_INT64; /* 64-bit integer */ +#else +/* not defined. compilation will die. */ +#endif + +/* The SHA1 block size and message digest sizes, in bytes */ + +#define SHA1_BLOCKSIZE 64 +#define SHA1_DIGESTSIZE 20 + +/* The structure for storing SHA1 info */ + +struct sha1_state { + SHA1_INT64 length; + SHA1_INT32 state[5], curlen; + unsigned char buf[SHA1_BLOCKSIZE]; +}; + +typedef struct { + PyObject_HEAD + + struct sha1_state hash_state; +} SHA1object; + + +/* ------------------------------------------------------------------------ + * + * This code for the SHA1 algorithm was noted as public domain. The + * original headers are pasted below. + * + * Several changes have been made to make it more compatible with the + * Python environment and desired interface. + * + */ + +/* LibTomCrypt, modular cryptographic library -- Tom St Denis + * + * LibTomCrypt is a library that provides various cryptographic + * algorithms in a highly modular and flexible manner. + * + * The library is free for all purposes without any express + * guarantee it works. + * + * Tom St Denis, tomstdenis@gmail.com, http://libtom.org + */ + +/* rotate the hard way (platform optimizations could be done) */ +#define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) +#define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) + +/* Endian Neutral macros that work on all platforms */ + +#define STORE32H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ + (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } + +#define LOAD32H(x, y) \ + { x = ((unsigned long)((y)[0] & 255)<<24) | \ + ((unsigned long)((y)[1] & 255)<<16) | \ + ((unsigned long)((y)[2] & 255)<<8) | \ + ((unsigned long)((y)[3] & 255)); } + +#define STORE64H(x, y) \ + { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ + (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ + (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ + (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } + +#ifndef MIN + #define MIN(x, y) ( ((x)<(y))?(x):(y) ) +#endif + + +/* SHA1 macros */ + +#define F0(x,y,z) (z ^ (x & (y ^ z))) +#define F1(x,y,z) (x ^ y ^ z) +#define F2(x,y,z) ((x & y) | (z & (x | y))) +#define F3(x,y,z) (x ^ y ^ z) + +static void sha1_compress(struct sha1_state *sha1, unsigned char *buf) +{ + SHA1_INT32 a,b,c,d,e,W[80],i; + + /* copy the state into 512-bits into W[0..15] */ + for (i = 0; i < 16; i++) { + LOAD32H(W[i], buf + (4*i)); + } + + /* copy state */ + a = sha1->state[0]; + b = sha1->state[1]; + c = sha1->state[2]; + d = sha1->state[3]; + e = sha1->state[4]; + + /* expand it */ + for (i = 16; i < 80; i++) { + W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); + } + + /* compress */ + /* round one */ + #define FF0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30); + #define FF1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30); + #define FF2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30); + #define FF3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30); + + for (i = 0; i < 20; ) { + FF0(a,b,c,d,e,i++); + FF0(e,a,b,c,d,i++); + FF0(d,e,a,b,c,i++); + FF0(c,d,e,a,b,i++); + FF0(b,c,d,e,a,i++); + } + + /* round two */ + for (; i < 40; ) { + FF1(a,b,c,d,e,i++); + FF1(e,a,b,c,d,i++); + FF1(d,e,a,b,c,i++); + FF1(c,d,e,a,b,i++); + FF1(b,c,d,e,a,i++); + } + + /* round three */ + for (; i < 60; ) { + FF2(a,b,c,d,e,i++); + FF2(e,a,b,c,d,i++); + FF2(d,e,a,b,c,i++); + FF2(c,d,e,a,b,i++); + FF2(b,c,d,e,a,i++); + } + + /* round four */ + for (; i < 80; ) { + FF3(a,b,c,d,e,i++); + FF3(e,a,b,c,d,i++); + FF3(d,e,a,b,c,i++); + FF3(c,d,e,a,b,i++); + FF3(b,c,d,e,a,i++); + } + + #undef FF0 + #undef FF1 + #undef FF2 + #undef FF3 + + /* store */ + sha1->state[0] = sha1->state[0] + a; + sha1->state[1] = sha1->state[1] + b; + sha1->state[2] = sha1->state[2] + c; + sha1->state[3] = sha1->state[3] + d; + sha1->state[4] = sha1->state[4] + e; +} + +/** + Initialize the hash state + @param sha1 The hash state you wish to initialize +*/ +void sha1_init(struct sha1_state *sha1) +{ + assert(sha1 != NULL); + sha1->state[0] = 0x67452301UL; + sha1->state[1] = 0xefcdab89UL; + sha1->state[2] = 0x98badcfeUL; + sha1->state[3] = 0x10325476UL; + sha1->state[4] = 0xc3d2e1f0UL; + sha1->curlen = 0; + sha1->length = 0; +} + +/** + Process a block of memory though the hash + @param sha1 The hash state + @param in The data to hash + @param inlen The length of the data (octets) +*/ +void sha1_process(struct sha1_state *sha1, + const unsigned char *in, unsigned long inlen) +{ + unsigned long n; + + assert(sha1 != NULL); + assert(in != NULL); + assert(sha1->curlen <= sizeof(sha1->buf)); + + while (inlen > 0) { + if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) { + sha1_compress(sha1, (unsigned char *)in); + sha1->length += SHA1_BLOCKSIZE * 8; + in += SHA1_BLOCKSIZE; + inlen -= SHA1_BLOCKSIZE; + } else { + n = MIN(inlen, (SHA1_BLOCKSIZE - sha1->curlen)); + memcpy(sha1->buf + sha1->curlen, in, (size_t)n); + sha1->curlen += n; + in += n; + inlen -= n; + if (sha1->curlen == SHA1_BLOCKSIZE) { + sha1_compress(sha1, sha1->buf); + sha1->length += 8*SHA1_BLOCKSIZE; + sha1->curlen = 0; + } + } + } +} + +/** + Terminate the hash to get the digest + @param sha1 The hash state + @param out [out] The destination of the hash (20 bytes) +*/ +void sha1_done(struct sha1_state *sha1, unsigned char *out) +{ + int i; + + assert(sha1 != NULL); + assert(out != NULL); + assert(sha1->curlen < sizeof(sha1->buf)); + + /* increase the length of the message */ + sha1->length += sha1->curlen * 8; + + /* append the '1' bit */ + sha1->buf[sha1->curlen++] = (unsigned char)0x80; + + /* if the length is currently above 56 bytes we append zeros + * then compress. Then we can fall back to padding zeros and length + * encoding like normal. + */ + if (sha1->curlen > 56) { + while (sha1->curlen < 64) { + sha1->buf[sha1->curlen++] = (unsigned char)0; + } + sha1_compress(sha1, sha1->buf); + sha1->curlen = 0; + } + + /* pad upto 56 bytes of zeroes */ + while (sha1->curlen < 56) { + sha1->buf[sha1->curlen++] = (unsigned char)0; + } + + /* store length */ + STORE64H(sha1->length, sha1->buf+56); + sha1_compress(sha1, sha1->buf); + + /* copy output */ + for (i = 0; i < 5; i++) { + STORE32H(sha1->state[i], out+(4*i)); + } +} + + +/* .Source: /cvs/libtom/libtomcrypt/src/hashes/sha1.c,v $ */ +/* .Revision: 1.10 $ */ +/* .Date: 2007/05/12 14:25:28 $ */ + +/* + * End of copied SHA1 code. + * + * ------------------------------------------------------------------------ + */ + +static PyTypeObject SHA1type; + + +static SHA1object * +newSHA1object(void) +{ + return (SHA1object *)PyObject_New(SHA1object, &SHA1type); +} + + +/* Internal methods for a hash object */ + +static void +SHA1_dealloc(PyObject *ptr) +{ + PyObject_Del(ptr); +} + + +/* External methods for a hash object */ + +PyDoc_STRVAR(SHA1_copy__doc__, "Return a copy of the hash object."); + +static PyObject * +SHA1_copy(SHA1object *self, PyObject *unused) +{ + SHA1object *newobj; + + if (Py_Type(self) == &SHA1type) { + if ( (newobj = newSHA1object())==NULL) + return NULL; + } else { + if ( (newobj = newSHA1object())==NULL) + return NULL; + } + + newobj->hash_state = self->hash_state; + return (PyObject *)newobj; +} + +PyDoc_STRVAR(SHA1_digest__doc__, +"Return the digest value as a string of binary data."); + +static PyObject * +SHA1_digest(SHA1object *self, PyObject *unused) +{ + unsigned char digest[SHA1_DIGESTSIZE]; + struct sha1_state temp; + + temp = self->hash_state; + sha1_done(&temp, digest); + return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE); +} + +PyDoc_STRVAR(SHA1_hexdigest__doc__, +"Return the digest value as a string of hexadecimal digits."); + +static PyObject * +SHA1_hexdigest(SHA1object *self, PyObject *unused) +{ + unsigned char digest[SHA1_DIGESTSIZE]; + struct sha1_state temp; + PyObject *retval; + Py_UNICODE *hex_digest; + int i, j; + + /* Get the raw (binary) digest value */ + temp = self->hash_state; + sha1_done(&temp, digest); + + /* Create a new string */ + retval = PyUnicode_FromStringAndSize(NULL, SHA1_DIGESTSIZE * 2); + if (!retval) + return NULL; + hex_digest = PyUnicode_AS_UNICODE(retval); + if (!hex_digest) { + Py_DECREF(retval); + return NULL; + } + + /* Make hex version of the digest */ + for(i=j=0; i<SHA1_DIGESTSIZE; i++) { + char c; + c = (digest[i] >> 4) & 0xf; + c = (c>9) ? c+'a'-10 : c + '0'; + hex_digest[j++] = c; + c = (digest[i] & 0xf); + c = (c>9) ? c+'a'-10 : c + '0'; + hex_digest[j++] = c; + } + return retval; +} + +PyDoc_STRVAR(SHA1_update__doc__, +"Update this hash object's state with the provided string."); + +static PyObject * +SHA1_update(SHA1object *self, PyObject *args) +{ + unsigned char *cp; + int len; + + if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) + return NULL; + + sha1_process(&self->hash_state, cp, len); + + Py_INCREF(Py_None); + return Py_None; +} + +static PyMethodDef SHA1_methods[] = { + {"copy", (PyCFunction)SHA1_copy, METH_NOARGS, SHA1_copy__doc__}, + {"digest", (PyCFunction)SHA1_digest, METH_NOARGS, SHA1_digest__doc__}, + {"hexdigest", (PyCFunction)SHA1_hexdigest, METH_NOARGS, SHA1_hexdigest__doc__}, + {"update", (PyCFunction)SHA1_update, METH_VARARGS, SHA1_update__doc__}, + {NULL, NULL} /* sentinel */ +}; + +static PyObject * +SHA1_get_block_size(PyObject *self, void *closure) +{ + return PyInt_FromLong(SHA1_BLOCKSIZE); +} + +static PyObject * +SHA1_get_name(PyObject *self, void *closure) +{ + return PyUnicode_FromStringAndSize("SHA1", 3); +} + +static PyObject * +sha1_get_digest_size(PyObject *self, void *closure) +{ + return PyInt_FromLong(SHA1_DIGESTSIZE); +} + + +static PyGetSetDef SHA1_getseters[] = { + {"block_size", + (getter)SHA1_get_block_size, NULL, + NULL, + NULL}, + {"name", + (getter)SHA1_get_name, NULL, + NULL, + NULL}, + {"digest_size", + (getter)sha1_get_digest_size, NULL, + NULL, + NULL}, + {NULL} /* Sentinel */ +}; + +static PyTypeObject SHA1type = { + PyVarObject_HEAD_INIT(NULL, 0) + "_sha1.sha1", /*tp_name*/ + sizeof(SHA1object), /*tp_size*/ + 0, /*tp_itemsize*/ + /* methods */ + SHA1_dealloc, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + 0, /*tp_doc*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + SHA1_methods, /* tp_methods */ + NULL, /* tp_members */ + SHA1_getseters, /* tp_getset */ +}; + + +/* The single module-level function: new() */ + +PyDoc_STRVAR(SHA1_new__doc__, +"Return a new SHA1 hash object; optionally initialized with a string."); + +static PyObject * +SHA1_new(PyObject *self, PyObject *args, PyObject *kwdict) +{ + static char *kwlist[] = {"string", NULL}; + SHA1object *new; + unsigned char *cp = NULL; + int len; + + if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s#:new", kwlist, + &cp, &len)) { + return NULL; + } + + if ((new = newSHA1object()) == NULL) + return NULL; + + sha1_init(&new->hash_state); + + if (PyErr_Occurred()) { + Py_DECREF(new); + return NULL; + } + if (cp) + sha1_process(&new->hash_state, cp, len); + + return (PyObject *)new; +} + + +/* List of functions exported by this module */ + +static struct PyMethodDef SHA1_functions[] = { + {"sha1",(PyCFunction)SHA1_new, METH_VARARGS|METH_KEYWORDS,SHA1_new__doc__}, + {NULL, NULL} /* Sentinel */ +}; + + +/* Initialize this module. */ + +#define insint(n,v) { PyModule_AddIntConstant(m,n,v); } + +PyMODINIT_FUNC +init_sha1(void) +{ + PyObject *m; + + Py_Type(&SHA1type) = &PyType_Type; + if (PyType_Ready(&SHA1type) < 0) + return; + m = Py_InitModule("_sha1", SHA1_functions); + if (m == NULL) + return; +} diff --git a/Modules/sha256module.c b/Modules/sha256module.c index 27c4b19..da31d18 100644 --- a/Modules/sha256module.c +++ b/Modules/sha256module.c @@ -9,7 +9,7 @@ Greg Stein (gstein@lyra.org) Trevor Perrin (trevp@trevp.net) - Copyright (C) 2005 Gregory P. Smith (greg@electricrain.com) + Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) Licensed to PSF under a Contributor Agreement. */ @@ -103,7 +103,7 @@ static void SHAcopy(SHAobject *src, SHAobject *dest) * The library is free for all purposes without any express * gurantee it works. * - * Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org + * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org */ diff --git a/Modules/sha512module.c b/Modules/sha512module.c index 1d3a70b..8b33bb0 100644 --- a/Modules/sha512module.c +++ b/Modules/sha512module.c @@ -9,7 +9,7 @@ Greg Stein (gstein@lyra.org) Trevor Perrin (trevp@trevp.net) - Copyright (C) 2005 Gregory P. Smith (greg@electricrain.com) + Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org) Licensed to PSF under a Contributor Agreement. */ @@ -113,7 +113,7 @@ static void SHAcopy(SHAobject *src, SHAobject *dest) * The library is free for all purposes without any express * gurantee it works. * - * Tom St Denis, tomstdenis@iahu.ca, http://libtomcrypt.org + * Tom St Denis, tomstdenis@iahu.ca, http://libtom.org */ @@ -618,6 +618,11 @@ class PyBuildExt(build_ext): exts.append( Extension('_sha256', ['sha256module.c']) ) exts.append( Extension('_sha512', ['sha512module.c']) ) + if not openssl_ver: + # no openssl at all, use our own md5 and sha1 + exts.append( Extension('_md5', ['md5module.c']) ) + exts.append( Extension('_sha1', ['sha1module.c']) ) + # Modules that provide persistent dictionary-like semantics. You will # probably want to arrange for at least one of them to be available on # your machine, though none are defined by default because of library |