// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movq %r9, %rbx
movq %rcx, 0x10(%rsp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, 0x40(%rsp)
movq (%r8), %rax
leaq 0x20(%rsp), %r15
movq %rax, (%r15)
leaq 0x18(%rsp), %r13
andq $0x0, (%r13)
movq $0x30, 0x8(%r15)
leaq 0x8(%rsp), %rbp
movl $0x80, %edi
movq %r9, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x10d0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x162b
movq $0x70, 0x8(%r15)
movl $0x80, %edi
movq %rbx, 0x48(%rsp)
movq %rbx, %rsi
movq %r15, %rdx
movq %rbp, %rcx
callq 0x10d0
movq %r13, %rdi
movq %rbp, %rsi
callq 0x162b
testq %r12, %r12
je 0x1459
xorl %ebp, %ebp
leaq 0x8(%rsp), %r15
xorl %r13d, %r13d
movl %ebp, %esi
addl $0x8, %ebp
movl %r13d, %ebx
cmpq %r12, %rbp
ja 0x1400
btsq $0x3d, %rbx
movbeq %rbx, 0x28(%rsp)
addq 0x40(%rsp), %rsi
movl $0x80, %edi
leaq 0x20(%rsp), %rdx
movq %r15, %rcx
callq 0x10d0
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x162b
incl %r13d
jmp 0x13be
subq %rsi, %r12
jbe 0x1459
leaq 0x38(%rsp), %r15
andq $0x0, (%r15)
movq 0x40(%rsp), %rax
addq %rsi, %rax
movq %r15, %rdi
movq %rax, %rsi
movq %r12, %rdx
callq 0x1130
movb $-0x80, 0x38(%rsp,%r12)
movabsq $0x6000000000000000, %rax # imm = 0x6000000000000000
orq %rbx, %rax
leaq 0x20(%rsp), %rdx
movbeq %rax, 0x8(%rdx)
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
movq %r15, %rsi
callq 0x10d0
xorl %r12d, %r12d
jmp 0x145e
xorl %r12d, %r12d
jmp 0x146d
leaq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x162b
leal 0x8(,%r12,8), %eax
movl %r12d, %ebp
cmpq 0x10(%rsp), %rax
ja 0x14ad
leal 0x1(%r12), %ebx
leaq 0x20(%rsp), %rdx
movbeq %rbp, 0x8(%rdx)
shll $0x3, %r12d
addq %r14, %r12
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
movq %r12, %rsi
callq 0x10d0
movl %ebx, %r12d
jmp 0x145e
shll $0x3, %r12d
movq 0x10(%rsp), %r15
subq %r12, %r15
jbe 0x150e
leaq 0x38(%rsp), %r13
andq $0x0, (%r13)
addq %r14, %r12
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x1130
movb $-0x80, 0x38(%rsp,%r15)
btsq $0x3e, %rbp
leaq 0x20(%rsp), %rdx
movbeq %rbp, 0x8(%rdx)
leaq 0x8(%rsp), %r15
movl $0x80, %edi
movq %r13, %rsi
movq %r15, %rcx
callq 0x10d0
leaq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x162b
leaq 0x20(%rsp), %r12
movq $0x10, 0x8(%r12)
leaq 0x18(%rsp), %rsi
leaq 0x50(%rsp), %r15
movl $0x80, %edi
movq %r12, %rdx
movq %r15, %rcx
callq 0x10d0
movq %r15, %rdi
callq 0x1030
movq %rax, %r13
xorl %ebx, %ebx
leal 0x8(%rbx), %ebp
movl %ebx, %r15d
cmpq 0x10(%rsp), %rbp
ja 0x159d
movq %r13, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
orq %rcx, %rax
movbeq %rax, 0x28(%rsp)
movq 0x90(%rsp), %rax
leaq (%rax,%r15), %rbx
movl $0x80, %edi
movq 0x48(%rsp), %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x10d0
addq %r14, %r15
movq %rbx, %rdi
movq %r15, %rsi
callq 0x162b
incq %r13
movl %ebp, %ebx
jmp 0x1543
movq 0x10(%rsp), %r12
subq %r15, %r12
jbe 0x15f7
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %r13
leaq 0x20(%rsp), %rdx
movbeq %r13, 0x8(%rdx)
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
movq 0x48(%rsp), %rsi
callq 0x10d0
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, %r12
jbe 0x15f7
movl %ebx, %ecx
movb 0x8(%rsp,%rax), %dl
xorb (%r14,%rcx), %dl
movq 0x90(%rsp), %rsi
movb %dl, (%rsi,%rcx)
incl %eax
incl %ebx
jmp 0x15d5
movq 0x50(%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x90(%rsp), %rdx
movq %rax, (%rdx,%rcx)
addq $0x8, %rcx
movq 0x98(%rsp), %rax
movq %rcx, (%rax)
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq