// File: /workspace/github_c_makefile_star2_O1/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, %r15
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), %rax
movq %rax, 0x40(%rsp)
movq (%r8), %rax
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
movq $0x0, 0x18(%rsp)
movq $0x30, 0x8(%r14)
leaq 0x8(%rsp), %r12
movl $0x80, %edi
movq %r9, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x10b0
movq (%r12), %rax
xorq %rax, 0x18(%rsp)
movq $0x70, 0x8(%r14)
movl $0x80, %edi
movq %r15, 0x48(%rsp)
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x10b0
movq 0x98(%rsp), %rdi
movq (%r12), %rax
xorq %rax, 0x18(%rsp)
addq $-0x8, %rdi
testq %r13, %r13
movq %rdi, 0x10(%rsp)
je 0x196b
xorl %r14d, %r14d
cmpq $0x8, %r13
jb 0x18f8
xorl %r12d, %r12d
leaq 0x20(%rsp), %r15
xorl %r14d, %r14d
movl %r14d, %eax
btsq $0x3d, %rax
movbeq %rax, 0x28(%rsp)
movl %r12d, %esi
addq %rbp, %rsi
movl $0x80, %edi
movq %r15, %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 0x18b6
leal (,%r14,8), %eax
subq %rax, %r13
movq 0x10(%rsp), %rdi
jbe 0x196b
leaq 0x30(%rsp), %r15
movq $0x0, (%r15)
addq %rax, %rbp
movq %r15, %rdi
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
movq %r15, %rsi
movq %r12, %rcx
callq 0x10b0
movq 0x10(%rsp), %rdi
movq (%r12), %rax
xorq %rax, 0x18(%rsp)
movq 0x40(%rsp), %r12
movl %r12d, %ecx
andl $0x7f, %ecx
shlq $0x38, %rcx
movl $0xff00, %eax           # imm = 0xFF00
movq %r12, %rdx
andq %rax, %rdx
shlq $0x28, %rdx
orq %rcx, %rdx
movl $0xff0000, %ecx         # imm = 0xFF0000
movq %r12, %rsi
andq %rcx, %rsi
shlq $0x18, %rsi
orq %rdx, %rsi
movl %r12d, %edx
andl $0xff000000, %edx       # imm = 0xFF000000
shlq $0x8, %rdx
orq %rsi, %rdx
movq %r12, %rsi
shrq $0x8, %rsi
andl $0xff000000, %esi       # imm = 0xFF000000
orq %rdx, %rsi
movq %r12, %rdx
shrq $0x18, %rdx
andq %rcx, %rdx
orq %rsi, %rdx
movq %r12, %rcx
shrq $0x28, %rcx
andq %rax, %rcx
orq %rdx, %rcx
shrq $0x38, %r12
xorq %rcx, %r12
xorl %ebp, %ebp
cmpq $0x8, %rdi
jb 0x1a6b
movl $0x1, %eax
xorl %r14d, %r14d
xorl %ebp, %ebp
movl %ebp, %ecx
movl %eax, %ebp
movl %r14d, %r15d
leaq (%rbx,%r15), %r13
addq 0x90(%rsp), %r15
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
callq 0x10b0
xorl %eax, %eax
movb (%r15,%rax), %cl
xorb %cl, (%r13,%rax)
incq %rax
cmpq $0x8, %rax
jne 0x1a3b
incq %r12
leal 0x1(%rbp), %eax
leal 0x8(,%rbp,8), %ecx
addl $0x8, %r14d
movq 0x10(%rsp), %rdi
cmpq %rcx, %rdi
jae 0x19f5
shll $0x3, %ebp
movq 0x38(%rsp), %r15
movl %ebp, %r14d
movq %rdi, %r13
subq %r14, %r13
movq 0x90(%rsp), %rbp
jbe 0x1acf
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
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 0x1ab6
xorl %r13d, %r13d
cmpq $0x8, %rdi
jb 0x1b23
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
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 0x1ae7
leal (,%r13,8), %esi
movq %rdi, %r14
subq %rsi, %r14
jbe 0x1b84
leaq 0x30(%rsp), %r12
movq $0x0, (%r12)
addq %rbx, %rsi
movq %r12, %rdi
movq %r14, %rdx
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
callq 0x10b0
movq 0x40(%rsp), %rax
xorq (%r14), %rax
je 0x1bc8
movq %rbx, %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x1040
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
jmp 0x1bd2
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
nopl (%rax)