From: leslie Date: Sat, 23 Jul 2011 15:11:04 +0000 (+0200) Subject: updated crypto algos (RC6/IDEA/AES) X-Git-Tag: upstream/620~58 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=b3c05f01aebfbc1a3c718e0d91eec9c7744eddf2;p=sasc-ng.git updated crypto algos (RC6/IDEA/AES) --- diff --git a/crypto.c b/crypto.c index b5d9248..7fe8fef 100644 --- a/crypto.c +++ b/crypto.c @@ -149,6 +149,16 @@ int cIDEA::Encrypt(const unsigned char *data, int len, unsigned char *crypt, Ide 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 @@ -425,3 +435,82 @@ bool cAES::Decrypt(unsigned char *data, int len) const } return false; } + +bool cAES::Decrypt(const unsigned char *data, int len, unsigned char *decrypt) const +{ + if(active) { + for(int i=0; i 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<>(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; vRC6_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]; +} diff --git a/crypto.h b/crypto.h index 1f618a6..5603b9e 100644 --- a/crypto.h +++ b/crypto.h @@ -90,12 +90,12 @@ class cAES { 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); }; // ---------------------------------------------------------------- @@ -120,6 +120,7 @@ public: 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; }; // ---------------------------------------------------------------- @@ -134,4 +135,21 @@ public: 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 diff --git a/systems/viaccess/tps.c b/systems/viaccess/tps.c index d55a2f5..1c7896a 100644 --- a/systems/viaccess/tps.c +++ b/systems/viaccess/tps.c @@ -46,70 +46,6 @@ #include #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 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<>(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; vRC6_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 { diff --git a/systems/viaccess/tps.h b/systems/viaccess/tps.h index a5574f2..c8e40f8 100644 --- a/systems/viaccess/tps.h +++ b/systems/viaccess/tps.h @@ -33,22 +33,6 @@ class cST20; // ---------------------------------------------------------------- -#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;