// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/aegis128l/aesnib/encrypt.c
// Function: crypto_aead_encrypt
int crypto_aead_encrypt(
	unsigned char *c,unsigned long long *clen,
	const unsigned char *m,unsigned long long mlen,
	const unsigned char *ad,unsigned long long adlen,
	const unsigned char *nsec,
	const unsigned char *npub,
	const unsigned char *k
	)
        //void aegis128L_enc_aut(unsigned char *key, unsigned char *iv, unsigned char *ad, unsigned char *plaintext,
        //unsigned char *ciphertext, unsigned char *mac, unsigned char maclen, unsigned long msglen, unsigned long adlen)
{
    unsigned long long i;  
    unsigned char plaintextblock[32], ciphertextblock[32], mac[16];
    __m128i state[8], tmp, ct0, ct1, msg0, msg1;

    //initialization stage
    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);
    }  


    // encrypt the message 
    for (i = 0; (i+32) <= mlen; i = i+32) 
    {   
        msg0 = _mm_load_si128((__m128i*)(m+i));
        msg1 = _mm_load_si128((__m128i*)(m+i+16));

        ct0 = _mm_xor_si128(msg0, state[6]);
        ct0 = _mm_xor_si128(ct0,  state[1]);
        ct1 = _mm_xor_si128(msg1, state[2]);
        ct1 = _mm_xor_si128(ct1,  state[5]);
        ct0 = _mm_xor_si128(ct0,  _mm_and_si128(state[2], state[3]));
        ct1 = _mm_xor_si128(ct1,  _mm_and_si128(state[6], state[7]));
    	_mm_store_si128((__m128i*)(c+i), ct0);
    	_mm_store_si128((__m128i*)(c+i+16), ct1);

        //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 ( (mlen & 0x1f) != 0 )  {    
         memset(plaintextblock, 0, 32);   
         memcpy(plaintextblock, m+i, mlen & 0x1f);   
         aegis128L_enc_aut_step(plaintextblock, ciphertextblock, state);  
         memcpy(c+i, ciphertextblock, mlen & 0x1f);   
    }

    // finalization stage, we assume that the tag length is multiple of bytes
    aegis128L_tag_generation(mlen,adlen, 16, mac, state);  
    *clen = mlen + 16;  
    memcpy(c+mlen, mac, 16);  
          
    return 0; 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movq %r9, %r12
movq %r8, %rbp
movq %rcx, %rbx
movq %rdx, %r13
movq %rsi, 0x8(%rsp)
movq %rdi, %r14
movq 0x130(%rsp), %rdi
movq 0x128(%rsp), %rsi
leaq 0x30(%rsp), %r15
movq %r15, %rdx
callq 0x1040
vmovdqa (%r15), %xmm0
vmovdqa 0x10(%r15), %xmm1
vmovdqa 0x20(%r15), %xmm2
vmovdqa 0x30(%r15), %xmm3
vmovdqa 0x70(%r15), %xmm7
vmovdqa 0x60(%r15), %xmm5
vmovdqa 0x50(%r15), %xmm4
vmovdqa 0x40(%r15), %xmm6
pushq $0x20
popq %rax
cmpq %r12, %rax
ja 0x1552
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 0x1513
vmovdqa %xmm7, 0xa0(%rsp)
vmovdqa %xmm5, 0x90(%rsp)
vmovdqa %xmm4, 0x80(%rsp)
vmovdqa %xmm6, 0x70(%rsp)
vmovdqa %xmm3, 0x60(%rsp)
vmovdqa %xmm2, 0x50(%rsp)
vmovdqa %xmm1, 0x40(%rsp)
vmovdqa %xmm0, 0x30(%rsp)
movq %r12, %r15
andq $0x1f, %r15
je 0x1609
addq %rax, %rbp
addq $-0x20, %rbp
pushq $0x20
popq %rdx
subq %r15, %rdx
leaq (%rsp,%r15), %rdi
addq $0x10, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x10b0
leaq 0xc0(%rsp), %rsi
leaq 0x30(%rsp), %r15
leaq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0x1060
vmovdqa (%r15), %xmm0
vmovdqa 0x10(%r15), %xmm1
vmovdqa 0x20(%r15), %xmm2
vmovdqa 0x30(%r15), %xmm3
vmovdqa 0x60(%r15), %xmm5
vmovdqa 0x50(%r15), %xmm4
vmovdqa 0x70(%r15), %xmm7
vmovdqa 0x40(%r15), %xmm6
pushq $0x20
popq %r15
cmpq %rbx, %r15
ja 0x168b
vmovdqa -0x20(%r13,%r15), %xmm8
vmovdqa -0x10(%r13,%r15), %xmm9
vmovdqa %xmm8, %xmm10
vpternlogq $0x78, %xmm2, %xmm3, %xmm10
vpternlogq $0x96, %xmm5, %xmm1, %xmm10
vmovdqa %xmm9, %xmm11
vpternlogq $0x78, %xmm5, %xmm7, %xmm11
vpternlogq $0x96, %xmm2, %xmm4, %xmm11
vmovdqa %xmm10, -0x20(%r14,%r15)
vmovdqa %xmm11, -0x10(%r14,%r15)
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
addq $0x20, %r15
jmp 0x160d
vmovdqa %xmm5, 0x90(%rsp)
vmovdqa %xmm1, 0x40(%rsp)
vmovdqa %xmm2, 0x50(%rsp)
vmovdqa %xmm4, 0x80(%rsp)
vmovdqa %xmm3, 0x60(%rsp)
vmovdqa %xmm7, 0xa0(%rsp)
vmovdqa %xmm6, 0x70(%rsp)
vmovdqa %xmm0, 0x30(%rsp)
movq %rbx, %rbp
andq $0x1f, %rbp
je 0x1726
addq %r15, %r13
addq $-0x20, %r13
pushq $0x20
popq %rdx
subq %rbp, %rdx
leaq (%rsp,%rbp), %rdi
addq $0x10, %rdi
xorl %esi, %esi
callq 0x1030
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x10b0
leaq 0xc0(%rsp), %r13
leaq 0x30(%rsp), %rdx
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0x1060
leaq (%r14,%r15), %rdi
addq $-0x20, %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x10b0
pushq $0x10
popq %rdx
leaq 0xb0(%rsp), %r15
leaq 0x30(%rsp), %r8
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rcx
callq 0x1070
leaq 0x10(%rbx), %rax
movq 0x8(%rsp), %rcx
movq %rax, (%rcx)
vmovaps (%r15), %xmm0
vmovups %xmm0, (%r14,%rbx)
xorl %eax, %eax
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq