// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/elmd600v2/ref/encrypt.c
// Function: crypto_aead_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
)
{
	u8 param[]={0x06,0,0,0,0x80,0,0,0}; 
	
	block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS;
	int i; u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1;
	unsigned long long cnt;
	for(i=0; i<16; i++)	{zeroes[i]=0x00;}   	
	for(i=1; i<16; i++)	{ozs[i]=0x00;} ozs[0] = 0x80; 	


	cnt = (8+mlen-1)/16 + 1;
	*clen = (cnt +1)* 16 + 1;

	if((mlen+8)%16 == 0) 
		c[*clen-1] = 0x00; 
	else 
		c[*clen-1] = 0x01; 

	key_schedule(k);

	/* ==========  Generate the Masks =========== */
	AES(6, ENCRYPT, blk, zeroes, &aes_key1);
	AES(6, ENCRYPT, L, blk, &aes_key1);

	mult_3(Delta_0, L); 
	mult_inv2(Delta_0, Delta_0); 

	mult_inv2(Delta_1, L); 	

	mult_3(Delta_2, L); 
	mult_3(Delta_2, Delta_2); 
	mult_inv2(Delta_2, Delta_2);

	/* ======  Process Associated Data ======== */
	for(i=0; i<16; i++)
		W[i]=0x00;
	process_AD(W, Delta_0, npub, param, ad, adlen);


	/* ================  Process Successive Message Blocks ==================== */

	
	/* ====== Process the first Message block, whose first 8 byte is the secret message number ===== */
	
	if(mlen < 8){ Is_complete = 0; }
	if(mlen <= 8) { blen = 8 + mlen; }
	load_block(blk, nsec, m, 8, blen-8); copy_block(CS, blk); 
	process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE);
	
	store_bytes(c, result, 0, 15); c +=16;
	if(mlen >= 8)  {mlen -= 8; m +=8;}
	else mlen = 0;

	/* ============= Process Message blocks ================== */
	while(mlen > 0){
		if(mlen >= 16){
			load_block(blk, m, ozs, 16, 0);
			if(mlen == 16) {xor_block(blk, blk, CS); }
			else xor_block(CS, CS, blk);
			blen = 16; mlen -= 16; m+=16; 
		}
		else 	{Is_complete = 0; blen = mlen; mlen = 0; 
			load_block(blk, m, ozs, blen, 0); xor_block(blk, CS, blk);
			
		}	
		process_block(Delta_1, Delta_2, result, blk, W, Is_complete, ENCRYPT, Is_final, MESSAGE); 
		store_bytes(c, result, 0, 15); c +=16;
		
	}

	/* ================ Process checksum block ====================== */
	process_block(Delta_1, Delta_2, result, blk, W, 1, ENCRYPT, 1, MESSAGE); 
	store_bytes(c, result, 0, 15);
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, 0x70(%rsp)
movq %r8, %rbp
movq %rdi, %rbx
movq 0x110(%rsp), %rdi
movabsq $0x8000000006, %rax     # imm = 0x8000000006
movq %rax, 0x88(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2295
movb $0x0, 0x90(%rsp,%rax)
incq %rax
jmp 0x2282
pushq $0x1
popq %rax
cmpq $0x10, %rax
je 0x22a8
movb $0x0, 0x10(%rsp,%rax)
incq %rax
jmp 0x2298
movq %rdx, 0x80(%rsp)
movb $-0x80, 0x10(%rsp)
leaq 0x8(%rcx), %r15
movq %rcx, 0x78(%rsp)
leaq 0x7(%rcx), %rax
andq $-0x10, %rax
leaq 0x21(%rax), %rcx
movq %rcx, (%rsi)
testb $0xf, %r15b
setne 0x20(%rbx,%rax)
callq 0x2576
movq 0x4cee(%rip), %r13      # 0x6fd0
leaq 0x90(%rsp), %rdi
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movq %r13, %rdx
callq 0x1050
leaq 0xb0(%rsp), %r14
movq %r12, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1050
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x25a0
movq %r12, %rdi
movq %r12, %rsi
callq 0x25dd
leaq 0x50(%rsp), %rdi
movq %r14, %rsi
callq 0x25dd
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x25a0
movq %r12, %rdi
movq %r12, %rsi
callq 0x25a0
movq %r12, %rdi
movq %r12, %rsi
callq 0x25dd
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2373
movb $0x0, 0x60(%rsp,%rax)
incq %rax
jmp 0x2363
leaq 0x60(%rsp), %r13
leaq 0xa0(%rsp), %rsi
leaq 0x88(%rsp), %rcx
movq %r13, %rdi
movq 0x108(%rsp), %rdx
movq %rbp, %r8
movq 0x70(%rsp), %r9
callq 0x2629
xorl %ebp, %ebp
movq 0x78(%rsp), %r12
cmpq $0x8, %r12
setae %bpl
addb $-0x8, %r15b
cmpq $0x9, %r12
movzbl %r15b, %eax
pushq $0x8
popq %rcx
cmovael %ecx, %eax
movzbl %al, %r8d
leaq 0x30(%rsp), %r14
movq %r14, %rdi
movq 0x100(%rsp), %rsi
movq 0x80(%rsp), %r15
movq %r15, %rdx
callq 0x2714
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
callq 0x2761
subq $0x8, %rsp
pushq $0x1
popq %rax
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %r14, %rcx
movq %r13, %r8
movl %ebp, %r9d
pushq %rax
pushq $0x0
pushq %rax
callq 0x2775
addq $0x20, %rsp
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2431
movb (%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x2420
leaq -0x8(%r12), %r13
xorl %eax, %eax
xorl %ebp, %ebp
cmpq $0x8, %r12
setae %bpl
leaq (%r15,%rbp,8), %r14
cmovbq %rax, %r13
leaq 0x30(%rsp), %r15
testq %r13, %r13
je 0x251d
cmpq $0x10, %r13
jb 0x2487
movq %r15, %rdi
movq %r14, %rsi
leaq 0x10(%rsp), %rdx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x2714
cmpq $0x10, %r13
jne 0x24b5
movq %r15, %rdi
movq %r15, %rsi
leaq 0x20(%rsp), %rdx
jmp 0x24c0
movzbl %r13b, %ecx
xorl %r13d, %r13d
movq %r15, %rdi
movq %r14, %rsi
leaq 0x10(%rsp), %rdx
xorl %r8d, %r8d
callq 0x2714
movq %r15, %rdi
leaq 0x20(%rsp), %rsi
movq %r15, %rdx
callq 0x28fa
xorl %ebp, %ebp
jmp 0x24cd
leaq 0x20(%rsp), %rdi
movq %rdi, %rsi
movq %r15, %rdx
callq 0x28fa
addq $-0x10, %r13
addq $0x10, %r14
leaq 0x10(%rbx), %r12
subq $0x8, %rsp
movzbl %bpl, %r9d
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movq %r15, %rcx
leaq 0x68(%rsp), %r8
pushq $0x1
popq %rax
pushq %rax
pushq $0x0
pushq %rax
callq 0x2775
addq $0x20, %rsp
pushq $0x10
popq %rax
cmpq $0x20, %rax
je 0x2515
movb -0x10(%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x2503
movq %r12, %rbx
jmp 0x244f
subq $0x8, %rsp
leaq 0x58(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x38(%rsp), %rcx
leaq 0x68(%rsp), %r8
pushq $0x1
popq %r9
pushq %r9
pushq %r9
pushq %r9
callq 0x2775
addq $0x20, %rsp
pushq $0x10
popq %rax
cmpq $0x20, %rax
je 0x2562
movb -0x10(%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x2550
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq