// File: /workspace/github_c_makefile_star2_O0/zecoxao[P]ps3_decrypt_tools/lib/eid.c
// Function: eid0_decrypt_section
void eid0_decrypt_section(u8 *eid0_in, u8 *section_out, int i) {
    u8 indiv[INDIV_SIZE];
    u8 key[0x10];
	u8 key_priv[0x10];
	u8 zero_iv[0x10];
	memset (key_priv,0,0x10);
	memset (zero_iv,0,0x10);
    aes_context aes_ctxt;
	//aes_context aes_ctxt2;

    //Generate individuals.
    indiv_gen(eid0_indiv_seed, NULL, NULL, NULL, indiv);

    //Generate key.
    if (i == 0) {
        aes_setkey_enc(&aes_ctxt, indiv + INDIV_EID0_SEC_0_GENKEY_OFFSET, 0x100);
        aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, eid0_keyseed_0, key);
    } else if (i == 6) {
        aes_setkey_enc(&aes_ctxt, indiv + INDIV_EID0_SEC_0_GENKEY_OFFSET, 0x100);
        aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, eid0_keyseed_6, key);
		/*
		aes_setkey_enc(&aes_ctxt2, indiv + INDIV_EID0_SEC_0_GENKEY_OFFSET, 0x100);
        aes_crypt_ecb(&aes_ctxt2, AES_ENCRYPT, eid0_keyseed_6_priv, key_priv);
		*/
    } else if (i == 10) {
        aes_setkey_enc(&aes_ctxt, indiv + INDIV_EID0_SEC_0_GENKEY_OFFSET, 0x100);
        aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, eid0_keyseed_A, key);
    }
    //Decrypt section 0 of eid0.
    aes_setkey_dec(&aes_ctxt, key, 0x80);
    aes_crypt_cbc(&aes_ctxt, AES_DECRYPT, 0xC0, indiv + INDIV_EID0_SEC_0_IV_OFFSET, eid0_in + 0x20 + 0xC0 * i, section_out);
	
    //Calculate aes omac1.
    u8 digest[AES_OMAC1_DIGEST_SIZE];
    aes_omac1(digest, section_out, 0xA8, key, 0x80);

    if (memcmp(digest, section_out + 0xA8, AES_OMAC1_DIGEST_SIZE) != 0){
		if (i == 0) {
			printf("warning: eid0 section 0 hash check failed!\n");
		} else if (i == 6) {
			printf("warning: eid0 section 6 hash check failed!\n");
		} else if (i == 10) {
			printf("warning: eid0 section A hash check failed!\n");
		}
		
	}
	
	/*
	if (i == 6) {
		//Decrypt section 0 of eid0.
		aes_setkey_dec(&aes_ctxt2, key_priv, 0x80);
		aes_crypt_cbc(&aes_ctxt2, AES_DECRYPT, 0x20, zero_iv, section_out + 0x88, section_out + 0x88);
		
		memset (zero_iv, 0, 0x10);
		
		if (memcmp(section_out + 0x9C, zero_iv, 0xC) != 0){
			printf("Failure with padding on section 6 priv key area!\n");
		}
		else{
			_hexdump(stdout, "Section 6:", 0x88, section_out + 0x88 , 0x20, TRUE);
		}
	}
	*/
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x390, %rsp            # imm = 0x390
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
leaq -0x140(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1090
leaq -0x150(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x1090
leaq -0x120(%rbp), %r8
leaq 0xa356(%rip), %rdi      # 0x14360
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x9a80
cmpl $0x0, -0x14(%rbp)
jne 0xa05f
leaq -0x120(%rbp), %rsi
addq $0x20, %rsi
leaq -0x380(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x1250
leaq -0x130(%rbp), %rcx
leaq -0x380(%rbp), %rdi
movl $0x1, %esi
leaq 0xa46b(%rip), %rdx      # 0x144c0
callq 0x1be0
jmp 0xa0e7
cmpl $0x6, -0x14(%rbp)
jne 0xa0a2
leaq -0x120(%rbp), %rsi
addq $0x20, %rsi
leaq -0x380(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x1250
leaq -0x130(%rbp), %rcx
leaq -0x380(%rbp), %rdi
movl $0x1, %esi
leaq 0xa435(%rip), %rdx      # 0x144d0
callq 0x1be0
jmp 0xa0e5
cmpl $0xa, -0x14(%rbp)
jne 0xa0e3
leaq -0x120(%rbp), %rsi
addq $0x20, %rsi
leaq -0x380(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x1250
leaq -0x130(%rbp), %rcx
leaq -0x380(%rbp), %rdi
movl $0x1, %esi
leaq 0xa412(%rip), %rdx      # 0x144f0
callq 0x1be0
jmp 0xa0e5
jmp 0xa0e7
leaq -0x130(%rbp), %rsi
leaq -0x380(%rbp), %rdi
movl $0x80, %edx
callq 0x1810
leaq -0x120(%rbp), %rcx
addq $0x10, %rcx
movq -0x8(%rbp), %r8
addq $0x20, %r8
imull $0xc0, -0x14(%rbp), %eax
cltq
addq %rax, %r8
movq -0x10(%rbp), %r9
leaq -0x380(%rbp), %rdi
xorl %esi, %esi
movl $0xc0, %edx
callq 0x2eb0
leaq -0x390(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x130(%rbp), %rcx
movl $0xa8, %edx
movl $0x80, %r8d
callq 0x9280
leaq -0x390(%rbp), %rax
movq -0x10(%rbp), %rcx
addq $0xa8, %rcx
movups (%rcx), %xmm1
movups (%rax), %xmm0
pcmpeqb %xmm1, %xmm0
pmovmskb %xmm0, %eax
subl $0xffff, %eax           # imm = 0xFFFF
setne %al
movzbl %al, %eax
cmpl $0x0, %eax
je 0xa1cd
cmpl $0x0, -0x14(%rbp)
jne 0xa19d
leaq 0x6afd(%rip), %rdi      # 0x10c91
movb $0x0, %al
callq 0x1080
jmp 0xa1cb
cmpl $0x6, -0x14(%rbp)
jne 0xa1b3
leaq 0x6b13(%rip), %rdi      # 0x10cbd
movb $0x0, %al
callq 0x1080
jmp 0xa1c9
cmpl $0xa, -0x14(%rbp)
jne 0xa1c7
leaq 0x6b29(%rip), %rdi      # 0x10ce9
movb $0x0, %al
callq 0x1080
jmp 0xa1c9
jmp 0xa1cb
jmp 0xa1cd
addq $0x390, %rsp            # imm = 0x390
popq %rbp
retq
nopw %cs:(%rax,%rax)