// File: /workspace/github_c_makefile_star2_O0/zecoxao[P]ps3_decrypt_tools/lib/aes_xts.c
// Function: aes_xts_crypt
int aes_xts_crypt(aes_xts_ctxt_t *ctxt, u64 seqno, u32 sector_size, const u8 *in, u8 *out) {
    u32 i = 0, j = 0;
    u8 tweak[0x10], buf[0x10];
    u8 carry_in, carry_out;

    //Check alignment.
    if (!(sector_size % 0x10 == 0))
        return -1;

    //Init tweak.
    ((u64 *) tweak)[0] = seqno; //_ES64(seqno);
    ((u64 *) tweak)[1] = 0;
    aes_crypt_ecb(&ctxt->twk_ctxt, AES_ENCRYPT, tweak, tweak);

    //En-/decrypt sector.
    for (i = 0; i < sector_size; i += 0x10) {
        for (j = 0; j < 0x10; j++)
            buf[j] = in[i + j] ^ tweak[j];

        //En-/decrypt 0x10 bytes.
        aes_crypt_ecb(&ctxt->aes_ctxt, ctxt->mode, buf, buf);

        for (j = 0; j < 0x10; j++)
            out[i + j] = buf[j] ^ tweak[j];

        //Update tweak (GF MUL).
        carry_in = 0;
        for (j = 0; j < 0x10; j++) {
            carry_out = (tweak[j] >> 7) & 1;
            tweak[j] = ((tweak[j] << 1) + carry_in) & 0xFF;
            carry_in = carry_out;
        }
        if (carry_out)
            tweak[0] ^= 0x87;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x1c(%rbp), %eax
andl $0xf, %eax
cmpl $0x0, %eax
je 0x96d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x981b
movq -0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x10(%rbp), %rdi
leaq -0x50(%rbp), %rdx
leaq -0x50(%rbp), %rcx
movl $0x1, %esi
callq 0x1be0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x9814
movl $0x0, -0x38(%rbp)
cmpl $0x10, -0x38(%rbp)
jae 0x9744
movq -0x28(%rbp), %rax
movl -0x34(%rbp), %ecx
addl -0x38(%rbp), %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %eax
movl -0x38(%rbp), %ecx
movzbl -0x50(%rbp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %cl
movl -0x38(%rbp), %eax
movb %cl, -0x60(%rbp,%rax)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x9710
movq -0x10(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
movq -0x10(%rbp), %rax
movl 0x460(%rax), %esi
leaq -0x60(%rbp), %rdx
leaq -0x60(%rbp), %rcx
callq 0x1be0
movl $0x0, -0x38(%rbp)
cmpl $0x10, -0x38(%rbp)
jae 0x97a1
movl -0x38(%rbp), %eax
movzbl -0x60(%rbp,%rax), %eax
movl -0x38(%rbp), %ecx
movzbl -0x50(%rbp,%rcx), %ecx
xorl %ecx, %eax
movb %al, %dl
movq -0x30(%rbp), %rax
movl -0x34(%rbp), %ecx
addl -0x38(%rbp), %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x976d
movb $0x0, -0x61(%rbp)
movl $0x0, -0x38(%rbp)
cmpl $0x10, -0x38(%rbp)
jae 0x97f2
movl -0x38(%rbp), %eax
movzbl -0x50(%rbp,%rax), %eax
sarl $0x7, %eax
andl $0x1, %eax
movb %al, -0x62(%rbp)
movl -0x38(%rbp), %eax
movzbl -0x50(%rbp,%rax), %eax
shll %eax
movzbl -0x61(%rbp), %ecx
addl %ecx, %eax
andl $0xff, %eax
movb %al, %cl
movl -0x38(%rbp), %eax
movb %cl, -0x50(%rbp,%rax)
movb -0x62(%rbp), %al
movb %al, -0x61(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x97ac
cmpb $0x0, -0x62(%rbp)
je 0x9804
movzbl -0x50(%rbp), %eax
xorl $0x87, %eax
movb %al, -0x50(%rbp)
jmp 0x9806
movl -0x34(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x34(%rbp)
jmp 0x96fd
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)
nop