ace.c 29.5 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7
/* Reference implementation of ACE-128, AEAD
 Written by:
 Kalikinkar Mandal <kmandal@uwaterloo.ca>
 */

#include "ace.h"

lwc-tester committed
8 9 10 11
/*
   *ace320: 16-round ace permutation of width 320 bits
   *state: input state, and output is stored in state again (inplace)
*/
lwc-tester committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
void ace320( u32 *state )
{
        u8 i, j, k;
        u32 t1, t2;
        
        __m128i x[10*PARAL_INST_BY4];
        __m128i xtmp, ytmp;
        
        //Packing state
        for ( k = 0; k < PARAL_INST_BY4; k++)
        {
        	xtmp = _mm_loadu_si128((void *) (state + 0+40*k));
        	ytmp = _mm_loadu_si128((void *) (state + 4+40*k));
        	x[10*k+0] = _mm_unpacklo_epi64(xtmp, ytmp);
        	x[10*k+4] = _mm_unpackhi_epi64(xtmp, ytmp);

        	xtmp = _mm_loadu_si128((void *) (state + 10+40*k));
        	ytmp = _mm_loadu_si128((void *) (state + 14+40*k));
        	x[10*k+1] = _mm_unpacklo_epi64(xtmp, ytmp);
        	x[10*k+5] = _mm_unpackhi_epi64(xtmp, ytmp);
        	
		xtmp = _mm_loadu_si128((void *) (state + 20+40*k));
        	ytmp = _mm_loadu_si128((void *) (state + 24+40*k));
        	x[10*k+2] = _mm_unpacklo_epi64(xtmp, ytmp);
        	x[10*k+6] = _mm_unpackhi_epi64(xtmp, ytmp);
		
		xtmp = _mm_loadu_si128((void *) (state + 30+40*k));
        	ytmp = _mm_loadu_si128((void *) (state + 34+40*k));
        	x[10*k+3] = _mm_unpacklo_epi64(xtmp, ytmp);
        	x[10*k+7] = _mm_unpackhi_epi64(xtmp, ytmp);
        	
		xtmp = _mm_loadu_si128((void *) (state + 8+40*k));
        	ytmp = _mm_loadu_si128((void *) (state + 18+40*k));
        	x[10*k+8] = _mm_unpacklo_epi64(xtmp, ytmp);

		xtmp = _mm_loadu_si128((void *) (state + 28+40*k));
        	ytmp = _mm_loadu_si128((void *) (state + 38+40*k));
        	x[10*k+9] = _mm_unpacklo_epi64(xtmp, ytmp);
        }
        
        for ( i = 0; i < NUMSTEPS; i++ )
        {
                
                for ( k = 0; k < PARAL_INST_BY4; k++ )
                {
                        //Chi
                        PACK_SSb(x[10*k],x[10*k+1]);
                        for ( j = 0; j < SIMECKROUND; j++ )
                        {
                                t1 = (u32)((RC0[i] >> j)&1);
                                t2 = (u32)((RC1[i] >> j)&1);
                                ROAX(x[10*k], x[10*k+1], t1, t2);
                        }
                        UNPACK_SSb(x[10*k],x[10*k+1]);
                        
			PACK_SSb(x[10*k+2],x[10*k+3]);
                        for ( j = 0; j < SIMECKROUND; j++ )
                        {
                                t1 = (u32)((RC0[i] >> j)&1);
                                t2 = (u32)((RC1[i] >> j)&1);
                                ROAX(x[10*k+2], x[10*k+3], t1, t2);
                        }
                        UNPACK_SSb(x[10*k+2],x[10*k+3]);
			
			PACK_SSb(x[10*k+8],x[10*k+9]);
                        for ( j = 0; j < SIMECKROUND; j++ )
                        {
                                t1 = (u32)((RC2[i] >> j)&1);
                                t2 = (u32)((RC2[i] >> j)&1);
                                ROAX(x[10*k+8], x[10*k+9], t1, t2);
                        }
                        UNPACK_SSb(x[10*k+8],x[10*k+9]);

                        //Diffussion
                        t1 = (u32)SC0[i];
                        t2 = (u32)SC1[i];
                        x[10*k+4] = x[10*k+4]^SC(t1, t2);
                        x[10*k+5] = x[10*k+5]^SC(t1, t2);
                        x[10*k+6] = x[10*k+6]^SC(t1, t2);
                        x[10*k+7] = x[10*k+7]^SC(t1, t2);

			xtmp = _mm_unpacklo_epi64(_mm_shuffle_epi32(x[10*k], _MM_SHUFFLE(1, 0, 3,2)), x[10*k+8]);
			x[10*k+4]^=xtmp;
			x[10*k+4] = _mm_shuffle_epi32(x[10*k+4], _MM_SHUFFLE(1, 0, 3,2));
			
			xtmp = _mm_unpackhi_epi64(x[10*k+1], x[10*k+8]);
			x[10*k+5]^=xtmp;
			x[10*k+5] = _mm_shuffle_epi32(x[10*k+5], _MM_SHUFFLE(1, 0, 3,2));
			
			xtmp = _mm_unpacklo_epi64(_mm_shuffle_epi32(x[10*k+2], _MM_SHUFFLE(1, 0, 3,2)), x[10*k+9]);
			x[10*k+6]^=xtmp;
			x[10*k+6] = _mm_shuffle_epi32(x[10*k+6], _MM_SHUFFLE(1, 0, 3,2));
			
			xtmp = _mm_unpackhi_epi64(x[10*k+3], x[10*k+9]);
			x[10*k+7]^=xtmp;
			x[10*k+7] = _mm_shuffle_epi32(x[10*k+7], _MM_SHUFFLE(1, 0, 3,2));
			
			x[10*k+8]^=(x[10*k]&masklo);
			x[10*k+8]^=(_mm_shuffle_epi32(x[10*k+1], _MM_SHUFFLE(1, 0, 3,2))&maskhi);
			x[10*k+8]^=SC((u32)SC2[i], (u32)SC2[i]);
			
			x[10*k+9]^=(x[10*k+2]&masklo);
			x[10*k+9]^=(_mm_shuffle_epi32(x[10*k+3], _MM_SHUFFLE(1, 0, 3,2))&maskhi);
			x[10*k+9]^=SC((u32)SC2[i], (u32)SC2[i]);


			xtmp = _mm_unpacklo_epi64(x[10*k+4], x[10*k]);
			ytmp = _mm_unpackhi_epi64(x[10*k], _mm_shuffle_epi32(x[10*k+8], _MM_SHUFFLE(1, 0, 3,2)));
			x[10*k] = xtmp;
			x[10*k+8] = x[10*k+8]&maskhi; 
			x[10*k+8]^=(_mm_shuffle_epi32(x[10*k+4], _MM_SHUFFLE(1, 0, 3,2))&masklo);
			x[10*k+4] = ytmp;

			xtmp = _mm_unpacklo_epi64(x[10*k+5], x[10*k+1]);
			ytmp = _mm_unpackhi_epi64(x[10*k+1], x[10*k+8]);
			x[10*k+1] = xtmp;
			x[10*k+8] = x[10*k+8]&masklo;
			x[10*k+8]^=x[10*k+5]&maskhi;
			x[10*k+5] = ytmp;

			xtmp = _mm_unpacklo_epi64(x[10*k+6], x[10*k+2]);
			ytmp = _mm_unpackhi_epi64(x[10*k+2], _mm_shuffle_epi32(x[10*k+9], _MM_SHUFFLE(1, 0, 3,2)));
			x[10*k+2] = xtmp;
			x[10*k+9] = x[10*k+9]&maskhi; 
			x[10*k+9]^=(_mm_shuffle_epi32(x[10*k+6], _MM_SHUFFLE(1, 0, 3,2))&masklo);
			x[10*k+6] = ytmp;

			xtmp = _mm_unpacklo_epi64(x[10*k+7], x[10*k+3]);
			ytmp = _mm_unpackhi_epi64(x[10*k+3], x[10*k+9]);
			x[10*k+3] = xtmp;
			x[10*k+9] = x[10*k+9]&masklo;
			x[10*k+9]^=x[10*k+7]&maskhi;
			x[10*k+7] = ytmp;
                }
        }
        //Unpack state
        for ( k = 0; k < PARAL_INST_BY4; k++)
        {
		xtmp = _mm_unpacklo_epi64(x[10*k], x[10*k+4]);
		ytmp = _mm_unpackhi_epi64(x[10*k], x[10*k+4]);
		_mm_storeu_si128((void *) (state + 0+40*k),xtmp);
		_mm_storeu_si128((void *) (state + 4 +40*k),ytmp);
		_mm_storeu_si128((void *) (state + 8 +40*k), x[10*k+8]);


		xtmp = _mm_unpacklo_epi64(x[10*k+1], x[10*k+5]);
		ytmp = _mm_unpackhi_epi64(x[10*k+1], x[10*k+5]);
		_mm_storeu_si128((void *) (state + 10+40*k),xtmp);
		_mm_storeu_si128((void *) (state + 14 +40*k),ytmp);
		_mm_storeu_si128((void *) (state + 18 +40*k), _mm_shuffle_epi32(x[10*k+8], _MM_SHUFFLE(1, 0, 3,2)));

		xtmp = _mm_unpacklo_epi64(x[10*k+2], x[10*k+6]);
		ytmp = _mm_unpackhi_epi64(x[10*k+2], x[10*k+6]);
		_mm_storeu_si128((void *) (state + 20+40*k),xtmp);
		_mm_storeu_si128((void *) (state + 24 +40*k),ytmp);
		_mm_storeu_si128((void *) (state + 28 +40*k), x[10*k+9]);
		
		xtmp = _mm_unpacklo_epi64(x[10*k+3], x[10*k+7]);
		ytmp = _mm_unpackhi_epi64(x[10*k+3], x[10*k+7]);
		_mm_storeu_si128((void *) (state + 30+40*k),xtmp);
		_mm_storeu_si128((void *) (state + 34 +40*k),ytmp);
		state[38 +40*k] = _mm_extract_epi32(x[10*k+9], 0);
		state[39 +40*k] = _mm_extract_epi32(x[10*k+9], 1);
        }
        return;
}

lwc-tester committed
179 180 181 182
/*
   *ace: 16-round ace permutation of width 320 bits
   *x: input state, and output state is stored in state (inplace)
*/
lwc-tester committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
void ace( __m128i *x )
{
        u8 i, j, k;
        u32 t1, t2;
        __m128i xtmp, ytmp;
        
        for ( i = 0; i < NUMSTEPS; i++ )
        {
                
                for ( k = 0; k < PARAL_INST_BY4; k++ )
                {
                        //Chi
                        PACK_SSb(x[10*k],x[10*k+1]);
                        for ( j = 0; j < SIMECKROUND; j++ )
                        {
                                t1 = (u32)((RC0[i] >> j)&1);
                                t2 = (u32)((RC1[i] >> j)&1);
                                ROAX(x[10*k], x[10*k+1], t1, t2);
                        }
                        UNPACK_SSb(x[10*k],x[10*k+1]);
                        
			PACK_SSb(x[10*k+2],x[10*k+3]);
                        for ( j = 0; j < SIMECKROUND; j++ )
                        {
                                t1 = (u32)((RC0[i] >> j)&1);
                                t2 = (u32)((RC1[i] >> j)&1);
                                ROAX(x[10*k+2], x[10*k+3], t1, t2);
                        }
                        UNPACK_SSb(x[10*k+2],x[10*k+3]);
			
			PACK_SSb(x[10*k+8],x[10*k+9]);
                        for ( j = 0; j < SIMECKROUND; j++ )
                        {
                                t1 = (u32)((RC2[i] >> j)&1);
                                t2 = (u32)((RC2[i] >> j)&1);
                                ROAX(x[10*k+8], x[10*k+9], t1, t2);
                        }
                        UNPACK_SSb(x[10*k+8],x[10*k+9]);

                        //Diffusion
                        t1 = (u32)SC0[i];
                        t2 = (u32)SC1[i];
                        x[10*k+4] = x[10*k+4]^SC(t1, t2);
                        x[10*k+5] = x[10*k+5]^SC(t1, t2);
                        x[10*k+6] = x[10*k+6]^SC(t1, t2);
                        x[10*k+7] = x[10*k+7]^SC(t1, t2);

			xtmp = _mm_unpacklo_epi64(_mm_shuffle_epi32(x[10*k], _MM_SHUFFLE(1, 0, 3,2)), x[10*k+8]);
			x[10*k+4]^=xtmp;
			x[10*k+4] = _mm_shuffle_epi32(x[10*k+4], _MM_SHUFFLE(1, 0, 3,2));
			
			xtmp = _mm_unpackhi_epi64(x[10*k+1], x[10*k+8]);
			x[10*k+5]^=xtmp;
			x[10*k+5] = _mm_shuffle_epi32(x[10*k+5], _MM_SHUFFLE(1, 0, 3,2));
			
			xtmp = _mm_unpacklo_epi64(_mm_shuffle_epi32(x[10*k+2], _MM_SHUFFLE(1, 0, 3,2)), x[10*k+9]);
			x[10*k+6]^=xtmp;
			x[10*k+6] = _mm_shuffle_epi32(x[10*k+6], _MM_SHUFFLE(1, 0, 3,2));
			
			xtmp = _mm_unpackhi_epi64(x[10*k+3], x[10*k+9]);
			x[10*k+7]^=xtmp;
			x[10*k+7] = _mm_shuffle_epi32(x[10*k+7], _MM_SHUFFLE(1, 0, 3,2));
			
			x[10*k+8]^=(x[10*k]&masklo);
			x[10*k+8]^=(_mm_shuffle_epi32(x[10*k+1], _MM_SHUFFLE(1, 0, 3,2))&maskhi);
			x[10*k+8]^=SC((u32)SC2[i], (u32)SC2[i]);
			
			x[10*k+9]^=(x[10*k+2]&masklo);
			x[10*k+9]^=(_mm_shuffle_epi32(x[10*k+3], _MM_SHUFFLE(1, 0, 3,2))&maskhi);
			x[10*k+9]^=SC((u32)SC2[i], (u32)SC2[i]);


			xtmp = _mm_unpacklo_epi64(x[10*k+4], x[10*k]);
			ytmp = _mm_unpackhi_epi64(x[10*k], _mm_shuffle_epi32(x[10*k+8], _MM_SHUFFLE(1, 0, 3,2)));
			x[10*k] = xtmp;
			x[10*k+8] = x[10*k+8]&maskhi; 
			x[10*k+8]^=(_mm_shuffle_epi32(x[10*k+4], _MM_SHUFFLE(1, 0, 3,2))&masklo);
			x[10*k+4] = ytmp;

			xtmp = _mm_unpacklo_epi64(x[10*k+5], x[10*k+1]);
			ytmp = _mm_unpackhi_epi64(x[10*k+1], x[10*k+8]);
			x[10*k+1] = xtmp;
			x[10*k+8] = x[10*k+8]&masklo;
			x[10*k+8]^=x[10*k+5]&maskhi;
			x[10*k+5] = ytmp;

			xtmp = _mm_unpacklo_epi64(x[10*k+6], x[10*k+2]);
			ytmp = _mm_unpackhi_epi64(x[10*k+2], _mm_shuffle_epi32(x[10*k+9], _MM_SHUFFLE(1, 0, 3,2)));
			x[10*k+2] = xtmp;
			x[10*k+9] = x[10*k+9]&maskhi; 
			x[10*k+9]^=(_mm_shuffle_epi32(x[10*k+6], _MM_SHUFFLE(1, 0, 3,2))&masklo);
			x[10*k+6] = ytmp;

			xtmp = _mm_unpacklo_epi64(x[10*k+7], x[10*k+3]);
			ytmp = _mm_unpackhi_epi64(x[10*k+3], x[10*k+9]);
			x[10*k+3] = xtmp;
			x[10*k+9] = x[10*k+9]&masklo;
			x[10*k+9]^=x[10*k+7]&maskhi;
			x[10*k+7] = ytmp;
                }
        }
        return;
}

lwc-tester committed
287 288 289 290 291 292 293 294 295 296 297 298 299
/*
   *crypto_aead_encrypt: encrypt plaintext and produce tag
   *klen: key length
   *npub: public nonce
   *k: key 
   *adlen: length of ad
   *ad: associate data
   *mlen: length of plaintext
   *m: plaintext to be encrypted
   *c: ciphertext
   *tlen: length of tag (in 32-bit word)
   *tag: tag
*/
lwc-tester committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
int crypto_aead_encrypt( u32 *tag, u32 tlen, u32 *c, u32 *m, u32 mlen, u32 *ad, u32 adlen, u8 *k, u8 *npub, u32 klen )
{
        u8 i;
        u64 l, mblock, adblock;
        __m128i x[10*PARAL_INST_BY4];
	u32 index1, index2;
	mblock = mlen/2;
	adblock = adlen/2;
       
	//Initialization: Loading Key & Nonce
        for ( i = 0; i < PARAL_INST_BY4; i++ )
	{
		//Key and Nonce are loaded according to Packing.
		x[10*i+0] = _mm_loadu_si128((void *) (k+0+64*i));
		x[10*i+4] = _mm_loadl_epi64 ((const __m128i *)(npub+0+64*i));
		
		x[10*i+1] = _mm_loadu_si128((void *) (k+16+64*i));
		x[10*i+5] = _mm_loadl_epi64 ((const __m128i*)(npub + 16+64*i));

		x[10*i+8] = _mm_set_epi8( *(npub+31+64*i), *(npub+30+64*i), *(npub+29+64*i), *(npub+28+64*i), *(npub+27+64*i), *(npub+26+64*i), *(npub+25+64*i), *(npub+24+64*i), *(npub+15+64*i), *(npub+14+64*i), *(npub+13+64*i), *(npub+12+64*i), *(npub+11+64*i), *(npub+10+64*i), *(npub+9+64*i), *(npub+8+64*i));


		x[10*i+2] = _mm_loadu_si128((void *) (k+32+64*i));
		x[10*i+6] = _mm_loadl_epi64 ((const __m128i*)(npub+32+64*i));
		
		x[10*i+3] = _mm_loadu_si128((void *) (k+48+64*i));
		x[10*i+7] = _mm_loadl_epi64 ((const __m128i*)(npub+48+64*i));

		x[10*i+9] = _mm_set_epi8( *(npub+63+64*i), *(npub+62+64*i), *(npub+61+64*i), *(npub+60+64*i), *(npub+59+64*i), *(npub+58+64*i), *(npub+57+64*i), *(npub+56+64*i), *(npub+47+64*i), *(npub+46+64*i), *(npub+45+64*i), *(npub+44+64*i), *(npub+43+64*i), *(npub+42+64*i), *(npub+41+64*i), *(npub+40+64*i));

	}
	ace(x);
lwc-tester committed
332
	//Absorbing first 64-bit key 
lwc-tester committed
333 334 335 336 337 338 339 340
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		x[10*i+0] = x[10*i+0]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+7+64*i), *(k+6+64*i), *(k+5+64*i), *(k+4+64*i), 0x0, 0x0, 0x0, 0x0, *(k+3+64*i), *(k+2+64*i), *(k+1+64*i), *(k+64*i));
		x[10*i+1] = x[10*i+1]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+23+64*i), *(k+22+64*i), *(k+21+64*i), *(k+20+64*i), 0x0, 0x0, 0x0, 0x0, *(k+19+64*i), *(k+18+64*i), *(k+17+64*i), *(k+16+64*i));
		x[10*i+2] = x[10*i+2]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+39+64*i), *(k+38+64*i), *(k+37+64*i), *(k+36+64*i), 0x0, 0x0, 0x0, 0x0, *(k+35+64*i), *(k+34+64*i), *(k+33+64*i), *(k+32+64*i));
		x[10*i+3] = x[10*i+3]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+55+64*i), *(k+54+64*i), *(k+53+64*i), *(k+52+64*i), 0x0, 0x0, 0x0, 0x0, *(k+51+64*i), *(k+50+64*i), *(k+49+64*i), *(k+48+64*i));
	}
	ace(x);
lwc-tester committed
341
	//Absorbing last 64-bit key 
lwc-tester committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		x[10*i+0] = x[10*i+0]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+15+64*i), *(k+14+64*i), *(k+13+64*i), *(k+12+64*i), 0x0, 0x0, 0x0, 0x0, *(k+11+64*i), *(k+10+64*i), *(k+9+64*i), *(k+8+64*i));
		x[10*i+1] = x[10*i+1]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+31+64*i), *(k+30+64*i), *(k+29+64*i), *(k+28+64*i), 0x0, 0x0, 0x0, 0x0, *(k+27+64*i), *(k+26+64*i), *(k+25+64*i), *(k+24+64*i));
		x[10*i+2] = x[10*i+2]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+47+64*i), *(k+46+64*i), *(k+45+64*i), *(k+44+64*i), 0x0, 0x0, 0x0, 0x0, *(k+43+64*i), *(k+42+64*i), *(k+41+64*i), *(k+40+64*i));
		x[10*i+3] = x[10*i+3]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+63+64*i), *(k+62+64*i), *(k+61+64*i), *(k+60+64*i), 0x0, 0x0, 0x0, 0x0, *(k+59+64*i), *(k+58+64*i), *(k+57+64*i), *(k+56+64*i));
	}
	ace(x);

	//Proceesing Associated Data
	for ( l = 0; l < adblock; l++ )
	{
		for ( i = 0; i < PARAL_INST_BY4; i++)
		{
			index1 = 2*l + adlen*4*i;
			index2 = 2*l + adlen*4*i+1;
			x[10*i+0] = x[10*i+0]^_mm_set_epi32(0x0, ad[index2], 0x0, ad[index1]);
			
			index1 = 2*l + adlen*4*i+ adlen;
			index2 = 2*l + adlen*4*i+ adlen+1;
			x[10*i+1] = x[10*i+1]^_mm_set_epi32(0x0, ad[index2], 0x0, ad[index1]);
			
			index1 = 2*l + adlen*4*i+ 2*adlen;
			index2 = 2*l + adlen*4*i+ 2*adlen+1;
			x[10*i+2] = x[10*i+2]^_mm_set_epi32(0x0, ad[index2], 0x0, ad[index1]);
			
			index1 = 2*l + adlen*4*i+ 3*adlen;
			index2 = 2*l + adlen*4*i+ 3*adlen+1;
			x[10*i+3] = x[10*i+3]^_mm_set_epi32(0x0, ad[index2], 0x0, ad[index1]);

			//Domain seperator
lwc-tester committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 1)^0x00000001, 1 );
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 3)^0x00000001, 3 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 1)^0x00000001, 1 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 3)^0x00000001, 3 );
		}
		ace(x);
	}
	if (adlen%2 == 0 && adlen != 0 )
	{
		for ( i = 0; i < PARAL_INST_BY4; i++)
		{
			//Padding: 10*
			x[10*i+0] = x[10*i+0]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+1] = x[10*i+1]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+2] = x[10*i+2]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+3] = x[10*i+3]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			//Domain seperator
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 1)^0x00000001, 1 );
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 3)^0x00000001, 3 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 1)^0x00000001, 1 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 3)^0x00000001, 3 );
lwc-tester committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
		}
		ace(x);
	}
	//Encrypt plaintext
	for ( l = 0; l < mblock; l++ )
	{
		for ( i = 0; i < PARAL_INST_BY4; i++)
		{
			index1 = 2*l + mlen*4*i;
			index2 = 2*l + mlen*4*i+1;
			c[index1] = _mm_extract_epi32 (x[10*i+0], 0)^m[index1];
			c[index2] = _mm_extract_epi32 (x[10*i+0], 2)^m[index2];
			x[10*i+0] = x[10*i+0]^_mm_set_epi32(0x0, m[index2], 0x0, m[index1]);
			
			index1 = 2*l + mlen*4*i+ mlen;
			index2 = 2*l + mlen*4*i+ mlen+1;
			c[index1] = _mm_extract_epi32 (x[10*i+1], 0)^m[index1];
			c[index2] = _mm_extract_epi32 (x[10*i+1], 2)^m[index2];
			x[10*i+1] = x[10*i+1]^_mm_set_epi32(0x0, m[index2], 0x0, m[index1]);
			
			index1 = 2*l + mlen*4*i+ 2*mlen;
			index2 = 2*l + mlen*4*i+ 2*mlen+1;
			c[index1] = _mm_extract_epi32 (x[10*i+2], 0)^m[index1];
			c[index2] = _mm_extract_epi32 (x[10*i+2], 2)^m[index2];
			x[10*i+2] = x[10*i+2]^_mm_set_epi32(0x0, m[index2], 0x0, m[index1]);
			
			index1 = 2*l + mlen*4*i+ 3*mlen;
			index2 = 2*l + mlen*4*i+ 3*mlen+1;
			c[index1] = _mm_extract_epi32 (x[10*i+3], 0)^m[index1];
			c[index2] = _mm_extract_epi32 (x[10*i+3], 2)^m[index2];
			x[10*i+3] = x[10*i+3]^_mm_set_epi32(0x0, m[index2], 0x0, m[index1]);

			//Domain seperator
lwc-tester committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 1)^0x00000002, 1 );
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 3)^0x00000002, 3 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 1)^0x00000002, 1 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 3)^0x00000002, 3 );
		}
		ace(x);
	}
	if ( mlen%2 == 0 )
	{
		for ( i = 0; i < PARAL_INST_BY4; i++)
		{
			//Padding: 10*
			x[10*i+0] = x[10*i+0]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+1] = x[10*i+1]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+2] = x[10*i+2]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+3] = x[10*i+3]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			//Domain seperator
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 1)^0x00000002, 1 );
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 3)^0x00000002, 3 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 1)^0x00000002, 1 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 3)^0x00000002, 3 );
lwc-tester committed
448 449 450 451
		}
		ace(x);
	}

lwc-tester committed
452 453
	//Tag generation phase
	//Absorbing first 64-bit key 
lwc-tester committed
454 455 456 457 458 459 460 461
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		x[10*i+0] = x[10*i+0]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+7+64*i), *(k+6+64*i), *(k+5+64*i), *(k+4+64*i), 0x0, 0x0, 0x0, 0x0, *(k+3+64*i), *(k+2+64*i), *(k+1+64*i), *(k+64*i));
		x[10*i+1] = x[10*i+1]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+23+64*i), *(k+22+64*i), *(k+21+64*i), *(k+20+64*i), 0x0, 0x0, 0x0, 0x0, *(k+19+64*i), *(k+18+64*i), *(k+17+64*i), *(k+16+64*i));
		x[10*i+2] = x[10*i+2]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+39+64*i), *(k+38+64*i), *(k+37+64*i), *(k+36+64*i), 0x0, 0x0, 0x0, 0x0, *(k+35+64*i), *(k+34+64*i), *(k+33+64*i), *(k+32+64*i));
		x[10*i+3] = x[10*i+3]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+55+64*i), *(k+54+64*i), *(k+53+64*i), *(k+52+64*i), 0x0, 0x0, 0x0, 0x0, *(k+51+64*i), *(k+50+64*i), *(k+49+64*i), *(k+48+64*i));
	}
	ace(x);
lwc-tester committed
462
	//Absorbing last 64-bit key 
lwc-tester committed
463 464 465 466 467 468 469 470
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		x[10*i+0] = x[10*i+0]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+15+64*i), *(k+14+64*i), *(k+13+64*i), *(k+12+64*i), 0x0, 0x0, 0x0, 0x0, *(k+11+64*i), *(k+10+64*i), *(k+9+64*i), *(k+8+64*i));
		x[10*i+1] = x[10*i+1]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+31+64*i), *(k+30+64*i), *(k+29+64*i), *(k+28+64*i), 0x0, 0x0, 0x0, 0x0, *(k+27+64*i), *(k+26+64*i), *(k+25+64*i), *(k+24+64*i));
		x[10*i+2] = x[10*i+2]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+47+64*i), *(k+46+64*i), *(k+45+64*i), *(k+44+64*i), 0x0, 0x0, 0x0, 0x0, *(k+43+64*i), *(k+42+64*i), *(k+41+64*i), *(k+40+64*i));
		x[10*i+3] = x[10*i+3]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+63+64*i), *(k+62+64*i), *(k+61+64*i), *(k+60+64*i), 0x0, 0x0, 0x0, 0x0, *(k+59+64*i), *(k+58+64*i), *(k+57+64*i), *(k+56+64*i));
	}
	ace(x);
lwc-tester committed
471
	//Extracting tag
lwc-tester committed
472 473 474 475 476 477 478 479 480 481
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		_mm_storeu_si128((void *) (tag+16*i), x[10*i]);
		_mm_storeu_si128((void *) (tag+4+16*i), x[10*i+1]);
		_mm_storeu_si128((void *) (tag+8+16*i), x[10*i+2]);
		_mm_storeu_si128((void *) (tag+12+16*i), x[10*i+3]);
	}
return 0;
}

lwc-tester committed
482 483 484 485 486 487 488 489 490 491 492 493 494
/*
   *crypto_aead_decrypt: decrypt ciphertext
   *klen: key length
   *npub: public nonce
   *k: key 
   *adlen: length of ad
   *ad: associate data
   *tlen: length of tag (in 32-bit word)
   *tag: tag
   *mlen: length of ciphertext
   *c: ciphertext
   *m: plaintext to be encrypted
*/
lwc-tester committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
int crypto_aead_decrypt( u32 *m, u32 *c, u32 mlen, u32 *tag, u32 tlen, u32 *ad, u32 adlen, u8 *k, u8 *npub, u32 klen )
{
        u8 i;
        u64 l, mblock, adblock;
        __m128i x[10*PARAL_INST_BY4];
	u32 index1, index2;

	mblock = mlen/2;
	adblock = adlen/2;
       
	//Initialization: Loading Key & Nonce
        for ( i = 0; i < PARAL_INST_BY4; i++ )
	{
		//Key and Nonce are loaded according to Packing.
		x[10*i+0] = _mm_loadu_si128((void *) (k+0+64*i));
		x[10*i+4] = _mm_loadl_epi64 ((const __m128i *)(npub+0+64*i));
		
		x[10*i+1] = _mm_loadu_si128((void *) (k+16+64*i));
		x[10*i+5] = _mm_loadl_epi64 ((const __m128i*)(npub + 16+64*i));

		x[10*i+8] = _mm_set_epi8( *(npub+31+64*i), *(npub+30+64*i), *(npub+29+64*i), *(npub+28+64*i), *(npub+27+64*i), *(npub+26+64*i), *(npub+25+64*i), *(npub+24+64*i), *(npub+15+64*i), *(npub+14+64*i), *(npub+13+64*i), *(npub+12+64*i), *(npub+11+64*i), *(npub+10+64*i), *(npub+9+64*i), *(npub+8+64*i));


		x[10*i+2] = _mm_loadu_si128((void *) (k+32+64*i));
		x[10*i+6] = _mm_loadl_epi64 ((const __m128i*)(npub+32+64*i));
		
		x[10*i+3] = _mm_loadu_si128((void *) (k+48+64*i));
		x[10*i+7] = _mm_loadl_epi64 ((const __m128i*)(npub+48+64*i));

		x[10*i+9] = _mm_set_epi8( *(npub+63+64*i), *(npub+62+64*i), *(npub+61+64*i), *(npub+60+64*i), *(npub+59+64*i), *(npub+58+64*i), *(npub+57+64*i), *(npub+56+64*i), *(npub+47+64*i), *(npub+46+64*i), *(npub+45+64*i), *(npub+44+64*i), *(npub+43+64*i), *(npub+42+64*i), *(npub+41+64*i), *(npub+40+64*i));

	}
	ace(x);
lwc-tester committed
528
	//Absorbing first 64-bit key 
lwc-tester committed
529 530 531 532 533 534 535 536
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		x[10*i+0] = x[10*i+0]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+7+64*i), *(k+6+64*i), *(k+5+64*i), *(k+4+64*i), 0x0, 0x0, 0x0, 0x0, *(k+3+64*i), *(k+2+64*i), *(k+1+64*i), *(k+64*i));
		x[10*i+1] = x[10*i+1]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+23+64*i), *(k+22+64*i), *(k+21+64*i), *(k+20+64*i), 0x0, 0x0, 0x0, 0x0, *(k+19+64*i), *(k+18+64*i), *(k+17+64*i), *(k+16+64*i));
		x[10*i+2] = x[10*i+2]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+39+64*i), *(k+38+64*i), *(k+37+64*i), *(k+36+64*i), 0x0, 0x0, 0x0, 0x0, *(k+35+64*i), *(k+34+64*i), *(k+33+64*i), *(k+32+64*i));
		x[10*i+3] = x[10*i+3]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+55+64*i), *(k+54+64*i), *(k+53+64*i), *(k+52+64*i), 0x0, 0x0, 0x0, 0x0, *(k+51+64*i), *(k+50+64*i), *(k+49+64*i), *(k+48+64*i));
	}
	ace(x);
lwc-tester committed
537
	//Absorbing last 64-bit key 
lwc-tester committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		x[10*i+0] = x[10*i+0]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+15+64*i), *(k+14+64*i), *(k+13+64*i), *(k+12+64*i), 0x0, 0x0, 0x0, 0x0, *(k+11+64*i), *(k+10+64*i), *(k+9+64*i), *(k+8+64*i));
		x[10*i+1] = x[10*i+1]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+31+64*i), *(k+30+64*i), *(k+29+64*i), *(k+28+64*i), 0x0, 0x0, 0x0, 0x0, *(k+27+64*i), *(k+26+64*i), *(k+25+64*i), *(k+24+64*i));
		x[10*i+2] = x[10*i+2]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+47+64*i), *(k+46+64*i), *(k+45+64*i), *(k+44+64*i), 0x0, 0x0, 0x0, 0x0, *(k+43+64*i), *(k+42+64*i), *(k+41+64*i), *(k+40+64*i));
		x[10*i+3] = x[10*i+3]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+63+64*i), *(k+62+64*i), *(k+61+64*i), *(k+60+64*i), 0x0, 0x0, 0x0, 0x0, *(k+59+64*i), *(k+58+64*i), *(k+57+64*i), *(k+56+64*i));
	}
	ace(x);

	//Proceesing Associated Data
	for ( l = 0; l < adblock; l++ )
	{
		for ( i = 0; i < PARAL_INST_BY4; i++)
		{
			index1 = 2*l + adlen*4*i;
			index2 = 2*l + adlen*4*i+1;
			x[10*i+0] = x[10*i+0]^_mm_set_epi32(0x0, ad[index2], 0x0, ad[index1]);
			
			index1 = 2*l + adlen*4*i+ adlen;
			index2 = 2*l + adlen*4*i+ adlen+1;
			x[10*i+1] = x[10*i+1]^_mm_set_epi32(0x0, ad[index2], 0x0, ad[index1]);
			
			index1 = 2*l + adlen*4*i+ 2*adlen;
			index2 = 2*l + adlen*4*i+ 2*adlen+1;
			x[10*i+2] = x[10*i+2]^_mm_set_epi32(0x0, ad[index2], 0x0, ad[index1]);
			
			index1 = 2*l + adlen*4*i+ 3*adlen;
			index2 = 2*l + adlen*4*i+ 3*adlen+1;
			x[10*i+3] = x[10*i+3]^_mm_set_epi32(0x0, ad[index2], 0x0, ad[index1]);

			//Domain seperator
lwc-tester committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 1)^0x00000001, 1 );
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 3)^0x00000001, 3 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 1)^0x00000001, 1 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 3)^0x00000001, 3 );
		}
		ace(x);
	}
	if (adlen%2 == 0 && adlen != 0 )
	{
		for ( i = 0; i < PARAL_INST_BY4; i++)
		{
			//Padding: 10*
			x[10*i+0] = x[10*i+0]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+1] = x[10*i+1]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+2] = x[10*i+2]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+3] = x[10*i+3]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			//Domain seperator
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 1)^0x00000001, 1 );
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 3)^0x00000001, 3 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 1)^0x00000001, 1 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 3)^0x00000001, 3 );
lwc-tester committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
		}
		ace(x);
	}
	
	//Decrypt ciphertext
	for ( l = 0; l < mblock; l++ )
	{
		for ( i = 0; i < PARAL_INST_BY4; i++)
		{
			index1 = 2*l + mlen*4*i;
			index2 = 2*l + mlen*4*i+1;
			m[index1] = _mm_extract_epi32 (x[10*i+0], 0)^c[index1];
			m[index2] = _mm_extract_epi32 (x[10*i+0], 2)^c[index2];
			x[10*i+0] = x[10*i+0]^_mm_set_epi32(0x0, m[index2], 0x0, m[index1]);
			
			index1 = 2*l + mlen*4*i+ mlen;
			index2 = 2*l + mlen*4*i+ mlen+1;
			m[index1] = _mm_extract_epi32 (x[10*i+1], 0)^c[index1];
			m[index2] = _mm_extract_epi32 (x[10*i+1], 2)^c[index2];
			x[10*i+1] = x[10*i+1]^_mm_set_epi32(0x0, m[index2], 0x0, m[index1]);
			
			index1 = 2*l + mlen*4*i+ 2*mlen;
			index2 = 2*l + mlen*4*i+ 2*mlen+1;
			m[index1] = _mm_extract_epi32 (x[10*i+2], 0)^c[index1];
			m[index2] = _mm_extract_epi32 (x[10*i+2], 2)^c[index2];
			x[10*i+2] = x[10*i+2]^_mm_set_epi32(0x0, m[index2], 0x0, m[index1]);
			
			index1 = 2*l + mlen*4*i+ 3*mlen;
			index2 = 2*l + mlen*4*i+ 3*mlen+1;
			m[index1] = _mm_extract_epi32 (x[10*i+3], 0)^c[index1];
			m[index2] = _mm_extract_epi32 (x[10*i+3], 2)^c[index2];
			x[10*i+3] = x[10*i+3]^_mm_set_epi32(0x0, m[index2], 0x0, m[index1]);

			//Domain seperator
lwc-tester committed
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 1)^0x00000002, 1 );
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 3)^0x00000002, 3 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 1)^0x00000002, 1 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 3)^0x00000002, 3 );
		}
		ace(x);
	}
	if ( mlen%2 == 0 )
	{
		for ( i = 0; i < PARAL_INST_BY4; i++)
		{
			//Padding: 10*
			x[10*i+0] = x[10*i+0]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+1] = x[10*i+1]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+2] = x[10*i+2]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			x[10*i+3] = x[10*i+3]^_mm_set_epi32(0x0, 0x0, 0x0, 0x80000000);
			//Domain seperator
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 1)^0x00000002, 1 );
			x[10*i+8] = _mm_insert_epi32(x[10*i+8], _mm_extract_epi32 (x[10*i+8], 3)^0x00000002, 3 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 1)^0x00000002, 1 );
			x[10*i+9] = _mm_insert_epi32(x[10*i+9], _mm_extract_epi32 (x[10*i+9], 3)^0x00000002, 3 );
lwc-tester committed
645 646 647 648
		}
		ace(x);
	}

lwc-tester committed
649 650
	//Tag generation phase
	//Absorbing first 64-bit key 
lwc-tester committed
651 652 653 654 655 656 657 658
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		x[10*i+0] = x[10*i+0]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+7+64*i), *(k+6+64*i), *(k+5+64*i), *(k+4+64*i), 0x0, 0x0, 0x0, 0x0, *(k+3+64*i), *(k+2+64*i), *(k+1+64*i), *(k+64*i));
		x[10*i+1] = x[10*i+1]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+23+64*i), *(k+22+64*i), *(k+21+64*i), *(k+20+64*i), 0x0, 0x0, 0x0, 0x0, *(k+19+64*i), *(k+18+64*i), *(k+17+64*i), *(k+16+64*i));
		x[10*i+2] = x[10*i+2]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+39+64*i), *(k+38+64*i), *(k+37+64*i), *(k+36+64*i), 0x0, 0x0, 0x0, 0x0, *(k+35+64*i), *(k+34+64*i), *(k+33+64*i), *(k+32+64*i));
		x[10*i+3] = x[10*i+3]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+55+64*i), *(k+54+64*i), *(k+53+64*i), *(k+52+64*i), 0x0, 0x0, 0x0, 0x0, *(k+51+64*i), *(k+50+64*i), *(k+49+64*i), *(k+48+64*i));
	}
	ace(x);
lwc-tester committed
659
	//Absorbing last 64-bit key 
lwc-tester committed
660 661 662 663 664 665 666 667
	for ( i = 0; i < PARAL_INST_BY4; i++)
        {
		x[10*i+0] = x[10*i+0]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+15+64*i), *(k+14+64*i), *(k+13+64*i), *(k+12+64*i), 0x0, 0x0, 0x0, 0x0, *(k+11+64*i), *(k+10+64*i), *(k+9+64*i), *(k+8+64*i));
		x[10*i+1] = x[10*i+1]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+31+64*i), *(k+30+64*i), *(k+29+64*i), *(k+28+64*i), 0x0, 0x0, 0x0, 0x0, *(k+27+64*i), *(k+26+64*i), *(k+25+64*i), *(k+24+64*i));
		x[10*i+2] = x[10*i+2]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+47+64*i), *(k+46+64*i), *(k+45+64*i), *(k+44+64*i), 0x0, 0x0, 0x0, 0x0, *(k+43+64*i), *(k+42+64*i), *(k+41+64*i), *(k+40+64*i));
		x[10*i+3] = x[10*i+3]^_mm_set_epi8(0x0, 0x0, 0x0, 0x0, *(k+63+64*i), *(k+62+64*i), *(k+61+64*i), *(k+60+64*i), 0x0, 0x0, 0x0, 0x0, *(k+59+64*i), *(k+58+64*i), *(k+57+64*i), *(k+56+64*i));
	}
	ace(x);
lwc-tester committed
668
	//Extracting tag
lwc-tester committed
669 670 671 672 673 674 675 676 677
	for ( i = 0; i < PARAL_INST_BY4; i++)
	{
		_mm_storeu_si128((void *) (tag+16*i), x[10*i]);
		_mm_storeu_si128((void *) (tag+4+16*i), x[10*i+1]);
		_mm_storeu_si128((void *) (tag+8+16*i), x[10*i+2]);
		_mm_storeu_si128((void *) (tag+12+16*i), x[10*i+3]);
	}
return 0;
}