Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sebest committed May 8, 2012
0 parents commit bf33177
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README
@@ -0,0 +1,8 @@
Python implementation of EdgeCast Token: ectoken_generate

Usage:
import ectoken

key = 'XXXXXXXXXXXXXX'
message = 'YYYYYYYYYY'
print ectoken.ectoken_generate(key, message)
84 changes: 84 additions & 0 deletions blowfish.c
@@ -0,0 +1,84 @@
/*
* gcc -Wl,-soname,_ecblowfish -fPIC -shared -o _ecblowfish.so blowfish.c -lssl
*/

#include <openssl/blowfish.h>
#include <string.h>

#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
l|=((unsigned long)(*((c)++)))<<16L, \
l|=((unsigned long)(*((c)++)))<< 8L, \
l|=((unsigned long)(*((c)++))))

#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
*((c)++)=(unsigned char)(((l)>>16L)&0xff), \
*((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
*((c)++)=(unsigned char)(((l) )&0xff))

void bfencrypt(unsigned char *keydata, int keydatalen, const unsigned char *in, unsigned char *out, unsigned int inlen);
static void cfb64_encrypt(const unsigned char* in, unsigned char* out, long length, BF_KEY* schedule, unsigned char* ivec, int *num, int encrypt);

/**
*
*
* @param in
* @param out
* @param length
* @param schedule
* @param ivec
* @param num
* @param encrypt
*/

static void cfb64_encrypt(const unsigned char* in, unsigned char* out, long length,
BF_KEY* schedule,
unsigned char* ivec,
int *num,
int encrypt)
{
register BF_LONG v0,v1,t;
register int n= *num;
register long l=length;
BF_LONG ti[2];
unsigned char *iv,c,cc;

iv=(unsigned char *)ivec;
while (l--)
{
if (n == 0)
{
n2l(iv,v0); ti[0]=v0;
n2l(iv,v1); ti[1]=v1;
BF_encrypt((BF_LONG*)ti,schedule);
iv=(unsigned char *)ivec;
t=ti[0]; l2n(t,iv);
t=ti[1]; l2n(t,iv);
iv=(unsigned char *)ivec;
}
c= *(in++)^iv[n];
*(out++)=c;
iv[n]=c;
n=(n+1)&0x07;
}
v0=v1=ti[0]=ti[1]=t=c=cc=0;
*num=n;
}

/**
*
* @param keydata
* @param keydatalen
* @param in
* @param out
* @param inlen
*/

void bfencrypt(unsigned char *keydata, int keydatalen, const unsigned char *in, unsigned char *out, unsigned int inlen) {
BF_KEY key;
unsigned char ivec[32];
int num=0;
/** set up for encryption **/
BF_set_key(&key, keydatalen, keydata);
memset(ivec, '\0', 32);
cfb64_encrypt(in, out, inlen, &key, ivec, &num, BF_ENCRYPT);
}
10 changes: 10 additions & 0 deletions ectoken.py
@@ -0,0 +1,10 @@
import os.path as osp
from ctypes import CDLL, create_string_buffer, byref

bf = CDLL(osp.join(osp.dirname(__file__), '_ecblowfish.so'))

def ectoken_generate(key, string):
string = 'ec_secure=%03d&%s' % (len(string) + 14, string)
output = create_string_buffer(1024)
bf.bfencrypt(key, len(key), string, byref(output), len(string))
return output.value.encode('hex_codec')
17 changes: 17 additions & 0 deletions setup.py
@@ -0,0 +1,17 @@
name, version = 'ectoken', '0.1'

from distutils.core import setup, Extension
from distutils.sysconfig import get_python_lib

blowfish = Extension(
name = '_ecblowfish',
sources = ['blowfish.c'],
libraries=['ssl'],
)

setup(
name = name,
version = version,
py_modules=['ectoken'],
ext_modules = [blowfish],
)

0 comments on commit bf33177

Please sign in to comment.