KVIrc  4.9.2
DeveloperAPIs
libkvirijndael.h
Go to the documentation of this file.
1 #ifndef _LIBKVIRIJNDAEL_H_
2 #define _LIBKVIRIJNDAEL_H_
3 //=============================================================================
4 //
5 // File : libkvirijndael.h
6 // Creation date : Sat Now 4 2000 15:33:12 CEST by Szymon Stefanek
7 //
8 // This file is part of the KVIrc IRC client distribution
9 // Copyright (C) 2000 Till Bush (buti@geocities.com)
10 // Copyright (C) 2000-2010 Szymon Stefanek (pragma at kvirc dot net)
11 //
12 // This program is FREE software. You can redistribute it and/or
13 // modify it under the terms of the GNU General Public License
14 // as published by the Free Software Foundation; either version 2
15 // of the License, or (at your option) any later version.
16 //
17 // This program is distributed in the HOPE that it will be USEFUL,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 // See the GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program. If not, write to the Free Software Foundation,
24 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 //
26 //=============================================================================
27 
28 #include "kvi_settings.h"
29 #include "KviCString.h"
30 
31 #ifdef COMPILE_CRYPT_SUPPORT
32 
33 #include "KviCryptEngine.h"
34 #include "Rijndael.h"
35 
36 class KviRijndaelEngine : public KviCryptEngine
37 {
38  Q_OBJECT
39 public:
40  KviRijndaelEngine();
41  virtual ~KviRijndaelEngine();
42 
43 private:
44  enum OperationalMode
45  {
46  OldCBC = 1,
47  CBC = 2,
48  ECB = 3
49  };
50  Rijndael * m_pEncryptCipher;
51  Rijndael * m_pDecryptCipher;
52  OperationalMode m_bEncryptMode;
53  OperationalMode m_bDecryptMode;
54 
55 public:
56  virtual bool init(const char * encKey, int encKeyLen, const char * decKey, int decKeyLen);
57  virtual KviCryptEngine::EncryptResult encrypt(const char * plainText, KviCString & outBuffer);
58  virtual KviCryptEngine::DecryptResult decrypt(const char * inBuffer, KviCString & plainText);
59 
60 protected:
61  virtual bool binaryToAscii(const char *, int, KviCString &) { return false; };
62  virtual bool asciiToBinary(const char *, int *, char **) { return false; };
63  virtual int getKeyLen() { return 32; };
64  virtual Rijndael::KeyLength getKeyLenId() { return Rijndael::Key32Bytes; };
65 private:
66  void setLastErrorFromRijndaelErrorCode(int errCode);
67 };
68 
69 class KviRijndaelHexEngine : public KviRijndaelEngine
70 {
71  Q_OBJECT
72 public:
73  KviRijndaelHexEngine() : KviRijndaelEngine(){};
74  virtual ~KviRijndaelHexEngine(){};
75 
76 protected:
77  virtual bool binaryToAscii(const char * inBuffer, int len, KviCString & outBuffer);
78  virtual bool asciiToBinary(const char * inBuffer, int * len, char ** outBuffer);
79 };
80 
81 class KviRijndael128HexEngine : public KviRijndaelHexEngine
82 {
83  Q_OBJECT
84 public:
85  KviRijndael128HexEngine() : KviRijndaelHexEngine(){};
86  virtual ~KviRijndael128HexEngine(){};
87 
88 protected:
89  virtual int getKenLen() { return 16; };
90  virtual Rijndael::KeyLength getKeyLenId() { return Rijndael::Key16Bytes; };
91 };
92 
93 class KviRijndael192HexEngine : public KviRijndaelHexEngine
94 {
95  Q_OBJECT
96 public:
97  KviRijndael192HexEngine() : KviRijndaelHexEngine(){};
98  virtual ~KviRijndael192HexEngine(){};
99 
100 protected:
101  virtual int getKenLen() { return 24; };
102  virtual Rijndael::KeyLength getKeyLenId() { return Rijndael::Key24Bytes; };
103 };
104 
105 class KviRijndael256HexEngine : public KviRijndaelHexEngine
106 {
107  Q_OBJECT
108 public:
109  KviRijndael256HexEngine() : KviRijndaelHexEngine(){};
110  virtual ~KviRijndael256HexEngine(){};
111 
112 protected:
113  virtual int getKenLen() { return 32; };
114 };
115 
116 class KviRijndaelBase64Engine : public KviRijndaelEngine
117 {
118  Q_OBJECT
119 public:
120  KviRijndaelBase64Engine() : KviRijndaelEngine(){};
121  virtual ~KviRijndaelBase64Engine(){};
122 
123 protected:
124  virtual bool binaryToAscii(const char * inBuffer, int len, KviCString & outBuffer);
125  virtual bool asciiToBinary(const char * inBuffer, int * len, char ** outBuffer);
126 };
127 
128 class KviRijndael128Base64Engine : public KviRijndaelBase64Engine
129 {
130  Q_OBJECT
131 public:
132  KviRijndael128Base64Engine() : KviRijndaelBase64Engine(){};
133  virtual ~KviRijndael128Base64Engine(){};
134 
135 protected:
136  virtual int getKenLen() { return 16; };
137  virtual Rijndael::KeyLength getKeyLenId() { return Rijndael::Key16Bytes; };
138 };
139 
140 class KviRijndael192Base64Engine : public KviRijndaelBase64Engine
141 {
142  Q_OBJECT
143 public:
144  KviRijndael192Base64Engine() : KviRijndaelBase64Engine(){};
145  virtual ~KviRijndael192Base64Engine(){};
146 
147 protected:
148  virtual int getKenLen() { return 24; };
149  virtual Rijndael::KeyLength getKeyLenId() { return Rijndael::Key24Bytes; };
150 };
151 
152 class KviRijndael256Base64Engine : public KviRijndaelBase64Engine
153 {
154  Q_OBJECT
155 public:
156  KviRijndael256Base64Engine() : KviRijndaelBase64Engine(){};
157  virtual ~KviRijndael256Base64Engine(){};
158 
159 protected:
160  virtual int getKenLen() { return 32; };
161 };
162 
163 // Mircyption stuff
164 #define MCPS2_STARTTAG "\xABm\xAB"
165 #define MCPS2_ENDTAG "\xBBm\xBB"
166 
167 class KviMircryptionEngine : public KviCryptEngine
168 {
169  Q_OBJECT
170 public:
171  KviMircryptionEngine();
172  ~KviMircryptionEngine();
173 
174 protected:
175  KviCString m_szEncryptKey;
176  bool m_bEncryptCBC;
177  KviCString m_szDecryptKey;
178  bool m_bDecryptCBC;
179 
180 public:
181  virtual bool isCryptographicEngine() { return false; }; // we need to return false since it doesn't use the Qt::CTRL+P escape
182  virtual bool init(const char * encKey, int encKeyLen, const char * decKey, int decKeyLen);
183  virtual KviCryptEngine::EncryptResult encrypt(const char * plainText, KviCString & outBuffer);
184  virtual KviCryptEngine::DecryptResult decrypt(const char * inBuffer, KviCString & plainText);
185 
186 protected:
187  bool doDecryptECB(KviCString & encoded, KviCString & plain);
188  bool doDecryptCBC(KviCString & encoded, KviCString & plain);
189  bool doEncryptECB(KviCString & plain, KviCString & encoded);
190  bool doEncryptCBC(KviCString & plain, KviCString & encoded);
191 };
192 
193 #endif // COMPILE_CRYPT_SUPPORT
194 
195 #endif // _LIBKVIRIJNDAEL_H_
EncryptResult
Definition: KviCryptEngine.h:68
Definition: KviCString.h:105
DecryptResult
Definition: KviCryptEngine.h:75
Definition: KviCryptEngine.h:54
int init()
Definition: winamp.cpp:118
This file contains compile time settings.