hawq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cjcjame...@apache.org
Subject [59/80] [abbrv] incubator-hawq git commit: HAWQ-863. Add python module pycrypto
Date Wed, 29 Jun 2016 01:23:52 GMT
http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/MD2.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/MD2.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/MD2.c
new file mode 100644
index 0000000..aa53c59
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/MD2.c
@@ -0,0 +1,118 @@
+
+/*
+ *  md2.c : MD2 hash algorithm.
+ *
+ * Part of the Python Cryptography Toolkit
+ *
+ * Distribute and use freely; there are no restrictions on further 
+ * dissemination and usage except those imposed by the laws of your 
+ * country of residence.
+ *
+ */
+  
+
+#include <string.h>
+#include "Python.h"
+
+#define MODULE_NAME MD2
+#define DIGEST_SIZE 16
+
+typedef unsigned char U8;
+typedef unsigned int U32;
+
+typedef struct {
+	U8 C[16], X[48];
+	int count;
+	U8 buf[16];
+} hash_state;
+
+static void hash_init (hash_state *ptr)
+{
+	memset(ptr->X, 0, 48);
+	memset(ptr->C, 0, 16);
+	ptr->count=0;
+}
+
+static U8 S[256] = {
+	41, 46, 67, 201, 162, 216, 124, 1, 61, 54, 84, 161, 236, 240, 6,
+	19, 98, 167, 5, 243, 192, 199, 115, 140, 152, 147, 43, 217, 188,
+	76, 130, 202, 30, 155, 87, 60, 253, 212, 224, 22, 103, 66, 111, 24,
+	138, 23, 229, 18, 190, 78, 196, 214, 218, 158, 222, 73, 160, 251,
+	245, 142, 187, 47, 238, 122, 169, 104, 121, 145, 21, 178, 7, 63,
+	148, 194, 16, 137, 11, 34, 95, 33, 128, 127, 93, 154, 90, 144, 50,
+	39, 53, 62, 204, 231, 191, 247, 151, 3, 255, 25, 48, 179, 72, 165,
+	181, 209, 215, 94, 146, 42, 172, 86, 170, 198, 79, 184, 56, 210,
+	150, 164, 125, 182, 118, 252, 107, 226, 156, 116, 4, 241, 69, 157,
+	112, 89, 100, 113, 135, 32, 134, 91, 207, 101, 230, 45, 168, 2, 27,
+	96, 37, 173, 174, 176, 185, 246, 28, 70, 97, 105, 52, 64, 126, 15,
+	85, 71, 163, 35, 221, 81, 175, 58, 195, 92, 249, 206, 186, 197,
+	234, 38, 44, 83, 13, 110, 133, 40, 132, 9, 211, 223, 205, 244, 65,
+	129, 77, 82, 106, 220, 55, 200, 108, 193, 171, 250, 36, 225, 123,
+	8, 12, 189, 177, 74, 120, 136, 149, 139, 227, 99, 232, 109, 233,
+	203, 213, 254, 59, 0, 29, 57, 242, 239, 183, 14, 102, 88, 208, 228,
+	166, 119, 114, 248, 235, 117, 75, 10, 49, 68, 80, 180, 143, 237,
+	31, 26, 219, 153, 141, 51, 159, 17, 131, 20
+};
+
+static void
+hash_copy(hash_state *src, hash_state *dest)
+{
+	dest->count=src->count;  
+	memcpy(dest->buf, src->buf, dest->count);
+	memcpy(dest->X, src->X, 48);
+	memcpy(dest->C, src->C, 16);
+}
+
+
+static void hash_update (hash_state *self, const U8 *buf, U32 len)
+{
+	U32 L;
+	while (len) 
+	{
+		L=(16-self->count) < len ? (16-self->count) : len;
+		memcpy(self->buf+self->count, buf, L);
+		self->count+=L;
+		buf+=L;
+		len-=L;
+		if (self->count==16) 
+		{
+			U8 t;
+			int i,j;
+	  
+			self->count=0;
+			memcpy(self->X+16, self->buf, 16);
+			t=self->C[15];
+			for(i=0; i<16; i++)
+			{
+				self->X[32+i]=self->X[16+i]^self->X[i];
+				t=self->C[i]^=S[self->buf[i]^t];
+			}
+	  
+			t=0;
+			for(i=0; i<18; i++)
+			{
+				for(j=0; j<48; j++)
+					t=self->X[j]^=S[t];
+				t=(t+i) & 0xFF;
+			}
+		}
+	}
+}
+
+static PyObject *
+hash_digest (const hash_state *self)
+{
+	U8 padding[16];
+	U32 padlen;
+	hash_state temp;
+	int i;
+  
+	memcpy(&temp, self, sizeof(hash_state));
+	padlen= 16-self->count;
+	for(i=0; i<padlen; i++) padding[i]=padlen;
+	hash_update(&temp, padding, padlen);
+	hash_update(&temp, temp.C, 16);
+	return PyString_FromStringAndSize(temp.X, 16);
+}
+
+#include "hash_template.c"

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/MD4.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/MD4.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/MD4.c
new file mode 100644
index 0000000..6f7fe54
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/MD4.c
@@ -0,0 +1,203 @@
+
+/*
+ *  md4.c : MD4 hash algorithm.
+ *
+ * Part of the Python Cryptography Toolkit
+ *
+ * Distribute and use freely; there are no restrictions on further 
+ * dissemination and usage except those imposed by the laws of your 
+ * country of residence.
+ *
+ */
+  
+
+#include <string.h>
+#include <Python.h>
+
+#define MODULE_NAME MD4
+#define DIGEST_SIZE 16
+
+typedef unsigned int U32;
+typedef unsigned char U8;
+#define U32_MAX (U32)4294967295
+
+typedef struct {
+	U32 A,B,C,D, count;
+	U32 len1, len2;
+	U8 buf[64];
+} hash_state;
+
+#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
+#define H(x, y, z) ((x) ^ (y) ^ (z))
+
+/* ROTATE_LEFT rotates x left n bits */
+#define ROL(x, n) (((x) << n) | ((x) >> (32-n) ))
+
+static void 
+hash_init (hash_state *ptr)
+{
+	ptr->A=(U32)0x67452301;
+	ptr->B=(U32)0xefcdab89;
+	ptr->C=(U32)0x98badcfe;
+	ptr->D=(U32)0x10325476;
+	ptr->count=ptr->len1=ptr->len2=0;
+}
+
+static void
+hash_copy(hash_state *src, hash_state *dest)
+{
+	dest->len1=src->len1;
+	dest->len2=src->len2;
+	dest->A=src->A;
+	dest->B=src->B;
+	dest->C=src->C;
+	dest->D=src->D;
+	dest->count=src->count;  
+	memcpy(dest->buf, src->buf, dest->count);
+}
+
+static void 
+hash_update (hash_state *self, const U8 *buf, U32 len)
+{
+	U32 L;
+
+	if ((self->len1+(len<<3))<self->len1)
+	{
+		self->len2++;
+	}
+	self->len1+=len<< 3;
+	self->len2+=len>>29;
+	while (len>0) 
+	{
+		L=(64-self->count) < len ? (64-self->count) : len;
+		memcpy(self->buf+self->count, buf, L);
+		self->count+=L;
+		buf+=L;
+		len-=L;
+		if (self->count==64) 
+		{
+			U32 X[16], A, B, C, D;
+			int i,j;
+			self->count=0;
+			for(i=j=0; j<16; i+=4, j++) 
+				X[j]=((U32)self->buf[i]       + ((U32)self->buf[i+1]<<8) +
+				      ((U32)self->buf[i+2]<<16) + ((U32)self->buf[i+3]<<24));
+
+
+			A=self->A; B=self->B; C=self->C; D=self->D;
+
+#define function(a,b,c,d,k,s) a=ROL(a+F(b,c,d)+X[k],s);	 
+			function(A,B,C,D, 0, 3);
+			function(D,A,B,C, 1, 7);
+			function(C,D,A,B, 2,11);
+			function(B,C,D,A, 3,19);
+			function(A,B,C,D, 4, 3);
+			function(D,A,B,C, 5, 7);
+			function(C,D,A,B, 6,11);
+			function(B,C,D,A, 7,19);
+			function(A,B,C,D, 8, 3);
+			function(D,A,B,C, 9, 7);
+			function(C,D,A,B,10,11);
+			function(B,C,D,A,11,19);
+			function(A,B,C,D,12, 3);
+			function(D,A,B,C,13, 7);
+			function(C,D,A,B,14,11);
+			function(B,C,D,A,15,19);
+
+#undef function	  
+#define function(a,b,c,d,k,s) a=ROL(a+G(b,c,d)+X[k]+(U32)0x5a827999,s);	 
+			function(A,B,C,D, 0, 3);
+			function(D,A,B,C, 4, 5);
+			function(C,D,A,B, 8, 9);
+			function(B,C,D,A,12,13);
+			function(A,B,C,D, 1, 3);
+			function(D,A,B,C, 5, 5);
+			function(C,D,A,B, 9, 9);
+			function(B,C,D,A,13,13);
+			function(A,B,C,D, 2, 3);
+			function(D,A,B,C, 6, 5);
+			function(C,D,A,B,10, 9);
+			function(B,C,D,A,14,13);
+			function(A,B,C,D, 3, 3);
+			function(D,A,B,C, 7, 5);
+			function(C,D,A,B,11, 9);
+			function(B,C,D,A,15,13);
+
+#undef function	 
+#define function(a,b,c,d,k,s) a=ROL(a+H(b,c,d)+X[k]+(U32)0x6ed9eba1,s);	 
+			function(A,B,C,D, 0, 3);
+			function(D,A,B,C, 8, 9);
+			function(C,D,A,B, 4,11);
+			function(B,C,D,A,12,15);
+			function(A,B,C,D, 2, 3);
+			function(D,A,B,C,10, 9);
+			function(C,D,A,B, 6,11);
+			function(B,C,D,A,14,15);
+			function(A,B,C,D, 1, 3);
+			function(D,A,B,C, 9, 9);
+			function(C,D,A,B, 5,11);
+			function(B,C,D,A,13,15);
+			function(A,B,C,D, 3, 3);
+			function(D,A,B,C,11, 9);
+			function(C,D,A,B, 7,11);
+			function(B,C,D,A,15,15);
+
+			self->A+=A; self->B+=B; self->C+=C; self->D+=D;
+		}
+	}
+}
+
+static PyObject *
+hash_digest (const hash_state *self)
+{
+	U8 digest[16];
+	static U8 s[8];
+	U32 padlen, oldlen1, oldlen2;
+	hash_state temp;
+	static U8 padding[64] = {
+		0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+	};
+
+	memcpy(&temp, self, sizeof(hash_state));
+	oldlen1=temp.len1; oldlen2=temp.len2;  /* Save current length */
+	padlen= (56<=self->count) ? 56-self->count+64: 56-self->count;
+	hash_update(&temp, padding, padlen);
+	s[0]= oldlen1       & 255;
+	s[1]=(oldlen1 >>  8) & 255;
+	s[2]=(oldlen1 >> 16) & 255;
+	s[3]=(oldlen1 >> 24) & 255;
+	s[4]= oldlen2        & 255;
+	s[5]=(oldlen2 >>  8) & 255;
+	s[6]=(oldlen2 >> 16) & 255;
+	s[7]=(oldlen2 >> 24) & 255;
+	hash_update(&temp, s, 8);
+  
+	digest[ 0]= temp.A        & 255;
+	digest[ 1]=(temp.A >>  8) & 255;
+	digest[ 2]=(temp.A >> 16) & 255;
+	digest[ 3]=(temp.A >> 24) & 255;
+	digest[ 4]= temp.B        & 255;
+	digest[ 5]=(temp.B >>  8) & 255;
+	digest[ 6]=(temp.B >> 16) & 255;
+	digest[ 7]=(temp.B >> 24) & 255;
+	digest[ 8]= temp.C        & 255;
+	digest[ 9]=(temp.C >>  8) & 255;
+	digest[10]=(temp.C >> 16) & 255;
+	digest[11]=(temp.C >> 24) & 255;
+	digest[12]= temp.D        & 255;
+	digest[13]=(temp.D >>  8) & 255;
+	digest[14]=(temp.D >> 16) & 255;
+	digest[15]=(temp.D >> 24) & 255;
+  
+	return PyString_FromStringAndSize(digest, 16);
+}
+
+#include "hash_template.c"

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/RC5.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/RC5.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/RC5.c
new file mode 100644
index 0000000..9d7bf4a
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/RC5.c
@@ -0,0 +1,212 @@
+/*
+ *  RC5.c : Implementation code for the RC5 block cipher
+ *
+ * Part of the Python Cryptography Toolkit
+ *
+ * Distribute and use freely; there are no restrictions on further 
+ * dissemination and usage except those imposed by the laws of your 
+ * country of residence.
+ *
+ */
+
+#include "Python.h"
+
+#define MODULE_NAME RC5
+#define BLOCK_SIZE 8
+#define KEY_SIZE 0
+#define PCT_RC5_MODULE          /* Define this to get RC5's additional 
+				   keywords */ 
+#define MAXTABLE 100		/* Maximum size of S-box table; changing this
+				   affects the maximum number of rounds
+				   possible. */
+typedef unsigned int U32;
+#define LEFT(v,x,y,w,MASK)  {U32 t1=(y) % (w), t2,t3=x;\
+		        t2=(w)-t1;\
+		        v= ( (t3 << t1) & MASK) | \
+   		           ( (t3 >> t2) & MASK);}
+#define RIGHT(v,x,y,w,MASK)  {U32 t1=(y) % (w), t2,t3=x;\
+		        t2=(w)-t1;\
+		        v= ( (t3 >> t1) & MASK) | \
+   		           ( (t3 << t2) & MASK);}
+
+
+
+typedef struct 
+{
+	int version;			/* Version number of algorithm */
+	int word_size;			/* Word size */
+	int rounds;			/* Number of rounds */
+	U32 S[MAXTABLE];
+	U32 mask;
+} block_state;
+
+static void
+block_init(block_state *self, unsigned char *key, int keylen)
+{
+	unsigned int P = 0, Q = 0;
+	int i;
+  
+	switch(self->word_size)
+	{
+	case(16):
+		P=0xb7e1; Q=0x9e37; self->mask=0xffff;
+		break;
+	case(32):
+		P=0xb7e15163; Q=0x9e3779b9; self->mask=0xffffffff;
+		break;
+	}
+	for(i=0; i<2*self->rounds+2; i++) self->S[i]=0;
+	{
+		unsigned int *L, A, B;
+		int u=self->word_size/8, num;
+		int j, t=2*(self->rounds+1), c=(keylen-1)/u;
+		if ((keylen-1) % u) c++;
+		L=malloc(sizeof(unsigned int)*c);
+		if (L==NULL) 
+		{
+			PyErr_SetString(PyExc_MemoryError,
+					"RC5: Can't allocate memory");
+		}
+		for(i=0; i<c; i++) L[i]=0;
+		for(i=keylen-1; 0<=i; i--) L[i/u]=(L[i/u]<<8)+key[i];
+		self->S[0]=P;
+		for(i=1; i<t; i++) self->S[i]=(self->S[i-1]+Q) & self->mask;
+		i=j=0;
+		A=B=0;
+		for(num = (t>c) ? 3*t : 3*c; 0<num; num--) 
+		{
+			LEFT(A, self->S[i]+A+B, 3, self->word_size, self->mask);
+			self->S[i]=A;
+			LEFT(B, L[j]+A+B, A+B, self->word_size, self->mask);
+			L[j]=B;
+			i=(i+1)%t;
+			j=(j+1)%c;
+		}
+		free(L);
+	}
+}
+
+static void RC5Encipher(block_state *self, U32 *Aptr, U32 *Bptr)
+{
+	int i;
+	register U32 A, B;
+
+	A=(*Aptr+self->S[0]) & self->mask;
+	B=(*Bptr+self->S[1]) & self->mask;
+
+	if (self->rounds)
+		for (i=2; i<=2*self->rounds; i+=2) 
+		{
+			LEFT(A,A^B,B,self->word_size,self->mask);
+			A += self->S[i];
+			LEFT(B,A^B,A,self->word_size,self->mask);
+			B += self->S[i+1];
+		}
+	*Aptr=A;
+	*Bptr=B;
+}
+
+static void RC5Decipher(block_state *self, unsigned int *Aptr, 
+			unsigned int *Bptr)
+{
+	int i;
+	U32 A, B;
+
+	A=*Aptr;
+	B=*Bptr;
+
+	if (self->rounds)
+		for (i=2*self->rounds; 2<=i; i-=2) 
+		{
+			RIGHT(B,B-self->S[i+1],A,self->word_size,self->mask);
+			B ^= A;
+			RIGHT(A,A-self->S[i],B,self->word_size,self->mask);
+			A ^= B;
+		}
+	A = (A-self->S[0]) & self->mask;
+	B = (B-self->S[1]) & self->mask;
+	if (self->word_size==32) 
+	{
+		*Aptr=A;
+		*Bptr=B;
+	}
+	else /* self->word_size==16 */
+	{
+		*Aptr=A;
+		*Bptr=B;
+	}
+}
+
+static void block_encrypt(block_state *self, unsigned char *in,
+			  unsigned char *out)
+{
+	U32 A,B;
+  
+	switch(self->word_size)
+	{
+	case (32):
+		A=in[0] | in[1]<<8 | in[2]<<16 | in[3]<<24;
+		B=in[4] | in[5]<<8 | in[6]<<16 | in[7]<<24;
+		RC5Encipher(self, &A, &B);
+		out[0]=A & 255; A>>=8;      
+		out[1]=A & 255; A>>=8;      
+		out[2]=A & 255; A>>=8;      
+		out[3]=A; 
+		out[4]=B & 255; B>>=8;      
+		out[5]=B & 255; B>>=8;      
+		out[6]=B & 255; B>>=8;      
+		out[7]=B; 
+		break;
+	case (16):
+		A=in[0] + in[1]*256;
+		B=in[2] + in[3]*256;
+		RC5Encipher(self, &A, &B);
+		out[0] = A & 255; out[1] = A>>8;
+		out[2] = B & 255; out[3] = B>>8;
+      
+		A=in[4] + in[5]*256;
+		B=in[6] + in[7]*256;
+		RC5Encipher(self, &A, &B);
+		out[4] = A & 255; out[5] = A>>8; 
+		out[6] = B & 255; out[7] = B>>8;
+		break;
+	}
+}
+
+static void block_decrypt(block_state *self, unsigned char *in,
+			  unsigned char *out)
+{
+	U32 A,B;
+  
+	switch(self->word_size)
+	{
+	case (32):
+		A=in[0] | in[1]<<8 | in[2]<<16 | in[3]<<24;
+		B=in[4] | in[5]<<8 | in[6]<<16 | in[7]<<24;
+		RC5Decipher(self, &A, &B);
+		out[0]=A & 255; A>>=8;      
+		out[1]=A & 255; A>>=8;      
+		out[2]=A & 255; A>>=8;      
+		out[3]=A; 
+		out[4]=B & 255; B>>=8;      
+		out[5]=B & 255; B>>=8;      
+		out[6]=B & 255; B>>=8;      
+		out[7]=B; 
+		break;
+	case (16):
+		A=in[0] + in[1]*256;
+		B=in[2] + in[3]*256;
+		RC5Decipher(self, &A, &B);
+		out[0] = A & 255; out[1] = A>>8;
+		out[2] = B & 255; out[3] = B>>8;
+      
+		A=in[4] + in[5]*256;
+		B=in[6] + in[7]*256;
+		RC5Decipher(self, &A, &B);
+		out[4] = A & 255; out[5] = A>>8;
+		out[6] = B & 255; out[7] = B>>8;
+		break;
+	}
+}
+
+#include "block_template.c"

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/RIPEMD.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/RIPEMD.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/RIPEMD.c
new file mode 100644
index 0000000..2d6f947
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/RIPEMD.c
@@ -0,0 +1,507 @@
+/* header files */
+
+/********************************************************************\
+ *  FILE:     rmd160.c
+ *  CONTENTS: A sample C-implementation of the RIPEMD-160 hash-function.
+ *  TARGET:   any computer with an ANSI C compiler
+ *  AUTHOR:   Antoon Bosselaers, Dept. Electrical Eng.-ESAT/COSIC
+ *  DATE:     1 March 1996       VERSION:  1.0
+ **********************************************************************
+ * Copyright (c) Katholieke Universiteit Leuven 1996, All Rights Reserved
+ * The Katholieke Universiteit Leuven makes no representations concerning
+ * either the merchantability of this software or the suitability of this
+ * software for any particular purpose. It is provided "as is" without
+ * express or implied warranty of any kind. These notices must be retained
+ * in any copies of any part of this documentation and/or software.
+\********************************************************************/
+
+#include <string.h>
+#include "Python.h"
+
+#ifdef MS_WIN32
+#include <winsock2.h>
+#else
+#include <sys/param.h>
+#include <netinet/in.h>
+#endif
+
+#include "Python.h"
+
+#define MODULE_NAME RIPEMD
+#define DIGEST_SIZE 20
+
+/********************************************************************/
+/* Macro definitions */
+
+/* ROL(x, n) cyclically rotates x over n bits to the left
+   x must be of an unsigned 32 bits type and 0 <= n < 32.
+*/
+#define ROL(x, n)        (((x) << (n)) | ((x) >> (32-(n))))
+
+/* The five basic RIPEMD-160 functions F1(), F2(), F3(), F4(), and F5()
+*/
+#define F1(x, y, z)        ((x) ^ (y) ^ (z))
+#define F2(x, y, z)        (((x) & (y)) | (~(x) & (z)))
+#define F3(x, y, z)        (((x) | ~(y)) ^ (z))
+#define F4(x, y, z)        (((x) & (z)) | ((y) & ~(z)))
+#define F5(x, y, z)        ((x) ^ ((y) | ~(z)))
+
+/* The ten basic RIPEMD-160 transformations FF1() through FFF5()
+*/
+#define FF1(a, b, c, d, e, x, s)        {\
+      (a) += F1((b), (c), (d)) + (x);\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FF2(a, b, c, d, e, x, s)        {\
+      (a) += F2((b), (c), (d)) + (x) + 0x5a827999UL;\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FF3(a, b, c, d, e, x, s)        {\
+      (a) += F3((b), (c), (d)) + (x) + 0x6ed9eba1UL;\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FF4(a, b, c, d, e, x, s)        {\
+      (a) += F4((b), (c), (d)) + (x) + 0x8f1bbcdcUL;\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FF5(a, b, c, d, e, x, s)        {\
+      (a) += F5((b), (c), (d)) + (x) + 0xa953fd4eUL;\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FFF1(a, b, c, d, e, x, s)        {\
+      (a) += F1((b), (c), (d)) + (x);\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FFF2(a, b, c, d, e, x, s)        {\
+      (a) += F2((b), (c), (d)) + (x) + 0x7a6d76e9UL;\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FFF3(a, b, c, d, e, x, s)        {\
+      (a) += F3((b), (c), (d)) + (x) + 0x6d703ef3UL;\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FFF4(a, b, c, d, e, x, s)        {\
+      (a) += F4((b), (c), (d)) + (x) + 0x5c4dd124UL;\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+#define FFF5(a, b, c, d, e, x, s)        {\
+      (a) += F5((b), (c), (d)) + (x) + 0x50a28be6UL;\
+      (a) = ROL((a), (s)) + (e);\
+      (c) = ROL((c), 10);\
+   }
+
+typedef    unsigned char        byte;   /* unsigned 8-bit integer */
+#ifdef __alpha__
+typedef    unsigned int        word;   /* unsigned 32-bit integer */
+#elif defined(__amd64__)
+typedef    uint32_t            word;   /* unsigned 32-bit integer */
+#else
+typedef    unsigned long        word;   /* unsigned 32-bit integer */
+#endif
+typedef unsigned char   BYTE;
+#define RMD_DATASIZE    64
+#define RMD_DIGESTSIZE  20
+#define RMDsize 160
+typedef struct {
+	word digest[ 5 ];            /* Message digest */
+	word countLo, countHi;       /* 64-bit bit count */
+	word data[ 16 ];         /* data buffer*/
+	int nbytes;
+} hash_state;
+
+static void MDinit(word *MDbuf);
+static void MDcompress(word *MDbuf, word *X);
+static void MDfinish(hash_state *self);
+
+/********************************************************************/
+
+static void hash_init(hash_state *rmdInfo)
+/* Initialization of the 5-word MDbuf array to the magic
+   initialization constants
+ */
+{  
+	MDinit(rmdInfo->digest);
+	rmdInfo->countLo = rmdInfo->countHi =rmdInfo->nbytes =  0;
+}
+
+static void hash_update(hash_state *shsInfo,char *buffer, int count)
+{
+	word tmp;
+	int dataCount, i;
+	BYTE *p;
+
+	/* Update bitcount */
+	tmp = shsInfo->countLo;
+	if ( ( shsInfo->countLo = tmp + ( ( word ) count << 3 ) ) < tmp )
+		shsInfo->countHi++;             /* Carry from low to high */
+	shsInfo->countHi += count >> 29;
+
+	/* Get count of bytes already in data */
+	dataCount = ( int ) ( tmp >> 3 ) & 0x3F;
+
+	/* Handle any leading odd-sized chunks */
+	if(dataCount)
+        {
+		p = ( BYTE * ) shsInfo->data + dataCount;
+
+		dataCount = RMD_DATASIZE - dataCount;
+		if( count < dataCount )
+		{
+			memcpy(p, buffer, count);
+			return;
+		}
+		memcpy(p, buffer, dataCount);
+		for(i=0; i<16; i++)
+		{
+			long t = htonl(shsInfo->data[i]);
+			t = ( ((t>>24) & 0xff)  + 
+			      (((t>>16) & 0xff)<<8) + 
+			      (((t>> 8) & 0xff)<<16) + 
+			      (((t    ) & 0xff)<<24) );
+			shsInfo->data[i] = t;
+		}
+		MDcompress(shsInfo->digest,shsInfo->data);
+		buffer += dataCount;
+		count -= dataCount;
+        }
+
+	/* Process data in RMD_DATASIZE chunks */
+	while( count >= RMD_DATASIZE )
+        {
+		memcpy( shsInfo->data, buffer, RMD_DATASIZE );
+		for(i=0; i<16; i++)
+		{
+			long t = htonl(shsInfo->data[i]);
+			t = ( ((t>>24) & 0xff)  + 
+			      (((t>>16) & 0xff)<<8) + 
+			      (((t>> 8) & 0xff)<<16) + 
+			      (((t    ) & 0xff)<<24) );
+			shsInfo->data[i] = t;
+		}
+		MDcompress(shsInfo->digest,shsInfo->data);
+		buffer += RMD_DATASIZE;
+		count -= RMD_DATASIZE;
+        }
+
+	/* Handle any remaining bytes of data. */
+	memcpy(shsInfo->data, buffer, count);
+}
+
+static PyObject *hash_digest(hash_state *self)
+{
+	hash_state temp;
+	int i;
+	byte   hashcode[RMDsize/8]; /* final hash-value             */
+  
+	temp.countLo=self->countLo;
+	temp.countHi=self->countHi;
+	for(i=0; i<5; i++) temp.digest[i]=self->digest[i];
+	for(i=0; i<16; i++) temp.data[i]=self->data[i];
+
+	MDfinish(&temp);
+	/* Convert word to a string of bytes using a Little-endian convention */
+	for (i=0; i<RMDsize/8; i+=4) {
+		hashcode[i]   =  temp.digest[i>>2];
+		hashcode[i+1] = (temp.digest[i>>2] >>  8);
+		hashcode[i+2] = (temp.digest[i>>2] >> 16);
+		hashcode[i+3] = (temp.digest[i>>2] >> 24);
+	}
+	return PyString_FromStringAndSize(hashcode, RMD_DIGESTSIZE);
+}
+
+static void hash_copy(hash_state *src,hash_state *dest)
+{
+	int i;
+
+	dest->countLo=src->countLo;
+	dest->countHi=src->countHi;
+	for(i=0; i<5; i++) dest->digest[i]=src->digest[i];
+	for(i=0; i<16; i++) dest->data[i]=src->data[i];
+}
+/********************************************************************/
+static void MDinit(word *MDbuf)
+/* Initialization of the 5-word MDbuf array to the magic
+   initialization constants
+ */
+{
+	MDbuf[0] = 0x67452301UL;
+	MDbuf[1] = 0xefcdab89UL;
+	MDbuf[2] = 0x98badcfeUL;
+	MDbuf[3] = 0x10325476UL;
+	MDbuf[4] = 0xc3d2e1f0UL;
+}
+
+/********************************************************************/
+static void MDcompress(word *MDbuf, word *X)
+/* The compression function is called for every complete 64-byte
+   message block. The 5-word internal state MDbuf is updated using
+   message words X[0] through X[15]. The conversion from a string
+   of 64 bytes to an array of 16 words using a Little-endian
+   convention is the responsibility of the calling function.
+*/
+{
+	/* make two copies of the old state */
+	word aa = MDbuf[0],  bb = MDbuf[1],  cc = MDbuf[2],
+		dd = MDbuf[3],  ee = MDbuf[4];
+	word aaa = aa, bbb = bb, ccc = cc, ddd = dd, eee = ee;
+
+	/*{printf("\nWords: ");
+	  for(i=0; i<16; i++) printf("%x ", X[i]);
+	  printf("\n");}
+	  printf("before compress: %x %x %x %x %x\n",
+	  aa, bb, cc, dd, ee);*/
+
+	/* round 1 */
+	FF1(aa, bb, cc, dd, ee, X[ 0], 11);
+	FF1(ee, aa, bb, cc, dd, X[ 1], 14);
+	FF1(dd, ee, aa, bb, cc, X[ 2], 15);
+	FF1(cc, dd, ee, aa, bb, X[ 3], 12);
+	FF1(bb, cc, dd, ee, aa, X[ 4],  5);
+	FF1(aa, bb, cc, dd, ee, X[ 5],  8);
+	FF1(ee, aa, bb, cc, dd, X[ 6],  7);
+	FF1(dd, ee, aa, bb, cc, X[ 7],  9);
+	FF1(cc, dd, ee, aa, bb, X[ 8], 11);
+	FF1(bb, cc, dd, ee, aa, X[ 9], 13);
+	FF1(aa, bb, cc, dd, ee, X[10], 14);
+	FF1(ee, aa, bb, cc, dd, X[11], 15);
+	FF1(dd, ee, aa, bb, cc, X[12],  6);
+	FF1(cc, dd, ee, aa, bb, X[13],  7);
+	FF1(bb, cc, dd, ee, aa, X[14],  9);
+	FF1(aa, bb, cc, dd, ee, X[15],  8);
+
+	/* round 2 */
+	FF2(ee, aa, bb, cc, dd, X[ 7],  7);
+	FF2(dd, ee, aa, bb, cc, X[ 4],  6);
+	FF2(cc, dd, ee, aa, bb, X[13],  8);
+	FF2(bb, cc, dd, ee, aa, X[ 1], 13);
+	FF2(aa, bb, cc, dd, ee, X[10], 11);
+	FF2(ee, aa, bb, cc, dd, X[ 6],  9);
+	FF2(dd, ee, aa, bb, cc, X[15],  7);
+	FF2(cc, dd, ee, aa, bb, X[ 3], 15);
+	FF2(bb, cc, dd, ee, aa, X[12],  7);
+	FF2(aa, bb, cc, dd, ee, X[ 0], 12);
+	FF2(ee, aa, bb, cc, dd, X[ 9], 15);
+	FF2(dd, ee, aa, bb, cc, X[ 5],  9);
+	FF2(cc, dd, ee, aa, bb, X[ 2], 11);
+	FF2(bb, cc, dd, ee, aa, X[14],  7);
+	FF2(aa, bb, cc, dd, ee, X[11], 13);
+	FF2(ee, aa, bb, cc, dd, X[ 8], 12);
+
+	/* round 3 */
+	FF3(dd, ee, aa, bb, cc, X[ 3], 11);
+	FF3(cc, dd, ee, aa, bb, X[10], 13);
+	FF3(bb, cc, dd, ee, aa, X[14],  6);
+	FF3(aa, bb, cc, dd, ee, X[ 4],  7);
+	FF3(ee, aa, bb, cc, dd, X[ 9], 14);
+	FF3(dd, ee, aa, bb, cc, X[15],  9);
+	FF3(cc, dd, ee, aa, bb, X[ 8], 13);
+	FF3(bb, cc, dd, ee, aa, X[ 1], 15);
+	FF3(aa, bb, cc, dd, ee, X[ 2], 14);
+	FF3(ee, aa, bb, cc, dd, X[ 7],  8);
+	FF3(dd, ee, aa, bb, cc, X[ 0], 13);
+	FF3(cc, dd, ee, aa, bb, X[ 6],  6);
+	FF3(bb, cc, dd, ee, aa, X[13],  5);
+	FF3(aa, bb, cc, dd, ee, X[11], 12);
+	FF3(ee, aa, bb, cc, dd, X[ 5],  7);
+	FF3(dd, ee, aa, bb, cc, X[12],  5);
+
+	/* round 4 */
+	FF4(cc, dd, ee, aa, bb, X[ 1], 11);
+	FF4(bb, cc, dd, ee, aa, X[ 9], 12);
+	FF4(aa, bb, cc, dd, ee, X[11], 14);
+	FF4(ee, aa, bb, cc, dd, X[10], 15);
+	FF4(dd, ee, aa, bb, cc, X[ 0], 14);
+	FF4(cc, dd, ee, aa, bb, X[ 8], 15);
+	FF4(bb, cc, dd, ee, aa, X[12],  9);
+	FF4(aa, bb, cc, dd, ee, X[ 4],  8);
+	FF4(ee, aa, bb, cc, dd, X[13],  9);
+	FF4(dd, ee, aa, bb, cc, X[ 3], 14);
+	FF4(cc, dd, ee, aa, bb, X[ 7],  5);
+	FF4(bb, cc, dd, ee, aa, X[15],  6);
+	FF4(aa, bb, cc, dd, ee, X[14],  8);
+	FF4(ee, aa, bb, cc, dd, X[ 5],  6);
+	FF4(dd, ee, aa, bb, cc, X[ 6],  5);
+	FF4(cc, dd, ee, aa, bb, X[ 2], 12);
+
+	/* round 5 */
+	FF5(bb, cc, dd, ee, aa, X[ 4],  9);
+	FF5(aa, bb, cc, dd, ee, X[ 0], 15);
+	FF5(ee, aa, bb, cc, dd, X[ 5],  5);
+	FF5(dd, ee, aa, bb, cc, X[ 9], 11);
+	FF5(cc, dd, ee, aa, bb, X[ 7],  6);
+	FF5(bb, cc, dd, ee, aa, X[12],  8);
+	FF5(aa, bb, cc, dd, ee, X[ 2], 13);
+	FF5(ee, aa, bb, cc, dd, X[10], 12);
+	FF5(dd, ee, aa, bb, cc, X[14],  5);
+	FF5(cc, dd, ee, aa, bb, X[ 1], 12);
+	FF5(bb, cc, dd, ee, aa, X[ 3], 13);
+	FF5(aa, bb, cc, dd, ee, X[ 8], 14);
+	FF5(ee, aa, bb, cc, dd, X[11], 11);
+	FF5(dd, ee, aa, bb, cc, X[ 6],  8);
+	FF5(cc, dd, ee, aa, bb, X[15],  5);
+	FF5(bb, cc, dd, ee, aa, X[13],  6);
+
+	/* parallel round 1 */
+	FFF5(aaa, bbb, ccc, ddd, eee, X[ 5],  8);
+	FFF5(eee, aaa, bbb, ccc, ddd, X[14],  9);
+	FFF5(ddd, eee, aaa, bbb, ccc, X[ 7],  9);
+	FFF5(ccc, ddd, eee, aaa, bbb, X[ 0], 11);
+	FFF5(bbb, ccc, ddd, eee, aaa, X[ 9], 13);
+	FFF5(aaa, bbb, ccc, ddd, eee, X[ 2], 15);
+	FFF5(eee, aaa, bbb, ccc, ddd, X[11], 15);
+	FFF5(ddd, eee, aaa, bbb, ccc, X[ 4],  5);
+	FFF5(ccc, ddd, eee, aaa, bbb, X[13],  7);
+	FFF5(bbb, ccc, ddd, eee, aaa, X[ 6],  7);
+	FFF5(aaa, bbb, ccc, ddd, eee, X[15],  8);
+	FFF5(eee, aaa, bbb, ccc, ddd, X[ 8], 11);
+	FFF5(ddd, eee, aaa, bbb, ccc, X[ 1], 14);
+	FFF5(ccc, ddd, eee, aaa, bbb, X[10], 14);
+	FFF5(bbb, ccc, ddd, eee, aaa, X[ 3], 12);
+	FFF5(aaa, bbb, ccc, ddd, eee, X[12],  6);
+
+	/* parallel round 2 */
+	FFF4(eee, aaa, bbb, ccc, ddd, X[ 6],  9);
+	FFF4(ddd, eee, aaa, bbb, ccc, X[11], 13);
+	FFF4(ccc, ddd, eee, aaa, bbb, X[ 3], 15);
+	FFF4(bbb, ccc, ddd, eee, aaa, X[ 7],  7);
+	FFF4(aaa, bbb, ccc, ddd, eee, X[ 0], 12);
+	FFF4(eee, aaa, bbb, ccc, ddd, X[13],  8);
+	FFF4(ddd, eee, aaa, bbb, ccc, X[ 5],  9);
+	FFF4(ccc, ddd, eee, aaa, bbb, X[10], 11);
+	FFF4(bbb, ccc, ddd, eee, aaa, X[14],  7);
+	FFF4(aaa, bbb, ccc, ddd, eee, X[15],  7);
+	FFF4(eee, aaa, bbb, ccc, ddd, X[ 8], 12);
+	FFF4(ddd, eee, aaa, bbb, ccc, X[12],  7);
+	FFF4(ccc, ddd, eee, aaa, bbb, X[ 4],  6);
+	FFF4(bbb, ccc, ddd, eee, aaa, X[ 9], 15);
+	FFF4(aaa, bbb, ccc, ddd, eee, X[ 1], 13);
+	FFF4(eee, aaa, bbb, ccc, ddd, X[ 2], 11);
+
+	/* parallel round 3 */
+	FFF3(ddd, eee, aaa, bbb, ccc, X[15],  9);
+	FFF3(ccc, ddd, eee, aaa, bbb, X[ 5],  7);
+	FFF3(bbb, ccc, ddd, eee, aaa, X[ 1], 15);
+	FFF3(aaa, bbb, ccc, ddd, eee, X[ 3], 11);
+	FFF3(eee, aaa, bbb, ccc, ddd, X[ 7],  8);
+	FFF3(ddd, eee, aaa, bbb, ccc, X[14],  6);
+	FFF3(ccc, ddd, eee, aaa, bbb, X[ 6],  6);
+	FFF3(bbb, ccc, ddd, eee, aaa, X[ 9], 14);
+	FFF3(aaa, bbb, ccc, ddd, eee, X[11], 12);
+	FFF3(eee, aaa, bbb, ccc, ddd, X[ 8], 13);
+	FFF3(ddd, eee, aaa, bbb, ccc, X[12],  5);
+	FFF3(ccc, ddd, eee, aaa, bbb, X[ 2], 14);
+	FFF3(bbb, ccc, ddd, eee, aaa, X[10], 13);
+	FFF3(aaa, bbb, ccc, ddd, eee, X[ 0], 13);
+	FFF3(eee, aaa, bbb, ccc, ddd, X[ 4],  7);
+	FFF3(ddd, eee, aaa, bbb, ccc, X[13],  5);
+
+	/* parallel round 4 */
+	FFF2(ccc, ddd, eee, aaa, bbb, X[ 8], 15);
+	FFF2(bbb, ccc, ddd, eee, aaa, X[ 6],  5);
+	FFF2(aaa, bbb, ccc, ddd, eee, X[ 4],  8);
+	FFF2(eee, aaa, bbb, ccc, ddd, X[ 1], 11);
+	FFF2(ddd, eee, aaa, bbb, ccc, X[ 3], 14);
+	FFF2(ccc, ddd, eee, aaa, bbb, X[11], 14);
+	FFF2(bbb, ccc, ddd, eee, aaa, X[15],  6);
+	FFF2(aaa, bbb, ccc, ddd, eee, X[ 0], 14);
+	FFF2(eee, aaa, bbb, ccc, ddd, X[ 5],  6);
+	FFF2(ddd, eee, aaa, bbb, ccc, X[12],  9);
+	FFF2(ccc, ddd, eee, aaa, bbb, X[ 2], 12);
+	FFF2(bbb, ccc, ddd, eee, aaa, X[13],  9);
+	FFF2(aaa, bbb, ccc, ddd, eee, X[ 9], 12);
+	FFF2(eee, aaa, bbb, ccc, ddd, X[ 7],  5);
+	FFF2(ddd, eee, aaa, bbb, ccc, X[10], 15);
+	FFF2(ccc, ddd, eee, aaa, bbb, X[14],  8);
+
+	/* parallel round 5 */
+	FFF1(bbb, ccc, ddd, eee, aaa, X[12] ,  8);
+	FFF1(aaa, bbb, ccc, ddd, eee, X[15] ,  5);
+	FFF1(eee, aaa, bbb, ccc, ddd, X[10] , 12);
+	FFF1(ddd, eee, aaa, bbb, ccc, X[ 4] ,  9);
+	FFF1(ccc, ddd, eee, aaa, bbb, X[ 1] , 12);
+	FFF1(bbb, ccc, ddd, eee, aaa, X[ 5] ,  5);
+	FFF1(aaa, bbb, ccc, ddd, eee, X[ 8] , 14);
+	FFF1(eee, aaa, bbb, ccc, ddd, X[ 7] ,  6);
+	FFF1(ddd, eee, aaa, bbb, ccc, X[ 6] ,  8);
+	FFF1(ccc, ddd, eee, aaa, bbb, X[ 2] , 13);
+	FFF1(bbb, ccc, ddd, eee, aaa, X[13] ,  6);
+	FFF1(aaa, bbb, ccc, ddd, eee, X[14] ,  5);
+	FFF1(eee, aaa, bbb, ccc, ddd, X[ 0] , 15);
+	FFF1(ddd, eee, aaa, bbb, ccc, X[ 3] , 13);
+	FFF1(ccc, ddd, eee, aaa, bbb, X[ 9] , 11);
+	FFF1(bbb, ccc, ddd, eee, aaa, X[11] , 11);
+
+	/* combine results into new state */
+	ddd += cc + MDbuf[1];
+	MDbuf[1] = MDbuf[2] + dd + eee;
+	MDbuf[2] = MDbuf[3] + ee + aaa;
+	MDbuf[3] = MDbuf[4] + aa + bbb;
+	MDbuf[4] = MDbuf[0] + bb + ccc;
+	MDbuf[0] = ddd;
+	/*printf("after compress: %x %x %x %x %x\n",
+	  MDbuf[0], MDbuf[1], MDbuf[2], MDbuf[3], MDbuf[4]);*/
+}
+
+/********************************************************************/
+static void MDfinish( hash_state *shsInfo)
+/* The final value of the 5-word MDbuf array is calculated. 
+   lswlen and mswlen contain, respectively, the least and most significant
+   32 bits of the message bit length mod 2^64, and string is an incomplete
+   block containing the (lswlen mod 512) remaining message bits.
+   (In case the message is already a multiple of 512 bits, string
+   is not used.) The conversion of the 5-word final state MDbuf to
+   the 20-byte hash result using a Little-endian convention is the
+   responsibility of the calling function.
+*/
+{
+	word *MDbuf = shsInfo->digest;
+	byte *string = (byte *)shsInfo->data;
+	word lswlen = shsInfo->countLo ;
+	word mswlen = shsInfo->countHi ;
+/*   word lswlen = shsInfo->countLo << 3;*/
+/*   word mswlen = (shsInfo->countLo >>29)|(shsInfo->countHi <<3);*/
+
+	size_t i, length;
+	byte   mask;
+	word   X[16];
+
+	/* clear 16-word message block */
+	memset(X, 0, 16*sizeof(word));
+
+	/* copy (lswlen mod 512) bits from string into X */
+	length = ((lswlen&511)+7)/8; /* number of bytes */
+	mask = (lswlen&7) ? ((byte)1 << (lswlen&7)) - 1 : 0xff;
+	for (i=0; i<length; i++) {
+		/* byte i goes into word X[i div 4] at bit position 8*(i mod 4) */
+		if (i == length-1)
+			X[i>>2] ^= (word) (*string&mask) << (8*(i&3));
+		else
+			X[i>>2] ^= (word) *string++ << (8*(i&3));
+	}
+
+	/* append a single 1 */
+	X[(lswlen>>5)&15] ^= (word)1 << (8*((lswlen>>3)&3)+7-(lswlen&7));
+
+	if ((lswlen & 511) > 447) {
+		/* length doesn't fit in this block anymore.
+		   Compress, and put length in the next block */
+		MDcompress(MDbuf, X);
+		memset(X, 0, 16*sizeof(word));
+	}
+	/* append length in bits*/
+	X[14] = lswlen;
+	X[15] = mswlen;
+	MDcompress(MDbuf, X);
+}
+ 	
+
+#include "hash_template.c"

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/SHA256.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/SHA256.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/SHA256.c
new file mode 100644
index 0000000..6bd9df2
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/SHA256.c
@@ -0,0 +1,200 @@
+/*
+ * An implementation of the SHA-256 hash function, this is endian neutral
+ * so should work just about anywhere.
+ *
+ * This code works much like the MD5 code provided by RSA.  You sha_init()
+ * a "sha_state" then sha_process() the bytes you want and sha_done() to get
+ * the output.
+ *
+ * Revised Code:  Complies to SHA-256 standard now.
+ *
+ * Tom St Denis -- http://tomstdenis.home.dhs.org
+ * */
+#include "Python.h"
+#define MODULE_NAME SHA256
+#define DIGEST_SIZE 32
+
+typedef unsigned char U8;
+#ifdef __alpha__
+typedef    unsigned int        U32;
+#elif defined(__amd64__)
+#include <inttypes.h>
+typedef uint32_t U32;
+#else
+typedef unsigned int U32;
+#endif
+
+typedef struct {
+    U32 state[8], length, curlen;
+    unsigned char buf[64];
+}
+hash_state;
+
+/* the K array */
+static const U32 K[64] = {
+    0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
+    0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
+    0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
+    0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
+    0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
+    0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
+    0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
+    0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
+    0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
+    0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
+    0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
+    0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
+    0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
+};
+
+/* Various logical functions */
+#define Ch(x,y,z)    ((x & y) ^ (~x & z))
+#define Maj(x,y,z)  ((x & y) ^ (x & z) ^ (y & z))
+#define S(x, n)        (((x)>>((n)&31))|((x)<<(32-((n)&31))))
+#define R(x, n)        ((x)>>(n))
+#define Sigma0(x)    (S(x, 2) ^ S(x, 13) ^ S(x, 22))
+#define Sigma1(x)    (S(x, 6) ^ S(x, 11) ^ S(x, 25))
+#define Gamma0(x)    (S(x, 7) ^ S(x, 18) ^ R(x, 3))
+#define Gamma1(x)    (S(x, 17) ^ S(x, 19) ^ R(x, 10))
+
+/* compress 512-bits */
+static void sha_compress(hash_state * md)
+{
+    U32 S[8], W[64], t0, t1;
+    int i;
+
+    /* copy state into S */
+    for (i = 0; i < 8; i++)
+        S[i] = md->state[i];
+
+    /* copy the state into 512-bits into W[0..15] */
+    for (i = 0; i < 16; i++)
+        W[i] = (((U32) md->buf[(4 * i) + 0]) << 24) |
+            (((U32) md->buf[(4 * i) + 1]) << 16) |
+            (((U32) md->buf[(4 * i) + 2]) << 8) |
+            (((U32) md->buf[(4 * i) + 3]));
+
+    /* fill W[16..63] */
+    for (i = 16; i < 64; i++)
+        W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
+
+    /* Compress */
+    for (i = 0; i < 64; i++) {
+        t0 = S[7] + Sigma1(S[4]) + Ch(S[4], S[5], S[6]) + K[i] + W[i];
+        t1 = Sigma0(S[0]) + Maj(S[0], S[1], S[2]);
+        S[7] = S[6];
+        S[6] = S[5];
+        S[5] = S[4];
+        S[4] = S[3] + t0;
+        S[3] = S[2];
+        S[2] = S[1];
+        S[1] = S[0];
+        S[0] = t0 + t1;
+    }
+
+    /* feedback */
+    for (i = 0; i < 8; i++)
+        md->state[i] += S[i];
+}
+
+/* init the SHA state */
+void sha_init(hash_state * md)
+{
+    md->curlen = md->length = 0;
+    md->state[0] = 0x6A09E667UL;
+    md->state[1] = 0xBB67AE85UL;
+    md->state[2] = 0x3C6EF372UL;
+    md->state[3] = 0xA54FF53AUL;
+    md->state[4] = 0x510E527FUL;
+    md->state[5] = 0x9B05688CUL;
+    md->state[6] = 0x1F83D9ABUL;
+    md->state[7] = 0x5BE0CD19UL;
+}
+
+void sha_process(hash_state * md, unsigned char *buf, int len)
+{
+    while (len--) {
+        /* copy byte */
+        md->buf[md->curlen++] = *buf++;
+
+        /* is 64 bytes full? */
+        if (md->curlen == 64) {
+            sha_compress(md);
+            md->length += 512;
+            md->curlen = 0;
+        }
+    }
+}
+
+void sha_done(hash_state * md, unsigned char *hash)
+{
+    int i;
+
+    /* increase the length of the message */
+    md->length += md->curlen * 8;
+
+    /* append the '1' bit */
+    md->buf[md->curlen++] = 0x80;
+
+    /* if the length is currenlly above 56 bytes we append zeros
+                               * then compress.  Then we can fall back to padding zeros and length
+                               * encoding like normal.
+                             */
+    if (md->curlen >= 56) {
+        for (; md->curlen < 64;)
+            md->buf[md->curlen++] = 0;
+        sha_compress(md);
+        md->curlen = 0;
+    }
+
+    /* pad upto 56 bytes of zeroes */
+    for (; md->curlen < 56;)
+        md->buf[md->curlen++] = 0;
+
+    /* since all messages are under 2^32 bits we mark the top bits zero */
+    for (i = 56; i < 60; i++)
+        md->buf[i] = 0;
+
+    /* append length */
+    for (i = 60; i < 64; i++)
+        md->buf[i] = (md->length >> ((63 - i) * 8)) & 255;
+    sha_compress(md);
+
+    /* copy output */
+    for (i = 0; i < 32; i++)
+        hash[i] = (md->state[i >> 2] >> (((3 - i) & 3) << 3)) & 255;
+}
+
+// Done
+static void hash_init (hash_state *ptr)
+{
+	sha_init(ptr);
+}
+
+// Done
+static void 
+hash_update (hash_state *self, const U8 *buf, U32 len)
+{
+	sha_process(self,(unsigned char *)buf,len);
+}
+
+// Done
+static void
+hash_copy(hash_state *src, hash_state *dest)
+{
+	memcpy(dest,src,sizeof(hash_state));
+}
+
+// Done
+static PyObject *
+hash_digest (const hash_state *self)
+{
+	unsigned char digest[32];
+	hash_state temp;
+
+	hash_copy((hash_state*)self,&temp);
+	sha_done(&temp,digest);
+	return PyString_FromStringAndSize(digest, 32);
+}
+
+#include "hash_template.c"

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/XOR.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/XOR.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/XOR.c
new file mode 100644
index 0000000..f2c74af
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/XOR.c
@@ -0,0 +1,52 @@
+/*
+ *  xor.c : Source for the trivial cipher which XORs the message with the key.
+ *          The key can be up to 32 bytes long.
+ *
+ * Part of the Python Cryptography Toolkit
+ *
+ * Distribute and use freely; there are no restrictions on further 
+ * dissemination and usage except those imposed by the laws of your 
+ * country of residence.
+ *
+ */
+
+#define MODULE_NAME XOR
+#define BLOCK_SIZE 1
+#define KEY_SIZE 0
+
+typedef struct 
+{
+	unsigned char key[32];
+	int keylen, last_pos;
+} stream_state;
+
+static void
+stream_init(stream_state *self, unsigned char *key, int len)
+{
+	int i;
+  
+	if (32 <= len) len=32;
+	self->keylen = len;
+	self->last_pos = 0;
+
+	for(i=0; i<len; i++)
+	{
+		self->key[i] = key[i];
+	}
+}
+
+/* Encryption and decryption are symmetric */
+#define stream_decrypt stream_encrypt	
+
+static void stream_encrypt(stream_state *self, unsigned char *block, 
+			   int len)
+{
+	int i, j = self->last_pos;
+	for(i=0; i<len; i++, j=(j+1) % self->keylen)
+	{
+		block[i] ^= self->key[j];
+	}
+	self->last_pos = j;
+}
+
+#include "stream_template.c"

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/_dsa.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/_dsa.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/_dsa.c
new file mode 100755
index 0000000..4752f43
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/_dsa.c
@@ -0,0 +1,331 @@
+
+/*
+ *  _dsa.c: C implementation of the DSA algorithm.
+ *
+ * Part of the Python Cryptography Toolkit
+ *
+ * Distribute and use freely; there are no restrictions on further 
+ * dissemination and usage except those imposed by the laws of your 
+ * country of residence.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <Python.h>
+#include <longintrepr.h>				/* for conversions */
+#include <gmp.h>
+
+PyObject *_dsa_module;
+PyObject *_dsa_dict;
+
+void
+longObjToMPZ (mpz_t m, PyLongObject * p)
+{
+	int size, i;
+	mpz_t temp, temp2;
+	mpz_init (temp);
+	mpz_init (temp2);
+	if (p->ob_size > 0)
+		size = p->ob_size;
+	else
+		size = -p->ob_size;
+	for (i = 0; i < size; i++)
+		{
+			mpz_set_ui (temp, p->ob_digit[i]);
+			mpz_mul_2exp (temp2, temp, SHIFT * i);
+			mpz_add (m, m, temp2);
+		}
+	mpz_clear (temp);
+	mpz_clear (temp2);
+}
+
+PyObject *
+mpzToLongObj (mpz_t m)
+{
+	/* borrowed from gmpy */
+	int size = (mpz_sizeinbase (m, 2) + SHIFT - 1) / SHIFT;
+	int i;
+	mpz_t temp;
+	PyLongObject *l = _PyLong_New (size);
+	if (!l)
+		{
+			return NULL;
+		}
+	mpz_init_set (temp, m);
+	for (i = 0; i < size; i++)
+		{
+			l->ob_digit[i] = (digit) (mpz_get_ui (temp) & MASK);
+			mpz_fdiv_q_2exp (temp, temp, SHIFT);
+		}
+	i = size;
+	while ((i > 0) && (l->ob_digit[i - 1] == 0))
+		i--;
+	l->ob_size = i;
+	mpz_clear (temp);
+	return (PyObject *) l;
+}
+
+PyObject *dsaKey_new (PyObject *, PyObject *);
+
+static PyMethodDef _dsa__methods__[] = {
+	{"construct", dsaKey_new, METH_VARARGS},
+	{NULL, NULL}
+};
+
+typedef struct
+{
+	PyObject_HEAD mpz_t y;
+	mpz_t g;
+	mpz_t p;
+	mpz_t q;
+	mpz_t x;
+}
+dsaKey;
+
+static int
+dsaSign (dsaKey * key, mpz_t m, mpz_t k, mpz_t r, mpz_t s)
+{
+	mpz_t temp;
+	if (mpz_cmp_ui (k, 2) < 0 || mpz_cmp (k, key->q) >= 0)
+		{
+			return 1;
+		}
+	mpz_init (temp);
+	mpz_powm (r, key->g, k, key->p);
+	mpz_mod (r, r, key->q);
+	mpz_invert (s, k, key->q);
+	mpz_mul (temp, key->x, r);
+	mpz_add (temp, m, temp);
+	mpz_mul (s, s, temp);
+	mpz_mod (s, s, key->q);
+	mpz_clear (temp);
+	return 0;
+}
+
+static int
+dsaVerify (dsaKey * key, mpz_t m, mpz_t r, mpz_t s)
+{
+	int result;
+	mpz_t u1, u2, v1, v2, w;
+	if (mpz_cmp_ui (r, 0) <= 0 || mpz_cmp (r, key->q) >= 0 ||
+			mpz_cmp_ui (s, 0) <= 0 || mpz_cmp (s, key->q) >= 0)
+		return 0;
+	mpz_init (u1);
+	mpz_init (u2);
+	mpz_init (v1);
+	mpz_init (v2);
+	mpz_init (w);
+	mpz_invert (w, s, key->q);
+	mpz_mul (u1, m, w);
+	mpz_mod (u1, u1, key->q);
+	mpz_mul (u2, r, w);
+	mpz_mod (u2, u2, key->q);
+	mpz_powm (v1, key->g, u1, key->p);
+	mpz_powm (v2, key->y, u2, key->p);
+	mpz_mul (w, v1, v2);
+	mpz_mod (w, w, key->p);
+	mpz_mod (w, w, key->q);
+	if (mpz_cmp (r, w) == 0)
+		result = 1;
+	else
+		result = 0;
+	mpz_clear (u1);
+	mpz_clear (u2);
+	mpz_clear (v1);
+	mpz_clear (v2);
+	mpz_clear (w);
+	return result;
+}
+
+static void dsaKey_dealloc (dsaKey *);
+static PyObject *dsaKey_getattr (dsaKey *, char *);
+static PyObject *dsaKey__sign (dsaKey *, PyObject *);
+static PyObject *dsaKey__verify (dsaKey *, PyObject *);
+static PyObject *dsaKey_size (dsaKey *, PyObject *);
+static PyObject *dsaKey_hasprivate (dsaKey *, PyObject *);
+
+PyObject *dsaError;							/* raised on errors */
+
+static PyTypeObject dsaKeyType = {
+	PyObject_HEAD_INIT (NULL) 0,
+	"dsaKey",
+	sizeof (dsaKey),
+	0,
+	(destructor) dsaKey_dealloc,	/* dealloc */
+	0,														/* print */
+	(getattrfunc) dsaKey_getattr,	/* getattr */
+	0,														/* setattr */
+	0,														/* compare */
+	0,														/* repr */
+	0,														/* as_number */
+	0,														/* as_sequence */
+	0,														/* as_mapping */
+	0,														/* hash */
+	0,														/* call */
+};
+
+static PyMethodDef dsaKey__methods__[] = {
+	{"_sign", (PyCFunction) dsaKey__sign, METH_VARARGS, "Sign the given long."},
+	{"_verify", (PyCFunction) dsaKey__verify, METH_VARARGS,
+	 "Verify that the signature is valid."},
+	{"size", (PyCFunction) dsaKey_size, METH_VARARGS,
+	 "Return the number of bits that this key can handle."},
+	{"hasprivate", (PyCFunction) dsaKey_hasprivate, METH_VARARGS,
+	 "Return 1 or 0 if this key does/doesn't have a private key."},
+	{NULL, NULL, 0, NULL}
+};
+
+PyObject *
+dsaKey_new (PyObject * self, PyObject * args)
+{
+	PyLongObject *y = NULL, *g = NULL, *p = NULL, *q = NULL, *x = NULL;
+	dsaKey *key;
+	key = PyObject_New (dsaKey, &dsaKeyType);
+	mpz_init (key->y);
+	mpz_init (key->g);
+	mpz_init (key->p);
+	mpz_init (key->q);
+	mpz_init (key->x);
+	PyArg_ParseTuple (args, "O!O!O!O!|O!", &PyLong_Type, &y,
+										&PyLong_Type, &g,
+										&PyLong_Type, &p, &PyLong_Type, &q, &PyLong_Type, &x);
+	longObjToMPZ (key->y, y);
+	longObjToMPZ (key->g, g);
+	longObjToMPZ (key->p, p);
+	longObjToMPZ (key->q, q);
+	if (x)
+		{
+			longObjToMPZ (key->x, x);
+		}
+	/*Py_XDECREF(n);
+	   Py_XDECREF(e);
+	   Py_XDECREF(d);
+	   Py_XDECREF(p);
+	   Py_XDECREF(q); */
+	return (PyObject *) key;
+}
+
+static void
+dsaKey_dealloc (dsaKey * key)
+{
+	mpz_clear (key->y);
+	mpz_clear (key->g);
+	mpz_clear (key->p);
+	mpz_clear (key->q);
+	mpz_clear (key->x);
+	PyObject_Del (key);
+}
+
+static PyObject *
+dsaKey_getattr (dsaKey * key, char *attr)
+{
+	if (strcmp (attr, "y") == 0)
+		return mpzToLongObj (key->y);
+	else if (strcmp (attr, "g") == 0)
+		return mpzToLongObj (key->g);
+	else if (strcmp (attr, "p") == 0)
+		return mpzToLongObj (key->p);
+	else if (strcmp (attr, "q") == 0)
+		return mpzToLongObj (key->q);
+	else if (strcmp (attr, "x") == 0)
+		{
+			if (mpz_size (key->x) == 0)
+				{
+					PyErr_SetString (PyExc_AttributeError,
+													 "rsaKey instance has no attribute 'x'");
+					return NULL;
+				}
+			return mpzToLongObj (key->x);
+		}
+	else
+		{
+			return Py_FindMethod (dsaKey__methods__, (PyObject *) key, attr);
+		}
+}
+
+PyObject *
+dsaKey__sign (dsaKey * key, PyObject * args)
+{
+	PyObject *lm, *lk, *lr, *ls;
+	mpz_t m, k, r, s;
+	int result;
+	if (!(PyArg_ParseTuple (args, "O!O!", &PyLong_Type, &lm,
+													&PyLong_Type, &lk)))
+		{
+			return NULL;
+		}
+	mpz_init (m);
+	mpz_init (k);
+	mpz_init (r);
+	mpz_init (s);
+	longObjToMPZ (m, (PyLongObject *) lm);
+	longObjToMPZ (k, (PyLongObject *) lk);
+	result = dsaSign (key, m, k, r, s);
+	if (result == 1)
+		{
+			PyErr_SetString (dsaError, "K not between 2 and q");
+			return NULL;
+		}
+	lr = mpzToLongObj (r);
+	ls = mpzToLongObj (s);
+	mpz_clear (m);
+	mpz_clear (k);
+	mpz_clear (r);
+	mpz_clear (s);
+	return Py_BuildValue ("(NN)", lr, ls);
+}
+
+PyObject *
+dsaKey__verify (dsaKey * key, PyObject * args)
+{
+	PyObject *lm, *lr, *ls;
+	mpz_t m, r, s;
+	int result;
+	if (!(PyArg_ParseTuple (args, "O!O!O!", &PyLong_Type, &lm,
+													&PyLong_Type, &lr, &PyLong_Type, &ls)))
+		{
+			return NULL;
+		}
+	mpz_init (m);
+	mpz_init (r);
+	mpz_init (s);
+	longObjToMPZ (m, (PyLongObject *) lm);
+	longObjToMPZ (r, (PyLongObject *) lr);
+	longObjToMPZ (s, (PyLongObject *) ls);
+	result = dsaVerify (key, m, r, s);
+	mpz_clear (m);
+	mpz_clear (r);
+	mpz_clear (s);
+	return Py_BuildValue ("i", result);
+}
+
+PyObject *
+dsaKey_size (dsaKey * key, PyObject * args)
+{
+	if (!PyArg_ParseTuple (args, ""))
+		return NULL;
+	return Py_BuildValue ("i", mpz_sizeinbase (key->p, 2) - 1);
+}
+
+PyObject *
+dsaKey_hasprivate (dsaKey * key, PyObject * args)
+{
+	if (!PyArg_ParseTuple (args, ""))
+		return NULL;
+	if (mpz_size (key->x) == 0)
+		return Py_BuildValue ("i", 0);
+	else
+		return Py_BuildValue ("i", 1);
+}
+
+
+void
+init_dsa (void)
+{
+	dsaKeyType.ob_type = &PyType_Type;
+	_dsa_module = Py_InitModule ("_dsa", _dsa__methods__);
+	_dsa_dict = PyModule_GetDict (_dsa_module);
+	dsaError = PyErr_NewException ("_dsa.error", NULL, NULL);
+	PyDict_SetItemString (_dsa_dict, "error", dsaError);
+}

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/_fastmath.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/_fastmath.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/_fastmath.c
new file mode 100755
index 0000000..7fe7abe
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/_fastmath.c
@@ -0,0 +1,804 @@
+
+/*
+ *  _fastmath.c: Accelerator module that uses GMP for faster numerics.
+ *
+ * Part of the Python Cryptography Toolkit
+ *
+ * Distribute and use freely; there are no restrictions on further 
+ * dissemination and usage except those imposed by the laws of your 
+ * country of residence.
+ *
+ * $Id: _fastmath.c,v 1.13 2003/04/04 19:20:29 jbontje Exp $
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <Python.h>
+#include <longintrepr.h>				/* for conversions */
+#include <gmp.h>
+
+void
+longObjToMPZ (mpz_t m, PyLongObject * p)
+{
+	int size, i;
+	mpz_t temp, temp2;
+	mpz_init (temp);
+	mpz_init (temp2);
+	if (p->ob_size > 0)
+		size = p->ob_size;
+	else
+		size = -p->ob_size;
+	for (i = 0; i < size; i++)
+	{
+		mpz_set_ui (temp, p->ob_digit[i]);
+		mpz_mul_2exp (temp2, temp, SHIFT * i);
+		mpz_add (m, m, temp2);
+	}
+	mpz_clear (temp);
+	mpz_clear (temp2);
+}
+
+PyObject *
+mpzToLongObj (mpz_t m)
+{
+	/* borrowed from gmpy */
+	int size = (mpz_sizeinbase (m, 2) + SHIFT - 1) / SHIFT;
+	int i;
+	mpz_t temp;
+	PyLongObject *l = _PyLong_New (size);
+	if (!l)
+		return NULL;
+	mpz_init_set (temp, m);
+	for (i = 0; i < size; i++)
+	{
+		l->ob_digit[i] = (digit) (mpz_get_ui (temp) & MASK);
+		mpz_fdiv_q_2exp (temp, temp, SHIFT);
+	}
+	i = size;
+	while ((i > 0) && (l->ob_digit[i - 1] == 0))
+		i--;
+	l->ob_size = i;
+	mpz_clear (temp);
+	return (PyObject *) l;
+}
+
+typedef struct
+{
+	PyObject_HEAD mpz_t y;
+	mpz_t g;
+	mpz_t p;
+	mpz_t q;
+	mpz_t x;
+}
+dsaKey;
+
+typedef struct
+{
+	PyObject_HEAD mpz_t n;
+	mpz_t e;
+	mpz_t d;
+	mpz_t p;
+	mpz_t q;
+	mpz_t u;
+}
+rsaKey;
+
+PyObject *rsaKey_new (PyObject *, PyObject *);
+PyObject *dsaKey_new (PyObject *, PyObject *);
+
+static void dsaKey_dealloc (dsaKey *);
+static PyObject *dsaKey_getattr (dsaKey *, char *);
+static PyObject *dsaKey__sign (dsaKey *, PyObject *);
+static PyObject *dsaKey__verify (dsaKey *, PyObject *);
+static PyObject *dsaKey_size (dsaKey *, PyObject *);
+static PyObject *dsaKey_has_private (dsaKey *, PyObject *);
+
+static void rsaKey_dealloc (rsaKey *);
+static PyObject *rsaKey_getattr (rsaKey *, char *);
+static PyObject *rsaKey__encrypt (rsaKey *, PyObject *);
+static PyObject *rsaKey__decrypt (rsaKey *, PyObject *);
+static PyObject *rsaKey__verify (rsaKey *, PyObject *);
+static PyObject *rsaKey__blind (rsaKey *, PyObject *);
+static PyObject *rsaKey__unblind (rsaKey *, PyObject *);
+static PyObject *rsaKey_size (rsaKey *, PyObject *);
+static PyObject *rsaKey_has_private (rsaKey *, PyObject *);
+
+static int
+dsaSign (dsaKey * key, mpz_t m, mpz_t k, mpz_t r, mpz_t s)
+{
+	mpz_t temp;
+	if (mpz_cmp_ui (k, 2) < 0 || mpz_cmp (k, key->q) >= 0)
+	{
+		return 1;
+	}
+	mpz_init (temp);
+	mpz_powm (r, key->g, k, key->p);
+	mpz_mod (r, r, key->q);
+	mpz_invert (s, k, key->q);
+	mpz_mul (temp, key->x, r);
+	mpz_add (temp, m, temp);
+	mpz_mul (s, s, temp);
+	mpz_mod (s, s, key->q);
+	mpz_clear (temp);
+	return 0;
+}
+
+static int
+dsaVerify (dsaKey * key, mpz_t m, mpz_t r, mpz_t s)
+{
+	int result;
+	mpz_t u1, u2, v1, v2, w;
+	if (mpz_cmp_ui (r, 0) <= 0 || mpz_cmp (r, key->q) >= 0 ||
+	    mpz_cmp_ui (s, 0) <= 0 || mpz_cmp (s, key->q) >= 0)
+		return 0;
+	mpz_init (u1);
+	mpz_init (u2);
+	mpz_init (v1);
+	mpz_init (v2);
+	mpz_init (w);
+	mpz_invert (w, s, key->q);
+	mpz_mul (u1, m, w);
+	mpz_mod (u1, u1, key->q);
+	mpz_mul (u2, r, w);
+	mpz_mod (u2, u2, key->q);
+	mpz_powm (v1, key->g, u1, key->p);
+	mpz_powm (v2, key->y, u2, key->p);
+	mpz_mul (w, v1, v2);
+	mpz_mod (w, w, key->p);
+	mpz_mod (w, w, key->q);
+	if (mpz_cmp (r, w) == 0)
+		result = 1;
+	else
+		result = 0;
+	mpz_clear (u1);
+	mpz_clear (u2);
+	mpz_clear (v1);
+	mpz_clear (v2);
+	mpz_clear (w);
+	return result;
+}
+
+
+static int
+rsaEncrypt (rsaKey * key, mpz_t v)
+{
+	if (mpz_cmp (v, key->n) >= 0)
+	{
+		return 1;
+	}
+	mpz_powm (v, v, key->e, key->n);
+	return 0;
+}
+
+static int
+rsaDecrypt (rsaKey * key, mpz_t v)
+{
+    mpz_t m1, m2, h;
+	if (mpz_cmp (v, key->n) >= 0)
+	{
+		return 1;
+	}
+	if (mpz_size (key->d) == 0)
+	{
+		return 2;
+	}
+
+    if ((mpz_size (key->p) != 0) && (mpz_size (key->q) != 0) && 
+        (mpz_size (key->u) != 0))
+    {
+        /* fast path */
+        mpz_init(m1);
+        mpz_init(m2);
+        mpz_init(h);
+
+        /* m1 = c ^ (d mod (p-1)) mod p */
+        mpz_sub_ui(h, key->p, 1);
+        mpz_fdiv_r(h, key->d, h);
+        mpz_powm(m1, v, h, key->p);
+        /* m2 = c ^ (d mod (q-1)) mod q */
+        mpz_sub_ui(h, key->q, 1);
+        mpz_fdiv_r(h, key->d, h);
+        mpz_powm(m2, v, h, key->q);
+        /* h = u * ( m2 - m1 ) mod q */
+        mpz_sub(h, m2, m1);
+        if (mpz_sgn(h)==-1)
+            mpz_add(h, h, key->q);
+        mpz_mul(h, key->u, h);
+        mpz_mod(h, h, key->q);
+        /* m = m2 + h * p */
+        mpz_mul(h, h, key->p);
+        mpz_add(v, m1, h);
+        /* ready */
+
+        mpz_clear(m1);
+        mpz_clear(m2);
+        mpz_clear(h);
+        return 0;
+    }
+
+    /* slow */
+	mpz_powm (v, v, key->d, key->n);
+	return 0;
+}
+
+static int
+rsaBlind (rsaKey * key, mpz_t v, mpz_t b)
+{
+    if (mpz_cmp (v, key->n) >= 0)
+        {
+            return 1;
+        }
+    if (mpz_cmp (b, key->n) >= 0)
+        {
+            return 2;
+        }
+    mpz_powm (b, b, key->e, key->n);
+    mpz_mul (v, v, b);
+    mpz_mod (v, v, key->n);
+    return 0;
+}
+
+static int
+rsaUnBlind (rsaKey * key, mpz_t v, mpz_t b)
+{
+    if (mpz_cmp (v, key->n) >= 0)
+        {
+            return 1;
+        }
+    if (mpz_cmp (b, key->n) >= 0)
+        {
+            return 2;
+        }
+    if (!mpz_invert (b, b, key->n))
+        {
+            return 3;
+        }
+    mpz_mul (v, v, b);
+    mpz_mod (v, v, key->n);
+    return 0;
+}
+ 
+
+static PyTypeObject dsaKeyType = {
+	PyObject_HEAD_INIT (NULL) 0,
+	"dsaKey",
+	sizeof (dsaKey),
+	0,
+	(destructor) dsaKey_dealloc,	/* dealloc */
+	0,				/* print */
+	(getattrfunc) dsaKey_getattr,	/* getattr */
+	0,				/* setattr */
+	0,				/* compare */
+	0,				/* repr */
+	0,				/* as_number */
+	0,				/* as_sequence */
+	0,				/* as_mapping */
+	0,				/* hash */
+	0,				/* call */
+};
+
+static PyMethodDef dsaKey__methods__[] = {
+	{"_sign", (PyCFunction) dsaKey__sign, METH_VARARGS, 
+	 "Sign the given long."},
+	{"_verify", (PyCFunction) dsaKey__verify, METH_VARARGS,
+	 "Verify that the signature is valid."},
+	{"size", (PyCFunction) dsaKey_size, METH_VARARGS,
+	 "Return the number of bits that this key can handle."},
+	{"has_private", (PyCFunction) dsaKey_has_private, METH_VARARGS,
+	 "Return 1 or 0 if this key does/doesn't have a private key."},
+	{NULL, NULL, 0, NULL}
+};
+
+PyObject *fastmathError;							/* raised on errors */
+
+static PyTypeObject rsaKeyType = {
+	PyObject_HEAD_INIT (NULL) 0,
+	"rsaKey",
+	sizeof (rsaKey),
+	0,
+	(destructor) rsaKey_dealloc,	/* dealloc */
+	0,				/* print */
+	(getattrfunc) rsaKey_getattr,	/* getattr */
+	0,                              /* setattr */
+	0,				/* compare */
+	0,				/* repr */
+	0,				/* as_number */
+	0,				/* as_sequence */
+	0,				/* as_mapping */
+	0,				/* hash */
+	0,				/* call */
+};
+
+static PyMethodDef rsaKey__methods__[] = {
+	{"_encrypt", (PyCFunction) rsaKey__encrypt, METH_VARARGS,
+	 "Encrypt the given long."},
+	{"_decrypt", (PyCFunction) rsaKey__decrypt, METH_VARARGS,
+	 "Decrypt the given long."},
+	{"_sign", (PyCFunction) rsaKey__decrypt, METH_VARARGS,
+	 "Sign the given long."},
+	{"_verify", (PyCFunction) rsaKey__verify, METH_VARARGS,
+	 "Verify that the signature is valid."},
+ 	{"_blind", (PyCFunction) rsaKey__blind, METH_VARARGS,
+ 	 "Blind the given long."},
+ 	{"_unblind", (PyCFunction) rsaKey__unblind, METH_VARARGS,
+ 	 "Unblind the given long."},
+	{"size", (PyCFunction) rsaKey_size, METH_VARARGS,
+	 "Return the number of bits that this key can handle."},
+	{"has_private", (PyCFunction) rsaKey_has_private, METH_VARARGS,
+	 "Return 1 or 0 if this key does/doesn't have a private key."},
+	{NULL, NULL, 0, NULL}
+};
+
+PyObject *
+dsaKey_new (PyObject * self, PyObject * args)
+{
+	PyLongObject *y = NULL, *g = NULL, *p = NULL, *q = NULL, *x = NULL;
+	dsaKey *key;
+	if (!PyArg_ParseTuple(args, "O!O!O!O!|O!", &PyLong_Type, &y,
+			      &PyLong_Type, &g, &PyLong_Type, &p, 
+			      &PyLong_Type, &q, &PyLong_Type, &x))
+		return NULL;
+
+	key = PyObject_New (dsaKey, &dsaKeyType);
+	mpz_init (key->y);
+	mpz_init (key->g);
+	mpz_init (key->p);
+	mpz_init (key->q);
+	mpz_init (key->x);
+	longObjToMPZ (key->y, y);
+	longObjToMPZ (key->g, g);
+	longObjToMPZ (key->p, p);
+	longObjToMPZ (key->q, q);
+	if (x)
+	{
+		longObjToMPZ (key->x, x);
+	}
+	return (PyObject *) key;
+}
+
+static void
+dsaKey_dealloc (dsaKey * key)
+{
+	mpz_clear (key->y);
+	mpz_clear (key->g);
+	mpz_clear (key->p);
+	mpz_clear (key->q);
+	mpz_clear (key->x);
+	PyObject_Del (key);
+}
+
+static PyObject *
+dsaKey_getattr (dsaKey * key, char *attr)
+{
+	if (strcmp (attr, "y") == 0)
+		return mpzToLongObj (key->y);
+	else if (strcmp (attr, "g") == 0)
+		return mpzToLongObj (key->g);
+	else if (strcmp (attr, "p") == 0)
+		return mpzToLongObj (key->p);
+	else if (strcmp (attr, "q") == 0)
+		return mpzToLongObj (key->q);
+	else if (strcmp (attr, "x") == 0)
+	{
+		if (mpz_size (key->x) == 0)
+		{
+			PyErr_SetString (PyExc_AttributeError,
+					 "dsaKey instance has no attribute 'x'");
+			return NULL;
+		}
+		return mpzToLongObj (key->x);
+	}
+	else
+	{
+		return Py_FindMethod (dsaKey__methods__, (PyObject *) key, attr);
+	}
+}
+
+PyObject *
+dsaKey__sign (dsaKey * key, PyObject * args)
+{
+	PyObject *lm, *lk, *lr, *ls;
+	mpz_t m, k, r, s;
+	int result;
+	if (!PyArg_ParseTuple (args, "O!O!", &PyLong_Type, &lm,
+			       &PyLong_Type, &lk))
+	{
+		return NULL;
+	}
+	mpz_init (m);
+	mpz_init (k);
+	mpz_init (r);
+	mpz_init (s);
+	longObjToMPZ (m, (PyLongObject *) lm);
+	longObjToMPZ (k, (PyLongObject *) lk);
+	result = dsaSign (key, m, k, r, s);
+	if (result == 1)
+	{
+		PyErr_SetString (fastmathError, "K not between 2 and q");
+		return NULL;
+	}
+	lr = mpzToLongObj (r);
+	ls = mpzToLongObj (s);
+	mpz_clear (m);
+	mpz_clear (k);
+	mpz_clear (r);
+	mpz_clear (s);
+	return Py_BuildValue ("(NN)", lr, ls);
+}
+
+PyObject *
+dsaKey__verify (dsaKey * key, PyObject * args)
+{
+	PyObject *lm, *lr, *ls;
+	mpz_t m, r, s;
+	int result;
+	if (!PyArg_ParseTuple (args, "O!O!O!", &PyLong_Type, &lm,
+			       &PyLong_Type, &lr, &PyLong_Type, &ls))
+	{
+		return NULL;
+	}
+	mpz_init (m);
+	mpz_init (r);
+	mpz_init (s);
+	longObjToMPZ (m, (PyLongObject *) lm);
+	longObjToMPZ (r, (PyLongObject *) lr);
+	longObjToMPZ (s, (PyLongObject *) ls);
+	result = dsaVerify (key, m, r, s);
+	mpz_clear (m);
+	mpz_clear (r);
+	mpz_clear (s);
+	if (result) {
+		Py_INCREF(Py_True);
+		return Py_True;
+        } else {
+		Py_INCREF(Py_False);
+		return Py_False;
+	}
+}
+
+PyObject *
+dsaKey_size (dsaKey * key, PyObject * args)
+{
+	if (!PyArg_ParseTuple (args, ""))
+		return NULL;
+	return Py_BuildValue ("i", mpz_sizeinbase (key->p, 2) - 1);
+}
+
+PyObject *
+dsaKey_has_private (dsaKey * key, PyObject * args)
+{
+	if (!PyArg_ParseTuple (args, ""))
+		return NULL;
+	if (mpz_size (key->x) == 0) {
+		Py_INCREF(Py_False);
+		return Py_False;
+        } else {
+		Py_INCREF(Py_True);
+		return Py_True;
+        }
+}
+
+PyObject *
+rsaKey_new (PyObject * self, PyObject * args)
+{
+	PyLongObject *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL, 
+                     *u = NULL;
+	rsaKey *key;
+
+	if (!PyArg_ParseTuple(args, "O!O!|O!O!O!O!", &PyLong_Type, &n,
+			      &PyLong_Type, &e, &PyLong_Type, &d, 
+			      &PyLong_Type, &p, &PyLong_Type, &q,
+                              &PyLong_Type, &u))
+		return NULL;
+
+	key = PyObject_New (rsaKey, &rsaKeyType);
+	mpz_init (key->n);
+	mpz_init (key->e);
+	mpz_init (key->d);
+	mpz_init (key->p);
+	mpz_init (key->q);
+	mpz_init (key->u);
+	longObjToMPZ (key->n, n);
+	longObjToMPZ (key->e, e);
+	if (!d)
+	{
+		return (PyObject *) key;
+	}
+	longObjToMPZ (key->d, d);
+	if (p && q)
+	{
+		longObjToMPZ (key->p, p);
+		longObjToMPZ (key->q, q);
+		if (u) {
+			longObjToMPZ (key->u, u);
+		} else {
+			mpz_invert (key->u, key->p, key->q);
+		}
+	}
+	return (PyObject *) key;
+}
+
+static void
+rsaKey_dealloc (rsaKey * key)
+{
+	mpz_clear (key->n);
+	mpz_clear (key->e);
+	mpz_clear (key->d);
+	mpz_clear (key->p);
+	mpz_clear (key->q);
+	mpz_clear (key->u);
+	PyObject_Del (key);
+}
+
+static PyObject *
+rsaKey_getattr (rsaKey * key, char *attr)
+{
+	if (strcmp (attr, "n") == 0)
+		return mpzToLongObj (key->n);
+	else if (strcmp (attr, "e") == 0)
+		return mpzToLongObj (key->e);
+	else if (strcmp (attr, "d") == 0)
+	{
+		if (mpz_size (key->d) == 0)
+		{
+			PyErr_SetString(PyExc_AttributeError,
+					"rsaKey instance has no attribute 'd'");
+			return NULL;
+		}
+		return mpzToLongObj (key->d);
+	}
+	else if (strcmp (attr, "p") == 0)
+	{
+		if (mpz_size (key->p) == 0)
+		{
+			PyErr_SetString(PyExc_AttributeError,
+					"rsaKey instance has no attribute 'p'");
+			return NULL;
+		}
+		return mpzToLongObj (key->p);
+	}
+	else if (strcmp (attr, "q") == 0)
+	{
+		if (mpz_size (key->q) == 0)
+		{
+			PyErr_SetString(PyExc_AttributeError,
+					"rsaKey instance has no attribute 'q'");
+			return NULL;
+		}
+		return mpzToLongObj (key->q);
+	}
+	else if (strcmp (attr, "u") == 0)
+	{
+		if (mpz_size (key->u) == 0)
+		{
+			PyErr_SetString(PyExc_AttributeError,
+					"rsaKey instance has no attribute 'u'");
+			return NULL;
+		}
+		return mpzToLongObj (key->u);
+	}
+	else
+	{
+		return Py_FindMethod (rsaKey__methods__, 
+				      (PyObject *) key, attr);
+	}
+}
+
+PyObject *
+rsaKey__encrypt (rsaKey * key, PyObject * args)
+{
+	PyObject *l, *r;
+	mpz_t v;
+	int result;
+	if (!PyArg_ParseTuple (args, "O!", &PyLong_Type, &l))
+	{
+		return NULL;
+	}
+	mpz_init (v);
+	longObjToMPZ (v, (PyLongObject *) l);
+	result = rsaEncrypt (key, v);
+	if (result == 1)
+	{
+		PyErr_SetString (fastmathError, "Plaintext too large");
+		return NULL;
+	}
+	r = (PyObject *) mpzToLongObj (v);
+	mpz_clear (v);
+	return Py_BuildValue ("N", r);
+}
+
+PyObject *
+rsaKey__decrypt (rsaKey * key, PyObject * args)
+{
+	PyObject *l, *r;
+	mpz_t v;
+	int result;
+	if (!PyArg_ParseTuple (args, "O!", &PyLong_Type, &l))
+	{
+		return NULL;
+	}
+	mpz_init (v);
+	longObjToMPZ (v, (PyLongObject *) l);
+	result = rsaDecrypt (key, v);
+	if (result == 1)
+	{
+		PyErr_SetString (fastmathError, 
+				 "Ciphertext too large");
+		return NULL;
+	}
+	else if (result == 2)
+	{
+		PyErr_SetString (fastmathError, 
+				 "Private key not available in this object");
+		return NULL;
+	}
+	r = mpzToLongObj (v);
+	mpz_clear (v);
+	return Py_BuildValue ("N", r);
+}
+
+PyObject *
+rsaKey__verify (rsaKey * key, PyObject * args)
+{
+	PyObject *l, *lsig;
+	mpz_t v, vsig;
+	if (!PyArg_ParseTuple(args, "O!O!", 
+			      &PyLong_Type, &l, &PyLong_Type, &lsig))
+	{
+		return NULL;
+	}
+	mpz_init (v);
+	mpz_init (vsig);
+	longObjToMPZ (v, (PyLongObject *) l);
+	longObjToMPZ (vsig, (PyLongObject *) lsig);
+	rsaEncrypt (key, vsig);
+	if (mpz_cmp (v, vsig) == 0) {
+		Py_INCREF(Py_True);
+		return Py_True;
+	}
+	else {
+		Py_INCREF(Py_False);
+		return Py_False;
+        }
+}
+
+PyObject *
+rsaKey__blind (rsaKey * key, PyObject * args)
+{
+	PyObject *l, *lblind, *r;
+	mpz_t v, vblind;
+	int result;
+	if (!PyArg_ParseTuple (args, "O!O!", &PyLong_Type, &l, 
+                               &PyLong_Type, &lblind))
+		{
+			return NULL;
+		}
+	mpz_init (v);
+	mpz_init (vblind);
+	longObjToMPZ (v, (PyLongObject *) l);
+	longObjToMPZ (vblind, (PyLongObject *) lblind);
+	result = rsaBlind (key, v, vblind);
+	if (result == 1)
+		{
+			PyErr_SetString (fastmathError, "Message too large");
+			return NULL;
+		}
+	else if (result == 2)
+		{
+			PyErr_SetString (fastmathError, "Blinding factor too large");
+			return NULL;
+		}
+	r = (PyObject *) mpzToLongObj (v);
+	mpz_clear (v);
+	mpz_clear (vblind);
+	return Py_BuildValue ("N", r);
+}
+
+PyObject *
+rsaKey__unblind (rsaKey * key, PyObject * args)
+{
+	PyObject *l, *lblind, *r;
+	mpz_t v, vblind;
+	int result;
+	if (!PyArg_ParseTuple (args, "O!O!", &PyLong_Type, &l, 
+                               &PyLong_Type, &lblind))
+		{
+			return NULL;
+		}
+	mpz_init (v);
+	mpz_init (vblind);
+	longObjToMPZ (v, (PyLongObject *) l);
+	longObjToMPZ (vblind, (PyLongObject *) lblind);
+	result = rsaUnBlind (key, v, vblind);
+	if (result == 1)
+		{
+			PyErr_SetString (fastmathError, "Message too large");
+			return NULL;
+		}
+	else if (result == 2)
+		{
+			PyErr_SetString (fastmathError, "Blinding factor too large");
+			return NULL;
+		}
+	else if (result == 3)
+		{
+			PyErr_SetString (fastmathError, "Inverse doesn't exist");
+			return NULL;
+		}
+	r = (PyObject *) mpzToLongObj (v);
+	mpz_clear (v);
+	mpz_clear (vblind);
+	return Py_BuildValue ("N", r);
+}
+  
+PyObject *
+rsaKey_size (rsaKey * key, PyObject * args)
+{
+	if (!PyArg_ParseTuple (args, ""))
+		return NULL;
+	return Py_BuildValue ("i", mpz_sizeinbase (key->n, 2) - 1);
+}
+
+PyObject *
+rsaKey_has_private (rsaKey * key, PyObject * args)
+{
+	if (!PyArg_ParseTuple (args, ""))
+		return NULL;
+	if (mpz_size (key->d) == 0) {
+		Py_INCREF(Py_False);
+		return Py_False;
+        } else {
+		Py_INCREF(Py_True);
+		return Py_True;
+	}
+}
+
+
+PyObject *
+isPrime (PyObject * self, PyObject * args)
+{
+	PyObject *l;
+	mpz_t n;
+	int result;
+
+	if (!PyArg_ParseTuple (args, "O!", &PyLong_Type, &l))
+	{
+		return NULL;
+	}
+	mpz_init (n);
+	longObjToMPZ (n, (PyLongObject *) l);
+
+	result = mpz_probab_prime_p(n, 5);
+
+	mpz_clear (n);
+
+	if (result == 0) {
+		Py_INCREF(Py_False);
+		return Py_False;
+        } else {
+		Py_INCREF(Py_True);
+		return Py_True;
+	}
+}
+
+
+static PyMethodDef _fastmath__methods__[] = {
+	{"dsa_construct", dsaKey_new, METH_VARARGS},
+	{"rsa_construct", rsaKey_new, METH_VARARGS},
+        {"isPrime", isPrime, METH_VARARGS},
+	{NULL, NULL}
+};
+
+void
+init_fastmath (void)
+{
+        PyObject *_fastmath_module;
+        PyObject *_fastmath_dict;
+
+	rsaKeyType.ob_type = &PyType_Type;
+	dsaKeyType.ob_type = &PyType_Type;
+	_fastmath_module = Py_InitModule ("_fastmath", _fastmath__methods__);
+	_fastmath_dict = PyModule_GetDict (_fastmath_module);
+	fastmathError = PyErr_NewException ("_fastmath.error", NULL, NULL);
+	PyDict_SetItemString (_fastmath_dict, "error", fastmathError);
+}

http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/2c87cdcb/tools/bin/pythonSrc/pycrypto-2.0.1/src/_rsa.c
----------------------------------------------------------------------
diff --git a/tools/bin/pythonSrc/pycrypto-2.0.1/src/_rsa.c b/tools/bin/pythonSrc/pycrypto-2.0.1/src/_rsa.c
new file mode 100755
index 0000000..e51d188
--- /dev/null
+++ b/tools/bin/pythonSrc/pycrypto-2.0.1/src/_rsa.c
@@ -0,0 +1,346 @@
+
+/*
+ *  _rsa.c: C implementation of the RSA algorithm.
+ *
+ * Part of the Python Cryptography Toolkit
+ *
+ * Distribute and use freely; there are no restrictions on further 
+ * dissemination and usage except those imposed by the laws of your 
+ * country of residence.
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <Python.h>
+#include <longintrepr.h>				/* for conversions */
+#include <gmp.h>
+
+PyObject *_rsa_module;
+PyObject *_rsa_dict;
+
+void
+longObjToMPZ (mpz_t m, PyLongObject * p)
+{
+	int size, i;
+	mpz_t temp, temp2;
+	mpz_init (temp);
+	mpz_init (temp2);
+	if (p->ob_size > 0)
+		size = p->ob_size;
+	else
+		size = -p->ob_size;
+	for (i = 0; i < size; i++)
+		{
+			mpz_set_ui (temp, p->ob_digit[i]);
+			mpz_mul_2exp (temp2, temp, SHIFT * i);
+			mpz_add (m, m, temp2);
+		}
+	mpz_clear (temp);
+	mpz_clear (temp2);
+}
+
+PyObject *
+mpzToLongObj (mpz_t m)
+{
+	/* borrowed from gmpy */
+	int size = (mpz_sizeinbase (m, 2) + SHIFT - 1) / SHIFT;
+	int i;
+	mpz_t temp;
+	PyLongObject *l = _PyLong_New (size);
+	if (!l)
+		return NULL;
+	mpz_init_set (temp, m);
+	for (i = 0; i < size; i++)
+		{
+			l->ob_digit[i] = (digit) (mpz_get_ui (temp) & MASK);
+			mpz_fdiv_q_2exp (temp, temp, SHIFT);
+		}
+	i = size;
+	while ((i > 0) && (l->ob_digit[i - 1] == 0))
+		i--;
+	l->ob_size = i;
+	mpz_clear (temp);
+	return (PyObject *) l;
+}
+
+PyObject *rsaKey_new (PyObject *, PyObject *);
+
+static PyMethodDef _rsa__methods__[] = {
+	{"construct", rsaKey_new, METH_VARARGS},
+	{NULL, NULL}
+};
+
+typedef struct
+{
+	PyObject_HEAD mpz_t n;
+	mpz_t e;
+	mpz_t d;
+	mpz_t p;
+	mpz_t q;
+}
+rsaKey;
+
+static int
+rsaEncrypt (rsaKey * key, mpz_t v)
+{
+	if (mpz_cmp (v, key->n) >= 0)
+		{
+			return 1;
+		}
+	mpz_powm (v, v, key->e, key->n);
+	return 0;
+}
+
+static int
+rsaDecrypt (rsaKey * key, mpz_t v)
+{
+	if (mpz_cmp (v, key->n) >= 0)
+		{
+			return 1;
+		}
+	if (mpz_size (key->d) == 0)
+		{
+			return 2;
+		}
+	mpz_powm (v, v, key->d, key->n);
+	return 0;
+}
+
+static void rsaKey_dealloc (rsaKey *);
+static PyObject *rsaKey_getattr (rsaKey *, char *);
+static PyObject *rsaKey__encrypt (rsaKey *, PyObject *);
+static PyObject *rsaKey__decrypt (rsaKey *, PyObject *);
+static PyObject *rsaKey__verify (rsaKey *, PyObject *);
+static PyObject *rsaKey_size (rsaKey *, PyObject *);
+static PyObject *rsaKey_hasprivate (rsaKey *, PyObject *);
+
+PyObject *rsaError;							/* raised on errors */
+
+static PyTypeObject rsaKeyType = {
+	PyObject_HEAD_INIT (NULL) 0,
+	"rsaKey",
+	sizeof (rsaKey),
+	0,
+	(destructor) rsaKey_dealloc,	/* dealloc */
+	0,														/* print */
+	(getattrfunc) rsaKey_getattr,	/* getattr */
+	0,														/* setattr */
+	0,														/* compare */
+	0,														/* repr */
+	0,														/* as_number */
+	0,														/* as_sequence */
+	0,														/* as_mapping */
+	0,														/* hash */
+	0,														/* call */
+};
+
+static PyMethodDef rsaKey__methods__[] = {
+	{"_encrypt", (PyCFunction) rsaKey__encrypt, METH_VARARGS,
+	 "Encrypt the given long."},
+	{"_decrypt", (PyCFunction) rsaKey__decrypt, METH_VARARGS,
+	 "Decrypt the given long."},
+	{"_sign", (PyCFunction) rsaKey__decrypt, METH_VARARGS,
+	 "Sign the given long."},
+	{"_verify", (PyCFunction) rsaKey__verify, METH_VARARGS,
+	 "Verify that the signature is valid."},
+	{"size", (PyCFunction) rsaKey_size, METH_VARARGS,
+	 "Return the number of bits that this key can handle."},
+	{"hasprivate", (PyCFunction) rsaKey_hasprivate, METH_VARARGS,
+	 "Return 1 or 0 if this key does/doesn't have a private key."},
+	{NULL, NULL, 0, NULL}
+};
+
+PyObject *
+rsaKey_new (PyObject * self, PyObject * args)
+{
+	PyLongObject *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL;
+	rsaKey *key;
+	key = PyObject_New (rsaKey, &rsaKeyType);
+	mpz_init (key->n);
+	mpz_init (key->e);
+	mpz_init (key->d);
+	mpz_init (key->p);
+	mpz_init (key->q);
+	PyArg_ParseTuple (args, "O!O!|O!O!O!", &PyLong_Type, &n,
+										&PyLong_Type, &e,
+										&PyLong_Type, &d, &PyLong_Type, &p, &PyLong_Type, &q);
+	longObjToMPZ (key->n, n);
+	longObjToMPZ (key->e, e);
+	if (!d)
+		{
+			return (PyObject *) key;
+		}
+	longObjToMPZ (key->d, d);
+	if (p)
+		{
+			if (q)
+				{
+					longObjToMPZ (key->p, p);
+					longObjToMPZ (key->q, q);
+				}
+		}
+	/*Py_XDECREF(n);
+	   Py_XDECREF(e);
+	   Py_XDECREF(d);
+	   Py_XDECREF(p);
+	   Py_XDECREF(q); */
+	return (PyObject *) key;
+}
+
+static void
+rsaKey_dealloc (rsaKey * key)
+{
+	mpz_clear (key->n);
+	mpz_clear (key->e);
+	mpz_clear (key->d);
+	mpz_clear (key->p);
+	mpz_clear (key->q);
+	PyObject_Del (key);
+}
+
+static PyObject *
+rsaKey_getattr (rsaKey * key, char *attr)
+{
+	if (strcmp (attr, "n") == 0)
+		return mpzToLongObj (key->n);
+	else if (strcmp (attr, "e") == 0)
+		return mpzToLongObj (key->e);
+	else if (strcmp (attr, "d") == 0)
+		{
+			if (mpz_size (key->d) == 0)
+				{
+					PyErr_SetString (PyExc_AttributeError,
+													 "rsaKey instance has no attribute 'd'");
+					return NULL;
+				}
+			return mpzToLongObj (key->d);
+		}
+	else if (strcmp (attr, "p") == 0)
+		{
+			if (mpz_size (key->p) == 0)
+				{
+					PyErr_SetString (PyExc_AttributeError,
+													 "rsaKey instance has no attribute 'p'");
+					return NULL;
+				}
+			return mpzToLongObj (key->p);
+		}
+	else if (strcmp (attr, "q") == 0)
+		{
+			if (mpz_size (key->q) == 0)
+				{
+					PyErr_SetString (PyExc_AttributeError,
+													 "rsaKey instance has no attribute 'q'");
+					return NULL;
+				}
+			return mpzToLongObj (key->q);
+		}
+	else
+		{
+			return Py_FindMethod (rsaKey__methods__, (PyObject *) key, attr);
+		}
+}
+
+PyObject *
+rsaKey__encrypt (rsaKey * key, PyObject * args)
+{
+	PyObject *l, *r;
+	mpz_t v;
+	int result;
+	if (!(PyArg_ParseTuple (args, "O!", &PyLong_Type, &l)))
+		{
+			return NULL;
+		}
+	mpz_init (v);
+	longObjToMPZ (v, (PyLongObject *) l);
+	result = rsaEncrypt (key, v);
+	if (result == 1)
+		{
+			PyErr_SetString (rsaError, "Plaintext too large");
+			return NULL;
+		}
+	r = (PyObject *) mpzToLongObj (v);
+	mpz_clear (v);
+	return Py_BuildValue ("N", r);
+}
+
+PyObject *
+rsaKey__decrypt (rsaKey * key, PyObject * args)
+{
+	PyObject *l, *r;
+	mpz_t v;
+	int result;
+	if (!(PyArg_ParseTuple (args, "O!", &PyLong_Type, &l)))
+		{
+			return NULL;
+		}
+	mpz_init (v);
+	longObjToMPZ (v, (PyLongObject *) l);
+	result = rsaDecrypt (key, v);
+	if (result == 1)
+		{
+			PyErr_SetString (rsaError, "Ciphertext too large");
+			return NULL;
+		}
+	else if (result == 2)
+		{
+			PyErr_SetString (rsaError, "Private key not available in this object");
+			return NULL;
+		}
+	r = mpzToLongObj (v);
+	mpz_clear (v);
+	return Py_BuildValue ("N", r);
+}
+
+PyObject *
+rsaKey__verify (rsaKey * key, PyObject * args)
+{
+	PyObject *l, *lsig;
+	mpz_t v, vsig;
+	if (!
+			(PyArg_ParseTuple
+			 (args, "O!O!", &PyLong_Type, &l, &PyLong_Type, &lsig)))
+		{
+			return NULL;
+		}
+	mpz_init (v);
+	mpz_init (vsig);
+	longObjToMPZ (v, (PyLongObject *) l);
+	longObjToMPZ (vsig, (PyLongObject *) lsig);
+	rsaEncrypt (key, vsig);
+	if (mpz_cmp (v, vsig) == 0)
+		return Py_BuildValue ("i", 1);
+	else
+		return Py_BuildValue ("i", 0);
+}
+
+PyObject *
+rsaKey_size (rsaKey * key, PyObject * args)
+{
+	if (!PyArg_ParseTuple (args, ""))
+		return NULL;
+	return Py_BuildValue ("i", mpz_sizeinbase (key->n, 2) - 1);
+}
+
+PyObject *
+rsaKey_hasprivate (rsaKey * key, PyObject * args)
+{
+	if (!PyArg_ParseTuple (args, ""))
+		return NULL;
+	if (mpz_size (key->d) == 0)
+		return Py_BuildValue ("i", 0);
+	else
+		return Py_BuildValue ("i", 1);
+}
+
+
+void
+init_rsa (void)
+{
+	rsaKeyType.ob_type = &PyType_Type;
+	_rsa_module = Py_InitModule ("_rsa", _rsa__methods__);
+	_rsa_dict = PyModule_GetDict (_rsa_module);
+	rsaError = PyErr_NewException ("_rsa.error", NULL, NULL);
+	PyDict_SetItemString (_rsa_dict, "error", rsaError);
+}


Mime
View raw message