KVIrc  4.9.2
DeveloperAPIs
BlowFish.h
Go to the documentation of this file.
1 #ifndef _BLOWFISH_H_
2 #define _BLOWFISH_H_
3 //=============================================================================
4 //
5 // File : BlowFish.h
6 // Creation date : Wed Jan 13 2005 02:04:10 CEST by Szymon Stefanek
7 //
8 // This file is part of the KVIrc IRC client distribution
9 // Copyright (C) 2005-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 // This code is adapted from the MircCryption engine suite by Dark Raichu
29 // Original comments follow.
30 //
31 
32 //---------------------------------------------------------------------------
33 // The implementation of blowfish for CBC mode is from
34 // http://www.thecodeproject.com/cpp/blowfish.asp by George Anescu
35 // I removed exception handling, so it is callers responsibility to insure
36 // that strings are length multiples of 8 bytes
37 // -mouser 1/08/05
38 //---------------------------------------------------------------------------
39 
40 //
41 // BLOWFISH ENCRYPTION ALGORITHM
42 //
43 // Implementation of Bruce Schneier's BLOWFISH algorithm from "Applied
44 // Cryptography", Second Edition.
45 //
46 // Encryption and Decryption of Byte Strings using the Blowfish Encryption Algorithm.
47 // Blowfish is a block cipher that encrypts data in 8-byte blocks. The algorithm consists
48 // of two parts: a key-expansion part and a data-ancryption part. Key expansion converts a
49 // variable key of at least 1 and at most 56 bytes into several subkey arrays totaling
50 // 4168 bytes. Blowfish has 16 rounds. Each round consists of a key-dependent permutation,
51 // and a key and data-dependent substitution. All operations are XORs and additions on 32-bit words.
52 // The only additional operations are four indexed array data lookups per round.
53 // Blowfish uses a large number of subkeys. These keys must be precomputed before any data
54 // encryption or decryption. The P-array consists of 18 32-bit subkeys: P0, P1,...,P17.
55 // There are also four 32-bit S-boxes with 256 entries each: S0,0, S0,1,...,S0,255;
56 // S1,0, S1,1,...,S1,255; S2,0, S2,1,...,S2,255; S3,0, S3,1,...,S3,255;
57 //
58 // The Electronic Code Book (ECB), Cipher Block Chaining (CBC) and Cipher Feedback modes
59 // are used:
60 //
61 // In ECB mode if the same block is encrypted twice with the same key, the resulting
62 // ciphertext blocks are the same.
63 //
64 // In CBC Mode a ciphertext block is obtained by first xoring the
65 // plaintext block with the previous ciphertext block, and encrypting the resulting value.
66 //
67 // In CFB mode a ciphertext block is obtained by encrypting the previous ciphertext block
68 // and xoring the resulting value with the plaintext
69 //
70 // The previous ciphertext block is usually stored in an Initialization Vector (IV).
71 // An Initialization Vector of zero is commonly used for the first block, though other
72 // arrangements are also in use.
73 //
74 
75 #include "kvi_settings.h"
76 
77 #if defined(COMPILE_CRYPT_SUPPORT) || defined(Q_MOC_RUN)
78 
79 //Block Structure
80 struct SBlock
81 {
82  //Constructors
83  SBlock(unsigned int l = 0, unsigned int r = 0) : m_uil(l), m_uir(r) {}
84  //Copy Constructor
85  SBlock(const SBlock & roBlock) : m_uil(roBlock.m_uil), m_uir(roBlock.m_uir) {}
86  SBlock & operator^=(SBlock & b)
87  {
88  m_uil ^= b.m_uil;
89  m_uir ^= b.m_uir;
90  return *this;
91  }
92  unsigned int m_uil, m_uir;
93 };
94 
95 class BlowFish
96 {
97 public:
98  enum
99  {
100  ECB = 0,
101  CBC = 1,
102  CFB = 2
103  };
104 
105  //Constructor - Initialize the P and S boxes for a given Key
106  BlowFish(unsigned char * ucKey, unsigned int n, const SBlock & roChain = SBlock(0UL, 0UL));
107 
108  //Resetting the chaining block
109  void ResetChain() { m_oChain = m_oChain0; }
110 
111  // Encrypt/Decrypt Buffer in Place
112  void Encrypt(unsigned char * buf, unsigned int n, int iMode = ECB);
113  void Decrypt(unsigned char * buf, unsigned int n, int iMode = ECB);
114 
115  // Encrypt/Decrypt from Input Buffer to Output Buffer
116  void Encrypt(const unsigned char * in, unsigned char * out, unsigned int n, int iMode = ECB);
117  void Decrypt(const unsigned char * in, unsigned char * out, unsigned int n, int iMode = ECB);
118 
119  //Private Functions
120 private:
121  unsigned int F(unsigned int ui);
122  void Encrypt(SBlock &);
123  void Decrypt(SBlock &);
124 
125 private:
126  //The Initialization Vector, by default {0, 0}
127  SBlock m_oChain0;
128  SBlock m_oChain;
129  unsigned int m_auiP[18];
130  unsigned int m_auiS[4][256];
131  static const unsigned int scm_auiInitP[18];
132  static const unsigned int scm_auiInitS[4][256];
133 };
134 
135 //Extract low order byte
136 inline unsigned char Byte(unsigned int ui)
137 {
138  return (unsigned char)(ui & 0xff);
139 }
140 
141 //Function F
142 inline unsigned int BlowFish::F(unsigned int ui)
143 {
144  return ((m_auiS[0][Byte(ui >> 24)] + m_auiS[1][Byte(ui >> 16)]) ^ m_auiS[2][Byte(ui >> 8)]) + m_auiS[3][Byte(ui)];
145 }
146 
147 #endif //COMPILE_CRYPT_SUPPORT
148 
149 #endif
#define l
Definition: detector.cpp:76
#define n
Definition: detector.cpp:78
#define r
Definition: detector.cpp:82
This file contains compile time settings.