KVIrc 5.2.6
Developer APIs
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
36class KviRijndaelEngine : public KviCryptEngine
37{
38 Q_OBJECT
39public:
40 KviRijndaelEngine();
41 ~KviRijndaelEngine();
42
43private:
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
55public:
56 bool init(const char * encKey, int encKeyLen, const char * decKey, int decKeyLen) override;
57 KviCryptEngine::EncryptResult encrypt(const char * plainText, KviCString & outBuffer) override;
58 KviCryptEngine::DecryptResult decrypt(const char * inBuffer, KviCString & plainText) override;
59
60protected:
61 virtual bool binaryToAscii(const char *, int, KviCString &) { return false; }
62 virtual bool asciiToBinary(const char *, int *, char **) { return false; }
63 virtual int getKeyLen() const { return 32; }
64 virtual Rijndael::KeyLength getKeyLenId() const { return Rijndael::Key32Bytes; }
65private:
66 void setLastErrorFromRijndaelErrorCode(int errCode);
67};
68
69class KviRijndaelHexEngine : public KviRijndaelEngine
70{
71 Q_OBJECT
72public:
73 KviRijndaelHexEngine() : KviRijndaelEngine(){}
74 ~KviRijndaelHexEngine() = default;
75
76protected:
77 bool binaryToAscii(const char * inBuffer, int len, KviCString & outBuffer) override;
78 bool asciiToBinary(const char * inBuffer, int * len, char ** outBuffer) override;
79};
80
81class KviRijndael128HexEngine : public KviRijndaelHexEngine
82{
83 Q_OBJECT
84public:
85 KviRijndael128HexEngine() : KviRijndaelHexEngine(){}
86 ~KviRijndael128HexEngine() = default;
87
88protected:
89 int getKeyLen() const override { return 16; }
90 Rijndael::KeyLength getKeyLenId() const override { return Rijndael::Key16Bytes; }
91};
92
93class KviRijndael192HexEngine : public KviRijndaelHexEngine
94{
95 Q_OBJECT
96public:
97 KviRijndael192HexEngine() : KviRijndaelHexEngine(){}
98 ~KviRijndael192HexEngine() = default;
99
100protected:
101 int getKeyLen() const override { return 24; }
102 Rijndael::KeyLength getKeyLenId() const override { return Rijndael::Key24Bytes; }
103};
104
105class KviRijndael256HexEngine : public KviRijndaelHexEngine
106{
107 Q_OBJECT
108public:
109 KviRijndael256HexEngine() : KviRijndaelHexEngine(){}
110 ~KviRijndael256HexEngine() = default;
111
112protected:
113 int getKeyLen() const override { return 32; }
114};
115
116class KviRijndaelBase64Engine : public KviRijndaelEngine
117{
118 Q_OBJECT
119public:
120 KviRijndaelBase64Engine() : KviRijndaelEngine(){}
121 ~KviRijndaelBase64Engine() = default;
122
123protected:
124 bool binaryToAscii(const char * inBuffer, int len, KviCString & outBuffer) override;
125 bool asciiToBinary(const char * inBuffer, int * len, char ** outBuffer) override;
126};
127
128class KviRijndael128Base64Engine : public KviRijndaelBase64Engine
129{
130 Q_OBJECT
131public:
132 KviRijndael128Base64Engine() : KviRijndaelBase64Engine(){}
133 ~KviRijndael128Base64Engine() = default;
134
135protected:
136 int getKeyLen() const override { return 16; }
137 Rijndael::KeyLength getKeyLenId() const override { return Rijndael::Key16Bytes; }
138};
139
140class KviRijndael192Base64Engine : public KviRijndaelBase64Engine
141{
142 Q_OBJECT
143public:
144 KviRijndael192Base64Engine() : KviRijndaelBase64Engine(){}
145 ~KviRijndael192Base64Engine() = default;
146
147protected:
148 int getKeyLen() const override { return 24; }
149 Rijndael::KeyLength getKeyLenId() const override { return Rijndael::Key24Bytes; }
150};
151
152class KviRijndael256Base64Engine : public KviRijndaelBase64Engine
153{
154 Q_OBJECT
155public:
156 KviRijndael256Base64Engine() : KviRijndaelBase64Engine(){}
157 ~KviRijndael256Base64Engine() = default;
158
159protected:
160 int getKeyLen() const override { return 32; }
161};
162
163// Mircyption stuff
164#define MCPS2_STARTTAG "\xABm\xAB"
165#define MCPS2_ENDTAG "\xBBm\xBB"
166
167class KviMircryptionEngine : public KviCryptEngine
168{
169 Q_OBJECT
170public:
171 KviMircryptionEngine();
172 ~KviMircryptionEngine();
173
174protected:
175 KviCString m_szEncryptKey;
176 bool m_bEncryptCBC;
177 KviCString m_szDecryptKey;
178 bool m_bDecryptCBC;
179
180public:
181 bool init(const char * encKey, int encKeyLen, const char * decKey, int decKeyLen) override;
182 KviCryptEngine::EncryptResult encrypt(const char * plainText, KviCString & outBuffer) override;
183 KviCryptEngine::DecryptResult decrypt(const char * inBuffer, KviCString & plainText) override;
184
185protected:
186 bool doDecryptECB(KviCString & encoded, KviCString & plain);
187 bool doDecryptCBC(KviCString & encoded, KviCString & plain);
188 bool doEncryptECB(KviCString & plain, KviCString & encoded);
189 bool doEncryptCBC(KviCString & plain, KviCString & encoded);
190};
191
192#endif // COMPILE_CRYPT_SUPPORT
193
194#endif // _LIBKVIRIJNDAEL_H_
Definition KviCString.h:102
Definition KviCryptEngine.h:54
EncryptResult
Definition KviCryptEngine.h:68
DecryptResult
Definition KviCryptEngine.h:75
This file contains compile time settings.
int init()
Definition winamp.cpp:118