// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/elmd1001v2/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,0,0x01,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;
	for(i=0; i<16; i++)	{zeroes[i]=0x00;}   	
	for(i=1; i<16; i++)	{ozs[i]=0x00;} ozs[0] = 0x80; 	

	*clen = mlen + 24; 

	key_schedule(k);

	/* ==========  Generate the Masks =========== */
	AES(10, ENCRYPT, L, zeroes, &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, CS, blk); }
			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, blen-1);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, %r13
movq %r8, %rbp
movq %rcx, %r12
movq %rdx, 0x8(%rsp)
movq %rdi, %rbx
movq 0x110(%rsp), %rdi
movabsq $0x800100000a, %rax     # imm = 0x800100000A
movq %rax, 0x68(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2268
movb $0x0, 0x80(%rsp,%rax)
incq %rax
jmp 0x2255
pushq $0x1
popq %rax
cmpq $0x10, %rax
je 0x227b
movb $0x0, 0x20(%rsp,%rax)
incq %rax
jmp 0x226b
movb $-0x80, 0x20(%rsp)
leaq 0x18(%r12), %rax
movq %rax, (%rsi)
callq 0x2516
movq 0x4d3c(%rip), %rdx      # 0x6fd0
leaq 0x80(%rsp), %rdi
leaq 0xb0(%rsp), %r14
movq %r14, %rsi
callq 0x1050
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x2540
movq %r15, %rdi
movq %r15, %rsi
callq 0x257d
leaq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x257d
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x2540
movq %r15, %rdi
movq %r15, %rsi
callq 0x2540
movq %r15, %rdi
movq %r15, %rsi
callq 0x257d
xorl %eax, %eax
cmpq $0x10, %rax
je 0x230f
movb $0x0, 0x50(%rsp,%rax)
incq %rax
jmp 0x22ff
leaq 0x50(%rsp), %r15
leaq 0xa0(%rsp), %rsi
leaq 0x68(%rsp), %rcx
movq %r15, %rdi
movq 0x108(%rsp), %rdx
movq %rbp, %r8
movq %r13, %r9
callq 0x25c9
xorl %r14d, %r14d
cmpq $0x8, %r12
setae %r14b
leal 0x8(%r12), %eax
cmpq $0x9, %r12
movzbl %al, %eax
pushq $0x10
popq %rcx
cmovbl %eax, %ecx
movq %rcx, (%rsp)
leal -0x8(%rcx), %eax
movzbl %al, %r8d
leaq 0x90(%rsp), %r13
pushq $0x8
popq %rcx
movq %r13, %rdi
movq 0x100(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x26b4
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x2701
subq $0x8, %rsp
pushq $0x1
popq %rax
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r13, %rcx
movq %r15, %r8
movl %r14d, %r9d
pushq %rax
pushq $0x0
pushq %rax
callq 0x2715
addq $0x20, %rsp
xorl %eax, %eax
cmpq $0x10, %rax
je 0x23cc
movb 0x10(%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x23ba
leaq -0x8(%r12), %rbp
xorl %eax, %eax
xorl %r15d, %r15d
cmpq $0x8, %r12
setae %r15b
movq 0x8(%rsp), %rcx
leaq (%rcx,%r15,8), %r12
cmovbq %rax, %rbp
leaq 0x10(%rbx), %r14
testq %rbp, %rbp
je 0x24b9
cmpq $0x10, %rbp
jb 0x243c
movq %r13, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x26b4
cmpq $0x10, %rbp
leaq 0x70(%rsp), %rsi
movq %rsi, %rdi
cmoveq %r13, %rdi
movq %r13, %rdx
callq 0x289a
addq $-0x10, %rbp
addq $0x10, %r12
movb $0x10, %al
movq %rax, (%rsp)
jmp 0x246e
movl %ebp, %eax
movq %rax, (%rsp)
movzbl %bpl, %ecx
xorl %ebp, %ebp
movq %r13, %rdi
movq %r12, %rsi
leaq 0x20(%rsp), %rdx
xorl %r8d, %r8d
callq 0x26b4
movq %r13, %rdi
leaq 0x70(%rsp), %rsi
movq %r13, %rdx
callq 0x289a
xorl %r15d, %r15d
subq $0x8, %rsp
movzbl %r15b, %r9d
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rdx
movq %r13, %rcx
leaq 0x58(%rsp), %r8
pushq $0x1
popq %rax
pushq %rax
pushq $0x0
pushq %rax
callq 0x2715
addq $0x20, %rsp
pushq $0x10
popq %rax
cmpq $0x20, %rax
je 0x24b1
movb (%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x24a0
movq %r14, %rbx
jmp 0x23eb
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rbx
leaq 0x98(%rsp), %rcx
leaq 0x58(%rsp), %r8
movq %rbx, %rdx
pushq $0x1
popq %r9
pushq %r9
pushq %r9
pushq %r9
callq 0x2715
addq $0x20, %rsp
movzbl (%rsp), %ecx
decl %ecx
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2881
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq