20 #include "runtime.hpp"
28 #define U32V(x) (word32)(x)
31 #ifdef BIG_ENDIAN_ORDER
32 #define LITTLE32(x) ByteReverse((word32)x)
34 #define LITTLE32(x) (x)
44 word32 RABBIT_g_func(word32 x)
54 h = (((U32V(a*a)>>17) + U32V(a*b))>>15) + b*b;
66 void Rabbit::NextState(RabbitCtx which)
69 word32 g[8], c_old[8],
i;
83 ctx->c[0] = U32V(ctx->c[0] + 0x4D34D34D + ctx->carry);
84 ctx->c[1] = U32V(ctx->c[1] + 0xD34D34D3 + (ctx->c[0] < c_old[0]));
85 ctx->c[2] = U32V(ctx->c[2] + 0x34D34D34 + (ctx->c[1] < c_old[1]));
86 ctx->c[3] = U32V(ctx->c[3] + 0x4D34D34D + (ctx->c[2] < c_old[2]));
87 ctx->c[4] = U32V(ctx->c[4] + 0xD34D34D3 + (ctx->c[3] < c_old[3]));
88 ctx->c[5] = U32V(ctx->c[5] + 0x34D34D34 + (ctx->c[4] < c_old[4]));
89 ctx->c[6] = U32V(ctx->c[6] + 0x4D34D34D + (ctx->c[5] < c_old[5]));
90 ctx->c[7] = U32V(ctx->c[7] + 0xD34D34D3 + (ctx->c[6] < c_old[6]));
91 ctx->carry = (ctx->c[7] < c_old[7]);
95 g[i] = RABBIT_g_func(U32V(ctx->x[i] + ctx->c[i]));
98 ctx->x[0] = U32V(g[0] + rotlFixed(g[7],16) + rotlFixed(g[6], 16));
99 ctx->x[1] = U32V(g[1] + rotlFixed(g[0], 8) + g[7]);
100 ctx->x[2] = U32V(g[2] + rotlFixed(g[1],16) + rotlFixed(g[0], 16));
101 ctx->x[3] = U32V(g[3] + rotlFixed(g[2], 8) + g[1]);
102 ctx->x[4] = U32V(g[4] + rotlFixed(g[3],16) + rotlFixed(g[2], 16));
103 ctx->x[5] = U32V(g[5] + rotlFixed(g[4], 8) + g[3]);
104 ctx->x[6] = U32V(g[6] + rotlFixed(g[5],16) + rotlFixed(g[4], 16));
105 ctx->x[7] = U32V(g[7] + rotlFixed(g[6], 8) + g[5]);
110 void Rabbit::SetIV(
const byte* iv)
113 word32 i0, i1, i2, i3,
i;
116 i0 = LITTLE32(*(word32*)(iv+0));
117 i2 = LITTLE32(*(word32*)(iv+4));
118 i1 = (i0>>16) | (i2&0xFFFF0000);
119 i3 = (i2<<16) | (i0&0x0000FFFF);
122 workCtx_.c[0] = masterCtx_.c[0] ^ i0;
123 workCtx_.c[1] = masterCtx_.c[1] ^ i1;
124 workCtx_.c[2] = masterCtx_.c[2] ^ i2;
125 workCtx_.c[3] = masterCtx_.c[3] ^ i3;
126 workCtx_.c[4] = masterCtx_.c[4] ^ i0;
127 workCtx_.c[5] = masterCtx_.c[5] ^ i1;
128 workCtx_.c[6] = masterCtx_.c[6] ^ i2;
129 workCtx_.c[7] = masterCtx_.c[7] ^ i3;
133 workCtx_.x[i] = masterCtx_.x[i];
134 workCtx_.carry = masterCtx_.carry;
143 void Rabbit::SetKey(
const byte* key,
const byte* iv)
146 word32 k0, k1, k2, k3,
i;
149 k0 = LITTLE32(*(word32*)(key+ 0));
150 k1 = LITTLE32(*(word32*)(key+ 4));
151 k2 = LITTLE32(*(word32*)(key+ 8));
152 k3 = LITTLE32(*(word32*)(key+12));
155 masterCtx_.x[0] = k0;
156 masterCtx_.x[2] = k1;
157 masterCtx_.x[4] = k2;
158 masterCtx_.x[6] = k3;
159 masterCtx_.x[1] = U32V(k3<<16) | (k2>>16);
160 masterCtx_.x[3] = U32V(k0<<16) | (k3>>16);
161 masterCtx_.x[5] = U32V(k1<<16) | (k0>>16);
162 masterCtx_.x[7] = U32V(k2<<16) | (k1>>16);
165 masterCtx_.c[0] = rotlFixed(k2, 16);
166 masterCtx_.c[2] = rotlFixed(k3, 16);
167 masterCtx_.c[4] = rotlFixed(k0, 16);
168 masterCtx_.c[6] = rotlFixed(k1, 16);
169 masterCtx_.c[1] = (k0&0xFFFF0000) | (k1&0xFFFF);
170 masterCtx_.c[3] = (k1&0xFFFF0000) | (k2&0xFFFF);
171 masterCtx_.c[5] = (k2&0xFFFF0000) | (k3&0xFFFF);
172 masterCtx_.c[7] = (k3&0xFFFF0000) | (k0&0xFFFF);
175 masterCtx_.carry = 0;
183 masterCtx_.c[i] ^= masterCtx_.x[(i+4)&0x7];
186 for (i=0; i<8; i++) {
187 workCtx_.x[
i] = masterCtx_.x[
i];
188 workCtx_.c[
i] = masterCtx_.c[
i];
190 workCtx_.carry = masterCtx_.carry;
197 void Rabbit::Process(byte* output,
const byte* input, word32 msglen)
204 while (msglen >= 16) {
209 *(word32*)(output+ 0) = *(word32*)(input+ 0) ^
210 LITTLE32(workCtx_.x[0] ^ (workCtx_.x[5]>>16) ^
211 U32V(workCtx_.x[3]<<16));
212 *(word32*)(output+ 4) = *(word32*)(input+ 4) ^
213 LITTLE32(workCtx_.x[2] ^ (workCtx_.x[7]>>16) ^
214 U32V(workCtx_.x[5]<<16));
215 *(word32*)(output+ 8) = *(word32*)(input+ 8) ^
216 LITTLE32(workCtx_.x[4] ^ (workCtx_.x[1]>>16) ^
217 U32V(workCtx_.x[7]<<16));
218 *(word32*)(output+12) = *(word32*)(input+12) ^
219 LITTLE32(workCtx_.x[6] ^ (workCtx_.x[3]>>16) ^
220 U32V(workCtx_.x[1]<<16));
234 *(word32*)(buffer+ 0) = LITTLE32(workCtx_.x[0] ^
235 (workCtx_.x[5]>>16) ^ U32V(workCtx_.x[3]<<16));
236 *(word32*)(buffer+ 4) = LITTLE32(workCtx_.x[2] ^
237 (workCtx_.x[7]>>16) ^ U32V(workCtx_.x[5]<<16));
238 *(word32*)(buffer+ 8) = LITTLE32(workCtx_.x[4] ^
239 (workCtx_.x[1]>>16) ^ U32V(workCtx_.x[7]<<16));
240 *(word32*)(buffer+12) = LITTLE32(workCtx_.x[6] ^
241 (workCtx_.x[3]>>16) ^ U32V(workCtx_.x[1]<<16));
244 for (i=0; i<msglen; i++)
245 output[i] = input[i] ^ buffer[i];