// File: /workspace/github_c_makefile_star2_O2/mjosaarinen[P]brutus/crypto_aead/deoxyseq256128v13/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;        
    uint64_t hblock1,hblock2;
    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], 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, 32);

     /* Associated data */
    memset(Auth, 0, 16);
    
    /* The first two blocks with Nonce plaintext */
    set_block_number_in_tweak(tweak + 0 , 0 );
    set_block_number_in_tweak(tweak + 8 , 0 );
    set_stage_in_tweak(tweak, MSB_AD_N1);
    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 (16*(i+1) <= ass_data_len) {
            set_block_number_in_tweak(tweak + 8 , 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 + 8 , i );
            set_tweak_in_tweakey(tweakey, tweak);
            aesTweakEncrypt(TWEAKEY_STATE_SIZE, last_block, tweakey, temp);

            xor_values(Auth, temp);
        }

    }/* if ass_data_len>0 */
    
    

    /* Get the message
     * (from the second pass) 
     */
    memcpy(tweak, Tag, 16 );
    tweak[0] = 0x80 ^ (tweak[0] & 0x7f);   
    
    i = 0;
    hblock1 = get_block_number_from_tag( Tag + 0 );
    hblock2 = get_block_number_from_tag( Tag + 8 );
    
    while (16*(i+1) <= c_len) {
        set_block_number_in_tweak(tweak + 8 , hblock2 );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, message + 16*i );
	xor_values( message + 16*i  , ciphertext + 16*i);
        i++;
        hblock2++;
        if( hblock2 == 0 ){
            hblock1 ++;
            set_block_number_in_tweak(tweak , hblock1 );
            tweak[0] = 0x80 ^ (tweak[0] & 0x7f);
        }
    }
    

    
    /* Impartial block */
    if (c_len >  16*i) {
        set_block_number_in_tweak(tweak + 8 , hblock2 );
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, nonce , tweakey, temp );
        for( j = 0; j < c_len - 16 * i; j ++)
            message[ 16 * i + j ] = ciphertext[ 16 * i + j] ^ temp[j];
    } 
    

    
    /* Message 
     * first pass
     */
    
    
    memset(tweak, 0, sizeof(tweak));
    set_stage_in_tweak(tweak, MSB_M);
    i=0;
    while (16*(i+1) <= c_len) {
        set_block_number_in_tweak(tweak + 8 , i);
        set_tweak_in_tweakey(tweakey, tweak);
        aesTweakEncrypt(TWEAKEY_STATE_SIZE, message + 16*i, tweakey, temp);
        xor_values(Auth, temp );
        i++;
    }

    
    /* Process incomplete block */
    if (c_len >  16*i) {
      
        /* Prepare the last padded block */
        memset(last_block, 0, 16);
        memcpy(last_block, message+16*i, c_len-16*i);
        last_block[c_len-16*i]=0x80;

        set_stage_in_tweak(tweak, MSB_M_LAST_NONZERO );
        set_block_number_in_tweak(tweak + 8 , 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(tweak + 8 , 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 $0xf8, %rsp
movq 0x130(%rsp), %r10
movq 0x138(%rsp), %rax
movq %rax, 0x10(%rsp)
vmovups -0x10(%r10,%rax), %xmm0
vmovaps %xmm0, 0xb0(%rsp)
vmovdqu (%r8), %ymm0
leaq 0x41(%rsp), %rax
vmovdqu %ymm0, -0x21(%rax)
movq %r9, %rbp
movq %rcx, 0xd8(%rsp)
movq %rdx, 0x58(%rsp)
movq %rsi, %r15
movq %rdi, %r14
vpxor %xmm0, %xmm0, %xmm0
leaq 0x70(%rsp), %r12
vmovdqa %xmm0, (%r12)
movb $0x30, -0x1(%rax)
andq $0x0, (%rax)
andq $0x0, 0x7(%rax)
leaq 0x20(%rsp), %r13
leaq 0x60(%rsp), %rbx
movl $0x180, %edi            # imm = 0x180
movq %r9, %rsi
movq %r13, %rdx
movq %rbx, %rcx
vzeroupper
callq 0x10e0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x17ca
movb $0x70, 0x20(%r13)
andq $0x0, 0x21(%r13)
andq $0x0, 0x28(%r13)
movl $0x180, %edi            # imm = 0x180
movq %rbp, 0x88(%rsp)
movq %rbp, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x10e0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x17ca
testq %r15, %r15
je 0x19f0
xorl %esi, %esi
leaq 0x60(%rsp), %r12
movq %r15, %r13
xorl %ebx, %ebx
leaq 0x10(%rsi), %rbp
vpbroadcastq %rbx, %ymm0
cmpq %r15, %rbp
ja 0x1946
movb $0x20, 0x40(%rsp)
leaq 0x41(%rsp), %rax
andl $0x0, 0x3(%rax)
andl $0x0, (%rax)
vpsrlvq 0x370d(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
shrl $0x18, %eax
movl %ebx, %ecx
vpmovqb %ymm0, 0x48(%rsp)
shrl $0x10, %ecx
movb %al, 0x4c(%rsp)
movb %cl, 0x4d(%rsp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x4f(%rsp)
addq %r14, %rsi
movl $0x180, %edi            # imm = 0x180
leaq 0x20(%rsp), %rdx
movq %r12, %rcx
vzeroupper
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0x17ca
addq $-0x10, %r13
incq %rbx
movq %rbp, %rsi
jmp 0x18ca
cmpq %r15, %rsi
jae 0x19f0
vmovdqu %ymm0, 0x90(%rsp)
vpxor %xmm0, %xmm0, %xmm0
leaq 0xc0(%rsp), %r15
vmovdqa %xmm0, (%r15)
addq %rsi, %r14
movq %r15, %rdi
movq %r14, %rsi
movq %r13, %rdx
vzeroupper
callq 0x1120
movb $-0x80, 0xc0(%rsp,%r13)
leaq 0x20(%rsp), %rdx
movb $0x60, 0x20(%rdx)
leaq 0x41(%rsp), %rax
andl $0x0, 0x3(%rax)
andl $0x0, (%rax)
vmovdqu 0x90(%rsp), %ymm0
vpsrlvq 0x3653(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x60(%rsp), %rbx
movl $0x180, %edi            # imm = 0x180
movq %r15, %rsi
movq %rbx, %rcx
vzeroupper
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x17ca
leaq 0xb8(%rsp), %rbx
vmovd -0x7(%rbx), %xmm0
vmovdqa %xmm0, 0x90(%rsp)
addq $-0x10, 0x10(%rsp)
movb -0x8(%rbx), %r14b
movb -0x3(%rbx), %al
movl %eax, 0x1c(%rsp)
movb -0x2(%rbx), %al
movl %eax, 0x18(%rsp)
movb -0x1(%rbx), %al
movb %al, 0xf(%rsp)
orb $-0x80, %r14b
leaq 0xb0(%rsp), %rdi
vzeroupper
callq 0x1030
movq %rax, %r13
movq %rbx, %rdi
callq 0x1030
movq %rax, %rbx
xorl %r15d, %r15d
leaq 0x10(%r15), %rbp
vpbroadcastq %rbx, %ymm0
cmpq 0x10(%rsp), %rbp
ja 0x1b41
vpsrlvq 0x3598(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
shrl $0x18, %eax
movl %ebx, %ecx
movb %r14b, 0x40(%rsp)
vmovaps 0x90(%rsp), %xmm1
vmovss %xmm1, 0x41(%rsp)
movl 0x1c(%rsp), %edx
movb %dl, 0x45(%rsp)
movl 0x18(%rsp), %edx
movb %dl, 0x46(%rsp)
movb 0xf(%rsp), %dl
movb %dl, 0x47(%rsp)
vpmovqb %ymm0, 0x48(%rsp)
shrl $0x10, %ecx
movb %al, 0x4c(%rsp)
movb %cl, 0x4d(%rsp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x4f(%rsp)
movq 0x58(%rsp), %rax
leaq (%rax,%r15), %r12
movl $0x180, %edi            # imm = 0x180
movq 0x88(%rsp), %rsi
leaq 0x20(%rsp), %rdx
movq %r12, %rcx
vzeroupper
callq 0x10e0
addq 0x130(%rsp), %r15
movq %r12, %rdi
movq %r15, %rsi
callq 0x17ca
incq %rbx
jne 0x1b39
incq %r13
movq %r13, %r14
shrq $0x38, %r14
vpbroadcastq %r13, %ymm0
vpsrlvq 0x3513(%rip), %ymm0, %ymm0 # 0x5020
vpmovqb %ymm0, %xmm0
vmovdqa %xmm0, 0x90(%rsp)
movl %r13d, %eax
shrl $0x10, %eax
movl %eax, 0x1c(%rsp)
movl %r13d, %eax
shrl $0x8, %eax
movl %eax, 0x18(%rsp)
orb $-0x80, %r14b
movb %r13b, 0xf(%rsp)
movq %rbp, %r15
jmp 0x1a4a
cmpq %r15, 0x10(%rsp)
leaq 0x60(%rsp), %r12
jbe 0x1be4
vpsrlvq 0x34a6(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
leaq 0x20(%rsp), %rdx
movb %r14b, 0x20(%rdx)
vmovaps 0x90(%rsp), %xmm1
vmovss %xmm1, 0x21(%rdx)
movl 0x1c(%rsp), %ecx
movb %cl, 0x25(%rdx)
movl 0x18(%rsp), %ecx
movb %cl, 0x26(%rdx)
movb 0xf(%rsp), %cl
movb %cl, 0x27(%rdx)
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
movl $0x180, %edi            # imm = 0x180
movq 0x88(%rsp), %rsi
movq %r12, %rcx
vzeroupper
callq 0x10e0
cmpq %r15, 0x10(%rsp)
je 0x1be4
movb (%r12), %al
movq 0x130(%rsp), %rcx
xorb (%rcx,%r15), %al
movq 0x58(%rsp), %rcx
movb %al, (%rcx,%r15)
incq %r15
incq %r12
jmp 0x1bbc
xorl %esi, %esi
leaq 0x20(%rsp), %r15
leaq 0x60(%rsp), %r12
leaq 0x70(%rsp), %r13
movq 0x10(%rsp), %r14
xorl %ebx, %ebx
leaq 0x10(%rsi), %rbp
vpbroadcastq %rbx, %ymm0
cmpq 0x10(%rsp), %rbp
ja 0x1c6d
andq $0x0, 0x40(%rsp)
vpsrlvq 0x33e4(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
shrl $0x18, %eax
movl %ebx, %ecx
vpmovqb %ymm0, 0x48(%rsp)
shrl $0x10, %ecx
movb %al, 0x4c(%rsp)
movb %cl, 0x4d(%rsp)
movb %bh, 0x4e(%rsp)
movb %bl, 0x4f(%rsp)
addq 0x58(%rsp), %rsi
movl $0x180, %edi            # imm = 0x180
movq %r15, %rdx
movq %r12, %rcx
vzeroupper
callq 0x10e0
movq %r13, %rdi
movq %r12, %rsi
callq 0x17ca
addq $-0x10, %r14
incq %rbx
movq %rbp, %rsi
jmp 0x1bfc
cmpq %rsi, 0x10(%rsp)
jbe 0x1d18
vmovdqu %ymm0, 0x90(%rsp)
vpxor %xmm0, %xmm0, %xmm0
leaq 0xc0(%rsp), %r15
vmovdqa %xmm0, (%r15)
addq 0x58(%rsp), %rsi
movq %r15, %rdi
movq %r14, %rdx
vzeroupper
callq 0x1120
movb $-0x80, 0xc0(%rsp,%r14)
leaq 0x20(%rsp), %rdx
movb $0x40, 0x20(%rdx)
leaq 0x41(%rsp), %rax
andl $0x0, 0x3(%rax)
andl $0x0, (%rax)
vmovdqu 0x90(%rsp), %ymm0
vpsrlvq 0x332b(%rip), %ymm0, %ymm0 # 0x5000
movl %ebx, %eax
vpmovqb %ymm0, 0x28(%rdx)
shrl $0x18, %eax
movl %ebx, %ecx
shrl $0x10, %ecx
movb %al, 0x2c(%rdx)
movb %cl, 0x2d(%rdx)
movb %bh, 0x4e(%rsp)
movb %bl, 0x2f(%rdx)
leaq 0x60(%rsp), %rbx
movl $0x180, %edi            # imm = 0x180
movq %r15, %rsi
movq %rbx, %rcx
vzeroupper
callq 0x10e0
leaq 0x70(%rsp), %rdi
movq %rbx, %rsi
callq 0x17ca
leaq 0x20(%rsp), %rdx
movb $0x10, 0x20(%rdx)
leaq 0x41(%rsp), %rax
andq $0x0, 0x7(%rax)
andq $0x0, (%rax)
leaq 0x70(%rsp), %rsi
leaq 0xe0(%rsp), %rcx
movl $0x180, %edi            # imm = 0x180
vzeroupper
callq 0x10e0
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x10, %rcx
je 0x1d68
movb 0xe0(%rsp,%rcx), %dl
xorb 0xb0(%rsp,%rcx), %dl
orb %dl, %al
incq %rcx
jmp 0x1d4d
testb %al, %al
je 0x1d82
movq 0x58(%rsp), %rdi
xorl %esi, %esi
movq 0x10(%rsp), %rdx
callq 0x1060
pushq $-0x1
popq %rax
jmp 0x1d94
movq 0x10(%rsp), %rax
movq 0xd8(%rsp), %rcx
movq %rax, (%rcx)
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop