// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x178, %rsp            # imm = 0x178
movq 0x190(%rsp), %rax
movq 0x188(%rsp), %rax
movq 0x180(%rsp), %rax
movq %rdi, 0x110(%rsp)
movq %rsi, 0x108(%rsp)
movq %rdx, 0x100(%rsp)
movq %rcx, 0xf8(%rsp)
movq %r8, 0xf0(%rsp)
movq %r9, 0xe8(%rsp)
movb $0x0, 0x8f(%rsp)
cmpq $0x10, 0xf0(%rsp)
jae 0x26e2
movl $0x1, 0x11c(%rsp)
jmp 0x2a44
movq 0x190(%rsp), %rdi
movq 0x188(%rsp), %rsi
movq %rsp, %rdx
callq 0x1040
movq $0x0, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
addq $0x20, %rax
cmpq 0x180(%rsp), %rax
ja 0x2752
movq 0xe8(%rsp), %rdi
addq 0xe0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq %rsp, %rdx
callq 0x1060
movq 0xe0(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xe0(%rsp)
jmp 0x2706
movq 0x180(%rsp), %rax
andq $0x1f, %rax
cmpq $0x0, %rax
je 0x27b9
leaq 0xc0(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1030
leaq 0xc0(%rsp), %rdi
movq 0xe8(%rsp), %rsi
addq 0xe0(%rsp), %rsi
movq 0x180(%rsp), %rdx
andq $0x1f, %rdx
callq 0x10b0
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq %rsp, %rdx
callq 0x1060
movq 0xf0(%rsp), %rcx
subq $0x10, %rcx
movq 0x108(%rsp), %rax
movq %rcx, (%rax)
movq $0x0, 0xe0(%rsp)
movq 0xe0(%rsp), %rax
addq $0x20, %rax
movq 0x108(%rsp), %rcx
cmpq (%rcx), %rax
ja 0x2833
movq 0x110(%rsp), %rdi
addq 0xe0(%rsp), %rdi
movq 0xf8(%rsp), %rsi
addq 0xe0(%rsp), %rsi
movq %rsp, %rdx
callq 0x10a0
movq 0xe0(%rsp), %rax
addq $0x20, %rax
movq %rax, 0xe0(%rsp)
jmp 0x27dc
movq 0x108(%rsp), %rax
movq (%rax), %rax
andq $0x1f, %rax
cmpq $0x0, %rax
je 0x2988
leaq 0xa0(%rsp), %rdi
xorl %esi, %esi
movl $0x20, %edx
callq 0x1030
leaq 0xa0(%rsp), %rdi
movq 0xf8(%rsp), %rsi
addq 0xe0(%rsp), %rsi
movq 0x108(%rsp), %rax
movq (%rax), %rdx
andq $0x1f, %rdx
callq 0x10b0
leaq 0xc0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq %rsp, %rdx
callq 0x10a0
movq 0x110(%rsp), %rdi
addq 0xe0(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
movq 0x108(%rsp), %rax
movq (%rax), %rdx
andq $0x1f, %rdx
callq 0x10b0
leaq 0xc0(%rsp), %rdi
movq 0x108(%rsp), %rax
movq (%rax), %rdx
andq $0x1f, %rdx
xorl %esi, %esi
callq 0x1030
vmovdqa (%rsp), %xmm1
leaq 0xc0(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm1, 0x160(%rsp)
vmovdqa %xmm0, 0x150(%rsp)
vmovdqa 0x160(%rsp), %xmm0
vpxor 0x150(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, (%rsp)
vmovdqa 0x40(%rsp), %xmm1
leaq 0xc0(%rsp), %rax
addq $0x10, %rax
movq %rax, 0x120(%rsp)
movq 0x120(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa %xmm1, 0x140(%rsp)
vmovdqa %xmm0, 0x130(%rsp)
vmovdqa 0x140(%rsp), %xmm0
vpxor 0x130(%rsp), %xmm0, %xmm0
vmovdqa %xmm0, 0x40(%rsp)
movq 0x108(%rsp), %rax
movq (%rax), %rdi
movq 0x180(%rsp), %rsi
leaq 0x90(%rsp), %rcx
movq %rsp, %r8
movl $0x10, %edx
callq 0x1070
movq $0x0, 0xe0(%rsp)
cmpq $0x10, 0xe0(%rsp)
jae 0x2a1f
movq 0xe0(%rsp), %rax
movzbl 0x90(%rsp,%rax), %ecx
movq 0xf8(%rsp), %rax
movq 0xe0(%rsp), %rdx
movq 0x108(%rsp), %rsi
addq (%rsi), %rdx
movzbl (%rax,%rdx), %eax
xorl %eax, %ecx
movzbl 0x8f(%rsp), %eax
orl %ecx, %eax
movb %al, 0x8f(%rsp)
movq 0xe0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xe0(%rsp)
jmp 0x29bc
movzbl 0x8f(%rsp), %eax
cmpl $0x0, %eax
jne 0x2a39
movl $0x0, 0x11c(%rsp)
jmp 0x2a44
movl $0x1, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
addq $0x178, %rsp            # imm = 0x178
retq
nopw %cs:(%rax,%rax)
nopl (%rax)