encrypt.c 7.32 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 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 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
#include "crypto_aead.h"


/** 
*  Designers: Daniel Penazzi, Miguel Montes
This codification: Daniel Penazzi.
 
* This code is hereby placed in the public domain.
*/

typedef unsigned long long u64;
typedef unsigned int u32;

//number of rounds of initialization, times 11 (12x11)
#define NRINIT 132

//number of rounds of processing ad and message, times 11
#define NRMID 99

//extra number of rounds of finalization (after NRMID rounds are done), times 11
#define NREND 33

//increase of i for the round constants
#define STEP 11

#define ROTR(x,n) (((x)>>(n))|((x)<<(64-(n))))

void Difusion(u64 W[5]){
	W[3]^= ROTR(W[3],57)^ROTR(W[3],63);
	W[4]^= ROTR(W[4],3)^ROTR(W[4],26);
	W[0]^= ROTR(W[0],43)^ROTR(W[0],62);
	W[1]^= ROTR(W[1],21)^ROTR(W[1],46);
	W[2]^= ROTR(W[2],58)^ROTR(W[2],61);
	W[0]^=W[3]^W[4];W[1]^=W[3]^W[4];W[2]^=W[3]^W[4];
	W[3]^=W[0]^W[1]^W[2];W[4]^=W[0]^W[1]^W[2];
	W[0]=ROTR(W[0],8);
	W[1]=ROTR(W[1],24);
	W[2]=ROTR(W[2],40);
	W[3]=ROTR(W[3],56);
}

void Sbox(u64 W[5]){
	u64 x=W[0];
	u64 y=W[1];
	u64 z=W[2];
	u64 u=W[3];
	u64 v=W[4];
	W[0]=v^(x|u)^(y|(z^x));
	W[1]=x^(y|v)^(z|(u^y));
	W[2]=y^(z|x)^(u|(v^z));
	W[3]=z^(u|y)^(v|(x^u));
	W[4]=(~u)^(v|z)^(x|(y^v));
}




//R7R6....R1R0
void LESTOREu64in8bytearray(u64 Rv,unsigned char* R){
	for(u32 i=0;i<8;i++) {
		R[i]=(Rv&0xff);
		Rv=(Rv>>8);
	} 
} 


void PartialLESTOREu64inbytearray(u64 Rv,unsigned char* R,u64 Top){
	for(u32 i=0;i<Top;i++) {
		R[i]=(Rv&0xff);
		Rv=(Rv>>8);
	} 
} 


u64 LELOAD8bytearrayinu64(const unsigned char* R){
	u64 Rv=0;
	for(u32 i=0;i<8;i++) {
		Rv|=((u64)(R[i])<<(8*i));
	}
	return Rv; 
}

//should only be used if Top<8.
u64 PartialLELOADbytearrayinu64(const unsigned char* R,u64 Top){
	u64 Rv=(u64)R[0];
	for(u64 i=1;i<Top;i++) {
		Rv^=((u64)(R[i])<<(8*i));
	}
	return Rv;
}


void Ronda(u64 Rconst,u64 W[5]) {
	W[0]^=Rconst;
	Sbox(W);
	Difusion(W);
}



//aux funcs


void Initialize(u64 W[5],const unsigned char *npub,const unsigned char *k){
	W[0]=LELOAD8bytearrayinu64(k);k+=8;
	W[1]=LELOAD8bytearrayinu64(k);
	W[2]=LELOAD8bytearrayinu64(npub);npub+=8;
	W[3]=LELOAD8bytearrayinu64(npub);
	W[4]=ROTR(W[0]^W[1],32)^0xff;

	for(unsigned int i=0;i<NRINIT;i=i+STEP) Ronda(i^0xff,W);

}

void ProcessAd(u64 W[5],const unsigned char *ad,unsigned long long adlen){
		u64 remain=adlen;		
		while(remain>=16){
			W[3]^=LELOAD8bytearrayinu64(ad);	ad+=8;
			W[2]^=LELOAD8bytearrayinu64(ad);
			for(unsigned int  i=0;i<NRMID;i=i+STEP) Ronda(i^0xad,W);
			remain-=16;
			ad+=8;
		}//endwhile
		if(remain){
			if(remain<8){
				W[3]^=PartialLELOADbytearrayinu64(ad,remain);
				W[3]^=((u64)(1)<<(8*remain));
			}else{
				W[3]^=LELOAD8bytearrayinu64(ad);	ad+=8;remain-=8;
				if(remain) W[2]^=PartialLELOADbytearrayinu64(ad,remain);
				W[2]^=((u64)(1)<<(8*remain));
			}
			W[1]^=1;//distinguisher from remain=0 case
			for(unsigned int  i=0;i<NRMID;i=i+STEP) Ronda(i^0xad,W);
		}//endifadremain

	W[1]^=1;//delimiter ad/mesg

}

unsigned char* Processplaintxt(u64 W[5],unsigned char *c,const unsigned char *m,unsigned long long mlen){
		u64 remain=mlen;
		while(remain>=16){
			W[3]^=LELOAD8bytearrayinu64(m);
			LESTOREu64in8bytearray(W[3],c);m+=8;c+=8;
			W[2]^=LELOAD8bytearrayinu64(m);
			LESTOREu64in8bytearray(W[2],c);

			for(unsigned int  i=0;i<NRMID;i=i+STEP) Ronda(i^0xff,W);
			remain-=16;
			m+=8;c+=8;
		}//endwhile

		if(remain){
			if(remain<8){
				W[3]^=PartialLELOADbytearrayinu64(m,remain);
				PartialLESTOREu64inbytearray(W[3],c,remain);
				W[3]^=((u64)(1)<<(8*remain));
			}
			else{
				W[3]^=LELOAD8bytearrayinu64(m);
				LESTOREu64in8bytearray(W[3],c);m+=8;c+=8;remain-=8;
				if(remain){
					W[2]^=PartialLELOADbytearrayinu64(m,remain);
					PartialLESTOREu64inbytearray(W[2],c,remain);
				}
				W[2]^=((u64)(1)<<(8*remain));
			}
			W[1]^=1;//distinguisher from remain=0 case
			c+=remain;
			for(unsigned int  i=0;i<NRMID;i=i+STEP) Ronda(i^0xff,W);
		}//endifremain
return c;//need it to write the tag into c
}

//must be used with a prior check that clen is >=16
void Processciphertxt(u64 W[5],unsigned char *m,
const unsigned char *c,unsigned long long clen){

		u64 remain=clen-16;
		while(remain>=16){
			LESTOREu64in8bytearray(W[3],m);
			for(u32 sk=0;sk<8;sk++) m[sk]^=c[sk];
			W[3]=LELOAD8bytearrayinu64(c);m+=8;c+=8;

			LESTOREu64in8bytearray(W[2],m);
			for(u32 sk=0;sk<8;sk++) m[sk]^=c[sk];
			W[2]=LELOAD8bytearrayinu64(c);

			for(unsigned int  i=0;i<NRMID;i=i+STEP) Ronda(i^0xff,W);
			remain-=16;
			m+=8;c+=8;
		}//endwhile

		if(remain){
			if(remain<8){
			PartialLESTOREu64inbytearray(W[3],m,remain);//m=trunc(u)
			for(u32 s=0;s<remain;s++) m[s]^=c[s];
			//now m is trunc(u)^c
			W[3]^=PartialLELOADbytearrayinu64(m,remain);/*u^=(u^c) gives u=c in msg bytes ,
																								while u^=0 leaves the other ones untouched*/
			c+=remain;
			W[3]^=((u64)(1)<<(8*remain));
			}
			else{
				LESTOREu64in8bytearray(W[3],m);
				for(u32 sk=0;sk<8;sk++) m[sk]^=c[sk];
				W[3]=LELOAD8bytearrayinu64(c);m+=8;c+=8;remain-=8;
				if(remain){
					PartialLESTOREu64inbytearray(W[2],m,remain);
					for(u32 s=0;s<remain;s++) m[s]^=c[s];
					W[2]^=PartialLELOADbytearrayinu64(m,remain);
				}
				W[2]^=((u64)(1)<<(8*remain));
				c+=remain;
			}

			W[1]^=1;//distinguisher from remain=0 case
			for(unsigned int  i=0;i<NRMID;i=i+STEP) Ronda(i^0xff,W);
		}//endifremain

}







//main functions below






//=======================ENCRYPT======================

int crypto_aead_encrypt(
unsigned char *c,unsigned long long *clen,
const unsigned char *m,unsigned long long mlen,
const unsigned char *ad,unsigned long long adlen,
const unsigned char *nsec,
const unsigned char *npub,
const unsigned char *k
){
/*generating a ciphertext c[0],c[1],...,c[*clen-1]
... from a plaintext m[0],m[1],...,m[mlen-1]
... and associated data ad[0],ad[1],...,ad[adlen-1]
... and nonce npub[0],npub[1],...
... and secret key k[0],k[1],...
... the implementation shall not use nsec*/

		(void)nsec;//avoid warning
//init
	u64 W[5];
	Initialize(W,npub,k);

//ad
		ProcessAd(W,ad,adlen);

//cipher
c=Processplaintxt(W,c,m,mlen);

//finalization
	for(unsigned int i=NRMID;i<NRMID+NREND;i=i+STEP) Ronda(i^0xff,W);

	//tag:
	LESTOREu64in8bytearray(W[0],c);c+=8;
	LESTOREu64in8bytearray(W[1],c);
	*clen = mlen+16;

return 0;
}








//=========================DECRYPT=============================


int crypto_aead_decrypt(
unsigned char *m,unsigned long long *mlen,
unsigned char *nsec,
const unsigned char *c,unsigned long long clen,
const unsigned char *ad,unsigned long long adlen,
const unsigned char *npub,
const unsigned char *k
){
/*
... generating a plaintext m[0],m[1],...,m[*mlen-1]
... and secret message number nsec[0],nsec[1],...
... from a ciphertext c[0],c[1],...,c[clen-1]
... and associated data ad[0],ad[1],...,ad[adlen-1]
... and nonce number npub[0],npub[1],...
... and secret key k[0],k[1],...*/

/*if ciphertext is not valid return -1;*/

	(void)nsec;//avoid warning
//init
	u64 W[5];
	Initialize(W,npub,k);

//ad
		ProcessAd(W,ad,adlen);

//decipher
	if(clen<16) return -1;
	Processciphertxt(W,m,c,clen);
	c+=clen-16;

//finalization
	for(unsigned int i=NRMID;i<NRMID+NREND;i=i+STEP) Ronda(i^0xff,W);
	//tag check:

	unsigned char tagcheck[8];unsigned int flagtagcheck=0;
	LESTOREu64in8bytearray(W[0],tagcheck);
	for(u32 i=0;i<8;i++) if(c[i]!=tagcheck[i]){flagtagcheck=1;}
	LESTOREu64in8bytearray(W[1],tagcheck);
	for(u32 i=8;i<16;i++) if(c[i]!=tagcheck[i-8]){flagtagcheck=1;}
	if(flagtagcheck){*mlen=0; return -1;}
	*mlen=clen-16;


return 0;
}