Crypto++  5.6.3
Free C++ class library of cryptographic schemes
tiger.cpp
1 // tiger.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "config.h"
5 
6 #include "tiger.h"
7 #include "misc.h"
8 #include "cpu.h"
9 
10 NAMESPACE_BEGIN(CryptoPP)
11 
12 void Tiger::InitState(HashWordType *state)
13 {
14  state[0] = W64LIT(0x0123456789ABCDEF);
15  state[1] = W64LIT(0xFEDCBA9876543210);
16  state[2] = W64LIT(0xF096A5B4C3B2E187);
17 }
18 
19 void Tiger::TruncatedFinal(byte *hash, size_t size)
20 {
21  ThrowIfInvalidTruncatedSize(size);
22 
23  PadLastBlock(56, 0x01);
24  CorrectEndianess(m_data, m_data, 56);
25 
26  m_data[7] = GetBitCountLo();
27 
28  Transform(m_state, m_data);
29  CorrectEndianess(m_state, m_state, DigestSize());
30  memcpy(hash, m_state, size);
31 
32  Restart(); // reinit for next use
33 }
34 
35 void Tiger::Transform (word64 *digest, const word64 *X)
36 {
37 #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32)
38  if (HasSSE2())
39  {
40 #ifdef __GNUC__
41  __asm__ __volatile__
42  (
43  INTEL_NOPREFIX
44  AS_PUSH_IF86(bx)
45 #else
46  #if _MSC_VER < 1300
47  const word64 *t = table;
48  AS2( mov edx, t)
49  #else
50  AS2( lea edx, [table])
51  #endif
52  AS2( mov eax, digest)
53  AS2( mov esi, X)
54 #endif
55  AS2( movq mm0, [eax])
56  AS2( movq mm1, [eax+1*8])
57  AS2( movq mm5, mm1)
58  AS2( movq mm2, [eax+2*8])
59  AS2( movq mm7, [edx+4*2048+0*8])
60  AS2( movq mm6, [edx+4*2048+1*8])
61  AS2( mov ecx, esp)
62  AS2( and esp, 0xfffffff0)
63  AS2( sub esp, 8*8)
64  AS_PUSH_IF86(cx)
65 
66 #define SSE2_round(a,b,c,x,mul) \
67  AS2( pxor c, [x])\
68  AS2( movd ecx, c)\
69  AS2( movzx edi, cl)\
70  AS2( movq mm3, [edx+0*2048+edi*8])\
71  AS2( movzx edi, ch)\
72  AS2( movq mm4, [edx+3*2048+edi*8])\
73  AS2( shr ecx, 16)\
74  AS2( movzx edi, cl)\
75  AS2( pxor mm3, [edx+1*2048+edi*8])\
76  AS2( movzx edi, ch)\
77  AS2( pxor mm4, [edx+2*2048+edi*8])\
78  AS3( pextrw ecx, c, 2)\
79  AS2( movzx edi, cl)\
80  AS2( pxor mm3, [edx+2*2048+edi*8])\
81  AS2( movzx edi, ch)\
82  AS2( pxor mm4, [edx+1*2048+edi*8])\
83  AS3( pextrw ecx, c, 3)\
84  AS2( movzx edi, cl)\
85  AS2( pxor mm3, [edx+3*2048+edi*8])\
86  AS2( psubq a, mm3)\
87  AS2( movzx edi, ch)\
88  AS2( pxor mm4, [edx+0*2048+edi*8])\
89  AS2( paddq b, mm4)\
90  SSE2_mul_##mul(b)
91 
92 #define SSE2_mul_5(b) \
93  AS2( movq mm3, b)\
94  AS2( psllq b, 2)\
95  AS2( paddq b, mm3)
96 
97 #define SSE2_mul_7(b) \
98  AS2( movq mm3, b)\
99  AS2( psllq b, 3)\
100  AS2( psubq b, mm3)
101 
102 #define SSE2_mul_9(b) \
103  AS2( movq mm3, b)\
104  AS2( psllq b, 3)\
105  AS2( paddq b, mm3)
106 
107 #define label2_5 1
108 #define label2_7 2
109 #define label2_9 3
110 
111 #define SSE2_pass(A,B,C,mul,X) \
112  AS2( xor ebx, ebx)\
113  ASL(mul)\
114  SSE2_round(A,B,C,X+0*8+ebx,mul)\
115  SSE2_round(B,C,A,X+1*8+ebx,mul)\
116  AS2( cmp ebx, 6*8)\
117  ASJ( je, label2_##mul, f)\
118  SSE2_round(C,A,B,X+2*8+ebx,mul)\
119  AS2( add ebx, 3*8)\
120  ASJ( jmp, mul, b)\
121  ASL(label2_##mul)
122 
123 #define SSE2_key_schedule(Y,X) \
124  AS2( movq mm3, [X+7*8])\
125  AS2( pxor mm3, mm6)\
126  AS2( movq mm4, [X+0*8])\
127  AS2( psubq mm4, mm3)\
128  AS2( movq [Y+0*8], mm4)\
129  AS2( pxor mm4, [X+1*8])\
130  AS2( movq mm3, mm4)\
131  AS2( movq [Y+1*8], mm4)\
132  AS2( paddq mm4, [X+2*8])\
133  AS2( pxor mm3, mm7)\
134  AS2( psllq mm3, 19)\
135  AS2( movq [Y+2*8], mm4)\
136  AS2( pxor mm3, mm4)\
137  AS2( movq mm4, [X+3*8])\
138  AS2( psubq mm4, mm3)\
139  AS2( movq [Y+3*8], mm4)\
140  AS2( pxor mm4, [X+4*8])\
141  AS2( movq mm3, mm4)\
142  AS2( movq [Y+4*8], mm4)\
143  AS2( paddq mm4, [X+5*8])\
144  AS2( pxor mm3, mm7)\
145  AS2( psrlq mm3, 23)\
146  AS2( movq [Y+5*8], mm4)\
147  AS2( pxor mm3, mm4)\
148  AS2( movq mm4, [X+6*8])\
149  AS2( psubq mm4, mm3)\
150  AS2( movq [Y+6*8], mm4)\
151  AS2( pxor mm4, [X+7*8])\
152  AS2( movq mm3, mm4)\
153  AS2( movq [Y+7*8], mm4)\
154  AS2( paddq mm4, [Y+0*8])\
155  AS2( pxor mm3, mm7)\
156  AS2( psllq mm3, 19)\
157  AS2( movq [Y+0*8], mm4)\
158  AS2( pxor mm3, mm4)\
159  AS2( movq mm4, [Y+1*8])\
160  AS2( psubq mm4, mm3)\
161  AS2( movq [Y+1*8], mm4)\
162  AS2( pxor mm4, [Y+2*8])\
163  AS2( movq mm3, mm4)\
164  AS2( movq [Y+2*8], mm4)\
165  AS2( paddq mm4, [Y+3*8])\
166  AS2( pxor mm3, mm7)\
167  AS2( psrlq mm3, 23)\
168  AS2( movq [Y+3*8], mm4)\
169  AS2( pxor mm3, mm4)\
170  AS2( movq mm4, [Y+4*8])\
171  AS2( psubq mm4, mm3)\
172  AS2( movq [Y+4*8], mm4)\
173  AS2( pxor mm4, [Y+5*8])\
174  AS2( movq [Y+5*8], mm4)\
175  AS2( paddq mm4, [Y+6*8])\
176  AS2( movq [Y+6*8], mm4)\
177  AS2( pxor mm4, [edx+4*2048+2*8])\
178  AS2( movq mm3, [Y+7*8])\
179  AS2( psubq mm3, mm4)\
180  AS2( movq [Y+7*8], mm3)
181 
182 #if CRYPTOPP_BOOL_X32
183  SSE2_pass(mm0, mm1, mm2, 5, esi)
184  SSE2_key_schedule(esp+8, esi)
185  SSE2_pass(mm2, mm0, mm1, 7, esp+8)
186  SSE2_key_schedule(esp+8, esp+8)
187  SSE2_pass(mm1, mm2, mm0, 9, esp+8)
188 #else
189  SSE2_pass(mm0, mm1, mm2, 5, esi)
190  SSE2_key_schedule(esp+4, esi)
191  SSE2_pass(mm2, mm0, mm1, 7, esp+4)
192  SSE2_key_schedule(esp+4, esp+4)
193  SSE2_pass(mm1, mm2, mm0, 9, esp+4)
194 #endif
195 
196  AS2( pxor mm0, [eax+0*8])
197  AS2( movq [eax+0*8], mm0)
198  AS2( psubq mm1, mm5)
199  AS2( movq [eax+1*8], mm1)
200  AS2( paddq mm2, [eax+2*8])
201  AS2( movq [eax+2*8], mm2)
202 
203  AS_POP_IF86(sp)
204  AS1( emms)
205 
206 #ifdef __GNUC__
207  AS_POP_IF86(bx)
208  ATT_PREFIX
209  :
210  : "a" (digest), "S" (X), "d" (table)
211  : "%ecx", "%edi", "memory", "cc"
212  );
213 #endif
214  }
215  else
216 #endif
217  {
218  word64 a = digest[0];
219  word64 b = digest[1];
220  word64 c = digest[2];
221  word64 Y[8];
222 
223 #define t1 (table)
224 #define t2 (table+256)
225 #define t3 (table+256*2)
226 #define t4 (table+256*3)
227 
228 #define round(a,b,c,x,mul) \
229  c ^= x; \
230  a -= t1[GETBYTE(c,0)] ^ t2[GETBYTE(c,2)] ^ t3[GETBYTE(c,4)] ^ t4[GETBYTE(c,6)]; \
231  b += t4[GETBYTE(c,1)] ^ t3[GETBYTE(c,3)] ^ t2[GETBYTE(c,5)] ^ t1[GETBYTE(c,7)]; \
232  b *= mul
233 
234 #define pass(a,b,c,mul,X) {\
235  int i=0;\
236  while (true)\
237  {\
238  round(a,b,c,X[i+0],mul); \
239  round(b,c,a,X[i+1],mul); \
240  if (i==6)\
241  break;\
242  round(c,a,b,X[i+2],mul); \
243  i+=3;\
244  }}
245 
246 #define key_schedule(Y,X) \
247  Y[0] = X[0] - (X[7]^W64LIT(0xA5A5A5A5A5A5A5A5)); \
248  Y[1] = X[1] ^ Y[0]; \
249  Y[2] = X[2] + Y[1]; \
250  Y[3] = X[3] - (Y[2] ^ ((~Y[1])<<19)); \
251  Y[4] = X[4] ^ Y[3]; \
252  Y[5] = X[5] + Y[4]; \
253  Y[6] = X[6] - (Y[5] ^ ((~Y[4])>>23)); \
254  Y[7] = X[7] ^ Y[6]; \
255  Y[0] += Y[7]; \
256  Y[1] -= Y[0] ^ ((~Y[7])<<19); \
257  Y[2] ^= Y[1]; \
258  Y[3] += Y[2]; \
259  Y[4] -= Y[3] ^ ((~Y[2])>>23); \
260  Y[5] ^= Y[4]; \
261  Y[6] += Y[5]; \
262  Y[7] -= Y[6] ^ W64LIT(0x0123456789ABCDEF)
263 
264  pass(a,b,c,5,X);
265  key_schedule(Y,X);
266  pass(c,a,b,7,Y);
267  key_schedule(Y,Y);
268  pass(b,c,a,9,Y);
269 
270  digest[0] = a ^ digest[0];
271  digest[1] = b - digest[1];
272  digest[2] = c + digest[2];
273  }
274 }
275 
276 NAMESPACE_END
Utility functions for the Crypto++ library.
Tiger
Definition: tiger.h:10
Library configuration file.
Functions for CPU features and intrinsics.
Crypto++ library namespace.
void TruncatedFinal(byte *hash, size_t size)
Computes the hash of the current message.
Definition: tiger.cpp:19