// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/deoxysneq128128v13/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;
    uint8_t tweak[16];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[16];
    uint8_t last_block[16];
    uint8_t Checksum[16];
    uint8_t Final[16];
    uint8_t zero_block[16];
    uint8_t Pad[16];
    uint8_t temp[16];

    
    /* Fill the tweak with zeros (no nonce !!!) */
    memset(tweak, 0, sizeof(tweak));

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

    /* Associated data */
    memset(Auth, 0, 16);

    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, 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, 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 */
    memset(tweak, 0, sizeof(tweak));
    set_nonce_in_tweak(tweak, nonce);
    
    memset(Checksum, 0, 16);
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (16*(i+1) <= m_len) {
        xor_values(Checksum, message+16*i );
        set_block_number_in_tweak(tweak, i );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, message+16*i , tweakey, ciphertext+16*i );
        i++;
    }

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

        /* Create the zero block for encryption */
        memset(zero_block, 0, 16);

        /* Encrypt it */
        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO);
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, zero_block, tweakey, Pad);

        for (j=0; j<m_len-16*i; j++) {
            ciphertext[16*i+j]=last_block[j] ^ Pad[j];
        }
        set_stage_in_tweak(tweak, MSB_CHKSUM_NON_FULL);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);
    }
    else{
        set_block_number_in_tweak(tweak, i);
        set_stage_in_tweak(tweak, MSB_CHKSUM_FULL);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);
    }

    /* Append the authentication tag to the ciphertext */
    for (i=0; i<16; i++) {
        ciphertext[m_len+i]=Final[i] ^ Auth[i];
    }

    /* 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 $0xc8, %rsp
vxorps %xmm1, %xmm1, %xmm1
vmovaps %xmm1, (%rsp)
vmovups (%r8), %xmm0
movq %r9, %rbx
movq %rcx, 0x10(%rsp)
movq %rdx, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
vmovaps %xmm0, 0x30(%rsp)
vmovaps %xmm1, 0x70(%rsp)
testq %rsi, %rsi
je 0x134b
movq %rsi, %r15
movq %rbx, 0x18(%rsp)
movq %rsp, %rax
movb $0x20, (%rax)
xorl %r12d, %r12d
leaq 0x80(%rsp), %rbx
movq %rsi, %rbp
xorl %r13d, %r13d
leaq 0x10(%r12), %r14
cmpq %r15, %r14
ja 0x12c7
movq %rsp, %rdi
movq %r13, %rsi
callq 0x153f
vmovaps (%rsp), %xmm0
leaq 0x40(%rsp), %rax
vmovaps %xmm0, (%rax)
addq 0x20(%rsp), %r12
movl $0x100, %edi            # imm = 0x100
movq %r12, %rsi
leaq 0x30(%rsp), %rdx
movq %rbx, %rcx
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x1582
addq $-0x10, %rbp
incq %r13
movq %r14, %r12
jmp 0x1271
cmpq %r15, %r12
movq 0x18(%rsp), %rbx
jae 0x134b
vxorps %xmm0, %xmm0, %xmm0
leaq 0x50(%rsp), %rbx
vmovaps %xmm0, (%rbx)
movq 0x20(%rsp), %rsi
addq %r12, %rsi
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x1120
movb $-0x80, 0x50(%rsp,%rbp)
movq %rsp, %r14
movb (%r14), %al
andb $0xf, %al
orb $0x60, %al
movb %al, (%r14)
movq %r14, %rdi
movq %r13, %rsi
callq 0x153f
vmovaps (%r14), %xmm0
leaq 0x40(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x30(%rsp), %rdx
leaq 0x80(%rsp), %r14
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rsi
movq 0x18(%rsp), %rbx
movq %r14, %rcx
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x1582
movq 0x100(%rsp), %r15
movq %rsp, %r14
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%r14)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1596
leaq 0x60(%rsp), %rax
andb $0xf, (%r14)
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rax)
xorl %ebx, %ebx
leaq 0x30(%rsp), %rbp
movq 0x10(%rsp), %r12
xorl %r13d, %r13d
leaq 0x10(%rbx), %r14
cmpq 0x10(%rsp), %r14
ja 0x13e3
addq 0x28(%rsp), %rbx
leaq 0x60(%rsp), %rdi
movq %rbx, %rsi
callq 0x1582
movq %rsp, %rdi
movq %r13, %rsi
callq 0x153f
vmovaps (%rsp), %xmm0
leaq 0x40(%rsp), %rax
vmovaps %xmm0, (%rax)
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rsi
movq %rbp, %rdx
movq %r15, %rcx
callq 0x10e0
addq $-0x10, %r12
addq $0x10, %r15
incq %r13
movq %r14, %rbx
jmp 0x138a
cmpq 0x10(%rsp), %rbx
jae 0x148f
vxorps %xmm0, %xmm0, %xmm0
leaq 0x50(%rsp), %r14
vmovaps %xmm0, (%r14)
movq 0x28(%rsp), %rsi
addq %rbx, %rsi
movq %r14, %rdi
movq %r12, %rdx
callq 0x1120
movb $-0x80, 0x50(%rsp,%r12)
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x1582
leaq 0xb0(%rsp), %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rbx)
movq %rsp, %r14
movb (%r14), %al
andb $0xf, %al
orb $0x40, %al
movb %al, (%r14)
movq %r14, %rdi
movq %r13, %rsi
callq 0x153f
vmovaps (%r14), %xmm0
leaq 0x40(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x30(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rsi
callq 0x10e0
xorl %eax, %eax
cmpq %rax, %r12
je 0x149e
movb 0x90(%rsp,%rax), %cl
xorb 0x50(%rsp,%rax), %cl
movb %cl, (%r15)
incq %rax
incq %r15
jmp 0x1474
movq %rsp, %rdi
movq %r13, %rsi
callq 0x153f
movb $0x10, %al
jmp 0x14a0
movb $0x50, %al
movb (%rsp), %cl
andb $0xf, %cl
orb %al, %cl
movb %cl, (%rsp)
movb (%rsp), %al
leaq 0x40(%rsp), %rcx
movb %al, (%rcx)
movq 0x1(%rsp), %rax
movq %rax, 0x1(%rcx)
movl 0x9(%rsp), %eax
movl %eax, 0x9(%rcx)
movzwl 0xd(%rsp), %eax
movw %ax, 0xd(%rcx)
movb 0xf(%rsp), %al
movb %al, 0xf(%rcx)
leaq 0x60(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0xa0(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
movq 0x100(%rsp), %rdx
addq 0x10(%rsp), %rdx
xorl %eax, %eax
cmpq $0x10, %rax
je 0x1519
movb 0x70(%rsp,%rax), %cl
xorb 0xa0(%rsp,%rax), %cl
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x1500
movq 0x10(%rsp), %rax
addq $0x10, %rax
movq 0x108(%rsp), %rcx
movq %rax, (%rcx)
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq