encrypt.c 9.48 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 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
/*
 * HYENA_GIFT-128
 * 
 * 
 * HYENA_GIFT-128 is a nonce-based AEAD based on Hybrid Feedback mode of
 * operation and GIFT-128 block cipher.
 * 
 * Test Vector (in little endian format):
 * Key	: 0f 0e 0d 0c 0b 0a 09 08 07 06 05 04 03 02 01 00
 * PT 	:
 * AD	: 
 * CT	: 
 * 
 */

#include "crypto_aead.h"
#include "api.h"
#include "hyena.h"

u64 load64(u8 *Bytes)
{
    int i; u64 Block;
 
    Block=0;
 
    Block = (u64)(Bytes[0]);
     
    for(i = 1; i < 8; i++) {Block <<= 8; Block = (Block)^(u64)(Bytes[i]);}
 
    return Block;
}

void store64(u8 *Bytes, u64 Block)
{ 
    int i; 
     
    for (i = 7; i >= 0 ; i--) {Bytes[i] = (u8)Block; Block >>= 8; }
}



/**********************************************************************
 * 
 * @name	:	mult_by_alpha
 * 
 * @note	:	Multiplies given field element in "src" with \alpha,
 * 				the primitive element corresponding to the primitive
 * 				polynomial p(x) as defined in PRIM_POLY_MOD_128, and
 * 				stores the result in "dest".
 * 
 **********************************************************************/	
void mult_by_alpha(u8 *dest, u8 *src)
{
	u64 b;
	b =  load64(src);

        if(((b>>63)&1)==1) b = (b<<1)^(0x000000000000001B); 
                         else b = b<<1;

	store64(dest, b);	
		
}


/**********************************************************************
 * 
 * @name	:	memcpy_and_zero_one_pad
 * 
 * @note	:	Copies src bytes to dest and pads with 10* to create
 * 				CRYPTO_BLOCKBYTES-oriented data.
 * 
 **********************************************************************/
void memcpy_and_zero_one_pad(u8* dest, const u8 *src, u8 len)
{
	memset(dest, 0, 16);
	memcpy(dest, src, len);
	dest[len] ^= 0x01;
}



/**********************************************************************
 * 
 * @name	:	Feedback_TXT_Enc
 * 
 * @note	:	The FB+ module
 * 
 **********************************************************************/
void Feedback_TXT_Enc(u8 *State, u8 *output, const u8 *Delta, const u8 *input, const u64 inputlen)
{
	u32 i;
	u8 pad1[16], pad2[16], feedback[16];

	for(i = 0 ; i < inputlen;i++)  output[i] = input[i]^State[i];


	if(inputlen < 16) 
	{
		memcpy_and_zero_one_pad(&pad1[0], input, inputlen);
		memcpy_and_zero_one_pad(&pad2[0], output, inputlen);
	}
	else 
	{
		for(i = 0 ; i < 16 ; i++)
		{
		pad1[i] = input[i];
		pad2[i] = output[i];
		}
	}

     for(i=0; i<8 ;i++)
     { 
		feedback[i] = pad1[i];
           feedback[i+8] = pad2[i+8];

     }
117
     for(i=8; i<16 ;i++)
lwc-tester committed
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
     { 
		feedback[i] ^= Delta[i-8];
     }


     
	for(i = 0 ; i < 16 ; i++) State[i] ^= feedback[i];
      
}		

/**********************************************************************
 * 
 * @name	:	Feedback_TXT_Dec
 * 
 * @note	:	The FB- module
 * 
 **********************************************************************/
void Feedback_TXT_Dec(u8 *State, u8 *output, const u8 *Delta, const u8 *input, const u64 inputlen)
{
	u32 i;
	u8 pad1[16], pad2[16], feedback[16];
	for(i = 0 ; i < inputlen;i++)   output[i] = input[i]^State[i];

	if(inputlen < 16) 
	{
		memcpy_and_zero_one_pad(&pad1[0], output, inputlen);
		memcpy_and_zero_one_pad(&pad2[0], input, inputlen);
	}
	else 
	{
		for(i = 0 ; i < 16 ; i++)
		{
		pad1[i] = output[i];
		pad2[i] = input[i];
		}
	}
     	for(i=0; i<8 ;i++)
     	{ 
		feedback[i] = pad1[i];
        	feedback[i+8] = pad2[i+8];
        }
159
    	for(i=8; i<16 ;i++)
lwc-tester committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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 287 288 289 290 291 292 293 294 295 296 297 298 299 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 332 333 334 335 336 337 338 339 340 341 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 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
     	{ 
		feedback[i] ^= Delta[i-8];
     	}
     
	for(i = 0 ; i < 16 ; i++) State[i] ^= feedback[i];
      
}



/**********************************************************************
 * 
 * @name	:	INIT
 * 
 * @note	:	Derives nonce-dependent initial state and mask.
 * 
 **********************************************************************/
void INIT(u8 *State, u8 * Delta, const u8 *npub, const u32 cntrl, const u8 (*round_keys)[32])
{
	u32 i;
	for(i = 4 ; i < 16 ; i++) 
		State[i] = npub[i-4];
	for(i = 0 ; i < 4 ; i++)
		State[i] = 0;
	State[0] ^= (u8)cntrl;		
	gift_enc(State, &round_keys[0], State);	
	for(i = 0 ; i < 8 ; i++)
		Delta[i] = State[i+8];
}

/**********************************************************************
 * 
 * @name	:	PROC_AD
 * 
 * @note	:	Processes associated data.
 * 
 **********************************************************************/
void PROC_AD(u8 *State, u8 * Delta,  const u8 *input,  u64 inputlen, const u8 (*round_keys)[32])
{
      u8 output[16] = { 0 };
      u64 outputlen = 0;

	while(inputlen > 16)
	{
		mult_by_alpha(Delta, Delta);
		Feedback_TXT_Enc(State, output, Delta, input+outputlen, 16);
		gift_enc(State, &round_keys[0], State);	
		inputlen -= 16; outputlen += 16;
	}		
	mult_by_alpha(Delta, Delta);
	
	if(inputlen < 16)
	{
		mult_by_alpha(Delta, Delta); 
		mult_by_alpha(Delta, Delta);
	}
	else
	{
		mult_by_alpha(Delta, Delta);
	}

	Feedback_TXT_Enc(State, output, Delta, input+outputlen, inputlen);			
	//gift_enc(State, &round_keys[0], State);			
}

/**********************************************************************
 * 
 * @name	:	Proc_TXT
 * 
 * @note	:	Generates ciphertext/plaintext by encrypting/decrypting
 * 				plaintext/ciphertext.
 * 
 **********************************************************************/
void Proc_TXT(u8 *State, u8 *Delta,  u8 *output, u64 *outputlen, const u8 *input, u64 inputlen,  const u8 (*round_keys)[32], const u32 direction)
{
	if(inputlen != 0)
	{
		while(inputlen > 16)
		{		
			mult_by_alpha(Delta, Delta);
			gift_enc(State, &round_keys[0], State);	
			if(direction==0)
				Feedback_TXT_Enc(State, output + *outputlen, Delta, input + *outputlen, 16);
                 	else 
				Feedback_TXT_Dec(State, output + *outputlen, Delta, input + *outputlen, 16);
			inputlen -= 16; *outputlen += 16; 
		}
		mult_by_alpha(Delta, Delta);
		if(inputlen < 16)
		{
			mult_by_alpha(Delta, Delta); mult_by_alpha(Delta, Delta); 	 
		}
		else
		{
			mult_by_alpha(Delta, Delta);
		}
		gift_enc(State, &round_keys[0], State);			
		if(direction==0)
			Feedback_TXT_Enc(State, output + *outputlen, Delta, input + *outputlen, inputlen);
                else 
			Feedback_TXT_Dec(State, output + *outputlen, Delta, input + *outputlen, inputlen);
		*outputlen = *outputlen + inputlen; 		
	}
}

void swap(u8 *a, u8 *b)
{
	*a = *a ^ *b;
	*b = *a ^ *b;
	*a = *a ^ *b;
}

/**********************************************************************
 * 
 * @name	:	Tag_Gen
 * 
 * @note	:	Tag generator.
 * 
 **********************************************************************/
void Tag_Gen(u8 *State, const u8 (*round_keys)[32])
{ 	
	u32 i;
	for(i = 0 ; i < 8 ; i++)
		swap(&State[i], &State[i+8]);	
	gift_enc(State, &round_keys[0], State);
}

/**********************************************************************
 * 
 * @name	:	crypto_aead_encrypt
 * 
 * @note	:	Main encryption function.
 * 
 **********************************************************************/
int crypto_aead_encrypt(
	unsigned char *ct, unsigned long long *ctlen,
	const unsigned char *pt, unsigned long long ptlen,
	const unsigned char *ad, unsigned long long adlen,
	const unsigned char *nsec,
	const unsigned char *npub,
	const unsigned char *k
)
{
	// to bypass unused warning on nsec
	nsec = nsec;
	
	u32 i;

	u32 cntrl;

	*ctlen = 0;
	cntrl = 0;

	u8  HYENA_State[16], Delta[8], round_keys[CRYPTO_BC_NUM_ROUNDS][32];

      

	for(i = 0 ; i < 16 ; i++) HYENA_State[i] = 0;
        for(i = 0 ; i < 8 ; i++) Delta[i] = 0;

	if(adlen == 0 && ptlen == 0) cntrl = 0x03;
	if(adlen == 0 && ptlen > 0) cntrl = 0x01;

	_GIFT_ENC_ROUND_KEY_GEN(&round_keys[0], k);

	INIT(&HYENA_State[0], &Delta[0], npub, cntrl, &round_keys[0]);
	


      PROC_AD(&HYENA_State[0], &Delta[0], ad, adlen, &round_keys[0]);
	if(ptlen != 0)
	{				
  		Proc_TXT(&HYENA_State[0], &Delta[0], ct, ctlen, pt, ptlen, &round_keys[0], 0);
                

	}


	Tag_Gen(&HYENA_State[0], &round_keys[0]);

     

	for(i = 0 ; i< CRYPTO_ABYTES ; i++) ct[*ctlen + i] = HYENA_State[i];
      
        *ctlen  += CRYPTO_ABYTES;


	return 0;
}

/**********************************************************************
 * 
 * @name	:	crypto_aead_decrypt
 * 
 * @note	:	Main decryption function.
 * 
 **********************************************************************/
int crypto_aead_decrypt(
	unsigned char *pt, unsigned long long *ptlen,
	unsigned char *nsec,
	const unsigned char *ct, unsigned long long ctlen,
	const unsigned char *ad, unsigned long long adlen,
	const unsigned char *npub,
	const unsigned char *k
)
{
	// to bypass unused warning on nsec
	nsec = nsec;
	
	int pass;
	u32 i;
	u8  tag[16], HYENA_State[16], Delta[8], cntrl, round_keys[CRYPTO_BC_NUM_ROUNDS][32];

      cntrl = 0;
      *ptlen = 0;
	for(i = 0 ; i < 16 ; i++) HYENA_State[i] = 0;
      for(i = 0 ; i < 8 ; i++) Delta[i] = 0;

	if(adlen == 0 && ctlen == 16) cntrl = 0x03;

	if(adlen == 0 && ctlen != 16) cntrl = 0x01;
	_GIFT_ENC_ROUND_KEY_GEN(&round_keys[0], k);

	INIT(&HYENA_State[0], &Delta[0], npub, cntrl, &round_keys[0]);


        PROC_AD(&HYENA_State[0], &Delta[0], ad, adlen, &round_keys[0]);

	if(ctlen != 16)
	{
  		Proc_TXT(&HYENA_State[0], &Delta[0], pt, ptlen, ct, ctlen-16, &round_keys[0], 1);
        }


	Tag_Gen(&HYENA_State[0], &round_keys[0]);
	pass = 0;

	for(i = 0 ; i< CRYPTO_ABYTES ; i++) 
	{
		tag[i] = HYENA_State[i];
		if(tag[i]!=ct[*ptlen + i]) pass = -1;
	}
	
	return pass;
}