// File: /workspace/github_c_makefile_star2_O0/zecoxao[P]ps3_decrypt_tools/lib/eid.c
// Function: eid1_decrypt_buffer
void eid1_decrypt_buffer(u8 *eid1) {
    u8 indiv[INDIV_SIZE];
    aes_context aes_ctxt;

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

    //Calculate eid1 aes omac1.
    u8 digest[AES_OMAC1_DIGEST_SIZE];
    aes_omac1(digest, eid1, 0x290, indiv + 0x20, 0x100);

    if (memcmp(digest, eid1 + 0x290, AES_OMAC1_DIGEST_SIZE) != 0)
        printf("warning: eid1 hash check failed!\n");

    //Decrypt eid1.
    aes_setkey_dec(&aes_ctxt, indiv + INDIV_EID1_KEY_OFFSET, 0x100);
    aes_crypt_cbc(&aes_ctxt, AES_DECRYPT, 0x290, indiv + INDIV_EID1_IV_OFFSET, eid1, eid1);
	
    aes_omac1(digest, eid1, 0x280, eid1_master_key, 0x80);

    if (memcmp(digest, eid1 + 0x280, AES_OMAC1_DIGEST_SIZE) != 0){
        printf("warning: eid1 second hash check failed! Using second key...\n");
		goto key2;
	}else{
		//Decrypt eid1.
		aes_setkey_dec(&aes_ctxt, eid1_master_key, 0x80);
		aes_crypt_cbc(&aes_ctxt, AES_DECRYPT, 0x280, zero_iv, eid1, eid1);
		return;
	}
	
key2:
	aes_omac1(digest, eid1, 0x280, eid1_master_key2, 0x80);
	
	if (memcmp(digest, eid1 + 0x280, AES_OMAC1_DIGEST_SIZE) != 0){
        printf("warning: eid1 second hash check failed!\n");
	}
	
    //Decrypt eid1.
	aes_setkey_dec(&aes_ctxt, eid1_master_key2, 0x80);
	aes_crypt_cbc(&aes_ctxt, AES_DECRYPT, 0x280, zero_iv, eid1, eid1);
	return;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x350, %rsp            # imm = 0x350
movq %rdi, -0x8(%rbp)
leaq -0x110(%rbp), %r8
leaq 0x9953(%rip), %rdi      # 0x143a0
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x9a80
leaq -0x350(%rbp), %rdi
movq -0x8(%rbp), %rsi
leaq -0x110(%rbp), %rcx
addq $0x20, %rcx
movl $0x290, %edx            # imm = 0x290
movl $0x100, %r8d            # imm = 0x100
callq 0x9280
leaq -0x350(%rbp), %rax
movq -0x8(%rbp), %rcx
addq $0x290, %rcx            # imm = 0x290
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 0xaac0
leaq 0x62ea(%rip), %rdi      # 0x10da3
movb $0x0, %al
callq 0x1080
leaq -0x110(%rbp), %rsi
addq $0x20, %rsi
leaq -0x340(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x1810
leaq -0x110(%rbp), %rcx
addq $0x10, %rcx
movq -0x8(%rbp), %r8
movq -0x8(%rbp), %r9
leaq -0x340(%rbp), %rdi
xorl %esi, %esi
movl $0x290, %edx            # imm = 0x290
callq 0x2eb0
leaq -0x350(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl $0x280, %edx            # imm = 0x280
leaq 0x98c7(%rip), %rcx      # 0x143e0
movl $0x80, %r8d
callq 0x9280
leaq -0x350(%rbp), %rax
movq -0x8(%rbp), %rcx
addq $0x280, %rcx            # imm = 0x280
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 0xab64
leaq 0x626a(%rip), %rdi      # 0x10dc5
movb $0x0, %al
callq 0x1080
jmp 0xaba3
leaq -0x340(%rbp), %rdi
leaq 0x986e(%rip), %rsi      # 0x143e0
movl $0x80, %edx
callq 0x1810
movq -0x8(%rbp), %r8
movq -0x8(%rbp), %r9
leaq -0x340(%rbp), %rdi
xorl %esi, %esi
movl $0x280, %edx            # imm = 0x280
leaq 0x9a37(%rip), %rcx      # 0x145d0
callq 0x2eb0
jmp 0xac3d
leaq -0x350(%rbp), %rdi
movq -0x8(%rbp), %rsi
movl $0x280, %edx            # imm = 0x280
leaq 0x9836(%rip), %rcx      # 0x143f0
movl $0x80, %r8d
callq 0x9280
leaq -0x350(%rbp), %rax
movq -0x8(%rbp), %rcx
addq $0x280, %rcx            # imm = 0x280
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 0xac03
leaq 0x6206(%rip), %rdi      # 0x10e02
movb $0x0, %al
callq 0x1080
leaq -0x340(%rbp), %rdi
leaq 0x97df(%rip), %rsi      # 0x143f0
movl $0x80, %edx
callq 0x1810
movq -0x8(%rbp), %r8
movq -0x8(%rbp), %r9
leaq -0x340(%rbp), %rdi
xorl %esi, %esi
movl $0x280, %edx            # imm = 0x280
leaq 0x9998(%rip), %rcx      # 0x145d0
callq 0x2eb0
addq $0x350, %rsp            # imm = 0x350
popq %rbp
retq
nopw %cs:(%rax,%rax)