// File: /workspace/github_c_makefile_star2_O1/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:
movl $0x1, %eax
cmpq $0x10, %r8
jb 0x1e9d
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, %r13
movq %r8, %r12
movq %rcx, %rbx
movq %rsi, %r14
movq %rdi, %r15
movq 0x140(%rsp), %rax
movq 0x138(%rsp), %rcx
movq 0x130(%rsp), %rbp
vmovdqa (%rax), %xmm0
vmovdqa (%rcx), %xmm1
vmovdqa 0x65b(%rip), %xmm5      # 0x2010
vpxor %xmm0, %xmm1, %xmm4
vpxor %xmm5, %xmm0, %xmm11
vmovdqa 0x63b(%rip), %xmm6      # 0x2000
vpxor %xmm6, %xmm0, %xmm7
movl $0xa, %eax
vmovdqa %xmm11, %xmm9
vmovdqa %xmm6, %xmm8
vmovdqa %xmm4, %xmm10
vaesenc %xmm10, %xmm11, %xmm3
vaesenc %xmm11, %xmm7, %xmm11
vaesenc %xmm7, %xmm9, %xmm7
vaesenc %xmm9, %xmm4, %xmm9
vaesenc %xmm4, %xmm6, %xmm2
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm8, %xmm5
vaesenc %xmm8, %xmm10, %xmm8
vpxor %xmm1, %xmm3, %xmm10
vpxor %xmm0, %xmm2, %xmm4
decl %eax
jne 0x19db
cmpq $0x20, %rbp
jae 0x1a19
xorl %ecx, %ecx
jmp 0x1a69
xorl %eax, %eax
vmovdqa %xmm11, %xmm0
vaesenc %xmm4, %xmm6, %xmm1
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm8, %xmm5
vaesenc %xmm8, %xmm10, %xmm8
vaesenc %xmm10, %xmm0, %xmm2
vaesenc %xmm0, %xmm7, %xmm11
vaesenc %xmm7, %xmm9, %xmm7
vpxor (%r13,%rax), %xmm2, %xmm10
vaesenc %xmm9, %xmm4, %xmm9
vpxor 0x10(%r13,%rax), %xmm1, %xmm4
leaq 0x20(%rax), %rcx
addq $0x40, %rax
vmovdqa %xmm11, %xmm0
cmpq %rbp, %rax
movq %rcx, %rax
jbe 0x1a1f
andq $0x1f, %rbp
je 0x1b7e
addq %rcx, %r13
movl $0x20, %edx
subq %rbp, %rdx
leaq (%rsp,%rbp), %rdi
addq $0xd0, %rdi
xorl %esi, %esi
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm6, 0x40(%rsp)
vmovdqa %xmm7, (%rsp)
vmovdqa %xmm8, 0x30(%rsp)
vmovdqa %xmm9, 0x20(%rsp)
vmovdqa %xmm10, 0x60(%rsp)
vmovdqa %xmm4, 0x50(%rsp)
vmovdqa %xmm11, 0x80(%rsp)
callq 0x1030
leaq 0xd0(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x1070
vmovdqa 0x20(%rsp), %xmm9
vmovdqa 0x30(%rsp), %xmm8
vmovdqa (%rsp), %xmm7
vmovdqa 0x40(%rsp), %xmm6
vmovdqa 0x10(%rsp), %xmm5
leaq 0xd0(%rsp), %rax
vmovdqa (%rax), %xmm0
vmovdqa 0x10(%rax), %xmm1
vmovdqa %xmm7, %xmm2
vpternlogq $0x78, %xmm6, %xmm5, %xmm2
vpternlogq $0x96, %xmm8, %xmm0, %xmm2
vmovdqa %xmm9, %xmm3
vmovdqa 0x80(%rsp), %xmm4
vpternlogq $0x78, %xmm4, %xmm7, %xmm3
vpternlogq $0x96, %xmm5, %xmm1, %xmm3
vmovdqa %xmm2, 0xb0(%rsp)
vmovdqa %xmm3, 0xc0(%rsp)
vmovdqa 0x60(%rsp), %xmm10
vaesenc %xmm10, %xmm4, %xmm2
vaesenc %xmm4, %xmm7, %xmm11
vaesenc %xmm7, %xmm9, %xmm7
vmovdqa 0x50(%rsp), %xmm4
vaesenc %xmm9, %xmm4, %xmm9
vaesenc %xmm4, %xmm6, %xmm4
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm8, %xmm5
vaesenc %xmm8, %xmm10, %xmm8
vpxor %xmm0, %xmm2, %xmm10
vpxor %xmm1, %xmm4, %xmm4
addq $-0x10, %r12
movq %r12, (%r14)
cmpq $0x20, %r12
jae 0x1b93
xorl %r13d, %r13d
jmp 0x1c14
xorl %eax, %eax
vmovdqa %xmm11, %xmm0
vmovdqa (%rbx,%rax), %xmm1
vpternlogq $0x78, %xmm6, %xmm5, %xmm1
vpternlogq $0x96, %xmm7, %xmm8, %xmm1
vmovdqa 0x10(%rbx,%rax), %xmm2
vpternlogq $0x78, %xmm0, %xmm7, %xmm2
vpternlogq $0x96, %xmm9, %xmm5, %xmm2
vmovdqa %xmm1, (%r15,%rax)
vmovdqa %xmm2, 0x10(%r15,%rax)
vaesenc %xmm0, %xmm7, %xmm11
vaesenc %xmm7, %xmm9, %xmm7
vaesenc %xmm9, %xmm4, %xmm9
vaesenc %xmm4, %xmm6, %xmm3
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm8, %xmm5
vaesenc %xmm8, %xmm10, %xmm8
vaesenc %xmm10, %xmm0, %xmm0
vpxor %xmm1, %xmm0, %xmm10
vpxor %xmm2, %xmm3, %xmm4
movq (%r14), %r12
leaq 0x20(%rax), %r13
addq $0x40, %rax
vmovdqa %xmm11, %xmm0
cmpq %r12, %rax
movq %r13, %rax
jbe 0x1b99
andq $0x1f, %r12
je 0x1e11
leaq (%rbx,%r13), %rax
movq %rax, 0x70(%rsp)
movl $0x20, %edx
subq %r12, %rdx
leaq (%rsp,%r12), %rdi
addq $0xb0, %rdi
xorl %esi, %esi
vmovdqa %xmm5, 0x10(%rsp)
vmovdqa %xmm6, 0x40(%rsp)
vmovdqa %xmm7, (%rsp)
vmovdqa %xmm8, 0x30(%rsp)
vmovdqa %xmm9, 0x20(%rsp)
vmovdqa %xmm10, 0x60(%rsp)
vmovdqa %xmm4, 0x50(%rsp)
vmovdqa %xmm11, 0x80(%rsp)
callq 0x1030
leaq 0xb0(%rsp), %rbp
movq %rbp, %rdi
movq 0x70(%rsp), %rsi
movq %r12, %rdx
callq 0x1070
vmovdqa (%rbp), %xmm1
vmovdqa 0x40(%rsp), %xmm0
vpternlogq $0x78, 0x10(%rsp), %xmm0, %xmm1
vmovdqa 0x30(%rsp), %xmm0
vpternlogq $0x96, (%rsp), %xmm0, %xmm1
vmovdqa %xmm1, 0x70(%rsp)
vmovdqa 0x10(%rbp), %xmm2
vmovdqa 0x80(%rsp), %xmm3
vpternlogq $0x78, (%rsp), %xmm3, %xmm2
leaq 0xd0(%rsp), %rbp
vmovdqa %xmm1, (%rbp)
vmovdqa 0x20(%rsp), %xmm0
vpternlogq $0x96, 0x10(%rsp), %xmm0, %xmm2
vmovdqa %xmm2, 0xa0(%rsp)
vmovdqa %xmm2, 0x10(%rbp)
vmovdqa (%rsp), %xmm0
vaesenc %xmm3, %xmm0, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
vmovdqa (%rsp), %xmm0
vmovdqa 0x20(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, (%rsp)
vmovdqa 0x20(%rsp), %xmm0
vmovdqa 0x50(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x20(%rsp)
vmovdqa 0x40(%rsp), %xmm0
vmovdqa 0x50(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, 0x50(%rsp)
vmovdqa 0x10(%rsp), %xmm0
vmovdqa 0x40(%rsp), %xmm1
vaesenc %xmm1, %xmm0, %xmm1
vmovdqa %xmm1, 0x40(%rsp)
vmovdqa 0x10(%rsp), %xmm0
vmovdqa 0x30(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x10(%rsp)
vmovdqa 0x30(%rsp), %xmm0
vmovdqa 0x60(%rsp), %xmm1
vaesenc %xmm0, %xmm1, %xmm0
vmovdqa %xmm0, 0x30(%rsp)
vmovdqa 0x60(%rsp), %xmm0
vaesenc %xmm0, %xmm3, %xmm0
vmovdqa %xmm0, 0x60(%rsp)
addq %r13, %r15
movq %r15, %rdi
movq %rbp, %rsi
movq %r12, %rdx
callq 0x1070
movq (%r14), %rdx
andl $0x1f, %edx
movq %rbp, %rdi
xorl %esi, %esi
callq 0x1030
vmovdqa 0x50(%rsp), %xmm4
vmovdqa 0x60(%rsp), %xmm10
vmovdqa 0x20(%rsp), %xmm9
vmovdqa 0x30(%rsp), %xmm8
vmovdqa (%rsp), %xmm7
vmovdqa 0x40(%rsp), %xmm6
vmovdqa 0x10(%rsp), %xmm5
vmovdqa 0x70(%rsp), %xmm0
vpternlogq $0x96, (%rbp), %xmm0, %xmm10
vmovdqa 0xa0(%rsp), %xmm0
vpternlogq $0x96, 0x10(%rbp), %xmm0, %xmm4
vmovdqa 0x90(%rsp), %xmm11
movq (%r14), %rax
vmovq 0x130(%rsp), %xmm0
vmovq %rax, %xmm1
vpunpcklqdq %xmm1, %xmm0, %xmm0 # xmm0 = xmm0[0],xmm1[0]
vpsllq $0x3, %xmm0, %xmm0
vpxor %xmm5, %xmm0, %xmm0
movl $0x7, %ecx
vaesenc %xmm10, %xmm11, %xmm1
vaesenc %xmm11, %xmm7, %xmm11
vaesenc %xmm7, %xmm9, %xmm7
vaesenc %xmm9, %xmm4, %xmm9
vaesenc %xmm4, %xmm6, %xmm3
vaesenc %xmm6, %xmm5, %xmm6
vaesenc %xmm5, %xmm8, %xmm5
vaesenc %xmm8, %xmm10, %xmm8
vpxor %xmm0, %xmm1, %xmm10
vpxor %xmm0, %xmm3, %xmm4
decl %ecx
jne 0x1e34
vpxor %xmm7, %xmm9, %xmm0
vpternlogq $0x96, %xmm4, %xmm0, %xmm6
vpternlogq $0x96, %xmm5, %xmm6, %xmm8
vpternlogq $0x96, (%rbx,%rax), %xmm8, %xmm10
xorl %eax, %eax
vptest %xmm10, %xmm10
setne %al
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop