// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/elmd600v2/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[]={0x06,0,0,0,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;	
	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(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); 

	
	 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;

	/* ============= 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); 
		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;

	   if(clen != 17)
		   m +=16; 
	}
	

	/* ==========  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 $0xb8, %rsp
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r13
movq %rdi, %r15
movq 0x100(%rsp), %rdi
movabsq $0x8000000006, %rax     # imm = 0x8000000006
movq %rax, 0x88(%rsp)
xorl %eax, %eax
cmpq $0x10, %rax
je 0x295a
movb $0x0, 0x70(%rsp,%rax)
incq %rax
jmp 0x294a
movl %r14d, %eax
andl $0xf, %eax
cmpl $0x1, %eax
setne %al
cmpq $0x21, %r14
setb %cl
orb %al, %cl
pushq $-0x1
popq %rax
jne 0x2d86
movq %rsi, 0x80(%rsp)
movq %r9, (%rsp)
callq 0x2576
movq 0x4640(%rip), %rdx      # 0x6fd0
leaq 0x70(%rsp), %rdi
leaq 0x20(%rsp), %r12
movq %r12, %rsi
callq 0x1050
leaq 0xa0(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
movq 0x4619(%rip), %rdx      # 0x6fd0
callq 0x1050
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x25a0
movq %r12, %rdi
movq %r12, %rsi
callq 0x25dd
leaq 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x25dd
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x25a0
movq %r12, %rdi
movq %r12, %rsi
callq 0x25a0
movq %r12, %rdi
movq %r12, %rsi
callq 0x25dd
movq 0xf8(%rsp), %rdx
movq (%rsp), %r8
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2a2b
movb $0x0, 0x30(%rsp,%rax)
incq %rax
jmp 0x2a1b
leaq 0x30(%rsp), %rdi
leaq 0x90(%rsp), %rsi
leaq 0x88(%rsp), %rcx
movq 0xf0(%rsp), %r9
callq 0x2629
leaq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %r12
pushq $0x10
popq %rbp
movl $0x0, 0xc(%rsp)
movq %r12, %rsi
movq %r12, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x2714
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
movq %r12, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x2714
cmpq $0x21, %r14
jne 0x2b2b
cmpb $0x1, 0x20(%rbx)
jne 0x2b2b
subq $0x20, %rsp
andl $0x0, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
andl $0x0, (%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0x40(%rsp), %rcx
leaq 0x50(%rsp), %r8
xorl %r12d, %r12d
xorl %r9d, %r9d
callq 0x2775
addq $0x20, %rsp
pushq $0x7
popq %rcx
leaq 0x8(%rcx), %rax
cmpl $0x8, %eax
jb 0x2d83
movl %eax, %eax
cmpb 0x10(%rsp,%rax), %r12b
jo 0x2b05
decq %rcx
jmp 0x2adb
cmpb $0x0, 0x11(%rsp,%rax)
leaq 0x1(%rax), %rax
jne 0x2d83
cmpq $0xf, %rax
jne 0x2af6
movq %rcx, (%rsp)
addl $0x7, %ecx
leaq 0x10(%rsp), %rsi
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x28e1
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
jmp 0x2b7a
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x18(%rsp), %r12
leaq 0x28(%rsp), %rcx
leaq 0x38(%rsp), %r8
pushq $0x1
popq %r9
movq %r12, %rdx
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x2775
addq $0x20, %rsp
pushq $0x8
popq %rdx
pushq $0xf
popq %rcx
movq %r15, %rdi
movq %r12, %rsi
callq 0x28e1
addq $0x8, %r15
pushq $0x8
popq %rax
movq %rax, (%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movq %rdi, %rsi
callq 0x28fa
xorl %eax, %eax
cmpq $0x8, %rax
je 0x2ba2
movb 0x10(%rsp,%rax), %cl
movb %cl, (%r13,%rax)
incq %rax
jmp 0x2b8e
addq $-0x10, %r14
leaq 0x20(%rsp), %rdi
leaq 0x10(%rsp), %rbp
leaq 0x10(%rbx), %r12
cmpq $0x12, %r14
jb 0x2d3c
movq %rbx, %r13
movq %r12, %rsi
leaq 0x70(%rsp), %rdx
movq %rdi, %rbx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x2714
cmpq $0x21, %r14
jne 0x2c43
cmpb $0x1, 0x30(%r13)
jne 0x2c71
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rbp, %rdx
leaq 0x28(%rsp), %rcx
leaq 0x38(%rsp), %r8
xorl %r9d, %r9d
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x2775
addq $0x20, %rsp
movq %rbp, %rdi
movq %rbp, %rsi
leaq 0x60(%rsp), %rdx
callq 0x28fa
pushq $0xf
popq %rbx
xorl %ecx, %ecx
testl %ebx, %ebx
je 0x2d02
movl %ebx, %eax
cmpb 0x10(%rsp,%rax), %cl
jo 0x2d09
decq %rbx
jmp 0x2c2a
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rbp, %rdx
movq %rbx, %rcx
leaq 0x38(%rsp), %r8
pushq $0x1
popq %r9
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x2775
addq $0x20, %rsp
jmp 0x2caf
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
movq %rbp, %rdx
leaq 0x28(%rsp), %rcx
leaq 0x38(%rsp), %r8
pushq $0x1
popq %r9
pushq $0x2
pushq $0x0
pushq $0x0
callq 0x2775
addq $0x20, %rsp
movq %rbp, %rdi
movq %rbp, %rsi
leaq 0x60(%rsp), %rdx
callq 0x28fa
xorl %eax, %eax
movq (%rsp), %rdx
cmpq $0x10, %rax
je 0x2cc8
movb 0x10(%rsp,%rax), %cl
movb %cl, (%r15,%rax)
incq %rax
jmp 0x2cb5
pushq $0x10
popq %rbx
addq %rbx, %rdx
movq %rdx, (%rsp)
leaq 0x60(%rsp), %rdi
movq %rdi, %rsi
movq %rbp, %rdx
callq 0x28fa
addq $-0x10, %r14
xorl %eax, %eax
cmpq $0x11, %r14
setne %al
shll $0x4, %eax
addq %rax, %r15
movq %r12, %rbx
leaq 0x20(%rsp), %rdi
jmp 0x2bb0
cmpl $0x0, 0xc(%rsp)
je 0x2d83
movq %rbx, %rax
cmpq $0xf, %rax
je 0x2d1f
cmpb $0x0, 0x11(%rsp,%rax)
leaq 0x1(%rax), %rax
je 0x2d0c
jmp 0x2d83
leal -0x1(%rbx), %ecx
movq %r15, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x28e1
pushq $0x1
popq %rax
movl %eax, 0xc(%rsp)
movq (%rsp), %rdx
jmp 0x2ccb
subq $0x8, %rsp
leaq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rsi
leaq 0x18(%rsp), %rdx
leaq 0x38(%rsp), %r8
pushq $0x1
popq %r9
movq %r12, %rcx
pushq $0x2
pushq $0x1
pushq $0x0
callq 0x2775
addq $0x20, %rsp
xorl %eax, %eax
cmpq $0x10, %rax
je 0x2d98
movb 0x60(%rsp,%rax), %cl
leaq 0x1(%rax), %rdx
cmpb 0x10(%rsp,%rax), %cl
movq %rdx, %rax
je 0x2d6c
pushq $-0x1
popq %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x80(%rsp), %rax
movq (%rsp), %rcx
movq %rcx, (%rax)
xorl %eax, %eax
jmp 0x2d86