--- old-from_zaula_new_and_improved/pycryptopp/hash/tigermodule.cpp 1969-12-31 17:00:00.000000000 -0700 +++ new-from_zaula_new_and_improved/pycryptopp/hash/tigermodule.cpp 2009-03-02 14:23:09.000000000 -0700 @@ -0,0 +1,197 @@ +/** + * tigermodule.cpp -- Python wrappers around Crypto++'s Tiger hash + */ + +#include + +#if (PY_VERSION_HEX < 0x02050000) +typedef int Py_ssize_t; +#endif + +/* from Crypto++ */ +#include "tiger.h" +#include "hex.h" +#include "filters.h" + +static char tiger__doc__[] = "\ +tiger hash function\ +"; + +static PyObject *tiger_error; + +typedef struct { + PyObject_HEAD + + /* internal */ + CryptoPP::Tiger* h; + PyStringObject* digest; +} Tiger; + +PyDoc_STRVAR(Tiger__doc__, +"a Tiger hash object\n\ +Its constructor takes an optional string, which has the same effect as\n\ +calling .update() with that string."); + +static PyObject * +Tiger_update(Tiger* self, PyObject* msgobj) { + if (self->digest) + return PyErr_Format(tiger_error, "Precondition violation: once .digest() has been called you are required to never call .update() again."); + + const char *msg; + Py_ssize_t msgsize; + if (PyString_AsStringAndSize(msgobj, const_cast(&msg), &msgsize)) + return NULL; + self->h->Update(reinterpret_cast(msg), msgsize); + Py_RETURN_NONE; +} + +PyDoc_STRVAR(Tiger_update__doc__, +"Update the hash object with the string msg. Repeated calls are equivalent to\n\ +a single call with the concatenation of all the messages."); + +static PyObject * +Tiger_digest(Tiger* self, PyObject* dummy) { + if (!self->digest) { + self->digest = reinterpret_cast(PyString_FromStringAndSize(NULL, self->h->DigestSize())); + if (!self->digest) + return NULL; + self->h->Final(reinterpret_cast(PyString_AS_STRING(self->digest))); + } + + Py_INCREF(self->digest); + return reinterpret_cast(self->digest); +} + +PyDoc_STRVAR(Tiger_digest__doc__, +"Return the binary digest of the messages that were passed to the update()\n\ +method (including the initial message if any)."); + +static PyObject * +Tiger_hexdigest(Tiger* self, PyObject* dummy) { + PyObject* digest = Tiger_digest(self, NULL); + if (!digest) + return NULL; + Py_ssize_t dsize = PyString_GET_SIZE(digest); + PyStringObject* hexdigest = reinterpret_cast(PyString_FromStringAndSize(NULL, dsize*2)); + CryptoPP::ArraySink* as = new CryptoPP::ArraySink(reinterpret_cast(PyString_AS_STRING(hexdigest)), dsize*2); + CryptoPP::HexEncoder enc; + enc.Attach(as); + enc.Put(reinterpret_cast(PyString_AS_STRING(digest)), static_cast(dsize)); + Py_DECREF(digest); digest = NULL; + + return reinterpret_cast(hexdigest); +} + +PyDoc_STRVAR(Tiger_hexdigest__doc__, +"Return the hex-encoded digest of the messages that were passed to the update()\n\ +method (including the initial message if any)."); + +static PyMethodDef Tiger_methods[] = { + {"update", reinterpret_cast(Tiger_update), METH_O, Tiger_update__doc__}, + {"digest", reinterpret_cast(Tiger_digest), METH_NOARGS, Tiger_digest__doc__}, + {"hexdigest", reinterpret_cast(Tiger_hexdigest), METH_NOARGS, Tiger_hexdigest__doc__}, + {NULL}, +}; + +static PyObject * +Tiger_new(PyTypeObject* type, PyObject *args, PyObject *kwdict) { + Tiger* self = reinterpret_cast(type->tp_alloc(type, 0)); + if (!self) + return NULL; + self->h = new CryptoPP::Tiger(); + if (!self->h) + return PyErr_NoMemory(); + self->digest = NULL; + return reinterpret_cast(self); +} + +static void +Tiger_dealloc(Tiger* self) { + Py_XDECREF(self->digest); + delete self->h; + self->ob_type->tp_free((PyObject*)self); +} + +static int +Tiger_init(PyObject* self, PyObject *args, PyObject *kwdict) { + static const char *kwlist[] = { "msg", NULL }; + const char *msg = NULL; + size_t msgsize = 0; + if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|t#", const_cast(kwlist), &msg, &msgsize)) + return -1; + + if (msg) + reinterpret_cast(self)->h->Update(reinterpret_cast(msg), msgsize); + return 0; +} + +static PyTypeObject Tiger_type = { + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "tiger.Tiger", /*tp_name*/ + sizeof(Tiger), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + reinterpret_cast(Tiger_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 | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + Tiger__doc__, /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + Tiger_methods, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + //reinterpret_cast(Tiger_init), /* tp_init */ + Tiger_init, /* tp_init */ + 0, /* tp_alloc */ + Tiger_new, /* tp_new */ +}; + +static struct PyMethodDef tiger_functions[] = { + {NULL, NULL} /* Sentinel */ +}; + +#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ +#define PyMODINIT_FUNC void +#endif +PyMODINIT_FUNC +inittiger(void) { + PyObject *module; + PyObject *module_dict; + + if (PyType_Ready(&Tiger_type) < 0) + return; + + module = Py_InitModule3("tiger", tiger_functions, tiger__doc__); + if (!module) + return; + + Py_INCREF(&Tiger_type); + + PyModule_AddObject(module, "Tiger", (PyObject *)&Tiger_type); + + module_dict = PyModule_GetDict(module); + tiger_error = PyErr_NewException(const_cast("tiger.Error"), NULL, NULL); + PyDict_SetItemString(module_dict, "Error", tiger_error); +} diff -rN -u old-from_zaula_new_and_improved/pycryptopp/publickey/ecdsamodule.cpp new-from_zaula_new_and_improved/pycryptopp/publickey/ecdsamodule.cpp --- old-from_zaula_new_and_improved/pycryptopp/test/test_tiger.py 1969-12-31 17:00:00.000000000 -0700 +++ new-from_zaula_new_and_improved/pycryptopp/test/test_tiger.py 2009-03-02 14:23:09.000000000 -0700 @@ -0,0 +1,133 @@ +#!/usr/bin/env python + +import os, random, re + +import unittest + +from binascii import b2a_hex, a2b_hex + +global VERBOSE +VERBOSE=False + +from pycryptopp.hash import tiger + +from base64 import b32encode +def ab(x): # debuggery + if len(x) >= 3: + return "%s:%s" % (len(x), b32encode(x[-3:]),) + elif len(x) == 2: + return "%s:%s" % (len(x), b32encode(x[-2:]),) + elif len(x) == 1: + return "%s:%s" % (len(x), b32encode(x[-1:]),) + elif len(x) == 0: + return "%s:%s" % (len(x), "--empty--",) + +def randstr(n): + return ''.join(map(chr, map(random.randrange, [0]*n, [256]*n))) + +h0 = a2b_hex("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") +h_bd = a2b_hex("68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b") +h_5fd4 = a2b_hex("7c4fbf484498d21b487b9d61de8914b2eadaf2698712936d47c3ada2558f6788") + +class Tiger(unittest.TestCase): + def test_digest(self): + empty_digest = tiger.Tiger().digest() + self.failUnless(isinstance(empty_digest, str)) + self.failUnlessEqual(len(empty_digest), 32) + self.failUnlessEqual(empty_digest, h0) + + def test_hexdigest(self): + empty_hexdigest = tiger.Tiger().hexdigest() + self.failUnlessEqual(a2b_hex(empty_hexdigest), h0) + test_hexdigest.todo = "Not yet implemented: Tiger.hexdigest()." + + def test_onebyte_1(self): + d = tiger.Tiger("\xbd").digest() + self.failUnlessEqual(d, h_bd) + + def test_onebyte_2(self): + s = tiger.Tiger() + s.update("\xbd") + d = s.digest() + self.failUnlessEqual(d, h_bd) + + def test_update(self): + s = tiger.Tiger("\x5f") + s.update("\xd4") + d = s.digest() + self.failUnlessEqual(d, h_5fd4) + + def test_constructor_type_check(self): + self.failUnlessRaises(TypeError, tiger.Tiger, None) + + def test_update_type_check(self): + h = tiger.Tiger() + self.failUnlessRaises(TypeError, h.update, None) + + def test_digest_twice(self): + h = tiger.Tiger() + d1 = h.digest() + self.failUnless(isinstance(d1, str)) + d2 = h.digest() + self.failUnlessEqual(d1, d2) + + def test_digest_then_update_fail(self): + h = tiger.Tiger() + d1 = h.digest() + try: + h.update("oops") + except tiger.Error, le: + self.failUnless("digest() has been called" in str(le), le) + +VECTS_RE=re.compile("\nLen = ([0-9]+)\nMsg = ([0-9a-f]+)\nMD = ([0-9a-f]+)") + +class SHSVectors(unittest.TestCase): + """ + All of the SHA-256 test vectors from the NIST SHS, in the files distributed + by NIST. (NIST distributes them in a .zip, but we expect them to be + unpacked and in a subdirectory named 'vectors'). + """ + def test_short(self): + return self._test_vect_file(open(os.path.join('pycryptopp', 'test', 'vectors', 'SHA256ShortMsg.txt'), 'rU')) + + def test_long(self): + return self._test_vect_file(open(os.path.join('pycryptopp', 'test', 'vectors', 'SHA256LongMsg.txt'), 'rU')) + + def _test_vect_file(self, infile): + vects_str = infile.read() + for mo in VECTS_RE.finditer(vects_str): + msglenbits = int(mo.group(1)) + assert msglenbits % 8 == 0 + msglen = msglenbits / 8 + msg = a2b_hex(mo.group(2))[:msglen] # The slice is necessary because NIST seems to think that "00" is a reasonable representation for the zero-length string. + assert len(msg) == msglen, (len(msg), msglen) + md = a2b_hex(mo.group(3)) + + computed_md = tiger.Tiger(msg).digest() + self.failUnlessEqual(computed_md, md) + + def test_monte(self): + infile = open(os.path.join('pycryptopp', 'test', 'vectors', 'SHA256Monte.txt'), 'rU') + for line in infile: + line = line.strip() + if line[:7] == 'Seed = ': + seed = a2b_hex(line[7:]) + break + + j = 0 + for line in infile: + line = line.strip() + if line[:8] == 'COUNT = ': + assert int(line[8:]) == j + elif line[:5] == 'MD = ': + mds = [] + mds.append(seed);mds.append(seed);mds.append(seed); + for i in range(1000): + m = mds[-3]+mds[-2]+mds[-1] + mds.append(tiger.Tiger(m).digest()) + seed = mds[-1] + self.failUnlessEqual(line[5:], b2a_hex(seed)) + j += 1 + +if __name__ == "__main__": + unittest.main() diff -rN -u old-from_zaula_new_and_improved/pycryptopp/test/vectors/salsa.txt new-from_zaula_new_and_improved/pycryptopp/test/vectors/salsa.txt --- old-from_zaula_new_and_improved/setup.py 2009-03-02 14:23:06.000000000 -0700 +++ new-from_zaula_new_and_improved/setup.py 2009-03-02 14:23:09.000000000 -0700 @@ -100,9 +100,17 @@ ) ext_modules.append( + Extension('pycryptopp.hash.tiger', cryptopp_src + ['pycryptopp/hash/tigermodule.cpp',], include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries, extra_link_args=extra_link_args, extra_compile_args=extra_compile_args, define_macros=define_macros, undef_macros=undef_macros) + ) + +ext_modules.append( Extension('pycryptopp.cipher.aes', cryptopp_src + ['pycryptopp/cipher/aesmodule.cpp',], include_dirs=include_dirs, library_dirs=library_dirs, libraries=libraries, extra_link_args=extra_link_args, extra_compile_args=extra_compile_args, define_macros=define_macros, undef_macros=undef_macros) )