// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aegis128/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 aegis128_state[5];

        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);
        }

        //decrypt the ciphertext
        *mlen = clen - 16; 
        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)  ) ;
        }     

        //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[i+*mlen]);  
        if (check == 0) return 0;  
        else return -1;
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x10, %r8
jb 0x18ee
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, %r14
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %rcx
movq 0xd0(%rsp), %rdx
vmovdqa (%rax), %xmm1
vmovdqa (%rcx), %xmm0
vmovdqa 0xa20(%rip), %xmm4      # 0x2000
vpxor %xmm4, %xmm1, %xmm6
vmovdqa 0xa24(%rip), %xmm5      # 0x2010
vpxor %xmm5, %xmm1, %xmm8
vpxor %xmm1, %xmm0, %xmm1
movl $0xa, %eax
vmovdqa %xmm1, %xmm7
vaesenc %xmm7, %xmm8, %xmm3
vaesenc %xmm8, %xmm6, %xmm8
vaesenc %xmm6, %xmm4, %xmm6
vaesenc %xmm4, %xmm5, %xmm4
vaesenc %xmm5, %xmm7, %xmm5
vpxor %xmm0, %xmm1, %xmm1
vpxor %xmm1, %xmm3, %xmm7
decl %eax
jne 0x15fd
cmpq $0x10, %rdx
jae 0x162c
xorl %ecx, %ecx
jmp 0x1665
xorl %eax, %eax
vmovdqa %xmm8, %xmm0
vaesenc %xmm0, %xmm6, %xmm8
vaesenc %xmm6, %xmm4, %xmm6
vaesenc %xmm4, %xmm5, %xmm4
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm0, %xmm0
vpxor (%r9,%rax), %xmm0, %xmm7
leaq 0x10(%rax), %rcx
addq $0x20, %rax
vmovdqa %xmm8, %xmm0
cmpq %rdx, %rax
movq %rcx, %rax
jbe 0x1632
andq $0xf, %rdx
je 0x16e1
vpxor %xmm0, %xmm0, %xmm0
leaq 0x70(%rsp), %r13
vmovdqa %xmm0, (%r13)
addq %rcx, %r9
movq %r13, %rdi
movq %r9, %rsi
vmovdqa %xmm4, 0x20(%rsp)
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm6, (%rsp)
vmovdqa %xmm7, 0x30(%rsp)
vmovdqa %xmm8, 0x40(%rsp)
callq 0x1070
vmovdqa (%rsp), %xmm6
vmovdqa 0x10(%rsp), %xmm5
vmovdqa 0x20(%rsp), %xmm4
vmovdqa 0x30(%rsp), %xmm2
vmovdqa 0x40(%rsp), %xmm1
vaesenc %xmm2, %xmm1, %xmm0
vaesenc %xmm1, %xmm6, %xmm8
vaesenc %xmm6, %xmm4, %xmm6
vaesenc %xmm4, %xmm5, %xmm4
vaesenc %xmm5, %xmm2, %xmm5
vpxor (%r13), %xmm0, %xmm7
addq $-0x10, %r14
movq %r14, (%r15)
cmpq $0x10, %r14
jae 0x16f2
xorl %ebp, %ebp
jmp 0x1745
xorl %eax, %eax
vmovdqa %xmm8, %xmm0
vmovdqa (%rbx,%rax), %xmm1
vpternlogq $0x78, %xmm6, %xmm4, %xmm1
vpternlogq $0x96, %xmm0, %xmm5, %xmm1
vmovdqa %xmm1, (%r12,%rax)
vaesenc %xmm0, %xmm6, %xmm8
vaesenc %xmm6, %xmm4, %xmm6
vaesenc %xmm4, %xmm5, %xmm4
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm0, %xmm0
vpxor %xmm1, %xmm0, %xmm7
movq (%r15), %r14
leaq 0x10(%rax), %rbp
addq $0x20, %rax
vmovdqa %xmm8, %xmm0
cmpq %r14, %rax
movq %rbp, %rax
jbe 0x16f8
movq %r14, %r13
andq $0xf, %r13
je 0x1876
vpxor %xmm0, %xmm0, %xmm0
leaq 0x80(%rsp), %r14
vmovdqa %xmm0, (%r14)
leaq (%rbx,%rbp), %rsi
movq %r14, %rdi
movq %r13, %rdx
vmovdqa %xmm4, 0x20(%rsp)
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm6, (%rsp)
vmovdqa %xmm7, 0x30(%rsp)
vmovdqa %xmm8, 0x40(%rsp)
callq 0x1070
vmovdqa (%r14), %xmm1
vmovdqa (%rsp), %xmm0
vpternlogq $0x78, 0x20(%rsp), %xmm0, %xmm1
vmovdqa 0x40(%rsp), %xmm2
vpternlogq $0x96, 0x10(%rsp), %xmm2, %xmm1
vmovdqa %xmm1, 0x50(%rsp)
leaq 0x70(%rsp), %rax
vmovdqa %xmm1, (%rax)
vmovdqa (%rsp), %xmm0
vaesenc %xmm2, %xmm0, %xmm0
vmovdqa %xmm0, 0x60(%rsp)
vmovdqa 0x20(%rsp), %xmm0
vmovdqa (%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, (%rsp)
vmovdqa 0x20(%rsp), %xmm0
vmovdqa 0x10(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
vmovdqa 0x10(%rsp), %xmm0
vmovdqa 0x30(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
vmovdqa 0x30(%rsp), %xmm0
vaesenc %xmm0, %xmm2, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
addq %rbp, %r12
movq %r12, %rdi
movq %rax, %r12
movq %rax, %rsi
movq %r13, %rdx
callq 0x1070
movq (%r15), %r14
movl %r14d, %edx
andl $0xf, %edx
movq %r12, %rdi
xorl %esi, %esi
callq 0x1030
vmovdqa 0x30(%rsp), %xmm7
vmovdqa (%rsp), %xmm6
vmovdqa 0x10(%rsp), %xmm5
vmovdqa 0x20(%rsp), %xmm4
vmovdqa 0x50(%rsp), %xmm0
vpternlogq $0x96, (%r12), %xmm0, %xmm7
vmovdqa 0x60(%rsp), %xmm8
vmovq %r14, %xmm0
vmovq 0xd0(%rsp), %xmm1
vpunpcklqdq %xmm0, %xmm1, %xmm0 # xmm0 = xmm1[0],xmm0[0]
vpsllq $0x3, %xmm0, %xmm0
movl $0x7, %eax
vmovdqa %xmm6, %xmm1
vaesenc %xmm8, %xmm1, %xmm2
vaesenc %xmm1, %xmm4, %xmm1
vaesenc %xmm4, %xmm5, %xmm4
vaesenc %xmm5, %xmm7, %xmm5
vaesenc %xmm7, %xmm8, %xmm7
vpternlogq $0x96, %xmm6, %xmm0, %xmm7
vmovdqa %xmm2, %xmm8
decl %eax
jne 0x1896
vpxor %xmm2, %xmm1, %xmm0
vpternlogq $0x96, %xmm4, %xmm0, %xmm5
vpternlogq $0x96, (%rbx,%r14), %xmm5, %xmm7
xorl %eax, %eax
vptest %xmm7, %xmm7
setne %al
negl %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop