// File: /workspace/github_c_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x348, %rsp            # imm = 0x348
movq %rdi, %rbx
leaq 0x9394(%rip), %rdi      # 0xf3b0
leaq 0x10(%rsp), %r8
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x54fc
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 0x4ee8
movdqu 0x290(%rbx), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6071
leaq 0x5d85(%rip), %rdi      # 0xbdf1
callq 0x1050
leaq 0x118(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x1541
leaq 0x20(%rsp), %rcx
movl $0x290, %edx            # imm = 0x290
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %r8
movq %rbx, %r9
callq 0x1de8
leaq 0x9346(%rip), %r14      # 0xf3f0
movq %rsp, %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x280, %edx            # imm = 0x280
movq %r14, %rcx
movl $0x80, %r8d
callq 0x4ee8
movdqu 0x280(%rbx), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6136
leaq 0x280(%rbx), %r12
leaq 0x5d26(%rip), %rdi      # 0xbe12
callq 0x1050
leaq 0x9308(%rip), %r14      # 0xf400
movq %rsp, %r15
movq %r15, %rdi
movq %rbx, %rsi
movl $0x280, %edx            # imm = 0x280
movq %r14, %rcx
movl $0x80, %r8d
callq 0x4ee8
movdqu (%r12), %xmm0
pcmpeqb (%r15), %xmm0
pmovmskb %xmm0, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6136
leaq 0x5d1d(%rip), %rdi      # 0xbe4e
callq 0x1050
leaq 0x118(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movl $0x80, %edx
callq 0x1541
leaq 0x948b(%rip), %rcx      # 0xf5e0
movl $0x280, %edx            # imm = 0x280
movq %r15, %rdi
xorl %esi, %esi
movq %rbx, %r8
movq %rbx, %r9
callq 0x1de8
addq $0x348, %rsp            # imm = 0x348
popq %rbx
popq %r12
popq %r14
popq %r15
retq