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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %rsi, (%rsp)
cmpq $0x10, %r8
jae 0x14c3
pushq $0x1
popq %rax
jmp 0x1676
movq %r9, %rbp
movq %r8, %r13
movq %rdi, %r12
movq %rcx, 0x8(%rsp)
movq 0x130(%rsp), %rdi
movq 0x128(%rsp), %rsi
leaq 0x60(%rsp), %r14
movq %r14, %rdx
callq 0x1040
pushq $0x20
popq %r15
leaq 0x20(%rsp), %rbx
cmpq 0x120(%rsp), %r15
ja 0x1519
movq %rbp, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1060
addq $0x20, %rbp
addq $0x20, %r15
jmp 0x14f7
movq 0x120(%rsp), %rbx
andq $0x1f, %rbx
je 0x1561
pushq $0x20
popq %rdx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0x40, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x40(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x10b0
leaq 0x20(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movq %r14, %rdi
callq 0x1060
addq $-0x10, %r13
movq (%rsp), %rax
movq %r13, (%rax)
pushq $0x20
popq %rbx
leaq 0x60(%rsp), %r14
movq 0x8(%rsp), %r15
movq %r15, %rbp
cmpq %r13, %rbx
ja 0x15a4
movq %r12, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10a0
movq (%rsp), %rax
movq (%rax), %r13
addq $0x20, %r12
addq $0x20, %rbp
addq $0x20, %rbx
jmp 0x157c
movq %r13, %rbx
andq $0x1f, %rbx
je 0x1636
pushq $0x20
popq %rdx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0x20, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x10b0
leaq 0x40(%rsp), %rbx
leaq 0x60(%rsp), %rbp
movq %rbx, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x10a0
movq (%rsp), %r14
movq (%r14), %rdx
andl $0x1f, %edx
movq %r12, %rdi
movq %rbx, %rsi
callq 0x10b0
movq (%r14), %r13
movl %r13d, %edx
andl $0x1f, %edx
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1030
vmovaps (%rbx), %xmm0
vmovaps 0x10(%rbx), %xmm1
vxorps (%rbp), %xmm0, %xmm0
vmovaps %xmm0, (%rbp)
vxorps 0x40(%rbp), %xmm1, %xmm0
vmovaps %xmm0, 0x40(%rbp)
pushq $0x10
popq %rdx
leaq 0x10(%rsp), %rcx
leaq 0x60(%rsp), %r8
movq %r13, %rdi
movq 0x120(%rsp), %rsi
callq 0x1070
addq %r13, %r15
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x10, %rax
je 0x166f
movb (%r15,%rax), %dl
xorb 0x10(%rsp,%rax), %dl
orb %dl, %cl
incq %rax
jmp 0x165a
xorl %eax, %eax
testb %cl, %cl
setne %al
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq