// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/aegis128l/ref/ecrypt.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;
        unsigned char aegis128L_state[128];

        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);
        }


        *mlen = clen - 16;

        //decrypt the ciphertext
        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);
              XOR128(aegis128L_state, aegis128L_state, plaintextblock);
              XOR128(aegis128L_state+64, aegis128L_state+64, 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[clen - 16 + i]);
        if (check == 0) return 0;
        else return -1;
}

// Assembly:
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpq $0x10, %r8
jb 0x1988
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, %rbp
movq %rdi, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
movq %r8, (%rsp)
movq 0x140(%rsp), %rdi
movq 0x138(%rsp), %rsi
movq 0x130(%rsp), %r12
leaq 0x70(%rsp), %rdx
callq 0x1040
cmpq $0x20, %r12
jae 0x17c9
xorl %eax, %eax
jmp 0x17f5
xorl %r14d, %r14d
leaq 0x20(%rsp), %rbx
leaq 0x70(%rsp), %r15
leaq (%r14,%rbp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1070
leaq 0x20(%r14), %rax
addq $0x40, %r14
cmpq %r12, %r14
movq %rax, %r14
jbe 0x17d6
movq %r12, %rbx
andq $0x1f, %rbx
je 0x183d
addq %rax, %rbp
movl $0x20, %edx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0x40, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x10c0
leaq 0x20(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movq %r15, %rdi
callq 0x1070
movq (%rsp), %rbp
addq $-0x10, %rbp
movq 0x18(%rsp), %rcx
movq %rbp, (%rcx)
cmpq $0x20, %rbp
movq %rbp, (%rsp)
jae 0x1861
xorl %r14d, %r14d
movq 0x8(%rsp), %r12
jmp 0x189b
xorl %r15d, %r15d
leaq 0x70(%rsp), %rbx
movq 0x8(%rsp), %r12
movq 0x10(%rsp), %r13
leaq (%r15,%r13), %rdi
leaq (%r12,%r15), %rsi
movq %rbx, %rdx
callq 0x10b0
movq 0x18(%rsp), %rcx
movq (%rcx), %rbp
leaq 0x20(%r15), %r14
addq $0x40, %r15
cmpq %rbp, %r15
movq %r14, %r15
jbe 0x1873
movq %rbp, %rbx
andq $0x1f, %rbx
movq %r12, %r15
je 0x193b
leaq (%r15,%r14), %rbp
movl $0x20, %edx
subq %rbx, %rdx
leaq (%rsp,%rbx), %rdi
addq $0x20, %rdi
movq %rcx, %r12
xorl %esi, %esi
callq 0x1030
leaq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %rbx, %rdx
callq 0x10c0
leaq 0x40(%rsp), %r13
leaq 0x70(%rsp), %rbx
movq %r13, %rdi
leaq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0x10b0
movq 0x10(%rsp), %rdi
addq %r14, %rdi
movq (%r12), %rdx
andl $0x1f, %edx
movq %r13, %rsi
callq 0x10c0
movq (%r12), %rbp
movl %ebp, %edx
andl $0x1f, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x1030
vmovaps (%r13), %xmm0
vmovaps 0x10(%r13), %xmm1
vxorps (%rbx), %xmm0, %xmm0
vmovaps %xmm0, (%rbx)
vxorps 0x40(%rbx), %xmm1, %xmm0
vmovaps %xmm0, 0x40(%rbx)
leaq 0x60(%rsp), %rbx
leaq 0x70(%rsp), %r8
movq %rbp, %rdi
movq 0x130(%rsp), %rsi
movl $0x10, %edx
movq %rbx, %rcx
callq 0x1080
movq (%rsp), %rax
vmovdqu (%r15,%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
nopl (%rax)