// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xe8, %rsp
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rax
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movq %rcx, 0xc8(%rsp)
movq %r8, 0xc0(%rsp)
movq %r9, 0xb8(%rsp)
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
movq 0xc0(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x70(%rsp)
movq 0x8(%rax), %rax
movq %rax, 0x78(%rsp)
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
cmpq $0x0, 0xd8(%rsp)
je 0x1433
leaq 0x90(%rsp), %rdi
movl $0x20, %esi
callq 0x17e0
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0xd8(%rsp), %rax
ja 0x1352
leaq 0x90(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1810
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1900
movq 0xe0(%rsp), %rsi
movq 0xb0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x70(%rsp), %rdx
movq %rsp, %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
leaq 0x60(%rsp), %rdi
movq %rsp, %rsi
callq 0x1930
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x12c2
movq 0xd8(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x1431
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0x50(%rsp), %rdi
movq 0xe0(%rsp), %rsi
movq 0xb0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0xd8(%rsp), %rdx
movq 0xb0(%rsp), %rax
shlq $0x4, %rax
subq %rax, %rdx
callq 0x1120
movq 0xd8(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movb $-0x80, 0x50(%rsp,%rax)
leaq 0x90(%rsp), %rdi
movl $0x60, %esi
callq 0x17e0
leaq 0x90(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1810
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1900
leaq 0x50(%rsp), %rsi
leaq 0x70(%rsp), %rdx
movq %rsp, %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
leaq 0x60(%rsp), %rdi
movq %rsp, %rsi
callq 0x1930
jmp 0x1433
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0x90(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x1980
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0x90(%rsp), %rdi
xorl %esi, %esi
callq 0x17e0
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
shlq $0x4, %rax
cmpq 0xc8(%rsp), %rax
ja 0x1544
leaq 0x40(%rsp), %rdi
movq 0xd0(%rsp), %rsi
movq 0xb0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
callq 0x1930
leaq 0x90(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1810
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1900
movq 0xd0(%rsp), %rsi
movq 0xb0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
leaq 0x70(%rsp), %rdx
movq 0xf0(%rsp), %rcx
movq 0xb0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x1488
movq 0xc8(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x4, %rcx
cmpq %rcx, %rax
jbe 0x16fa
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0x50(%rsp), %rdi
movq 0xd0(%rsp), %rsi
movq 0xb0(%rsp), %rax
shlq $0x4, %rax
addq %rax, %rsi
movq 0xc8(%rsp), %rdx
movq 0xb0(%rsp), %rax
shlq $0x4, %rax
subq %rax, %rdx
callq 0x1120
movq 0xc8(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x4, %rcx
subq %rcx, %rax
movb $-0x80, 0x50(%rsp,%rax)
leaq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x1930
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1050
leaq 0x90(%rsp), %rdi
movl $0x40, %esi
callq 0x17e0
leaq 0x90(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1810
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1900
leaq 0x20(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
movq $0x0, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq 0xc8(%rsp), %rcx
movq 0xb0(%rsp), %rdx
shlq $0x4, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x16bb
movq 0xa8(%rsp), %rax
movzbl 0x50(%rsp,%rax), %eax
movq 0xa8(%rsp), %rcx
movzbl 0x10(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xf0(%rsp), %rax
movq 0xb0(%rsp), %rcx
shlq $0x4, %rcx
addq 0xa8(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x1644
leaq 0x90(%rsp), %rdi
movl $0x50, %esi
callq 0x17e0
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1900
leaq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
jmp 0x174c
leaq 0x90(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1810
leaq 0x90(%rsp), %rdi
movl $0x10, %esi
callq 0x17e0
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1900
leaq 0x40(%rsp), %rsi
leaq 0x70(%rsp), %rdx
leaq 0x30(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
callq 0x10e0
movq $0x0, 0xb0(%rsp)
cmpq $0x10, 0xb0(%rsp)
jae 0x17b2
movq 0xb0(%rsp), %rax
movzbl 0x30(%rsp,%rax), %eax
movq 0xb0(%rsp), %rcx
movzbl 0x60(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xf0(%rsp), %rax
movq 0xc8(%rsp), %rcx
addq 0xb0(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x1758
movq 0xc8(%rsp), %rcx
addq $0x10, %rcx
movq 0xf8(%rsp), %rax
movq %rcx, (%rax)
addq $0xe8, %rsp
retq
nopw %cs:(%rax,%rax)