// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/deoxyseq256128v13/ref/deoxys.c
// Function: deoxys_aead_encrypt
void deoxys_aead_encrypt(const uint8_t *ass_data, size_t ass_data_len,
                         const uint8_t *message, size_t m_len,
                         const uint8_t *key,
                         const uint8_t *nonce,
                         uint8_t *ciphertext, size_t *c_len)
{

    uint64_t i;
    uint64_t j;
    uint64_t hblock1,hblock2;
    uint8_t tweak[16];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[16];
    uint8_t last_block[16];
    uint8_t Final[16];
    uint8_t zero_block[16];
    uint8_t Pad[16];
    uint8_t temp[16];
    uint8_t tag[16];
    
    /* Fill the tweak with zeros (no nonce !!!) */
    memset(tweak, 0, sizeof(tweak));

    /* Fill the key(s) in the tweakey state */
    memcpy(tweakey, key, 32);

    /* Associated data */
    memset(Auth, 0, 16);
    
    /* The first two blocks with Nonce plaintext */
    set_block_number_in_tweak(tweak + 0 , 0 );
    set_block_number_in_tweak(tweak + 8 , 0 );
    set_stage_in_tweak(tweak, MSB_AD_N1);
    set_tweak_in_tweakey(tweakey, tweak);
    aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce, tweakey, temp);
    xor_values(Auth, temp);

    set_stage_in_tweak(tweak, MSB_AD_N2);
    set_tweak_in_tweakey(tweakey, tweak);
    aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce, tweakey, temp);
    xor_values(Auth, temp);

    if(ass_data_len) {

        set_stage_in_tweak(tweak, MSB_AD);

        /* For each full input blocks */
        i=0;
        while (16*(i+1) <= ass_data_len) {

            /* Encrypt the current block */
            set_block_number_in_tweak(tweak + 8, i );
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt(TWEAKEY_STATE_SIZE, ass_data+16*i, tweakey, temp);

            /* Update Auth value */
            xor_values(Auth, temp);

            /* Go on with the next block */
            i++;
        }

        /* Last block if incomplete */
        if ( ass_data_len > 16*i ) {
	  
            /* Prepare the last padded block */
            memset(last_block, 0, 16);
            memcpy(last_block, ass_data+16*i, ass_data_len-16*i);
            last_block[ass_data_len-16*i]=0x80;

            /* Encrypt the last block */
            set_stage_in_tweak(tweak, MSB_AD_LAST);
            set_block_number_in_tweak(tweak + 8 , i );
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt(TWEAKEY_STATE_SIZE, last_block, tweakey, temp);

            /* Update the Auth value */
            xor_values(Auth, temp);
        }

    }/* if ass_data_len>0 */

    
    
    /* Message 
     * first pass
     */
    
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (16*(i+1) <= m_len) {
        set_block_number_in_tweak(tweak + 8, i);
	set_tweak_in_tweakey(tweakey, tweak);
	aesTweakEncrypt(TWEAKEY_STATE_SIZE, message + 16*i, tweakey, temp);
        xor_values(Auth, temp );
        i++;
	
    }
    


    /* Process incomplete block */
    if (m_len >  16*i) {
      
        /* Prepare the last padded block */
        memset(last_block, 0, 16);
        memcpy(last_block, message+16*i, m_len-16*i);
        last_block[m_len-16*i]=0x80;

        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO );
        set_block_number_in_tweak(tweak + 8 , i );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, last_block, tweakey, temp);
	
        xor_values(Auth, temp );
    } 
    
    /* Last encryption */
    set_stage_in_tweak(tweak, MSB_M_LAST_ZERO );
    set_block_number_in_tweak(tweak + 8 , 0 );
    set_tweak_in_tweakey(tweakey, tweak);
    aesTweakEncrypt(TWEAKEY_STATE_SIZE, Auth, tweakey, tag );
    
    
    /* Message 
     * second pass
     */
    memcpy(tweak, tag, 16 );
    tweak[0] = 0x80 ^ (tweak[0] & 0x7f);
    
    i = 0;
    hblock1 = get_block_number_from_tag( tag + 0 );
    hblock2 = get_block_number_from_tag( tag + 8 );
    while (16*(i+1) <= m_len) {
      
        set_block_number_in_tweak(tweak + 8, hblock2 );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, ciphertext + 16*i );
        xor_values(ciphertext + 16*i, message + 16*i );
        i++;
        hblock2++;
        if( hblock2 == 0 ){
            hblock1 ++;
            set_block_number_in_tweak(tweak , hblock1 );
            tweak[0] = 0x80 ^ (tweak[0] & 0x7f);
        }
    }
    
    /* Impartial block */
    if (m_len >  16*i) {
        set_block_number_in_tweak(tweak + 8 , hblock2 );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, temp );
        for( j = 0; j < m_len - 16 * i; j ++)
            ciphertext[ 16 * i + j ] = message[ 16 * i + j] ^ temp[j];

    } 
    
    
    /* Append the authentication tag to the ciphertext */
    memcpy( ciphertext + m_len, tag, 16 ); 

    /* The authentication tag is one block long, i.e. 16 bytes */
    *c_len=m_len+16;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %r9, %r15
movq %rcx, 0x18(%rsp)
movq %rdx, %rbp
movq %rsi, %r12
movq %rdi, %r14
vmovdqu (%r8), %ymm0
leaq 0x41(%rsp), %rax
vmovdqu %ymm0, -0x21(%rax)
vpxor %xmm0, %xmm0, %xmm0
leaq 0x70(%rsp), %rcx
vmovdqa %xmm0, (%rcx)
movb $0x30, -0x1(%rax)
andq $0x0, (%rax)
andq $0x0, 0x7(%rax)
leaq 0x20(%rsp), %r13
leaq 0x60(%rsp), %rbx
movl $0x180, %edi            # imm = 0x180
movq %r9, %rsi
movq %r13, %rdx
movq %rbx, %rcx
vzeroupper
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x17ca
movb $0x70, 0x20(%r13)
andq $0x0, 0x21(%r13)
andq $0x0, 0x28(%r13)
movl $0x180, %edi            # imm = 0x180
movq %r15, 0x88(%rsp)
movq %r15, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x17ca
testq %r12, %r12
movq %rbp, 0x58(%rsp)
je 0x1448
xorl %esi, %esi
leaq 0x60(%rsp), %r15
movq %r12, %r13
xorl %ebx, %ebx
leaq 0x10(%rsi), %rbp
vpbroadcastq %rbx, %ymm0
cmpq %r12, %rbp
ja 0x139f
movb $0x20, 0x40(%rsp)
leaq 0x41(%rsp), %rax
andl $0x0, 0x3(%rax)
andl $0x0, (%rax)
vpsrlvq 0x3cb4(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
shrl $0x18, %eax
movl %ebx, %ecx
vpmovqb %ymm0, 0x48(%rsp)
shrl $0x10, %ecx
movb %al, 0x4c(%rsp)
movb %cl, 0x4d(%rsp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x4f(%rsp)
addq %r14, %rsi
movl $0x180, %edi            # imm = 0x180
leaq 0x20(%rsp), %rdx
movq %r15, %rcx
vzeroupper
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x17ca
addq $-0x10, %r13
incq %rbx
movq %rbp, %rsi
jmp 0x1323
cmpq %r12, %rsi
movq 0x58(%rsp), %rbp
jae 0x1448
vmovdqu %ymm0, 0x90(%rsp)
vpxor %xmm0, %xmm0, %xmm0
leaq 0xb0(%rsp), %r15
vmovdqa %xmm0, (%r15)
addq %rsi, %r14
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
vzeroupper
callq 0x1120
movb $-0x80, 0xb0(%rsp,%r13)
leaq 0x20(%rsp), %rdx
movb $0x60, 0x20(%rdx)
leaq 0x41(%rsp), %rax
andl $0x0, 0x3(%rax)
andl $0x0, (%rax)
vmovdqu 0x90(%rsp), %ymm0
vpsrlvq 0x3bf5(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x60(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
movq %r15, %rsi
vzeroupper
callq 0x10e0
xorl %r14d, %r14d
movq 0x18(%rsp), %r15
jmp 0x1451
xorl %ebx, %ebx
movq 0x18(%rsp), %r15
jmp 0x1463
leaq 0x70(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x17ca
movq %r14, %rbx
leaq 0x1(%rbx), %r14
movq %r14, %rax
shlq $0x4, %rax
vpbroadcastq %rbx, %ymm0
cmpq %r15, %rax
ja 0x14c8
vpsrlvq 0x3b7e(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x20(%rsp), %rdx
andq $0x0, 0x20(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
vpmovqb %ymm0, 0x28(%rdx)
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
shlq $0x4, %rbx
addq %rbp, %rbx
leaq 0x60(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
movq %rbx, %rsi
vzeroupper
callq 0x10e0
jmp 0x1451
movq %rbx, %rsi
shlq $0x4, %rsi
subq %rsi, %r15
jbe 0x1576
vmovdqu %ymm0, 0x90(%rsp)
vpxor %xmm0, %xmm0, %xmm0
leaq 0xb0(%rsp), %r14
vmovdqa %xmm0, (%r14)
addq %rbp, %rsi
movq %r14, %rdi
movq %r15, %rdx
vzeroupper
callq 0x1120
movb $-0x80, 0xb0(%rsp,%r15)
leaq 0x20(%rsp), %rdx
movb $0x40, 0x20(%rdx)
leaq 0x41(%rsp), %rax
andl $0x0, 0x3(%rax)
andl $0x0, (%rax)
vmovdqu 0x90(%rsp), %ymm0
vpsrlvq 0x3acd(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x60(%rsp), %r15
movl $0x180, %edi            # imm = 0x180
movq %r14, %rsi
movq %r15, %rcx
vzeroupper
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x17ca
leaq 0x20(%rsp), %rdx
movb $0x10, 0x20(%rdx)
leaq 0x41(%rsp), %rax
andq $0x0, 0x7(%rax)
andq $0x0, (%rax)
leaq 0x70(%rsp), %rsi
leaq 0xc0(%rsp), %rbx
movl $0x180, %edi            # imm = 0x180
movq %rbx, %rcx
vzeroupper
callq 0x10e0
vmovd 0x1(%rbx), %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movb (%rbx), %r14b
movb 0x5(%rbx), %al
movl %eax, 0x14(%rsp)
movb 0x6(%rbx), %al
movl %eax, 0x10(%rsp)
movb 0x7(%rbx), %al
movb %al, 0xf(%rsp)
leaq 0xc8(%rsp), %r15
orb $-0x80, %r14b
movq %rbx, %rdi
callq 0x1030
movq %rax, %r13
movq %r15, %rdi
callq 0x1030
movq %rax, %rbx
xorl %r12d, %r12d
movq 0x18(%rsp), %r15
leaq 0x10(%r12), %rbp
vpbroadcastq %rbx, %ymm0
cmpq %r15, %rbp
ja 0x16f0
vpsrlvq 0x39e9(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
shrl $0x18, %eax
movl %ebx, %ecx
movb %r14b, 0x40(%rsp)
vmovaps 0x90(%rsp), %xmm1
vmovss %xmm1, 0x41(%rsp)
movl 0x14(%rsp), %edx
movb %dl, 0x45(%rsp)
movl 0x10(%rsp), %edx
movb %dl, 0x46(%rsp)
movb 0xf(%rsp), %dl
movb %dl, 0x47(%rsp)
vpmovqb %ymm0, 0x48(%rsp)
shrl $0x10, %ecx
movb %al, 0x4c(%rsp)
movb %cl, 0x4d(%rsp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x4f(%rsp)
movq 0x110(%rsp), %rax
leaq (%rax,%r12), %r15
movl $0x180, %edi            # imm = 0x180
movq 0x88(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r15, %rcx
vzeroupper
callq 0x10e0
addq 0x58(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x17ca
incq %rbx
jne 0x16e8
incq %r13
movq %r13, %r14
shrq $0x38, %r14
vpbroadcastq %r13, %ymm0
vpsrlvq 0x3964(%rip), %ymm0, %ymm0 # 0x5020
vpmovqb %ymm0, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movl %r13d, %eax
shrl $0x10, %eax
movl %eax, 0x14(%rsp)
movl %r13d, %eax
shrl $0x8, %eax
movl %eax, 0x10(%rsp)
orb $-0x80, %r14b
movb %r13b, 0xf(%rsp)
movq %rbp, %r12
jmp 0x15f5
cmpq %r15, %r12
movq 0x58(%rsp), %r13
leaq 0x60(%rsp), %rbp
jae 0x178f
vpsrlvq 0x38f4(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x20(%rsp), %rdx
movb %r14b, 0x20(%rdx)
vmovaps 0x90(%rsp), %xmm1
vmovss %xmm1, 0x21(%rdx)
movl 0x14(%rsp), %ecx
movb %cl, 0x25(%rdx)
movl 0x10(%rsp), %ecx
movb %cl, 0x26(%rdx)
movb 0xf(%rsp), %cl
movb %cl, 0x27(%rdx)
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
movl $0x180, %edi            # imm = 0x180
movq 0x88(%rsp), %rsi
movq %rbp, %rcx
vzeroupper
callq 0x10e0
cmpq %r12, %r15
je 0x178f
movb (%rbp), %al
xorb (%r13,%r12), %al
movq 0x110(%rsp), %rcx
movb %al, (%rcx,%r12)
incq %r12
incq %rbp
jmp 0x176e
vmovaps 0xc0(%rsp), %xmm0
movq 0x110(%rsp), %rax
vmovups %xmm0, (%rax,%r15)
addq $0x10, %r15
movq 0x118(%rsp), %rax
movq %r15, (%rax)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
vzeroupper
retq