[Pymoul-svn] SF.net SVN: pymoul: [211] xtea/trunk
Status: Alpha
Brought to you by:
tiran
From: <ti...@us...> - 2007-02-28 01:42:14
|
Revision: 211 http://pymoul.svn.sourceforge.net/pymoul/?rev=211&view=rev Author: tiran Date: 2007-02-27 17:42:12 -0800 (Tue, 27 Feb 2007) Log Message: ----------- Moved to package with pyrex optimization Modified Paths: -------------- xtea/trunk/Makefile xtea/trunk/README.txt xtea/trunk/setup.py xtea/trunk/src/xtea.egg-info/SOURCES.txt Added Paths: ----------- xtea/trunk/src/xtea/ xtea/trunk/src/xtea/__init__.py xtea/trunk/src/xtea/_xtea.c xtea/trunk/src/xtea/_xtea.pyx xtea/trunk/src/xtea/xtea.py xtea/trunk/src/xtea.egg-info/native_libs.txt Removed Paths: ------------- xtea/trunk/src/xtea.py Property Changed: ---------------- xtea/trunk/ Property changes on: xtea/trunk ___________________________________________________________________ Name: svn:ignore + build dist Modified: xtea/trunk/Makefile =================================================================== --- xtea/trunk/Makefile 2007-02-27 16:22:39 UTC (rev 210) +++ xtea/trunk/Makefile 2007-02-28 01:42:12 UTC (rev 211) @@ -9,30 +9,33 @@ all: inplace # Build in-place -inplace: +inplace: pyrex $(PYTHON) setup.py $(SETUPFLAGS) build_ext -i -build: +build: pyrex $(PYTHON) setup.py $(SETUPFLAGS) build +pyrex: + pyrexc src/xtea/_xtea.pyx + egg24: $(PYTHON24) setup.py $(SETUPFLAGS) bdist_egg egg25: $(PYTHON25) setup.py $(SETUPFLAGS) bdist_egg -dist25: +dist25: realclean build $(PYTHON25) setup.py $(SETUPFLAGS) bdist_egg \ sdist --formats=zip,gztar -dist_upload: realclean +dist_upload: realclean build $(PYTHON25) setup.py $(SETUPFLAGS) bdist_egg \ sdist --formats=zip,gztar \ upload --sign --identity=$(KEYID) # What should the default be? test: - $(PYTHON) xtea.py + $(PYTHON) src/xtea/xtea.py egg: egg24 egg25 Modified: xtea/trunk/README.txt =================================================================== --- xtea/trunk/README.txt 2007-02-27 16:22:39 UTC (rev 210) +++ xtea/trunk/README.txt 2007-02-28 01:42:12 UTC (rev 211) @@ -3,6 +3,8 @@ Author: Paul Chakravarti (paul_dot_chakravarti_at_mac_dot_com) License: Public Domain +pyrex optimization: Christian Heimes + This module provides a Python implementation of the XTEA block encryption algorithm (http://www.cix.co.uk/~klockstone/xtea.pdf). Modified: xtea/trunk/setup.py =================================================================== --- xtea/trunk/setup.py 2007-02-27 16:22:39 UTC (rev 210) +++ xtea/trunk/setup.py 2007-02-28 01:42:12 UTC (rev 211) @@ -28,6 +28,7 @@ # import the rest from setuptools import setup from setuptools import find_packages +from setuptools import Extension from distutils.core import setup @@ -60,9 +61,9 @@ 'Programming Language :: Python', 'Topic :: Software Development :: Libraries' ), + ext_modules=[Extension('xtea._xtea', ['src/xtea/_xtea.c'])], setup_requires = ["setuptools>="+SETUPTOOLS_VERSION,], - #packages = ['xtea'], - py_modules = ['xtea'], + packages = ['xtea'], package_dir = {'' : 'src'}, zip_safe = True, ) Property changes on: xtea/trunk/src/xtea ___________________________________________________________________ Name: svn:ignore + _xtea.so Added: xtea/trunk/src/xtea/__init__.py =================================================================== --- xtea/trunk/src/xtea/__init__.py (rev 0) +++ xtea/trunk/src/xtea/__init__.py 2007-02-28 01:42:12 UTC (rev 211) @@ -0,0 +1,6 @@ +# xtea package +from xtea import crypt +from xtea import xtea_encrypt +from xtea import xtea_encryptQuad +from xtea import xtea_decrypt +from xtea import xtea_decryptQuad Property changes on: xtea/trunk/src/xtea/__init__.py ___________________________________________________________________ Name: svn:keywords + 'Id Revision' Name: svn:eol-style + native Added: xtea/trunk/src/xtea/_xtea.c =================================================================== --- xtea/trunk/src/xtea/_xtea.c (rev 0) +++ xtea/trunk/src/xtea/_xtea.c 2007-02-28 01:42:12 UTC (rev 211) @@ -0,0 +1,391 @@ +/* Generated by Pyrex 0.9.4.1 on Wed Feb 28 02:39:57 2007 */ + +#include "Python.h" +#include "structmember.h" +#ifndef PY_LONG_LONG + #define PY_LONG_LONG LONG_LONG +#endif +#ifdef __cplusplus +#define __PYX_EXTERN_C extern "C" +#else +#define __PYX_EXTERN_C extern +#endif +__PYX_EXTERN_C double pow(double, double); + + +typedef struct {PyObject **p; char *s;} __Pyx_InternTabEntry; /*proto*/ +typedef struct {PyObject **p; char *s; long n;} __Pyx_StringTabEntry; /*proto*/ +static PyObject *__Pyx_UnpackItem(PyObject *, int); /*proto*/ +static int __Pyx_EndUnpack(PyObject *, int); /*proto*/ +static int __Pyx_PrintItem(PyObject *); /*proto*/ +static int __Pyx_PrintNewline(void); /*proto*/ +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ +static void __Pyx_ReRaise(void); /*proto*/ +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/ +static PyObject *__Pyx_GetExcValue(void); /*proto*/ +static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, char *name); /*proto*/ +static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/ +static int __Pyx_GetStarArgs(PyObject **args, PyObject **kwds, char *kwd_list[], int nargs, PyObject **args2, PyObject **kwds2); /*proto*/ +static void __Pyx_WriteUnraisable(char *name); /*proto*/ +static void __Pyx_AddTraceback(char *funcname); /*proto*/ +static PyTypeObject *__Pyx_ImportType(char *module_name, char *class_name, long size); /*proto*/ +static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/ +static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/ +static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, char *modname); /*proto*/ +static int __Pyx_InternStrings(__Pyx_InternTabEntry *t); /*proto*/ +static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/ +static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/ + +static PyObject *__pyx_m; +static PyObject *__pyx_b; +static int __pyx_lineno; +static char *__pyx_filename; +static char **__pyx_f; + +/* Declarations from _xtea */ + + +/* Implementation of _xtea */ + +static PyObject *__pyx_n__xtea_encryptQuad; +static PyObject *__pyx_n__xtea_decryptQuad; + +static PyObject *__pyx_f_5_xtea__xtea_encryptQuad(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_f_5_xtea__xtea_encryptQuad(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + unsigned long __pyx_v_v0; + unsigned long __pyx_v_v1; + PyObject *__pyx_v_key = 0; + int __pyx_v_rounds; + int __pyx_v_i; + unsigned long __pyx_v_delta; + unsigned long __pyx_v_sum; + PyObject *__pyx_v_result; + PyObject *__pyx_r; + PyObject *__pyx_1 = 0; + PyObject *__pyx_2 = 0; + PyObject *__pyx_3 = 0; + PyObject *__pyx_4 = 0; + PyObject *__pyx_5 = 0; + unsigned long __pyx_6; + static char *__pyx_argnames[] = {"v0","v1","key","rounds",0}; + if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "kkOi", __pyx_argnames, &__pyx_v_v0, &__pyx_v_v1, &__pyx_v_key, &__pyx_v_rounds)) return 0; + Py_INCREF(__pyx_v_key); + __pyx_v_result = Py_None; Py_INCREF(Py_None); + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":11 */ + __pyx_v_delta = 2654435769; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":12 */ + __pyx_v_sum = 0; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":13 */ + __pyx_1 = PyList_New(0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 13; goto __pyx_L1;} + Py_DECREF(__pyx_v_result); + __pyx_v_result = __pyx_1; + __pyx_1 = 0; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":15 */ + for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_rounds; ++__pyx_v_i) { + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":16 */ + __pyx_1 = PyLong_FromUnsignedLong(__pyx_v_v0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + __pyx_2 = PyLong_FromUnsignedLong((((__pyx_v_v1 << 4) ^ (__pyx_v_v1 >> 5)) + __pyx_v_v1)); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + __pyx_3 = PyLong_FromUnsignedLong(__pyx_v_sum); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + __pyx_4 = PyLong_FromUnsignedLong((__pyx_v_sum & 3)); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + __pyx_5 = PyObject_GetItem(__pyx_v_key, __pyx_4); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + Py_DECREF(__pyx_4); __pyx_4 = 0; + __pyx_4 = PyNumber_Add(__pyx_3, __pyx_5); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + Py_DECREF(__pyx_3); __pyx_3 = 0; + Py_DECREF(__pyx_5); __pyx_5 = 0; + __pyx_3 = PyNumber_Xor(__pyx_2, __pyx_4); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + Py_DECREF(__pyx_2); __pyx_2 = 0; + Py_DECREF(__pyx_4); __pyx_4 = 0; + __pyx_5 = PyNumber_Add(__pyx_1, __pyx_3); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + Py_DECREF(__pyx_1); __pyx_1 = 0; + Py_DECREF(__pyx_3); __pyx_3 = 0; + __pyx_6 = PyInt_AsUnsignedLongMask(__pyx_5); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; goto __pyx_L1;} + Py_DECREF(__pyx_5); __pyx_5 = 0; + __pyx_v_v0 = __pyx_6; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":17 */ + __pyx_v_sum = (__pyx_v_sum + __pyx_v_delta); + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":18 */ + __pyx_2 = PyLong_FromUnsignedLong(__pyx_v_v1); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + __pyx_4 = PyLong_FromUnsignedLong((((__pyx_v_v0 << 4) ^ (__pyx_v_v0 >> 5)) + __pyx_v_v0)); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + __pyx_1 = PyLong_FromUnsignedLong(__pyx_v_sum); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + __pyx_3 = PyLong_FromUnsignedLong(((__pyx_v_sum >> 11) & 3)); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + __pyx_5 = PyObject_GetItem(__pyx_v_key, __pyx_3); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + Py_DECREF(__pyx_3); __pyx_3 = 0; + __pyx_3 = PyNumber_Add(__pyx_1, __pyx_5); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + Py_DECREF(__pyx_1); __pyx_1 = 0; + Py_DECREF(__pyx_5); __pyx_5 = 0; + __pyx_1 = PyNumber_Xor(__pyx_4, __pyx_3); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + Py_DECREF(__pyx_4); __pyx_4 = 0; + Py_DECREF(__pyx_3); __pyx_3 = 0; + __pyx_5 = PyNumber_Add(__pyx_2, __pyx_1); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + Py_DECREF(__pyx_2); __pyx_2 = 0; + Py_DECREF(__pyx_1); __pyx_1 = 0; + __pyx_6 = PyInt_AsUnsignedLongMask(__pyx_5); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; goto __pyx_L1;} + Py_DECREF(__pyx_5); __pyx_5 = 0; + __pyx_v_v1 = __pyx_6; + __pyx_L2:; + } + __pyx_L3:; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":20 */ + __pyx_4 = PyLong_FromUnsignedLong(__pyx_v_v0); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; goto __pyx_L1;} + __pyx_3 = PyLong_FromUnsignedLong(__pyx_v_v1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; goto __pyx_L1;} + __pyx_2 = PyList_New(2); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; goto __pyx_L1;} + PyList_SET_ITEM(__pyx_2, 0, __pyx_4); + PyList_SET_ITEM(__pyx_2, 1, __pyx_3); + __pyx_4 = 0; + __pyx_3 = 0; + Py_DECREF(__pyx_v_result); + __pyx_v_result = __pyx_2; + __pyx_2 = 0; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":21 */ + Py_INCREF(__pyx_v_result); + __pyx_r = __pyx_v_result; + goto __pyx_L0; + + __pyx_r = Py_None; Py_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1:; + Py_XDECREF(__pyx_1); + Py_XDECREF(__pyx_2); + Py_XDECREF(__pyx_3); + Py_XDECREF(__pyx_4); + Py_XDECREF(__pyx_5); + __Pyx_AddTraceback("_xtea._xtea_encryptQuad"); + __pyx_r = 0; + __pyx_L0:; + Py_DECREF(__pyx_v_result); + Py_DECREF(__pyx_v_key); + return __pyx_r; +} + +static PyObject *__pyx_f_5_xtea__xtea_decryptQuad(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_f_5_xtea__xtea_decryptQuad(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + unsigned long __pyx_v_v0; + unsigned long __pyx_v_v1; + PyObject *__pyx_v_key = 0; + int __pyx_v_rounds; + int __pyx_v_i; + unsigned long __pyx_v_delta; + unsigned long __pyx_v_sum; + PyObject *__pyx_v_result; + PyObject *__pyx_r; + PyObject *__pyx_1 = 0; + PyObject *__pyx_2 = 0; + PyObject *__pyx_3 = 0; + PyObject *__pyx_4 = 0; + PyObject *__pyx_5 = 0; + unsigned long __pyx_6; + static char *__pyx_argnames[] = {"v0","v1","key","rounds",0}; + if (!PyArg_ParseTupleAndKeywords(__pyx_args, __pyx_kwds, "kkOi", __pyx_argnames, &__pyx_v_v0, &__pyx_v_v1, &__pyx_v_key, &__pyx_v_rounds)) return 0; + Py_INCREF(__pyx_v_key); + __pyx_v_result = Py_None; Py_INCREF(Py_None); + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":27 */ + __pyx_v_delta = 2654435769; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":28 */ + __pyx_v_sum = (__pyx_v_delta * __pyx_v_rounds); + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":29 */ + __pyx_1 = PyList_New(0); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; goto __pyx_L1;} + Py_DECREF(__pyx_v_result); + __pyx_v_result = __pyx_1; + __pyx_1 = 0; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":31 */ + for (__pyx_v_i = 0; __pyx_v_i < __pyx_v_rounds; ++__pyx_v_i) { + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":32 */ + __pyx_1 = PyLong_FromUnsignedLong(__pyx_v_v1); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + __pyx_2 = PyLong_FromUnsignedLong((((__pyx_v_v0 << 4) ^ (__pyx_v_v0 >> 5)) + __pyx_v_v0)); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + __pyx_3 = PyLong_FromUnsignedLong(__pyx_v_sum); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + __pyx_4 = PyLong_FromUnsignedLong(((__pyx_v_sum >> 11) & 3)); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + __pyx_5 = PyObject_GetItem(__pyx_v_key, __pyx_4); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + Py_DECREF(__pyx_4); __pyx_4 = 0; + __pyx_4 = PyNumber_Add(__pyx_3, __pyx_5); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + Py_DECREF(__pyx_3); __pyx_3 = 0; + Py_DECREF(__pyx_5); __pyx_5 = 0; + __pyx_3 = PyNumber_Xor(__pyx_2, __pyx_4); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + Py_DECREF(__pyx_2); __pyx_2 = 0; + Py_DECREF(__pyx_4); __pyx_4 = 0; + __pyx_5 = PyNumber_Subtract(__pyx_1, __pyx_3); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + Py_DECREF(__pyx_1); __pyx_1 = 0; + Py_DECREF(__pyx_3); __pyx_3 = 0; + __pyx_6 = PyInt_AsUnsignedLongMask(__pyx_5); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; goto __pyx_L1;} + Py_DECREF(__pyx_5); __pyx_5 = 0; + __pyx_v_v1 = __pyx_6; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":33 */ + __pyx_v_sum = (__pyx_v_sum - __pyx_v_delta); + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":34 */ + __pyx_2 = PyLong_FromUnsignedLong(__pyx_v_v0); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + __pyx_4 = PyLong_FromUnsignedLong((((__pyx_v_v1 << 4) ^ (__pyx_v_v1 >> 5)) + __pyx_v_v1)); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + __pyx_1 = PyLong_FromUnsignedLong(__pyx_v_sum); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + __pyx_3 = PyLong_FromUnsignedLong((__pyx_v_sum & 3)); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + __pyx_5 = PyObject_GetItem(__pyx_v_key, __pyx_3); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + Py_DECREF(__pyx_3); __pyx_3 = 0; + __pyx_3 = PyNumber_Add(__pyx_1, __pyx_5); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + Py_DECREF(__pyx_1); __pyx_1 = 0; + Py_DECREF(__pyx_5); __pyx_5 = 0; + __pyx_1 = PyNumber_Xor(__pyx_4, __pyx_3); if (!__pyx_1) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + Py_DECREF(__pyx_4); __pyx_4 = 0; + Py_DECREF(__pyx_3); __pyx_3 = 0; + __pyx_5 = PyNumber_Subtract(__pyx_2, __pyx_1); if (!__pyx_5) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + Py_DECREF(__pyx_2); __pyx_2 = 0; + Py_DECREF(__pyx_1); __pyx_1 = 0; + __pyx_6 = PyInt_AsUnsignedLongMask(__pyx_5); if (PyErr_Occurred()) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; goto __pyx_L1;} + Py_DECREF(__pyx_5); __pyx_5 = 0; + __pyx_v_v0 = __pyx_6; + __pyx_L2:; + } + __pyx_L3:; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":36 */ + __pyx_4 = PyLong_FromUnsignedLong(__pyx_v_v0); if (!__pyx_4) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;} + __pyx_3 = PyLong_FromUnsignedLong(__pyx_v_v1); if (!__pyx_3) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;} + __pyx_2 = PyList_New(2); if (!__pyx_2) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; goto __pyx_L1;} + PyList_SET_ITEM(__pyx_2, 0, __pyx_4); + PyList_SET_ITEM(__pyx_2, 1, __pyx_3); + __pyx_4 = 0; + __pyx_3 = 0; + Py_DECREF(__pyx_v_result); + __pyx_v_result = __pyx_2; + __pyx_2 = 0; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":37 */ + Py_INCREF(__pyx_v_result); + __pyx_r = __pyx_v_result; + goto __pyx_L0; + + __pyx_r = Py_None; Py_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1:; + Py_XDECREF(__pyx_1); + Py_XDECREF(__pyx_2); + Py_XDECREF(__pyx_3); + Py_XDECREF(__pyx_4); + Py_XDECREF(__pyx_5); + __Pyx_AddTraceback("_xtea._xtea_decryptQuad"); + __pyx_r = 0; + __pyx_L0:; + Py_DECREF(__pyx_v_result); + Py_DECREF(__pyx_v_key); + return __pyx_r; +} + +static __Pyx_InternTabEntry __pyx_intern_tab[] = { + {&__pyx_n__xtea_decryptQuad, "_xtea_decryptQuad"}, + {&__pyx_n__xtea_encryptQuad, "_xtea_encryptQuad"}, + {0, 0} +}; + +static struct PyMethodDef __pyx_methods[] = { + {"_xtea_encryptQuad", (PyCFunction)__pyx_f_5_xtea__xtea_encryptQuad, METH_VARARGS|METH_KEYWORDS, 0}, + {"_xtea_decryptQuad", (PyCFunction)__pyx_f_5_xtea__xtea_decryptQuad, METH_VARARGS|METH_KEYWORDS, 0}, + {0, 0, 0, 0} +}; + +static void __pyx_init_filenames(void); /*proto*/ + +PyMODINIT_FUNC init_xtea(void); /*proto*/ +PyMODINIT_FUNC init_xtea(void) { + __pyx_init_filenames(); + __pyx_m = Py_InitModule4("_xtea", __pyx_methods, 0, 0, PYTHON_API_VERSION); + if (!__pyx_m) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; goto __pyx_L1;}; + __pyx_b = PyImport_AddModule("__builtin__"); + if (!__pyx_b) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; goto __pyx_L1;}; + if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; goto __pyx_L1;}; + if (__Pyx_InternStrings(__pyx_intern_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; goto __pyx_L1;}; + + /* "/home/heimes/dev/pymoul/xtea/src/xtea/_xtea.pyx":23 */ + return; + __pyx_L1:; + __Pyx_AddTraceback("_xtea"); +} + +static char *__pyx_filenames[] = { + "_xtea.pyx", +}; + +/* Runtime support code */ + +static void __pyx_init_filenames(void) { + __pyx_f = __pyx_filenames; +} + +static int __Pyx_InternStrings(__Pyx_InternTabEntry *t) { + while (t->p) { + *t->p = PyString_InternFromString(t->s); + if (!*t->p) + return -1; + ++t; + } + return 0; +} + +#include "compile.h" +#include "frameobject.h" +#include "traceback.h" + +static void __Pyx_AddTraceback(char *funcname) { + PyObject *py_srcfile = 0; + PyObject *py_funcname = 0; + PyObject *py_globals = 0; + PyObject *empty_tuple = 0; + PyObject *empty_string = 0; + PyCodeObject *py_code = 0; + PyFrameObject *py_frame = 0; + + py_srcfile = PyString_FromString(__pyx_filename); + if (!py_srcfile) goto bad; + py_funcname = PyString_FromString(funcname); + if (!py_funcname) goto bad; + py_globals = PyModule_GetDict(__pyx_m); + if (!py_globals) goto bad; + empty_tuple = PyTuple_New(0); + if (!empty_tuple) goto bad; + empty_string = PyString_FromString(""); + if (!empty_string) goto bad; + py_code = PyCode_New( + 0, /*int argcount,*/ + 0, /*int nlocals,*/ + 0, /*int stacksize,*/ + 0, /*int flags,*/ + empty_string, /*PyObject *code,*/ + empty_tuple, /*PyObject *consts,*/ + empty_tuple, /*PyObject *names,*/ + empty_tuple, /*PyObject *varnames,*/ + empty_tuple, /*PyObject *freevars,*/ + empty_tuple, /*PyObject *cellvars,*/ + py_srcfile, /*PyObject *filename,*/ + py_funcname, /*PyObject *name,*/ + __pyx_lineno, /*int firstlineno,*/ + empty_string /*PyObject *lnotab*/ + ); + if (!py_code) goto bad; + py_frame = PyFrame_New( + PyThreadState_Get(), /*PyThreadState *tstate,*/ + py_code, /*PyCodeObject *code,*/ + py_globals, /*PyObject *globals,*/ + 0 /*PyObject *locals*/ + ); + if (!py_frame) goto bad; + py_frame->f_lineno = __pyx_lineno; + PyTraceBack_Here(py_frame); +bad: + Py_XDECREF(py_srcfile); + Py_XDECREF(py_funcname); + Py_XDECREF(empty_tuple); + Py_XDECREF(empty_string); + Py_XDECREF(py_code); + Py_XDECREF(py_frame); +} Property changes on: xtea/trunk/src/xtea/_xtea.c ___________________________________________________________________ Name: svn:eol-style + native Added: xtea/trunk/src/xtea/_xtea.pyx =================================================================== --- xtea/trunk/src/xtea/_xtea.pyx (rev 0) +++ xtea/trunk/src/xtea/_xtea.pyx 2007-02-28 01:42:12 UTC (rev 211) @@ -0,0 +1,37 @@ +# Copyright (C) 2007 Christian Heimes <christian (at) cheimes (dot) de> +# pyrexc optimized version +# License: Public Domain + +ctypedef unsigned long ULong + +def _xtea_encryptQuad(ULong v0, ULong v1, key, int rounds): + cdef int i + cdef ULong delta, sum + + delta=0x9E3779B9 + sum=0 + result = [] + + for i from 0 <= i < rounds: + v0 = v0 + (((v1<<4 ^ v1>>5) + v1) ^ (sum + key[sum & 3])) + sum = sum + delta + v1 = v1 + (((v0<<4 ^ v0>>5) + v0) ^ (sum + key[sum>>11 & 3])) + + result = [v0, v1] + return result + +def _xtea_decryptQuad(ULong v0, ULong v1, key, int rounds): + cdef int i + cdef ULong delta, sum + + delta=0x9E3779B9 + sum=delta*rounds + result = [] + + for i from 0 <= i < rounds: + v1 = v1 - (((v0<<4 ^ v0>>5) + v0) ^ (sum + key[sum>>11 & 3])) + sum = sum - delta + v0 = v0 - (((v1<<4 ^ v1>>5) + v1) ^ (sum + key[sum & 3])) + + result = [v0, v1] + return result Added: xtea/trunk/src/xtea/xtea.py =================================================================== --- xtea/trunk/src/xtea/xtea.py (rev 0) +++ xtea/trunk/src/xtea/xtea.py 2007-02-28 01:42:12 UTC (rev 211) @@ -0,0 +1,170 @@ +""" +XTEA Block Encryption Algorithm + +Author: Paul Chakravarti (paul_dot_chakravarti_at_mac_dot_com) +License: Public Domain + +This module provides a Python implementation of the XTEA block encryption +algorithm (http://www.cix.co.uk/~klockstone/xtea.pdf). + +The module implements the basic XTEA block encryption algortithm +(`xtea_encrypt`/`xtea_decrypt`) and also provides a higher level `crypt` +function which symmetrically encrypts/decrypts a variable length string using +XTEA in OFB mode as a key generator. The `crypt` function does not use +`xtea_decrypt` which is provided for completeness only (but can be used +to support other stream modes - eg CBC/CFB). + +This module is intended to provide a simple 'privacy-grade' Python encryption +algorithm with no external dependencies. The implementation is relatively slow +and is best suited to small volumes of data. Note that the XTEA algorithm has +not been subjected to extensive analysis (though is believed to be relatively +secure - see http://en.wikipedia.org/wiki/XTEA). For applications requiring +'real' security please use a known and well tested algorithm/implementation. + +The security of the algorithm is entirely based on quality (entropy) and +secrecy of the key. You should generate the key from a known random source and +exchange using a trusted mechanism. In addition, you should always use a random +IV to seed the key generator (the IV is not sensitive and does not need to be +exchanged securely) + + >>> import os + >>> iv = 'ABCDEFGH' + >>> z = crypt('0123456789012345','Hello There',iv) + >>> z.encode('hex') + 'fe196d0a40d6c222b9eff3' + >>> crypt('0123456789012345',z,iv) + 'Hello There' + +""" + +import struct + +def crypt(key, data, iv='\00\00\00\00\00\00\00\00', n=32): + """ + Encrypt/decrypt variable length string using XTEA cypher as + key generator (OFB mode) + * key = 128 bit (16 char) + * iv = 64 bit (8 char) + * data = string (any length) + + >>> import os + >>> key = os.urandom(16) + >>> iv = os.urandom(8) + >>> data = os.urandom(10000) + >>> z = crypt(key,data,iv) + >>> crypt(key,z,iv) == data + True + + """ + def keygen(key, iv, n): + while True: + iv = xtea_encrypt(key, iv, n) + for k in iv: + yield ord(k) + xor = [ chr(x^y) for (x, y) in zip(map(ord, data), keygen(key, iv, n)) ] + return "".join(xor) + +def xtea_encrypt(key, block, rounds=32, endian="!"): + """ + Encrypt 64 bit data block using XTEA block cypher + * key = 128 bit (16 char) + * block = 64 bit (8 char) + * n = rounds (default 32) + * endian = byte order (see 'struct' doc - default big/network) + + >>> z = xtea_encrypt('0123456789012345','ABCDEFGH') + >>> z.encode('hex') + 'b67c01662ff6964a' + + Only need to change byte order if sending/receiving from + alternative endian implementation + + >>> z = xtea_encrypt('0123456789012345','ABCDEFGH',endian="<") + >>> z.encode('hex') + 'ea0c3d7c1c22557f' + + """ + v0,v1 = struct.unpack(endian+"2L", block) + k = struct.unpack(endian+"4L", key) + v0, v1 = xtea_encryptQuad(v0, v1, k, rounds) + return struct.pack(endian+"2L", v0, v1) + +def _xtea_encryptQuad(v0, v1, key, rounds=32, delta=0x9e3779b9L, mask=0xffffffffL): + """Encrypt a quad + + @param v0: value 0 + @type v0: int (unsigned long 32) + @param v1: value 1 + @type v1: int (unsigned long 32) + @param key: decryption key + @type key: list[4] int + @param n: rounds + @type n: int + @return: decrypted values + @rtype: list[2] int + """ + sum = 0 + for round in range(rounds): + v0 = (v0 + (((v1<<4 ^ v1>>5) + v1) ^ (sum + key[sum & 3]))) & mask + sum = (sum + delta) & mask + v1 = (v1 + (((v0<<4 ^ v0>>5) + v0) ^ (sum + key[sum>>11 & 3]))) & mask + return v0, v1 + +def xtea_decrypt(key, block, rounds=32, endian="!"): + """ + Decrypt 64 bit data block using XTEA block cypher + * key = 128 bit (16 char) + * block = 64 bit (8 char) + * n = rounds (default 32) + * endian = byte order (see 'struct' doc - default big/network) + + >>> z = 'b67c01662ff6964a'.decode('hex') + >>> xtea_decrypt('0123456789012345',z) + 'ABCDEFGH' + + Only need to change byte order if sending/receiving from + alternative endian implementation + + >>> z = 'ea0c3d7c1c22557f'.decode('hex') + >>> xtea_decrypt('0123456789012345',z,endian="<") + 'ABCDEFGH' + + """ + v0, v1 = struct.unpack(endian+"2L", block) + k = struct.unpack(endian+"4L", key) + v0, v1 = xtea_decryptQuad(v0, v1, k, rounds) + return struct.pack(endian+"2L", v0, v1) + +def _xtea_decryptQuad(v0, v1, key, rounds=32, delta=0x9e3779b9L, mask=0xffffffffL): + """Decrypt a quad + + @param v0: value 0 + @type v0: int (unsigned long 32) + @param v1: value 1 + @type v1: int (unsigned long 32) + @param key: decryption key + @type key: list[4] int + @param rounds: rounds + @type rounds: int + @return: decrypted values + @rtype: list[2] int + """ + sum = (delta * rounds) & mask + for round in range(rounds): + v1 = (v1 - (((v0<<4 ^ v0>>5) + v0) ^ (sum + key[sum>>11 & 3]))) & mask + sum = (sum - delta) & mask + v0 = (v0 - (((v1<<4 ^ v1>>5) + v1) ^ (sum + key[sum & 3]))) & mask + return v0, v1 + +# Try to replace core functions with optimized versions +try: + #raise ImportError + from _xtea import encipher as xtea_encryptQuad + from _xtea import decipher as xtea_decryptQuad +except ImportError, msg: + xtea_encryptQuad = _xtea_encryptQuad + xtea_decryptQuad = _xtea_decryptQuad + +if __name__ == "__main__": + import doctest + doctest.testmod() Property changes on: xtea/trunk/src/xtea/xtea.py ___________________________________________________________________ Name: svn:keywords + 'Id Revision' Name: svn:eol-style + native Modified: xtea/trunk/src/xtea.egg-info/SOURCES.txt =================================================================== --- xtea/trunk/src/xtea.egg-info/SOURCES.txt 2007-02-27 16:22:39 UTC (rev 210) +++ xtea/trunk/src/xtea.egg-info/SOURCES.txt 2007-02-28 01:42:12 UTC (rev 211) @@ -7,9 +7,13 @@ ez_setup.py setup.py version.txt -src/xtea.py +src/xtea/__init__.py +src/xtea/_xtea.c +src/xtea/tests.py +src/xtea/xtea.py src/xtea.egg-info/PKG-INFO src/xtea.egg-info/SOURCES.txt src/xtea.egg-info/dependency_links.txt +src/xtea.egg-info/native_libs.txt src/xtea.egg-info/top_level.txt src/xtea.egg-info/zip-safe Added: xtea/trunk/src/xtea.egg-info/native_libs.txt =================================================================== --- xtea/trunk/src/xtea.egg-info/native_libs.txt (rev 0) +++ xtea/trunk/src/xtea.egg-info/native_libs.txt 2007-02-28 01:42:12 UTC (rev 211) @@ -0,0 +1 @@ +xtea/_xtea.so Property changes on: xtea/trunk/src/xtea.egg-info/native_libs.txt ___________________________________________________________________ Name: svn:keywords + 'Id Revision' Name: svn:eol-style + native Deleted: xtea/trunk/src/xtea.py =================================================================== --- xtea/trunk/src/xtea.py 2007-02-27 16:22:39 UTC (rev 210) +++ xtea/trunk/src/xtea.py 2007-02-28 01:42:12 UTC (rev 211) @@ -1,128 +0,0 @@ -""" -XTEA Block Encryption Algorithm - -Author: Paul Chakravarti (paul_dot_chakravarti_at_mac_dot_com) -License: Public Domain - -This module provides a Python implementation of the XTEA block encryption -algorithm (http://www.cix.co.uk/~klockstone/xtea.pdf). - -The module implements the basic XTEA block encryption algortithm -(`xtea_encrypt`/`xtea_decrypt`) and also provides a higher level `crypt` -function which symmetrically encrypts/decrypts a variable length string using -XTEA in OFB mode as a key generator. The `crypt` function does not use -`xtea_decrypt` which is provided for completeness only (but can be used -to support other stream modes - eg CBC/CFB). - -This module is intended to provide a simple 'privacy-grade' Python encryption -algorithm with no external dependencies. The implementation is relatively slow -and is best suited to small volumes of data. Note that the XTEA algorithm has -not been subjected to extensive analysis (though is believed to be relatively -secure - see http://en.wikipedia.org/wiki/XTEA). For applications requiring -'real' security please use a known and well tested algorithm/implementation. - -The security of the algorithm is entirely based on quality (entropy) and -secrecy of the key. You should generate the key from a known random source and -exchange using a trusted mechanism. In addition, you should always use a random -IV to seed the key generator (the IV is not sensitive and does not need to be -exchanged securely) - - >>> import os - >>> iv = 'ABCDEFGH' - >>> z = crypt('0123456789012345','Hello There',iv) - >>> z.encode('hex') - 'fe196d0a40d6c222b9eff3' - >>> crypt('0123456789012345',z,iv) - 'Hello There' - -""" - -import struct - -def crypt(key,data,iv='\00\00\00\00\00\00\00\00',n=32): - """ - Encrypt/decrypt variable length string using XTEA cypher as - key generator (OFB mode) - * key = 128 bit (16 char) - * iv = 64 bit (8 char) - * data = string (any length) - - >>> import os - >>> key = os.urandom(16) - >>> iv = os.urandom(8) - >>> data = os.urandom(10000) - >>> z = crypt(key,data,iv) - >>> crypt(key,z,iv) == data - True - - """ - def keygen(key,iv,n): - while True: - iv = xtea_encrypt(key,iv,n) - for k in iv: - yield ord(k) - xor = [ chr(x^y) for (x,y) in zip(map(ord,data),keygen(key,iv,n)) ] - return "".join(xor) - -def xtea_encrypt(key,block,n=32,endian="!"): - """ - Encrypt 64 bit data block using XTEA block cypher - * key = 128 bit (16 char) - * block = 64 bit (8 char) - * n = rounds (default 32) - * endian = byte order (see 'struct' doc - default big/network) - - >>> z = xtea_encrypt('0123456789012345','ABCDEFGH') - >>> z.encode('hex') - 'b67c01662ff6964a' - - Only need to change byte order if sending/receiving from - alternative endian implementation - - >>> z = xtea_encrypt('0123456789012345','ABCDEFGH',endian="<") - >>> z.encode('hex') - 'ea0c3d7c1c22557f' - - """ - v0,v1 = struct.unpack(endian+"2L",block) - k = struct.unpack(endian+"4L",key) - sum,delta,mask = 0L,0x9e3779b9L,0xffffffffL - for round in range(n): - v0 = (v0 + (((v1<<4 ^ v1>>5) + v1) ^ (sum + k[sum & 3]))) & mask - sum = (sum + delta) & mask - v1 = (v1 + (((v0<<4 ^ v0>>5) + v0) ^ (sum + k[sum>>11 & 3]))) & mask - return struct.pack(endian+"2L",v0,v1) - -def xtea_decrypt(key,block,n=32,endian="!"): - """ - Decrypt 64 bit data block using XTEA block cypher - * key = 128 bit (16 char) - * block = 64 bit (8 char) - * n = rounds (default 32) - * endian = byte order (see 'struct' doc - default big/network) - - >>> z = 'b67c01662ff6964a'.decode('hex') - >>> xtea_decrypt('0123456789012345',z) - 'ABCDEFGH' - - Only need to change byte order if sending/receiving from - alternative endian implementation - - >>> z = 'ea0c3d7c1c22557f'.decode('hex') - >>> xtea_decrypt('0123456789012345',z,endian="<") - 'ABCDEFGH' - - """ - v0,v1 = struct.unpack(endian+"2L",block) - k = struct.unpack(endian+"4L",key) - delta,mask = 0x9e3779b9L,0xffffffffL - sum = (delta * n) & mask - for round in range(n): - v1 = (v1 - (((v0<<4 ^ v0>>5) + v0) ^ (sum + k[sum>>11 & 3]))) & mask - sum = (sum - delta) & mask - v0 = (v0 - (((v1<<4 ^ v1>>5) + v1) ^ (sum + k[sum & 3]))) & mask - return struct.pack(endian+"2L",v0,v1) - -if __name__ == "__main__": - import doctest - doctest.testmod() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |