// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aegis128/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
	)
{
        unsigned long i;
        unsigned char plaintextblock[16], ciphertextblock[16];
        unsigned char tag[16];
        unsigned char check = 0;
        unsigned char aegis128_state[80];

        if (clen < 16) return -1;

        aegis128_initialization(k, npub, aegis128_state);

        //process the associated data
        for (i = 0; (i+16) <= adlen; i += 16) {
              aegis128_enc_aut_step(ad+i, ciphertextblock, aegis128_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);
              aegis128_enc_aut_step(plaintextblock, ciphertextblock, aegis128_state);
        }


        *mlen = clen - 16;

        //decrypt the ciphertext
        for (i = 0; (i+16) <= *mlen; i += 16) {
              aegis128_dec_aut_step(m+i, c+i, aegis128_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);
              aegis128_dec_aut_step(plaintextblock, ciphertextblock, aegis128_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);
              //aegis128_state[0] = _mm_xor_si128( aegis128_state[0], _mm_load_si128((__m128i*)plaintextblock)  ) ;
              ((unsigned long long*)aegis128_state)[0] ^= ((unsigned long long*)plaintextblock)[0];
              ((unsigned long long*)aegis128_state)[1] ^= ((unsigned long long*)plaintextblock)[1];
        }

        //we assume that the tag length is multiple of bytes
        aegis128_tag_generation(*mlen, adlen, 16, tag, aegis128_state);

        //verification
        for (i = 0; i  < 16; i++) check |= (tag[i] ^ c[clen - 16 + i]);
        if (check == 0) return 0;
        else return -1;
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x10, %r8
jb 0x1e17
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r8, %r13
movq %rsi, %rbp
movq %r9, 0x70(%rsp)
movq %rdi, 0x88(%rsp)
movq %rcx, 0x80(%rsp)
movq 0x140(%rsp), %rdi
movq 0x138(%rsp), %rsi
movq 0x130(%rsp), %r12
leaq 0x20(%rsp), %rdx
callq 0x1060
cmpq $0x10, %r12
movq %rbp, 0x18(%rsp)
jae 0x1932
xorl %eax, %eax
jmp 0x1aa9
movq %r13, 0x10(%rsp)
xorl %eax, %eax
movq 0x70(%rsp), %r15
movq %rax, %r12
movq 0x40(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0x58(%rsp), %r13
movq 0x30(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0x68(%rsp), %rbx
movq (%r15,%r12), %rax
movq %rax, 0xd8(%rsp)
movq 0x8(%r15,%r12), %rax
movq %rax, 0xa8(%rsp)
leaq 0x60(%rsp), %rdi
vmovaps (%rdi), %xmm0
vmovaps %xmm0, (%rsp)
leaq 0x50(%rsp), %r14
movq %r14, %rsi
movq %rdi, %rdx
callq 0x1040
movq %r14, %rdi
leaq 0x40(%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1040
movq %rbp, %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1040
movq %r14, %rdi
leaq 0x20(%rsp), %rbp
movq %rbp, %rsi
movq %r14, %rdx
callq 0x1040
movq %rbp, %rdi
movq %rsp, %rsi
movq %rbp, %rdx
callq 0x1040
vmovdqu (%r15,%r12), %xmm0
vpxor 0x20(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
leaq 0x10(%r12), %rax
addq $0x20, %r12
cmpq 0x130(%rsp), %r12
movq 0x130(%rsp), %r12
jbe 0x193e
andq 0xb0(%rsp), %r13
movq %rbx, %rcx
xorq 0xa0(%rsp), %rcx
xorq %r13, %rcx
movq 0xa8(%rsp), %rbx
xorq %rcx, %rbx
movq %rbx, 0x98(%rsp)
movq 0xd0(%rsp), %rcx
andq 0xc8(%rsp), %rcx
movq 0xc0(%rsp), %rdx
xorq 0xb8(%rsp), %rdx
xorq %rcx, %rdx
movq 0xd8(%rsp), %rcx
xorq %rdx, %rcx
movq 0x10(%rsp), %r13
movq 0x18(%rsp), %rbp
movq %rcx, 0x90(%rsp)
movq %r12, %rbx
andq $0xf, %rbx
je 0x1b9a
movq 0x70(%rsp), %r15
addq %rax, %r15
movl $0x10, %edx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0xe0, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0xe0(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x10a0
leaq 0x40(%rsp), %rbx
leaq 0x50(%rsp), %r15
leaq 0x60(%rsp), %rdi
vmovdqa -0x30(%rdi), %xmm0
vmovdqa -0x10(%rdi), %xmm1
vmovaps (%rdi), %xmm2
vpternlogq $0x6c, -0x20(%rdi), %xmm0, %xmm1
vmovdqa (%r14), %xmm0
vmovdqa %xmm0, 0x70(%rsp)
vpternlogq $0x96, (%rdi), %xmm0, %xmm1
leaq 0x30(%rsp), %r14
vmovdqa %xmm1, 0x90(%rsp)
movq %rsp, %r12
vmovaps %xmm2, (%r12)
movq %r15, %rsi
movq %rdi, %rdx
callq 0x1040
movq %r15, %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1040
movq %rbx, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1040
leaq 0x20(%rsp), %rbx
movq %r14, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x1040
movq %rbx, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x1040
vmovdqa 0x70(%rsp), %xmm0
vpxor (%rbx), %xmm0, %xmm0
vmovdqa %xmm0, (%rbx)
addq $-0x10, %r13
movq %r13, (%rbp)
cmpq $0x10, %r13
movq %r13, 0x10(%rsp)
jae 0x1bbc
xorl %ebx, %ebx
movq 0x80(%rsp), %rcx
jmp 0x1ca5
xorl %ebp, %ebp
leaq 0x20(%rsp), %r14
movq 0x80(%rsp), %rcx
movq 0x88(%rsp), %r15
vmovdqa 0x30(%rsp), %xmm0
vmovdqa 0x50(%rsp), %xmm1
vpand 0x40(%rsp), %xmm1, %xmm1
vpternlogq $0x96, 0x60(%rsp), %xmm0, %xmm1
vmovdqu %xmm1, (%r15,%rbp)
vmovq %xmm1, %rax
xorq (%rcx,%rbp), %rax
movq %rax, (%r15,%rbp)
vpextrq $0x1, %xmm1, %rax
xorq 0x8(%rcx,%rbp), %rax
movq %rax, 0x8(%r15,%rbp)
leaq 0x60(%rsp), %rdi
vmovaps (%rdi), %xmm0
vmovaps %xmm0, (%rsp)
leaq 0x50(%rsp), %r12
movq %r12, %rsi
movq %rdi, %rdx
movq %rcx, %rbx
callq 0x1040
movq %r12, %rdi
leaq 0x40(%rsp), %r13
movq %r13, %rsi
movq %r12, %rdx
callq 0x1040
movq %r13, %rdi
leaq 0x30(%rsp), %r12
movq %r12, %rsi
movq %r13, %rdx
callq 0x1040
movq %r12, %rdi
movq %r14, %rsi
movq %r12, %rdx
callq 0x1040
movq %r14, %rdi
movq %rsp, %rsi
movq %r14, %rdx
callq 0x1040
movq %rbx, %rcx
vmovdqu (%r15,%rbp), %xmm0
vpxor 0x20(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %r13
leaq 0x10(%rbp), %rbx
addq $0x20, %rbp
cmpq %r13, %rbp
movq %rbx, %rbp
jbe 0x1bd3
movq %r13, %r15
andq $0xf, %r15
movq %rcx, %rbp
je 0x1dcb
leaq (%rbx,%rbp), %r14
movl $0x10, %edx
subq %r15, %rdx
leaq (%rsp,%r15), %rdi
addq $0x90, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x90(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x10a0
leaq 0x40(%rsp), %r14
leaq 0x50(%rsp), %r13
leaq 0x30(%rsp), %r15
leaq 0x60(%rsp), %rdi
vmovdqa -0x30(%rdi), %xmm0
vmovdqa -0x10(%rdi), %xmm1
vmovaps (%rdi), %xmm2
vpternlogq $0x6c, -0x20(%rdi), %xmm0, %xmm1
vmovdqa (%rdi), %xmm0
vpternlogq $0x96, (%r12), %xmm1, %xmm0
vmovdqa %xmm0, 0x70(%rsp)
leaq 0xe0(%rsp), %r12
vmovdqa %xmm0, (%r12)
movq %rsp, %rax
vmovaps %xmm2, (%rax)
movq %r13, %rsi
movq %rdi, %rdx
callq 0x1040
movq %r13, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x1040
movq %r14, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1040
leaq 0x20(%rsp), %r14
movq %r15, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x1040
movq %r14, %rdi
movq %rsp, %rsi
movq %r14, %rdx
callq 0x1040
movq 0x88(%rsp), %rdi
addq %rbx, %rdi
movq 0x18(%rsp), %rbx
movq (%rbx), %rdx
andl $0xf, %edx
movq %r12, %rsi
callq 0x10a0
movq (%rbx), %r13
movl %r13d, %edx
andl $0xf, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1030
vmovdqa (%r14), %xmm0
vmovdqa 0x70(%rsp), %xmm1
vpternlogq $0x96, (%r12), %xmm1, %xmm0
vmovdqa %xmm0, (%r14)
movq %rsp, %rbx
leaq 0x20(%rsp), %r8
movq %r13, %rdi
movq 0x130(%rsp), %rsi
movl $0x10, %edx
movq %rbx, %rcx
callq 0x1090
movq 0x10(%rsp), %rax
vmovdqu (%rbp,%rax), %xmm0
vpxor (%rbx), %xmm0, %xmm0
xorl %eax, %eax
vptest %xmm0, %xmm0
setne %al
negl %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq