// File: /workspace/github_c_makefile_star2_O0/zecoxao[P]ps3_decrypt_tools/lib/main.c
// Function: syscon_auth
void syscon_auth() {
    
    u8 indiv[0x40];
    u8 indiv_key[0x20];
    //u8 zero_iv[0x10] = {0};
    u8 enc_key_seed[INDIV_SIZE];
	u8 *eid1_dec;
	u8 session_key[0x10];
	u8 enc_eid1[INDIV_SIZE];
	u8 another_enc_eid1[INDIV_SIZE];

    //fetching root_key
    eid_root_key = _read_buffer((s8*) "data/eid_root_key", NULL);

    //Generate individuals.
    indiv_gen(eid1_indiv_seed, NULL, NULL, NULL, indiv);
    _write_buffer((s8*) "syscon/indiv", indiv, 0x40);

    //Generate seeds
    //memcpy(indiv_key, indiv + 0x20, 0x20);
    //aes_setkey_enc(&aes_ctxt, indiv_key, KEY_BITS(0x20));
    //aes_crypt_cbc(&aes_ctxt, AES_ENCRYPT, INDIV_SIZE, zero_iv, syscon_key_seed, enc_key_seed);
    //_write_buffer((s8*) "syscon/enc_key_seed", enc_key_seed, INDIV_SIZE);
	
	memcpy(indiv_key, indiv + 0x20, 0x20);
    
	for(int i=0;i<0x100;i=i+0x10){
		aes_context aes_ctxt;
		aes_setkey_enc(&aes_ctxt, indiv_key, KEY_BITS(0x20));
		aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, syscon_key_seed+i, enc_key_seed+i);
	}
    _write_buffer((s8*) "syscon/enc_key_seed", enc_key_seed, INDIV_SIZE);
	
	eid1_dec = _read_buffer((s8*) "eid/eid1decrypted.bin", NULL);
	
	
	for(int j=0;j<0x100;j=j+0x10){
		//AUTH1
		if(j%0x20==0){
			memcpy(session_key,session_key_create_key + (j / 0x20) * 0x10,0x10);
			aes_context aes_ctxt;
			aes_setkey_enc(&aes_ctxt, session_key, KEY_BITS(0x10));
			aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, eid1_dec + 0x10 + (j / 0x20) * 0x10, enc_eid1 + j);
			if(memcmp(enc_eid1+j,enc_key_seed+j,0x10)!=0){
				printf("warning! auth1 eid1 even offset %d mismatch!\n", (j/0x20));
			}else{
				hexDump(enc_eid1+j, 0x10);
				hexDump(enc_key_seed+j,0x10);
			}
		}
	}
	free(eid1_dec);
	
	eid1_dec = _read_buffer((s8*) "eid/eid1decrypted.bin", NULL);
	
	for(int k=0;k<0x80;k=k+0x10){
		//AUTH2
		memcpy(session_key,session_key_create_key + k,0x10);
		aes_context aes_ctxt;
		aes_setkey_enc(&aes_ctxt, session_key, KEY_BITS(0x10));
		aes_crypt_ecb(&aes_ctxt, AES_ENCRYPT, eid1_dec+0x90 + k, another_enc_eid1 + k);
		if(memcmp(another_enc_eid1 + k,enc_key_seed + 0x10 + (k * 2),0x10)!=0){
			printf("warning! auth2 odd offset %d mismatch!\n", (k/0x10));
		}else{
			hexDump(another_enc_eid1 + k, 0x10);
			hexDump(enc_key_seed+ (k * 2) + 0x10,0x10);
		}
	}
	free(eid1_dec);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa30, %rsp            # imm = 0xA30
leaq 0x57b6(%rip), %rdi      # 0x10eb8
xorl %eax, %eax
movl %eax, %esi
callq 0x9960
movq %rax, %rcx
leaq 0x8eab(%rip), %rax      # 0x145c0
movq %rcx, (%rax)
leaq -0x40(%rbp), %r8
leaq 0x8c7d(%rip), %rdi      # 0x143a0
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x9a80
leaq -0x40(%rbp), %rsi
leaq 0x5955(%rip), %rdi      # 0x11092
movl $0x40, %edx
callq 0x9a20
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x384(%rbp)
cmpl $0x100, -0x384(%rbp)    # imm = 0x100
jge 0xb7d6
leaq -0x60(%rbp), %rsi
leaq -0x5b8(%rbp), %rdi
movl $0x100, %edx            # imm = 0x100
callq 0x1250
movslq -0x384(%rbp), %rax
leaq 0x8980(%rip), %rdx      # 0x14120
addq %rax, %rdx
leaq -0x160(%rbp), %rcx
movslq -0x384(%rbp), %rax
addq %rax, %rcx
leaq -0x5b8(%rbp), %rdi
movl $0x1, %esi
callq 0x1be0
movl -0x384(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x384(%rbp)
jmp 0xb771
leaq -0x160(%rbp), %rsi
leaq 0x58bb(%rip), %rdi      # 0x1109f
movl $0x100, %edx            # imm = 0x100
callq 0x9a20
leaq 0x5745(%rip), %rdi      # 0x10f3a
xorl %eax, %eax
movl %eax, %esi
callq 0x9960
movq %rax, -0x168(%rbp)
movl $0x0, -0x5bc(%rbp)
cmpl $0x100, -0x5bc(%rbp)    # imm = 0x100
jge 0xb97f
movl -0x5bc(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
cmpl $0x0, %edx
jne 0xb969
movl -0x5bc(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
shll $0x4, %eax
movslq %eax, %rcx
leaq 0x89cf(%rip), %rax      # 0x14220
movq (%rax,%rcx), %rdx
movq %rdx, -0x180(%rbp)
movq 0x8(%rax,%rcx), %rax
movq %rax, -0x178(%rbp)
leaq -0x180(%rbp), %rsi
leaq -0x7f0(%rbp), %rdi
movl $0x80, %edx
callq 0x1250
movq -0x168(%rbp), %rax
addq $0x10, %rax
movq %rax, -0xa30(%rbp)
movl -0x5bc(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movq -0xa30(%rbp), %rdx
shll $0x4, %eax
cltq
addq %rax, %rdx
leaq -0x280(%rbp), %rcx
movslq -0x5bc(%rbp), %rax
addq %rax, %rcx
leaq -0x7f0(%rbp), %rdi
movl $0x1, %esi
callq 0x1be0
leaq -0x280(%rbp), %rax
movslq -0x5bc(%rbp), %rcx
addq %rcx, %rax
leaq -0x160(%rbp), %rcx
movslq -0x5bc(%rbp), %rdx
addq %rdx, %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 0xb931
movl -0x5bc(%rbp), %eax
movl $0x20, %ecx
cltd
idivl %ecx
movl %eax, %esi
leaq 0x578b(%rip), %rdi      # 0x110b3
movb $0x0, %al
callq 0x1080
jmp 0xb967
leaq -0x280(%rbp), %rdi
movslq -0x5bc(%rbp), %rax
addq %rax, %rdi
movl $0x10, %esi
callq 0xb670
leaq -0x160(%rbp), %rdi
movslq -0x5bc(%rbp), %rax
addq %rax, %rdi
movl $0x10, %esi
callq 0xb670
jmp 0xb969
jmp 0xb96b
movl -0x5bc(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x5bc(%rbp)
jmp 0xb80f
movq -0x168(%rbp), %rdi
callq 0x1030
leaq 0x55a8(%rip), %rdi      # 0x10f3a
xorl %eax, %eax
movl %eax, %esi
callq 0x9960
movq %rax, -0x168(%rbp)
movl $0x0, -0x7f4(%rbp)
cmpl $0x80, -0x7f4(%rbp)
jge 0xbaee
movslq -0x7f4(%rbp), %rcx
leaq 0x8856(%rip), %rax      # 0x14220
movq (%rax,%rcx), %rdx
movq %rdx, -0x180(%rbp)
movq 0x8(%rax,%rcx), %rax
movq %rax, -0x178(%rbp)
leaq -0x180(%rbp), %rsi
leaq -0xa28(%rbp), %rdi
movl $0x80, %edx
callq 0x1250
movq -0x168(%rbp), %rdx
addq $0x90, %rdx
movslq -0x7f4(%rbp), %rax
addq %rax, %rdx
leaq -0x380(%rbp), %rcx
movslq -0x7f4(%rbp), %rax
addq %rax, %rcx
leaq -0xa28(%rbp), %rdi
movl $0x1, %esi
callq 0x1be0
leaq -0x380(%rbp), %rax
movslq -0x7f4(%rbp), %rcx
addq %rcx, %rax
leaq -0x160(%rbp), %rcx
addq $0x10, %rcx
movl -0x7f4(%rbp), %edx
shll %edx
movslq %edx, %rdx
addq %rdx, %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 0xba9b
movl -0x7f4(%rbp), %eax
movl $0x10, %ecx
cltd
idivl %ecx
movl %eax, %esi
leaq 0x564f(%rip), %rdi      # 0x110e1
movb $0x0, %al
callq 0x1080
jmp 0xbad8
leaq -0x380(%rbp), %rdi
movslq -0x7f4(%rbp), %rax
addq %rax, %rdi
movl $0x10, %esi
callq 0xb670
leaq -0x160(%rbp), %rdi
movl -0x7f4(%rbp), %eax
shll %eax
cltq
addq %rax, %rdi
addq $0x10, %rdi
movl $0x10, %esi
callq 0xb670
jmp 0xbada
movl -0x7f4(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x7f4(%rbp)
jmp 0xb9ac
movq -0x168(%rbp), %rdi
callq 0x1030
addq $0xa30, %rsp            # imm = 0xA30
popq %rbp
retq
nopw %cs:(%rax,%rax)