MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
blowfish.hpp
1 /*
2  Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; see the file COPYING. If not, write to the
15  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
16  MA 02110-1301 USA.
17 */
18 
19 /* blowfish.hpp defines Blowfish
20 */
21 
22 
23 #ifndef TAO_CRYPT_BLOWFISH_HPP
24 #define TAO_CRYPT_BLOWFISH_HPP
25 
26 #include "misc.hpp"
27 #include "modes.hpp"
28 #ifdef USE_SYS_STL
29  #include <algorithm>
30 #else
31  #include "algorithm.hpp"
32 #endif
33 
34 
35 namespace STL = STL_NAMESPACE;
36 
37 
38 #if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
39  #define DO_BLOWFISH_ASM
40 #endif
41 
42 
43 namespace TaoCrypt {
44 
45 enum { BLOWFISH_BLOCK_SIZE = 8 };
46 
47 
48 // Blowfish encryption and decryption, see
49 class Blowfish : public Mode_BASE {
50 public:
51  enum { BLOCK_SIZE = BLOWFISH_BLOCK_SIZE, ROUNDS = 16 };
52 
53  Blowfish(CipherDir DIR, Mode MODE)
54  : Mode_BASE(BLOCK_SIZE, DIR, MODE), sbox_(pbox_ + ROUNDS + 2) {}
55 
56 #ifdef DO_BLOWFISH_ASM
57  void Process(byte*, const byte*, word32);
58 #endif
59  void SetKey(const byte* key, word32 sz, CipherDir fake = ENCRYPTION);
60  void SetIV(const byte* iv) { memcpy(r_, iv, BLOCK_SIZE); }
61 private:
62  static const word32 p_init_[ROUNDS + 2];
63  static const word32 s_init_[4 * 256];
64 
65  word32 pbox_[ROUNDS + 2 + 4 * 256];
66  word32* sbox_;
67 
68  void crypt_block(const word32 in[2], word32 out[2]) const;
69  void AsmProcess(const byte* in, byte* out) const;
70  void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
71 
72  Blowfish(const Blowfish&); // hide copy
73  Blowfish& operator=(const Blowfish&); // and assign
74 };
75 
76 
79 
82 
83 
84 
85 } // namespace
86 
87 #endif // TAO_CRYPT_BLOWFISH_HPP
88