return len;
}
+void cIDEA::EcbEncrypt(const unsigned char *data, int len, unsigned char *crypt, IdeaKS *ks) const
+{
+ len/=8;
+ while(len>0) {
+ idea_ecb_encrypt(data,crypt,ks);
+ data+=8;
+ crypt+=8;
+ }
+}
+
// -- cRSA ---------------------------------------------------------------------
bool cRSA::Input(cBN *d, const unsigned char *in, int n, bool LE) const
}
return false;
}
+
+bool cAES::Decrypt(const unsigned char *data, int len, unsigned char *decrypt) const
+{
+ if(active) {
+ for(int i=0; i<len; i+=16) AES_decrypt(data+i,decrypt+i,(const AES_KEY *)&dkey);
+ return true;
+ }
+ return false;
+}
+
+// -- cRC6 ---------------------------------------------------------------------
+
+/*
+ * This code implements the RC6-32/20 block cipher.
+ *
+ * The algorithm is due to Ron Rivest and RSA Labs. This code is based on code
+ * which was written by Martin Hinner <mhi@penguin.cz> in 1999, no copyright is
+ * claimed.
+ */
+
+#define RC6_WORDSIZE 32
+#define RC6_P32 0xB7E15163L
+#define RC6_Q32 0x9E3779B9L
+
+unsigned int cRC6::rol(unsigned int v, unsigned int cnt)
+{
+ cnt&=(RC6_WORDSIZE-1);
+ return (v<<cnt) | (v>>(RC6_WORDSIZE-cnt));
+}
+
+unsigned int cRC6::ror(unsigned int v, unsigned int cnt)
+{
+ cnt&=(RC6_WORDSIZE-1);
+ return (v>>cnt) | (v<<(RC6_WORDSIZE-cnt));
+}
+
+void cRC6::SetKey(const unsigned char *Key, int len)
+{
+ key[0]=RC6_P32;
+ for(int v=1; v<RC6_MAX; v++) key[v]=key[v-1]+RC6_Q32;
+ len/=4;
+ unsigned int a=0, b=0, *l=AUTOARRAY(unsigned int,len);
+ memcpy(l,Key,len*4);
+ for(int i=0,j=0,v=3*(len>RC6_MAX ? len : RC6_MAX) ; v>0; v--) {
+ a=key[i]=rol(key[i]+a+b,3);
+ b= l[j]=rol( l[j]+a+b,a+b);
+ i++; i%=RC6_MAX;
+ j++; j%=len;
+ }
+}
+
+void cRC6::Decrypt(unsigned char *data)
+{
+ Decrypt(data,data);
+}
+
+void cRC6::Decrypt(const unsigned char *in, unsigned char *out)
+{
+ unsigned int *l=(unsigned int *)in;
+ unsigned int a, b, c, d;
+ a=l[0]-key[RC6_MAX-2];
+ b=l[1];
+ c=l[2]-key[RC6_MAX-1];
+ d=l[3];
+ for(int i=RC6_ROUNDS; i>0; i--) {
+ unsigned int t=a;
+ unsigned int u=b;
+ a=d; d=c; b=t; c=u;
+ u=rol((d*(2*d+1)),5);
+ t=rol((b*(2*b+1)),5);
+ c=ror(c-key[2*i+1],t)^u;
+ a=ror(a-key[2*i ],u)^t;
+ }
+ l=(unsigned int *)out;
+ l[0]=a;
+ l[1]=b-key[0];
+ l[2]=c;
+ l[3]=d-key[1];
+}
private:
bool active;
AES_KEY dkey, ekey;
-protected:
+public:
+ cAES(void);
void SetKey(const unsigned char *key);
bool Decrypt(unsigned char *data, int len) const ;
+ bool Decrypt(const unsigned char *data, int len, unsigned char *decrypt) const;
int Encrypt(const unsigned char *data, int len, unsigned char *crypt) const;
-public:
- cAES(void);
};
// ----------------------------------------------------------------
void SetDecKey(const unsigned char *key, IdeaKS *ks) const;
void Decrypt(unsigned char *data, int len, IdeaKS *ks, unsigned char *iv) const;
int Encrypt(const unsigned char *data, int len, unsigned char *crypt, IdeaKS *ks, unsigned char *iv) const;
+ void EcbEncrypt(const unsigned char *data, int len, unsigned char *crypt, IdeaKS *ks) const;
};
// ----------------------------------------------------------------
int RSA(unsigned char *out, int len, BIGNUM *in, const BIGNUM *exp, const BIGNUM *mod, bool LE=true) const;
};
+// ----------------------------------------------------------------
+
+#define RC6_ROUNDS 20
+#define RC6_MAX (RC6_ROUNDS*2+4)
+
+class cRC6 {
+private:
+ unsigned int key[RC6_MAX];
+ //
+ unsigned int rol(unsigned int v, unsigned int cnt);
+ unsigned int ror(unsigned int v, unsigned int cnt);
+public:
+ void SetKey(const unsigned char *Key, int len);
+ void Decrypt(unsigned char *data);
+ void Decrypt(const unsigned char *in, unsigned char *out);
+ };
+
#endif //___CRYPTO_H
#include <unistd.h>
#endif
-// -- cRC6 ---------------------------------------------------------------------
-
-/*
- * This code implements the RC6-32/20 block cipher.
- *
- * The algorithm is due to Ron Rivest and RSA Labs. This code is based on code
- * which was written by Martin Hinner <mhi@penguin.cz> in 1999, no copyright is
- * claimed.
- */
-
-#define RC6_WORDSIZE 32
-#define RC6_P32 0xB7E15163L
-#define RC6_Q32 0x9E3779B9L
-
-unsigned int cRC6::rol(unsigned int v, unsigned int cnt)
-{
- cnt&=(RC6_WORDSIZE-1);
- return (v<<cnt) | (v>>(RC6_WORDSIZE-cnt));
-}
-
-unsigned int cRC6::ror(unsigned int v, unsigned int cnt)
-{
- cnt&=(RC6_WORDSIZE-1);
- return (v>>cnt) | (v<<(RC6_WORDSIZE-cnt));
-}
-
-void cRC6::SetKey(const unsigned char *Key, int len)
-{
- key[0]=RC6_P32;
- for(int v=1; v<RC6_MAX; v++) key[v]=key[v-1]+RC6_Q32;
- len/=4;
- unsigned int a=0, b=0, *l=AUTOARRAY(unsigned int,len);
- memcpy(l,Key,len*4);
- for(int i=0,j=0,v=3*(len>RC6_MAX ? len : RC6_MAX) ; v>0; v--) {
- a=key[i]=rol(key[i]+a+b,3);
- b= l[j]=rol( l[j]+a+b,a+b);
- i++; i%=RC6_MAX;
- j++; j%=len;
- }
-}
-
-void cRC6::Decrypt(unsigned char *data)
-{
- unsigned int *l=(unsigned int *)data;
- unsigned int a, b, c, d;
- a=l[0]-key[RC6_MAX-2];
- b=l[1];
- c=l[2]-key[RC6_MAX-1];
- d=l[3];
- for(int i=RC6_ROUNDS; i>0; i--) {
- unsigned int t=a;
- unsigned int u=b;
- a=d; d=c; b=t; c=u;
- u=rol((d*(2*d+1)),5);
- t=rol((b*(2*b+1)),5);
- c=ror(c-key[2*i+1],t)^u;
- a=ror(a-key[2*i ],u)^t;
- }
- l[0]=a;
- l[1]=b-key[0];
- l[2]=c;
- l[3]=d-key[1];
-}
-
// -- cTransponderTime ---------------------------------------------------------
class cTransponderTime : public cSimpleItem {
// ----------------------------------------------------------------
-#define RC6_ROUNDS 20
-#define RC6_MAX (RC6_ROUNDS*2+4)
-
-class cRC6 {
-private:
- unsigned int key[RC6_MAX];
- //
- unsigned int rol(unsigned int v, unsigned int cnt);
- unsigned int ror(unsigned int v, unsigned int cnt);
-public:
- void SetKey(const unsigned char *Key, int len);
- void Decrypt(unsigned char *data);
- };
-
-// ----------------------------------------------------------------
-
class cTPSDecrypt : private cAES, private cRC6 {
private:
static unsigned char *mem;