// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/elmd101270v2/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[]={0x0a,0,0x7f,0x80,0x80,0,0,0}; 
	
	block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS;
	int i; 
	unsigned long long h, cnt, blk_ctr=0, blk_ctr1=0 ;
	u8 zeroes[16], ozs[16], blen = 16, Is_final = 0, Is_complete =1;
	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;
	h = (mlen+8-1) / (IT_GAP * 16);
	if(h > IT_MAX)
		h = IT_MAX;
	*clen = (cnt +1)* 16 + 1 + h*16 ;  

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

	key_schedule(k);

	/* ==========  Generate the Masks =========== */
	AES(10, ENCRYPT, L, zeroes, &aes_key1);
	//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 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; blk_ctr++; 
	if(mlen >= 8)  {mlen -= 8; m +=8;}
	else mlen = 0;

	/* ============= Process Successive 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; blk_ctr++; 
		
		if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && mlen>0) {
			AES(10, DECRYPT, result, W, &aes_key2); 	
			mask(Delta_2, result, result, 1); 
			store_bytes(c, result, 0, 15); c +=16; blk_ctr =0; blk_ctr1++;
		}
	}

	/* ================ 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, (%rsp)
movq %r8, 0x70(%rsp)
movq %rdx, %r12
movq %rdi, %r13
movq 0x110(%rsp), %rdi
movabsq $0x80807f000a, %rax     # imm = 0x80807F000A
movq %rax, 0x78(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2266
movb $0x0, 0x90(%rsp,%rax)
incq %rax
jmp 0x2253
pushq $0x1
popq %rax
cmpq $0x10, %rax
je 0x2279
movb $0x0, 0x40(%rsp,%rax)
incq %rax
jmp 0x2269
movb $-0x80, 0x40(%rsp)
leaq 0x8(%rcx), %r15
movq %rcx, 0x8(%rsp)
leaq 0x7(%rcx), %rax
movq %rax, %rcx
andq $-0x10, %rcx
movl $0x7f0, %r8d            # imm = 0x7F0
xorl %edx, %edx
divq %r8
xorl %ebp, %ebp
cmpq $0x20, %rax
pushq $0x20
popq %rdx
cmovbq %rax, %rdx
shll $0x4, %edx
leaq (%rcx,%rdx), %rax
addq %rdx, %rcx
addq $0x21, %rcx
movq %rcx, (%rsi)
testb $0xf, %r15b
setne 0x20(%r13,%rax)
callq 0x25df
movq 0x5cff(%rip), %rdx      # 0x7fd0
leaq 0x90(%rsp), %rdi
leaq 0xb0(%rsp), %rbx
movq %rbx, %rsi
callq 0x1050
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2609
movq %r14, %rdi
movq %r14, %rsi
callq 0x2646
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x2646
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2609
movq %r14, %rdi
movq %r14, %rsi
callq 0x2609
movq %r14, %rdi
movq %r14, %rsi
movq %r12, %r14
callq 0x2646
cmpq $0x10, %rbp
je 0x234d
movb $0x0, 0x30(%rsp,%rbp)
incq %rbp
jmp 0x233d
leaq 0x30(%rsp), %rbx
leaq 0xa0(%rsp), %rsi
leaq 0x78(%rsp), %rcx
movq %rbx, %rdi
movq 0x108(%rsp), %rdx
movq 0x70(%rsp), %r8
movq (%rsp), %r9
callq 0x2692
xorl %ebp, %ebp
movq 0x8(%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 0x80(%rsp), %r15
movq %r15, %rdi
movq 0x100(%rsp), %rsi
movq %r14, %rdx
callq 0x277d
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x27ca
subq $0x8, %rsp
pushq $0x1
popq %rax
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r15, %rcx
movq %rbx, %r8
movl %ebp, %r9d
pushq %rax
pushq $0x0
pushq %rax
callq 0x27de
addq $0x20, %rsp
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2407
movb 0x10(%rsp,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
jmp 0x23f3
addq $0x10, %r13
leaq -0x8(%r12), %rbp
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0x8, %r12
setae %al
movq %rax, 0x8(%rsp)
leaq (%r14,%rax,8), %r15
movl $0x0, %eax
movq %rax, (%rsp)
cmovbq %rcx, %rbp
pushq $0x1
popq %r14
leaq 0x80(%rsp), %r12
movq %r13, %rbx
testq %rbp, %rbp
je 0x2584
cmpq $0x10, %rbp
jb 0x2478
movq %r12, %rdi
movq %r15, %rsi
leaq 0x40(%rsp), %rdx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x277d
cmpq $0x10, %rbp
jne 0x24a5
movq %r12, %rdi
movq %r12, %rsi
leaq 0x50(%rsp), %rdx
jmp 0x24b0
movzbl %bpl, %ecx
xorl %ebp, %ebp
movq %r12, %rdi
movq %r15, %rsi
leaq 0x40(%rsp), %rdx
xorl %r8d, %r8d
callq 0x277d
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
movq %r12, %rdx
callq 0x2963
xorl %eax, %eax
jmp 0x24c2
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movq %r12, %rdx
callq 0x2963
addq $-0x10, %rbp
addq $0x10, %r15
movq 0x8(%rsp), %rax
subq $0x8, %rsp
movq %rax, 0x10(%rsp)
movzbl %al, %r9d
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r12, %rcx
leaq 0x38(%rsp), %r8
pushq $0x1
popq %rax
pushq %rax
pushq $0x0
pushq %rax
callq 0x27de
addq $0x20, %rsp
xorl %eax, %eax
cmpq $0x10, %rax
je 0x250a
movb 0x10(%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x24f8
leaq 0x10(%rbx), %r13
incq %r14
cmpq $0x7f, %r14
jne 0x243d
cmpq $0x1f, (%rsp)
ja 0x243d
testq %rbp, %rbp
je 0x243d
leaq 0x30(%rsp), %rdi
leaq 0x10(%rsp), %r14
movq %r14, %rsi
movq 0x5a9d(%rip), %rdx      # 0x7fe0
callq 0x1040
leaq 0x20(%rsp), %rdi
movq %r14, %rsi
movq %r14, %rdx
pushq $0x1
popq %rcx
callq 0x297a
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2571
movb 0x10(%rsp,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
jmp 0x255d
addq $0x20, %rbx
incq (%rsp)
xorl %r14d, %r14d
movq %rbx, %r13
jmp 0x243d
subq $0x8, %rsp
leaq 0x68(%rsp), %rdi
leaq 0x28(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x88(%rsp), %rcx
leaq 0x38(%rsp), %r8
pushq $0x1
popq %r9
pushq %r9
pushq %r9
pushq %r9
callq 0x27de
addq $0x20, %rsp
xorl %eax, %eax
cmpq $0x10, %rax
je 0x25cb
movb 0x10(%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x25b9
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq