// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/elmd101270v2/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,0x80,0x80,0,0,0}; 

	block L, W, Delta_0, Delta_1, Delta_2, blk, result, CS;
	int i,j, flag = 0; 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 % 16) != 1)||(clen < 33))
		return -1;
	
	key_schedule(k);
	
	/* =========== Generate the Masks ========== */
	AES(10, ENCRYPT, L, 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); 

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

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

	 /* =================== Process 1st Block =================== */

	if( (clen==33) && (c[clen-1]==0x01) ) {
	 	process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); 
		for(i=15; i>7; i--) { if(result[i]==0x80) { flag = 1; break; } }
		if(flag == 0) {return -1; }
		for(j=i+1; j<16; j++) { if(result[j]!=0) {return -1; }}
		store_bytes(m, result, 8, i-1); 
		outputmlen = i-8;
	 }
	 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 ++; 

	/* ============= Process Successive Ciphertext Blocks ============== */

	while(clen > 17){
	   
	   load_block(blk, c, zeroes, 16, 0);  
	   if((clen == 33) && (c[clen-1]==0x01)) { 
	   	process_block(Delta_2, Delta_1, result, blk, W, 0, DECRYPT, Is_final, CIPHERTEXT); 
		i = -1;
		xor_block(result, result, CS);
		for(i=15; i>0; i--) { if(result[i]==0x80) { flag = 1; break; } }
		if(flag == 0) {return -1; }
		for(j=i+1; j<16; j++) { if(result[j]!=0) {return -1; }}
	   	store_bytes(m, result, 0, i-1); outputmlen += i;
	   }
	   else{ 
	        process_block(Delta_2, Delta_1, result, blk, W, 1, DECRYPT, Is_final, CIPHERTEXT); 
		if(clen == 33) { 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 != 17)
		   m +=16; 
	
	   if(blk_ctr == IT_GAP && blk_ctr1 < IT_MAX && clen > 48){ 
		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_2, Delta_1, result, c, W, 1, DECRYPT, Is_final, CIPHERTEXT); 	
	for(i=0; i<16; i++) {if(CS[i]!=result[i]) return -1; } 
	*mlen = outputmlen;
	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r8, %rbp
movq %rcx, %r12
movq %rdx, %rbx
movq %rdi, %r14
movq 0x110(%rsp), %rdi
movabsq $0x80807f000a, %rax     # imm = 0x80807F000A
movq %rax, 0x98(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2a34
movb $0x0, 0x70(%rsp,%rax)
incq %rax
jmp 0x2a24
movl %ebp, %eax
andl $0xf, %eax
cmpl $0x1, %eax
setne %al
cmpq $0x21, %rbp
setb %cl
orb %al, %cl
pushq $-0x1
popq %rax
jne 0x2f05
movq %rsi, 0x90(%rsp)
movq %r12, 0x18(%rsp)
movq %r9, %r12
callq 0x25df
movq 0x5563(%rip), %rdx      # 0x7fd0
leaq 0x70(%rsp), %rdi
leaq 0xb0(%rsp), %r13
movq %r13, %rsi
callq 0x1050
leaq 0xa0(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2609
movq %r15, %rdi
movq %r15, %rsi
callq 0x2646
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x2646
leaq 0x30(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x2609
movq %r15, %rdi
movq %r15, %rsi
callq 0x2609
movq %r15, %rdi
movq %r15, %rsi
callq 0x2646
movq 0x108(%rsp), %rdx
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2aed
movb $0x0, 0x20(%rsp,%rax)
incq %rax
jmp 0x2add
leaq 0x20(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x98(%rsp), %rcx
movq %r12, %r8
movq 0x100(%rsp), %r9
callq 0x2692
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %r15
pushq $0x10
popq %r13
movl $0x0, 0x14(%rsp)
movq %r15, %rsi
movq %r15, %rdx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x277d
leaq 0x80(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %rdx
movl %r13d, %ecx
xorl %r8d, %r8d
callq 0x277d
cmpq $0x21, %rbp
jne 0x2bfb
movq 0x18(%rsp), %rax
cmpb $0x1, 0x20(%rax)
jne 0x2bfb
subq $0x20, %rsp
andl $0x0, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
andl $0x0, (%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
leaq 0x40(%rsp), %r8
xorl %r15d, %r15d
xorl %r9d, %r9d
callq 0x27de
addq $0x20, %rsp
pushq $0x7
popq %r13
leaq 0x8(%r13), %rax
cmpl $0x8, %eax
jb 0x2f02
movl %eax, %eax
cmpb (%rsp,%rax), %r15b
jo 0x2bda
decq %r13
jmp 0x2bb1
cmpb $0x0, 0x1(%rsp,%rax)
leaq 0x1(%rax), %rax
jne 0x2f02
cmpq $0xf, %rax
jne 0x2bcb
leal 0x7(%r13), %ecx
movq %rsp, %rsi
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x294a
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
jmp 0x2c4a
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %r15
leaq 0x88(%rsp), %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
movq %r15, %rdx
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x27de
addq $0x20, %rsp
pushq $0x8
popq %rdx
pushq $0xf
popq %rcx
movq %r14, %rdi
movq %r15, %rsi
callq 0x294a
addq $0x8, %r14
pushq $0x8
popq %r13
leaq 0x50(%rsp), %rdi
movq %rsp, %rdx
movq %rdi, %rsi
callq 0x2963
xorl %eax, %eax
movq 0x18(%rsp), %r8
cmpq $0x8, %rax
je 0x2c72
movb (%rsp,%rax), %cl
movb %cl, (%rbx,%rax)
incq %rax
jmp 0x2c61
addq $-0x10, %rbp
addq $0x10, %r8
pushq $0x1
popq %rcx
movq $0x0, 0x68(%rsp)
movq %r13, %rbx
movq %r8, %r15
cmpq $0x12, %rbp
jb 0x2eb4
movq %rbp, %r12
movq %rcx, 0x18(%rsp)
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
leaq 0x70(%rsp), %rdx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x277d
cmpq $0x21, %rbp
jne 0x2d2b
cmpb $0x1, 0x20(%r15)
movq %rsp, %rbp
jne 0x2d5e
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %rbp, %rdx
movq %r13, %rcx
leaq 0x28(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x27de
addq $0x20, %rsp
movq %rbp, %rdi
movq %rbp, %rsi
leaq 0x50(%rsp), %rdx
callq 0x2963
pushq $0xf
popq %r13
xorl %ecx, %ecx
testl %r13d, %r13d
je 0x2e10
movl %r13d, %eax
cmpb (%rsp,%rax), %cl
jo 0x2e1b
decq %r13
jmp 0x2d11
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rbp
movq %rbp, %rdx
movq %r13, %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x27de
addq $0x20, %rsp
jmp 0x2d9a
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
movq %rbp, %rdx
movq %r13, %rcx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x27de
addq $0x20, %rsp
movq %rbp, %rdi
movq %rbp, %rsi
leaq 0x50(%rsp), %rdx
callq 0x2963
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2dae
movb (%rsp,%rax), %cl
movb %cl, (%r14,%rax)
incq %rax
jmp 0x2d9c
pushq $0x10
popq %r13
addq %rbx, %r13
leaq 0x50(%rsp), %rdi
movq %rdi, %rsi
movq %rbp, %rdx
callq 0x2963
leaq -0x10(%r12), %rbp
xorl %eax, %eax
cmpq $0x11, %rbp
setne %al
leaq 0x10(%r15), %r8
movq 0x18(%rsp), %rcx
incq %rcx
shll $0x4, %eax
addq %rax, %r14
movq %r13, %rbx
cmpq $0x31, %rbp
jb 0x2c89
movq %r13, %rbx
cmpq $0x7f, %rcx
jne 0x2c89
movq %r13, %rbx
cmpq $0x1f, 0x68(%rsp)
ja 0x2c89
jmp 0x2e51
cmpl $0x0, 0x14(%rsp)
je 0x2f02
movq %r13, %rax
cmpq $0xf, %rax
je 0x2e34
cmpb $0x0, 0x1(%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x2e1e
jmp 0x2f02
leal -0x1(%r13), %ecx
movq %r14, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x294a
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
jmp 0x2db2
leaq 0x20(%rsp), %rdi
movq %rsp, %rbp
movq %rbp, %rsi
movq 0x517d(%rip), %rdx      # 0x7fe0
movq %r8, %rbx
callq 0x1040
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
movq %rbp, %rdx
pushq $0x1
popq %rcx
callq 0x297a
movq %rbx, %rsi
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2e9a
movb (%rsi,%rax), %cl
leaq 0x1(%rax), %rdx
cmpb (%rsp,%rax), %cl
movq %rdx, %rax
je 0x2e83
jmp 0x2f02
addq $0x20, %r15
addq $-0x20, %r12
incq 0x68(%rsp)
movq %r12, %rbp
movq %r15, %r8
xorl %ecx, %ecx
jmp 0x2c86
subq $0x8, %rsp
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x8(%rsp), %rdx
leaq 0x28(%rsp), %r8
pushq $0x1
popq %r9
movq %r15, %rcx
pushq $0x2
pushq $0x1
pushq $0x0
callq 0x27de
addq $0x20, %rsp
xorl %eax, %eax
movq 0x90(%rsp), %rsi
cmpq $0x10, %rax
je 0x2f17
movb 0x50(%rsp,%rax), %cl
leaq 0x1(%rax), %rdx
cmpb (%rsp,%rax), %cl
movq %rdx, %rax
je 0x2eec
pushq $-0x1
popq %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, (%rsi)
xorl %eax, %eax
jmp 0x2f05