// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x5b8, %rsp            # imm = 0x5B8
leaq 0x55d8(%rip), %rdi      # 0xbf00
xorl %r15d, %r15d
xorl %esi, %esi
callq 0x5437
leaq 0x8c97(%rip), %rcx      # 0xf5d0
movq %rax, (%rcx)
leaq 0x8a6d(%rip), %rdi      # 0xf3b0
leaq 0x140(%rsp), %rbx
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x54fc
leaq 0x577a(%rip), %rdi      # 0xc0da
pushq $0x40
popq %rdx
movq %rbx, %rsi
callq 0x54bc
movdqa 0x20(%rbx), %xmm0
movdqa 0x30(%rbx), %xmm1
leaq 0x20(%rsp), %rbx
movdqa %xmm1, 0x10(%rbx)
movdqa %xmm0, (%rbx)
leaq 0x180(%rsp), %r14
leaq 0x879e(%rip), %r12      # 0xf130
pushq $0x1
popq %rbp
cmpq $0x100, %r15            # imm = 0x100
jae 0x69ca
movq %r14, %rdi
movq %rbx, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x126c
leaq (%r15,%r12), %rdx
leaq (%rsp,%r15), %rcx
addq $0x40, %rcx
movq %r14, %rdi
movl %ebp, %esi
callq 0x169e
addq $0x10, %r15
jmp 0x6995
leaq 0x5716(%rip), %rdi      # 0xc0e7
leaq 0x40(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x54bc
leaq 0x559b(%rip), %rdi      # 0xbf82
xorl %esi, %esi
callq 0x5437
movq %rax, %rbx
pushq $0x10
popq %r12
leaq 0x180(%rsp), %r15
leaq 0x10(%rsp), %r14
xorl %ebp, %ebp
movq %rax, 0x8(%rsp)
cmpq $0x100, %rbp            # imm = 0x100
jae 0x6acf
testb $0x10, %bpl
jne 0x6ac2
leaq 0x8809(%rip), %rax      # 0xf230
movups -0x10(%rax,%r12), %xmm0
movaps %xmm0, 0x10(%rsp)
movq %r15, %rdi
movq %r14, %rsi
movl $0x80, %edx
callq 0x126c
leaq (%rbx,%r12), %rdx
leaq (%rsp,%rbp), %r13
addq $0x4b0, %r13            # imm = 0x4B0
movq %r15, %rdi
pushq $0x1
popq %rsi
movq %r13, %rcx
callq 0x169e
movdqu 0x40(%rsp,%rbp), %xmm0
movdqu (%r13), %xmm1
pcmpeqb %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6a8f
movl %ebp, %esi
shrl $0x5, %esi
leaq 0x5675(%rip), %rdi      # 0xc0fb
xorl %eax, %eax
callq 0x1090
jmp 0x6ac2
movq %r15, %rbx
movq %r14, %r15
leaq (%rsp,%rbp), %r14
addq $0x40, %r14
movq %r13, %rdi
pushq $0x10
popq %r13
movq %r13, %rsi
callq 0x68ab
movq %r14, %rdi
movq %r15, %r14
movq %rbx, %r15
movq 0x8(%rsp), %rbx
movq %r13, %rsi
callq 0x68ab
addq $0x10, %rbp
addq $0x8, %r12
jmp 0x6a09
movq %rbx, %rdi
callq 0x1030
leaq 0x54a4(%rip), %rdi      # 0xbf82
xorl %esi, %esi
callq 0x5437
movq %rax, %r15
leaq 0x50(%rsp), %r14
movq %rax, 0x8(%rsp)
addq $0x90, %r15
leaq 0x3b0(%rsp), %r12
leaq 0x180(%rsp), %r13
xorl %ebp, %ebp
xorl %ebx, %ebx
cmpq $0x80, %rbx
jae 0x6bb2
leaq 0x870f(%rip), %rax      # 0xf230
movaps (%rax,%rbx), %xmm0
movaps %xmm0, 0x10(%rsp)
movq %r13, %rdi
leaq 0x10(%rsp), %rsi
movl $0x80, %edx
callq 0x126c
movq %r13, %rdi
pushq $0x1
popq %rsi
movq %r15, %rdx
movq %r12, %rcx
callq 0x169e
movdqu (%r14), %xmm0
movdqu (%r12), %xmm1
pcmpeqb %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6b79
leaq 0x55bb(%rip), %rdi      # 0xc129
movl %ebp, %esi
xorl %eax, %eax
callq 0x1090
jmp 0x6b9b
movq %r12, %rdi
pushq $0x10
popq %r13
movq %r13, %rsi
callq 0x68ab
movq %r14, %rdi
movq %r13, %rsi
leaq 0x180(%rsp), %r13
callq 0x68ab
addq $0x10, %rbx
incl %ebp
addq $0x20, %r14
addq $0x10, %r12
addq $0x10, %r15
jmp 0x6b0d
movq 0x8(%rsp), %rdi
callq 0x1030
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq