cryptography.h

00001 // --*-c++-*-- 00002 /* 00003 $Id: cryptography_8h-source.html,v 1.1 2004/10/05 21:12:01 mentat Exp $ 00004 00005 GNU Messenger - The secure instant messenger 00006 Copyright (C) 2002-2004 Jesse Lovelace - jllovela@eos.ncsu.edu 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 00022 */ 00023 #ifndef GM_CRYPTOGRAPHY_H 00024 #define GM_CRYPTOGRAPHY_H 00025 00026 #include <string> 00027 #include "gm/exception.h" 00028 #include "gm/buffer.h" 00029 #include "gm/crypto_defs.h" 00030 #include "cryptopp/misc.h" 00031 #include "cryptopp/secblock.h" 00032 00033 namespace GNUMessenger { 00034 00035 using namespace std; 00036 using namespace CryptoPP; 00037 00038 //template<class CIPHER, class MODE> 00039 class CryptoSession { 00040 public: 00041 CryptoSession(const string& pk); 00042 00043 private: 00044 // CIPHER m_ciper; 00045 // MODE m_mode; 00046 }; 00047 00062 class CryptoManager { 00063 public: 00064 00066 class CryptoError: public Exception 00067 { 00068 public: 00069 CryptoError(const string& msg): Exception(msg) {} 00070 }; 00071 00073 class IOError: public CryptoError 00074 { 00075 public: 00076 IOError(const string& msg): CryptoError(msg) {} 00077 }; 00078 00080 class InvalidPassword: public CryptoError 00081 { 00082 public: 00083 InvalidPassword(const string& msg): CryptoError(msg) {} 00084 }; 00085 00087 class AuthFailed: public CryptoError 00088 { 00089 public: 00090 AuthFailed(const string& msg): CryptoError(msg) {} 00091 }; 00092 00094 class RNGError: public CryptoError 00095 { 00096 public: 00097 RNGError(const string& msg): CryptoError(msg) {} 00098 }; 00099 00101 class AlgoError: public CryptoError 00102 { 00103 public: 00104 AlgoError(const string& msg): CryptoError(msg) {} 00105 }; 00106 00108 class BlockSizeError: public CryptoError 00109 { 00110 public: 00111 BlockSizeError(const string& msg): CryptoError(msg) {} 00112 }; 00113 00115 class KeySizeError: public CryptoError 00116 { 00117 public: 00118 KeySizeError(const string& msg, const unsigned int min = 0, 00119 const unsigned int max = 0, 00120 const unsigned int mult = 0) 00121 : CryptoError(msg), m_max(max), m_min(min), m_mult(mult) {} 00122 unsigned int GetMax() { return m_max; } 00123 unsigned int GetMin() { return m_min; } 00124 unsigned int GetMult() { return m_mult; } 00125 00126 private: 00127 unsigned int m_max; 00128 unsigned int m_min; 00129 unsigned int m_mult; 00130 00131 }; 00132 00134 CryptoManager(const string& pk); 00135 00136 virtual ~CryptoManager(); 00137 00139 CryptoSession * createSession(); 00140 00149 static byte * hash(const string& str, unsigned int& len, 00150 CryptDefines::Hashes type = CryptDefines::DefaultHash) 00151 throw (AlgoError); 00152 00160 static string encode(const SecByteBlock& array, 00161 CryptDefines::Encoding enc = CryptDefines::DefaultEncoder) 00162 throw (AlgoError); 00163 00164 static string encode(const string& str, 00165 CryptDefines::Encoding enc = CryptDefines::DefaultEncoder) { 00166 SecByteBlock sec((const unsigned char *)str.c_str(), str.length()); 00167 return encode(sec, enc); 00168 } 00169 00177 static byte * decode(const string& input, unsigned int& outLen, 00178 CryptDefines::Encoding enc) throw (AlgoError); 00179 00180 00184 static string hashEncode(const string& str, 00185 CryptDefines::Hashes type = CryptDefines::DefaultHash, 00186 CryptDefines::Encoding enc = CryptDefines::DefaultEncoder) 00187 throw (AlgoError); 00188 00195 static byte * generateRandom(const unsigned int size) throw (RNGError); 00196 00211 static byte * encrypt(const SecByteBlock& data, 00212 const SecByteBlock& key, 00213 unsigned int& resultLen, 00214 const unsigned int blockSize = 0, 00215 CryptDefines::BlockCipher cipher = CryptDefines::DefaultBC, 00216 CryptDefines::Mode mode = CryptDefines::DefaultMode) 00217 throw (RNGError, AlgoError, KeySizeError, BlockSizeError); 00218 00237 static void encryptToFileWithHMAC(const string& filename, 00238 const SecByteBlock& data, 00239 const SecByteBlock& key, 00240 const unsigned int blockSize = 0, 00241 CryptDefines::BlockCipher bc = CryptDefines::DefaultBC, 00242 CryptDefines::Mode mode = CryptDefines::DefaultMode, 00243 CryptDefines::Hashes hash = CryptDefines::DefaultHash) 00244 throw (RNGError, AlgoError, KeySizeError, BlockSizeError, IOError); 00245 00253 static byte * compress(const SecByteBlock& data, 00254 unsigned int& resultLen, 00255 const unsigned int level = CryptDefines::DEFAULT_COMPRESS_LEVEL); 00256 00263 static byte * decompress(const SecByteBlock& data, 00264 unsigned int& resultLen); 00265 00273 static byte * generateIV(const SecByteBlock& data, 00274 CryptDefines::Hashes hash = CryptDefines::DefaultHash); 00275 00276 static unsigned int testRNG(const unsigned int insize) throw (RNGError); 00277 00278 static bool encryptFile(const string& filename, const VBuffer& key, 00279 const string& data) 00280 throw (IOError); 00281 00282 static string decryptFile(const string& filename, const VBuffer &key) 00283 throw (InvalidPassword, AuthFailed); 00284 00285 static string hashEncode(const VBuffer& toHash, 00286 CryptDefines::Hashes hash = CryptDefines::DefaultHash, 00287 CryptDefines::Encoding enc = CryptDefines::DefaultEncoder); 00288 00289 static VBuffer hash(const VBuffer& toHash, CryptDefines::Hashes hash = CryptDefines::DefaultHash); 00290 00291 00292 protected: 00299 static HashTransformation * getHash(CryptDefines::Hashes type) 00300 throw (AlgoError); 00301 00302 00303 static SymmetricCipher * getEncryptor( 00304 CryptDefines::BlockCipher cipher, 00305 CryptDefines::Mode mode) 00306 throw (AlgoError); 00307 }; 00308 00309 00310 } // !GNUMessenger 00311 00312 00313 #endif 00314 00315 /* 00316 ----- 00317 $Log: cryptography.h,v $ 00318 Revision 1.1.1.1 2004/10/03 06:17:37 mentat 00319 Initial re-import. 00320 00321 Revision 1.2 2003/04/09 21:18:30 mentat 00322 Adding new headers and pruning old. 00323 00324 Revision 1.1 2003/04/05 23:12:28 mentat 00325 Moving headers to a new dir. 00326 00327 00328 */

Generated on Tue Oct 5 14:41:47 2004 for GNU Messenger by doxygen 1.3.8