// File: /workspace/github_c_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
andq $0x0, (%rsp)
movq %r9, %r14
movq %rcx, 0x10(%rsp)
movq %rdx, 0x50(%rsp)
andl $0x0, 0x8(%rsp)
movq (%r8), %rax
movq %rax, 0x20(%rsp)
movl 0x8(%r8), %eax
movl %eax, 0x28(%rsp)
andq $0x0, 0x48(%rsp)
testq %rsi, %rsi
je 0x1412
movq %rsi, %r12
movq %rdi, %r13
movq %r14, 0x90(%rsp)
movb $0x20, (%rsp)
xorl %esi, %esi
leaq 0x58(%rsp), %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
leaq 0x8(%rsi), %r14
vpbroadcastq %rbp, %ymm0
cmpq %r12, %r14
ja 0x135d
movq %rbp, %rax
shrq $0x28, %rax
andb $0xf, %al
movb %al, 0x6(%rsp)
vpsrlvq 0x1cf7(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movb %bpl, 0xb(%rsp)
movl 0x8(%rsp), %eax
leaq 0x2c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
addq %r13, %rsi
movl $0xc0, %edi
leaq 0x20(%rsp), %rdx
movq %r15, %rcx
vzeroupper
callq 0x10d0
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x1674
addq $-0x8, %rbx
incq %rbp
movq %r14, %rsi
jmp 0x12e4
movq %rbx, %rax
negq %rax
cmpq %r12, %rax
movq 0x90(%rsp), %r14
jae 0x1412
leaq 0x18(%rsp), %rdi
andq $0x0, (%rdi)
leaq (,%rbp,8), %rsi
addq %r13, %rsi
addq %rbx, %r12
movq %rdi, %rbx
movq %r12, %rdx
vmovdqu %ymm0, 0x70(%rsp)
vzeroupper
callq 0x1120
movb $0x60, (%rsp)
movq %rbp, %rax
shrq $0x28, %rax
andb $0xf, %al
movb %al, 0x6(%rsp)
vmovdqu 0x70(%rsp), %ymm0
vpsrlvq 0x1c41(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movb $-0x80, 0x18(%rsp,%r12)
movb %bpl, 0xb(%rsp)
movq (%rsp), %rax
leaq 0x2c(%rsp), %rcx
movq %rax, (%rcx)
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rcx)
leaq 0x20(%rsp), %rdx
leaq 0x58(%rsp), %r15
movl $0xc0, %edi
movq %rbx, %rsi
movq %r15, %rcx
vzeroupper
callq 0x10d0
leaq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0x1674
movq 0xf0(%rsp), %r15
movq %rsp, %rbx
movq %rbx, %rdi
movq %r14, %rsi
vzeroupper
callq 0x1688
leaq 0x40(%rsp), %rax
andq $0x0, (%rax)
movb (%rbx), %al
movb 0x6(%rbx), %r13b
andb $0xf, %al
movq %rax, 0x70(%rsp)
movb %al, (%rbx)
andb $-0x10, %r13b
xorl %ebx, %ebx
movq 0x10(%rsp), %r12
xorl %r14d, %r14d
leaq 0x8(%rbx), %rbp
vpbroadcastq %r14, %ymm0
cmpq 0x10(%rsp), %rbp
vmovdqu %ymm0, 0x90(%rsp)
ja 0x14f4
addq 0x50(%rsp), %rbx
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
vzeroupper
callq 0x1674
movq %r14, %rax
shrq $0x28, %rax
andb $0xf, %al
orb %r13b, %al
movb %al, 0x6(%rsp)
vmovdqu 0x90(%rsp), %ymm0
vpsrlvq 0x1b5a(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movb %r14b, 0xb(%rsp)
movl 0x8(%rsp), %eax
leaq 0x2c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
movl $0xc0, %edi
movq %rbx, %rsi
leaq 0x20(%rsp), %rdx
movq %r15, %rcx
vzeroupper
callq 0x10d0
addq $-0x8, %r12
addq $0x8, %r15
incq %r14
movq %rbp, %rbx
jmp 0x1451
testq %r12, %r12
je 0x15c6
leaq 0x18(%rsp), %rbx
andq $0x0, (%rbx)
movq 0x50(%rsp), %rax
leaq (%rax,%r14,8), %rsi
movq %rbx, %rdi
movq %r12, %rdx
vzeroupper
callq 0x1120
movb $-0x80, 0x18(%rsp,%r12)
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x1674
leaq 0xb0(%rsp), %rsi
andq $0x0, (%rsi)
movq 0x70(%rsp), %rbx
leal 0x40(%rbx), %eax
movb %al, (%rsp)
movq %r14, %rax
shrq $0x28, %rax
andb $0xf, %al
orb %al, %r13b
movb %r13b, 0x6(%rsp)
vmovdqu 0x90(%rsp), %ymm0
vpsrlvq 0x1a96(%rip), %ymm0, %ymm0 # 0x3000
vpmovqb %ymm0, 0x7(%rsp)
movb %r14b, 0xb(%rsp)
movl 0x8(%rsp), %eax
leaq 0x2c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
leaq 0x20(%rsp), %rdx
leaq 0x60(%rsp), %rcx
movl $0xc0, %edi
vzeroupper
callq 0x10d0
xorl %eax, %eax
cmpq %rax, %r12
je 0x15be
movb 0x60(%rsp,%rax), %cl
xorb 0x18(%rsp,%rax), %cl
movb %cl, (%r15)
incq %rax
incq %r15
jmp 0x15a6
orb $0x50, %bl
movb %bl, (%rsp)
jmp 0x15fa
movq 0x70(%rsp), %rax
orb $0x10, %al
movb %al, (%rsp)
movq %r14, %rax
shrq $0x28, %rax
andb $0xf, %al
orb %al, %r13b
vpsrlvq 0x1a1b(%rip), %ymm0, %ymm0 # 0x3000
movb %r13b, 0x6(%rsp)
vpmovqb %ymm0, 0x7(%rsp)
movb %r14b, 0xb(%rsp)
movl 0x8(%rsp), %eax
leaq 0x2c(%rsp), %rcx
movl %eax, 0x8(%rcx)
movq (%rsp), %rax
movq %rax, (%rcx)
leaq 0x40(%rsp), %rsi
leaq 0x20(%rsp), %rdx
leaq 0x68(%rsp), %rcx
movl $0xc0, %edi
vzeroupper
callq 0x10d0
movq 0xf0(%rsp), %rdx
addq 0x10(%rsp), %rdx
xorl %eax, %eax
cmpq $0x8, %rax
je 0x164e
movb 0x48(%rsp,%rax), %cl
xorb 0x68(%rsp,%rax), %cl
movb %cl, (%rdx,%rax)
incq %rax
jmp 0x1638
movq 0x10(%rsp), %rax
addq $0x8, %rax
movq 0xf8(%rsp), %rcx
movq %rax, (%rcx)
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq