// File: /workspace/github_c_makefile_star2_O2/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:
cmpq $0x10, %r8
jae 0x15dd
pushq $-0x1
popq %rax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, %rbp
movq %rsi, %r12
movq %rdi, %r13
movq %r8, (%rsp)
movq %rcx, 0x8(%rsp)
movq 0xe0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
leaq 0x40(%rsp), %r14
movq %r14, %rdx
callq 0x1070
pushq $0x10
popq %rbx
leaq 0x10(%rsp), %r15
cmpq 0xd0(%rsp), %rbx
ja 0x1647
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1040
addq $0x10, %rbp
addq $0x10, %rbx
jmp 0x1625
movq 0xd0(%rsp), %r14
andq $0xf, %r14
je 0x168f
pushq $0x10
popq %rdx
subq %r14, %rdx
leaq (%rsp,%r14), %rdi
addq $0x20, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x20(%rsp), %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10c0
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %rdx
movq %r15, %rdi
callq 0x1040
movq (%rsp), %r14
addq $-0x10, %r14
movq %r14, (%r12)
pushq $0x10
popq %rbx
leaq 0x40(%rsp), %r15
movq 0x8(%rsp), %rbp
movq %r14, (%rsp)
cmpq %r14, %rbx
ja 0x16d1
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1080
movq (%r12), %r14
addq $0x10, %r13
addq $0x10, %rbp
addq $0x10, %rbx
jmp 0x16ac
movq %r14, %r15
andq $0xf, %r15
je 0x174f
pushq $0x10
popq %rdx
subq %r15, %rdx
leaq (%rsp,%r15), %rdi
addq $0x10, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x10c0
leaq 0x20(%rsp), %r15
leaq 0x40(%rsp), %rbp
movq %r15, %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x1080
movq (%r12), %rdx
andl $0xf, %edx
movq %r13, %rdi
movq %r15, %rsi
callq 0x10c0
movq (%r12), %r14
movl %r14d, %edx
andl $0xf, %edx
movq %r15, %rdi
xorl %esi, %esi
callq 0x1030
vmovaps (%rbp), %xmm0
vxorps (%r15), %xmm0, %xmm0
vmovaps %xmm0, (%rbp)
pushq $0x10
popq %rdx
leaq 0x30(%rsp), %rcx
leaq 0x40(%rsp), %r8
movq %r14, %rdi
movq 0xd0(%rsp), %rsi
callq 0x10b0
movq 0x8(%rsp), %rsi
addq (%rsp), %rsi
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x10, %rax
je 0x178d
movb (%rsi,%rax), %dl
xorb 0x30(%rsp,%rax), %dl
orb %dl, %cl
incq %rax
jmp 0x1779
xorl %eax, %eax
negb %cl
sbbl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)