// File: /workspace/github_c_makefile_star2_O1/mjosaarinen[P]brutus/crypto_aead/joltikeq6464v13/ref/joltik.c
// Function: joltik_aead_encrypt
void joltik_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)
{

    uint32_t i,j;
    uint64_t hblock;
    uint8_t tweak[8];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[8];
    uint8_t last_block[8];
    uint8_t Checksum[8];
    uint8_t Final[8];
    uint8_t temp[8];
    uint8_t tag[8];

    /* Fill the tweak from nonce */
    memset(tweak, 0, sizeof(tweak));

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

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

    /* The first two blocks with Nonce plaintext */
    set_stage_in_tweak(tweak, MSB_AD_N1);
    set_block_number_in_tweak_4(tweak, 0 );
    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);
        
        i = 0;
        while (8*(i+1) <= ass_data_len) {
            set_block_number_in_tweak_4 (tweak, i);
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt (TWEAKEY_STATE_SIZE, ass_data+8*i, tweakey, temp);
            xor_values (Auth, temp);
            i++;
        }
        
        /* Last incomplete BLOCK */
        if ( ass_data_len > 8*i ) {
            
            /* Prepare the last padded block */
            memset(last_block, 0, 8);
            memcpy(last_block, ass_data+8*i, ass_data_len-8*i);
            last_block[ass_data_len-8*i]=0x80;
            
            /* Encrypt the last block */
            set_stage_in_tweak(tweak, MSB_AD_LAST);
            set_block_number_in_tweak_4(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
     * first pass
     */
    
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (8*(i+1) <= m_len) {
        set_block_number_in_tweak_4(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, message + 8*i, tweakey, temp);
        xor_values(Auth, temp );
        i++;
    }
    
    /* Process incomplete block */
    if (m_len >  8*i) {
        
        /* Prepare the last padded block */
        memset(last_block, 0, 8);
        memcpy(last_block, message+8*i, m_len-8*i);
        last_block[m_len-8*i]=0x80;
        
        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO );
        set_block_number_in_tweak_4(tweak, 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_4(tweak, 0 );
    set_tweak_in_tweakey(tweakey, tweak);
    aesTweakEncrypt(TWEAKEY_STATE_SIZE, Auth, tweakey, tag );
    

    
    /* Message
     * second pass
     */
    
    i = 0;
    hblock = get_block_number_from_tag( tag );
    while (8*(i+1) <= m_len) {
        set_block_number_in_tweak_1(tweak, hblock );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, ciphertext + 8*i );
        xor_values(ciphertext + 8*i, message + 8*i );
        i++;
        hblock++;
    }
    
    /* Impartial block */
    if (m_len >  8*i) {
        set_block_number_in_tweak_1(tweak, hblock );
	set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, temp );
        
        for( j = 0; j < m_len - 8 * i; j ++)
            ciphertext[ 8 * i + j ] = message[ 8 * i + j] ^ temp[j];
    } 
    
    /* Append the authentication tag to the ciphertext */
    memcpy( ciphertext + m_len, tag, 8 );

    /* Update the size of the ciphertext (incl. tag) */
    *c_len=8+m_len;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movq %rcx, 0x18(%rsp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r13
movq (%r8), %rax
leaq 0x20(%rsp), %r15
movq %rax, (%r15)
movq $0x0, 0x10(%rsp)
movq $0x30, 0x8(%r15)
leaq 0x8(%rsp), %rbp
movl $0x80, %edi
movq %r9, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x10b0
movq (%rbp), %rax
xorq %rax, 0x10(%rsp)
movq $0x70, 0x8(%r15)
movl $0x80, %edi
movq %rbx, 0x38(%rsp)
movq %rbx, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x10b0
movq (%rbp), %rax
xorq %rax, 0x10(%rsp)
testq %r12, %r12
je 0x1584
xorl %ebp, %ebp
cmpq $0x8, %r12
jb 0x151d
xorl %ebx, %ebx
leaq 0x8(%rsp), %r15
xorl %ebp, %ebp
movl %ebp, %eax
btsq $0x3d, %rax
movbeq %rax, 0x28(%rsp)
movl %ebx, %esi
addq %r13, %rsi
movl $0x80, %edi
leaq 0x20(%rsp), %rdx
movq %r15, %rcx
callq 0x10b0
movq 0x8(%rsp), %rax
xorq %rax, 0x10(%rsp)
leal 0x10(%rbx), %eax
incl %ebp
addl $0x8, %ebx
cmpq %r12, %rax
jbe 0x14e1
leal (,%rbp,8), %eax
subq %rax, %r12
jbe 0x1584
leaq 0x30(%rsp), %r15
movq $0x0, (%r15)
addq %rax, %r13
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x1100
movb $-0x80, 0x30(%rsp,%r12)
movl %ebp, %eax
movabsq $0x6000000000000000, %rcx # imm = 0x6000000000000000
orq %rax, %rcx
leaq 0x20(%rsp), %rdx
movbeq %rcx, 0x8(%rdx)
leaq 0x8(%rsp), %r12
movl $0x80, %edi
movq %r15, %rsi
movq %r12, %rcx
callq 0x10b0
movq (%r12), %rax
xorq %rax, 0x10(%rsp)
xorl %r13d, %r13d
movq 0x18(%rsp), %rbp
cmpq $0x8, %rbp
jb 0x15d8
xorl %ebx, %ebx
leaq 0x20(%rsp), %r15
leaq 0x8(%rsp), %r12
xorl %r13d, %r13d
movl %r13d, %eax
movbeq %rax, 0x28(%rsp)
movl %ebx, %esi
addq %r14, %rsi
movl $0x80, %edi
movq %r15, %rdx
movq %r12, %rcx
callq 0x10b0
movq 0x8(%rsp), %rax
xorq %rax, 0x10(%rsp)
leal 0x10(%rbx), %eax
incl %r13d
addl $0x8, %ebx
cmpq %rbp, %rax
jbe 0x15a1
leal (,%r13,8), %esi
movq %rbp, %r15
subq %rsi, %r15
jbe 0x1639
leaq 0x30(%rsp), %r12
movq $0x0, (%r12)
addq %r14, %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x1100
movb $-0x80, 0x30(%rsp,%r15)
movl %r13d, %eax
btsq $0x3e, %rax
leaq 0x20(%rsp), %rdx
movbeq %rax, 0x8(%rdx)
leaq 0x8(%rsp), %r15
movl $0x80, %edi
movq %r12, %rsi
movq %r15, %rcx
callq 0x10b0
movq (%r15), %rax
xorq %rax, 0x10(%rsp)
leaq 0x20(%rsp), %rdx
movq $0x10, 0x8(%rdx)
leaq 0x10(%rsp), %rsi
leaq 0x40(%rsp), %r12
movl $0x80, %edi
movq %r12, %rcx
callq 0x10b0
movzbl (%r12), %eax
andl $0x7f, %eax
shlq $0x38, %rax
movzbl 0x1(%r12), %ecx
shlq $0x30, %rcx
orq %rax, %rcx
movzbl 0x2(%r12), %eax
shlq $0x28, %rax
orq %rcx, %rax
movzbl 0x3(%r12), %ecx
shlq $0x20, %rcx
orq %rax, %rcx
movl 0x4(%r12), %eax
shll $0x18, %eax
orq %rcx, %rax
movzbl 0x5(%r12), %ecx
shll $0x10, %ecx
orq %rax, %rcx
movzbl 0x6(%r12), %eax
shll $0x8, %eax
orq %rcx, %rax
movzbl 0x7(%r12), %ebp
xorq %rax, %rbp
xorl %r13d, %r13d
cmpq $0x8, 0x18(%rsp)
jae 0x16d4
movq 0x80(%rsp), %rsi
jmp 0x1760
movl $0x1, %eax
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x80(%rsp), %rsi
movl %r13d, %ecx
movl %eax, %r13d
movl %r12d, %ebx
leaq (%rsi,%rbx), %r15
addq %r14, %rbx
movq %rbp, %rax
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
orq %rdx, %rax
movbeq %rax, 0x28(%rsp)
shll $0x3, %ecx
addq %rsi, %rcx
movl $0x80, %edi
movq 0x38(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x10b0
xorl %eax, %eax
movb (%rbx,%rax), %cl
xorb %cl, (%r15,%rax)
incq %rax
cmpq $0x8, %rax
jne 0x172a
incq %rbp
leal 0x1(%r13), %eax
leal 0x8(,%r13,8), %ecx
addl $0x8, %r12d
cmpq 0x18(%rsp), %rcx
movq 0x80(%rsp), %rsi
jbe 0x16e7
shll $0x3, %r13d
movq 0x88(%rsp), %r12
movl %r13d, %r13d
movq 0x18(%rsp), %rbx
subq %r13, %rbx
jbe 0x17c4
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %rbp
leaq 0x20(%rsp), %rdx
movbeq %rbp, 0x8(%rdx)
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
movq 0x38(%rsp), %rsi
callq 0x10b0
movq 0x80(%rsp), %rsi
xorl %eax, %eax
leal (%rax,%r13), %ecx
movb 0x8(%rsp,%rax), %dl
xorb (%r14,%rcx), %dl
movb %dl, (%rsi,%rcx)
incq %rax
movl %eax, %ecx
cmpq %rcx, %rbx
ja 0x17ab
movq 0x40(%rsp), %rax
movq 0x18(%rsp), %rcx
movq %rax, (%rsi,%rcx)
addq $0x8, %rcx
movq %rcx, (%r12)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq