// File: /workspace/github_c_makefile_star2_O1/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, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, %r15
movq %rdx, %r14
movq %rdi, 0x8(%rsp)
movq 0x110(%rsp), %r12
movabsq $0x800100000a, %rax     # imm = 0x800100000A
leaq 0x78(%rsp), %rcx
movq %rax, (%rcx)
vxorps %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %rbx
vmovaps %xmm0, (%rbx)
leaq 0x18(%r15), %rax
movq %rax, (%rsi)
movq 0x5d24(%rip), %rbp      # 0x7fd0
movq %r12, %rdi
movl $0x80, %esi
movq %rbp, %rdx
callq 0x1090
movq 0x5d1d(%rip), %rdx      # 0x7fe0
movq %r12, %rdi
movl $0x80, %esi
callq 0x1040
leaq 0xb0(%rsp), %r13
movq %rbx, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1060
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
callq 0x259a
movq %rbp, %rdi
movq %rbp, %rsi
callq 0x25ff
leaq 0x60(%rsp), %rdi
movq %r13, %rsi
callq 0x25ff
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %r13, %rsi
callq 0x259a
movq %r12, %rdi
movq %r12, %rsi
callq 0x259a
movq %r12, %rdi
movq %r12, %rsi
callq 0x25ff
leaq 0x80(%rsp), %r13
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%r13)
movq %r13, %rdi
movq %rbp, %rsi
movq 0x108(%rsp), %rdx
leaq 0x78(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
callq 0x2679
leal 0x8(%r15), %eax
cmpq $0x9, %r15
movzbl %al, %eax
movl $0x10, %ebx
cmovbl %eax, %ebx
leal -0x8(%rbx), %eax
movzbl %al, %r8d
leaq 0x10(%rsp), %r12
movq %r12, %rdi
movq 0x100(%rsp), %rsi
movq %r14, %rdx
movl $0x8, %ecx
callq 0x278f
vmovaps (%r12), %xmm0
vmovaps %xmm0, 0x30(%rsp)
xorl %r9d, %r9d
cmpq $0x8, %r15
setae %r9b
subq $0x8, %rsp
movl $0x1, %eax
leaq 0x48(%rsp), %rbp
leaq 0x68(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rbp, %rdx
movq %r12, %rcx
movq %r13, %r8
movq %r9, %r13
pushq %rax
pushq $0x0
pushq %rax
callq 0x27f8
movq 0x28(%rsp), %rax
addq $0x20, %rsp
vmovaps (%rbp), %xmm0
vmovups %xmm0, (%rax)
cmpq $0x9, %r15
jb 0x2535
addq $-0x8, %r15
addq $0x8, %r14
addq $0x10, %rax
movq %rax, 0x28(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
cmpq $0x10, %r15
jb 0x246f
movb (%r14,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
cmpq $0x10, %rax
jne 0x241e
vmovaps 0x10(%rsp), %xmm1
vxorps 0x30(%rsp), %xmm1, %xmm0
vmovaps %xmm0, %xmm2
cmpq $0x10, %r15
movq %r13, %rax
je 0x244c
vmovaps %xmm1, %xmm2
vmovaps %xmm2, 0x10(%rsp)
movq 0x8(%rsp), %r13
je 0x245f
vmovaps %xmm0, 0x30(%rsp)
addq $-0x10, %r15
addq $0x10, %r14
movq %r15, %rbp
movb $0x10, %r15b
jmp 0x24ca
movb (%r14,%rax), %cl
movb %cl, 0x10(%rsp,%rax)
incq %rax
cmpq %rax, %r15
jne 0x246f
movb $-0x80, 0x10(%rsp,%r15)
cmpq $0xf, %r15
movq 0x8(%rsp), %r13
je 0x24b4
leaq (%rsp,%r15), %rdi
addq $0x10, %rdi
incq %rdi
movl $0xe, %edx
subl %r15d, %edx
movl $0x0, %eax
cmovbl %eax, %edx
incl %edx
xorl %esi, %esi
callq 0x1030
vmovaps 0x30(%rsp), %xmm0
vxorps 0x10(%rsp), %xmm0, %xmm0
vmovaps %xmm0, 0x10(%rsp)
xorl %ebp, %ebp
xorl %eax, %eax
movl %r15d, %ebx
movq %r12, %r15
shlq $0x4, %r15
addq 0x28(%rsp), %r15
addq $0x10, %r13
movq %r13, 0x8(%rsp)
subq $0x8, %rsp
movq %rax, %r13
movzbl %al, %r9d
leaq 0x68(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x48(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x88(%rsp), %r8
movl $0x1, %eax
pushq %rax
pushq $0x0
pushq %rax
callq 0x27f8
addq $0x20, %rsp
vmovaps 0x40(%rsp), %xmm0
vmovups %xmm0, (%r15)
incq %r12
movq %rbp, %r15
testq %rbp, %rbp
jne 0x2416
subq $0x8, %rsp
leaq 0x68(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x48(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x88(%rsp), %r8
movl $0x1, %r9d
movl $0x1, %eax
pushq %rax
pushq %rax
pushq %rax
callq 0x27f8
addq $0x20, %rsp
movzbl %bl, %eax
xorl %ecx, %ecx
movq 0x8(%rsp), %r13
movb 0x40(%rsp,%rcx), %dl
movb %dl, 0x10(%r13,%rcx)
incq %rcx
cmpl %ecx, %eax
jne 0x2576
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq