// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/deoxysneq128128v13/ref/deoxys.c
// Function: deoxys_aead_decrypt
int deoxys_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)
{

    uint64_t i;
    uint64_t j;
    uint8_t tweak[16];
    uint8_t tweakey[TWEAKEY_STATE_SIZE/8];
    uint8_t Auth[16];
    uint8_t last_block[16];
    uint8_t Checksum[16];
    uint8_t Final[16];
    uint8_t zero_block[16];
    uint8_t Pad[16];
    uint8_t Tag[16];
    uint8_t temp[16];

    /* Get the tag from the last 16 bytes of the ciphertext */
    memcpy(Tag, ciphertext+c_len-16, 16);

    /* Update c_len to the actual size of the ciphertext (i.e., without the tag) */
    c_len-=16;

    /* Fill the tweak with zeros (no nonce !!!) */
    memset(tweak, 0, sizeof(tweak));


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

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

    if(ass_data_len) {

        set_stage_in_tweak(tweak, MSB_AD);
        i=0;
        while (16*(i+1) <= ass_data_len) {
            set_block_number_in_tweak(tweak, i );
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt(TWEAKEY_STATE_SIZE, ass_data+16*i, tweakey, temp);
            xor_values(Auth, temp);
            i++;
        }

        /* Last block if incomplete */
        if ( ass_data_len > 16*i ) {
            memset(last_block, 0, 16);
            memcpy(last_block, ass_data+16*i, ass_data_len-16*i);
            last_block[ass_data_len-16*i]=0x80;
            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);
            xor_values(Auth, temp);
        }

    } /* if ass_data_len>0 */

    /* Ciphertext */
    memset(tweak, 0, sizeof(tweak));
    set_nonce_in_tweak(tweak, nonce);
    
    
    memset(Checksum, 0, 16);
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while(16*(i+1)<=c_len) {
        set_tweak_in_tweakey(tweakey, tweak);
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakDecrypt(TWEAKEY_STATE_SIZE, ciphertext+16*i, tweakey, message+16*i);
        xor_values(Checksum, message+16*i);
        i++;
    }

    /* Last block */
    /* If the block is full, i.e. M_last=epsilon */
    if (c_len == 16*i) {
        set_block_number_in_tweak(tweak, i);
        set_stage_in_tweak(tweak, MSB_CHKSUM_FULL);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);
        xor_values(Final, Auth);
        
        /* If the tags does not match, return error -1 */
        if( 0 != memcmp_const(Final, Tag, sizeof(Tag)) ) {
            memset( message, 0, c_len);
            return -1;
        }
        
    }
    else {
        
        /* Prepare the full-zero block */
        memset(zero_block, 0, 16);

        /* Prepare the tweak */
        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO);
        set_block_number_in_tweak(tweak, i);
        set_tweak_in_tweakey(tweakey, tweak);

        /* Encrypt */
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, zero_block, tweakey, Pad);

        /* XOR the partial ciphertext */
        memset(last_block, 0, 16);
        memcpy(last_block, ciphertext+16*i, c_len-16*i);
        memset(Pad+c_len-16*i, 0, 16-(c_len-16*i));
        xor_values(last_block, Pad);
        last_block[c_len-16*i]=0x80;

        for (j=0; j<c_len-16*i; j++) {
            message[16*i+j]=last_block[j];
        }

        /* Update checksum */
        xor_values(Checksum, last_block);

        /* Verify the tag */
        set_stage_in_tweak(tweak, MSB_CHKSUM_NON_FULL);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, Checksum, tweakey, Final);
        xor_values(Final, Auth);

        /* If the tags does not match, return error -1 */
        if( 0 != memcmp_const(Final, 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 $0xd8, %rsp
movq 0x118(%rsp), %rax
movq 0x110(%rsp), %r10
vmovups -0x10(%r10,%rax), %xmm0
vmovaps %xmm0, 0x80(%rsp)
vxorps %xmm1, %xmm1, %xmm1
vmovaps %xmm1, (%rsp)
vmovups (%r8), %xmm0
movq %r9, %r12
movq %rcx, 0x78(%rsp)
movq %rdx, 0x18(%rsp)
movq %rdi, 0x10(%rsp)
vmovaps %xmm0, 0x30(%rsp)
vmovaps %xmm1, 0x50(%rsp)
testq %rsi, %rsi
je 0x177c
movq %rsi, %r15
movq %r12, 0x70(%rsp)
movq %rsp, %rax
movb $0x20, (%rax)
xorl %r14d, %r14d
leaq 0xa0(%rsp), %r13
movq %rsi, %rbp
xorl %ebx, %ebx
leaq 0x10(%r14), %r12
cmpq %r15, %r12
ja 0x16fc
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x153f
vmovaps (%rsp), %xmm0
leaq 0x40(%rsp), %rax
vmovaps %xmm0, (%rax)
addq 0x10(%rsp), %r14
movl $0x100, %edi            # imm = 0x100
movq %r14, %rsi
leaq 0x30(%rsp), %rdx
movq %r13, %rcx
callq 0x10e0
leaq 0x50(%rsp), %rdi
movq %r13, %rsi
callq 0x1582
addq $-0x10, %rbp
incq %rbx
movq %r12, %r14
jmp 0x16a7
cmpq %r15, %r14
movq 0x70(%rsp), %r12
jae 0x177c
vxorps %xmm0, %xmm0, %xmm0
leaq 0x20(%rsp), %r15
vmovaps %xmm0, (%r15)
movq 0x10(%rsp), %rsi
addq %r14, %rsi
movq %r15, %rdi
movq %rbp, %rdx
callq 0x1120
movb $-0x80, 0x20(%rsp,%rbp)
movq %rsp, %r14
movb (%r14), %al
andb $0xf, %al
orb $0x60, %al
movb %al, (%r14)
movq %r14, %rdi
movq %rbx, %rsi
callq 0x153f
vmovaps (%r14), %xmm0
leaq 0x40(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x30(%rsp), %rdx
leaq 0xa0(%rsp), %rbx
movl $0x100, %edi            # imm = 0x100
movq %r15, %rsi
movq %rbx, %rcx
callq 0x10e0
leaq 0x50(%rsp), %rdi
movq %rbx, %rsi
callq 0x1582
movq 0x118(%rsp), %rax
addq $-0x10, %rax
movq %rax, 0x10(%rsp)
movq %rsp, %rbx
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rbx)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x1596
leaq 0x60(%rsp), %rax
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rax)
andb $0xf, (%rbx)
leaq 0x90(%rsp), %r13
leaq 0x20(%rsp), %r15
xorl %r14d, %r14d
leaq 0x30(%rsp), %r12
xorl %ebx, %ebx
leaq 0x10(%r14), %rbp
movq 0x10(%rsp), %rax
cmpq %rax, %rbp
ja 0x182f
movq %rsp, %rdi
movq %rbx, %rsi
callq 0x153f
vmovaps (%rsp), %xmm0
leaq 0x40(%rsp), %rax
vmovaps %xmm0, (%rax)
movq 0x110(%rsp), %rax
leaq (%rax,%r14), %rsi
addq 0x18(%rsp), %r14
movl $0x100, %edi            # imm = 0x100
movq %r12, %rdx
movq %r14, %rcx
callq 0x1060
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x1582
addq $-0x10, %r13
addq $-0x10, %r15
incq %rbx
movq %rbp, %r14
jmp 0x17ca
movq %rax, %rbp
subq %r14, %rbp
jne 0x18b4
movq %rsp, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x153f
movb (%r14), %al
andb $0xf, %al
orb $0x10, %al
movb %al, (%r14)
movb (%r14), %al
leaq 0x40(%rsp), %rcx
movb %al, (%rcx)
movq 0x1(%r14), %rax
movq %rax, 0x1(%rcx)
movl 0x9(%r14), %eax
movl %eax, 0x9(%rcx)
movzwl 0xd(%r14), %eax
movw %ax, 0xd(%rcx)
movb 0xf(%r14), %al
movb %al, 0xf(%rcx)
leaq 0x60(%rsp), %rsi
leaq 0x30(%rsp), %rdx
leaq 0xc0(%rsp), %rbx
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rcx
callq 0x10e0
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
callq 0x1582
leaq 0x80(%rsp), %rsi
movq %rbx, %rdi
jmp 0x1a02
vxorps %xmm0, %xmm0, %xmm0
leaq 0xb0(%rsp), %rcx
vmovaps %xmm0, (%rcx)
movq 0x18(%rsp), %rax
leaq (%rax,%r14), %r12
movq %rsp, %rdi
movb (%rdi), %al
andb $0xf, %al
orb $0x40, %al
movb %al, (%rdi)
movq %rbx, %rsi
callq 0x153f
movq %rsp, %rax
vmovaps (%rax), %xmm0
leaq 0x40(%rsp), %rax
vmovaps %xmm0, (%rax)
leaq 0x30(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movl $0x100, %edi            # imm = 0x100
leaq 0xb0(%rsp), %rsi
callq 0x10e0
leaq 0x20(%rsp), %rdi
vxorps %xmm0, %xmm0, %xmm0
vmovaps %xmm0, (%rdi)
movq 0x110(%rsp), %rsi
addq %r14, %rsi
movq %rbp, %rdx
callq 0x1120
addq 0x10(%rsp), %r13
movq 0x118(%rsp), %rax
subq %rax, %r14
addq $0x20, %r14
xorl %ebx, %ebx
movq %r13, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x1050
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x1582
movq 0x118(%rsp), %rax
movb $-0x80, -0x10(%r15,%rax)
cmpq %rbx, %rbp
je 0x1987
movb 0x20(%rsp,%rbx), %al
movb %al, (%r12)
incq %rbx
incq %r12
jmp 0x1972
leaq 0x60(%rsp), %rbx
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x1582
movb (%rsp), %al
andb $0xf, %al
orb $0x50, %al
movb %al, (%rsp)
movb (%rsp), %al
leaq 0x40(%rsp), %rcx
movb %al, (%rcx)
movq 0x1(%rsp), %rax
movq %rax, 0x1(%rcx)
movl 0x9(%rsp), %eax
movl %eax, 0x9(%rcx)
movzwl 0xd(%rsp), %eax
movw %ax, 0xd(%rcx)
movb 0xf(%rsp), %al
movb %al, 0xf(%rcx)
leaq 0x30(%rsp), %rdx
leaq 0xc0(%rsp), %r14
movl $0x100, %edi            # imm = 0x100
movq %rbx, %rsi
movq %r14, %rcx
callq 0x10e0
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x1582
leaq 0x80(%rsp), %rsi
movq %r14, %rdi
callq 0x1a42
testl %eax, %eax
je 0x1a21
movq 0x18(%rsp), %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x1050
pushq $-0x1
popq %rax
jmp 0x1a30
movq 0x78(%rsp), %rax
movq 0x10(%rsp), %rcx
movq %rcx, (%rax)
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq