// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/aegis256/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 aegis256_state[6];

        if (clen < 16) return -1;         

        aegis256_initialization(k, npub, aegis256_state);

        //process the associated data  
        for (i = 0; (i+16) <= adlen; i += 16) {
              aegis256_enc_aut_step(ad+i, ciphertextblock, aegis256_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);  
              aegis256_enc_aut_step(plaintextblock, ciphertextblock, aegis256_state);
        }

        //decrypt the ciphertext
        *mlen = clen - 16; 
        for (i = 0; (i+16) <= *mlen; i += 16) {  
              aegis256_dec_aut_step(m+i, c+i, aegis256_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);
              aegis256_dec_aut_step(plaintextblock, ciphertextblock, aegis256_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);
              aegis256_state[0] = _mm_xor_si128( aegis256_state[0], _mm_load_si128((__m128i*)plaintextblock)  ) ;
        }

        //we assume that the tag length is multiple of bytes
        aegis256_tag_generation(*mlen, adlen, 16, tag, aegis256_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 0x1b26
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r8, %r12
movq %rcx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0xf0(%rsp), %rax
movq 0xe8(%rsp), %rcx
vmovdqa (%rax), %xmm2
vmovdqa 0x10(%rax), %xmm3
vmovdqa (%rcx), %xmm0
movq 0xe0(%rsp), %rdx
vmovdqa 0x10(%rcx), %xmm1
vmovdqa 0x8cb(%rip), %xmm11     # 0x2010
vpxor %xmm2, %xmm11, %xmm10
vmovdqa 0x8af(%rip), %xmm7      # 0x2000
vpxor %xmm7, %xmm3, %xmm12
vpxor %xmm2, %xmm0, %xmm2
vpxor %xmm3, %xmm1, %xmm3
movl $0x8, %eax
vmovdqa %xmm3, %xmm8
vmovdqa %xmm2, %xmm9
vaesenc %xmm12, %xmm10, %xmm4
vaesenc %xmm10, %xmm11, %xmm5
vaesenc %xmm11, %xmm7, %xmm6
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm12, %xmm9
vpxor %xmm0, %xmm2, %xmm2
vpxor %xmm2, %xmm9, %xmm9
vaesenc %xmm4, %xmm5, %xmm12
vaesenc %xmm5, %xmm6, %xmm10
vaesenc %xmm6, %xmm7, %xmm11
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm4, %xmm4
vpxor %xmm1, %xmm3, %xmm3
vpxor %xmm3, %xmm4, %xmm9
decl %eax
jne 0x176a
cmpq $0x10, %rdx
jae 0x17ca
vmovdqa 0x60(%rsp), %xmm0
xorl %eax, %eax
jmp 0x182a
xorl %eax, %eax
vmovdqa %xmm12, %xmm2
vmovdqa %xmm10, %xmm1
vmovdqa %xmm11, %xmm4
vmovdqa %xmm7, %xmm5
vmovdqa %xmm8, %xmm3
movq %rax, %rcx
vmovdqa (%r9,%rax), %xmm0
vaesenc %xmm12, %xmm10, %xmm12
vaesenc %xmm10, %xmm11, %xmm10
vaesenc %xmm11, %xmm7, %xmm11
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm2, %xmm6
vpxor %xmm0, %xmm6, %xmm9
addq $0x10, %rax
addq $0x20, %rcx
cmpq %rdx, %rcx
jbe 0x17cc
vpand %xmm4, %xmm5, %xmm4
vpternlogq $0x96, %xmm4, %xmm2, %xmm1
vpternlogq $0x96, %xmm3, %xmm1, %xmm0
vmovdqa %xmm0, 0x60(%rsp)
andq $0xf, %rdx
je 0x18e0
vpxor %xmm0, %xmm0, %xmm0
leaq 0x90(%rsp), %r13
vmovdqa %xmm0, (%r13)
addq %rax, %r9
movq %r13, %rdi
movq %r9, %rsi
vmovdqa %xmm10, 0x30(%rsp)
vmovdqa %xmm7, 0x20(%rsp)
vmovdqa %xmm11, 0x10(%rsp)
vmovdqa %xmm9, 0x40(%rsp)
vmovdqa %xmm8, (%rsp)
vmovdqa %xmm12, 0x50(%rsp)
callq 0x1070
vmovdqa (%rsp), %xmm8
vmovdqa 0x10(%rsp), %xmm11
vmovdqa 0x20(%rsp), %xmm7
vmovdqa 0x30(%rsp), %xmm10
vmovdqa (%r13), %xmm0
vpand %xmm7, %xmm11, %xmm1
vmovdqa 0x50(%rsp), %xmm2
vpternlogq $0x96, %xmm10, %xmm2, %xmm1
vpternlogq $0x96, %xmm8, %xmm0, %xmm1
vmovdqa %xmm1, 0x60(%rsp)
vmovdqa 0x40(%rsp), %xmm3
vaesenc %xmm3, %xmm2, %xmm1
vaesenc %xmm2, %xmm10, %xmm12
vaesenc %xmm10, %xmm11, %xmm10
vaesenc %xmm11, %xmm7, %xmm11
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm3, %xmm8
vpxor %xmm0, %xmm1, %xmm9
addq $-0x10, %r12
movq %r12, (%r14)
cmpq $0x10, %r12
jae 0x18f1
xorl %ebp, %ebp
jmp 0x1949
xorl %eax, %eax
vmovdqa %xmm12, %xmm0
vpand %xmm7, %xmm11, %xmm1
vpternlogq $0x96, (%rbx,%rax), %xmm0, %xmm1
vpternlogq $0x96, %xmm10, %xmm8, %xmm1
vmovdqa %xmm1, (%r15,%rax)
vaesenc %xmm0, %xmm10, %xmm12
vaesenc %xmm10, %xmm11, %xmm10
vaesenc %xmm11, %xmm7, %xmm11
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm0, %xmm0
vpxor %xmm1, %xmm0, %xmm9
movq (%r14), %r12
leaq 0x10(%rax), %rbp
addq $0x20, %rax
vmovdqa %xmm12, %xmm0
cmpq %r12, %rax
movq %rbp, %rax
jbe 0x18f7
andq $0xf, %r12
je 0x1aa3
vpxor %xmm0, %xmm0, %xmm0
leaq 0x60(%rsp), %r13
vmovdqa %xmm0, (%r13)
leaq (%rbx,%rbp), %rsi
movq %r13, %rdi
movq %r12, %rdx
vmovdqa %xmm10, 0x30(%rsp)
vmovdqa %xmm7, 0x20(%rsp)
vmovdqa %xmm11, 0x10(%rsp)
vmovdqa %xmm9, 0x40(%rsp)
vmovdqa %xmm8, (%rsp)
vmovdqa %xmm12, 0x50(%rsp)
callq 0x1070
vmovdqa 0x10(%rsp), %xmm0
vpand 0x20(%rsp), %xmm0, %xmm1
vmovdqa 0x50(%rsp), %xmm2
vpternlogq $0x96, (%r13), %xmm2, %xmm1
vmovdqa (%rsp), %xmm0
vpternlogq $0x96, 0x30(%rsp), %xmm0, %xmm1
vmovdqa %xmm1, 0x80(%rsp)
leaq 0x90(%rsp), %r13
vmovdqa %xmm1, (%r13)
vmovdqa 0x30(%rsp), %xmm0
vaesenc %xmm2, %xmm0, %xmm0
vmovdqa %xmm0, 0x70(%rsp)
vmovdqa 0x30(%rsp), %xmm0
vmovdqa 0x10(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
vmovdqa 0x20(%rsp), %xmm0
vmovdqa 0x10(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, 0x10(%rsp)
vmovdqa 0x20(%rsp), %xmm0
vmovdqa (%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
vmovdqa 0x40(%rsp), %xmm0
vmovdqa (%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, (%rsp)
vmovdqa 0x40(%rsp), %xmm0
vaesenc %xmm0, %xmm2, %xmm0
vmovdqa %xmm0, 0x40(%rsp)
addq %rbp, %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x1070
movq (%r14), %rdx
andl $0xf, %edx
movq %r13, %rdi
xorl %esi, %esi
callq 0x1030
vmovdqa (%rsp), %xmm8
vmovdqa 0x40(%rsp), %xmm9
vmovdqa 0x10(%rsp), %xmm11
vmovdqa 0x20(%rsp), %xmm7
vmovdqa 0x30(%rsp), %xmm10
vmovdqa 0x80(%rsp), %xmm0
vpternlogq $0x96, (%r13), %xmm0, %xmm9
vmovdqa 0x70(%rsp), %xmm12
movq (%r14), %rax
vmovq 0xe0(%rsp), %xmm0
vmovq %rax, %xmm1
vpunpcklqdq %xmm1, %xmm0, %xmm0 # xmm0 = xmm0[0],xmm1[0]
vpsllq $0x3, %xmm0, %xmm0
movl $0x7, %ecx
vmovdqa %xmm11, %xmm1
vaesenc %xmm12, %xmm10, %xmm2
vaesenc %xmm10, %xmm1, %xmm10
vaesenc %xmm1, %xmm7, %xmm1
vaesenc %xmm7, %xmm8, %xmm7
vaesenc %xmm8, %xmm9, %xmm8
vaesenc %xmm9, %xmm12, %xmm9
vpternlogq $0x96, %xmm11, %xmm0, %xmm9
vmovdqa %xmm2, %xmm12
decl %ecx
jne 0x1ac6
vpternlogq $0x96, %xmm2, %xmm10, %xmm1
vpternlogq $0x96, %xmm7, %xmm1, %xmm8
vpternlogq $0x96, (%rbx,%rax), %xmm8, %xmm9
xorl %eax, %eax
vptest %xmm9, %xmm9
setne %al
negl %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop