MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
md4.cpp
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 
20 /* based on Wei Dai's md4.cpp from CryptoPP */
21 
22 #include "runtime.hpp"
23 #include "md4.hpp"
24 #ifdef USE_SYS_STL
25  #include <algorithm>
26 #else
27  #include "algorithm.hpp"
28 #endif
29 
30 
31 namespace STL = STL_NAMESPACE;
32 
33 
34 namespace TaoCrypt {
35 
36 void MD4::Init()
37 {
38  digest_[0] = 0x67452301L;
39  digest_[1] = 0xefcdab89L;
40  digest_[2] = 0x98badcfeL;
41  digest_[3] = 0x10325476L;
42 
43  buffLen_ = 0;
44  loLen_ = 0;
45  hiLen_ = 0;
46 }
47 
48 
49 MD4::MD4(const MD4& that) : HASHwithTransform(DIGEST_SIZE / sizeof(word32),
50  BLOCK_SIZE)
51 {
52  buffLen_ = that.buffLen_;
53  loLen_ = that.loLen_;
54  hiLen_ = that.hiLen_;
55 
56  memcpy(digest_, that.digest_, DIGEST_SIZE);
57  memcpy(buffer_, that.buffer_, BLOCK_SIZE);
58 }
59 
60 MD4& MD4::operator= (const MD4& that)
61 {
62  MD4 tmp(that);
63  Swap(tmp);
64 
65  return *this;
66 }
67 
68 
69 void MD4::Swap(MD4& other)
70 {
71  STL::swap(loLen_, other.loLen_);
72  STL::swap(hiLen_, other.hiLen_);
73  STL::swap(buffLen_, other.buffLen_);
74 
75  memcpy(digest_, other.digest_, DIGEST_SIZE);
76  memcpy(buffer_, other.buffer_, BLOCK_SIZE);
77 }
78 
79 
80 void MD4::Transform()
81 {
82 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
83 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
84 #define H(x, y, z) ((x) ^ (y) ^ (z))
85 
86  word32 A, B, C, D;
87 
88  A = digest_[0];
89  B = digest_[1];
90  C = digest_[2];
91  D = digest_[3];
92 
93 #define function(a,b,c,d,k,s) a=rotlFixed(a+F(b,c,d)+buffer_[k],s);
94  function(A,B,C,D, 0, 3);
95  function(D,A,B,C, 1, 7);
96  function(C,D,A,B, 2,11);
97  function(B,C,D,A, 3,19);
98  function(A,B,C,D, 4, 3);
99  function(D,A,B,C, 5, 7);
100  function(C,D,A,B, 6,11);
101  function(B,C,D,A, 7,19);
102  function(A,B,C,D, 8, 3);
103  function(D,A,B,C, 9, 7);
104  function(C,D,A,B,10,11);
105  function(B,C,D,A,11,19);
106  function(A,B,C,D,12, 3);
107  function(D,A,B,C,13, 7);
108  function(C,D,A,B,14,11);
109  function(B,C,D,A,15,19);
110 
111 #undef function
112 #define function(a,b,c,d,k,s) a=rotlFixed(a+G(b,c,d)+buffer_[k]+0x5a827999,s);
113  function(A,B,C,D, 0, 3);
114  function(D,A,B,C, 4, 5);
115  function(C,D,A,B, 8, 9);
116  function(B,C,D,A,12,13);
117  function(A,B,C,D, 1, 3);
118  function(D,A,B,C, 5, 5);
119  function(C,D,A,B, 9, 9);
120  function(B,C,D,A,13,13);
121  function(A,B,C,D, 2, 3);
122  function(D,A,B,C, 6, 5);
123  function(C,D,A,B,10, 9);
124  function(B,C,D,A,14,13);
125  function(A,B,C,D, 3, 3);
126  function(D,A,B,C, 7, 5);
127  function(C,D,A,B,11, 9);
128  function(B,C,D,A,15,13);
129 
130 #undef function
131 #define function(a,b,c,d,k,s) a=rotlFixed(a+H(b,c,d)+buffer_[k]+0x6ed9eba1,s);
132  function(A,B,C,D, 0, 3);
133  function(D,A,B,C, 8, 9);
134  function(C,D,A,B, 4,11);
135  function(B,C,D,A,12,15);
136  function(A,B,C,D, 2, 3);
137  function(D,A,B,C,10, 9);
138  function(C,D,A,B, 6,11);
139  function(B,C,D,A,14,15);
140  function(A,B,C,D, 1, 3);
141  function(D,A,B,C, 9, 9);
142  function(C,D,A,B, 5,11);
143  function(B,C,D,A,13,15);
144  function(A,B,C,D, 3, 3);
145  function(D,A,B,C,11, 9);
146  function(C,D,A,B, 7,11);
147  function(B,C,D,A,15,15);
148 
149  digest_[0] += A;
150  digest_[1] += B;
151  digest_[2] += C;
152  digest_[3] += D;
153 }
154 
155 
156 } // namespace
157