// File: /workspace/github_c_makefile_star2_O0/mjosaarinen[P]brutus/crypto_aead/joltikneq9696v13/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)
{

    uint64_t i;
    uint64_t j;
    uint8_t tweak[12];
    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 zero_block[8];
    uint8_t Pad[8];
    uint8_t temp[8];

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

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

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

    if(ass_data_len) {
            set_stage_in_tweak(tweak, MSB_AD);

            /* For each full input blocks */
            i=0;
            while (8*(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+8*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 > 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(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 */
    set_nonce_in_tweak(tweak, nonce);

    memset(Checksum, 0, 8);
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (8*(i+1) <= m_len) {
        xor_values(Checksum, message+8*i );
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, message+8*i, tweakey, ciphertext+8*i );
        i++;
    }
 
    /* Process last block */
    /* If the block is full, i.e. M_last=epsilon */
    if (m_len == 8*i) {
        set_stage_in_tweak(tweak, MSB_CHKSUM_FULL);
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);

    } else { /* If the block is partial and requires padding */
        memset(last_block, 0, 8);
        memcpy(last_block, message+8*i, m_len-8*i);
        last_block[m_len-8*i]=0x80;
        xor_values(Checksum, last_block);

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

        /* 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-8*i; j++) {
            ciphertext[8*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);
    }

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

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

}

// Assembly:
subq $0xa8, %rsp
movq 0xb8(%rsp), %rax
movq 0xb0(%rsp), %rax
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdx, 0x90(%rsp)
movq %rcx, 0x88(%rsp)
movq %r8, 0x80(%rsp)
movq %r9, 0x78(%rsp)
leaq 0x5c(%rsp), %rdi
xorl %esi, %esi
movl $0xc, %edx
callq 0x1040
movq 0x80(%rsp), %rax
movq (%rax), %rcx
movq %rcx, 0x40(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x48(%rsp)
leaq 0x38(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
cmpq $0x0, 0x98(%rsp)
je 0x150d
leaq 0x5c(%rsp), %rdi
movl $0x20, %esi
callq 0x1820
movq $0x0, 0x70(%rsp)
movq 0x70(%rsp), %rax
addq $0x1, %rax
shlq $0x3, %rax
cmpq 0x98(%rsp), %rax
ja 0x1440
leaq 0x5c(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
callq 0x1910
movq 0xa0(%rsp), %rsi
movq 0x70(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
leaq 0x40(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0xc0, %edi
callq 0x10d0
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1940
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x13c4
movq 0x98(%rsp), %rax
movq 0x70(%rsp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jbe 0x150b
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
leaq 0x30(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x70(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq 0x98(%rsp), %rdx
movq 0x70(%rsp), %rax
shlq $0x3, %rax
subq %rax, %rdx
callq 0x1120
movq 0x98(%rsp), %rax
movq 0x70(%rsp), %rcx
shlq $0x3, %rcx
subq %rcx, %rax
movb $-0x80, 0x30(%rsp,%rax)
leaq 0x5c(%rsp), %rdi
movl $0x60, %esi
callq 0x1820
leaq 0x5c(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
callq 0x1910
leaq 0x30(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x8(%rsp), %rcx
movl $0xc0, %edi
callq 0x10d0
leaq 0x38(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x1940
jmp 0x150d
leaq 0x5c(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1990
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
leaq 0x5c(%rsp), %rdi
xorl %esi, %esi
callq 0x1820
movq $0x0, 0x70(%rsp)
movq 0x70(%rsp), %rax
addq $0x1, %rax
shlq $0x3, %rax
cmpq 0x88(%rsp), %rax
ja 0x15e3
leaq 0x28(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x70(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0x1940
leaq 0x5c(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
callq 0x1910
movq 0x90(%rsp), %rsi
movq 0x70(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
leaq 0x40(%rsp), %rdx
movq 0xb0(%rsp), %rcx
movq 0x70(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rcx
movl $0xc0, %edi
callq 0x10d0
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x1542
movq 0x88(%rsp), %rax
movq 0x70(%rsp), %rcx
shlq $0x3, %rcx
cmpq %rcx, %rax
jne 0x1644
leaq 0x5c(%rsp), %rdi
movl $0x10, %esi
callq 0x1820
leaq 0x5c(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
callq 0x1910
leaq 0x28(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0xc0, %edi
callq 0x10d0
jmp 0x17a5
leaq 0x30(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
leaq 0x30(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x70(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq 0x88(%rsp), %rdx
movq 0x70(%rsp), %rax
shlq $0x3, %rax
subq %rax, %rdx
callq 0x1120
movq 0x88(%rsp), %rax
movq 0x70(%rsp), %rcx
shlq $0x3, %rcx
subq %rcx, %rax
movb $-0x80, 0x30(%rsp,%rax)
leaq 0x28(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1940
leaq 0x18(%rsp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1040
leaq 0x5c(%rsp), %rdi
movl $0x40, %esi
callq 0x1820
leaq 0x5c(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1850
leaq 0x40(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
callq 0x1910
leaq 0x18(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x10(%rsp), %rcx
movl $0xc0, %edi
callq 0x10d0
movq $0x0, 0x68(%rsp)
movq 0x68(%rsp), %rax
movq 0x88(%rsp), %rcx
movq 0x70(%rsp), %rdx
shlq $0x3, %rdx
subq %rdx, %rcx
cmpq %rcx, %rax
jae 0x176e
movq 0x68(%rsp), %rax
movzbl 0x30(%rsp,%rax), %eax
movq 0x68(%rsp), %rcx
movzbl 0x10(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xb0(%rsp), %rax
movq 0x70(%rsp), %rcx
shlq $0x3, %rcx
addq 0x68(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x68(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x68(%rsp)
jmp 0x170f
leaq 0x5c(%rsp), %rdi
movl $0x50, %esi
callq 0x1820
leaq 0x40(%rsp), %rdi
leaq 0x5c(%rsp), %rsi
callq 0x1910
leaq 0x28(%rsp), %rsi
leaq 0x40(%rsp), %rdx
leaq 0x20(%rsp), %rcx
movl $0xc0, %edi
callq 0x10d0
movq $0x0, 0x70(%rsp)
cmpq $0x8, 0x70(%rsp)
jae 0x17f6
movq 0x70(%rsp), %rax
movzbl 0x20(%rsp,%rax), %eax
movq 0x70(%rsp), %rcx
movzbl 0x38(%rsp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq 0xb0(%rsp), %rax
movq 0x88(%rsp), %rcx
addq 0x70(%rsp), %rcx
movb %dl, (%rax,%rcx)
movq 0x70(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x70(%rsp)
jmp 0x17ae
movq 0x88(%rsp), %rcx
addq $0x8, %rcx
movq 0xb8(%rsp), %rax
movq %rcx, (%rax)
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)