]> www.vanbest.org Git - sasc-ng.git/commitdiff
updated crypto algos (RC6/IDEA/AES)
authorleslie <unknown>
Sat, 23 Jul 2011 15:11:04 +0000 (17:11 +0200)
committerleslie <unknown>
Sat, 23 Jul 2011 15:11:04 +0000 (17:11 +0200)
crypto.c
crypto.h
systems/viaccess/tps.c
systems/viaccess/tps.h

index b5d9248a5b41d1797b535a1668b36a4023c82cb5..7fe8fefee11a9d6d56ef47170863be0381b3404e 100644 (file)
--- 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<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];
+}
index 1f618a69662f9fe75413cf501b8f4ea2118f21c9..5603b9e0b508046cce71e227954e9178e84874da 100644 (file)
--- 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
index d55a2f546a76f047ca84b820be9e1305dc245a9a..1c7896a00b0bb6a94210bb30ef272a4e433cab5c 100644 (file)
 #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 {
index a5574f23ee6029fafc285cc7e298dbaa20c45364..c8e40f8adbf6494aad0fef2ad20a5390589bac34 100644 (file)
@@ -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;