Main Page | Namespace List | Class List | File List | Namespace Members | Class Members | File Members

src/crypt.h

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * $Id: crypt.h,v 1.6 2005/11/29 20:15:46 pguio Exp $
00004  *
00005  * Copyright (c) Members of the EGEE Collaboration. 2004.
00006  * See http://eu-egee.org/partners/ for details on the copyright
00007  * holders.
00008  * For license conditions see the license file or
00009  * http://eu-egee.org/license.html
00010  * 
00011  *
00012  * Authors: Patrick Guio <patrick.guio@bccs.uib.no>
00013  *
00014  **************************************************************************/
00015 
00053 #ifndef CRYPT_H
00054 #define CRYPT_H 1
00055 
00056 #include <string>
00057 #include <iostream>
00058 #include <fstream>
00059 #include <sstream>
00060 #include <iomanip>
00061 #include <stdexcept>
00062 #include <vector>
00063 #include <limits>
00064 
00065 #include <openssl/bio.h>
00066 #include <openssl/evp.h>
00067 #include <openssl/err.h>
00068 #include <openssl/rand.h>
00069 
00070 namespace glite
00071 {
00072 
00074 
00082   class Crypt
00083   {
00084 
00085   public:
00086 
00088     typedef unsigned char byte;
00090     typedef std::vector<byte> Key;
00091 
00093     typedef std::string Base64Key;
00094 
00096     typedef std::vector<Key> SplitKeys;
00097 
00099     typedef unsigned short shortByte;
00101     typedef std::vector<shortByte> ShortKey;
00103     typedef std::vector<shortByte> Polynom;
00105     typedef std::vector<ShortKey > SplitShortKeys;
00107     typedef unsigned long longByte;
00109     typedef std::vector<longByte> LongKey;
00110 
00112     Crypt(const std::string cipher="bf-cbc", int keyLength=0);
00114     ~Crypt();
00115 
00117     void encrypt(byte *in, int isize, byte * &out, int &osize)                 const;
00119     void encrypt(const std::string &ifile, const std::string &ofile)           const;
00120 
00122     void decrypt(byte *in, int isize, byte * &out, int &osize)                 const;
00124     void decrypt(const std::string &ifile, const std::string &ofile)           const;
00125 
00127     void encodeBase64(const Key & key, Base64Key & b64key)                     const;
00129     void decodeBase64(const Base64Key & b64key, Key & key)                     const;
00130 
00132     void splitKeyTSS(const Key & key, int nShares, SplitKeys & keys)           const;
00134     void joinKeyTSS(const SplitKeys & keys, Key & key)                         const;
00135 
00137     void splitKeySSS(const Key & key, int nNeeded, int nShares,
00138                      Key & x, SplitShortKeys & keys)                           const;
00140     void joinKeySSS(const int nNeeded,
00141                     const Key & x, const SplitShortKeys & keys, Key & key)     const;
00142 
00144     void setKeyAndIV(const Key &key, const Key &iv=Key());
00146     Key  getKey()                                                              const;
00148     Key  getIV ()                                                              const;
00149 
00151     void setBufferSize(int size);
00153     int  getBufferSize() const;
00154 
00156     void setVerbose(int verbose);
00158     int  getVerbose() const;
00159 
00161     void printOn(std::ostream &os)                                            const;
00162 
00163   private:
00164 
00166     void seedPRNG(int bytes=1)                                                const;
00167 
00169     void drawKey();
00171     void drawIV();
00173     void initCrypters();
00174 
00176     void handleError(const char *thisFile, int thisLine)                      const;
00177 
00179     void drawPRN(Key & key)                                                   const;
00181     void drawPRN(Polynom & p)                                                 const;
00182 
00184     Polynom  initPolynom(int k, byte a0)                                      const;
00186     longByte evalPolynom(const Polynom & p, const longByte x)                 const;
00188     longByte inverseModulo(const longByte n)                                  const;
00190     longByte divideModulo(const longByte numerator,
00191                           const longByte denominator)                         const;
00193     void     evalLagrangeInterpAt0(const Key & x, LongKey & numerator,
00194                                    LongKey & denominator)                     const;
00195 
00196 
00197     const std::string m_cipherName; 
00198     unsigned int m_keyLength;       
00199     unsigned int m_ivLength;        
00200 
00201 #if 0
00202     // Do not use Engines since support is currently
00203     // OpenSSL <  0.9.6 not defined
00204     // OpenSSL == 0.9.6 definef struct engine
00205     // OpenSSL >  0.9.6 defines struct ENGINE
00206     ENGINE * m_impl;                
00207 #endif
00208 
00209     int m_bufferSize;               
00210 
00211     EVP_CIPHER_CTX * m_ectx;        
00212     EVP_CIPHER_CTX * m_dctx;        
00213 
00214     const EVP_CIPHER * m_type;      
00215 
00216     Key m_key;                      
00217     Key m_iv;                       
00218 
00219     int m_verbose;                  
00220 
00221   };
00222 
00224   std::ostream & operator<<(std::ostream &os, const Crypt & c);
00225 
00227   template<typename key_type>
00228   key_type Array2Key(const typename key_type::value_type * array, unsigned int length);
00230   template<typename key_type>
00231   typename key_type::value_type * Key2Array(const key_type & vec , unsigned int &length);
00232 
00233 
00235   template<typename key_type>
00236   struct outputKey
00237   {
00238     outputKey(const key_type & key, const char separator=0, int lineWidth=64);
00239     ~outputKey();
00240     key_type key_;
00241     char separator_;
00242     int lineWidth_;
00243   };
00244 
00246   template<typename key_type>
00247   std::ostream & operator<<(std::ostream &os, const outputKey<std::vector<key_type> > &k);
00249   std::ostream & operator<<(std::ostream &os, const outputKey<Crypt::Base64Key> &k);
00251   template<typename key_type>
00252   std::ostream & operator<<(std::ostream &os, const outputKey<std::vector<std::vector<key_type> > > &k);
00253 
00255   template<typename key_type>
00256   struct inputKey
00257   {
00258     inputKey(const char separator=0);
00259     ~inputKey();
00260     key_type key_;
00261     char separator_;
00262   };
00263 
00265   template<typename key_type>
00266   std::istream & operator>>(std::istream & is, inputKey<key_type> &k);
00267 
00268 
00270   struct setFormat
00271   {
00272     setFormat(std::ios::fmtflags base, int width=0, char padding=' ');
00273     ~setFormat();
00274     std::ios::fmtflags base_;
00275     int width_;
00276     char padding_;
00277   };
00278 
00280   std::istream & operator>>(std::istream& is, const setFormat &f);
00282   std::ostream & operator<<(std::ostream& os, const setFormat &f);
00283 
00284 }
00285 
00286 #endif // CRYPT_H

Generated on Thu May 11 18:54:33 2006 for Glite Security encrypted storage cpp by doxygen 1.3.5