// File: /workspace/github_c_makefile_star2_O1/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 %r15
pushq %r14
pushq %rbx
subq $0x340, %rsp            # imm = 0x340
movq %rdi, %rbx
leaq 0xa1a7(%rip), %rdi      # 0x103a0
leaq 0x10(%rsp), %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x56a8
leaq 0x30(%rsp), %r14
movq %rsp, %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x290, %edx            # imm = 0x290
movq %r14, %rcx
movl $0x100, %r8d            # imm = 0x100
callq 0x504d
movdqu 0x290(%rbx), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x624e
leaq 0x6b88(%rip), %rdi      # 0xcdd1
callq 0x1050
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x153b
leaq 0x20(%rsp), %rcx
movl $0x290, %edx            # imm = 0x290
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %r8
movq %rbx, %r9
callq 0x1eae
leaq 0xa159(%rip), %r14      # 0x103e0
movq %rsp, %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x280, %edx            # imm = 0x280
movq %r14, %rcx
movl $0x80, %r8d
callq 0x504d
movdqu 0x280(%rbx), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x630e
leaq 0x6b30(%rip), %rdi      # 0xcdf2
callq 0x1050
leaq 0xa122(%rip), %r14      # 0x103f0
movq %rsp, %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x280, %edx            # imm = 0x280
movq %r14, %rcx
movl $0x80, %r8d
callq 0x504d
movdqu 0x280(%rbx), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x630e
leaq 0x6b25(%rip), %rdi      # 0xce2e
callq 0x1050
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x80, %edx
callq 0x153b
leaq 0xa2a3(%rip), %rcx      # 0x105d0
movl $0x280, %edx            # imm = 0x280
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %r8
movq %rbx, %r9
callq 0x1eae
addq $0x340, %rsp            # imm = 0x340
popq %rbx
popq %r14
popq %r15
retq