KVIrc 5.2.4
Developer APIs
Rijndael.h
Go to the documentation of this file.
1#ifndef _RIJNDAEL_H_
2#define _RIJNDAEL_H_
3//=============================================================================
4//
5// File : Rijndael.h
6// Creation date : Sun Nov 5 2000 15:42:14 CEST by Szymon Stefanek
7//
8// This file is part of the KVIrc IRC client distribution
9// Copyright (C) 2000-2010 Szymon Stefanek (pragma at kvirc dot net)
10//
11// This program is FREE software. You can redistribute it and/or
12// modify it under the terms of the GNU General Public License
13// as published by the Free Software Foundation; either version 2
14// of the License, or (at your option) any later version.
15//
16// This program is distributed in the HOPE that it will be USEFUL,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19// See the GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program. If not, write to the Free Software Foundation,
23// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24//
25//=============================================================================
26
27//
28// Another implementation of the Rijndael cipher.
29// This is intended to be an easily usable library file.
30// Based on the Vincent Rijmen and K.U.Leuven implementation 2.4.
31//
32
33//
34// Original Copyright notice:
35//
36// rijndael-alg-fst.c v2.4 April '2000
37// rijndael-alg-fst.h
38// rijndael-api-fst.c
39// rijndael-api-fst.h
40//
41// Optimised ANSI C code
42//
43// authors: v1.0: Antoon Bosselaers
44// v2.0: Vincent Rijmen, K.U.Leuven
45// v2.3: Paulo Barreto
46// v2.4: Vincent Rijmen, K.U.Leuven
47//
48// This code is placed in the public domain.
49//
50
51//
52// This implementation works on 128, 192, 256 bit keys
53// and on 128 bit blocks
54//
55
56//
57// Example of usage:
58//
59// // Input data
60// unsigned char key[32]; // The key
61// initializeYour256BitKey(); // Obviously initialized with sth
62// const unsigned char * plainText = getYourPlainText(); // Your plain text
63// int plainTextLen = strlen(plainText); // Plain text length
64//
65// // Encrypting
66// Rijndael rin;
67// unsigned char output[plainTextLen + 16];
68//
69// rin.init(Rijndael::CBC,Rijndael::Encrypt,key,Rijndael::Key32Bytes);
70// // It is a good idea to check the error code
71// int len = rin.padEncrypt(plainText,len,output);
72// if(len >= 0)useYourEncryptedText();
73// else encryptError(len);
74//
75// // Decrypting: we can reuse the same object
76// unsigned char output2[len];
77// rin.init(Rijndael::ECB,Rijndael::Decrypt,keyMaterial,Rijndael::Key32Bytes));
78// len = rin.padDecrypt(output,len,output2);
79// if(len >= 0)useYourDecryptedText();
80// else decryptError(len);
81//
82
83#include "kvi_settings.h"
84
85#if defined(COMPILE_CRYPT_SUPPORT) || defined(Q_MOC_RUN)
86
87#define _MAX_KEY_COLUMNS (256 / 32)
88#define _MAX_ROUNDS 14
89//#define BITSPERBLOCK 128 /* Default number of bits in a cipher block */
90#define MAX_IV_SIZE 16
91#define STRICT_ALIGN 0
92
93// We assume that unsigned int is 32 bits long....
94typedef unsigned char UINT8;
95typedef unsigned int UINT32;
96typedef unsigned short UINT16;
97
98#define RIJNDAEL_SUCCESS 0
99#define RIJNDAEL_UNSUPPORTED_MODE -1
100#define RIJNDAEL_UNSUPPORTED_DIRECTION -2
101#define RIJNDAEL_UNSUPPORTED_KEY_LENGTH -3
102#define RIJNDAEL_BAD_KEY -4
103#define RIJNDAEL_NOT_INITIALIZED -5
104#define RIJNDAEL_BAD_DIRECTION -6
105#define RIJNDAEL_CORRUPTED_DATA -7
106
107class Rijndael
108{
109public:
110 enum Direction
111 {
112 Encrypt,
113 Decrypt
114 };
115 enum Mode
116 {
117 ECB,
118 CBC,
119 CFB1
120 };
121 enum KeyLength
122 {
123 Key16Bytes,
124 Key24Bytes,
125 Key32Bytes
126 };
127
128 Rijndael();
129 ~Rijndael();
130
131protected:
132 enum State
133 {
134 Valid,
135 Invalid
136 };
137
138 State m_state;
139 Mode m_mode;
140 Direction m_direction;
141 UINT8 m_initVector[MAX_IV_SIZE];
142 UINT32 m_uRounds;
143 UINT8 m_expandedKey[_MAX_ROUNDS + 1][4][4];
144
145public:
146 // Initializes the crypt session
147 // Returns RIJNDAEL_SUCCESS or an error code
148 int init(Mode mode, Direction dir, const UINT8 * key, KeyLength keyLen, UINT8 * initVector = nullptr);
149 // Input len is in BITS!
150 // Encrypts inputLen / 128 blocks of input and puts it in outBuffer
151 // outBuffer must be at least inputLen / 8 bytes long.
152 // Returns the encrypted buffer length in BITS or an error code < 0 in case of error
153 int blockEncrypt(const UINT8 * input, int inputLen, UINT8 * outBuffer, UINT8 * initVector = nullptr);
154 // Input len is in BYTES!
155 // outBuffer must be at least inputLen + 16 bytes long
156 // Returns the encrypted buffer length in BYTES or an error code < 0 in case of error
157 int padEncrypt(const UINT8 * input, int inputOctets, UINT8 * outBuffer, UINT8 * initVector = nullptr);
158 // Input len is in BITS!
159 // outBuffer must be at least inputLen / 8 bytes long
160 // Returns the decrypted buffer length in BITS and an error code < 0 in case of error
161 int blockDecrypt(const UINT8 * input, int inputLen, UINT8 * outBuffer, UINT8 * initVector = nullptr);
162 // Input len is in BYTES!
163 // outBuffer must be at least inputLen bytes long
164 // Returns the decrypted buffer length in BYTES and an error code < 0 in case of error
165 int padDecrypt(const UINT8 * input, int inputOctets, UINT8 * outBuffer, UINT8 * initVector = nullptr);
166
167protected:
168 void keySched(UINT8 key[_MAX_KEY_COLUMNS][4]);
169 void keyEncToDec();
170 void encrypt(const UINT8 a[16], UINT8 b[16]);
171 void decrypt(const UINT8 a[16], UINT8 b[16]);
172 void updateInitVector(UINT8 * initVector = nullptr);
173};
174
175#endif // COMPILE_CRYPT_SUPPORT
176
177#endif // _RIJNDAEL_H_
State
Definition NotifierSettings.h:62
#define a
Definition detector.cpp:92
This file contains compile time settings.
Mode
Definition KviOptions.h:607
int init()
Definition winamp.cpp:118