// File: /workspace/github_c_makefile_star2_O3/mjosaarinen[P]brutus/crypto_aead/joltikeq6464v13/ref/joltik.c
// Function: joltik_aead_decrypt
int joltik_aead_decrypt(const uint8_t *ass_data, size_t ass_data_len,
                        uint8_t *message, size_t *m_len,
                        const uint8_t *key,
                        const uint8_t *nonce,
                        const 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 zero_block[8];
    uint8_t Pad[8];
    uint8_t Tag[8], tag[8];
    uint8_t temp[8];
    
    /* Get the tag from the last 8 bytes of the ciphertext */
    memcpy(Tag, ciphertext+c_len-8, 8);

    /* Update c_len to the actual size of the ciphertext (i.e., without the tag) */
    c_len-=8;
    
    /* Fill the tweak with zeros (no 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 */
    

    
    /* Get the message
     * (from the second pass)
     */
    
    i = 0;
    hblock = get_block_number_from_tag( Tag );
    while (8*(i+1) <= c_len) {
        set_block_number_in_tweak_1(tweak, hblock );
	set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, message + 8*i );
        xor_values( message + 8*i  , ciphertext + 8*i);
        i++;
        hblock++;
    }
    
    /* Impartial block */
    if (c_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 < c_len - 8 * i; j ++)
            message[ 8 * i + j ] = ciphertext[ 8 * i + j] ^ temp[j];
        
    } 
    

    
    /* Message
     * first pass
     */
    
    
    memset(tweak, 0, sizeof(tweak));
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (8*(i+1) <= c_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 (c_len >  8*i) {
        
        /* Prepare the last padded block */
        memset(last_block, 0, 8);
        memcpy(last_block, message+8*i, c_len-8*i);
        last_block[c_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 );
    
    
    /* If the tags does not match, return error -1 */
    if( 0 != memcmp_const(Tag, tag, sizeof(Tag)) ) {
        memset( message, 0, c_len );
        return -1;
    }
    
    *m_len=c_len;
    return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r12
movq %rcx, 0x38(%rsp)
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %rbp
movq 0x90(%rsp), %rax
movq 0x98(%rsp), %rcx
movq -0x8(%rax,%rcx), %r15
movq (%r8), %rax
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movq $0x0, 0x18(%rsp)
movq $0x30, 0x8(%r14)
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
movq %r9, %rsi
movq %r14, %rdx
callq 0x10b0
movq %r15, 0x40(%rsp)
shrq $0x38, %r15
leaq 0x8(%rsp), %rcx
movq (%rcx), %rax
xorq %rax, 0x18(%rsp)
movq $0x70, 0x8(%r14)
movl $0x80, %edi
movq %r12, 0x48(%rsp)
movq %r12, %rsi
movq %r14, %rdx
movq %rcx, %r14
callq 0x10b0
movq 0x98(%rsp), %rdi
movq (%r14), %rax
xorq %rax, 0x18(%rsp)
addq $-0x8, %rdi
testq %r13, %r13
movq %rdi, 0x10(%rsp)
je 0x1962
xorl %r14d, %r14d
cmpq $0x8, %r13
jb 0x18f0
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %r14d, %eax
btsq $0x3d, %rax
movbeq %rax, 0x28(%rsp)
movl %r12d, %esi
addq %rbp, %rsi
movl $0x80, %edi
leaq 0x20(%rsp), %rdx
leaq 0x8(%rsp), %rcx
callq 0x10b0
movq 0x8(%rsp), %rax
xorq %rax, 0x18(%rsp)
leal 0x10(%r12), %eax
incl %r14d
addl $0x8, %r12d
cmpq %r13, %rax
jbe 0x18ac
leal (,%r14,8), %eax
subq %rax, %r13
movq 0x10(%rsp), %rdi
jbe 0x1962
leaq 0x30(%rsp), %rdi
movq $0x0, (%rdi)
addq %rax, %rbp
movq %rbp, %rsi
movq %r13, %rdx
callq 0x1100
movb $-0x80, 0x30(%rsp,%r13)
movl %r14d, %eax
movabsq $0x6000000000000000, %rcx # imm = 0x6000000000000000
orq %rax, %rcx
leaq 0x20(%rsp), %rdx
movbeq %rcx, 0x8(%rdx)
leaq 0x8(%rsp), %r12
movl $0x80, %edi
leaq 0x30(%rsp), %rsi
movq %r12, %rcx
callq 0x10b0
movq 0x10(%rsp), %rdi
movq (%r12), %rax
xorq %rax, 0x18(%rsp)
movq 0x40(%rsp), %r12
vpbroadcastq %r12, %ymm0
vpsllvq 0x178a(%rip), %ymm0, %ymm0 # 0x3100
vpand 0x17a2(%rip), %ymm0, %ymm0 # 0x3120
movq %r12, %rax
shrq $0x8, %rax
andl $0xff000000, %eax       # imm = 0xFF000000
movq %r12, %rcx
shrq $0x18, %rcx
andl $0xff0000, %ecx         # imm = 0xFF0000
orq %rax, %rcx
vextracti128 $0x1, %ymm0, %xmm1
vpor %xmm1, %xmm0, %xmm0
vpshufd $0xee, %xmm0, %xmm1     # xmm1 = xmm0[2,3,2,3]
vpor %xmm1, %xmm0, %xmm0
vmovq %xmm0, %rax
shrq $0x28, %r12
andl $0xff00, %r12d          # imm = 0xFF00
orq %rcx, %r12
orq %r15, %r12
orq %rax, %r12
xorl %ebp, %ebp
cmpq $0x8, %rdi
jb 0x1a55
movl $0x1, %eax
xorl %r14d, %r14d
xorl %ebp, %ebp
movl %ebp, %ecx
movl %eax, %ebp
movl %r14d, %r13d
leaq (%rbx,%r13), %r15
addq 0x90(%rsp), %r13
movq %r12, %rax
movabsq $-0x8000000000000000, %rdx # imm = 0x8000000000000000
orq %rdx, %rax
movbeq %rax, 0x28(%rsp)
shll $0x3, %ecx
addq %rbx, %rcx
movl $0x80, %edi
movq 0x48(%rsp), %rsi
leaq 0x20(%rsp), %rdx
vzeroupper
callq 0x10b0
xorl %eax, %eax
movb (%r13,%rax), %cl
xorb %cl, (%r15,%rax)
incq %rax
cmpq $0x8, %rax
jne 0x1a25
incq %r12
leal 0x1(%rbp), %eax
leal 0x8(,%rbp,8), %ecx
addl $0x8, %r14d
movq 0x10(%rsp), %rdi
cmpq %rcx, %rdi
jae 0x19dc
shll $0x3, %ebp
movq 0x38(%rsp), %r15
movl %ebp, %r14d
movq %rdi, %r13
subq %r14, %r13
movq 0x90(%rsp), %rbp
jbe 0x1abc
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %r12
leaq 0x20(%rsp), %rdx
movbeq %r12, 0x8(%rdx)
leaq 0x8(%rsp), %rcx
movl $0x80, %edi
movq 0x48(%rsp), %rsi
vzeroupper
callq 0x10b0
movq 0x10(%rsp), %rdi
xorl %eax, %eax
leal (%r14,%rax), %ecx
movb 0x8(%rsp,%rax), %dl
xorb (%rbp,%rcx), %dl
movb %dl, (%rbx,%rcx)
incq %rax
movl %eax, %ecx
cmpq %rcx, %r13
ja 0x1aa3
xorl %r13d, %r13d
cmpq $0x8, %rdi
jb 0x1b13
xorl %ebp, %ebp
leaq 0x20(%rsp), %r14
leaq 0x8(%rsp), %r12
xorl %r13d, %r13d
movl %r13d, %eax
movbeq %rax, 0x28(%rsp)
movl %ebp, %esi
addq %rbx, %rsi
movl $0x80, %edi
movq %r14, %rdx
movq %r12, %rcx
vzeroupper
callq 0x10b0
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rax
xorq %rax, 0x18(%rsp)
leal 0x10(%rbp), %eax
incl %r13d
addl $0x8, %ebp
cmpq %rax, %rdi
jae 0x1ad4
leal (,%r13,8), %esi
movq %rdi, %r14
subq %rsi, %r14
jbe 0x1b77
leaq 0x30(%rsp), %r12
movq $0x0, (%r12)
addq %rbx, %rsi
movq %r12, %rdi
movq %r14, %rdx
vzeroupper
callq 0x1100
movb $-0x80, 0x30(%rsp,%r14)
movl %r13d, %eax
btsq $0x3e, %rax
leaq 0x20(%rsp), %rdx
movbeq %rax, 0x8(%rdx)
leaq 0x8(%rsp), %r14
movl $0x80, %edi
movq %r12, %rsi
movq %r14, %rcx
callq 0x10b0
movq (%r14), %rax
xorq %rax, 0x18(%rsp)
leaq 0x20(%rsp), %rdx
movq $0x10, 0x8(%rdx)
leaq 0x18(%rsp), %rsi
leaq 0x50(%rsp), %r14
movl $0x80, %edi
movq %r14, %rcx
vzeroupper
callq 0x10b0
movq 0x40(%rsp), %rax
xorq (%r14), %rax
je 0x1bbe
movq %rbx, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x1040
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1bc8
movq 0x10(%rsp), %rax
movq %rax, (%r15)
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop