// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x128, %rsp            # imm = 0x128
movq 0x140(%rsp), %rax
movq 0x138(%rsp), %rax
movq 0x130(%rsp), %rax
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movq %rdx, 0x110(%rsp)
movq %rcx, 0x108(%rsp)
movq %r8, 0x100(%rsp)
movq %r9, 0xf8(%rsp)
movq 0x4b3a(%rip), %rax      # 0x8130
movq %rax, 0xf0(%rsp)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movb $0x10, 0x1f(%rsp)
movb $0x0, 0x1e(%rsp)
movb $0x1, 0x1d(%rsp)
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x3645
movslq 0x6c(%rsp), %rax
movb $0x0, 0x30(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x3627
movl $0x1, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x366b
movslq 0x6c(%rsp), %rax
movb $0x0, 0x20(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x364d
movb $-0x80, 0x20(%rsp)
movq 0x108(%rsp), %rax
addq $0x8, %rax
subq $0x1, %rax
shrq $0x4, %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
movq 0x108(%rsp), %rax
addq $0x8, %rax
subq $0x1, %rax
movl $0x7f0, %ecx            # imm = 0x7F0
xorl %edx, %edx
divq %rcx
movq %rax, 0x60(%rsp)
cmpq $0x20, 0x60(%rsp)
jbe 0x36bd
movq $0x20, 0x60(%rsp)
movq 0x58(%rsp), %rcx
addq $0x1, %rcx
shlq $0x4, %rcx
addq $0x1, %rcx
movq 0x60(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movq 0x118(%rsp), %rax
movq %rcx, (%rax)
movq 0x108(%rsp), %rax
addq $0x8, %rax
andq $0xf, %rax
cmpq $0x0, %rax
jne 0x3718
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rcx
movq (%rcx), %rcx
subq $0x1, %rcx
movb $0x0, (%rax,%rcx)
jmp 0x3733
movq 0x120(%rsp), %rax
movq 0x118(%rsp), %rcx
movq (%rcx), %rcx
subq $0x1, %rcx
movb $0x1, (%rax,%rcx)
movq 0x140(%rsp), %rdi
callq 0x3c40
leaq 0xe0(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movl $0xa, %edi
movl $0x1, %esi
movq 0x6872(%rip), %r8       # 0x9fd0
callq 0x3c80
leaq 0xc0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3d20
leaq 0xc0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3d60
leaq 0xb0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3d60
leaq 0xa0(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3d20
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3d20
leaq 0xa0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x3d60
movl $0x0, 0x6c(%rsp)
cmpl $0x10, 0x6c(%rsp)
jge 0x380a
movslq 0x6c(%rsp), %rax
movb $0x0, 0xd0(%rsp,%rax)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x37e9
leaq 0xd0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movq 0x138(%rsp), %rdx
leaq 0xf0(%rsp), %rcx
movq 0x100(%rsp), %r8
movq 0xf8(%rsp), %r9
callq 0x3e20
cmpq $0x8, 0x108(%rsp)
jae 0x384f
movb $0x0, 0x1d(%rsp)
cmpq $0x8, 0x108(%rsp)
ja 0x386a
movq 0x108(%rsp), %rax
addq $0x8, %rax
movb %al, 0x1f(%rsp)
leaq 0x90(%rsp), %rdi
movq 0x130(%rsp), %rsi
movq 0x110(%rsp), %rdx
movzbl 0x1f(%rsp), %eax
subl $0x8, %eax
movl $0x8, %ecx
movzbl %al, %r8d
callq 0x3fa0
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x40a0
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0xd0(%rsp), %r8
movb 0x1d(%rsp), %al
movzbl %al, %r9d
movl $0x1, (%rsp)
movzbl 0x1e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x40f0
movq 0x120(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x42e0
movq 0x120(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x120(%rsp)
movq 0x50(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x50(%rsp)
cmpq $0x8, 0x108(%rsp)
jb 0x396a
movq 0x108(%rsp), %rax
subq $0x8, %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
addq $0x8, %rax
movq %rax, 0x110(%rsp)
jmp 0x3976
movq $0x0, 0x108(%rsp)
jmp 0x3978
cmpq $0x0, 0x108(%rsp)
jbe 0x3bc1
cmpq $0x10, 0x108(%rsp)
jb 0x3a25
leaq 0x90(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x3fa0
cmpq $0x10, 0x108(%rsp)
jne 0x39df
leaq 0x90(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x70(%rsp), %rdx
callq 0x4340
jmp 0x39f6
leaq 0x70(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x4340
movb $0x10, 0x1f(%rsp)
movq 0x108(%rsp), %rax
subq $0x10, %rax
movq %rax, 0x108(%rsp)
movq 0x110(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x110(%rsp)
jmp 0x3a7e
movb $0x0, 0x1d(%rsp)
movq 0x108(%rsp), %rax
movb %al, 0x1f(%rsp)
movq $0x0, 0x108(%rsp)
leaq 0x90(%rsp), %rdi
movq 0x110(%rsp), %rsi
leaq 0x20(%rsp), %rdx
xorl %r8d, %r8d
movzbl 0x1f(%rsp), %ecx
callq 0x3fa0
leaq 0x90(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x4340
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0xd0(%rsp), %r8
movb 0x1d(%rsp), %al
movzbl %al, %r9d
movl $0x1, (%rsp)
movzbl 0x1e(%rsp), %eax
movl %eax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x40f0
movq 0x120(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x42e0
movq 0x120(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x120(%rsp)
movq 0x50(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x50(%rsp)
cmpq $0x7f, 0x50(%rsp)
jne 0x3bbc
cmpq $0x20, 0x48(%rsp)
jae 0x3bbc
cmpq $0x0, 0x108(%rsp)
jbe 0x3bbc
leaq 0x80(%rsp), %rdx
leaq 0xd0(%rsp), %rcx
movl $0xa, %edi
xorl %esi, %esi
movq 0x6492(%rip), %r8       # 0x9fe0
callq 0x3c80
leaq 0xa0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x80(%rsp), %rdx
movl $0x1, %ecx
callq 0x43a0
movq 0x120(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x42e0
movq 0x120(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x120(%rsp)
movq $0x0, 0x50(%rsp)
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x3978
leaq 0xb0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x80(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0xd0(%rsp), %r8
movl $0x1, %r9d
movl $0x1, (%rsp)
movl $0x1, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
callq 0x40f0
movq 0x120(%rsp), %rdi
leaq 0x80(%rsp), %rsi
xorl %edx, %edx
movl $0xf, %ecx
callq 0x42e0
xorl %eax, %eax
addq $0x128, %rsp            # imm = 0x128
retq
nopw %cs:(%rax,%rax)