// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %r9, %r15
movq %rcx, 0x58(%rsp)
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, 0x40(%rsp)
movq 0xa0(%rsp), %rcx
movq 0xa8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq -0x8(%rcx,%rax), %rax
movq %rax, 0x50(%rsp)
movq (%r8), %rax
leaq 0x20(%rsp), %r14
movq %rax, (%r14)
leaq 0x18(%rsp), %r12
andq $0x0, (%r12)
movq $0x30, 0x8(%r14)
leaq 0x10(%rsp), %rbp
movl $0x80, %edi
movq %r9, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x10d0
movq %r12, %rdi
movq %rbp, %rsi
callq 0x162b
movq $0x70, 0x8(%r14)
movl $0x80, %edi
movq %r15, 0x48(%rsp)
movq %r15, %rsi
movq %r14, %rdx
movq %rbp, %rcx
callq 0x10d0
movq %r12, %rdi
movq %rbp, %rsi
callq 0x162b
testq %r13, %r13
je 0x17a4
xorl %r12d, %r12d
leaq 0x10(%rsp), %r14
xorl %ebp, %ebp
movl %r12d, %esi
addl $0x8, %r12d
movl %ebp, %r15d
cmpq %r13, %r12
ja 0x1740
btsq $0x3d, %r15
movbeq %r15, 0x28(%rsp)
addq 0x40(%rsp), %rsi
movl $0x80, %edi
leaq 0x20(%rsp), %rdx
movq %r14, %rcx
callq 0x10d0
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x162b
incl %ebp
jmp 0x16fd
subq %rsi, %r13
jbe 0x17a4
leaq 0x38(%rsp), %r14
andq $0x0, (%r14)
movq 0x40(%rsp), %rax
addq %rsi, %rax
movq %r14, %rdi
movq %rax, %rsi
movq %r13, %rdx
callq 0x1130
movb $-0x80, 0x38(%rsp,%r13)
movabsq $0x6000000000000000, %rax # imm = 0x6000000000000000
orq %r15, %rax
leaq 0x20(%rsp), %rdx
movbeq %rax, 0x8(%rdx)
leaq 0x10(%rsp), %r13
movl $0x80, %edi
movq %r14, %rsi
movq %r13, %rcx
callq 0x10d0
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x162b
addq $-0x8, 0x8(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x1030
movq %rax, %r13
xorl %r12d, %r12d
leaq 0x20(%rsp), %r14
leal 0x8(%r12), %r15d
movl %r12d, %ebp
cmpq %r15, 0x8(%rsp)
jb 0x1819
movq %r13, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
orq %rcx, %rax
movbeq %rax, 0x28(%rsp)
leaq (%rbx,%rbp), %r12
movl $0x80, %edi
movq 0x48(%rsp), %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x10d0
addq 0xa0(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
callq 0x162b
incq %r13
movl %r15d, %r12d
jmp 0x17bf
movq 0x8(%rsp), %r14
subq %rbp, %r14
jbe 0x1874
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
orq %rax, %r13
leaq 0x20(%rsp), %rdx
movbeq %r13, 0x8(%rdx)
leaq 0x10(%rsp), %rcx
movl $0x80, %edi
movq 0x48(%rsp), %rsi
callq 0x10d0
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, %r14
jbe 0x1874
movl %r12d, %ecx
movb 0x10(%rsp,%rax), %dl
movq 0xa0(%rsp), %rsi
xorb (%rsi,%rcx), %dl
movb %dl, (%rbx,%rcx)
incl %eax
incl %r12d
jmp 0x1851
movq %rbx, %r14
xorl %ebp, %ebp
leaq 0x10(%rsp), %r12
leaq 0x18(%rsp), %r13
xorl %r15d, %r15d
movl %ebp, %esi
addl $0x8, %ebp
movl %r15d, %ebx
cmpq %rbp, 0x8(%rsp)
jb 0x18c1
movbeq %rbx, 0x28(%rsp)
addq %r14, %rsi
movl $0x80, %edi
leaq 0x20(%rsp), %rdx
movq %r12, %rcx
callq 0x10d0
movq %r13, %rdi
movq %r12, %rsi
callq 0x162b
incl %r15d
jmp 0x1886
movq 0x8(%rsp), %r15
subq %rsi, %r15
jbe 0x191b
leaq 0x38(%rsp), %r12
andq $0x0, (%r12)
addq %r14, %rsi
movq %r12, %rdi
movq %r15, %rdx
callq 0x1130
movb $-0x80, 0x38(%rsp,%r15)
btsq $0x3e, %rbx
leaq 0x20(%rsp), %rdx
movbeq %rbx, 0x8(%rdx)
leaq 0x10(%rsp), %rbx
movl $0x80, %edi
movq %r12, %rsi
movq %rbx, %rcx
callq 0x10d0
leaq 0x18(%rsp), %rdi
movq %rbx, %rsi
callq 0x162b
leaq 0x20(%rsp), %rdx
movq $0x10, 0x8(%rdx)
leaq 0x18(%rsp), %rsi
leaq 0x60(%rsp), %rbx
movl $0x80, %edi
movq %rbx, %rcx
callq 0x10d0
leaq 0x50(%rsp), %rdi
pushq $0x8
popq %rdx
movq %rbx, %rsi
callq 0x10f0
testl %eax, %eax
je 0x1967
movq %r14, %rdi
xorl %esi, %esi
movq 0x8(%rsp), %rdx
callq 0x1050
pushq $-0x1
popq %rax
jmp 0x1976
movq 0x58(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, (%rax)
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)