SASC-NG
DVBSoftCAM

crypto.h

Go to the documentation of this file.
00001 /*
00002  * Softcam plugin to VDR (C++)
00003  *
00004  * This code is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This code is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
00018  */
00019 
00020 #ifndef ___CRYPTO_H
00021 #define ___CRYPTO_H
00022 
00023 //
00024 // NOTE: all crypto classes SHOULD be fully reentrant.
00025 //       They may be called from different threads concurrently.
00026 //
00027 // NOTE: cAES is not fully reentrant. Encrypt/Decrypt are reentrant
00028 //       (as AES_KEY is const there), but SetKey is not. Be carefull.
00029 //
00030 
00031 #define OPENSSL_ALGORITHM_DEFINES
00032 #include <openssl/opensslconf.h>
00033 #include <openssl/opensslv.h>
00034 
00035 #if OPENSSL_VERSION_NUMBER < 0x0090700fL
00036 #error Openssl version 0.9.7 or newer is strongly recomended
00037 #endif
00038 
00039 #include "crypto-bn.h"
00040 
00041 // ----------------------------------------------------------------
00042 
00043 #define DES_LEFT    0
00044 #define DES_RIGHT   1
00045 
00046 #define DES_HASH    8
00047 #define DES_PC1    16
00048 #define DES_IP     32
00049 #define DES_FP     64
00050 #define DES_MOD   128
00051 
00052 // common DES modes
00053 
00054 #define PRV_DES_ENCRYPT (DES_LEFT|DES_PC1|DES_IP|DES_FP)
00055 #define PRV_DES_DECRYPT (DES_RIGHT|DES_PC1|DES_IP|DES_FP)
00056 
00057 #define VIA_DES       (DES_LEFT|DES_MOD)
00058 #define VIA_DES_HASH  (DES_LEFT|DES_HASH|DES_MOD)
00059 #define SECA_DES_ENCR  PRV_DES_ENCRYPT
00060 #define SECA_DES_DECR  PRV_DES_DECRYPT
00061 #define NAGRA_DES_ENCR PRV_DES_ENCRYPT
00062 #define NAGRA_DES_DECR PRV_DES_DECRYPT
00063 
00064 class cDes {
00065 private:
00066   static const unsigned char _E[], _P[], _PC1[], _PC2[], IP[], FP[];
00067 protected:
00068   static const unsigned char S[][64], LS[];
00069   const unsigned char *PC1, *PC2;
00070   unsigned char E[48], P[32];
00071   //
00072   virtual unsigned int Mod(unsigned int R, unsigned int key7) const { return R; }
00073   void Permute(unsigned char *data, const unsigned char *P, int n) const;
00074 public:
00075   cDes(const unsigned char *pc1=0, const unsigned char *pc2=0);
00076   virtual ~cDes() {}
00077   void Des(unsigned char *data, const unsigned char *key, int mode) const;
00078   };
00079 
00080 // ----------------------------------------------------------------
00081 
00082 #if defined(OPENSSL_NO_AES) | defined(NO_AES) | OPENSSL_VERSION_NUMBER<0x0090700fL
00083 #include "support/aes.h"
00084 #else
00085 #define OPENSSL_HAS_AES
00086 #include <openssl/aes.h>
00087 #endif
00088 
00089 class cAES {
00090 private:
00091   bool active;
00092   AES_KEY dkey, ekey;
00093 public:
00094   cAES(void);
00095   void SetKey(const unsigned char *key);
00096   bool Decrypt(unsigned char *data, int len) const ;
00097   bool Decrypt(const unsigned char *data, int len, unsigned char *decrypt) const;
00098   int Encrypt(const unsigned char *data, int len, unsigned char *crypt) const;
00099   };
00100 
00101 // ----------------------------------------------------------------
00102 
00103 #if defined(OPENSSL_NO_IDEA) | defined(NO_IDEA) | OPENSSL_VERSION_NUMBER<0x0090700fL
00104 #include "support/idea.h"
00105 #else
00106 #define OPENSSL_HAS_IDEA
00107 #include <openssl/idea.h>
00108 #endif
00109 
00110 typedef IDEA_KEY_SCHEDULE IdeaKS;
00111 
00112 class cIDEA {
00113 public:
00114   // single shot API
00115   void Decrypt(unsigned char *data, int len, const unsigned char *key, unsigned char *iv) const;
00116   void Decrypt(const unsigned char *data, int len, unsigned char *decrypt, const unsigned char *key, unsigned char *iv) const;
00117   int Encrypt(const unsigned char *data, int len, unsigned char *crypt, const unsigned char *key, unsigned char *iv) const;
00118   // multi shot API
00119   void SetEncKey(const unsigned char *key, IdeaKS *ks) const;
00120   void SetDecKey(const unsigned char *key, IdeaKS *ks) const;
00121   void Decrypt(unsigned char *data, int len, IdeaKS *ks, unsigned char *iv) const;
00122   int Encrypt(const unsigned char *data, int len, unsigned char *crypt, IdeaKS *ks, unsigned char *iv) const;
00123   void EcbEncrypt(const unsigned char *data, int len, unsigned char *crypt, IdeaKS *ks) const;
00124   };
00125 
00126 // ----------------------------------------------------------------
00127 
00128 class cRSA {
00129 private:
00130   bool Input(cBN *d, const unsigned char *in, int n, bool LE) const;
00131   int Output(unsigned char *out, int n, cBN *r, bool LE) const;
00132 public:
00133   int RSA(unsigned char *out, const unsigned char *in, int n, const BIGNUM *exp, const BIGNUM *mod, bool LE=true) const;
00134   int RSA(BIGNUM *out, const unsigned char *in, int n, const BIGNUM *exp, const BIGNUM *mod, bool LE=true) const;
00135   int RSA(unsigned char *out, int len, BIGNUM *in, const BIGNUM *exp, const BIGNUM *mod, bool LE=true) const;
00136   };
00137 
00138 // ----------------------------------------------------------------
00139 
00140 #define RC6_ROUNDS      20
00141 #define RC6_MAX         (RC6_ROUNDS*2+4)
00142 
00143 class cRC6 {
00144 private:
00145   unsigned int key[RC6_MAX];
00146   //
00147   unsigned int rol(unsigned int v, unsigned int cnt);
00148   unsigned int ror(unsigned int v, unsigned int cnt);
00149 public:
00150   void SetKey(const unsigned char *Key, int len);
00151   void Decrypt(unsigned char *data);
00152   void Decrypt(const unsigned char *in, unsigned char *out);
00153   };
00154 
00155 #endif //___CRYPTO_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines