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
125 c->
k43[i] = (b->
k4[i>>4] <<4 | b->
k3 [i &15])<<8;
126 c->
k21[i] = b->
k2[i>>4] <<4 | b->
k1 [i &15];
134 x = c->
k87[x>>24 & 255] | c->
k65[x>>16 & 255]|
135 c->
k43[x>> 8 & 255] | c->
k21[x & 255];
137 return x<<11 | x>>(32-11);
143 n1 = in[0]|(in[1]<<8)|(in[2]<<16)|(in[3]<<24);
144 n2 = in[4]|(in[5]<<8)|(in[6]<<16)|(in[7]<<24);
147 n2 ^=
f(c,n1+c->
k[0]); n1 ^=
f(c,n2+c->
k[1]);
148 n2 ^=
f(c,n1+c->
k[2]); n1 ^=
f(c,n2+c->
k[3]);
149 n2 ^=
f(c,n1+c->
k[4]); n1 ^=
f(c,n2+c->
k[5]);
150 n2 ^=
f(c,n1+c->
k[6]); n1 ^=
f(c,n2+c->
k[7]);
152 n2 ^=
f(c,n1+c->
k[0]); n1 ^=
f(c,n2+c->
k[1]);
153 n2 ^=
f(c,n1+c->
k[2]); n1 ^=
f(c,n2+c->
k[3]);
154 n2 ^=
f(c,n1+c->
k[4]); n1 ^=
f(c,n2+c->
k[5]);
155 n2 ^=
f(c,n1+c->
k[6]); n1 ^=
f(c,n2+c->
k[7]);
157 n2 ^=
f(c,n1+c->
k[0]); n1 ^=
f(c,n2+c->
k[1]);
158 n2 ^=
f(c,n1+c->
k[2]); n1 ^=
f(c,n2+c->
k[3]);
159 n2 ^=
f(c,n1+c->
k[4]); n1 ^=
f(c,n2+c->
k[5]);
160 n2 ^=
f(c,n1+c->
k[6]); n1 ^=
f(c,n2+c->
k[7]);
162 n2 ^=
f(c,n1+c->
k[7]); n1 ^=
f(c,n2+c->
k[6]);
163 n2 ^=
f(c,n1+c->
k[5]); n1 ^=
f(c,n2+c->
k[4]);
164 n2 ^=
f(c,n1+c->
k[3]); n1 ^=
f(c,n2+c->
k[2]);
165 n2 ^=
f(c,n1+c->
k[1]); n1 ^=
f(c,n2+c->
k[0]);
167 out[0] = (
byte)(n2&0xff); out[1] = (
byte)((n2>>8)&0xff);
168 out[2] = (
byte)((n2>>16)&0xff); out[3]=(
byte)(n2>>24);
169 out[4] = (
byte)(n1&0xff); out[5] = (
byte)((n1>>8)&0xff);
170 out[6] = (
byte)((n1>>16)&0xff); out[7] = (
byte)(n1>>24);
176 n1 = in[0]|(in[1]<<8)|(in[2]<<16)|(in[3]<<24);
177 n2 = in[4]|(in[5]<<8)|(in[6]<<16)|(in[7]<<24);
179 n2 ^=
f(c,n1+c->
k[0]); n1 ^=
f(c,n2+c->
k[1]);
180 n2 ^=
f(c,n1+c->
k[2]); n1 ^=
f(c,n2+c->
k[3]);
181 n2 ^=
f(c,n1+c->
k[4]); n1 ^=
f(c,n2+c->
k[5]);
182 n2 ^=
f(c,n1+c->
k[6]); n1 ^=
f(c,n2+c->
k[7]);
184 n2 ^=
f(c,n1+c->
k[7]); n1 ^=
f(c,n2+c->
k[6]);
185 n2 ^=
f(c,n1+c->
k[5]); n1 ^=
f(c,n2+c->
k[4]);
186 n2 ^=
f(c,n1+c->
k[3]); n1 ^=
f(c,n2+c->
k[2]);
187 n2 ^=
f(c,n1+c->
k[1]); n1 ^=
f(c,n2+c->
k[0]);
189 n2 ^=
f(c,n1+c->
k[7]); n1 ^=
f(c,n2+c->
k[6]);
190 n2 ^=
f(c,n1+c->
k[5]); n1 ^=
f(c,n2+c->
k[4]);
191 n2 ^=
f(c,n1+c->
k[3]); n1 ^=
f(c,n2+c->
k[2]);
192 n2 ^=
f(c,n1+c->
k[1]); n1 ^=
f(c,n2+c->
k[0]);
194 n2 ^=
f(c,n1+c->
k[7]); n1 ^=
f(c,n2+c->
k[6]);
195 n2 ^=
f(c,n1+c->
k[5]); n1 ^=
f(c,n2+c->
k[4]);
196 n2 ^=
f(c,n1+c->
k[3]); n1 ^=
f(c,n2+c->
k[2]);
197 n2 ^=
f(c,n1+c->
k[1]); n1 ^=
f(c,n2+c->
k[0]);
199 out[0] = (
byte)(n2&0xff); out[1] = (
byte)((n2>>8)&0xff);
200 out[2] = (
byte)((n2>>16)&0xff); out[3]=(
byte)(n2>>24);
201 out[4] = (
byte)(n1&0xff); out[5] = (
byte)((n1>>8)&0xff);
202 out[6] = (
byte)((n1>>16)&0xff); out[7] = (
byte)(n1>>24);
209 for(i=0;i<blocks;i++)
220 for(i=0;i<blocks;i++)
237 for(i=0,in=clear,out=cipher;i<blocks;i++,in+=8,out+=8)
242 cur_iv[j]=out[j]=in[j]^gamma[j];
255 for(i=0,in=cipher,out=clear;i<blocks;i++,in+=8,out+=8)
260 out[j]=(cur_iv[j]=in[j])^gamma[j];
276 for(i=0,j=0;i<8;i++,j+=4)
278 c->
k[i]=k[j]|(k[j+1]<<8)|(k[j+2]<<16)|(k[j+3]<<24);
286 for(i=0,j=0;i<8;i++,j+=4)
288 k[j]=(
byte)(c->
k[i]& 0xFF);
289 k[j+1]=(
byte)((c->
k[i]>>8 )&0xFF);
290 k[j+2]=(
byte)((c->
k[i]>>16) &0xFF);
291 k[j+3]=(
byte)((c->
k[i]>>24) &0xFF);
308 int i;
for(i=0;i<8;i++) c->
k[i]=0;
326 n1 = buffer[0]|(buffer[1]<<8)|(buffer[2]<<16)|(buffer[3]<<24);
327 n2 = buffer[4]|(buffer[5]<<8)|(buffer[6]<<16)|(buffer[7]<<24);
330 n2 ^=
f(c,n1+c->
k[0]); n1 ^=
f(c,n2+c->
k[1]);
331 n2 ^=
f(c,n1+c->
k[2]); n1 ^=
f(c,n2+c->
k[3]);
332 n2 ^=
f(c,n1+c->
k[4]); n1 ^=
f(c,n2+c->
k[5]);
333 n2 ^=
f(c,n1+c->
k[6]); n1 ^=
f(c,n2+c->
k[7]);
335 n2 ^=
f(c,n1+c->
k[0]); n1 ^=
f(c,n2+c->
k[1]);
336 n2 ^=
f(c,n1+c->
k[2]); n1 ^=
f(c,n2+c->
k[3]);
337 n2 ^=
f(c,n1+c->
k[4]); n1 ^=
f(c,n2+c->
k[5]);
338 n2 ^=
f(c,n1+c->
k[6]); n1 ^=
f(c,n2+c->
k[7]);
340 buffer[0] = (
byte)(n1&0xff); buffer[1] = (
byte)((n1>>8)&0xff);
341 buffer[2] = (
byte)((n1>>16)&0xff); buffer[3] = (
byte)(n1>>24);
342 buffer[4] = (
byte)(n2&0xff); buffer[5] = (
byte)((n2>>8)&0xff);
343 buffer[6] = (
byte)((n2>>16)&0xff); buffer[7] = (
byte)(n2>>24);
349 int nbytes= nbits >> 3;
350 int rembits = nbits & 7;
351 int mask =rembits?((1<rembits)-1):0;
353 for (i=0;i<nbytes;i++) out[i]=buffer[i];
354 if (rembits) out[i]=buffer[i]&mask;
360 unsigned int data_len,
unsigned char *mac)
362 byte buffer[8]={0,0,0,0,0,0,0,0};
365 for (i=0;i+8<=data_len;i+=8)
370 memcpy(buf2,data+i,data_len-i);
379 unsigned int data_len,
unsigned char *mac)
384 memcpy (buffer,iv,8);
385 for (i=0;i+8<=data_len;i+=8)
390 memcpy(buf2,data+i,data_len-i);
400 unsigned char newkey[32],newiv[8];