encrypt.c 9.69 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 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/*
 * Date: 29 November 2018
 * Contact: Thomas Peyrin - thomas.peyrin@gmail.com
 * Mustafa Khairallah - mustafam001@e.ntu.edu.sg
 * Email: mustafam001@e.ntu.edu.sg
 */

#include "crypto_aead.h"
#include "api.h"
#include "variant.h"
#include "bc.h"
#include <stdio.h>
#include <stdlib.h>

/*void display_vector (const unsigned char* x, int lenx) {
  int i;

  for (i = 0; i < lenx; i++) {
    printf("%02x",x[i]);
  }
  printf("\n");
  
  }*/

void pad (const unsigned char* m, unsigned char* mp, int l, int len8) {
  int i;

  for (i = 0; i < l; i++) {
    if (i < len8) {      
      mp[i] = m[i];
    }
    else if (i == l - 1) {
      mp[i] = (len8 & 0x0f);
    }
    else {
      mp[i] = 0x00;
    }      
  }
  
}

void g8A (unsigned char* s, unsigned char* c) {
  int i;

  for (i = 0; i < 16; i++) {
    c[i] = (s[i] >> 1) ^ (s[i] & 0x80) ^ ((s[i] & 0x01) << 7);
  }
  
}

void rho_ad (const unsigned char* m,
	     unsigned char* s,
	     int len8,
	     int ver) {
  int i;
  unsigned char mp [16];  

  //printf("rho in m  = ");display_vector(m,16);
  pad(m,mp,ver,len8);
  //printf("rho in mp = ");display_vector(mp,16);
  //printf("rho in s  = ");display_vector(s,16);
  for (i = 0; i < ver; i++) {
    s[i] = s[i] ^ mp[i];
  }
  //printf("rho out s = ");display_vector(s,16);
  
}

void rho (const unsigned char* m,
	  unsigned char* c,
	  unsigned char* s,
	  int len8,
	  int ver) {
  int i;
  unsigned char mp [16];

  //printf("rho in m  = ");display_vector(m,16);
  pad(m,mp,ver,len8);
  //printf("rho in mp = ");display_vector(mp,16);
  //printf("rho in s  = ");display_vector(s,16);

  g8A(s,c);
  for (i = 0; i < ver; i++) {
    s[i] = s[i] ^ mp[i];
    if (i < len8) {
      c[i] = c[i] ^ mp[i];
    }
    else {
      c[i] = 0;
    }
  }
  //printf("rho out s = ");display_vector(s,16);
  //printf("rho out c = ");display_vector(c,16);
  
}

void irho (unsigned char* m,
	  const unsigned char* c,
	  unsigned char* s,
	  int len8,
	  int ver) {
  int i;
  unsigned char cp [16];

  //printf("irho in c  = ");display_vector(c,16);
  pad(c,cp,ver,len8);
  //printf("irho in cp = ");display_vector(cp,16);
  //printf("irho in s  = ");display_vector(s,16);

  g8A(s,m);
  for (i = 0; i < ver; i++) {
    if (i < len8) {
      s[i] = s[i] ^ cp[i] ^ m[i];
    }
    else {
      s[i] = s[i] ^ cp[i];
    }
    if (i < len8) {
      m[i] = m[i] ^ cp[i];
    }
    else {
      m[i] = 0;
    }
  }
  //printf("irho out s = ");display_vector(s,16);
  //printf("irho out m = ");display_vector(c,16);
  
}

void lfsr_gf128 (unsigned char* CNT) {
  unsigned char fb0;
  
  fb0 = CNT[15] >> 7;

  CNT[15] = (CNT[15] << 1) | (CNT[14] >> 7);
  CNT[14] = (CNT[14] << 1) | (CNT[13] >> 7);
  CNT[13] = (CNT[13] << 1) | (CNT[12] >> 7);
  CNT[12] = (CNT[12] << 1) | (CNT[11] >> 7);
  CNT[11] = (CNT[11] << 1) | (CNT[10] >> 7);
  CNT[10] = (CNT[10] << 1) | (CNT[9] >> 7);
  CNT[9] = (CNT[9] << 1) | (CNT[8] >> 7);
  CNT[8] = (CNT[8] << 1) | (CNT[7] >> 7);
  CNT[7] = (CNT[7] << 1) | (CNT[6] >> 7);  
  CNT[6] = (CNT[6] << 1) | (CNT[5] >> 7);
  CNT[5] = (CNT[5] << 1) | (CNT[4] >> 7);
  CNT[4] = (CNT[4] << 1) | (CNT[3] >> 7);
  CNT[3] = (CNT[3] << 1) | (CNT[2] >> 7);
  CNT[2] = (CNT[2] << 1) | (CNT[1] >> 7);
  CNT[1] = (CNT[1] << 1) | (CNT[0] >> 7);
  if (fb0 == 1) {
    CNT[0] = (CNT[0] << 1) ^ 0x87;
  }
  else {
    CNT[0] = (CNT[0] << 1);
  }
}

void compose_tweakey (unsigned char* KT,
		      unsigned char* CNT,
		      unsigned char D) {

  int i;

  lfsr_gf128(CNT);
  for (i = 0; i < 15; i++) {
    KT[i] = CNT[i];
  }
  KT[i] = D ^ CNT[15];

}


void ice(unsigned char* s,
	 unsigned char* L,
	 unsigned char D) {
  unsigned char KT [16];

  compose_tweakey(KT,L,D);
  //printf("L = ");display_vector(L,16);
  //printf("D = %d\n",D);
  //printf("BC in kt = ");display_vector(KT,16);
  //printf("BC in s  = ");display_vector(s,16);
  block_cipher(s,KT);
  //printf("BC out s = ");display_vector(s,16);

}


void generate_tag (unsigned char* c, unsigned char* s) {
  
  g8A(s, c);

}

unsigned long long msg_encryption (const unsigned char** M, unsigned char** c,
				   unsigned char* L,
				   unsigned char*s, unsigned char D,
				   unsigned long long mlen) {
  int len8;

  
  if (mlen >= 16) {
    len8 = 16;
    mlen = mlen - 16;
  }
  else {
    len8 = mlen;
    mlen = 0;
  }
  rho(*M, *c, s, len8, 16);
  *c = *c + len8;
  *M = *M + len8;
  ice(s,L,D);
  return mlen;
}

unsigned long long msg_decryption (unsigned char** M, const unsigned char** c,
				   unsigned char* L,
				   unsigned char*s, unsigned char D,
				   unsigned long long clen) {
  int len8;

  
  if (clen >= 16) {
    len8 = 16;
    clen = clen - 16;
  }
  else {
    len8 = clen;
    clen = 0;
  }
  irho(*M, *c, s, len8, 16);
  *c = *c + len8;
  *M = *M + len8;
  ice(s,L,D);
  return clen;
}

unsigned long long ad_encryption (const unsigned char** M,
				  unsigned char* L,
				  unsigned char*s, unsigned char D,
				  unsigned long long adlen) {
  int len8;

  if (adlen >= 16) {
    len8 = 16;
    adlen = adlen - 16;
  }
  else {
    len8 = adlen;
    adlen = 0;
  }
  rho_ad(*M, s, len8, 16);
  *M = *M + len8;
  ice(s,L,D);
  return adlen;
  
}

void kdf (const unsigned char* k,
	  const unsigned char* npub,
	  unsigned char* s,
	  unsigned char* L) {
  unsigned int i;
  
  for (i = 0; i < 16; i = i + 1) {
    s[i] = npub[i];
  }  
  block_cipher(s,k);
  g8A(s,L);
  for (i = 0; i < 16; i = i + 1) {
    s[i] = 0x00;
  }

  
  
}

void encrypt_tag (unsigned char* s,
	     unsigned char* CNT,
	     unsigned char D) {
  unsigned char KT [16];
  int i;

  for (i = 0; i < 15; i++) {
    KT[i] = CNT[i];
  }
  KT[i] = D ^ CNT[15];
  //printf("BC in kt = ");display_vector(KT,16);
  //printf("BC in s  = ");display_vector(s,16);
  block_cipher(s,KT);
  //printf("BC out s = ");display_vector(s,16);
  
}


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
			 )
{

  unsigned long long mauthlen;
  unsigned char s[16];
  unsigned char L[16];
  unsigned char Lenc[16];
  unsigned char T[16];
  unsigned int i;

  (void) nsec;
  mauthlen = mlen;
  kdf(k,npub,s,L);

  //printf("s = ");display_vector(s,16);
  //printf("L = ");display_vector(L,16);

  for (i = 0; i < 16; i = i +1) {//Store the mask for the encryption part
    Lenc[i] = L[i];
  }

  // AD Len
  if (adlen == 0) { // Empty AD string
    ice(s,L,45);
  }
  else while (adlen > 0) {
      if (adlen < 16) { // Last AD block, partial
	adlen = ad_encryption(&ad,L,s,45,adlen);
      }
      else if (adlen == 16) {// Last AD block, full
	adlen = ad_encryption(&ad,L,s,44,adlen);
      }
      else { // Normal full AD block
	adlen = ad_encryption(&ad,L,s,36,adlen);
      }
    }

  // MSG_AUTH Len
  if (mauthlen == 0) { // Empty MSG string
    ice(s,L,47);
  }
  else while (mauthlen > 0) {
      if (mauthlen < 16) { // Last MSG block, partial
	mauthlen = ad_encryption(&m,L,s,47,mauthlen);
      }
      else if (mauthlen == 16) {// Last MSG block, full
	mauthlen = ad_encryption(&m,L,s,46,mauthlen);
      }
      else { // Normal full MSG block
	mauthlen = ad_encryption(&m,L,s,38,mauthlen);
      }
    }

  // Tag Generation
  generate_tag(T,s);
  //printf("T = ");display_vector(T,16);

  *clen = mlen + 16;
  m = m - mlen;
  
  // MSG Len
  if (mlen > 0) {
    for (i = 0; i < 16; i = i + 1) {
      s[i] = T[i];
    }
    encrypt_tag(s,Lenc,34);
    while (mlen > 16) {
      mlen = msg_encryption(&m,&c,Lenc,s,34,mlen);
    }
    rho(m, c, s, mlen, 16);
    c = c + mlen;
    m = m + mlen;
  }

  // Tag Concatenation
  for (i = 0; i < 16; i = i + 1) {
    *(c + i) = T[i];
  }

  c = c - *clen;
  
  return 0;
  
}

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
)
{
  unsigned long long mauthlen;
  unsigned char s[16];
  unsigned char L[16];
  unsigned char Lenc[16];
  unsigned char T[16];
  unsigned int i;
  const unsigned char* mauth;

  (void) nsec;
  mauth = m;

  mauthlen = clen - 16;
  kdf(k,npub,s,L);

  //printf("s = ");display_vector(s,16);

  for (i = 0; i < 16; i = i +1) {//Store the mask for the encryption part
    Lenc[i] = L[i];
  }

  for (i = 0; i < 16; i++) {
    T[i] = *(c + clen - 16 + i);
  }

  clen = clen - 16;
  *mlen = clen;
  
  // MSG Len
  if (clen > 0) {
    for (i = 0; i < 16; i = i + 1) {
      s[i] = T[i];
    }
    encrypt_tag(s,Lenc,34);
    while (clen > 16) {
      clen = msg_decryption(&m,&c,Lenc,s,34,clen);
    }
    irho(m, c, s, clen, 16);
    c = c + clen;
    m = m + clen;
  }

  m = m - *mlen;  

  for (i = 0; i < 16; i = i + 1) {
    s[i] = 0;
  }
  // AD Len
  if (adlen == 0) { // Empty AD string
    ice(s,L,45);
  }
  else while (adlen > 0) {
      if (adlen < 16) { // Last AD block, partial
	adlen = ad_encryption(&ad,L,s,45,adlen);
      }
      else if (adlen == 16) {// Last AD block, full
	adlen = ad_encryption(&ad,L,s,44,adlen);
      }
      else { // Normal full AD block
	adlen = ad_encryption(&ad,L,s,36,adlen);
      }
    }

  // MSG_AUTH Len
  if (mauthlen == 0) { // Empty MSG string
    ice(s,L,47);
  }
  else while (mauthlen > 0) {
      if (mauthlen < 16) { // Last MSG block, partial
	mauthlen = ad_encryption(&mauth,L,s,47,mauthlen);
      }
      else if (mauthlen == 16) {// Last MSG block, full
	mauthlen = ad_encryption(&mauth,L,s,46,mauthlen);
      }
      else { // Normal full MSG block
	mauthlen = ad_encryption(&mauth,L,s,38,mauthlen);
      }
    }

  // Tag generation 
  g8A(s, T);
  //printf("T = ");display_vector(T,16);
  for (i = 0; i < 16; i++) {
    if (T[i] != (*(c+i))) {
      return -1;
    }    
  }

  return 0;

}