// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/aegis256/aesni/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
	)
{
        unsigned long long i;
        unsigned char plaintextblock[16], ciphertextblock[16];
        unsigned char tag[16];
        unsigned char check = 0;
        __m128i aegis256_state[6];

        if (clen < 16) return -1;         

        aegis256_initialization(k, npub, aegis256_state);

        //process the associated data  
        for (i = 0; (i+16) <= adlen; i += 16) {
              aegis256_enc_aut_step(ad+i, ciphertextblock, aegis256_state);
        }

        //deal with the partial block of associated data  
        //in this program, we assume that the message length is multiple of bytes.
        if (  (adlen & 0xf) != 0 )  {  
              memset(plaintextblock, 0, 16);
              memcpy(plaintextblock, ad+i, adlen & 0xf);  
              aegis256_enc_aut_step(plaintextblock, ciphertextblock, aegis256_state);
        }

        //decrypt the ciphertext
        *mlen = clen - 16; 
        for (i = 0; (i+16) <= *mlen; i += 16) {  
              aegis256_dec_aut_step(m+i, c+i, aegis256_state);  
        }

        // Deal with the partial block
        // In this program, we assume that the message length is multiple of bytes.
        if (  (*mlen & 0xf) != 0 )  {
              memset(ciphertextblock, 0, 16);
              memcpy(ciphertextblock, c+i, *mlen & 0xf);
              aegis256_dec_aut_step(plaintextblock, ciphertextblock, aegis256_state);
              memcpy(m+i, plaintextblock, *mlen & 0xf);

              //need to modify the state here (because in the last block, keystream is wrongly used to update the state)
              memset(plaintextblock, 0, *mlen & 0xf);
              aegis256_state[0] = _mm_xor_si128( aegis256_state[0], _mm_load_si128((__m128i*)plaintextblock)  ) ;
        }

        //we assume that the tag length is multiple of bytes
        aegis256_tag_generation(*mlen, adlen, 16, tag, aegis256_state);

        //verification
        for (i = 0; i  < 16; i++) check |= (tag[i] ^ c[i+*mlen]);  
        if (check == 0) return 0;
        else return -1;
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq 0x130(%rsp), %rax
movq 0x128(%rsp), %rax
movq 0x120(%rsp), %rax
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq %rdx, 0xc8(%rsp)
movq %rcx, 0xc0(%rsp)
movq %r8, 0xb8(%rsp)
movq %r9, 0xb0(%rsp)
movb $0x0, 0x6f(%rsp)
cmpq $0x10, 0xb8(%rsp)
jae 0x23ef
movl $0xffffffff, 0xe4(%rsp) # imm = 0xFFFFFFFF
jmp 0x26f2
movq 0x130(%rsp), %rdi
movq 0x128(%rsp), %rsi
movq %rsp, %rdx
callq 0x1090
movq $0x0, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
addq $0x10, %rax
cmpq 0x120(%rsp), %rax
ja 0x245f
movq 0xb0(%rsp), %rdi
addq 0xa8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq %rsp, %rdx
callq 0x1050
movq 0xa8(%rsp), %rax
addq $0x10, %rax
movq %rax, 0xa8(%rsp)
jmp 0x2413
movq 0x120(%rsp), %rax
andq $0xf, %rax
cmpq $0x0, %rax
je 0x24c6
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1030
leaq 0x90(%rsp), %rdi
movq 0xb0(%rsp), %rsi
addq 0xa8(%rsp), %rsi
movq 0x120(%rsp), %rdx
andq $0xf, %rdx
callq 0x10a0
leaq 0x90(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq %rsp, %rdx
callq 0x1050
movq 0xb8(%rsp), %rcx
subq $0x10, %rcx
movq 0xd0(%rsp), %rax
movq %rcx, (%rax)
movq $0x0, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
addq $0x10, %rax
movq 0xd0(%rsp), %rcx
cmpq (%rcx), %rax
ja 0x2540
movq 0xd8(%rsp), %rdi
addq 0xa8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
addq 0xa8(%rsp), %rsi
movq %rsp, %rdx
callq 0x1040
movq 0xa8(%rsp), %rax
addq $0x10, %rax
movq %rax, 0xa8(%rsp)
jmp 0x24e9
movq 0xd0(%rsp), %rax
movq (%rax), %rax
andq $0xf, %rax
cmpq $0x0, %rax
je 0x2645
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1030
leaq 0x80(%rsp), %rdi
movq 0xc0(%rsp), %rsi
addq 0xa8(%rsp), %rsi
movq 0xd0(%rsp), %rax
movq (%rax), %rdx
andq $0xf, %rdx
callq 0x10a0
leaq 0x90(%rsp), %rdi
leaq 0x80(%rsp), %rsi
movq %rsp, %rdx
callq 0x1040
movq 0xd8(%rsp), %rdi
addq 0xa8(%rsp), %rdi
leaq 0x90(%rsp), %rsi
movq 0xd0(%rsp), %rax
movq (%rax), %rdx
andq $0xf, %rdx
callq 0x10a0
leaq 0x90(%rsp), %rdi
movq 0xd0(%rsp), %rax
movq (%rax), %rdx
andq $0xf, %rdx
xorl %esi, %esi
callq 0x1030
vmovdqa (%rsp), %xmm1
leaq 0x90(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm1, 0x100(%rsp)
vmovdqa %xmm0, 0xf0(%rsp)
vmovdqa 0x100(%rsp), %xmm0
vpxor 0xf0(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
movq 0xd0(%rsp), %rax
movq (%rax), %rdi
movq 0x120(%rsp), %rsi
leaq 0x70(%rsp), %rcx
movq %rsp, %r8
movl $0x10, %edx
callq 0x10c0
movq $0x0, 0xa8(%rsp)
cmpq $0x10, 0xa8(%rsp)
jae 0x26d0
movq 0xa8(%rsp), %rax
movzbl 0x70(%rsp,%rax), %ecx
movq 0xc0(%rsp), %rax
movq 0xa8(%rsp), %rdx
movq 0xd0(%rsp), %rsi
addq (%rsi), %rdx
movzbl (%rax,%rdx), %eax
xorl %eax, %ecx
movzbl 0x6f(%rsp), %eax
orl %ecx, %eax
movb %al, 0x6f(%rsp)
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x2676
movzbl 0x6f(%rsp), %eax
cmpl $0x0, %eax
jne 0x26e7
movl $0x0, 0xe4(%rsp)
jmp 0x26f2
movl $0xffffffff, 0xe4(%rsp) # imm = 0xFFFFFFFF
movl 0xe4(%rsp), %eax
addq $0x118, %rsp            # imm = 0x118
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)