// File: /workspace/github_c_makefile_star2_O3/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 0x6430(%rip), %rdi      # 0xcf10
xorl %r15d, %r15d
xorl %esi, %esi
callq 0x5588
leaq 0x9acf(%rip), %rcx      # 0x105c0
movq %rax, (%rcx)
leaq 0x98a5(%rip), %rdi      # 0x103a0
leaq 0x140(%rsp), %rbx
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x5654
leaq 0x65d2(%rip), %rdi      # 0xd0ea
movq %rbx, %rsi
movl $0x40, %edx
callq 0x5611
movdqa 0x20(%rbx), %xmm0
movdqa 0x30(%rbx), %xmm1
leaq 0x20(%rsp), %rbx
movdqa %xmm1, 0x10(%rbx)
movdqa %xmm0, (%rbx)
leaq 0x180(%rsp), %r14
leaq 0x95d4(%rip), %r12      # 0x10120
movq %r14, %rdi
movq %rbx, %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x125c
leaq (%r15,%r12), %rdx
leaq (%rsp,%r15), %rcx
addq $0x40, %rcx
movq %r14, %rdi
movl $0x1, %esi
callq 0x16d4
leaq 0x10(%r15), %rax
cmpq $0xf0, %r15
movq %rax, %r15
jb 0x6b4c
leaq 0x656b(%rip), %rdi      # 0xd0f7
leaq 0x40(%rsp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x5611
leaq 0x63f0(%rip), %rdi      # 0xcf92
xorl %esi, %esi
callq 0x5588
movq %rax, %rbx
movl $0x10, %r12d
leaq 0x180(%rsp), %r14
movq %rsp, %rbp
xorl %r15d, %r15d
movq %rax, 0x18(%rsp)
testb $0x10, %r15b
jne 0x6c74
leaq 0x964a(%rip), %rax      # 0x10220
movups -0x10(%rax,%r12), %xmm0
movaps %xmm0, (%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movl $0x80, %edx
callq 0x125c
leaq (%rbx,%r12), %rdx
leaq (%rsp,%r15), %r13
addq $0x4b0, %r13            # imm = 0x4B0
movq %r14, %rdi
movl $0x1, %esi
movq %r13, %rcx
callq 0x16d4
movdqu (%r13), %xmm0
movdqu 0x40(%rsp,%r15), %xmm1
pcmpeqb %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6c41
movl %r15d, %esi
shrl $0x5, %esi
leaq 0x64d3(%rip), %rdi      # 0xd10b
xorl %eax, %eax
callq 0x1090
jmp 0x6c74
movq %r14, %rbx
movq %rbp, %r14
leaq (%rsp,%r15), %rbp
addq $0x40, %rbp
movl $0x10, %esi
movq %r13, %rdi
callq 0x6a62
movl $0x10, %esi
movq %rbp, %rdi
movq %r14, %rbp
movq %rbx, %r14
movq 0x18(%rsp), %rbx
callq 0x6a62
leaq 0x10(%r15), %rax
addq $0x8, %r12
cmpq $0xf0, %r15
movq %rax, %r15
jb 0x6bc5
movq %rbx, %rdi
callq 0x1030
leaq 0x62f7(%rip), %rdi      # 0xcf92
xorl %esi, %esi
callq 0x5588
movq %rax, %rbx
leaq 0x50(%rsp), %r14
leaq 0x180(%rsp), %r15
xorl %ebp, %ebp
xorl %r12d, %r12d
leaq 0x9562(%rip), %rax      # 0x10220
movaps (%rax,%r12), %xmm0
movaps %xmm0, (%rsp)
movq %r15, %rdi
movq %rsp, %rsi
movl $0x80, %edx
callq 0x125c
leaq (%rbx,%r12), %rdx
addq $0x90, %rdx
leaq 0x3b0(%rsp,%r12), %r13
movq %r15, %rdi
movl $0x1, %esi
movq %r13, %rcx
callq 0x16d4
movdqu (%r13), %xmm0
movdqu (%r14), %xmm1
pcmpeqb %xmm0, %xmm1
pmovmskb %xmm1, %eax
cmpl $0xffff, %eax           # imm = 0xFFFF
je 0x6d26
leaq 0x641e(%rip), %rdi      # 0xd139
movl %ebp, %esi
xorl %eax, %eax
callq 0x1090
jmp 0x6d40
movl $0x10, %esi
movq %r13, %rdi
callq 0x6a62
movl $0x10, %esi
movq %r14, %rdi
callq 0x6a62
leaq 0x10(%r12), %rax
addq $0x20, %r14
incl %ebp
cmpq $0x70, %r12
movq %rax, %r12
jb 0x6cb7
movq %rbx, %rdi
callq 0x1030
addq $0x5b8, %rsp            # imm = 0x5B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq