spix.c 16.9 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7
/* Optimized (Intel Intrinsics) implementation of SPIX-128 AEAD
   Written by:
   Kalikinkar Mandal <kmandal@uwaterloo.ca>
*/

#include "spix.h"

lwc-tester committed
8 9 10 11
/*
   *sliscp_light256: 18-round sliscp-light permutation
   *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
void sliscp_light256 ( u32 *state )
{
        u8 i, j, k;
        u32 t1, t2;
        
        __m128i x[4*PARAL_INST_BY2];
        __m128i xtmp, ytmp;
        
        for ( k = 0; k < PARAL_INST_BY2; k++ )
                PACK(x[4*k], x[4*k+1], x[4*k+2], x[4*k+3], state, 16*k, 16*k+4, 16*k+8, 16*k+12);
        
        for ( i = 0; i < STEP_ROUND; i++ )
        {
                
                for ( k = 0; k < PARAL_INST_BY2; k++ )
                {
                        //SSb
                        PACK_SSb(x[4*k+2],x[4*k+3]);
                        for ( j = 0; j < SIMECK_ROUND; j++ )
                        {
lwc-tester committed
32 33
                                t1 = (u32)((RC1_256[i] >> j)&1);
                                t2 = (u32)((RC2_256[i] >> j)&1);
lwc-tester committed
34 35 36 37
                                ROAX(x[4*k+2], x[4*k+3], t1, t2);
                        }
                        UNPACK_SSb(x[4*k+2],x[4*k+3]);
                        //ASc
lwc-tester committed
38 39
                        t1 = (u32)SC1_256[i];
                        t2 = (u32)SC2_256[i];
lwc-tester committed
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
                        xtmp = x[4*k+0]^SC(t1, t2);
                        ytmp = x[4*k+1]^SC(t1, t2);
                        //MSb
                        x[4*k+0] = x[4*k+2];
                        x[4*k+1] = x[4*k+3];
                        x[4*k+2]^=xtmp;
                        x[4*k+3]^=ytmp;
                        x[4*k+2] = SWAPBLK(x[4*k+2]);
                        x[4*k+3] = SWAPBLK(x[4*k+3]);
                }
        }
        //Unpacking state
        for ( k = 0; k < PARAL_INST_BY2; k++)
        {
                xtmp = _mm_unpacklo_epi64(x[4*k+0], x[4*k+2]);
                ytmp = _mm_unpackhi_epi64(x[4*k+0], x[4*k+2]);
                _mm_storeu_si128((void *) (state + 0+16*k),xtmp);
                _mm_storeu_si128((void *) (state + 4 +16*k),ytmp);
                xtmp = _mm_unpacklo_epi64(x[4*k+1], x[4*k+3]);
                ytmp = _mm_unpackhi_epi64(x[4*k+1], x[4*k+3]);
                _mm_storeu_si128((void *) (state + 8+16*k),xtmp);
                _mm_storeu_si128((void *) (state + 12+16*k),ytmp);
        }
        return;
}

lwc-tester committed
66 67 68 69 70
/*
   *sliscp256: 18-round sliscp-light permutation of width 256 bits
   *x: input state, and output state is stored in state (inplace)
*/

lwc-tester committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84
void sliscp256( __m128i *x )
{
	__m128i xtmp, ytmp;
	u8 i, j, k;
	u32 t1, t2;

	for ( i = 0; i < STEP_ROUND; i++ )
	{
		for ( k = 0; k < PARAL_INST_BY2; k++ )
		{
			//SSb
        		PACK_SSb(x[4*k+2], x[4*k+3]);
        		for ( j = 0; j < SIMECK_ROUND; j++ )
        		{
lwc-tester committed
85 86
				t1 = (u32)((RC1_256[i] >> j)&1);
                		t2 = (u32)((RC2_256[i] >> j)&1);
lwc-tester committed
87 88 89 90
                		ROAX( x[4*k+2], x[4*k+3], t1, t2);
        		}
        		UNPACK_SSb(x[4*k+2], x[4*k+3]);
        		//ASc
lwc-tester committed
91 92
        		t1 = (u32)SC1_256[i];
        		t2 = (u32)SC2_256[i];
lwc-tester committed
93 94 95 96 97 98 99 100 101 102 103 104 105
        		xtmp = x[4*k+0]^SC(t1, t2);
        		ytmp = x[4*k+1]^SC(t1, t2);
        		//MSb
			x[4*k+0] = x[4*k+2];
			x[4*k+1] = x[4*k+3];
        		x[4*k+2]^=xtmp;
        		x[4*k+3]^=ytmp;
        		x[4*k+2] = SWAPBLK(x[4*k+2]);
        		x[4*k+3] = SWAPBLK(x[4*k+3]);
		}
	}
}

lwc-tester committed
106 107 108 109 110
/*
   *sliscp256: 9-round sliscp-light permutation of width 256 bits
   *x: input state, and output state is stored in state (inplace)
*/

lwc-tester committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124
void sliscp256r9( __m128i *x )
{
	__m128i xtmp, ytmp;
	u8 i, j, k;
	u32 t1, t2;

	for ( i = 0; i < 9; i++ )
	{
		for ( k = 0; k < PARAL_INST_BY2; k++ )
		{
			//SSb
        		PACK_SSb(x[4*k+2], x[4*k+3]);
        		for ( j = 0; j < SIMECK_ROUND; j++ )
        		{
lwc-tester committed
125 126
				t1 = (u32)((RC1_256[i] >> j)&1);
                		t2 = (u32)((RC2_256[i] >> j)&1);
lwc-tester committed
127 128 129 130
                		ROAX( x[4*k+2], x[4*k+3], t1, t2);
        		}
        		UNPACK_SSb(x[4*k+2], x[4*k+3]);
        		//ASc
lwc-tester committed
131 132
        		t1 = (u32)SC1_256[i];
        		t2 = (u32)SC2_256[i];
lwc-tester committed
133 134 135 136 137 138 139 140 141 142 143 144 145
        		xtmp = x[4*k+0]^SC(t1, t2);
        		ytmp = x[4*k+1]^SC(t1, t2);
        		//MSb
			x[4*k+0] = x[4*k+2];
			x[4*k+1] = x[4*k+3];
        		x[4*k+2]^=xtmp;
        		x[4*k+3]^=ytmp;
        		x[4*k+2] = SWAPBLK(x[4*k+2]);
        		x[4*k+3] = SWAPBLK(x[4*k+3]);
		}
	}
}

lwc-tester committed
146 147 148 149 150 151 152 153 154 155 156 157
/*
   *crypto_aead_encrypt: encrypt plaintext and produce tag
   *key: key 
   *nonce: public nonce
   *adlen: length of ad
   *ad: associate data
   *plen: length of plaintext
   *plaintext: plaintext to be encrypted
   *ciphertext: ciphertext
   *tlen: length of tag (in 32-bit word)
   *tag: tag
*/
lwc-tester committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
void crypto_aead_encrypt( u32 *tag, u32 tlen, u32 *ciphertext, u32 *plaintext, u32 plen, u32 *ad, u32 adlen, u8 *nonce, u8 *key)
{
        u8 k, i;
        u32 l, mblock, adblock;
        __m128i x[4*PARAL_INST_BY2];
	u32 index1, index2;

	mblock = plen/2;
	adblock = adlen/2;
       
	//Initialization: Loading Key & Nonce
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i] = _mm_set_epi8(nonce[32*i+15],nonce[32*i+14],nonce[32*i+13],nonce[32*i+12],nonce[32*i+11],nonce[32*i+10],nonce[32*i+9],nonce[32*i+8],nonce[32*i+7],nonce[32*i+6],nonce[32*i+5],nonce[32*i+4],nonce[32*i+3],nonce[32*i+2],nonce[32*i+1],nonce[32*i+0]);
		x[4*i+1] = _mm_set_epi8(nonce[32*i+31],nonce[32*i+30],nonce[32*i+29],nonce[32*i+28],nonce[32*i+27],nonce[32*i+26],nonce[32*i+25],nonce[32*i+24],nonce[32*i+23],nonce[32*i+22],nonce[32*i+21],nonce[32*i+20],nonce[32*i+19],nonce[32*i+18],nonce[32*i+17],nonce[32*i+16]);
		x[4*i+2] = _mm_set_epi8(key[32*i+15],key[32*i+14],key[32*i+13],key[32*i+12],key[32*i+11],key[32*i+10],key[32*i+9],key[32*i+8],key[32*i+7],key[32*i+6],key[32*i+5],key[32*i+4],key[32*i+3],key[32*i+2],key[32*i+1],key[32*i+0]);
		x[4*i+3] = _mm_set_epi8(key[32*i+31],key[32*i+30],key[32*i+29],key[32*i+28],key[32*i+27],key[32*i+26],key[32*i+25],key[32*i+24],key[32*i+23],key[32*i+22],key[32*i+21],key[32*i+20],key[32*i+19],key[32*i+18],key[32*i+17],key[32*i+16]);
	}
lwc-tester committed
176 177
	sliscp256(x); //18-round sliscp-light permutation
	//Absorbing first 64-bit key 
lwc-tester committed
178 179 180 181 182 183
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i+2] = x[4*i+2]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+7],key[32*i+6],key[32*i+5],key[32*i+4],0x0,0x0,0x0,0x0,key[32*i+3],key[32*i+2],key[32*i+1],key[32*i+0]);
		x[4*i+3] = x[4*i+3]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+23],key[32*i+22],key[32*i+21],key[32*i+20],0x0,0x0,0x0,0x0,key[32*i+19],key[32*i+18],key[32*i+17],key[32*i+16]);
	}
	sliscp256(x);
lwc-tester committed
184
	//Absorbing last 64-bit key 
lwc-tester committed
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
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i+2] = x[4*i+2]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+15],key[32*i+14],key[32*i+13],key[32*i+12],0x0,0x0,0x0,0x0,key[32*i+11],key[32*i+10],key[32*i+9],key[32*i+8]);
		x[4*i+3] = x[4*i+3]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+31],key[32*i+30],key[32*i+29],key[32*i+28],0x0,0x0,0x0,0x0,key[32*i+27],key[32*i+26],key[32*i+25],key[32*i+24]);
	}
	sliscp256(x);
	
	//Proceesing Associated Data
	for ( l = 0; l < adblock; l++ )
	{
		for ( k = 0; k < PARAL_INST_BY2; k++)
		{
			index1 = 2*l + adlen*2*k;
			index2 = 2*l + adlen*2*k+1;
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 0)^ad[index1], 0);
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 2)^ad[ index2], 2);
			index1 = 2*l + adlen*2*k+adlen;
			index2 = 2*l + adlen*2*k+adlen+1;
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 0)^ad[index1], 0);
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 2)^ad[index2], 2);
			//Domain seperator
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 3)^0x00000001, 3 );
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 3)^0x00000001, 3 );
		}
		sliscp256r9(x);
	}
lwc-tester committed
211
	if ( adlen%2 == 0 && adlen != 0 )
lwc-tester committed
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
	{
		for ( k = 0; k < PARAL_INST_BY2; k++)
		{
			//Padding: 10*
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 0)^(0x80000000), 0);
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 0)^(0x80000000), 0);

			//Domain seperator
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 3)^0x00000001, 3 );
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 3)^0x00000001, 3 );
		}
		sliscp256r9(x);
	}
	//Encrypting plaintext
	for ( l = 0; l < mblock; l++ )
	{
		for ( k = 0; k < PARAL_INST_BY2; k++)
		{
			index1 = 2*l + plen*2*k;
			index2 = 2*l + plen*2*k+1;
			ciphertext[index1] = _mm_extract_epi32 (x[4*k+2], 0)^plaintext[index1];
			ciphertext[index2] = _mm_extract_epi32 (x[4*k+2], 2)^plaintext[ index2];
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], ciphertext[index1], 0);
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], ciphertext[index2], 2);
			index1 = 2*l + plen*2*k+plen;
			index2 = 2*l + plen*2*k+plen+1;
			ciphertext[index1] = _mm_extract_epi32 (x[4*k+3], 0)^plaintext[index1];
			ciphertext[index2] = _mm_extract_epi32 (x[4*k+3], 2)^plaintext[index2];
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], ciphertext[index1], 0);
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], ciphertext[index2], 2);
			//Domain seperator
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 3)^0x00000002, 3 );
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 3)^0x00000002, 3 );
		}
		sliscp256r9(x);
	}
	if ( plen%2 == 0 )
	{
		for ( k = 0; k < PARAL_INST_BY2; k++)
		{
			//Padding: 10*
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 0)^(0x80000000), 0);
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 0)^(0x80000000), 0);
			//Domain seperator
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 3)^0x00000002, 3 );
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 3)^0x00000002, 3 );
		}
		sliscp256r9(x);
	}
lwc-tester committed
261 262
	//Tag generation phase
	//Absorbing first 64-bit key 
lwc-tester committed
263 264 265 266 267 268
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i+2] = x[4*i+2]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+7],key[32*i+6],key[32*i+5],key[32*i+4],0x0,0x0,0x0,0x0,key[32*i+3],key[32*i+2],key[32*i+1],key[32*i+0]);
		x[4*i+3] = x[4*i+3]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+23],key[32*i+22],key[32*i+21],key[32*i+20],0x0,0x0,0x0,0x0,key[32*i+19],key[32*i+18],key[32*i+17],key[32*i+16]);
	}
	sliscp256(x);
lwc-tester committed
269
	//Absorbing last 64-bit key 
lwc-tester committed
270 271 272 273 274 275
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i+2] = x[4*i+2]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+15],key[32*i+14],key[32*i+13],key[32*i+12],0x0,0x0,0x0,0x0,key[32*i+11],key[32*i+10],key[32*i+9],key[32*i+8]);
		x[4*i+3] = x[4*i+3]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+31],key[32*i+30],key[32*i+29],key[32*i+28],0x0,0x0,0x0,0x0,key[32*i+27],key[32*i+26],key[32*i+25],key[32*i+24]);
	}
	sliscp256(x);
lwc-tester committed
276
	//Extracting tag
lwc-tester committed
277 278 279 280 281 282 283 284 285
	for ( k = 0; k < PARAL_INST_BY2; k++)
	{
		_mm_storeu_si128((void *) (tag + 0 + 8*k), x[4*k+2]);
		_mm_storeu_si128((void *) (tag + 4 + 8*k), x[4*k+3]);
	}
return;
}


lwc-tester committed
286 287 288 289 290 291 292 293 294 295 296 297
/*
   *crypto_aead_decrypt: decrypt ciphertext
   *key: key 
   *nonce: public nonce
   *adlen: length of ad (in 32-bit word)
   *ad: associate data
   *tlen: length of tag (in 32-bit word)
   *tag: tag
   *plen: length of plaintext (in 32-bit word)
   *plaintext: plaintext to be encrypted
   *ciphertext: ciphertext
*/
lwc-tester committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
void crypto_aead_decrypt( u32 *plaintext, u32 *ciphertext, u32 plen, u32 *tag, u32 tlen, u32 *ad, u32 adlen, u8 *nonce, u8 *key)
{
        u8 k, i;
        u32 l, mblock, adblock;
        
        __m128i x[4*PARAL_INST_BY2];
	u32 index1, index2;

	mblock = plen/2;
	adblock = adlen/2;
       
	//Initialization: Loading KEY & NONCE into Regs
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i] = _mm_set_epi8(nonce[32*i+15],nonce[32*i+14],nonce[32*i+13],nonce[32*i+12],nonce[32*i+11],nonce[32*i+10],nonce[32*i+9],nonce[32*i+8],nonce[32*i+7],nonce[32*i+6],nonce[32*i+5],nonce[32*i+4],nonce[32*i+3],nonce[32*i+2],nonce[32*i+1],nonce[32*i+0]);
		x[4*i+1] = _mm_set_epi8(nonce[32*i+31],nonce[32*i+30],nonce[32*i+29],nonce[32*i+28],nonce[32*i+27],nonce[32*i+26],nonce[32*i+25],nonce[32*i+24],nonce[32*i+23],nonce[32*i+22],nonce[32*i+21],nonce[32*i+20],nonce[32*i+19],nonce[32*i+18],nonce[32*i+17],nonce[32*i+16]);
		x[4*i+2] = _mm_set_epi8(key[32*i+15],key[32*i+14],key[32*i+13],key[32*i+12],key[32*i+11],key[32*i+10],key[32*i+9],key[32*i+8],key[32*i+7],key[32*i+6],key[32*i+5],key[32*i+4],key[32*i+3],key[32*i+2],key[32*i+1],key[32*i+0]);
		x[4*i+3] = _mm_set_epi8(key[32*i+31],key[32*i+30],key[32*i+29],key[32*i+28],key[32*i+27],key[32*i+26],key[32*i+25],key[32*i+24],key[32*i+23],key[32*i+22],key[32*i+21],key[32*i+20],key[32*i+19],key[32*i+18],key[32*i+17],key[32*i+16]);
	}
lwc-tester committed
317 318
	sliscp256(x); //18-round sliscp-light permutation
	//Absorbing first 64-bit key 
lwc-tester committed
319 320 321 322 323 324
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i+2] = x[4*i+2]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+7],key[32*i+6],key[32*i+5],key[32*i+4],0x0,0x0,0x0,0x0,key[32*i+3],key[32*i+2],key[32*i+1],key[32*i+0]);
		x[4*i+3] = x[4*i+3]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+23],key[32*i+22],key[32*i+21],key[32*i+20],0x0,0x0,0x0,0x0,key[32*i+19],key[32*i+18],key[32*i+17],key[32*i+16]);
	}
	sliscp256(x);
lwc-tester committed
325
	//Absorbing last 64-bit key 
lwc-tester committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i+2] = x[4*i+2]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+15],key[32*i+14],key[32*i+13],key[32*i+12],0x0,0x0,0x0,0x0,key[32*i+11],key[32*i+10],key[32*i+9],key[32*i+8]);
		x[4*i+3] = x[4*i+3]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+31],key[32*i+30],key[32*i+29],key[32*i+28],0x0,0x0,0x0,0x0,key[32*i+27],key[32*i+26],key[32*i+25],key[32*i+24]);
	}
	sliscp256(x);
	
	//Processing Associated Data
	for ( l = 0; l < adblock; l++ )
	{
		for ( k = 0; k < PARAL_INST_BY2; k++)
		{
			index1 = 2*l + adlen*2*k;
			index2 = 2*l + adlen*2*k+1;
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 0)^ad[index1], 0);
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 2)^ad[ index2], 2);

			index1 = 2*l + adlen*2*k+adlen;
			index2 = 2*l + adlen*2*k+adlen+1;
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 0)^ad[index1], 0);
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 2)^ad[ index2], 2);

			//Domain seperator
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 3)^0x00000001, 3 );
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 3)^0x00000001, 3 );
		}
		sliscp256r9(x);
	}
lwc-tester committed
354
	if ( adlen%2 == 0 && adlen != 0 )
lwc-tester committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	{
		for ( k = 0; k < PARAL_INST_BY2; k++)
		{
			//Padding: 10*
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 0)^(0x80000000), 0);
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 0)^(0x80000000), 0);

			//Domain seperator
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 3)^0x00000001, 3 );
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 3)^0x00000001, 3 );
		}
		sliscp256r9(x);
	}
	//Decrypting ciphertext
	for ( l = 0; l < mblock; l++ )
	{
		for ( k = 0; k < PARAL_INST_BY2; k++)
		{
			index1 = 2*l + plen*2*k;
			index2 = 2*l + plen*2*k+1;
			plaintext[index1] = _mm_extract_epi32 (x[4*k+2], 0)^ciphertext[index1];
			plaintext[index2] = _mm_extract_epi32 (x[4*k+2], 2)^ciphertext[ index2];
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], ciphertext[index1], 0);
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], ciphertext[index2], 2);
			index1 = 2*l + plen*2*k+plen;
			index2 = 2*l + plen*2*k+plen+1;
			plaintext[index1] = _mm_extract_epi32 (x[4*k+3], 0)^ciphertext[index1];
			plaintext[index2] = _mm_extract_epi32 (x[4*k+3], 2)^ciphertext[ index2];
			
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], ciphertext[index1], 0);
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], ciphertext[index2], 2);

			//Domain seperator
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 3)^0x00000002, 3 );
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 3)^0x00000002, 3 );
		}
		sliscp256r9(x);
	}
	if ( plen%2 == 0 )
	{
		for ( k = 0; k < PARAL_INST_BY2; k++)
		{
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 0)^(0x80000000), 0);
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 0)^(0x80000000), 0);

			//Domain seperator
			x[4*k+2] = _mm_insert_epi32(x[4*k+2], _mm_extract_epi32 (x[4*k+2], 3)^0x00000002, 3 );
			x[4*k+3] = _mm_insert_epi32(x[4*k+3], _mm_extract_epi32 (x[4*k+3], 3)^0x00000002, 3 );
		}
		sliscp256r9(x);
	}
lwc-tester committed
406 407
	//Tag generation phase
	//Absorbing first 64-bit key 
lwc-tester committed
408 409 410 411 412 413
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i+2] = x[4*i+2]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+7],key[32*i+6],key[32*i+5],key[32*i+4],0x0,0x0,0x0,0x0,key[32*i+3],key[32*i+2],key[32*i+1],key[32*i+0]);
		x[4*i+3] = x[4*i+3]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+23],key[32*i+22],key[32*i+21],key[32*i+20],0x0,0x0,0x0,0x0,key[32*i+19],key[32*i+18],key[32*i+17],key[32*i+16]);
	}
	sliscp256(x);
lwc-tester committed
414
	//Absorbing last 64-bit key 
lwc-tester committed
415 416 417 418 419 420
	for ( i = 0; i < PARAL_INST_BY2; i++ )
	{
		x[4*i+2] = x[4*i+2]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+15],key[32*i+14],key[32*i+13],key[32*i+12],0x0,0x0,0x0,0x0,key[32*i+11],key[32*i+10],key[32*i+9],key[32*i+8]);
		x[4*i+3] = x[4*i+3]^_mm_set_epi8(0x0,0x0,0x0,0x0,key[32*i+31],key[32*i+30],key[32*i+29],key[32*i+28],0x0,0x0,0x0,0x0,key[32*i+27],key[32*i+26],key[32*i+25],key[32*i+24]);
	}
	sliscp256(x);
lwc-tester committed
421
	//Extracting tag
lwc-tester committed
422 423 424 425 426 427 428 429
	for ( k = 0; k < PARAL_INST_BY2; k++)
	{
		_mm_storeu_si128((void *) (tag + 0 + 8*k), x[4*k+2]);
		_mm_storeu_si128((void *) (tag + 4 + 8*k), x[4*k+3]);
	}
return;
}