00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00039 class CryptoSession {
00040
public:
00041
CryptoSession(
const string& pk);
00042
00043
private:
00044
00045
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 }
00311
00312
00313
#endif
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328