encrypt.c 15.6 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 488 489 490 491 492 493 494 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 528 529 530 531 532 533 534 535 536 537 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 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 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 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
/*
 * encrypt.c
 *
 *  Created on: 22 Feb 2019
 *      Author: mrz
 */

#include "crypto_aead.h"
#include "led.h"
#include "cilipadi.h"
#include "api.h"
#include <stdio.h>
#include <stdlib.h> // malloc() and free()

/*
 * pad the message and AD if necessary
 */
int pad(
		const unsigned char *m,
		unsigned long long mlen,
		const unsigned char *ad,
		unsigned long long adlen,
		unsigned char *mx,
		unsigned long long mxlen,
		unsigned char *adx,
		unsigned long long adxlen) {
	int i;

	// original message
	for (i = 0; i < mlen; ++i) {
		mx[i] = m[i];
	}

	// pad only if length is not a multiple of r bits
	if (mxlen > mlen) {
		// bit 1 followed by zeros
		mx[mlen] = 0x80;

		for (i = mlen+1; i < mxlen; ++i) {
			mx[i] = 0;
		}
	}

	// original AD
	for (i = 0; i < adlen; ++i) {
		adx[i] = ad[i];
	}

	// pad only if length is not a multiple of r bits
	if (adxlen > adlen) {
		// bit 1 followed by zeros
		adx[adlen] = 0x80;

		for (i = adlen+1; i < adxlen; ++i) {
			adx[i] = 0;
		}
	}

	return 0;
}


/*
 * Check the length of the message and AD whether they need padding
 */
int padding_len_check(
		unsigned long long mlen,
		unsigned long long adlen,
		unsigned long long *mxlen,
		unsigned long long *adxlen) {
	unsigned long long x;

	// check padding on the plaintext
	x = mlen % BYTERATE;

	// if mlen is already a multiple of r bits, i.e. x=0, then set x = r to offset BYTERATE in the equation below
	if (x==0) x = BYTERATE;

	*mxlen = mlen + BYTERATE - x;

	// check padding on the AD
	x = adlen % BYTERATE;

	// if length is already a multiple of r bits, i.e. x=0, then set x = r to offset BYTERATE in the equation below
	if (x==0) x = BYTERATE;

	*adxlen = adlen + BYTERATE - x;

	return 0;
}

/*
 * Initializataion Phase
 */
int init_phase(unsigned char *state, const unsigned char *npub, const unsigned char *k) {
	int i;

	// fill in the key
	for (i=0; i<CRYPTO_KEYBYTES; i++) {
		state[i] = k[i];
	}

	// fill in the nonce
	for (i=CRYPTO_KEYBYTES; i<STATELEN; i++) {
		state[i] = npub[i-CRYPTO_KEYBYTES];
	}

	//printf("initial state = \n");
	//for (i=0; i<STATELEN; i++) printf("%02x", state[i]); printf("\n");

	permutation_384(state, AROUNDS);

	return 0;
}

int ad_phase(unsigned char *state, unsigned char *state_r, const unsigned char *ad, unsigned long long adlen) {
	int i, j, s_adlen;

	s_adlen = adlen / BYTERATE;

#ifdef DEBUG
	printf("  state = ");
	for (j=0; j<STATELEN; j++) printf("%02x", state[j]); printf("\n");
#endif

	for (i = 0; i < s_adlen; ++i) {
		for (j = 0; j < BYTERATE; ++j) {
			state_r[j] = ad[i*BYTERATE+j];
		}

		// XOR with AD
		xor_bytes(state, state_r, BYTERATE);

#ifdef DEBUG
		printf("  AD_{%d} = ", i+1);
		for (j=0; j<BYTERATE; j++) printf("%02x", ad[i*BYTERATE+j]); printf("\n");
		printf("  after XOR with AD = ");
		for (j=0; j<STATELEN; j++) printf("%02x", state[j]); printf("\n");
#endif

		permutation_384(state, BROUNDS);
	}

	// XOR the last bit of the state with '1' to indicate completion of AD phase
	state[STATELEN-1] ^= 1;

#ifdef DEBUG
		printf("  end of AD Phase = ");
		for (j=0; j<STATELEN; j++) printf("%02x", state[j]); printf("\n");
#endif
	return 0;
}

/*
 * enc = { 0, 1 }. 0 = decrypt, 1 = encrypt
 */
int ciphering_phase(unsigned char *state,
		unsigned char *state_r,
		const unsigned char *in,
		unsigned long long inlen,
		unsigned long long unpadded_inlen,
		unsigned char *out,
		int enc) {
	int i, j;
	int t_inlen;

	// for decryption, t_inlen has to be deducted by one when processing the ciphertext to not count the tag
	t_inlen = inlen/BYTERATE;
	//if (enc==0) t_inlen--;

	//printf("t_mlen = %d\n", t_inlen);

	// allocate array for ciphertext
	//c = malloc((size_t)(mlen + taglen)); // ciphertext + tag

	// encryption
	if (enc) {
		for (i = 0; i < t_inlen; ++i) {

#ifdef DEBUG
			printf("  M%2d: ", i+1);
			// if this is the last message, print only the actual message
			if (i == (t_inlen-1)) {
				// it is the last message and the message is already in multiple of r bits
				if ((unpadded_inlen % BYTERATE)==0) {
					for (j = 0; j < BYTERATE; ++j) printf("%02x", in[i*BYTERATE+j]);
					printf("\n");
				}
				else {
					for (j = 0; j < (unpadded_inlen % BYTERATE); ++j) printf("%02x", in[i*BYTERATE+j]);
					printf("\n");
				}
			}
			else {
				for (j = 0; j < BYTERATE; ++j) printf("%02x", in[i*BYTERATE+j]);
				printf("\n");
			}
			printf("  state (before XOR with M%2d= ", i+1); for (j = 0; j < STATELEN; ++j) printf("%02x", state[j]);
			printf("\n");
#endif
			for (j = 0; j < BYTERATE; ++j) {
				state_r[j] = in[i*BYTERATE+j];
			}

			// XOR message with bitrate part of the state
			xor_bytes(state, state_r, BYTERATE);

			// output ciphertext
			// if this is the last message, only use unpadded one
			if (i == (t_inlen-1)) {
				if ((unpadded_inlen % BYTERATE)==0) {
					for (j = 0; j < BYTERATE; ++j) {
						out[i*BYTERATE+j] = state[j];
					}
				}
				else {
					for (j = 0; j < (unpadded_inlen % BYTERATE); ++j) {
						out[i*BYTERATE+j] = state[j];
					}
				}
			}
			else {
				for (j = 0; j < BYTERATE; ++j) {
					out[i*BYTERATE+j] = state[j];
				}
			}

#ifdef DEBUG
			printf("  state (after XOR with M%2d= ", i+1); for (j = 0; j < STATELEN; ++j) printf("%02x", state[j]);
			printf("\n");
			printf("  C%2d: ", i+1);
			// if this is the last message, print only the actual message
			if (i == (t_inlen-1)) {
				// it is the last message and the message is already in multiple of r bits
				if ((unpadded_inlen % BYTERATE)==0) {
					for (j = 0; j < BYTERATE; ++j) printf("%02x", out[i*BYTERATE+j]);
					printf("\n");
				}
				else {
					for (j = 0; j < (unpadded_inlen % BYTERATE); ++j) printf("%02x", out[i*BYTERATE+j]);
					printf("\n");
				}
			}
			else {
				for (j = 0; j < BYTERATE; ++j) printf("%02x", out[i*BYTERATE+j]);
				printf("\n");
			}
#endif

			//printf("state (after XOR with message %2d) \n", i+1);
			//for (j = 0; j < STATELEN; ++j) printf("%02x", state[j]); printf("\n");

			if ((i+1) < t_inlen) {
				permutation_384(state, BROUNDS);
			}
		}
	}
	// decryption
	else {
		for (i = 0; i <t_inlen; ++i) {

#ifdef DEBUG
			printf("  C%2d: ", i+1);
			// if this is the last message, print only the actual message
			if (i == (t_inlen-1)) {
				if ((unpadded_inlen % BYTERATE)==0) {
					for (j = 0; j < BYTERATE; ++j) printf("%02x", in[i*BYTERATE+j]);
					printf("\n");
				}
				else {
					for (j = 0; j < (unpadded_inlen % BYTERATE); ++j) printf("%02x", in[i*BYTERATE+j]);
					printf("\n");
				}
			}
			else {
				for (j = 0; j < BYTERATE; ++j) printf("%02x", in[i*BYTERATE+j]);
				printf("\n");
			}
#endif
			// XOR ciphertext with bitrate part of the state to obtain message
			// if this is the last message, only use unpadded one
			if (i == (t_inlen-1)) {
				if ((unpadded_inlen % BYTERATE)==0) {
					for (j = 0; j < BYTERATE; ++j) {
						out[i*BYTERATE+j] = in[i*BYTERATE+j] ^ state[j];
					}
				}
				else {
					for (j = 0; j < (unpadded_inlen % BYTERATE); ++j) {
						out[i*BYTERATE+j] = in[i*BYTERATE+j] ^ state[j];
					}
				}
			}
			else {
				for (j = 0; j < BYTERATE; ++j) {
					out[i*BYTERATE+j] = in[i*BYTERATE+j] ^ state[j];
				}
			}

#ifdef DEBUG
			printf("  M%2d: ", i+1);
			// if this is the last message, print only the actual message
			if (i == (t_inlen-1)) {
				if ((unpadded_inlen % BYTERATE)==0) {
					for (j = 0; j < BYTERATE; ++j) printf("%02x", out[i*BYTERATE+j]);
					printf("\n");
				}
				else {
					for (j = 0; j < (unpadded_inlen % BYTERATE); ++j) printf("%02x", out[i*BYTERATE+j]);
					printf("\n");
				}
			}
			else {
				for (j = 0; j < BYTERATE; ++j) printf("%02x", out[i*BYTERATE+j]);
				printf("\n");
			}

			printf("  state (before replace with ciphertext = ");
			for (j = 0; j < STATELEN; ++j) printf("%02x", state[j]);
			printf("\n");
#endif

			// replace bitrate part of the state with the current ciphertext block
			// if this is the last message, we need to handle it differently
			if (i == (t_inlen-1)) {
				if ((unpadded_inlen % BYTERATE)==0) {
					for (j = 0; j < BYTERATE; ++j) {
						state[j] = in[i*BYTERATE+j];
					}
				}
				else {
					for (j = 0; j < (unpadded_inlen % BYTERATE); ++j) {
						state[j] = in[i*BYTERATE+j];
					}
					// XOR state with the padded value (0x80)
					state[unpadded_inlen % BYTERATE] ^= in[i*BYTERATE+(unpadded_inlen % BYTERATE)];
				}
			}
			else {
				for (j = 0; j < BYTERATE; ++j) {
					state[j] = in[i*BYTERATE+j];
				}
			}
#ifdef DEBUG
			printf("  state (after replace with ciphertext = ");
			for (j = 0; j < STATELEN; ++j) printf("%02x", state[j]);
			printf("\n");
#endif

			//printf("state (after XOR with ciphertext %2d) \n", i+1);
			//for (j = 0; j < STATELEN; ++j) printf("%02x", state[j]); printf("\n");

			if ((i+1) < t_inlen) {
				permutation_384(state, BROUNDS);
			}
		}
	}

	//printf("state (after permutation) \n");
	//for (i = 0; i < STATELEN; ++i) printf("%02x", state[i]); printf("\n");

	/*
	if (enc) {
		for (i = 0; i < BYTERATE; ++i) {
			state_r[i] = in[(t_inlen-1)*BYTERATE+i];
		}

		// XOR message with bitrate part of the state
		xor_bytes(state, state_r, BYTERATE);

		// output ciphertext
		for (i = 0; i < BYTERATE; ++i) {
			out[inlen-CRYPTO_ABYTES+i] = state[i];
		}
	}
	else {

		// output message
		for (j = 0; j < BYTERATE; ++j) {
			out[inlen-BYTERATE+j] = in[(t_inlen-1)*BYTERATE+j] ^ state[j];
		}

		// replace bitrate part of the state with the current ciphertext block
		for (j = 0; j < BYTERATE; ++j) {
			state[j] = in[(t_inlen-1)*BYTERATE+j];
		}
	}
	*/

	//printf("state (after XOR with last input) \n");
	//for (i = 0; i < STATELEN; ++i) printf("%02x", state[i]); printf("\n");

	return 0;
}

/*
 * Finalization Phase
 */
int finalization_phase(unsigned char *state, const unsigned char *k) {
	//int i;

#ifdef DEBUG
	int j;
	printf("  state = ");
	for (j=0; j<STATELEN; j++) printf("%02x", state[j]); printf("\n");
#endif

	permutation_384(state, AROUNDS);

	//printf("state (after applying p^a_n) \n");
	//for (i = 0; i < STATELEN; ++i) printf("%02x", state[i]); printf("\n");

	// XOR with key
	xor_bytes(state, k, CRYPTO_KEYBYTES);

	//printf("state (after XOR with key) = \n");
	//for (i = 0; i < STATELEN; ++i) printf("%02x", state[i]); printf("\n");
	return 0;
}

/*
 * the code for the AEAD implementation goes here,
 *
 * 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
 *
 */
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 char state[STATELEN]; // state
	unsigned char state_r[BYTERATE]; // rate part of the state
	unsigned char *mx; // padded message
	unsigned long long mxlen = 0; // length of padded message
	unsigned char *adx; // padded AD
	unsigned long long adxlen = 0; // length of padded AD
	int i;

	/*
	 * Initialization
	 */
#ifdef DEBUG
	printf("-- INIT PHASE --\n");
#endif
	init_phase(state, npub, k);

	/*
	 * Padding check
	 */
	//printf("mlen = %llu, adlen = %llu, ", mlen, adlen);
	padding_len_check(mlen, adlen, &mxlen, &adxlen);
	//printf("mxlen = %llu, adxlen = %llu\n", mxlen, adxlen);

	// initialize padded message and AD
	mx = malloc((size_t)(mxlen));
	adx = malloc((size_t)(adxlen));

	pad(m, mlen, ad, adlen, mx, mxlen, adx, adxlen);

#ifdef DEBUG
	printf("  Padding Check\n");
	printf("   M = ");
	for (i = 0; i < mlen; ++i) printf("%02x", m[i]);
	printf("\n");

	printf("   M (padded) = ");
	for (i = 0; i < mxlen; ++i) printf("%02x", mx[i]);
	printf("\n");
#endif


	/*
	 * Processing the associated data
	 */
#ifdef DEBUG
	printf("-- AD PHASE --\n");
#endif
	//printf("state (before AD phase) = \n");
	//for (i=0; i<STATELEN; i++) printf("%02x", state[i]); printf("\n");

	ad_phase(state, state_r, adx, adxlen);

	//printf("state (after AD phase) = \n");
	//for (i=0; i<STATELEN; i++) printf("%02x", state[i]); printf("\n");


	/*
	 * Processing the plaintext
	 */
#ifdef DEBUG
	printf("-- MESSAGE ENCRYPTION PHASE --\n");
#endif
	ciphering_phase(state, state_r, mx, mxlen, mlen, c, 1);

	/*
	 * Finalization Phase
	 */
#ifdef DEBUG
	printf("-- FINALIZATION PHASE --\n");
#endif
	finalization_phase(state, k);

	// output the tag
	*clen = mlen + CRYPTO_ABYTES;
	for (i = 0; i < CRYPTO_ABYTES; ++i) {
		c[*clen-CRYPTO_ABYTES+i] = state[i];
	}

	free(mx);
	free(adx);

	return 0;
}

/*
 * the code for the AEAD implementation goes here,
 *
... 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],... ...
 */

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 char state[STATELEN]; // 16-byte state
	unsigned char state_r[BYTERATE]; // 8-byte rate part of the state
	unsigned char *cx; // padded message (+ tag)
	unsigned long long cxlen = 0; // length of padded message + tag
	unsigned long long c_onlylen = clen - CRYPTO_ABYTES; // length of ciphertext without tag
	unsigned char *adx; // padded AD
	unsigned long long adxlen = 0; // length of padded AD
	int i;
	unsigned char tag[CRYPTO_ABYTES]; // computed tag

	/*
	 * Initialization
	 */
#ifdef DEBUG
	int j;
	printf("-- INIT PHASE --\n");
#endif
	init_phase(state, npub, k);

	/*
	 * Padding check
	 */
	padding_len_check(c_onlylen, adlen, &cxlen, &adxlen);

	// initialize padded message and AD
	cx = malloc((size_t)(cxlen));
	adx = malloc((size_t)(adxlen));

	pad(c, c_onlylen, ad, adlen, cx, cxlen, adx, adxlen);

#ifdef DEBUG
	printf("  C = ");
	for (i = 0; i < c_onlylen; ++i) printf("%02x", c[i]);
	printf("\n");

	printf("  C (padded) = ");
	for (i = 0; i < cxlen; ++i) printf("%02x", cx[i]);
	printf("\n");
#endif

	/*
	 * Processing the associated data
	 */

#ifdef DEBUG
	printf("-- AD PHASE --\n");
#endif
	//printf("state (before AD phase) = \n");
	//for (i=0; i<STATELEN; i++) printf("%02x", state[i]); printf("\n");

	ad_phase(state, state_r, adx, adxlen);

	//printf("state (after AD phase) = \n");
	//for (i=0; i<STATELEN; i++) printf("%02x", state[i]); printf("\n");


	/*
	 * Processing the ciphertext
	 */
#ifdef DEBUG
	printf("-- MESSAGE DECRYPTION PHASE --\n");
#endif
	ciphering_phase(state, state_r, cx, cxlen, c_onlylen, m, 0);

	/*
	 * Finalization Phase
	 */
#ifdef DEBUG
	printf("-- FINALIZATION PHASE --\n");
#endif
	finalization_phase(state, k);

	// output the tag
	*mlen = clen - CRYPTO_ABYTES;
#ifdef DEBUG
	printf("\nComputed Tag =\n");
#endif
	for (i = 0; i < CRYPTO_ABYTES; ++i) {
		tag[i] = state[i];
#ifdef DEBUG
		printf("%02x", tag[i]);
#endif
	}

#ifdef DEBUG
	printf("\n");
#endif

	// compare computed tag with the one received
	for (i = 0; i < CRYPTO_ABYTES; ++i) {
		if (tag[i] != c[clen-CRYPTO_ABYTES+i]) {

#ifdef DEBUG
			printf("Ciphertext not authenticated!\n");
			printf("mlen: %llu,  clen: %llu, adlen: %llu\n", *mlen, clen, adlen);
			printf("cxlen: %llu, adxlen: %llu\n", cxlen, adxlen);
			printf("Message: "); for (j=0; j<*mlen; j++) printf("%02x", m[j]); printf("\n");
			printf("AD: "); for (j=0; j<adlen; j++) printf("%02x", ad[j]); printf("\n");
			printf("Key: "); for (j=0; j<CRYPTO_KEYBYTES; j++) printf("%02x", k[j]); printf("\n");
			printf("Ciphertext: "); for (j=0; j<c_onlylen; j++) printf("%02x", c[j]); printf("\n");
			printf("Tag in ciphertext: ");
			for (j = 0; j < CRYPTO_ABYTES; ++j) {
				printf("%02x", c[clen-CRYPTO_ABYTES]);
			}
			printf("\n");
#endif
			return -1;
		}
	}

	free(cx);
	free(adx);

	return 0;
}