// File: /workspace/github_cpp_makefile_star2_O1/dguglielmi[P]dlan-linux-software/common/hptools.cpp
// Function: HomePlugAvTools::DeviceAccessKeyFromSecurityID(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned char*)
void HomePlugAvTools::DeviceAccessKeyFromSecurityID(
  const std::string& strSecurityID,
  unsigned char cDAK[16])
{
  std::string strSaltedSecret = strSecurityID.substr(0, 64) + std::string("\x08\x85\x6D\xAF\x7C\xF5\x81\x85", 8);
  
  sha256_state sha256;
  unsigned char digest[32];
  sha256_init(&sha256);
  sha256_process(&sha256, (const unsigned char*)strSaltedSecret.data(), strSaltedSecret.size());
  sha256_done(&sha256, digest);
  for(int i = 0; i < 999; i++) 
  {
    sha256_init(&sha256);
    sha256_process(&sha256, digest, 32);
    sha256_done(&sha256, digest);
  }
  
  memcpy(cDAK, digest, 16);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
movq %rdi, %rsi
leaq 0x48(%rsp), %r15
movl $0x40, %ecx
movq %r15, %rdi
xorl %edx, %edx
callq 0x4290
leaq 0x30(%rsp), %r14
movq %r14, -0x10(%r14)
movabsq $-0x7a7e0a8350927af8, %rax # imm = 0x8581F57CAF6D8508
movq %rax, (%r14)
movq $0x8, -0x8(%r14)
movb $0x0, 0x8(%r14)
movq (%r15), %rcx
movq 0x8(%r15), %r8
leaq 0x8(%r8), %rax
leaq 0x58(%rsp), %r15
movl $0xf, %edx
cmpq %r15, %rcx
je 0x11247
movq 0x58(%rsp), %rdx
cmpq $0xf, %rax
ja 0x11262
cmpq %rdx, %rax
jbe 0x11262
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x42b0
jmp 0x11274
leaq 0x48(%rsp), %rdi
movl $0x8, %edx
movq %r14, %rsi
callq 0x40e0
leaq 0x10(%rsp), %rdx
movq %rdx, -0x10(%rdx)
movq (%rax), %rsi
leaq 0x10(%rax), %rcx
cmpq %rcx, %rsi
je 0x11297
movq %rsi, (%rsp)
movq (%rcx), %rdx
movq %rdx, 0x10(%rsp)
jmp 0x1129d
movups (%rcx), %xmm0
movups %xmm0, (%rdx)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, 0x8(%rsp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x112d1
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x112e8
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4270
leaq 0x48(%rsp), %r14
movl $0x0, 0x28(%r14)
movq $0x0, (%r14)
movaps 0x53bd(%rip), %xmm0     # 0x166c0
movups %xmm0, 0x8(%r14)
movaps 0x53c1(%rip), %xmm0     # 0x166d0
movups %xmm0, 0x18(%r14)
movq (%rsp), %rsi
movq 0x8(%rsp), %rdx
movq %r14, %rdi
callq 0x11052
leaq 0x20(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x11103
movl $0x3e7, %ebp            # imm = 0x3E7
movl $0x0, 0x70(%rsp)
movq $0x0, 0x48(%rsp)
movaps 0x536e(%rip), %xmm0     # 0x166c0
movups %xmm0, 0x50(%rsp)
movaps 0x5372(%rip), %xmm0     # 0x166d0
movups %xmm0, 0x60(%rsp)
movl $0x20, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x11052
movq %r14, %rdi
movq %r15, %rsi
callq 0x11103
decl %ebp
jne 0x1133a
movaps 0x20(%rsp), %xmm0
movups %xmm0, (%rbx)
leaq 0x10(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x113a5
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x4270
addq $0xb8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r14, %rdi
je 0x113cd
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x4270
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x113e4
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4270
movq %rbx, %rdi
callq 0x43e0