// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/elmd101271v2/ref/encrypt.c
// Function: crypto_aead_decrypt
int crypto_aead_decrypt(
unsigned char *m,unsigned long long *mlen,
unsigned char *nsec,
const unsigned char *c, unsigned long long clen,
const unsigned char *ad, unsigned long long adlen,
const unsigned char *npub,
const unsigned char *k
)
{
	u8 param[]={0x0a,0,0x7f,0x81,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 outputmlen, blk_ctr=0, blk_ctr1=0;	
	for(i=0; i<16; i++)	{zeroes[i]=0x00;}   		// all zero.
	for(i=1; i<16; i++)	{ozs[i]=0x00;} ozs[0] = 0x80; 	//useful for padding.
	
	if(clen < 24)
		return -1;
	
	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); 

	
	 load_block(CS, zeroes, zeroes, 16, 0);

	 /* ================ Process Ciphertext Blocks ============ */
	 load_block(blk, c, zeroes, 16, 0);  

	 /* =================== Process 1st Block =================== */
	 if(clen<32){
	 	process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); 
		store_bytes(m, result, 8, 8+(clen-25)); outputmlen = clen - 24;
	 }
	 else {
		process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT);
		store_bytes(m, result, 8, 15);  m +=8; outputmlen = 8;
	 }
	 xor_block(CS, CS, result); store_bytes(nsec, result, 0, 7); 
	 clen -= 16; c+=16; blk_ctr ++; 

	 if(clen < 16){ 
		if(result[clen] != 0x80) return -1; 
		for(i=clen+1; i<16; i++) {if(result[i]!=0) return -1;} 
	 }

	/* ============= Process Successive Ciphertext Blocks ============== */
	while(clen > 16){
	   
	   load_block(blk, c, zeroes, 16, 0);  
	   if(clen < 32){ 
	   	process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); 
		xor_block(result, result, CS);
	   	store_bytes(m, result, 0, clen - 17); outputmlen += clen-16 ;
	   }
	   else{ 
	        process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT); 
		if(clen == 32) {xor_block(result, result, CS);}
		store_bytes(m, result, 0, 15); outputmlen += 16;
	   } 

	   xor_block(CS, CS, result);
	   clen -= 16; c+=16; blk_ctr ++; 

	   
	   if(clen < 16){ 
		if(result[clen] != 0x80) return -1; 
		for(i=clen+1; i<16; i++) {if(result[i]!=0) return -1;} 
	   }
	   else
		   m +=16; 

	   
	   if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && clen > 32){ 
		AES(10, DECRYPT, result, W, &aes_key2); 
		mask(Delta_2, result, result, 1); 
		for(i=0; i<16; i++) { if(c[i] != result[i]) {return -1; } }
		c +=16; clen -= 16; blk_ctr =0; blk_ctr1++;
	   }
	   
	}

	/* ==========  Process checksum block  ============= */
	Is_final = 1;
	process_block(Delta_1, Delta_2, result, CS, W, 1, ENCRYPT, Is_final, MESSAGE); 
	for(i=0; i<clen; i++) {if(result[i]!=c[i]) return -1;} 
	*mlen = outputmlen;
	
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, %r14
movq %r8, %r13
movq %rdx, %rbx
movq %rdi, %r15
movq 0x110(%rsp), %rdi
movabsq $0x80817f000a, %rax     # imm = 0x80817F000A
movq %rax, 0x98(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2a1c
movb $0x0, 0x70(%rsp,%rax)
incq %rax
jmp 0x2a0c
pushq $-0x1
popq %rax
cmpq $0x18, %r13
jb 0x2c20
movq %rcx, 0x18(%rsp)
movq %rsi, 0x90(%rsp)
callq 0x25c7
movq 0x558e(%rip), %rdx      # 0x7fd0
leaq 0x70(%rsp), %rdi
leaq 0xb0(%rsp), %rbp
movq %rbp, %rsi
callq 0x1050
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x25f1
movq %r12, %rdi
movq %r12, %rsi
callq 0x262e
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x262e
leaq 0x30(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x25f1
movq %r12, %rdi
movq %r12, %rsi
callq 0x25f1
movq %r12, %rdi
movq %r12, %rsi
callq 0x262e
movq 0x108(%rsp), %rdx
movq 0x100(%rsp), %r9
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2aca
movb $0x0, 0x20(%rsp,%rax)
incq %rax
jmp 0x2aba
leaq 0x20(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x98(%rsp), %rcx
movq %r14, %r8
callq 0x267a
leaq 0x40(%rsp), %rdi
leaq 0x70(%rsp), %r14
pushq $0x10
popq %rbp
movq %r14, %rsi
movq %r14, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x2765
leaq 0x80(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r14, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x2765
cmpq $0x1f, %r13
ja 0x2b7c
subq $0x20, %rsp
movl $0x2, 0x10(%rsp)
andl $0x0, 0x8(%rsp)
andl $0x0, (%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x20(%rsp), %r14
leaq 0xa0(%rsp), %rcx
leaq 0x40(%rsp), %r8
movq %r14, %rdx
xorl %r9d, %r9d
callq 0x27c6
addq $0x20, %rsp
leal -0x11(%r13), %ecx
pushq $0x8
popq %rdx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2932
leaq -0x18(%r13), %r14
jmp 0x2bcb
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %r14
leaq 0x88(%rsp), %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
movq %r14, %rdx
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x27c6
addq $0x20, %rsp
pushq $0x8
popq %rdx
pushq $0xf
popq %rcx
movq %r15, %rdi
movq %r14, %rsi
callq 0x2932
addq $0x8, %r15
pushq $0x8
popq %r14
leaq 0x40(%rsp), %rdi
movq %rsp, %rdx
movq %rdi, %rsi
callq 0x294b
xorl %eax, %eax
movq 0x18(%rsp), %r8
cmpq $0x8, %rax
je 0x2bf3
movb (%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x2be2
leaq -0x10(%r13), %rbp
cmpq $0x10, %rbp
jae 0x2c32
xorl %eax, %eax
cmpb (%rsp,%rbp), %al
jno 0x2c1d
addl $0x11, %r13d
andl $0x1f, %r13d
cmpl $0xf, %r13d
jg 0x2c32
cmpb $0x0, (%rsp,%r13)
leaq 0x1(%r13), %r13
je 0x2c0c
pushq $-0x1
popq %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x10(%rsp)
addq $0x10, %r8
pushq $0x1
popq %rax
movq $0x0, 0x68(%rsp)
movq %r8, %r13
movq %rbp, %r12
cmpq $0x11, %rbp
jb 0x2e33
movq %rax, 0x18(%rsp)
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
leaq 0x70(%rsp), %rdx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x2765
subq $0x8, %rsp
cmpq $0x1f, %r12
ja 0x2ce2
leaq 0x38(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rbx
movq %rbx, %rdx
movq %r14, %rcx
leaq 0x28(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x27c6
addq $0x20, %rsp
movq %rbx, %rdi
movq %rbx, %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdx
callq 0x294b
leal -0x11(%r12), %ecx
movq %r15, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x2932
movq %r14, %rdi
leaq -0x10(%r12), %rbp
movq %rbp, %r14
jmp 0x2d47
leaq 0x38(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x8(%rsp), %rbx
movq %rbx, %rdx
movq %r14, %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x27c6
addq $0x20, %rsp
cmpq $0x20, %r12
jne 0x2d25
movq %rbx, %rdi
movq %rbx, %rsi
leaq 0x40(%rsp), %rdx
callq 0x294b
xorl %eax, %eax
leaq 0x40(%rsp), %rdi
cmpq $0x10, %rax
je 0x2d3e
movb (%rsp,%rax), %cl
movb %cl, (%r15,%rax)
incq %rax
jmp 0x2d2c
leaq -0x10(%r12), %rbp
pushq $0x10
popq %r14
movq %rdi, %rsi
movq %rbx, %rdx
callq 0x294b
cmpq $0xf, %rbp
ja 0x2d81
xorl %eax, %eax
cmpb (%rsp,%rbp), %al
jno 0x2c1d
leal -0xf(%r12), %eax
leaq (%rsp,%rax), %rcx
cmpl $0xf, %eax
jg 0x2d85
incl %eax
cmpb $0x0, (%rcx)
leaq 0x1(%rcx), %rcx
je 0x2d6c
jmp 0x2c1d
addq $0x10, %r15
movq 0x18(%rsp), %rax
addq 0x10(%rsp), %r14
leaq 0x10(%r13), %r8
incq %rax
movq %r14, 0x10(%rsp)
cmpq $0x21, %rbp
jb 0x2c47
movq %r14, 0x10(%rsp)
cmpq $0x7f, %rax
jne 0x2c47
movq %r14, 0x10(%rsp)
cmpq $0x1f, 0x68(%rsp)
ja 0x2c47
leaq 0x20(%rsp), %rdi
movq %rsp, %rbp
movq %rbp, %rsi
movq 0x5209(%rip), %rdx      # 0x7fe0
movq %r8, %rbx
callq 0x1040
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
movq %rbp, %rdx
pushq $0x1
popq %rcx
callq 0x2962
movq %rbx, %rdi
xorl %esi, %esi
pushq $-0x1
popq %rax
cmpq $0x10, %rsi
je 0x2e14
movb (%rdi,%rsi), %cl
leaq 0x1(%rsi), %rdx
cmpb (%rsp,%rsi), %cl
movq %rdx, %rsi
je 0x2dfa
jmp 0x2c20
addq $0x20, %r13
addq $-0x20, %r12
incq 0x68(%rsp)
movq %r12, %rbp
movq %r13, %r8
movq %r14, 0x10(%rsp)
xorl %eax, %eax
jmp 0x2c47
subq $0x8, %rsp
leaq 0x58(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x48(%rsp), %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
pushq %r9
pushq %r9
pushq %r9
callq 0x27c6
addq $0x20, %rsp
xorl %esi, %esi
pushq $-0x1
popq %rax
cmpq %rsi, %r12
je 0x2e83
movb (%rsp,%rsi), %cl
leaq 0x1(%rsi), %rdx
cmpb (%r13,%rsi), %cl
movq %rdx, %rsi
je 0x2e68
jmp 0x2c20
movq 0x90(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
xorl %eax, %eax
jmp 0x2c20