MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
des.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 /* des.hpp defines DES, DES_EDE2, and DES_EDE3
20  see FIPS 46-2 and FIPS 81
21 */
22 
23 
24 #ifndef TAO_CRYPT_DES_HPP
25 #define TAO_CRYPT_DES_HPP
26 
27 #include "misc.hpp"
28 #include "modes.hpp"
29 
30 
31 #if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
32  #define DO_DES_ASM
33 #endif
34 
35 
36 namespace TaoCrypt {
37 
38 
39 enum { DES_BLOCK_SIZE = 8, DES_KEY_SIZE = 32 };
40 
41 
42 class BasicDES {
43 public:
44  void SetKey(const byte*, word32, CipherDir dir);
45  void RawProcessBlock(word32&, word32&) const;
46 protected:
47  word32 k_[DES_KEY_SIZE];
48 };
49 
50 
51 // DES
52 class DES : public Mode_BASE, public BasicDES {
53 public:
54  DES(CipherDir DIR, Mode MODE)
55  : Mode_BASE(DES_BLOCK_SIZE, DIR, MODE) {}
56 
57 private:
58  void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
59 
60  DES(const DES&); // hide copy
61  DES& operator=(const DES&); // and assign
62 };
63 
64 
65 // DES_EDE2
66 class DES_EDE2 : public Mode_BASE {
67 public:
68  DES_EDE2(CipherDir DIR, Mode MODE)
69  : Mode_BASE(DES_BLOCK_SIZE, DIR, MODE) {}
70 
71  void SetKey(const byte*, word32, CipherDir dir);
72 private:
73  BasicDES des1_;
74  BasicDES des2_;
75 
76  void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
77 
78  DES_EDE2(const DES_EDE2&); // hide copy
79  DES_EDE2& operator=(const DES_EDE2&); // and assign
80 };
81 
82 
83 
84 // DES_EDE3
85 class DES_EDE3 : public Mode_BASE {
86 public:
87  DES_EDE3(CipherDir DIR, Mode MODE)
88  : Mode_BASE(DES_BLOCK_SIZE, DIR, MODE) {}
89 
90  void SetKey(const byte*, word32, CipherDir dir);
91  void SetIV(const byte* iv) { memcpy(r_, iv, DES_BLOCK_SIZE); }
92 #ifdef DO_DES_ASM
93  void Process(byte*, const byte*, word32);
94 #endif
95 private:
96  BasicDES des1_;
97  BasicDES des2_;
98  BasicDES des3_;
99 
100  void AsmProcess(const byte* in, byte* out, void* box) const;
101  void ProcessAndXorBlock(const byte*, const byte*, byte*) const;
102 
103  DES_EDE3(const DES_EDE3&); // hide copy
104  DES_EDE3& operator=(const DES_EDE3&); // and assign
105 };
106 
107 
110 
113 
116 
119 
122 
125 
126 
127 } // namespace
128 
129 
130 #endif // TAO_CRYPT_DES_HPP