21 #include "runtime.hpp"
25 #if defined(TAOCRYPT_X86ASM_AVAILABLE) && defined(TAO_ASM)
32 void ARC4::SetKey(
const byte* key, word32 length)
39 for (i = 0; i < STATE_SIZE; i++)
42 word32 keyIndex = 0, stateIndex = 0;
44 for (i = 0; i < STATE_SIZE; i++) {
46 stateIndex += key[keyIndex] + a;
48 state_[
i] = state_[stateIndex];
49 state_[stateIndex] = a;
51 if (++keyIndex >= length)
60 inline unsigned int MakeByte(word32& x, word32& y, byte* s)
70 return s[(a+b) & 0xff];
77 void ARC4::Process(byte* out,
const byte* in, word32 length)
79 if (length == 0)
return;
83 AsmProcess(out, in, length);
88 byte *
const s = state_;
94 *out++ ^= MakeByte(x, y, s);
97 *out++ = *in++ ^ MakeByte(x, y, s);
109 __attribute__ ((noinline))
111 void ARC4::AsmProcess(byte* out,
const byte* in, word32 length)
114 #define AS1(x) asm(#x);
115 #define AS2(x, y) asm(#x ", " #y);
118 asm(".intel_syntax noprefix"); \
119 AS2( movd mm3, edi ) \
120 AS2( movd mm4, ebx ) \
121 AS2( movd mm5, esi ) \
122 AS2( movd mm6, ebp ) \
123 AS2( mov ecx, DWORD PTR [ebp + 8] ) \
124 AS2( mov edi, DWORD PTR [ebp + 12] ) \
125 AS2( mov esi, DWORD PTR [ebp + 16] ) \
126 AS2( mov ebp, DWORD PTR [ebp + 20] )
129 AS2( movd ebp, mm6 ) \
130 AS2( movd esi, mm5 ) \
131 AS2( movd ebx, mm4 ) \
132 AS2( mov esp, ebp ) \
133 AS2( movd edi, mm3 ) \
137 #define AS1(x) __asm x
138 #define AS2(x, y) __asm x, y
142 AS2( mov ebp, esp ) \
143 AS2( movd mm3, edi ) \
144 AS2( movd mm4, ebx ) \
145 AS2( movd mm5, esi ) \
146 AS2( movd mm6, ebp ) \
147 AS2( mov edi, DWORD PTR [ebp + 8] ) \
148 AS2( mov esi, DWORD PTR [ebp + 12] ) \
149 AS2( mov ebp, DWORD PTR [ebp + 16] )
152 AS2( movd ebp, mm6 ) \
153 AS2( movd esi, mm5 ) \
154 AS2( movd ebx, mm4 ) \
155 AS2( movd edi, mm3 ) \
156 AS2( mov esp, ebp ) \
170 AS2( mov [esp], ebp )
172 AS2( movzx edx, BYTE PTR [ecx + 1] )
173 AS2( lea ebp, [ecx + 2] )
174 AS2( movzx ecx, BYTE PTR [ecx] )
178 AS2( movzx eax, BYTE PTR [ebp + ecx] )
188 AS2( movzx ebx, BYTE PTR [ebp + edx] )
191 AS2( mov [ebp + ecx], bl )
194 AS2( mov [ebp + edx], al )
204 AS2( movzx ebx, BYTE PTR [ebp + eax] )
207 AS2( movzx eax, BYTE PTR [ebp + ecx] )
210 AS2( xor bl, BYTE PTR [esi] )
217 AS1( dec DWORD PTR [esp] )
222 AS2( mov [ebp - 2], cl )
223 AS2( mov [ebp - 1], dl )
232 #endif // DO_ARC4_ASM