// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aegis128l/aesnib/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[32], ciphertextblock[32];
    unsigned char tag[16];
    unsigned char check = 0;
    __m128i state[8], tmp, ct0, ct1, msg0, msg1;

    if (clen < 16) return 1; 

    aegis128L_initialization(k, npub, state);

    //process the associated data 
    for (i = 0; (i+32) <= adlen; i = i+32)   
    {    
        msg0 = _mm_load_si128((__m128i*)(ad+i));
        msg1 = _mm_load_si128((__m128i*)(ad+i+16));

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);

        //message is used to update the state.
        state[0] = _mm_xor_si128(state[0],msg0);
        state[4] = _mm_xor_si128(state[4],msg1);  
     }  

    // Deal with the partial block    
    // 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, state);
    }  


    //decrypt the ciphertext 
    *mlen = clen - 16;  
    for (i = 0; (i+32) <= *mlen; i += 32) {
        msg0 = _mm_load_si128((__m128i*)(c+i));
        msg1 = _mm_load_si128((__m128i*)(c+i+16));
        
        //decryption
        msg0 = _mm_xor_si128(msg0, state[6]);
        msg0 = _mm_xor_si128(msg0, state[1]);
        msg1 = _mm_xor_si128(msg1, state[2]);
        msg1 = _mm_xor_si128(msg1, state[5]);
        msg0 = _mm_xor_si128(msg0, _mm_and_si128(state[2], state[3]));
        msg1 = _mm_xor_si128(msg1, _mm_and_si128(state[6], state[7]));
        _mm_store_si128((__m128i*)(m+i), msg0); 
        _mm_store_si128((__m128i*)(m+i+16), msg1); 

        //state update function
        tmp = state[7];
        state[7] = _mm_aesenc_si128(state[6],state[7]);
        state[6] = _mm_aesenc_si128(state[5],state[6]);
        state[5] = _mm_aesenc_si128(state[4],state[5]);
        state[4] = _mm_aesenc_si128(state[3],state[4]);
        state[3] = _mm_aesenc_si128(state[2],state[3]);
        state[2] = _mm_aesenc_si128(state[1],state[2]);
        state[1] = _mm_aesenc_si128(state[0],state[1]);
        state[0] = _mm_aesenc_si128(tmp,state[0]);  

        //message is used to update the state.  
        state[0] = _mm_xor_si128(state[0],msg0);   
        state[4] = _mm_xor_si128(state[4],msg1);   
    }

    // Deal with the partial block  
    // In this program, we assume that the message length is a multiple of bytes.
    if (  (*mlen & 0x1f) != 0 )  {  
        memset(ciphertextblock, 0, 32);  
        memcpy(ciphertextblock, c+i,  *mlen & 0x1f);  
        aegis128L_dec_aut_step(plaintextblock, ciphertextblock, 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);  
        state[0] = _mm_xor_si128( state[0], _mm_load_si128((__m128i*)plaintextblock)  ) ;  
        state[4] = _mm_xor_si128( 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, state);

    //verification
    for (i = 0; i  < 16; i++) check |= (tag[i] ^ c[i+*mlen]);
    if (check == 0) return 0;
    else return 1;  
}

// Assembly:
cmpq $0x10, %r8
jae 0x1779
pushq $0x1
popq %rax
retq
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, %rbp
movq %r8, %r13
movq %rcx, %rbx
movq %rsi, %r15
movq %rdi, %r12
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %rsi
movq %rsp, %r14
movq %r14, %rdx
callq 0x1040
vmovdqa (%r14), %xmm0
vmovdqa 0x10(%r14), %xmm1
vmovdqa 0x20(%r14), %xmm2
vmovdqa 0x30(%r14), %xmm3
vmovdqa 0x70(%r14), %xmm7
vmovdqa 0x60(%r14), %xmm5
vmovdqa 0x50(%r14), %xmm4
vmovdqa 0x40(%r14), %xmm6
movq 0x110(%rsp), %r14
pushq $0x20
popq %rax
cmpq %r14, %rax
ja 0x182d
vaesenc %xmm0, %xmm7, %xmm8
vaesenc %xmm7, %xmm5, %xmm7
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm6, %xmm4
vaesenc %xmm6, %xmm3, %xmm6
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm0, %xmm1
vpxor -0x20(%rbp,%rax), %xmm8, %xmm0
vpxor -0x10(%rbp,%rax), %xmm6, %xmm6
addq $0x20, %rax
jmp 0x17ee
vmovdqa %xmm7, 0x70(%rsp)
vmovdqa %xmm5, 0x60(%rsp)
vmovdqa %xmm4, 0x50(%rsp)
vmovdqa %xmm6, 0x40(%rsp)
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm2, 0x20(%rsp)
vmovdqa %xmm1, 0x10(%rsp)
vmovdqa %xmm0, (%rsp)
andq $0x1f, %r14
je 0x18de
addq %rax, %rbp
addq $-0x20, %rbp
pushq $0x20
popq %rdx
subq %r14, %rdx
leaq (%rsp,%r14), %rdi
addq $0xb0, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0xb0(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x10b0
leaq 0x90(%rsp), %rsi
movq %rsp, %r14
leaq 0xb0(%rsp), %rdi
movq %r14, %rdx
callq 0x1060
vmovdqa (%r14), %xmm0
vmovdqa 0x10(%r14), %xmm1
vmovdqa 0x20(%r14), %xmm2
vmovdqa 0x30(%r14), %xmm3
vmovdqa 0x60(%r14), %xmm5
vmovdqa 0x50(%r14), %xmm4
vmovdqa 0x70(%r14), %xmm7
vmovdqa 0x40(%r14), %xmm6
addq $-0x10, %r13
movq %r13, (%r15)
pushq $0x20
popq %r14
cmpq %r13, %r14
ja 0x1960
vmovdqa -0x20(%rbx,%r14), %xmm8
vpternlogq $0x78, %xmm2, %xmm3, %xmm8
vpternlogq $0x96, %xmm5, %xmm1, %xmm8
vmovdqa -0x10(%rbx,%r14), %xmm9
vpternlogq $0x78, %xmm5, %xmm7, %xmm9
vpternlogq $0x96, %xmm2, %xmm4, %xmm9
vmovdqa %xmm8, -0x20(%r12,%r14)
vmovdqa %xmm9, -0x10(%r12,%r14)
vaesenc %xmm0, %xmm7, %xmm10
vaesenc %xmm7, %xmm5, %xmm7
vaesenc %xmm5, %xmm4, %xmm5
vaesenc %xmm4, %xmm6, %xmm4
vaesenc %xmm6, %xmm3, %xmm6
vaesenc %xmm3, %xmm2, %xmm3
vaesenc %xmm2, %xmm1, %xmm2
vaesenc %xmm1, %xmm0, %xmm1
vpxor %xmm8, %xmm10, %xmm0
vpxor %xmm6, %xmm9, %xmm6
movq (%r15), %r13
addq $0x20, %r14
jmp 0x18e9
vmovdqa %xmm5, 0x60(%rsp)
vmovdqa %xmm1, 0x10(%rsp)
vmovdqa %xmm2, 0x20(%rsp)
vmovdqa %xmm4, 0x50(%rsp)
vmovdqa %xmm3, 0x30(%rsp)
vmovdqa %xmm7, 0x70(%rsp)
vmovdqa %xmm6, 0x40(%rsp)
vmovdqa %xmm0, (%rsp)
movq %r13, %rbp
andq $0x1f, %rbp
je 0x1a33
leaq (%rbx,%r14), %r13
addq $-0x20, %r13
pushq $0x20
popq %rdx
subq %rbp, %rdx
leaq (%rsp,%rbp), %rdi
addq $0x90, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x90(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x10b0
leaq 0xb0(%rsp), %r13
movq %rsp, %rdx
movq %r13, %rdi
leaq 0x90(%rsp), %rsi
callq 0x10a0
leaq (%r12,%r14), %rdi
addq $-0x20, %rdi
movq %r13, %rsi
movq %r13, %r14
movq %rbp, %rdx
callq 0x10b0
movq (%r15), %r13
movl %r13d, %edx
andl $0x1f, %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x1030
vmovaps (%r14), %xmm0
vmovdqa 0x10(%r14), %xmm1
movq %rsp, %rax
vxorps (%rax), %xmm0, %xmm0
vmovaps %xmm0, (%rax)
vpxor 0x40(%rax), %xmm1, %xmm0
vmovdqa %xmm0, 0x40(%rax)
pushq $0x10
popq %rdx
leaq 0x80(%rsp), %rcx
movq %rsp, %r8
movq %r13, %rdi
movq 0x110(%rsp), %rsi
callq 0x1070
addq %r13, %rbx
xorl %ecx, %ecx
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1a6f
movb (%rbx,%rax), %dl
xorb 0x80(%rsp,%rax), %dl
orb %dl, %cl
incq %rax
jmp 0x1a58
xorl %eax, %eax
testb %cl, %cl
setne %al
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq