MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
twofish.hpp
1 /*
2  Copyright (c) 2000, 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 /* twofish.hpp defines Twofish
20 */
21 
22 
23 #ifndef TAO_CRYPT_TWOFISH_HPP
24 #define TAO_CRYPT_TWOFISH_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_TWOFISH_ASM
40 #endif
41 
42 namespace TaoCrypt {
43 
44 enum { TWOFISH_BLOCK_SIZE = 16 };
45 
46 
47 // Twofish encryption and decryption, see
48 class Twofish : public Mode_BASE {
49 public:
50  enum { BLOCK_SIZE = TWOFISH_BLOCK_SIZE };
51 
52  Twofish(CipherDir DIR, Mode MODE)
53  : Mode_BASE(BLOCK_SIZE, DIR, MODE) {}
54 
55 #ifdef DO_TWOFISH_ASM
56  void Process(byte*, const byte*, word32);
57 #endif
58  void SetKey(const byte* key, word32 sz, CipherDir fake = ENCRYPTION);
59  void SetIV(const byte* iv) { memcpy(r_, iv, BLOCK_SIZE); }
60 private:
61  static const byte q_[2][256];
62  static const word32 mds_[4][256];
63 
64  word32 k_[40];
65  word32 s_[4][256];
66 
67  static word32 h0(word32 x, const word32 *key, unsigned int kLen);
68  static word32 h(word32 x, const word32 *key, unsigned int kLen);
69 
70  void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
71 
72  void encrypt(const byte*, const byte*, byte*) const;
73  void decrypt(const byte*, const byte*, byte*) const;
74 
75  void AsmEncrypt(const byte* inBlock, byte* outBlock) const;
76  void AsmDecrypt(const byte* inBlock, byte* outBlock) const;
77 
78  Twofish(const Twofish&); // hide copy
79  Twofish& operator=(const Twofish&); // and assign
80 };
81 
82 
85 
88 
89 
90 
91 } // naemspace
92 
93 #endif // TAO_CRYPT_TWOFISH_HPP
94